Skip to content

Possible incorrect date for Astronoby::Sun.observation_events.setting_time #97

Closed

Description

@trevorturk

Description

Possible incorrect date for Astronoby::Sun.observation_events.setting_time.

Reproduction Steps

lat = 41.87
lon = -87.62
zone = "America/Chicago" # ActiveSupport::TimeZone
time = "2024-09-11 16:36:50 -0500" # ActiveSupport::TimeWithZone

observer = Astronoby::Observer.new(
  latitude: Astronoby::Angle.from_degrees(lat),
  longitude: Astronoby::Angle.from_degrees(lon),
)

sun = Astronoby::Sun.new(time: time)
events = sun.observation_events(observer: observer)

events.rising_time # 2024-09-11 11:27:07 UTC
events.setting_time # 2024-09-11 00:07:35 UTC (notice this time is before the rising time)

events.rising_time.in_time_zone(zone) # 2024-09-11 06:27:07 -0500
events.setting_time.in_time_zone(zone) # 2024-09-10 19:07:35 -0500 (notice this is the correct time, but one day too early)

Expected behavior

Expect setting_time to be after rising_time on the same date. I tested also with passing in a UTC time, but I believe (from a quick search in the issues) that I should be able to pass in a ActiveSupport::TimeWithZone?

Actual behavior

Actual setting_time is before rising_time on the day before (but is the correct hour/min/sec).

System configuration

astronoby version: astronoby (0.5.0)
ruby version: ruby 3.3.5

Apologies if I'm misunderstanding something -- thank you for the help!

Activity

  1. rhannequin commented on Sep 13, 2024

    @rhannequin
    Owner

    Thank you for raising this.
    I was afraid this could come up, I do understand what is going on.

    The model I am using (Astronomical Algorithms from Jean Meeus) is based on a universal time (dynamic time actually, but let's not make this conversation more complex) and a date.
    It is quite counter intuitive, but we could see it this way: a day is not the same period of time depending on the time zone. For someone living in Chicago, September 11th starts on 2024-09-11 05:00:00 UTC and ends on 2024-09-12 05:00:00 UTC. The algorithm takes the "universal time" definition of the day. We could say something like "if someone using a UTC clock would like to know when the sun rises and when it sets in Chicago on September 11th, we are looking for the rising and setting events that will happen between 2024-09-11 00:00:00 UTC and 2024-09-12 00:00:00 UTC".

    Given this, during this period of time, in Chicago, the sun will rise at 2024-09-11 11:27:07 UTC and will set at 2024-09-11 00:07:35 UTC. Setting time is before rising time because during this period of time, the setting event will happen before in time, but it looks weird in our human definition of a day.

    This is the explanation, but I understand it is not convenient when trying to use the data in a project where the civil date matters more than the universal date.

    At the moment, I don't have the knowledge to change the algorithm to return events for the local date, but I can try. The book mentions this issue and suggests a solution, but I need to see how I could implement it.

    As a temporary workaround, you could calculate the events for the day n and n+1. This is not convenient are require internal logic, but it will work.

    I need to think more about the implications if I work towards a change to make these events associated with a civil date, because the whole library is currently more associated with universal time. Most of the events are calculated as "universal" events, where time is only converted into a different time zone for the observer.
    For example, the IMCCE behaves the same way by default, but with the correct civil date when using a different time scale:

    I tested also with passing in a UTC time, but I believe (from a quick search in the issues) that I should be able to pass in a ActiveSupport::TimeWithZone?

    astronoby uses Time, which means anything that behaves like a Time (I believe ActiveSupport::TimeWithZone does) should work, but I haven't tested it yet personally. Please let me know if you encounter any problem with it, I'll investigate.

  2. trevorturk commented on Sep 13, 2024

    @trevorturk
    Author

    Hmm, I see. I was attempting another workaround (but perhaps I did something wrong) where I would only pass in UTC times, and then only convert them into the local time (in my case UTC + 5) after the results came back. Is that an option, or would that perhaps have the same issue? I'm curious how the IMCCE works in this regard, too. Perhaps they run the calculation but then adjust for the timescale after the fact? FWIW it would be acceptable for me to take only the "time" portion (and not the "date" portion) of any returned results and then reconnect the time + current date for the complete result, if you see what I mean. Perhaps that's another possible workaround here? (Sorry I'm just brainstorming...)

  3. trevorturk commented on Nov 8, 2024

    @trevorturk
    Author

    FWIW I'm seeing the same with moon rise/set, and I'm working around it by lazily calculating +/- 1 day and picking the one that lands on "today" in that time zone. This seems a bit "incorrect" mathematically, but (as discussed elsewhere) seems to be what people typically expect. For example, this override "fixes" the moonrise for 2024-11-05T19:14:01-06:00 which aligns with the weather data sources I'm using (Apple Weather, AerisWeather, OpenWeather, etc...) so I think it's best for my use case. It might be worth considering making this an option or alternate method in the future, but (as discussed elsewhere) I'm not sure where Astronoby wants to draw the line. Perhaps we should consider documenting these kinds of things and/or offering code examples if it comes up much. In this case, FWIW, SunCalc has the same "problem" so it appears that at least in terms of weather data / sun-moon data sources, it's common to work around it.

  4. rhannequin commented on Nov 8, 2024

    @rhannequin
    Owner

    @trevorturk It's funny because I finally took the time today to have a look at this problem and I may have found a solution: #105

    But I need to run more tests and compare with multiple data sources because my results are not that great, I don't know if I'm a bit off on the setting time for the dates I selected, or if my fix is wrong (and I really need to compute the n+/-1 date).

    I already noticed differences of several minutes on different data sources, because the rising and setting times are very hard to predict exactly as they highly depend on the geographical aspects of the observer and the temperature and air pressure. The fixed configuration may vary from one source to another, which explains why we don't have results with an error margin of a few minutes.

    But I still aim to be as close as possible to sources I fully trust, like the IMCCE, so I need to run more tests before I move my PR from Draft to Ready of review.

  5. trevorturk commented on Nov 8, 2024

    @trevorturk
    Author

    #105 looks really interesting! It's kind of related to the time zone issue I was mentioning just now in #101 as well, where I think it's fine for Astronoby to operate with simple UTC times, but the user should be able to convert those times into the local time for the location? I'm not sure if we should then be passing a UTC offset into Astronoby, but I'm sure I'm missing some important details there, so apologies if I'm not helping on that front!

  6. rhannequin commented on Nov 22, 2024

    @rhannequin
    Owner

    This should be fixed now using main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions