Skip to content

Commit ff09e8c

Browse files
committed
Small improvments to instrument generator
1 parent dc0fec7 commit ff09e8c

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

codegen/lco/generator.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import textcase
88
from jinja2 import Environment, FileSystemLoader
99

10+
VALID_FACILITIES = ["SOAR", "LCO", "SAAO"]
11+
1012

1113
def get_modes(ins: dict, type: str) -> list[str]:
1214
try:
@@ -23,7 +25,7 @@ def generate_instrument_configs(ins_s: str, facility: str) -> str:
2325
2426
Args:
2527
ins_s (str): The input json containing instrument data.
26-
soar (bool): Whether to generate SOAR instruments.
28+
facility (str): Which facility to generate instruments for.
2729
2830
Returns:
2931
str: Generated Python Pydantic models as a string.
@@ -38,15 +40,21 @@ def generate_instrument_configs(ins_s: str, facility: str) -> str:
3840
ins_data = json.loads(ins_s)
3941
instruments = []
4042
if facility == "SOAR":
43+
# Soar instruments look like SoarGhtsBluecam, already prefixed, so no need to add a prefix.
4144
prefix = ""
4245
filtered = {k: v for k, v in ins_data.items() if "soar" in k.lower()}
4346
elif facility == "LCO":
44-
# LCO And SOAR share the same instruments endpoint, so we need to filter them out
47+
# We add a prefix for LCO because some instruments start with a number,
48+
# which is not allowed in Python class names. For example: Lco0M4ScicamQhy600
4549
prefix = "Lco"
4650
filtered = {k: v for k, v in ins_data.items() if "soar" not in k.lower()}
47-
else:
48-
prefix = facility
51+
elif facility == "SAAO":
52+
# SAAO config doesn't share any instruments with other facilities so we don't need
53+
# to filter it
54+
prefix = "SAAO"
4955
filtered = ins_data
56+
else:
57+
raise ValueError(f"Invalid facility. Must be one of {VALID_FACILITIES}")
5058

5159
# Instruments endpoint seems inconsistent, this should keep our output consistent
5260
ordered = dict(sorted(filtered.items()))
@@ -64,7 +72,7 @@ def generate_instrument_configs(ins_s: str, facility: str) -> str:
6472
"rotator_modes": get_modes(ins, "rotator"),
6573
"optical_elements": {
6674
# This gets rid of the silly trailing s on "filters" and "narrowband_g_positions"
67-
k[:-1]: v
75+
k.rstrip("s"): v
6876
for k, v in ins["optical_elements"].items()
6977
},
7078
}

0 commit comments

Comments
 (0)