Skip to content

Commit

Permalink
have sim_runner stop if skipping ahead in time due to no valid observ… (
Browse files Browse the repository at this point in the history
  • Loading branch information
yoachim authored Jan 17, 2025
2 parents 7ed6b2d + 5e41c0e commit 8ba3846
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
4 changes: 4 additions & 0 deletions rubin_scheduler/scheduler/schedulers/core_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def __init__(
# Counter for observations added to the queue
self.target_id_counter = target_id_counter

# Set to something so it doesn't fail if never set later
self.queue_fill_mjd_ns = -1
self.queue_reward_df = None

def flush_queue(self):
"""Like it sounds, clear any currently queued desired observations."""
self.queue = []
Expand Down
43 changes: 25 additions & 18 deletions rubin_scheduler/scheduler/sim_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ def sim_runner(
observatory.mjd = observatory.mjd + step_none
scheduler.update_conditions(observatory.return_conditions())
nskip += 1
continue
# Check we didn't skip so far we should end
if observatory.mjd > sim_end_mjd:
break
else:
continue
completed_obs, new_night = observatory.observe(desired_obs)

if completed_obs is not None:
Expand Down Expand Up @@ -218,23 +222,26 @@ def sim_runner(
# Only warn if it's a low-accuracy astropy conversion
lsst = Site("LSST")

# Using pseudo_parallactic_angle, see https://smtn-019.lsst.io/v/DM-44258/index.html
pa, alt, az = pseudo_parallactic_angle(
np.degrees(observations["RA"]),
np.degrees(observations["dec"]),
observations["mjd"],
lon=lsst.longitude,
lat=lsst.latitude,
height=lsst.height,
)
observations["alt"] = np.radians(alt)
observations["az"] = np.radians(az)
observations["pseudo_pa"] = np.radians(pa)
observations["rotTelPos"] = rc._rotskypos2rottelpos(observations["rotSkyPos"], observations["pseudo_pa"])

# Also include traditional parallactic angle
pa = _approx_altaz2pa(observations["alt"], observations["az"], lsst.latitude_rad)
observations["pa"] = pa
if len(observations) > 0:
# Using pseudo_parallactic_angle, see https://smtn-019.lsst.io/v/DM-44258/index.html
pa, alt, az = pseudo_parallactic_angle(
np.degrees(observations["RA"]),
np.degrees(observations["dec"]),
observations["mjd"],
lon=lsst.longitude,
lat=lsst.latitude,
height=lsst.height,
)
observations["alt"] = np.radians(alt)
observations["az"] = np.radians(az)
observations["pseudo_pa"] = np.radians(pa)
observations["rotTelPos"] = rc._rotskypos2rottelpos(
observations["rotSkyPos"], observations["pseudo_pa"]
)

# Also include traditional parallactic angle
pa = _approx_altaz2pa(observations["alt"], observations["az"], lsst.latitude_rad)
observations["pa"] = pa

runtime = time.time() - t0
print("Skipped %i observations" % nskip)
Expand Down
46 changes: 46 additions & 0 deletions tests/scheduler/test_sim_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import logging
import unittest

import numpy as np

import rubin_scheduler.utils as utils
from rubin_scheduler.scheduler import sim_runner
from rubin_scheduler.scheduler.model_observatory import ModelObservatory
from rubin_scheduler.scheduler.schedulers import CoreScheduler
from rubin_scheduler.scheduler.surveys import BaseSurvey


class NoObsSurvey(BaseSurvey):
"""Dummy class that always returns no valid reward"""

def calc_reward_function(self, conditions):
return -np.inf


class TestSimRunner(unittest.TestCase):

def test_no_obs(self):
"""Check that sim ends even if we stop returning observations."""
mjd_start = utils.SURVEY_START_MJD
nside = 32
survey_length = 1.5 # days

scheduler = CoreScheduler([NoObsSurvey([], detailers=[])])
observatory = ModelObservatory(nside=nside, mjd_start=mjd_start)
# Turn off noisy log warnings
logging.disable(logging.CRITICAL)
observatory, scheduler, observations = sim_runner(
observatory, scheduler, sim_duration=survey_length, filename=None
)

assert len(observations) == 0

observatory, scheduler, observations, reward_df, obs_rewards_series = sim_runner(
observatory, scheduler, sim_duration=survey_length, filename=None, record_rewards=True
)

assert len(observations) == 0


if __name__ == "__main__":
unittest.main()

0 comments on commit 8ba3846

Please sign in to comment.