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

Check ancillary_variables in exclude_attrs before get_extra_ds #1140

Merged
merged 6 commits into from
Jan 4, 2021
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
23 changes: 23 additions & 0 deletions satpy/tests/writer_tests/test_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ def test_save_array_coords(self):
self.assertEqual(f['test-array'].attrs['prerequisites'],
expected_prereq)

def test_ancillary_variables(self):
"""Test ancillary_variables cited each other."""
import xarray as xr
from satpy import Scene
scn = Scene()
start_time = datetime(2018, 5, 30, 10, 0)
end_time = datetime(2018, 5, 30, 10, 15)
da = xr.DataArray([1, 2, 3],
attrs=dict(start_time=start_time,
end_time=end_time,
prerequisites=[DatasetID('hej')]))
scn['test-array-1'] = da
scn['test-array-2'] = da.copy()
scn['test-array-1'].attrs['ancillary_variables'] = [scn['test-array-2']]
scn['test-array-2'].attrs['ancillary_variables'] = [scn['test-array-1']]
with TempFile() as filename:
scn.save_datasets(filename=filename, writer='cf')
with xr.open_dataset(filename) as f:
self.assertEqual(f['test-array-1'].attrs['ancillary_variables'],
'test-array-2')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the issue in xarray, ancillary_variables isn't decoded.
So, since save_datasets succeeds, I just check the name here.

self.assertEqual(f['test-array-2'].attrs['ancillary_variables'],
'test-array-1')

def test_groups(self):
"""Test creating a file with groups."""
import xarray as xr
Expand Down
6 changes: 4 additions & 2 deletions satpy/writers/cf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ def create_grid_mapping(area):
return area.area_id, grid_mapping


def get_extra_ds(dataset):
def get_extra_ds(dataset, keys=None):
"""Get the extra datasets associated to *dataset*."""
ds_collection = {}
for ds in dataset.attrs.get('ancillary_variables', []):
ds_collection.update(get_extra_ds(ds))
if keys and ds.name not in keys:
keys.append(ds.name)
ds_collection.update(get_extra_ds(ds, keys))
ds_collection[dataset.attrs['name']] = dataset

return ds_collection
Expand Down