From eb4b89c492b814daa5c595fce02111e3e9108635 Mon Sep 17 00:00:00 2001 From: "Adam.Dybbroe" Date: Thu, 1 Feb 2018 15:32:33 +0100 Subject: [PATCH] Bugfix viirs loading - picked from (xarray)develop branch Signed-off-by: Adam.Dybbroe --- satpy/readers/hdf5_utils.py | 6 +++++- satpy/readers/helper_functions.py | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/satpy/readers/hdf5_utils.py b/satpy/readers/hdf5_utils.py index e42a480d9b..463d91336c 100644 --- a/satpy/readers/hdf5_utils.py +++ b/satpy/readers/hdf5_utils.py @@ -59,7 +59,11 @@ def __init__(self, filename, filename_info, filetype_info): def _collect_attrs(self, name, attrs): for key, value in six.iteritems(attrs): value = np.squeeze(value) - self.file_content["{}/attr/{}".format(name, key)] = np2str(value) + fc_key = "{}/attr/{}".format(name, key) + try: + self.file_content[fc_key] = np2str(value) + except ValueError: + self.file_content[fc_key] = value def collect_metadata(self, name, obj): if isinstance(obj, h5py.Dataset): diff --git a/satpy/readers/helper_functions.py b/satpy/readers/helper_functions.py index c3d978316f..9c0a2c54fe 100644 --- a/satpy/readers/helper_functions.py +++ b/satpy/readers/helper_functions.py @@ -33,14 +33,26 @@ def np2str(value): - """Convert an np.string_ to str.""" - if issubclass(value.dtype.type, np.string_): + """Convert an `numpy.string_` to str. + + Args: + value (ndarray): scalar or 1-element numpy array to convert + + Raises: + ValueError: if value is array larger than 1-element or it is not of + type np.string_ or it is not a numpy array + + """ + if hasattr(value, 'dtype') and \ + issubclass(value.dtype.type, np.string_) and value.size == 1: value = np.asscalar(value) if not isinstance(value, str): # python 3 - was scalar numpy array of bytes # otherwise python 2 - scalar numpy array of 'str' value = value.decode() - return value + return value + else: + raise ValueError("Array is not a string type or is larger than 1") def get_geostationary_angle_extent(geos_area):