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

Bugfix of link_coords #1588

Merged
merged 7 commits into from Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions satpy/tests/writer_tests/test_cf.py
Expand Up @@ -621,11 +621,15 @@ def test_link_coords(self):

data = [[1, 2], [3, 4]]
lon = np.zeros((2, 2))
lon2 = np.zeros((1, 2, 2))
lat = np.ones((2, 2))
datasets = {
'var1': xr.DataArray(data=data, dims=('y', 'x'), attrs={'coordinates': 'lon lat'}),
'var2': xr.DataArray(data=data, dims=('y', 'x')),
'var3': xr.DataArray(data=data, dims=('y', 'x'), attrs={'coordinates': 'lon2 lat'}),
'var4': xr.DataArray(data=data, dims=('y', 'x'), attrs={'coordinates': 'not_exist lon lat'}),
'lon': xr.DataArray(data=lon, dims=('y', 'x')),
'lon2': xr.DataArray(data=lon2, dims=('time', 'y', 'x')),
'lat': xr.DataArray(data=lat, dims=('y', 'x'))
}

Expand All @@ -642,6 +646,10 @@ def test_link_coords(self):
self.assertNotIn('lon', datasets['var2'].coords)
self.assertNotIn('lat', datasets['var2'].coords)

# The not existed dimension or coordinate should be dropped
zxdawn marked this conversation as resolved.
Show resolved Hide resolved
self.assertNotIn('time', datasets['var3'].coords)
self.assertNotIn('not_exist', datasets['var4'].coords)

def test_make_alt_coords_unique(self):
"""Test that created coordinate variables are unique."""
import xarray as xr
Expand Down
25 changes: 13 additions & 12 deletions satpy/writers/cf_writer.py
Expand Up @@ -235,29 +235,30 @@ def assert_xy_unique(datas):


def link_coords(datas):
"""Link datasets and coordinates.
"""Link dataarrays and coordinates.

If the `coordinates` attribute of a data array links to other datasets in the scene, for example
If the `coordinates` attribute of a data array links to other dataarrays in the scene, for example
`coordinates='lon lat'`, add them as coordinates to the data array and drop that attribute. In the final call to
`xr.Dataset.to_netcdf()` all coordinate relations will be resolved and the `coordinates` attributes be set
automatically.

"""
for ds_name, dataset in datas.items():
coords = dataset.attrs.get('coordinates', [])
if isinstance(coords, str):
coords = coords.split(' ')
for coord in coords:
if coord not in dataset.coords:
for da_name, data in datas.items():
declared_coordinates = data.attrs.get('coordinates', [])
if isinstance(declared_coordinates, str):
declared_coordinates = declared_coordinates.split(' ')
for coord in declared_coordinates:
if coord not in data.coords:
try:
dataset[coord] = datas[coord]
dimensions_not_in_data = list(set(datas[coord].dims) - set(data.dims))
data[coord] = datas[coord].squeeze(dimensions_not_in_data, drop=True)
except KeyError:
warnings.warn('Coordinate "{}" referenced by dataset {} does not exist, dropping reference.'.format(
coord, ds_name))
warnings.warn('Coordinate "{}" referenced by dataarray {} does not exist, dropping reference.'
.format(coord, da_name))
continue

# Drop 'coordinates' attribute in any case to avoid conflicts in xr.Dataset.to_netcdf()
dataset.attrs.pop('coordinates', None)
data.attrs.pop('coordinates', None)


def dataset_is_projection_coords(dataset):
Expand Down