Skip to content

Commit

Permalink
Merge pull request #273 from cta-observatory/diff_metaparams
Browse files Browse the repository at this point in the history
Add diff and json feature to eventio_print_simtel_metaparams
  • Loading branch information
maxnoe authored Feb 15, 2024
2 parents 16a6061 + b2d8b69 commit 87d6314
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 15 deletions.
80 changes: 66 additions & 14 deletions src/eventio/scripts/print_simtel_metaparams.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
import sys

import json
from eventio import EventIOFile
from argparse import ArgumentParser
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from eventio.simtel import HistoryMeta
from eventio.search_utils import yield_toplevel_of_type
from difflib import unified_diff

parser = ArgumentParser()
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('inputfile')
parser.add_argument('--encoding', default='utf8', help='Encoding to use for decoding METAPARAMs')

group = parser.add_mutually_exclusive_group()
group.add_argument('--tel-diff', nargs=2, type=int)
group.add_argument("--json", action="store_true", help="output as json")


def print_metaparams():
args = parser.parse_args()

with EventIOFile(args.inputfile) as f:
global_meta, telescope_meta = read_meta(args.inputfile, args.encoding)

if args.tel_diff is not None:
tel_a, tel_b = args.tel_diff

meta_a = telescope_meta[tel_a]
meta_b = telescope_meta[tel_b]

diff = unified_diff(format_meta(meta_a).splitlines(), format_meta(meta_b).splitlines())
for line in diff:
print(line)
sys.exit(0)

if args.json:
meta = {"global": global_meta, "telescopes": telescope_meta}
print(json.dumps(meta, indent=2))
sys.exit(0)

if global_meta is not None:
print_meta(global_meta)

for tel_id, meta in telescope_meta.items():
print()
print_meta(meta, tel_id=tel_id)


def read_meta(path, encoding):
global_meta = None
telescope_meta = {}

with EventIOFile(path) as f:
found_meta = False

for o in f:
Expand All @@ -27,18 +64,33 @@ def print_metaparams():
continue

if o.header.id == -1:
s = "Global METAPARAMs"
print()
print(s)
print(len(s) * "-")
global_meta = decode(o.parse(), encoding)
else:
s = f"METAPARAMs for telescope={o.header.id}"
print()
print(s)
print(len(s) * "-")
telescope_meta[o.header.id] = decode(o.parse(), encoding)

return global_meta, telescope_meta


def decode(meta, encoding):
return {
k.decode(encoding): v.decode(encoding)
for k, v in meta.items()
}


def format_meta(meta):
return "\n".join(f"{k} = {v}" for k, v in meta.items())


def print_meta(meta, tel_id=None):
if tel_id is None:
title = "Global METAPARAMs"
else:
title = f"METAPARAMs for telescope={tel_id}"

for k, v in o.parse().items():
print(k.decode(args.encoding), "=", v.decode(args.encoding))
print(title)
print(len(title) * "-")
print(format_meta(meta))


def main():
Expand Down
37 changes: 36 additions & 1 deletion tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import tempfile
from pathlib import Path
import json

prod2_path = 'tests/resources/gamma_test.simtel.gz'
prod4b_astri_file = 'tests/resources/gamma_20deg_0deg_run103___cta-prod4-sst-astri_desert-2150m-Paranal-sst-astri.simtel.gz'
Expand All @@ -17,7 +18,8 @@ def run_command(*args):
result = sp.run(args, stdout=sp.PIPE, stderr=sp.PIPE, encoding='utf-8')

if result.returncode != 0:
raise IOError(f'Running {args} failed, output: \n {result.stdout}')
msg = f'Running {args} failed, stderr:\n{result.stderr}\n\nstdout:\n{result.stdout}'
raise IOError(msg)

return result

Expand All @@ -42,6 +44,17 @@ def test_print_simtel_history():
CAMERA_CONFIG_VERSION = 2021-01-06
'''

expected_diff = '''
OPTICS_CONFIG_NAME = LST
-OPTICS_CONFIG_VARIANT = LST-1 prototype
+OPTICS_CONFIG_VARIANT =
OPTICS_CONFIG_VERSION = 2020-04-29
CAMERA_CONFIG_NAME = LST
-CAMERA_CONFIG_VARIANT = LST-1 prototype, with nsb_autoscale_airmass
+CAMERA_CONFIG_VARIANT = LST camera, with nsb_autoscale_airmass
CAMERA_CONFIG_VERSION = 2020-11-24
'''


def test_print_simtel_metaparams():
result = run_command(
Expand All @@ -55,6 +68,28 @@ def test_print_simtel_metaparams():

assert expected_metaparams in result.stdout

result = run_command(
"eventio_print_simtel_metaparams",
"tests/resources/history_meta_75.simtel.zst",
"--tel-diff",
"1",
"2",
)
assert expected_diff in result.stdout

result = run_command(
"eventio_print_simtel_metaparams",
"tests/resources/history_meta_75.simtel.zst",
"--json",
)
data = json.loads(result.stdout)
assert "global" in data
assert "telescopes" in data
for tel_id in range(1, 20):
# keys in json are always str
assert str(tel_id) in data["telescopes"]
assert "OPTICS_CONFIG_NAME" in data["telescopes"][str(tel_id)]


def test_print_object_information():
run_command('eventio_print_object_information', prod4b_astri_file)
Expand Down

0 comments on commit 87d6314

Please sign in to comment.