Skip to content

Commit

Permalink
add check that scripted observations have unique id
Browse files Browse the repository at this point in the history
  • Loading branch information
yoachim committed Nov 21, 2024
1 parent 57614c1 commit 653b453
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion rubin_scheduler/scheduler/surveys/scripted_surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,24 @@ def set_script(self, obs_wanted, append=True, add_index=True):
self.script_id_array = np.array(
[int(note.split(", ")[-1]) for note in self.obs_wanted["scheduler_note"]]
)
self.id_start = self.script_id_array.max() + 1
if np.size(self.script_id_array) > 0:
self.id_start = self.script_id_array.max() + 1

if append & (self.obs_wanted is not None):
self.obs_wanted = np.concatenate([self.obs_wanted, obs_wanted])
self.obs_wanted.sort(order=["mjd", "filter"])
else:
self.obs_wanted = obs_wanted

# check that we have valid unique keys for observations
potential_keys = np.char.add(self.obs_wanted["scheduler_note"], self.obs_wanted["filter"])
if np.size(np.unique(potential_keys)) < np.size(self.obs_wanted):
msg = (
"Scripted observations do not have unique scheduler_note "
"+ filter values. Consider setting add_index=True"
)
raise ValueError(msg)

self.mjd_start = self.obs_wanted["mjd"] - self.obs_wanted["mjd_tol"]
# Here is the attribute that core scheduler checks to
# broadcast scheduled observations in the conditions object.
Expand Down
7 changes: 7 additions & 0 deletions tests/scheduler/test_surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ def test_scripted_survey(self):
sched.add_observations_array(completed_observations)
assert np.sum(survey.obs_wanted["observed"]) == 3

# Make sure error gets raised if we set a script wrong
survey = surveys.ScriptedSurvey([])
observations["scheduler_note"] = ["a", "a", "c", "d", "a"]
with self.assertRaises(Exception) as context:
survey.set_script(observations, add_index=False)
self.assertTrue("unique scheduler_note" in str(context))

def test_pointings_survey(self):
"""Test the pointing survey."""
mo = ModelObservatory()
Expand Down

0 comments on commit 653b453

Please sign in to comment.