Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HDF4 handling of scalar attributes #969

Merged
merged 1 commit into from Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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