|
| 1 | +# /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import yaml |
| 10 | + |
| 11 | +from modacor.dataclasses.source_data import SourceData |
| 12 | + |
| 13 | +__coding__ = "utf-8" |
| 14 | +__author__ = "Brian R. Pauw" |
| 15 | +__license__ = "BSD3" |
| 16 | +__copyright__ = "Copyright 2025, The MoDaCor team" |
| 17 | +__date__ = "24/05/2025" |
| 18 | +__version__ = "20250524.1" |
| 19 | +__status__ = "Development" # "Development", "Production" |
| 20 | +from logging import WARNING |
| 21 | + |
| 22 | +import h5py |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +from modacor.administration.licenses import BSD3Clause as __license__ # noqa: F401 |
| 26 | +from modacor.dataclasses.messagehandler import MessageHandler |
| 27 | + |
| 28 | +from ..io_source import IoSource |
| 29 | + |
| 30 | +# end of header and standard imports |
| 31 | + |
| 32 | + |
| 33 | +class StaticMetadata(IoSource): |
| 34 | + """ |
| 35 | + This IoSource is used to load and make experiment metadata available to |
| 36 | + the processing pipeline modules. |
| 37 | + It can be filled in with information such as wavelength, |
| 38 | + geometry and other relevant information which is needed in multiple |
| 39 | + processing steps. |
| 40 | + The metadata can be loaded from a yaml file with mappings. this is set in the configuraiton |
| 41 | + The entries are returned as BaseData elements, with units and uncertainties. |
| 42 | + """ |
| 43 | + |
| 44 | + _data_cache: dict[str, SourceData] = None |
| 45 | + _static_metadata_cache: dict[str, Any] = None |
| 46 | + |
| 47 | + def __init__(self, source_reference: str, logging_level=WARNING): |
| 48 | + super().__init__(source_reference) |
| 49 | + self.logger = MessageHandler(level=logging_level, name="StaticMetadata") |
| 50 | + self._data_cache = {} # for values with units and uncertainties |
| 51 | + self._static_metadata_cache = {} # for other elements such as strings and tags |
| 52 | + |
| 53 | + def _load_from_yaml(self, file_path: Path) -> None: |
| 54 | + """ |
| 55 | + Load static metadata from a YAML file. |
| 56 | + This method should be implemented to parse the YAML file and populate |
| 57 | + the _data_cache with SourceData objects. |
| 58 | + """ |
| 59 | + assert file_path.exists(), f"Static metadataa file {file_path} does not exist." |
| 60 | + with open(file_path, "r") as f: |
| 61 | + data = yaml.safe_load(f) |
| 62 | + |
| 63 | + for key, entry in data.items(): |
| 64 | + if isinstance(entry, dict): |
| 65 | + if all(k in entry for k in ("value", "units", "variance")): |
| 66 | + self._data_cache[key] = SourceData( |
| 67 | + value=np.array(entry.pop("value", [])), |
| 68 | + units=entry.pop("units", "rankine"), |
| 69 | + variance=np.array(entry.pop("variance", [])), |
| 70 | + attributes=entry if entry else {}, |
| 71 | + ) |
| 72 | + else: |
| 73 | + # invalid entry, raise an error or log it |
| 74 | + self.logger.error( |
| 75 | + f"Invalid entry for key '{key}': {entry}. Expected 'value', 'units', and 'variance'." |
| 76 | + ) |
| 77 | + else: |
| 78 | + # Store other metadata as static metadata |
| 79 | + self._static_metadata_cache[key] = entry |
| 80 | + |
| 81 | + def get_static_metadata(self, data_key: str) -> Any: |
| 82 | + if data_key not in self._static_metadata_cache: |
| 83 | + self.logger.error(f"Static metadata key '{data_key}' not in cache.") |
| 84 | + return None |
| 85 | + |
| 86 | + return self._static_metadata_cache.get(data_key) |
| 87 | + |
| 88 | + def get_data(self, data_key: str) -> SourceData: |
| 89 | + """ |
| 90 | + Get the data from the HDF5 file. |
| 91 | + """ |
| 92 | + if data_key not in self._data_cache: |
| 93 | + self.logger.error(f"Data key '{data_key}' not in static metadata cache.") |
| 94 | + return None |
| 95 | + |
| 96 | + return self._data_cache.get(data_key) |
0 commit comments