Skip to content

Commit f858fef

Browse files
committed
Fix astal crashes at high and low latitudes
1 parent 376bfbd commit f858fef

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

suntimes_astral2-2.py

100755100644
+28-2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,31 @@
88
zone=sys.argv[3]
99
l = LocationInfo('wsprep', 'local', zone, lat, lon,)
1010
d = date.today()
11-
sun=sun(l.observer, date=d, tzinfo=zone)
12-
print( str(sun['sunrise'])[11:16] + " " + str(sun['sunset'])[11:16] )
11+
#
12+
# Addition of try: and except * : exception handling for use in high north and south latitudes
13+
# at times when the sun does not dip more than 6 deg below horizon, or rises above 6 deg below
14+
# Gwyn G3ZIL 26 July 2023
15+
#
16+
# Complication is that the same exception is raised whether the sun never sets or the sun
17+
# rises. So look at day of year and which hemisphere in series of four ifs
18+
#
19+
# calculate day of year
20+
today=datetime.datetime.now()
21+
day_of_year=int(today.strftime('%j')) # day of year as integer
22+
23+
try:
24+
sun=sun(l.observer, date=d, tzinfo=zone)
25+
print( str(sun['sunrise'])[11:16] + " " + str(sun['sunset'])[11:16] )
26+
except ValueError as e:
27+
if (('Sun never reaches') or ('Sun is always') in str (e)): # exception raised
28+
if (lat> 60) and (100 <= day_of_year <=260): # Northern hemisphere summer
29+
print('00:00 23:59') # it is light all day
30+
elif (lat >60) and (1 <= day_of_year <=80 or 280 <= day_of_year <366): # Northern hemisphere winter
31+
print('00:00 00:01') # it is dark all day
32+
elif (lat< -60) and (100 <= day_of_year <=260): # Southern hemisphere winter
33+
print('00:00 00:01') # it is dark all day
34+
elif (lat <-60) and (1 <= day_of_year <=80 or 280 <= day_of_year <366): # Southern hemisphere summer
35+
print('00:00 23:59') # it is light all day
36+
else:
37+
print ('Should never get here given latitude and day of year')
38+

0 commit comments

Comments
 (0)