Skip to content
Snippets Groups Projects
Commit 74748c4e authored by Mattia Mancini's avatar Mattia Mancini
Browse files

SSB-47: implemented storing in hdf5 file

parent f32eb766
No related branches found
No related tags found
1 merge request!44Merge back holography to master
......@@ -4,7 +4,7 @@ from re import fullmatch
from struct import iter_unpack, pack
from typing import BinaryIO
from typing import List
from h5py import File
from dataclasses import dataclass, asdict
from numpy import empty as empty_ndarray, ndarray, fromiter as array_from_iter, float64
......@@ -126,6 +126,15 @@ class CalibrationTable:
data_packed = pack('%sd' % len(data_flattened), *data_flattened)
f_stream.write(data_packed)
def store_to_hdf(self, file_descriptor: File, uri: str):
if uri not in file_descriptor:
file_descriptor[uri] = self.data
for key, value in asdict(self).items():
if key is 'data':
continue
file_descriptor[uri].attrs[key] = value
file_descriptor.flush()
def store_to_file(self, file_path):
with open(file_path, 'wb') as file_stream:
self.__serialize_header(file_stream)
......
import unittest
from lofar.calibration.common.datacontainers.calibration_table import CalibrationTable, UnvalidFileException
from h5py import File as H5File
import logging
from tempfile import NamedTemporaryFile
from numpy.testing import assert_array_equal
from numpy import array
CALIBRATION_TABLE_FILENAME = 't_calibration_table.in_CalTable-401-HBA-110_190.dat'
......@@ -29,6 +32,22 @@ class TestCalibrationTable(unittest.TestCase):
stored_calibration_table = CalibrationTable.load_from_file(temp_file.name)
self.assertEqual(test_calibration_table.observation_station,
stored_calibration_table.observation_station)
assert_array_equal(test_calibration_table.data,
stored_calibration_table.data)
def test_storing_to_hdf(self):
with NamedTemporaryFile('w+b') as temp_file:
h5_file = H5File(temp_file.name, 'w')
test_calibration_table = CalibrationTable.load_from_file(CALIBRATION_TABLE_FILENAME)
test_calibration_table.store_to_hdf(h5_file, '/calibration_table')
h5_file.close()
h5_file = H5File(temp_file.name, 'r')
self.assertEqual(
h5_file['/calibration_table'].attrs['observation_station'],
'CS401'
)
assert_array_equal(array(h5_file['/calibration_table']), test_calibration_table.data)
if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment