diff --git a/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py b/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py index fa2de1dd5d9c86c58ec070d9f54d5e3489dab0fb..ba5d413953ddfb885f778017979b33b8cc70cb50 100644 --- a/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py +++ b/CAL/CalibrationCommon/lib/datacontainers/holography_dataset.py @@ -1,12 +1,10 @@ import logging import os - import h5py -from lofar.calibration.common.datacontainers.holography_observation import HolographyObservation from .holography_dataset_definitions import * from .holography_specification import HolographySpecification - +from .holography_observation import HolographyObservation logger = logging.getLogger(__file__) @@ -544,13 +542,52 @@ class HolographyDataset(): @staticmethod def load_from_file(path): """ - It reads a holography dataset from an HDF5 file and returns a + Read a holography dataset from an HDF5 file and returns a HolographyDataset class :param path: path to file :return: the read dataset """ f = None + try: + result, f = HolographyDataset.open_holography_file(path) + + result.data = dict() + for reference_station in f["CROSSCORRELATION"].keys(): + for frequency in f["CROSSCORRELATION"][reference_station].keys(): + for beamlet in f["CROSSCORRELATION"][reference_station][frequency].keys(): + if reference_station not in result.data: + result.data[reference_station] = dict() + if frequency not in result.data[reference_station]: + result.data[reference_station][frequency] = dict() + result.data[reference_station][frequency][beamlet] = numpy.array( + f["CROSSCORRELATION"][reference_station][frequency][beamlet]) + + if '/DERIVED_DATA' in f: + result.derived_data = HolographyDataset._read_grouped_data(f, '/DERIVED_DATA') + except Exception as e: + logger.exception( + "Cannot read the Holography Data Set data from the HDF5 file \"%s\"." + " This is the exception that was thrown: %s", + path, e) + raise e + finally: + if f is not None: + f.close() + + return result + + @staticmethod + def open_holography_file(path: str): + """ + Opens the Holography HDF file for access and returns the Holography Dataset and + it's file descriptor + :param path: path to file + :return: the read dataset + :rtype: List[HolographyDataset, h5py.File] + """ + f = None + if not os.path.exists(path): raise FileNotFoundError(path) @@ -584,32 +621,26 @@ class HolographyDataset(): result.ra_dec[frequency][beamlet] = numpy.array(f["RA_DEC"][frequency][beamlet]) beamlets = set() - result.data = dict() for reference_station in f["CROSSCORRELATION"].keys(): for frequency in f["CROSSCORRELATION"][reference_station].keys(): for beamlet in f["CROSSCORRELATION"][reference_station][frequency].keys(): beamlets.add(int(beamlet)) - if reference_station not in result.data: - result.data[reference_station] = dict() - if frequency not in result.data[reference_station]: - result.data[reference_station][frequency] = dict() - result.data[reference_station][frequency][beamlet] = numpy.array( - f["CROSSCORRELATION"][reference_station][frequency][beamlet]) + + result.data = f['CROSSCORRELATION'] result.central_beamlets = HolographyDataset._read_grouped_data(f, HDS_CENTRAL_BEAMLETS) if '/DERIVED_DATA' in f: - result.derived_data = HolographyDataset._read_grouped_data(f, '/DERIVED_DATA') + result.derived_data = f['/DERIVED_DATA'] + except Exception as e: logger.exception( - "Cannot read the Holography Data Set data from the HDF5 file \"%s\". This is the exception that was thrown: %s", + "Cannot read the Holography Data Set data from the HDF5 file \"%s\"." + " This is the exception that was thrown: %s", path, e) raise e - finally: - if f is not None: - f.close() - return result + return result, f def store_to_file(self, path): """ @@ -679,7 +710,8 @@ class HolographyDataset(): except Exception as e: logger.exception( - "Cannot write the Holography Data Set data to the HDF5 file \"%s\". This is the exception that was thrown: %s", + "Cannot write the Holography Data Set data to the HDF5 file \"%s\"." + " This is the exception that was thrown: %s", path, e) raise e finally: