From 1e33119ddce6d4110739adb6cfe931ea044e9fa8 Mon Sep 17 00:00:00 2001 From: Matthias Probst Date: Tue, 5 Dec 2023 13:14:31 +0100 Subject: [PATCH] resolved issue #1 --- h5rdmtoolbox/wrapper/core.py | 2 +- tests/test_xarray_export.py | 37 ++++++++++++++++++++++++++++++++++++ tests/wrapper/test_core.py | 8 ++++---- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 tests/test_xarray_export.py diff --git a/h5rdmtoolbox/wrapper/core.py b/h5rdmtoolbox/wrapper/core.py index dc1ffa43..e2472227 100644 --- a/h5rdmtoolbox/wrapper/core.py +++ b/h5rdmtoolbox/wrapper/core.py @@ -564,7 +564,7 @@ def create_time_dataset(self, where each datetime is converted to a string with ISO format""" if attrs is None: attrs = {} - attrs.update({'ISTIMEDS': True, + attrs.update({'ISTIMEDS': 1, 'TIMEFORMAT': 'ISO'}) if isinstance(data, np.ndarray): return self.create_string_dataset(name, data=[t.astype(datetime).isoformat() for t in data], diff --git a/tests/test_xarray_export.py b/tests/test_xarray_export.py new file mode 100644 index 00000000..ade95ae1 --- /dev/null +++ b/tests/test_xarray_export.py @@ -0,0 +1,37 @@ +import pathlib +import unittest + +import xarray as xr + +import h5rdmtoolbox as h5tbx +from datetime import datetime +logger = h5tbx.logger +# logger.setLevel('ERROR') +__this_dir__ = pathlib.Path(__file__).parent + + +class TestXrExport(unittest.TestCase): + + def test_export_1Dda(self): + with h5tbx.File() as h5: + ds = h5.create_dataset('ds', data=3, dtype='int64') + da = ds[()] + + self.assertIsInstance(da, xr.DataArray) + nc_fname = h5tbx.utils.generate_temporary_filename(suffix='.nc') + da.to_netcdf(nc_fname) + da2 = xr.open_dataarray(nc_fname) + self.assertIsInstance(da2, xr.DataArray) + self.assertEqual(da2[()], da[()]) + + def test_export_timeda(self): + with h5tbx.File() as h5: + ds = h5.create_time_dataset('ds', data=datetime.now()) + da = ds[()] + + self.assertIsInstance(da, xr.DataArray) + nc_fname = h5tbx.utils.generate_temporary_filename(suffix='.nc') + da.to_netcdf(nc_fname) + da2 = xr.open_dataarray(nc_fname) + self.assertIsInstance(da2, xr.DataArray) + self.assertEqual(da2[()], da[()]) diff --git a/tests/wrapper/test_core.py b/tests/wrapper/test_core.py index 6c55ba40..eda5009a 100644 --- a/tests/wrapper/test_core.py +++ b/tests/wrapper/test_core.py @@ -735,7 +735,7 @@ def test_time(self): tdata_np = np.asarray(tdata, dtype=np.datetime64) with h5tbx.File() as h5: h5.create_string_dataset('time', data=[t.isoformat() for t in tdata], - attrs={'ISTIMEDS': True, + attrs={'ISTIMEDS': 1, 'TIMEFORMAT': 'ISO'}) tds = h5['time'][()] @@ -743,7 +743,7 @@ def test_time(self): tds2 = h5['time2'][()] h5.create_time_dataset('time3', data=tdata_np, - attrs={'ISTIMEDS': True, + attrs={'ISTIMEDS': 1, 'TIMEFORMAT': 'ISO'}) tds3 = h5['time3'][()] @@ -764,7 +764,7 @@ def test_time_as_coord(self): h5.create_time_dataset('time', data=[datetime.now(), datetime.now() + timedelta(hours=1), datetime.now() + timedelta(hours=3)], - attrs={'ISTIMEDS': True, + attrs={'ISTIMEDS': 1, 'TIMEFORMAT': 'ISO'}, make_scale=True) h5.create_dataset('vel', data=[1, 2, -3], attach_scale='time') v = h5.vel[()] @@ -778,7 +778,7 @@ def test_multidim_time_ds(self): datetime.now() + timedelta(hours=6), datetime.now() + timedelta(hours=10)] ], - attrs={'ISTIMEDS': True, + attrs={'ISTIMEDS': 1, 'TIMEFORMAT': 'ISO'}) t = h5.time[()] self.assertIsInstance(t, xr.DataArray)