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 saving a dataset with a prerequisites attrs to netcdf #448

Merged
merged 3 commits into from
Oct 16, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion satpy/tests/writer_tests/test_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import os
import sys
from datetime import datetime
from satpy import DatasetID

import numpy as np

Expand All @@ -49,14 +50,20 @@ def test_save_array(self):
end_time = datetime(2018, 5, 30, 10, 15)
scn['test-array'] = xr.DataArray([1, 2, 3],
attrs=dict(start_time=start_time,
end_time=end_time))
end_time=end_time,
prerequisites=[DatasetID('hej')]))
try:
handle, filename = tempfile.mkstemp()
os.close(handle)
scn.save_datasets(filename=filename, writer='cf')
import h5netcdf as nc4
with nc4.File(filename) as f:
self.assertTrue(all(f['test-array'][:] == [1, 2, 3]))
expected_prereq = ("DatasetID(name='hej', wavelength=None, "
"resolution=None, polarization=None, "
"calibration=None, level=None, modifiers=())")
self.assertEqual(f['test-array'].attrs['prerequisites'][0],
np.string_(expected_prereq))
finally:
os.remove(filename)

Expand Down
3 changes: 3 additions & 0 deletions satpy/writers/cf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from datetime import datetime

import xarray as xr
import numpy as np

from pyresample.geometry import AreaDefinition, SwathDefinition
from satpy.writers import Writer
Expand Down Expand Up @@ -191,6 +192,8 @@ def da2cf(dataarray, epoch=EPOCH):
new_data['y'].attrs['units'] = 'm'

new_data.attrs.setdefault('long_name', new_data.attrs.pop('name'))
if 'prerequisites' in new_data.attrs:
new_data.attrs['prerequisites'] = [np.string_(str(prereq)) for prereq in new_data.attrs['prerequisites']]
return new_data

def save_dataset(self, dataset, filename=None, fill_value=None, **kwargs):
Expand Down