Skip to content

Commit

Permalink
selectable keys
Browse files Browse the repository at this point in the history
  • Loading branch information
schaten committed Nov 30, 2023
1 parent 44b00bd commit e166cce
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ scripts =
twixtools/twixzip.py
utils/convert_to_cfl.py
utils/rotate_3Dcfl.py
utils/dathdr2json.py
utils/datmeta2json.py
zip_safe = false
test_suite = test

Expand Down
31 changes: 0 additions & 31 deletions utils/dathdr2json.py

This file was deleted.

49 changes: 49 additions & 0 deletions utils/datmeta2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import twixtools
import json
import sys

parser = argparse.ArgumentParser(description="Output the header of a .dat file")
parser.add_argument("datfile", type=str, help="Path to .dat file")
parser.add_argument("--out", type=str, help="Output file / prefix instead of stdout")
parser.add_argument("--keys", type=str, help="Comma-separated list of hdrs to obtain; h to list available keys")
args = parser.parse_args()

twix = twixtools.read_twix(args.datfile, parse_data=False, verbose=False)

if args.keys:
selected_keys = [k.strip() for k in args.keys.split(',')]

if args.keys and 'h' == selected_keys[0]:
print(f"Allowed key types: {','.join([k for k in twix[-1]['hdr'].keys() if not '_raw' in k])}")
sys.exit(0)

for i, twix_i in enumerate(twix):

del_keys = []

for key in selected_keys:
if not key in twix_i['hdr'].keys():
print(f"Requested key {key} not found in twix header no. {i}", file=sys.stderr)
sys.exit(1)

for key in twix_i['hdr']:
if "_raw" in key:
del_keys.append(key)
elif args.keys and not key in selected_keys:
del_keys.append(key)

for k in del_keys:
del twix_i['hdr'][k]

if args.out is None:
print(json.dumps(twix_i['hdr'], indent=4))
else:
ofname = args.out
if len(twix) > 1:
ofname += f"_{i}"
with open(ofname, "w") as f:
print(json.dumps(twix_i['hdr'], indent=4), file=f)

0 comments on commit e166cce

Please sign in to comment.