sffjunkie / astral

Python calculations for the position of the sun and moon.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Astral fails to correctly calculate sunrise and sunset times in unique test case of Norwegian Winter

FMKaiba opened this issue · comments

import pytz
import datetime

import astral
import astral.location

tz='UTC'
utc_now = datetime.datetime(2010, 1, 1, tzinfo=pytz.timezone(tz))

obs = astral.Observer(latitude=66.5, longitude=162.4, elevation=0.0)

print(utc_now) # datetime.datetime(2010, 1, 1)

sunrise = astral.sun.sunrise(obs,utc_now)
sunset = astral.sun.sunset(obs,utc_now)
print(sunrise)
print(sunset)

utc_now = sunrise + datetime.timedelta(seconds=15)
print(utc_now) # Adjusted time to just after sunrise

print(astral.sun.sunrise(obs,utc_now)) #Completely different sunrise time, day before, why not same as above?
print(astral.sun.sunset(obs,utc_now)) #Same issue... getting time for previous day.

Produces:
2010-01-01 00:00:00+00:00
2009-12-31 23:53:51.078028+00:00 <==Sunrise Time
2010-01-01 02:33:44.399907+00:00 <==Sunset Time
2009-12-31 23:54:06.078028+00:00 <== Adjust time to Sunrise Time + 15 seconds
2009-12-30 23:55:49.136837+00:00 <== New Sunrise Time
2009-12-31 02:30:47.610544+00:00 <== New Sunset Time

Same here.
This is exactly what I came to report.
While troubleshooting I've made a debug print log:

 # this is when the function was called
2021-04-10 01:00:00.015625, light schedule updated 
# this is the resulting data .. for the day before.
dawn:  2021-04-09 02:59:08.007731
dusk:  2021-04-09 20:37:25.089938

Using Astral 2.2

It seems the solution can be achieved by edit this line of code

astral/src/astral/sun.py

Lines 363 to 370 in 96496f0

delta = -observer.longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720 + timeDiff - eq_of_time(t)
td = minutes_to_timedelta(timeUTC)
dt = datetime.datetime(date.year, date.month, date.day) + td
dt = pytz.utc.localize(dt) # pylint: disable=E1120
return dt

@yasirroni thanks, but what is that ? - it's not committed, not a PR? - will it make into a release soon?

That is the code that astral use that we need to edit and make a PR to solve local timezone bug changing day.

See suntime package. Basically they reproduce the same bug and I already fix that on suntime. Haven't got time to fix here too. I just pin point the problem for those willing to solve it.

@yasirroni unfortunatly, I can not use suntime due to SatAgro/suntime#18

Have you try my fork on test-pypi? Go to test-pypi and search suntime-yasirroni

@yasirroni thanks, but I try to stick to libraries from pip or so, to prevent wild searching and retracing what I did when something needs to run on another distro/hardware x years later.

@yasirroni thanks, but I try to stick to libraries from pip or so, to prevent wild searching and retracing what I did when something needs to run on another distro/hardware x years later.

Then, you can fork it and publish it on pypi yourself. Easy, hahaa

It seems the solution can be achieved by edit this line of code

astral/src/astral/sun.py

Lines 363 to 370 in 96496f0

delta = -observer.longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720 + timeDiff - eq_of_time(t)
td = minutes_to_timedelta(timeUTC)
dt = datetime.datetime(date.year, date.month, date.day) + td
dt = pytz.utc.localize(dt) # pylint: disable=E1120
return dt

Thanks I had that problem today, astral was showing error, "Unable to find a dawn time on the date specified". After applying your correction it works fine. Thank you very much.

I did one correction though : "eq_of_time(t)" I replaced t with jc for jc = julianday_to_juliancentury(jd + adjustment)