Skip to content

Commit

Permalink
update night in prenight when visits loaded, if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ehneilsen committed Dec 8, 2023
1 parent 36e7ead commit fdbd545
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
14 changes: 14 additions & 0 deletions schedview/app/prenight/prenight.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,20 @@ def _update_visits(self):
Time(self._almanac_events.loc["sunset", "UTC"]),
Time(self._almanac_events.loc["sunrise", "UTC"]),
)
if len(visits) == 0:
self.logger.info("No visits on requested night, looking for a night with visits.")

Check warning on line 309 in schedview/app/prenight/prenight.py

View check run for this annotation

Codecov / codecov/patch

schedview/app/prenight/prenight.py#L309

Added line #L309 was not covered by tests
# read all visits to so we can find a central one
visits = schedview.collect.opsim.read_opsim(self.opsim_output_fname)

Check warning on line 311 in schedview/app/prenight/prenight.py

View check run for this annotation

Codecov / codecov/patch

schedview/app/prenight/prenight.py#L311

Added line #L311 was not covered by tests
if len(visits) > 0:
self.logger.info("Changing night to one with visits.")
self.night = schedview.compute.astro.compute_central_night(visits)

Check warning on line 314 in schedview/app/prenight/prenight.py

View check run for this annotation

Codecov / codecov/patch

schedview/app/prenight/prenight.py#L313-L314

Added lines #L313 - L314 were not covered by tests
# Setting night will trigger a call to _update_visits
# by way of _almanac_events, so we should not continue
# this execution..
return

Check warning on line 318 in schedview/app/prenight/prenight.py

View check run for this annotation

Codecov / codecov/patch

schedview/app/prenight/prenight.py#L318

Added line #L318 was not covered by tests
else:
self.logger.info("No visits found in this opsim database.")

Check warning on line 320 in schedview/app/prenight/prenight.py

View check run for this annotation

Codecov / codecov/patch

schedview/app/prenight/prenight.py#L320

Added line #L320 was not covered by tests

self._visits = visits
self.logger.info("Finish updating visits DataFrame.")
self.logger.info("Starting to update visits ColumnDataSource.")
Expand Down
30 changes: 30 additions & 0 deletions schedview/compute/astro.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,33 @@ def night_events(night_date=None, site=None, timezone="Chile/Continental"):
time_df.index.name = "event"

return time_df


def compute_central_night(visits, site=None, timezone="Chile/Continental"):

Check warning on line 104 in schedview/compute/astro.py

View check run for this annotation

Codecov / codecov/patch

schedview/compute/astro.py#L104

Added line #L104 was not covered by tests
"""Compute the central night of a set of visits.
Parameters
----------
visits : `pandas.DataFrame`
A DataFrame of visits.
site : `astropy.coordinates.earth.EarthLocation`
The observatory location. Defaults to Rubin observatory.
timezone: `str`
The timezone name. Defaults to 'Chile/Continental'
Returns
-------
central_night : `datetime.date`
The central night of the visits.
"""
central_mjd = visits["observationStartMJD"].median()
candidate_night = Time(central_mjd, format="mjd", scale="utc").datetime.date()

Check warning on line 122 in schedview/compute/astro.py

View check run for this annotation

Codecov / codecov/patch

schedview/compute/astro.py#L121-L122

Added lines #L121 - L122 were not covered by tests

# The mjd rollover can occur during the night, so the above might be offset
# by a night. Make sure the night we have is the one with a central mjd
# closest to the median visit mjd.
candidate_middle_mjd = night_events(candidate_night, site, timezone).loc["night_middle", "MJD"]
mjd_shift = np.round(central_mjd - candidate_middle_mjd)
central_night = Time(central_mjd + mjd_shift, format="mjd", scale="utc").datetime.date()

Check warning on line 129 in schedview/compute/astro.py

View check run for this annotation

Codecov / codecov/patch

schedview/compute/astro.py#L127-L129

Added lines #L127 - L129 were not covered by tests

return central_night

Check warning on line 131 in schedview/compute/astro.py

View check run for this annotation

Codecov / codecov/patch

schedview/compute/astro.py#L131

Added line #L131 was not covered by tests
25 changes: 25 additions & 0 deletions tests/test_compute_astro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest

import numpy as np
import pandas as pd

from schedview.compute.astro import compute_central_night, night_events

TEST_MJDS = [60000.6, 60000.8, 60001.2]


class TestComputeAstro(unittest.TestCase):
def test_compute_central_night(self):
for mjd in TEST_MJDS:
visits = pd.DataFrame(
{
"observationStartMJD": [mjd - 0.1, mjd, mjd + 0.1],
"fieldRA": [0, 0, 0],
"fieldDec": [0, 0, 0],
"filter": ["r", "i", "z"],
}
)
computed_night = compute_central_night(visits)
computed_night_events = night_events(computed_night)
computed_night_middle_mjd = computed_night_events.loc["night_middle", "MJD"]
assert np.abs(computed_night_middle_mjd - mjd) <= 0.5

0 comments on commit fdbd545

Please sign in to comment.