From c594ffc6333e206fedc26e0292fad7564edb38b0 Mon Sep 17 00:00:00 2001 From: Maximilian Linhoff Date: Thu, 24 Oct 2024 12:14:02 +0200 Subject: [PATCH] Set null island as fallback reference location in Subarray.from_hdf, fixes #2626 --- docs/changes/2627.bugfix.rst | 6 +++++ src/ctapipe/instrument/subarray.py | 8 ++++++- src/ctapipe/tools/tests/test_process.py | 29 ++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 docs/changes/2627.bugfix.rst diff --git a/docs/changes/2627.bugfix.rst b/docs/changes/2627.bugfix.rst new file mode 100644 index 00000000000..4ed358e1ad8 --- /dev/null +++ b/docs/changes/2627.bugfix.rst @@ -0,0 +1,6 @@ +Fix that hdf5 files created with older versions of ctapipe, e.g. +the public dataset created with 0.17 can be read by ctapipe-process. +These files are missing the subarray reference location, which was +introduced in later versions of ctapipe. A dummy location (lon=0, lat=0) +is used for these now, the same value is already used for simtel files +lacking this information. diff --git a/src/ctapipe/instrument/subarray.py b/src/ctapipe/instrument/subarray.py index e69e34f77f3..eafd56c2a95 100644 --- a/src/ctapipe/instrument/subarray.py +++ b/src/ctapipe/instrument/subarray.py @@ -751,7 +751,6 @@ def from_hdf(cls, path, focal_length_choice=FocalLengthKind.EFFECTIVE): positions = np.column_stack([layout[f"pos_{c}"] for c in "xyz"]) - reference_location = None name = layout.meta.get("SUBARRAY", "Unknown") if "reference_itrs_x" in layout.meta: @@ -760,6 +759,13 @@ def from_hdf(cls, path, focal_length_choice=FocalLengthKind.EFFECTIVE): y=layout.meta["reference_itrs_y"] * u.m, z=layout.meta["reference_itrs_z"] * u.m, ) + else: + # "null island" dummy location for backwards compatibility with old files + reference_location = EarthLocation( + lon=0 * u.deg, + lat=0 * u.deg, + height=0 * u.m, + ) return cls( name=str(name), diff --git a/src/ctapipe/tools/tests/test_process.py b/src/ctapipe/tools/tests/test_process.py index 769574c71f4..17ba5ae845d 100644 --- a/src/ctapipe/tools/tests/test_process.py +++ b/src/ctapipe/tools/tests/test_process.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 - """ Test ctapipe-process on a few different use cases """ @@ -488,3 +487,31 @@ def test_only_trigger_and_simulation(tmp_path): assert len(events) == 7 assert "tels_with_trigger" in events.colnames assert "true_energy" in events.colnames + + +@pytest.mark.parametrize( + ("input_url", "args"), + [ + pytest.param( + "dataset://gamma_diffuse_dl2_train_small.dl2.h5", + ["--no-write-images", "--max-events=5"], + id="0.17", + ) + ], +) +def test_on_old_file(input_url, args, tmp_path): + config = resource_file("stage1_config.json") + + output_path = tmp_path / "test.dl1.h5" + run_tool( + ProcessorTool(), + argv=[ + f"--config={config}", + f"--input={input_url}", + f"--output={output_path}", + "--overwrite", + *args, + ], + cwd=tmp_path, + raises=True, + )