Skip to content

Commit be0e82d

Browse files
committed
Some refactoring and tweaks
1 parent 4543702 commit be0e82d

File tree

2 files changed

+66
-38
lines changed

2 files changed

+66
-38
lines changed

kadi/commands/commands_v2.py

+2-38
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
CommandTable,
3030
LazyVal,
3131
_find,
32+
filter_cmd_events_date_stop,
33+
filter_cmd_events_state,
3234
get_cmds_from_backstop,
3335
get_cxotime_now,
3436
get_par_idx_update_pars_dict,
@@ -1066,29 +1068,6 @@ def update_cmd_events(
10661068
return cmd_events
10671069

10681070

1069-
def filter_cmd_events_state(cmd_events: Table) -> np.ndarray[bool]:
1070-
"""
1071-
Filters command events based on State.
1072-
1073-
Parameters
1074-
----------
1075-
cmd_events : Table
1076-
Command events, where each event has a "State" attribute.
1077-
1078-
Returns
1079-
-------
1080-
numpy.ndarray
1081-
A boolean array indicating which command events have allowed states.
1082-
"""
1083-
allowed_states = ["Predictive", "Definitive"]
1084-
# In-work can be used to validate the new event vs telemetry prior to make it
1085-
# operational.
1086-
if conf.include_in_work_command_events:
1087-
allowed_states.append("In-work")
1088-
ok = np.isin(cmd_events["State"], allowed_states)
1089-
return ok
1090-
1091-
10921071
def get_cmd_events_from_sheet(scenario: str | None) -> Table:
10931072
"""
10941073
Fetch command events from Google Sheet for ``scenario`` and write to a CSV file.
@@ -1155,21 +1134,6 @@ def get_cmd_events_from_local(scenario=None):
11551134
return cmd_events
11561135

11571136

1158-
def filter_cmd_events_date_stop(date_stop):
1159-
def func(cmd_events):
1160-
stop = CxoTime(date_stop)
1161-
# Filter table based on stop date. Need to use CxoTime on each event separately
1162-
# because the date format could be inconsistent.
1163-
ok = [CxoTime(cmd_event["Date"]).date <= stop.date for cmd_event in cmd_events]
1164-
logger.debug(
1165-
f"Filtering cmd_events to stop date {stop.date} "
1166-
f"({np.count_nonzero(ok)} vs {len(cmd_events)})"
1167-
)
1168-
return ok
1169-
1170-
return func
1171-
1172-
11731137
def update_loads(scenario=None, *, lookback=None, stop_loads=None) -> Table:
11741138
"""Update local copy of approved command loads though ``lookback`` days."""
11751139
dt = 21 * u.day

kadi/commands/core.py

+64
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99
import weakref
1010
from pathlib import Path
11+
from typing import Callable
1112

1213
import numpy as np
1314
import parse_cm.paths
@@ -16,15 +17,23 @@
1617
from cxotime import CxoTime
1718
from ska_helpers import retry
1819

20+
from kadi.config import conf
1921
from kadi.paths import IDX_CMDS_PATH, PARS_DICT_PATH
2022

2123
__all__ = [
2224
"read_backstop",
2325
"get_cmds_from_backstop",
2426
"CommandTable",
2527
"add_observations_to_cmds",
28+
"filter_cmd_events_by_event",
29+
"SCS107_EVENTS",
30+
"filter_scs107_events",
2631
]
2732

33+
34+
# Events to filter for getting as-planned commands after SCS-107
35+
SCS107_EVENTS = ["SCS-107", "Observing not run", "Obsid", "RTS"]
36+
2837
logger = logging.getLogger(__name__)
2938

3039

@@ -1130,3 +1139,58 @@ def get_cxotime_now() -> str | None:
11301139
)
11311140

11321141
return cxotime_now
1142+
1143+
1144+
def filter_cmd_events_date_stop(date_stop):
1145+
def func(cmd_events):
1146+
stop = CxoTime(date_stop)
1147+
# Filter table based on stop date. Need to use CxoTime on each event separately
1148+
# because the date format could be inconsistent.
1149+
ok = [CxoTime(cmd_event["Date"]).date <= stop.date for cmd_event in cmd_events]
1150+
logger.debug(
1151+
f"Filtering cmd_events to stop date {stop.date} "
1152+
f"({np.count_nonzero(ok)} vs {len(cmd_events)})"
1153+
)
1154+
return ok
1155+
1156+
return func
1157+
1158+
1159+
def filter_cmd_events_by_event(event_events: list[str]) -> Callable:
1160+
def func(cmd_events: Table) -> np.ndarray[bool]:
1161+
ok = ~np.isin(cmd_events["Event"], event_events)
1162+
logger.info(
1163+
f"Filtering cmd_events['Event'] that match {event_events} "
1164+
f"({np.count_nonzero(ok)} vs {len(cmd_events)})"
1165+
)
1166+
return ok
1167+
1168+
return func
1169+
1170+
1171+
filter_scs107_events = filter_cmd_events_by_event(SCS107_EVENTS)
1172+
filter_scs107_events.__doc__ = "Filter SCS-107 related events from command history."
1173+
filter_scs107_events.__name__ = "filter_scs107_events"
1174+
1175+
1176+
def filter_cmd_events_state(cmd_events: Table) -> np.ndarray[bool]:
1177+
"""
1178+
Filters command events based on State.
1179+
1180+
Parameters
1181+
----------
1182+
cmd_events : Table
1183+
Command events, where each event has a "State" attribute.
1184+
1185+
Returns
1186+
-------
1187+
numpy.ndarray
1188+
A boolean array indicating which command events have allowed states.
1189+
"""
1190+
allowed_states = ["Predictive", "Definitive"]
1191+
# In-work can be used to validate the new event vs telemetry prior to make it
1192+
# operational.
1193+
if conf.include_in_work_command_events:
1194+
allowed_states.append("In-work")
1195+
ok = np.isin(cmd_events["State"], allowed_states)
1196+
return ok

0 commit comments

Comments
 (0)