Skip to content

Commit

Permalink
Decode all timestamps to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinkens committed Dec 19, 2023
1 parent edf933a commit 414f2d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
33 changes: 23 additions & 10 deletions satpy/readers/satpy_cf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,23 +334,36 @@ class DatasetAttributeDecoder:

def decode_attrs(self, dataset):
"""Decode dataset attributes."""
self._decode_dict_type_attrs(dataset)
self._decode_dict_type_attrs(dataset.attrs)
self._decode_timestamps(dataset.attrs)

def _decode_dict_type_attrs(self, data):
for key, val in data.attrs.items():
data.attrs[key] = self._str2dict(val)
def _decode_dict_type_attrs(self, attrs):
for key, val in attrs.items():
attrs[key] = self._str2dict(val)

def _str2dict(self, val):
"""Convert string to dictionary."""
if isinstance(val, str) and val.startswith("{"):
val = json.loads(val, object_hook=_datetime_parser)
val = json.loads(val, object_hook=_datetime_parser_json)
return val

def _decode_timestamps(self, attrs):
for key, value in attrs.items():
timestamp = _str2datetime(value)
if timestamp:
attrs[key] = timestamp

def _datetime_parser(json_dict):

def _datetime_parser_json(json_dict):
for key, value in json_dict.items():
try:
json_dict[key] = datetime.fromisoformat(value)
except (TypeError, ValueError):
pass
timestamp = _str2datetime(value)
if timestamp:
json_dict[key] = timestamp
return json_dict


def _str2datetime(string):
try:
return datetime.fromisoformat(string)
except (TypeError, ValueError):
return None
11 changes: 10 additions & 1 deletion satpy/tests/reader_tests/test_satpy_cf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def common_attrs(area):
"end_time": datetime(2019, 4, 1, 12, 15),
"platform_name": "tirosn",
"orbit_number": 99999,
"area": area
"area": area,
"my_timestamp": datetime(2000, 1, 1)
}


Expand Down Expand Up @@ -446,6 +447,14 @@ def test_decoding_of_dict_type_attributes(self, cf_scene, nc_filename):
new_attrs = scn_["image0"].attrs[attr_name]
assert new_attrs == orig_attrs

def test_decoding_of_timestamps(self, cf_scene, nc_filename):
"""Test decoding of timestamps."""
cf_scene.save_datasets(writer="cf", filename=nc_filename)
scn = Scene(reader="satpy_cf_nc", filenames=[nc_filename])
scn.load(["image0"])
expected = cf_scene["image0"].attrs["my_timestamp"]
assert scn["image0"].attrs["my_timestamp"] == expected

def test_write_and_read_from_two_files(self, nc_filename, nc_filename_i):
"""Save two datasets with different resolution and read the solar_zenith_angle again."""
_create_test_netcdf(nc_filename, resolution=742)
Expand Down

0 comments on commit 414f2d6

Please sign in to comment.