astral icon indicating copy to clipboard operation
astral copied to clipboard

Please handle polar nights and days .. not just fail.

Open AndKe opened this issue 3 years ago • 5 comments

>>> city = LocationInfo("Tromsø", "Norway", "Europe/Tromsø", 69.66, 18.82)
>>> s = sun(city.observer, date=datetime.date(2021, 7, 16))
Traceback (most recent call last):
 File "/home/andre/.local/lib/python3.8/site-packages/astral/sun.py", line 734, in dawn
   return time_of_transit(
 File "/home/andre/.local/lib/python3.8/site-packages/astral/sun.py", line 343, in time_of_transit
   hourangle = hour_angle(
 File "/home/andre/.local/lib/python3.8/site-packages/astral/sun.py", line 243, in hour_angle
   HA = acos(h)
ValueError: math domain error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/andre/.local/lib/python3.8/site-packages/astral/sun.py", line 1150, in sun
   "dawn": dawn(observer, date, dawn_dusk_depression, tzinfo),
 File "/home/andre/.local/lib/python3.8/site-packages/astral/sun.py", line 739, in dawn
   raise ValueError(
ValueError: Sun never reaches 6.0 degrees below the horizon, at this location.
>>> 

Instead of error and all, it would be much nicer to return one additional parameter like s['polar'] "na" or "day" or "night"

AndKe avatar Feb 16 '21 19:02 AndKe

It would make sense to return math.nan in a situation like this.

ldo avatar Feb 17 '21 01:02 ldo

yes, please, anything that is easy to handle, and preferably indicate whatever it's a polar day or night.

AndKe avatar Feb 17 '21 06:02 AndKe

+1, related to this, #74 is also looking for a better way to find this out, because the error also isn't always right: sometimes it says polar day when it is actually polar night.

acrylic-origami avatar Nov 18 '21 20:11 acrylic-origami

+1. I am writing an aurora notification service. I don't want to get notifications during midnight sun, but of course I want them during polar night. Of course it's obvious to me as a human, but I want to write a service that I can let run all the time and forget about. It needs to have different behaviors in these two circumstances, but right now both conditions just throw a ValueError. I guess I could import some second library to figure this out or try to do the math manually, but wouldn't that defeat the purpose of using astral?

@ldo suggested math.nan, but I don't think that's quite right since a datetime isn't a number - how about just None for the missing values, and adding a boolean key to astral.sun.sun like "is_daylight"?

doctorcolossus avatar Dec 27 '22 12:12 doctorcolossus

I just tried this with ephem and it raises either an AlwaysUpError or a NeverUpError, depending on the case. This seems like a good way to handle the problem.

doctorcolossus avatar Dec 27 '22 12:12 doctorcolossus