Skip to content

Commit

Permalink
Factorize attribute decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinkens committed Dec 19, 2023
1 parent 168ced2 commit a9b876a
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions satpy/readers/satpy_cf_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,10 @@ def get_dataset(self, ds_id, ds_info):
if name != ds_id["name"]:
data = data.rename(ds_id["name"])
data.attrs.update(nc.attrs) # For now add global attributes to all datasets
self._decode_dict_type_attrs(data)
decoder = DatasetAttributeDecoder()
decoder.decode_attrs(data)
return data

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

def get_area_def(self, dataset_id):
"""Get area definition from CF complient netcdf."""
try:
Expand All @@ -331,6 +328,24 @@ def get_area_def(self, dataset_id):
raise NotImplementedError


class DatasetAttributeDecoder:
"""Decode attributes from cf-compatible to Python object."""

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

def _decode_dict_type_attrs(self, data):
for key, val in data.attrs.items():
data.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)
return val


def _datetime_parser(json_dict):
import dateutil.parser
for key, value in json_dict.items():
Expand All @@ -339,10 +354,3 @@ def _datetime_parser(json_dict):
except (TypeError, ValueError):
pass
return json_dict


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

0 comments on commit a9b876a

Please sign in to comment.