From a6f1f3fa96b082ca6caac71f2ad8f4178b0aa348 Mon Sep 17 00:00:00 2001 From: Peter Yoachim Date: Fri, 25 Aug 2023 14:45:08 -0700 Subject: [PATCH] make sure conditions object resets if mjd is changed --- .../artificial_structure_calculation.py | 2 +- rubin_sim/scheduler/features/conditions.py | 27 ++++++++++++------- .../model_observatory/model_observatory.py | 2 +- rubin_sim/scheduler/sim_runner.py | 2 +- .../scheduler/surveys/long_gap_survey.py | 13 ++++++--- rubin_sim/scheduler/surveys/surveys.py | 1 + tests/maf/test_stackers.py | 6 ++--- 7 files changed, 33 insertions(+), 20 deletions(-) diff --git a/rubin_sim/maf/maf_contrib/lss_obs_strategy/artificial_structure_calculation.py b/rubin_sim/maf/maf_contrib/lss_obs_strategy/artificial_structure_calculation.py index 39d86ed7b..7bf240ace 100644 --- a/rubin_sim/maf/maf_contrib/lss_obs_strategy/artificial_structure_calculation.py +++ b/rubin_sim/maf/maf_contrib/lss_obs_strategy/artificial_structure_calculation.py @@ -67,8 +67,8 @@ from rubin_sim.maf.maf_contrib.lss_obs_strategy.masking_algorithm_generalized import ( masking_algorithm_generalized, ) -from rubin_sim.maf.metrics import CountMetric as NumObsMetric from rubin_sim.maf.maf_contrib.lss_obs_strategy.save_bundle_data_npz_format import save_bundle_data_npz_format +from rubin_sim.maf.metrics import CountMetric as NumObsMetric def artificial_structure_calculation( diff --git a/rubin_sim/scheduler/features/conditions.py b/rubin_sim/scheduler/features/conditions.py index 18c226d41..c8f021c8a 100644 --- a/rubin_sim/scheduler/features/conditions.py +++ b/rubin_sim/scheduler/features/conditions.py @@ -45,6 +45,7 @@ def __init__( mjd_start=59853.5, season_offset=None, sun_ra_start=None, + mjd=None, ): """ Parameters @@ -61,6 +62,9 @@ def __init__( season_offset : np.array A HEALpix array that specifies the day offset when computing the season for each HEALpix. sun_ra_start : float (None) + The RA of the sun at the start of the survey (radians) + mjd : float + The current MJD. Attributes (Set on init) ----------- @@ -212,6 +216,12 @@ def __init__( # The RA, Dec grid we are using self.ra, self.dec = _hpid2_ra_dec(nside, hpids) + self._init_attributes() + self.mjd = mjd + + def _init_attributes(self): + """Initialize all the attributes""" + # Modified Julian Date (day) self._mjd = None # Altitude and azimuth. Dict with degrees and radians @@ -290,6 +300,11 @@ def __init__( self.season_length = 365.25 self.season_floor = True + # Potential attributes that get computed + self._solar_elongation = None + self._az_to_sun = None + self._az_to_antisun = None + @property def lmst(self): if self._lmst is None: @@ -377,17 +392,9 @@ def mjd(self): @mjd.setter def mjd(self, value): + # If MJD is changed, everything else is no longer valid, so re-init + self._init_attributes() self._mjd = value - # Set things that need to be recalculated to None - self._az = None - self._alt = None - self._pa = None - self._HA = None - self._lmst = None - self._az_to_sun = None - self._az_to_antisun = None - self._season = None - self._solar_elongation = None @property def skybrightness(self): diff --git a/rubin_sim/scheduler/model_observatory/model_observatory.py b/rubin_sim/scheduler/model_observatory/model_observatory.py index d76c31f5b..a6a6e1b03 100644 --- a/rubin_sim/scheduler/model_observatory/model_observatory.py +++ b/rubin_sim/scheduler/model_observatory/model_observatory.py @@ -360,7 +360,7 @@ def mjd(self): def mjd(self, value): self._mjd = value self.almanac_indx = self.almanac.mjd_indx(value) - self.night = self.almanac.sunsets["night"][self.almanac_indx] + self.night = np.max(self.almanac.sunsets["night"][self.almanac_indx]) def observation_add_data(self, observation): """ diff --git a/rubin_sim/scheduler/sim_runner.py b/rubin_sim/scheduler/sim_runner.py index d0ec25595..0c443bad6 100644 --- a/rubin_sim/scheduler/sim_runner.py +++ b/rubin_sim/scheduler/sim_runner.py @@ -113,7 +113,7 @@ def sim_runner( mjd = observatory.mjd + 0 if verbose: if (mjd - mjd_track) > step: - progress = float(mjd - mjd_start) / mjd_run * 100 + progress = np.max((mjd - mjd_start) / mjd_run * 100) text = "\rprogress = %.2f%%" % progress sys.stdout.write(text) sys.stdout.flush() diff --git a/rubin_sim/scheduler/surveys/long_gap_survey.py b/rubin_sim/scheduler/surveys/long_gap_survey.py index 898f20530..367d75279 100644 --- a/rubin_sim/scheduler/surveys/long_gap_survey.py +++ b/rubin_sim/scheduler/surveys/long_gap_survey.py @@ -99,10 +99,15 @@ def _schedule_obs(self, observations): """ # Only match if we have completed the second of a pair and are in most recent night. - need_to_observe = np.where( - (observations["note"] == self.blob_survey.survey_note + ", b") - & (observations["night"] == np.max(observations["night"])) - )[0] + if np.size(observations) == 1: + need_to_observe = (observations["note"] == self.blob_survey.survey_note + ", b") & ( + observations["night"] == np.max(observations["night"]) + ) + else: + need_to_observe = np.where( + (observations["note"] == self.blob_survey.survey_note + ", b") + & (observations["night"] == np.max(observations["night"])) + )[0] # Set to the proper gap self.gap = self.gaps[np.max(observations["night"])] diff --git a/rubin_sim/scheduler/surveys/surveys.py b/rubin_sim/scheduler/surveys/surveys.py index 9f8ea7521..c2aea5f42 100644 --- a/rubin_sim/scheduler/surveys/surveys.py +++ b/rubin_sim/scheduler/surveys/surveys.py @@ -354,6 +354,7 @@ def calc_reward_function(self, conditions): self.smooth_reward() else: self.reward = -np.inf + return self.reward if self.area_required is not None: max_indices = np.where(self.reward == np.nanmax(self.reward))[0] diff --git a/tests/maf/test_stackers.py b/tests/maf/test_stackers.py index 10f4d7f92..bbae4d3d0 100644 --- a/tests/maf/test_stackers.py +++ b/tests/maf/test_stackers.py @@ -1,11 +1,13 @@ -import unittest import os +import unittest import warnings import matplotlib import numpy as np import rubin_sim.maf.stackers as stackers +from rubin_sim.data import get_data_dir +from rubin_sim.maf import get_sim_data from rubin_sim.utils import ( ObservationMetaData, Site, @@ -13,8 +15,6 @@ _galactic_from_equatorial, calc_lmst_last, ) -from rubin_sim.data import get_data_dir -from rubin_sim.maf import get_sim_data matplotlib.use("Agg")