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

Allow user to explicitly disable coordinates attribute #5514

Merged
merged 10 commits into from
Jul 1, 2021
8 changes: 6 additions & 2 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,12 @@ def _encode_coordinates(variables, attributes, non_dim_coord_names):
)

# if coordinates set to None, don't write coordinates attribute
if "coordinates" in attrs and attrs.get("coordinates") is None:
if (
"coordinates" in attrs
and attrs.get("coordinates") is None
or "coordinates" in encoding
and encoding.get("coordinates") is None
):
continue

# this will copy coordinates from encoding to attrs if "coordinates" in attrs
Expand All @@ -776,7 +781,6 @@ def _encode_coordinates(variables, attributes, non_dim_coord_names):
# conventions, only do it if necessary.
# Reference discussion:
# http://mailman.cgd.ucar.edu/pipermail/cf-metadata/2014/007571.html

global_coordinates.difference_update(written_coords)
if global_coordinates:
attributes = dict(attributes)
Expand Down
33 changes: 21 additions & 12 deletions xarray/tests/test_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,33 @@ def test_do_not_overwrite_user_coordinates(self):
with pytest.raises(ValueError, match=r"'coordinates' found in both attrs"):
conventions.encode_dataset_coordinates(orig)

def test_emit_coordinates_attribute(self):
def test_emit_coordinates_attribute_in_attrs(self):
orig = Dataset(
{
"foo": (("x", "y"), np.random.randn(2, 3)),
"x": [10, 20],
"bar": ("x", [1, 2]),
}
{"a": 1, "b": 1},
coords={"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)},
)
orig = orig.assign_coords(
{"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)}
)
orig = orig.assign({"a": 1})
orig = orig.assign({"b": 1})

orig["a"].attrs["coordinates"] = None
enc, _ = conventions.encode_dataset_coordinates(orig)

# check coordiante attribute emitted for 'a'
# check coordinate attribute emitted for 'a'
assert enc["a"].attrs.get("coordinates") is None
dcherian marked this conversation as resolved.
Show resolved Hide resolved
assert enc["a"].encoding.get("coordinates") is None

# check coordinate attribute not emitted for 'b'
assert enc["b"].attrs.get("coordinates") == "t"
assert enc["b"].encoding.get("coordinates") is None

def test_emit_coordinates_attribute_in_encoding(self):
orig = Dataset(
{"a": 1, "b": 1},
coords={"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)},
)

orig["a"].encoding["coordinates"] = None
enc, _ = conventions.encode_dataset_coordinates(orig)

# check coordinate attribute emitted for 'a'
assert enc["a"].attrs.get("coordinates") is None
assert enc["a"].encoding.get("coordinates") is None

Expand Down