Skip to content

Commit

Permalink
Merge pull request #353 from lsst/tickets/OPSIM-1067
Browse files Browse the repository at this point in the history
make sure conditions object resets if mjd is changed
  • Loading branch information
yoachim committed Aug 26, 2023
2 parents 25e95c1 + fe0f567 commit 3f6f0fd
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 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
5 changes: 4 additions & 1 deletion rubin_sim/scheduler/detailers/detailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ def __call__(self, observation_list, conditions):

# Find the closest in angular distance of the points that are in band
ang_dist = _angular_separation(az[in_band], alt[in_band], conditions.tel_az, conditions.tel_alt)
good = np.min(np.where(ang_dist == ang_dist.min())[0])
if np.size(ang_dist) == 1:
good = 0
else:
good = np.min(np.where(ang_dist == ang_dist.min())[0])
indx = in_band[good]
result = observation_list[indx:] + observation_list[:indx]
return result
Expand Down
30 changes: 20 additions & 10 deletions rubin_sim/scheduler/features/conditions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = ("Conditions",)

import warnings
from io import StringIO

import healpy as hp
Expand Down Expand Up @@ -45,6 +46,7 @@ def __init__(
mjd_start=59853.5,
season_offset=None,
sun_ra_start=None,
mjd=None,
):
"""
Parameters
Expand All @@ -61,6 +63,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 +217,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 +301,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 +393,11 @@ def mjd(self):

@mjd.setter
def mjd(self, value):
# If MJD is changed, everything else is no longer valid, so re-init
if self.mjd is not None:
warnings.warn("Changing MJD and resetting all attributes.")
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
17 changes: 13 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,19 @@ 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]
# ugh, stupid np.where doesn't support using scalars anymore
if np.size(observations) == 1:
if (observations["note"] == self.blob_survey.survey_note + ", b") & (
observations["night"] == np.max(observations["night"])
):
need_to_observe = np.array([0])
else:
need_to_observe = np.array([])
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 3f6f0fd

Please sign in to comment.