Repository: cta-observatory/ctapipe
Pull request #2106 (2022-10-26 10:18:20)

Correctly setup components in dump-instrument-info, add test
dump-instrument did not pass `parent` to EventSource, making it impossible to pass the focal length choice.


Patch:
diff --git a/ctapipe/tools/dump_instrument.py b/ctapipe/tools/dump_instrument.py
index 1e51ac51..09f72309 100644
--- a/ctapipe/tools/dump_instrument.py
+++ b/ctapipe/tools/dump_instrument.py
@@ -16,7 +16,6 @@ class DumpInstrumentTool(Tool):
     description = Unicode(__doc__)
     name = "ctapipe-dump-instrument"
 
-    infile = Path(exists=True, help="input simtelarray file").tag(config=True)
     outdir = Path(
         file_ok=False,
         directory_ok=True,
@@ -24,6 +23,7 @@ class DumpInstrumentTool(Tool):
         default_value=None,
         help="Output directory. If not given, the current working directory will be used.",
     ).tag(config=True)
+
     format = Enum(
         ["fits", "ecsv", "hdf5"],
         default_value="fits",
@@ -32,22 +32,26 @@ class DumpInstrumentTool(Tool):
     )
 
     aliases = {
-        ("i", "input"): "DumpInstrumentTool.infile",
+        ("i", "input"): "EventSource.input_url",
         ("f", "format"): "DumpInstrumentTool.format",
         ("o", "outdir"): "DumpInstrumentTool.outdir",
     }
 
+    classes = [EventSource]
+
     def setup(self):
-        with EventSource(self.infile) as source:
+        with EventSource(parent=self) as source:
+            self.infile = source.input_url
             self.subarray = source.subarray
 
     def start(self):
         if self.outdir is None:
             self.outdir = pathlib.Path(os.getcwd())
+
         self.outdir.mkdir(exist_ok=True, parents=True)
 
         if self.format == "hdf5":
-            self.subarray.to_hdf("subarray.h5")
+            self.subarray.to_hdf(self.outdir / "subarray.h5")
         else:
             self.write_camera_definitions()
             self.write_optics_descriptions()


Test patch:
diff --git a/ctapipe/tools/tests/test_tools.py b/ctapipe/tools/tests/test_tools.py
index 704f7029..834e9f53 100644
--- a/ctapipe/tools/tests/test_tools.py
+++ b/ctapipe/tools/tests/test_tools.py
@@ -149,20 +149,50 @@ def test_dump_instrument(tmp_path):
     from ctapipe.tools.dump_instrument import DumpInstrumentTool
 
     sys.argv = ["dump_instrument"]
-    tool = DumpInstrumentTool()
 
-    assert run_tool(tool, [f"--input={PROD5B_PATH}"], cwd=tmp_path) == 0
+    ret = run_tool(
+        DumpInstrumentTool(),
+        [f"--input={PROD5B_PATH}"],
+        cwd=tmp_path,
+        raises=True,
+    )
+    assert ret == 0
     assert (tmp_path / "FlashCam.camgeom.fits.gz").exists()
 
-    assert (
-        run_tool(tool, [f"--input={PROD5B_PATH}", "--format=ecsv"], cwd=tmp_path) == 0
+    ret = run_tool(
+        DumpInstrumentTool(),
+        [f"--input={PROD5B_PATH}", "--format=ecsv"],
+        cwd=tmp_path,
+        raises=True,
     )
-
+    assert ret == 0
     assert (tmp_path / "MonteCarloArray.optics.ecsv").exists()
 
-    assert (
-        run_tool(tool, [f"--input={PROD5B_PATH}", "--format=hdf5"], cwd=tmp_path) == 0
+    ret = run_tool(
+        DumpInstrumentTool(),
+        [f"--input={PROD5B_PATH}", "--format=hdf5"],
+        cwd=tmp_path,
+        raises=True,
     )
+    assert ret == 0
     assert (tmp_path / "subarray.h5").exists()
 
-    assert run_tool(tool, ["--help-all"], cwd=tmp_path) == 0
+    ret = run_tool(DumpInstrumentTool(), ["--help-all"], cwd=tmp_path, raises=True)
+    assert ret == 0
+
+    # test the tool uses options correctly
+    out = tmp_path / "foo"
+    out.mkdir()
+    ret = run_tool(
+        DumpInstrumentTool(),
+        [
+            f"--input={GAMMA_TEST_LARGE}",
+            "-o",
+            str(out),
+            "--SimTelEventSource.focal_length_choice=EQUIVALENT",
+        ],
+        cwd=tmp_path,
+        raises=True,
+    )
+    assert ret == 0
+    assert (out / "FlashCam.camgeom.fits.gz").exists()