Skip to content

Commit

Permalink
Merge pull request #347 from sot/improve-interpolate-states
Browse files Browse the repository at this point in the history
Clip times in interpolate_states
  • Loading branch information
taldcroft authored Jan 13, 2025
2 parents 510ea8c + 42fecc6 commit ad4a7b7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.2
rev: v0.9.0
hooks:
# Run the linter.
- id: ruff
Expand Down
12 changes: 6 additions & 6 deletions kadi/commands/commands_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def update_archive_and_get_cmds_recent(

# Update loads table and download/archive backstop files from OCCweb
loads = update_loads(scenario, cmd_events=cmd_events, lookback=lookback, stop=stop)
logger.info(f'Including loads {", ".join(loads["name"])}')
logger.info(f"Including loads {', '.join(loads['name'])}")

for load in loads:
load_name = load["name"]
Expand Down Expand Up @@ -437,7 +437,7 @@ def update_archive_and_get_cmds_recent(
logger.info(f"Processing {cmds_source} with {len(cmds)} commands")
end_scs = collections.defaultdict(list)
if date_end := cmds.meta.get("rltt"):
source = f'RLTT in {cmds["source"][0]}'
source = f"RLTT in {cmds['source'][0]}"
end_scs[date_end, source].extend([128, 129, 130, 131, 132, 133])

# Explicit END SCS commands. Most commonly these come from command events
Expand All @@ -447,7 +447,7 @@ def update_archive_and_get_cmds_recent(
if np.any(ok):
for cmd in cmds[ok]:
if (scs := cmd["params"]["codisas1"]) >= 128:
source = f'DISABLE SCS in {cmd["source"]} at {cmd["date"]}'
source = f"DISABLE SCS in {cmd['source']} at {cmd['date']}"
end_scs[cmd["date"], source].append(scs)

for (date_end, source), scss in end_scs.items():
Expand All @@ -465,7 +465,7 @@ def update_archive_and_get_cmds_recent(
n_bad = np.count_nonzero(bad)
logger.info(
f"Removing {n_bad} cmds in SCS slots {scss} from "
f'{prev_cmds["source"][0]} due to {source}'
f"{prev_cmds['source'][0]} due to {source}"
)
cmds_list[jj] = prev_cmds[~bad]

Expand Down Expand Up @@ -654,15 +654,15 @@ def get_cmds_obs_from_manvrs(cmds, prev_att=None):
npnt_enab = False
if targ_att is None:
# No target attitude is unexpected since we got to a MANVR cmd.
logger.warning(f'WARNING: no target attitude for {cmd["date"]}')
logger.warning(f"WARNING: no target attitude for {cmd['date']}")
log_context_obs(cmds, cmd)
continue
if prev_att is None:
# No previous attitude happens always for first manvr at the
# beginning of loads, and normally we just push on to the next
# OBS. But if we already have OBS command this is a problem.
if cmds_obs:
logger.warning(f'WARNING: No previous attitude for {cmd["date"]}')
logger.warning(f"WARNING: No previous attitude for {cmd['date']}")
log_context_obs(cmds, cmd)
prev_att = targ_att
continue
Expand Down
2 changes: 1 addition & 1 deletion kadi/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ def pformat_like_backstop(
fmt = "{}={}"
fmtvals.append(fmt.format(key, val))

fmtvals.append(f'scs={cmd["scs"]}')
fmtvals.append(f"scs={cmd['scs']}")

params_str = ", ".join(fmtvals)
else:
Expand Down
5 changes: 4 additions & 1 deletion kadi/commands/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,9 @@ def get_continuity(
def interpolate_states(states, times):
"""Interpolate ``states`` table at given times.
For any ``times`` that are before or after the range of the states table, the
output state will be the first or last state, respectively..
Parameters
----------
states
Expand All @@ -2554,7 +2557,7 @@ def interpolate_states(states, times):
tstops = date2secs(states["datestop"])

indexes = np.searchsorted(tstops, times)
out = states[indexes]
out = states[indexes.clip(0, len(states) - 1)]
out.add_column(Column(secs2date(times), name="date"), index=0)

return out
Expand Down
9 changes: 9 additions & 0 deletions kadi/commands/tests/test_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -1897,3 +1897,12 @@ def test_get_continuity_spm_eclipse():
"eclipse_enable_spm": "2017:087:07:49:55.838",
},
}


def test_interpolate_states_extrapolate():
"""Test that the states are correctly interpolated and extrapolated."""
# fmt: off
sts = states.get_states("2024:001:00:01:00", "2024:001:00:05:00", state_keys=["obsid"])
times = ["2024:001:00:00:00", "2024:001:00:02:30", "2024:001:00:05:00", "2024:001:00:06:00"]
sts_interp = states.interpolate_states(sts, times)
assert np.all(sts_interp["obsid"] == [43839] * 4)

0 comments on commit ad4a7b7

Please sign in to comment.