Skip to content

Commit

Permalink
make sure conditions object resets if mjd is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
yoachim committed Aug 25, 2023
1 parent 25e95c1 commit a6f1f3f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 17 additions & 10 deletions rubin_sim/scheduler/features/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(
mjd_start=59853.5,
season_offset=None,
sun_ra_start=None,
mjd=None,
):
"""
Parameters
Expand All @@ -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)
-----------
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion rubin_sim/scheduler/model_observatory/model_observatory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion rubin_sim/scheduler/sim_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 9 additions & 4 deletions rubin_sim/scheduler/surveys/long_gap_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])]
Expand Down
1 change: 1 addition & 0 deletions rubin_sim/scheduler/surveys/surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions tests/maf/test_stackers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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,
_alt_az_pa_from_ra_dec,
_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")

Expand Down

0 comments on commit a6f1f3f

Please sign in to comment.