Skip to content

Commit

Permalink
Merge pull request #969 from djhoese/bugfix-clavrx-3
Browse files Browse the repository at this point in the history
Fix HDF4 handling of scalar attributes
  • Loading branch information
djhoese committed Nov 15, 2019
2 parents bf60e6a + 9d7cad0 commit a357f4a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions satpy/readers/hdf4_utils.py
Expand Up @@ -68,13 +68,16 @@ 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)
if issubclass(value.dtype.type, np.string_) and not value.shape:
value = np.asscalar(value)
if issubclass(value.dtype.type, (np.string_, np.unicode_)) and not value.shape:
value = value.item() # convert to scalar
if not isinstance(value, str):
# python 3 - was scalar numpy array of bytes
# otherwise python 2 - scalar numpy array of 'str'
value = value.decode()
self.file_content["{}/attr/{}".format(name, key)] = value
elif not value.shape:
# convert to a scalar
self.file_content["{}/attr/{}".format(name, key)] = value.item()
else:
self.file_content["{}/attr/{}".format(name, key)] = value

Expand Down
3 changes: 3 additions & 0 deletions satpy/tests/reader_tests/test_hdf4_utils.py
Expand Up @@ -109,9 +109,12 @@ def test_all_basic(self):
self.assertEqual(attrs.get('test_attr_int'), 0)
self.assertEqual(attrs.get('test_attr_float'), 1.2)

self.assertIsInstance(file_handler['/attr/test_attr_str'], str)
self.assertEqual(file_handler['/attr/test_attr_str'], 'test_string')
# self.assertEqual(file_handler['/attr/test_attr_str_arr'], 'test_string2')
self.assertIsInstance(file_handler['/attr/test_attr_int'], int)
self.assertEqual(file_handler['/attr/test_attr_int'], 0)
self.assertIsInstance(file_handler['/attr/test_attr_float'], float)
self.assertEqual(file_handler['/attr/test_attr_float'], 1.2)

self.assertIsInstance(file_handler.get('ds1_f'), xr.DataArray)
Expand Down

0 comments on commit a357f4a

Please sign in to comment.