Skip to content

Commit 358312a

Browse files
committed
Add set_time_now() helper context manager
1 parent be0e82d commit 358312a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

kadi/commands/core.py

+29
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"filter_cmd_events_by_event",
2929
"SCS107_EVENTS",
3030
"filter_scs107_events",
31+
"set_time_now",
3132
]
3233

3334

@@ -1194,3 +1195,31 @@ def filter_cmd_events_state(cmd_events: Table) -> np.ndarray[bool]:
11941195
allowed_states.append("In-work")
11951196
ok = np.isin(cmd_events["State"], allowed_states)
11961197
return ok
1198+
1199+
1200+
def set_time_now(date):
1201+
"""Context manager to temporarily set CXOTIME_NOW to ``date``.
1202+
1203+
This temporarily sets the CXOTIME_NOW environment variable to ``date`` within the
1204+
context block. This is a very thin wrapper around ska_helpers.utils.temp_env_var.
1205+
This effectively makes ``date`` serve as the current time and is useful for testing.
1206+
1207+
In this example we get the observation history as if the 2025:012:14:37:04 SCS-107
1208+
run never happened::
1209+
1210+
import kadi.commands as kc
1211+
start = "2025:012"
1212+
stop = "2025:018"
1213+
with kc.set_time_now(stop):
1214+
obss_as_planned = kc.get_observations(
1215+
start, stop, event_filter=kc.filter_scs107_events
1216+
)
1217+
1218+
Parameters
1219+
----------
1220+
date : CxoTimeLike
1221+
Date in CxoTime-compatible format.
1222+
"""
1223+
from ska_helpers.utils import temp_env_var
1224+
1225+
return temp_env_var("CXOTIME_NOW", date)

0 commit comments

Comments
 (0)