Skip to content
Merged
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
28 changes: 27 additions & 1 deletion imap_processing/ancillary/ancillary_dataset_combiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def __init__(
):
super().__init__(ancillary_input, expected_end_date)

def convert_file_to_dataset(self, filepath: str | Path) -> xr.Dataset:
def convert_file_to_dataset(self, filepath: str | Path) -> xr.Dataset: # noqa: PLR0911
"""
Convert GLOWS ancillary .dat files to xarray datasets.

Expand All @@ -364,6 +364,19 @@ def convert_file_to_dataset(self, filepath: str | Path) -> xr.Dataset:
if "excluded-regions" in filename:
# Handle excluded regions (2 columns: longitude, latitude)
data = np.loadtxt(filepath, comments="#")
if data.size == 0:
return xr.Dataset(
{
"ecliptic_longitude_deg": (
["region"],
np.array([], dtype=float),
),
"ecliptic_latitude_deg": (
["region"],
np.array([], dtype=float),
),
}
)
Comment on lines +367 to +379
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

This new empty-file branch is not covered by tests. There are existing unit tests for GlowsAncillaryCombiner.convert_file_to_dataset(); please add a test that writes an empty (or comment-only) map-of-excluded-regions file and asserts the returned dataset has the expected variables and zero-length region dimension.

Copilot uses AI. Check for mistakes.
return xr.Dataset(
{
"ecliptic_longitude_deg": (["region"], data[:, 0]),
Expand Down Expand Up @@ -412,6 +425,19 @@ def convert_file_to_dataset(self, filepath: str | Path) -> xr.Dataset:
}
)

elif "l2-calibration" in filename:
# Handle calibration file (timestamp + cps_per_R float value)
with open(filepath) as f:
lines = [line.strip() for line in f if not line.startswith("#")]
identifiers = [line.split(" ", 1)[0] for line in lines]
values = [float(line.split(" ", 1)[1]) for line in lines]
return xr.Dataset(
{
"start_time_utc": (["time_block"], identifiers),
"cps_per_r": (["time_block"], values),
}
)

elif filename.endswith(".json"):
# Handle pipeline settings JSON file using the generic read_json method
return self.convert_json_to_dataset(filepath)
Expand Down
28 changes: 28 additions & 0 deletions imap_processing/tests/ancillary/test_ancillary_dataset_combiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ def test_glows_excluded_regions_combiner(glows_ancillary_filepath):
assert dataset["ecliptic_latitude_deg"].dims == ("region",)


def test_glows_excluded_regions_combiner_empty_file(tmp_path):
file_path = tmp_path / "imap_glows_l1b-map-of-excluded-regions_20251112_v001.dat"
file_path.write_text("# header only\n")

combiner = GlowsAncillaryCombiner([], "20251115")
dataset = combiner.convert_file_to_dataset(file_path)

assert "ecliptic_longitude_deg" in dataset.data_vars
assert "ecliptic_latitude_deg" in dataset.data_vars
assert len(dataset["ecliptic_longitude_deg"]) == 0
assert len(dataset["ecliptic_latitude_deg"]) == 0


def test_glows_uv_sources_combiner(glows_ancillary_filepath):
file_path = (
glows_ancillary_filepath / "imap_glows_map-of-uv-sources_20250923_v002.dat"
Expand Down Expand Up @@ -300,6 +313,21 @@ def test_glows_exclusions_by_instr_team_combiner(glows_ancillary_filepath):
assert combiner.timestamped_data[0].version == "v002"


def test_glows_l2_calibration_combiner(tmp_path):
file_path = tmp_path / "imap_glows_l2-calibration_20251112_v001.dat"
file_path.write_text(
"# header\n2025-11-13T18:12:48 1.020\n2025-11-14T09:58:04 0.849\n"
)

combiner = GlowsAncillaryCombiner([], "20251115")
dataset = combiner.convert_file_to_dataset(file_path)

assert "start_time_utc" in dataset.data_vars
assert "cps_per_r" in dataset.data_vars
assert len(dataset["cps_per_r"]) == 2
assert dataset["cps_per_r"].values[0] == pytest.approx(1.020)


def test_ancillary_combiner_empty_input():
"""Test AncillaryCombiner with empty input list."""
combiner = AncillaryCombiner([], "20251031")
Expand Down
Loading