Sun incorrectly reported to stay below horizon

Bug #1586829 reported by Kenneth Henderick
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
astral
In Progress
High
Simon Kennedy

Bug Description

At my location the run is incorrectly reported to stay below the horizon, something that doesn't happen here:

AstralError Traceback (most recent call last)
<ipython-input-5-b7a1432d534a> in <module>()
----> 1 l.sun(local=True)

/usr/lib/python2.7/site-packages/astral.py in sun(self, date, local)
    707 date = datetime.date.today()
    708
--> 709 sun = self.astral.sun_utc(date, self.latitude, self.longitude)
    710
    711 if local:

/usr/lib/python2.7/site-packages/astral.py in sun_utc(self, date, latitude, longitude)
   1510 """
   1511
-> 1512 dawn = self.dawn_utc(date, latitude, longitude)
   1513 sunrise = self.sunrise_utc(date, latitude, longitude)
   1514 noon = self.solar_noon_utc(date, longitude)

/usr/lib/python2.7/site-packages/astral.py in dawn_utc(self, date, latitude, longitude, depression)
   1547 return self._calc_time(depression, SUN_RISING, date, latitude, longitude)
   1548 except Exception as ex:
-> 1549 raise AstralError(('Sun remains below the horizon on this day, '
   1550 'at this location.'))
   1551

AstralError: Sun remains below the horizon on this day, at this location.

If I patch the _calc_time method to re-raise the exception, I get this:

...
/usr/lib/python2.7/site-packages/astral.py in _calc_time(self, depression, direction, date, latitude, longitude)
   2326 solarDec = self._sun_declination(newt)
   2327
-> 2328 hourangle = self._hour_angle(latitude, solarDec, depression)
   2329 if direction == SUN_SETTING:
   2330 hourangle = -hourangle

/usr/lib/python2.7/site-packages/astral.py in _hour_angle(self, latitude, declination, depression)
   2299 h = (n / d) - t
   2300
-> 2301 HA = acos(h)
   2302 return HA
   2303

ValueError: math domain error

It only happens on solar_depression of 18. Code snippet:

import astral
a = astral.Astral(astral.GoogleGeocoder)
l = a['Ghent,Belgium']
l.solar_depression = 18
l.sun(local=True)

Changed in astral:
status: New → Triaged
importance: Undecided → High
assignee: nobody → Simon Kennedy (sffjunkie)
Revision history for this message
Simon Kennedy (sffjunkie) wrote :

That there is an error is correct but not for the reason given in the message. It is occurring for a depression of 18 degrees because the sun never reaches that far below the horizon.

Revision history for this message
Kenneth Henderick (kenneth-w) wrote :

Aha, interesting. As I'm trying to figure out in which "band" (day, civil twilight, nautical twilight, ...) the sun is, I can most likely figure it out by checking out where exceptions are raised and where not. However, throwing a correct message (or even different exceptions, all inheriting from the same base exception for backwards compatibility) would be easier.

Changed in astral:
status: Triaged → In Progress
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.