Skip to content
Snippets Groups Projects
Commit e240a579 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-2865: even more exception handling around astropy bug(s)

parent 0933aa8c
No related branches found
No related tags found
No related merge requests found
......@@ -165,19 +165,11 @@ def timestamps_and_stations_to_sun_rise_and_set(timestamps: tuple, stations: tup
observer = create_astroplan_observer_for_station(station)
for timestamp in timestamps:
# TODO: check if ALL stations/timestamps are in DB once.
# Do it now in a loop for each station/timestamp, because we might missing something
try:
obj = StationTimeline.objects.get(station_name=station, timestamp=datetime.date(timestamp))
station_timestamp_found = True
except ObjectDoesNotExist:
station_timestamp_found = False
if station_timestamp_found:
logger.debug("StationTimeline data found in DB for station=%s, timestamp=%s" % (station,timestamp))
sunrise_dict = {"start": obj.sunrise_start, "end": obj.sunrise_end}
sunset_dict = {"start": obj.sunset_start, "end": obj.sunset_end}
else:
except ObjectDoesNotExist:
# Not found in database so calculate it
sunrise_dict, sunset_dict = calculate_and_get_sunrise_and_sunset_of_observer_day(observer, timestamp, angle_to_horizon, n_grid_points=n_grid_points)
......@@ -193,12 +185,11 @@ def timestamps_and_stations_to_sun_rise_and_set(timestamps: tuple, stations: tup
sunset_end=sunset_dict['end'])
logger.debug("StationTimeline %s calculated and created for station=%s, timestamp=%s" %
(station_timeline, station, timestamp))
except IntegrityError as e:
if 'unique_station_time_line' in str(e):
logger.info("StationTimeline with station=%s and timestamp=%s already exists, "
"so not added to database", station, timestamp)
else:
raise
except IntegrityError:
# already exists in db
pass
except Exception as e:
logger.error("timestamps_and_stations_to_sun_rise_and_set: %s", str(e))
# Create overall result
return_dict.setdefault(station, {})
......@@ -299,8 +290,8 @@ def calculate_and_get_sunrise_and_sunset_of_observer_day(observer, timestamp: da
n_grid_points=n_grid_points)
try:
sunrise_dict['start'] = round_to_second_precision(sunrise_start.to_datetime())
except ValueError:
# sun never rises
except (ValueError, AttributeError):
# sun never rises / astropy bug
sun_body = astropy.coordinates.get_body("sun", Time(noon), observer.location)
if observer.target_is_up(target=sun_body, time=Time(noon), horizon=-angle_to_horizon):
sunrise_dict['start'] = prev_midnight
......@@ -311,8 +302,8 @@ def calculate_and_get_sunrise_and_sunset_of_observer_day(observer, timestamp: da
n_grid_points=n_grid_points)
try:
sunrise_dict['end'] = round_to_second_precision(sunrise_end.to_datetime())
except ValueError:
# sun never rises
except (ValueError, AttributeError):
# sun never rises / astropy bug
sun_body = astropy.coordinates.get_body("sun", Time(sunrise_dict['start']), observer.location)
if observer.target_is_up(target=sun_body, time=Time(sunrise_dict['start']), horizon=angle_to_horizon):
sunrise_dict['end'] = prev_midnight
......@@ -323,8 +314,8 @@ def calculate_and_get_sunrise_and_sunset_of_observer_day(observer, timestamp: da
n_grid_points=n_grid_points)
try:
sunset_dict['start'] = round_to_second_precision(sunset_start.to_datetime())
except ValueError:
# sun never rises
except (ValueError, AttributeError):
# sun never rises / astropy bug
sun_body = astropy.coordinates.get_body("sun", Time(noon), observer.location)
if observer.target_is_up(target=sun_body, time=Time(noon), horizon=angle_to_horizon):
sunset_dict['start'] = next_midnight
......@@ -335,8 +326,8 @@ def calculate_and_get_sunrise_and_sunset_of_observer_day(observer, timestamp: da
n_grid_points=n_grid_points)
try:
sunset_dict['end'] = round_to_second_precision(sunset_end.to_datetime())
except ValueError:
# sun never rises
except (ValueError, AttributeError):
# sun never rises / astropy bug
sun_body = astropy.coordinates.get_body("sun", Time(noon), observer.location)
if observer.target_is_up(target=sun_body, time=Time(noon), horizon=-angle_to_horizon):
sunset_dict['end'] = next_midnight
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment