Skip to content

Commit

Permalink
Add diff and json feature to eventio_print_simtel_metaparams
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Feb 14, 2024
1 parent 16a6061 commit 0d3e290
Showing 1 changed file with 64 additions and 14 deletions.
78 changes: 64 additions & 14 deletions src/eventio/scripts/print_simtel_metaparams.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
import sys

from astropy.utils.misc 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')
parser.add_argument('--tel-diff', nargs=2, type=int)
parser.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, "telescope_meta": 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 +62,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

0 comments on commit 0d3e290

Please sign in to comment.