Skip to content

Commit

Permalink
add unit test, remove mjd_start from conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
yoachim committed Oct 3, 2024
1 parent 7830f1a commit 43c2cc6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
57 changes: 52 additions & 5 deletions rubin_scheduler/scheduler/features/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ def __init__(
during the current night. From interpolation.
sunrise : `float`
The MJD of sunrise during the current night. From interpolation
mjd_start : `float`
The starting MJD of the survey.
moonrise : `float`
The MJD of moonrise during the current night. From interpolation.
moonset : `float`
Expand Down Expand Up @@ -550,21 +548,71 @@ def set_auxtel_info(
for key in auxtel_args.parameters.keys():
setattr(self, key, loc[key])

potential_attrs = dir(self)
for key in kwargs:
if key not in potential_attrs:
warnings.warn("Setting unexpected Conditions attribute %s" % key)
setattr(self, key, kwargs[key])

def set_maintel_info(self, mjd, **kwargs):
def set_maintel_info(
self,
mjd,
slewtime,
current_filter,
mounted_filters,
night,
skybrightness,
fwhm_eff,
moon_alt,
moon_az,
moon_ra,
moon_dec,
moon_phase,
sun_alt,
sun_az,
sun_ra,
sun_dec,
tel_ra,
tel_dec,
tel_alt,
tel_az,
wind_speed,
wind_direction,
sun_n12_setting,
sun_n12_rising,
sun_n18_setting,
sun_n18_rising,
moonrise,
moonset,
planet_positions,
tel_alt_limits,
tel_az_limits,
sky_alt_limits,
sky_az_limits,
**kwargs,
):
"""Method to set all the information we expect will be required by
a standard maintel scheduler. Extra attributes can be set via **kwargs.
"""
self._init_attributes()

maintel_args = signature(self.set_auxtel_info)
maintel_args = signature(self.set_maintel_info)
loc = locals()
for key in maintel_args.parameters.keys():
setattr(self, key, loc[key])

potential_attrs = dir(self)
for key in kwargs:
if key not in potential_attrs:
warnings.warn("Setting unexpected Conditions attribute %s" % key)
setattr(self, key, kwargs[key])

def set_attrs(self, **kwargs):
"""Convience function for setting lots of attributes at once."""
potential_attrs = dir(self)
for key in kwargs:
if key not in potential_attrs:
warnings.warn("Setting unexpected Conditions attribute %s" % key)
setattr(self, key, kwargs[key])

def __repr__(self):
Expand Down Expand Up @@ -661,7 +709,6 @@ def __str__(self):
print(positions_deg.to_markdown(), file=output)

events = (
"mjd_start",
"mjd",
"sunset",
"sun_n12_setting",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,6 @@ def return_conditions(self):
sun_moon_info_start_of_night = self.almanac.get_sun_moon_positions(self.conditions.sunset)
self.conditions.moon_phase_sunset = sun_moon_info_start_of_night["moon_phase"]

self.conditions.mjd_start = self.mjd_start

# Telescope limits
self.conditions.sky_az_limits = self.sky_az_limits
self.conditions.sky_alt_limits = self.sky_alt_limits
Expand Down
39 changes: 35 additions & 4 deletions tests/scheduler/test_conditions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
from inspect import signature

import numpy as np

from rubin_scheduler.scheduler.features import Conditions
from rubin_scheduler.scheduler.model_observatory import ModelObservatory

Expand All @@ -12,16 +14,45 @@ def test_conditions(self):
mo = ModelObservatory()
mo_conditions = mo.return_conditions()

# Fresh empty conditions
conditions = Conditions()

# Find the arguments that need to be set
auxtel_args = signature(conditions.set_auxtel_info)

to_pass = {}

to_pass = []
for key in auxtel_args.parameters.keys():
if key != "kwargs":
to_pass.append(getattr(mo_conditions, key))

conditions.set_auxtel_info(*to_pass)

for key in auxtel_args.parameters.keys():
to_pass[key] = getattr(mo_conditions, key)
if np.size(getattr(conditions, key)) == 1:
assert getattr(conditions, key) == getattr(mo_conditions, key)
else:
assert np.array_equal(getattr(conditions, key), getattr(mo_conditions, key), equal_nan=True)

# Again for the maintel attrs
conditions = Conditions()
maintel_args = signature(conditions.set_maintel_info)

to_pass = []
for key in maintel_args.parameters.keys():
if key != "kwargs":
to_pass.append(getattr(mo_conditions, key))

conditions.set_maintel_info(*to_pass)

for key in maintel_args.parameters.keys():
assert getattr(conditions, key) == getattr(mo_conditions, key)

# check we can set some arbitrary attributes
conditions = Conditions()
conditions.set_attrs(mjd=62511, moon_alt=0.1)

conditions.set_auxtel_info(**to_pass)
with self.assertWarns(Warning):
conditions.set_attrs(not_an_attribute=10)


if __name__ == "__main__":
Expand Down

0 comments on commit 43c2cc6

Please sign in to comment.