Skip to content

Commit 476272c

Browse files
committed
Add event_filter to API for observations and states
1 parent 28ff699 commit 476272c

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

kadi/commands/observations.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ def get_starcats_as_table(
317317
scenario=None,
318318
cmds=None,
319319
starcat_date=None,
320+
event_filter=None,
320321
):
321322
"""Get a single table of star catalog entries corresponding to input parameters.
322323
@@ -360,6 +361,10 @@ def get_starcats_as_table(
360361
Use this command table instead of querying the archive.
361362
starcat_date : CxoTime-like, None
362363
Date of the observation's star catalog
364+
event_filter : callable, list of callable, None
365+
Callable function or list of callable functions that takes an Event Table as
366+
input and returns a boolean mask with same length as Table. If None, no
367+
filtering is done.
363368
364369
Returns
365370
-------
@@ -374,6 +379,7 @@ def get_starcats_as_table(
374379
cmds=cmds,
375380
starcat_date=starcat_date,
376381
as_dict=True,
382+
event_filter=event_filter,
377383
)
378384
out = defaultdict(list)
379385
for starcat in starcats:
@@ -404,6 +410,7 @@ def get_starcats(
404410
as_dict=False,
405411
starcat_date=None,
406412
show_progress=False,
413+
event_filter=None,
407414
):
408415
"""Get a list of star catalogs corresponding to input parameters.
409416
@@ -478,6 +485,10 @@ def get_starcats(
478485
Date of the observation's star catalog
479486
show_progress : bool
480487
Show progress bar for long queries (default=False)
488+
event_filter : callable, list of callable, None
489+
Callable function or list of callable functions that takes an Event Table as
490+
input and returns a boolean mask with same length as Table. If None, no
491+
filtering is done.
481492
482493
Returns
483494
-------
@@ -501,6 +512,7 @@ def get_starcats(
501512
scenario=scenario,
502513
cmds=cmds,
503514
starcat_date=starcat_date,
515+
event_filter=event_filter,
504516
)
505517
starcats = []
506518
rev_pars_dict = REV_PARS_DICT if cmds is None else cmds.rev_pars_dict()
@@ -571,7 +583,14 @@ def get_starcats(
571583

572584

573585
def get_observations(
574-
start=None, stop=None, *, obsid=None, scenario=None, cmds=None, starcat_date=None
586+
start=None,
587+
stop=None,
588+
*,
589+
obsid=None,
590+
scenario=None,
591+
cmds=None,
592+
starcat_date=None,
593+
event_filter=None,
575594
):
576595
"""Get observations corresponding to input parameters.
577596
@@ -604,8 +623,9 @@ def get_observations(
604623
605624
>>> obs_all = get_observations() # All observations in commands archive
606625
607-
# Might be convenient to handle this as a Table >>> from astropy.table
608-
import Table >>> obs_all = Table(obs_all)
626+
# Might be convenient to handle this as a Table
627+
>>> from astropy.table import Table
628+
>>> obs_all = Table(obs_all)
609629
610630
>>> from kadi.commands import get_observations
611631
>>> get_observations(starcat_date='2022:001:17:00:58.521')
@@ -635,6 +655,10 @@ def get_observations(
635655
Use this command table instead of querying the archive
636656
starcat_date : CxoTime-like, None
637657
Date of the observation's star catalog
658+
event_filter : callable, list of callable, None
659+
Callable function or list of callable functions that takes an Event Table as
660+
input and returns a boolean mask with same length as Table. If None, no
661+
filtering is done.
638662
639663
Returns
640664
-------
@@ -651,7 +675,7 @@ def get_observations(
651675

652676
if cmds is None:
653677
if scenario not in OBSERVATIONS:
654-
cmds = get_cmds(scenario=scenario)
678+
cmds = get_cmds(scenario=scenario, event_filter=event_filter)
655679
cmds_obs = cmds[cmds["tlmsid"] == "OBS"]
656680
obsids = []
657681
for cmd in cmds_obs:

kadi/commands/states.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,7 @@ def get_states(
21102110
reduce=True,
21112111
merge_identical=False,
21122112
scenario=None,
2113+
event_filter=None,
21132114
) -> Table:
21142115
"""
21152116
Get table of states for intervals when ``state_keys`` params are unchanged.
@@ -2155,6 +2156,10 @@ def get_states(
21552156
Merge identical states (see reduce_states() docs, default=False).
21562157
scenario : str, optional
21572158
Name of commands archive scenario to use instead of default.
2159+
event_filter : callable, list of callable, None
2160+
Callable function or list of callable functions that takes an Event Table as
2161+
input and returns a boolean mask with same length as Table. This is used to
2162+
select rows from the Table. If None, no filtering is done.
21582163
21592164
Returns
21602165
-------
@@ -2183,7 +2188,9 @@ def get_states(
21832188
if cmds is None:
21842189
if start is None:
21852190
raise ValueError("must supply either 'cmds' argument or 'start' argument")
2186-
cmds = commands.get_cmds(start, stop, scenario=scenario)
2191+
cmds = commands.get_cmds(
2192+
start, stop, scenario=scenario, event_filter=event_filter
2193+
)
21872194
start = DateTime(start).date
21882195
stop = DateTime(
21892196
stop or cmds[-1]["date"]
@@ -2195,7 +2202,9 @@ def get_states(
21952202
# Get initial state at start of commands
21962203
if continuity is None:
21972204
try:
2198-
continuity = get_continuity(start, state_keys, scenario=scenario)
2205+
continuity = get_continuity(
2206+
start, state_keys, scenario=scenario, event_filter=event_filter
2207+
)
21992208
except ValueError as exc:
22002209
if "did not find transitions" in str(exc):
22012210
raise ValueError(
@@ -2383,7 +2392,11 @@ def reduce_states(states, state_keys, merge_identical=False, all_keys=False) ->
23832392

23842393

23852394
def get_continuity(
2386-
date=None, state_keys=None, lookbacks=(7, 30, 180, 1000), scenario=None
2395+
date=None,
2396+
state_keys=None,
2397+
lookbacks=(7, 30, 180, 1000),
2398+
scenario=None,
2399+
event_filter=None,
23872400
):
23882401
"""
23892402
Get the state and transition dates at ``date`` for ``state_keys``.
@@ -2415,6 +2428,10 @@ def get_continuity(
24152428
list of lookback times in days (default=[7, 30, 180, 1000])
24162429
scenario
24172430
commands archive scenario (default=None)
2431+
event_filter : callable, list of callable, None
2432+
Callable function or list of callable functions that takes an Event Table as
2433+
input and returns a boolean mask with same length as Table. This is used to
2434+
select rows from the Table. If None, no filtering is done.
24182435
24192436
Returns
24202437
-------
@@ -2440,7 +2457,9 @@ def get_continuity(
24402457

24412458
for lookback in lookbacks:
24422459
start = stop - lookback
2443-
cmds = commands.get_cmds(start, stop, scenario=scenario)
2460+
cmds = commands.get_cmds(
2461+
start, stop, scenario=scenario, event_filter=event_filter
2462+
)
24442463
if len(cmds) == 0:
24452464
continue
24462465

@@ -2468,6 +2487,7 @@ def get_continuity(
24682487
continuity={},
24692488
reduce=False,
24702489
scenario=scenario,
2490+
event_filter=event_filter,
24712491
)
24722492
except NoTransitionsError:
24732493
# No transitions within `cmds` for state_key, continue with other keys

0 commit comments

Comments
 (0)