0

I have a problem I dont know how to solve. I have a Julian day number (integer) and a distance value (float) and I have another number which is the actually julian day of interest (integer). I want to take the actual julian day and fit it into the julian day ranges (i.e Julian day 1-14) so that I can determine the Distance value for that specific julian day. So my Julian day 10, fits in the julian day range or 1 - 14, making the distance = 0.9832. Any help on this would be greatly appreciated. All I ve tried so far is to use if else but I dont think this will work at all. Your ideas are welcome. (let me know if I not clear what I m trying to do!)

Julian Day Distance 1 0.9832 15 0.9836 32 0.9878 46 0.9878 60 99.0900

Wooble
  • 87,717
  • 12
  • 108
  • 131
Simon
  • 25
  • 1
  • 8
  • This isn't very clear. Is your "Julian Day Distance" line meant to map distance values based on the integer difference between the two day numbers? EG 14 or less all get distance 0.9832, 15-31 get 0.9836, etc? – Peter DeGlopper Jul 11 '13 at 03:18
  • Yes thats right. 15-31 = 0.9836 etc. The julian day could be any day and needs to be able to select a distance based on the date range it falls in. – Simon Jul 11 '13 at 04:21
  • In that case, @antak has the answer that is more or less what I would give. You could start at either end of the `day_distances` list depending on which situation you think is more likely (small values or large values) and omit the `sorted` call since the hardcoded `day_distances` is already sorted. And depending on whether you need to cover both the negative and positive cases, compare to `abs(day_number - day_of_interest)` or whatever. – Peter DeGlopper Jul 11 '13 at 04:27

2 Answers2

1

Assuming you're expecting good performance across multiple lookups, what you're trying to do can be achieved using the binary search algorithm.

In fact, just searching for "binary search" found a similar question.

If performance isn't an issue, a simple loop through your day-distances will work.

Either way, I'd start of by putting the day-distances into a sequential data structure:

day_distances = [( 1,  0.9832),
                 (15,  0.9836),
                 (32,  0.9878),
                 (46,  0.9878),
                 (60, 99.0900),
                ]

Now, if it's a simple loop I'm using (and I don't care at all about inefficiency) it'll look something like:

find_day = 10
found_distance = None

for day, distance in reversed(sorted(day_distances)):
    if day <= find_day:
        found_distance = distance
        break
Community
  • 1
  • 1
antak
  • 19,481
  • 9
  • 72
  • 80
  • Thanks I think that answered it perfectly! I m not 100 sure how its doing it but it works non the less. Cheers!! – Simon Jul 11 '13 at 04:37
  • `sorted` has a `reverse` argument. – Blender Jul 11 '13 at 07:14
  • @Blender thanks, didn't know about that. however, *sorting* is part of the data definition (and probably doesn't belong there) while `reversed` is part of the algorithm. my example is sloppy design to begin with, but changing it to `sorted(..., reverse=True)` might just be premature optimization (in design). – antak Jul 12 '13 at 02:03
0

"If" definitely works. For example,

actual_day = 10
if a >=1 and a<=14:
    distance =  0.9832
elif a>=15 and a<=31:
    distance =  0.9836
....

If the Julian day number (integer) is strictly increasing like 1, 15, 32, 46 etc, it would be easier to use "if" from greatest to smallest:

actual_day = 10
if a >= 60:
    distance = 99.0900
elif a >= 46:
    distance = 0.9878
....
Sheng
  • 3,467
  • 1
  • 17
  • 21