Skip to content

Commit

Permalink
resolved issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasprobst committed Dec 5, 2023
1 parent 4301413 commit 1e33119
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion h5rdmtoolbox/wrapper/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
37 changes: 37 additions & 0 deletions tests/test_xarray_export.py
Original file line number Diff line number Diff line change
@@ -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[()])
8 changes: 4 additions & 4 deletions tests/wrapper/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,15 +735,15 @@ 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'][()]

h5.create_time_dataset('time2', data=tdata)
tds2 = h5['time2'][()]

h5.create_time_dataset('time3', data=tdata_np,
attrs={'ISTIMEDS': True,
attrs={'ISTIMEDS': 1,
'TIMEFORMAT': 'ISO'})
tds3 = h5['time3'][()]

Expand All @@ -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[()]
Expand All @@ -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)
Expand Down

0 comments on commit 1e33119

Please sign in to comment.