diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/conversions.py b/SAS/TMSS/backend/src/tmss/tmssapp/conversions.py index 2214f6f987c6181537354f9afb0e8871391c101d..65d40dd65aa8bf5b710a3937e186a479ef8e9190 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/conversions.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/conversions.py @@ -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