Skip to content

Commit

Permalink
Make observe loop wait until evening twilight to start observing
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Jan 22, 2025
1 parent ffa21b8 commit e6d492c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
14 changes: 10 additions & 4 deletions src/gort/overwatcher/ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,28 @@ async def update_ephemeris(self, sjd: int | None = None):

return self.ephemeris

def is_night(self, mode: Literal["normal", "observer"] = "normal"):
def is_night(self, mode: Literal["strict", "observer"] = "strict"):
"""Determines whether it is night-time.
Parameters
----------
mode
The mode to use. If ``'normal'``, the function will return :obj:`True`
The mode to use. If ``'strict'``, the function will return :obj:`True`
if the current time is between 15-degree twilights. If ``'observer'``,
it will also return :obj:`False` if we are within
``scheduler.stop_secs_before_morning`` seconds of the morning twilight.
In this mode it also returns :obj:`True`
``scheduler.open_dome_secs_before_twilight`` seconds before the evening
twilight to allow time to open the dome before starting observations.
"""

ephemeris = self.ephemeris
now_jd = float(Time.now().jd)

stop_secs = self.config["overwatcher.schedule.stop_secs_before_morning"]
schedule_config = self.config["overwatcher.schedule"]
stop_secs = schedule_config["stop_secs_before_morning"]
open_dome_secs = schedule_config["open_dome_secs_before_twilight"]

if not ephemeris:
self.log.warning("Ephemeris data not available. is_night() returns False.")
Expand All @@ -145,6 +150,7 @@ def is_night(self, mode: Literal["normal", "observer"] = "normal"):
twilight_end = float(ephemeris.twilight_end)
twilight_start = float(ephemeris.twilight_start)
if mode == "observer":
twilight_end -= open_dome_secs / 86400
twilight_start -= stop_secs / 86400

return twilight_end < now_jd < twilight_start
Expand All @@ -161,7 +167,7 @@ def time_to_evening_twilight(self):

return round(evening_twilight - now, 2)

def time_to_morning_twilight(self, mode: Literal["normal", "observer"] = "normal"):
def time_to_morning_twilight(self, mode: Literal["strict", "observer"] = "strict"):
"""Returns the time to morning twilight in seconds."""

ephemeris = self.ephemeris
Expand Down
25 changes: 12 additions & 13 deletions src/gort/overwatcher/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ async def task(self):
state = self.overwatcher.state
ephemeris = self.overwatcher.ephemeris

# Number of seconds before evening twilight when to open the dome.
open_dome_secs = self.config["overwatcher.schedule.stop_secs_before_morning"]

while True:
if state.dry_run:
pass
Expand Down Expand Up @@ -83,16 +80,6 @@ async def task(self):
)
await asyncio.sleep(15)

else:
# If we are within open_dome_secs_before_twilight seconds
# of the evening twilight, open the dome to be ready for observing.

time_to_twilight = ephemeris.time_to_evening_twilight() or -1
if time_to_twilight > 0 and time_to_twilight < open_dome_secs:
if not await self.overwatcher.dome.is_opening():
await self.notify("Opening the dome for observing.")
await self.overwatcher.dome.open()

await asyncio.sleep(self.interval)


Expand Down Expand Up @@ -218,6 +205,18 @@ async def stop_observing(
async def observe_loop_task(self):
"""Runs the observing loop."""

# Check that we have not started too early (this happens when we open the dome
# a bit early to avoid wasting time). If so, wait until we are properly in
# the observing window.
if not self.overwatcher.ephemeris.is_night():
time_to_twilight = self.overwatcher.ephemeris.time_to_evening_twilight()
if time_to_twilight and time_to_twilight > 0:
await self.notify(
f"Waiting until evening twilight to start observing "
f"({time_to_twilight:.0f} seconds)."
)
await asyncio.sleep(time_to_twilight)

await self.gort.cleanup(readout=True)
observer = self.gort.observer

Expand Down

0 comments on commit e6d492c

Please sign in to comment.