Skip to content

Commit

Permalink
Fix handling of header attributes in CF writer
Browse files Browse the repository at this point in the history
Header attributes equal to zero were not included in the global
nc attributes.
  • Loading branch information
sfinkens committed Oct 25, 2019
1 parent e40961e commit 168321b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions satpy/tests/writer_tests/test_cf.py
Expand Up @@ -324,14 +324,19 @@ def test_header_attrs(self):
end_time=end_time))
with TempFile() as filename:
header_attrs = {'sensor': 'SEVIRI',
'orbit': None}
'orbit': None,
'empty': [],
'num': 1.1,
'zero': 0}
scn.save_datasets(filename=filename,
header_attrs=header_attrs,
writer='cf')
with xr.open_dataset(filename) as f:
self.assertTrue(f.attrs['sensor'] == 'SEVIRI')
self.assertTrue('sensor' in f.attrs.keys())
self.assertTrue('orbit' not in f.attrs.keys())
self.assertEqual(f.attrs['sensor'], 'SEVIRI')
self.assertEqual(f.attrs['num'], 1.1)
self.assertEqual(f.attrs['zero'], 0)
self.assertNotIn('orbit', f.attrs)
self.assertNotIn('empty', f.attrs)

def get_test_attrs(self):
"""Create some dataset attributes for testing purpose.
Expand Down
3 changes: 2 additions & 1 deletion satpy/writers/cf_writer.py
Expand Up @@ -638,7 +638,8 @@ def save_datasets(self, datasets, filename=None, groups=None, header_attrs=None,

root = xr.Dataset({}, attrs={'history': 'Created by pytroll/satpy on {}'.format(datetime.utcnow())})
if header_attrs is not None:
root.attrs.update({k: v for k, v in header_attrs.items() if v})
root.attrs.update({k: v for k, v in header_attrs.items()
if (np.isscalar(v) and v is not None) or v})
if groups is None:
# Groups are not CF-1.7 compliant
root.attrs['Conventions'] = CF_VERSION
Expand Down

0 comments on commit 168321b

Please sign in to comment.