Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions kadi/commands/commands_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from parse_cm.paths import ParseLoadNameError, load_dir_from_load_name, parse_load_name
from ska_helpers.retry import retry_func
from ska_sun import get_nsm_attitude
from testr.test_helper import has_internet

from kadi import occweb, paths
from kadi.commands.command_sets import get_cmds_from_event
Expand Down Expand Up @@ -76,7 +75,6 @@
# APR1420B was the first load set to have RLTT (backstop 6.9)
RLTT_ERA_START = CxoTime("2020-04-14")

HAS_INTERNET = has_internet()

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -273,11 +271,11 @@ def get_cmds(
# For flight scenario or no internet or if the query stop time is guaranteed
# to not require recent commands then just use the archive.
before_recent_cmds = stop < CxoTime(cxotime_now) - lookback * u.day
if scenario == "flight" or not HAS_INTERNET or before_recent_cmds:
if scenario == "flight" or before_recent_cmds:
cmds = IDX_CMDS
logger.info(
"Getting commands from archive only because of:"
f" {scenario=} {before_recent_cmds=} {HAS_INTERNET=}"
f" {scenario=} {before_recent_cmds=}"
)
else:
if cache_key not in CMDS_RECENT:
Expand Down Expand Up @@ -1124,7 +1122,7 @@ def update_cmd_events(

# Get sheet doc ids for scenario, or [] if not applicable
doc_ids = get_sheet_doc_ids_for_scenario(scenario)
if not doc_ids or not HAS_INTERNET:
if not doc_ids:
cmd_events = get_cmd_events_from_local(scenario)
else:
cmd_events = get_cmd_events_from_sheet(scenario, doc_ids)
Expand Down Expand Up @@ -1171,7 +1169,26 @@ def get_cmd_events_from_sheet(scenario: str | None, doc_ids: list[str]) -> Table
cmd_events_list = []
for doc_id in doc_ids:
# Fetch the command events from the Google Sheet URL(s).
cmd_events = read_cmd_events_from_sheet(doc_id)
try:
cmd_events = read_cmd_events_from_sheet(doc_id)
except requests.ConnectionError as exc:
if scenario != "flight":
msg = """\
connection error implies no internet, so the 'flight' scenario is required.

Options to fix this include:

- Ensure internet access is enabled (check if https://google.com responds).
- Call the kadi commands function with `scenario="flight"`.
- Set the environment variable KADI_SCENARIO="flight" in your shell.
- Set the environment variable KADI_SCENARIO="flight" in your code:
import os
os.environ["KADI_SCENARIO"] = "flight"
"""
raise ValueError(msg) from exc
else:
raise exc

cmd_events_list.append(cmd_events)

# Combine command events from multiple documents if necessary.
Expand Down Expand Up @@ -1201,11 +1218,13 @@ def get_sheet_doc_ids_for_scenario(scenario) -> list[str]:
Returns
-------
list[str]
List of Google sheet document IDs. This will be an empty list if no
Google sheet(s) are associated with the scenario.
List of Google sheet document IDs. This will be an empty list for the "flight"
scenario or for a local scenario.
"""
if scenario in ("flight", None):
if scenario is None:
doc_ids = [conf.cmd_events_flight_id]
elif scenario == "flight":
doc_ids = []
elif scenario == "custom":
doc_ids = [conf.cmd_events_custom_id]
elif scenario == "flight+custom":
Expand Down
68 changes: 52 additions & 16 deletions kadi/commands/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_find():


def test_get_cmds():
cs = commands.get_cmds("2012:029:12:00:00", "2012:030:12:00:00")
cs = commands.get_cmds("2012:029:12:00:00", "2012:030:12:00:00", scenario="flight")
assert isinstance(cs, commands.CommandTable)
assert len(cs) == 151 # OBS commands in v2 only
assert np.all(cs["source"][:10] == "JAN2612A")
Expand All @@ -121,7 +121,9 @@ def test_get_cmds():
assert cs["date"][-1] == "2012:030:11:00:01.285"
assert cs["tlmsid"][-1] == "CTXBON"

cs = commands.get_cmds("2012:029:12:00:00", "2012:030:12:00:00", type="simtrans")
cs = commands.get_cmds(
"2012:029:12:00:00", "2012:030:12:00:00", type="simtrans", scenario="flight"
)
assert len(cs) == 2
assert np.all(cs["date"] == ["2012:030:02:00:00.000", "2012:030:08:27:02.000"])
assert np.all(cs["pos"] == [75624, 73176]) # from params
Expand All @@ -140,7 +142,7 @@ def test_get_cmds():


def test_get_cmds_zero_length_result():
cmds = commands.get_cmds(date="2017:001:12:00:00")
cmds = commands.get_cmds(date="2017:001:12:00:00", scenario="flight")
assert len(cmds) == 0
source_name = "source"
assert cmds.colnames == [
Expand All @@ -162,15 +164,15 @@ def test_get_cmds_inclusive_stop():
# or start <= date <= stop for inclusive_stop=True.
# Query over a range that includes two commands at exactly start and stop.
start, stop = "2020:001:15:50:00.000", "2020:001:15:50:00.257"
cmds = commands.get_cmds(start, stop)
cmds = commands.get_cmds(start, stop, scenario="flight")
assert np.all(cmds["date"] == [start])

cmds = commands.get_cmds(start, stop, inclusive_stop=True)
cmds = commands.get_cmds(start, stop, inclusive_stop=True, scenario="flight")
assert np.all(cmds["date"] == [start, stop])


def test_cmds_as_list_of_dict():
cmds = commands.get_cmds("2020:140", "2020:141")
cmds = commands.get_cmds("2020:140", "2020:141", scenario="flight")
cmds_list = cmds.as_list_of_dict()
assert isinstance(cmds_list, list)
assert isinstance(cmds_list[0], dict)
Expand All @@ -182,7 +184,7 @@ def test_cmds_as_list_of_dict():

def test_cmds_as_list_of_dict_ska_parsecm():
"""Test the ska_parsecm=True compatibility mode for list_of_dict"""
cmds = commands.get_cmds("2020:140", "2020:141")
cmds = commands.get_cmds("2020:140", "2020:141", scenario="flight")
cmds_list = cmds.as_list_of_dict(ska_parsecm=True)
assert isinstance(cmds_list, list)
assert isinstance(cmds_list[0], dict)
Expand Down Expand Up @@ -211,7 +213,9 @@ def test_get_cmds_from_backstop_and_add_cmds():
bs_file = Path(parse_cm.tests.__file__).parent / "data" / "CR182_0803.backstop"
bs_cmds = commands.get_cmds_from_backstop(bs_file, remove_starcat=True)

cmds = commands.get_cmds(start="2018:182:00:00:00", stop="2018:182:08:00:00")
cmds = commands.get_cmds(
start="2018:182:00:00:00", stop="2018:182:08:00:00", scenario="flight"
)

assert len(bs_cmds) == 674
assert len(cmds) == 57
Expand Down Expand Up @@ -258,7 +262,9 @@ def test_commands_create_archive_regress(
kadi_orig = os.environ.get("KADI")
start = CxoTime("2021:290")
stop = start + 30 * u.day
cmds_flight = commands.get_cmds(start + 3 * u.day, stop - 3 * u.day)
cmds_flight = commands.get_cmds(
start + 3 * u.day, stop - 3 * u.day, scenario="flight"
)
cmds_flight.fetch_params()

sched_stop_flight: np.ndarray = (cmds_flight["type"] == "LOAD_EVENT") & (
Expand All @@ -280,11 +286,17 @@ def test_commands_create_archive_regress(
del commands_v2.REV_PARS_DICT._val

# Make sure we are seeing the temporary cmds archive
cmds_empty = commands.get_cmds(start - 60 * u.day, start - 50 * u.day)
cmds_empty = commands.get_cmds(start - 60 * u.day, start - 50 * u.day)
cmds_empty = commands.get_cmds(
start - 60 * u.day, start - 50 * u.day, scenario="flight"
)
cmds_empty = commands.get_cmds(
start - 60 * u.day, start - 50 * u.day, scenario="flight"
)
assert len(cmds_empty) == 0

cmds_local = commands.get_cmds(start + 3 * u.day, stop - 3 * u.day)
cmds_local = commands.get_cmds(
start + 3 * u.day, stop - 3 * u.day, scenario="flight"
)

cmds_local.fetch_params()
if len(cmds_flight) != len(cmds_local):
Expand Down Expand Up @@ -1104,7 +1116,7 @@ def test_get_starcat_only_agasc1p8():
def test_get_starcats_with_cmds():
start, stop = "2021:365:19:00:00", "2022:002:01:25:00"
cmds = commands.get_cmds(start, stop, scenario="flight")
starcats0 = get_starcats(start, stop)
starcats0 = get_starcats(start, stop, scenario="flight")
starcats1 = get_starcats(cmds=cmds)
assert len(starcats0) == len(starcats1)
for starcat0, starcat1 in zip(starcats0, starcats1):
Expand All @@ -1113,10 +1125,12 @@ def test_get_starcats_with_cmds():
assert np.all(col)


@pytest.mark.skipif(not HAS_INTERNET, reason="No internet connection")
def test_get_starcats_obsid():
from mica.starcheck import get_starcat

sc_kadi = get_starcats(obsid=26330, scenario="flight")[0]
# get_starcat() requires internet - it calls get_observations() with no scenario
sc_mica = get_starcat(26330)
assert len(sc_kadi) == len(sc_mica)
assert sc_kadi.colnames == [
Expand Down Expand Up @@ -1156,7 +1170,7 @@ def test_get_starcats_date():
sc = get_starcats(obsid=8008, scenario="flight")[0]
obs = get_observations(obsid=8008, scenario="flight")[0]
assert sc.date == obs["starcat_date"] == "2007:002:04:31:43.965"
cmds = commands.get_cmds("2007:002", "2007:003")
cmds = commands.get_cmds("2007:002", "2007:003", scenario="flight")
sc_cmd = cmds[cmds["date"] == obs["starcat_date"]][0]
assert sc_cmd["type"] == "MP_STARCAT"

Expand Down Expand Up @@ -1228,6 +1242,25 @@ def test_flight_scenario_sheet_access():
commands.get_cmds("-7d") # fails, bad sheet URL


@pytest.mark.skipif(HAS_INTERNET, reason="Requires no internet connection")
def test_no_internet():
# All OK for "flight" scenario, we get some commands in the last 3 weeks
cmds = commands.get_cmds("-21d", scenario="flight")
assert len(cmds) > 10

match = re.escape(
"connection error implies no internet, so the 'flight' scenario is required"
)
with pytest.raises(ValueError, match=match):
commands.get_cmds("-3d")

with pytest.raises(ValueError, match=match):
commands.get_cmds("-3d", scenario="custom")

with pytest.raises(ValueError, match=match):
commands.get_cmds("-3d", scenario="flight+custom")


@pytest.mark.skipif(not HAS_INTERNET, reason="No internet connection")
def test_custom_scenario(monkeypatch, stop_date_2024_035_23_00_00):
"""Test "custom" scenario with a carefully constructed sequence of events.
Expand Down Expand Up @@ -1747,14 +1780,14 @@ def test_fill_gaps():

def test_get_rltt_scheduled_stop_time():
"""RLTT and scheduled stop time are both 2023:009:04:14:00.000."""
cmds = commands.get_cmds("2023:009", "2023:010")
cmds = commands.get_cmds("2023:009", "2023:010", scenario="flight")
rltt = cmds.get_rltt()
assert rltt == "2023:009:04:14:00.000"

stt = cmds.get_scheduled_stop_time()
assert stt == "2023:009:04:14:00.000"

cmds = commands.get_cmds("2023:009:12:00:00", "2023:010")
cmds = commands.get_cmds("2023:009:12:00:00", "2023:010", scenario="flight")
assert cmds.get_rltt() is None
assert cmds.get_scheduled_stop_time() is None

Expand Down Expand Up @@ -1998,6 +2031,7 @@ def test_add_cmds():
assert cmds12_no_rltt.pformat_like_backstop() == exp_no_rltt


@pytest.mark.skipif(not HAS_INTERNET, reason="No internet connection")
def test_read_backstop_with_observations():
"""Test reading backstop with observations in it.

Expand All @@ -2009,6 +2043,8 @@ def test_read_backstop_with_observations():
except FileNotFoundError:
pytest.skip("No backstop file found")

# read_backstop(add_observations=True) requires internet because it calls
# kcs.get_continuity().
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you remove the commented-out lines or will they be needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a code comment justifying the pytest skipif above, not a commented-out line.

cmds = read_backstop(path, add_observations=True)
obss = get_observations(cmds=cmds)
starcats = get_starcats(cmds=cmds)
Expand Down
Loading