From 098a79531c9d0e91277f5cb440191e0ec609387a Mon Sep 17 00:00:00 2001 From: Florez Ospina Juan Felipe Date: Thu, 3 Oct 2024 09:07:06 +0200 Subject: [PATCH] Added new instrument (flagging app) file reading capabilities. It includes two files a flag_reader.py that takes flag.json files produced by the app into a standard intermidiate representation, and a yaml file with instrument dependent description terms. Last, we modified the filereader_registry.py to find the new instrument file reader. --- .../dictionaries/ACSM_TOFWARE_flags.yaml | 0 instruments/readers/filereader_registry.py | 5 +++ instruments/readers/flag_reader.py | 36 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 instruments/dictionaries/ACSM_TOFWARE_flags.yaml create mode 100644 instruments/readers/flag_reader.py diff --git a/instruments/dictionaries/ACSM_TOFWARE_flags.yaml b/instruments/dictionaries/ACSM_TOFWARE_flags.yaml new file mode 100644 index 0000000..e69de29 diff --git a/instruments/readers/filereader_registry.py b/instruments/readers/filereader_registry.py index 2d893fb..de5d717 100644 --- a/instruments/readers/filereader_registry.py +++ b/instruments/readers/filereader_registry.py @@ -22,6 +22,11 @@ file_readers = { 'ACSM_TOFWARE_csv': lambda a1: read_txt_files_as_dict(a1, instruments_dir=default_instruments_dir, work_with_copy=False) } +# Add new "instrument reader (Data flagging app data)" +from instruments.readers.flag_reader import read_jsonflag_as_dict +file_extensions.append('.json') +file_readers.update({'ACSM_TOFWARE_flags_json' : lambda x: read_jsonflag_as_dict(x)}) + def compute_filereader_key_from_path(hdf5_file_path): """Constructs the key 'instrumentname_ext' based on hdf5_file_path, structured as /instrumentname/to/filename.ext, which access the file reader that should be used to read such a file. diff --git a/instruments/readers/flag_reader.py b/instruments/readers/flag_reader.py new file mode 100644 index 0000000..35df85e --- /dev/null +++ b/instruments/readers/flag_reader.py @@ -0,0 +1,36 @@ +import os +import sys +#root_dir = os.path.abspath(os.curdir) +#sys.path.append(root_dir) + +import json +import dima.utils.g5505_utils as utils + + + +def read_jsonflag_as_dict(path_to_file): + + + file_dict = {} + path_tail, path_head = os.path.split(path_to_file) + + file_dict['name'] = path_head + # TODO: review this header dictionary, it may not be the best way to represent header data + file_dict['attributes_dict'] = {} + file_dict['datasets'] = [] + + try: + with open(path_to_file, 'r') as stream: + flag = json.load(stream)#, Loader=json.FullLoader) + except (FileNotFoundError, json.JSONDecodeError) as exc: + print(exc) + + dataset = {} + dataset['name'] = 'data_table'#_numerical_variables' + dataset['data'] = utils.convert_attrdict_to_np_structured_array(flag) #df_numerical_attrs.to_numpy() + dataset['shape'] = dataset['data'].shape + dataset['dtype'] = type(dataset['data']) + + file_dict['datasets'].append(dataset) + + return file_dict \ No newline at end of file