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

Pass variable name to encode_zarr_variable #8809

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions xarray/backends/common.py
Expand Up @@ -329,11 +329,11 @@ def encode(self, variables, attributes):
attributes : dict-like

"""
variables = {k: self.encode_variable(v) for k, v in variables.items()}
variables = {k: self.encode_variable(v, k) for k, v in variables.items()}
attributes = {k: self.encode_attribute(v) for k, v in attributes.items()}
return variables, attributes

def encode_variable(self, v):
def encode_variable(self, v, name):
slevang marked this conversation as resolved.
Show resolved Hide resolved
"""encode one variable"""
return v

Expand Down Expand Up @@ -480,7 +480,7 @@ def encode(self, variables, attributes):
# All NetCDF files get CF encoded by default, without this attempting
# to write times, for example, would fail.
variables, attributes = cf_encoder(variables, attributes)
variables = {k: self.encode_variable(v) for k, v in variables.items()}
variables = {k: self.encode_variable(v, k) for k, v in variables.items()}
attributes = {k: self.encode_attribute(v) for k, v in attributes.items()}
return variables, attributes

Expand Down
2 changes: 1 addition & 1 deletion xarray/backends/h5netcdf_.py
Expand Up @@ -260,7 +260,7 @@ def set_dimension(self, name, length, is_unlimited=False):
def set_attribute(self, key, value):
self.ds.attrs[key] = value

def encode_variable(self, variable):
def encode_variable(self, variable, name):
return _encode_nc4_variable(variable)

def prepare_variable(
Expand Down
2 changes: 1 addition & 1 deletion xarray/backends/netCDF4_.py
Expand Up @@ -490,7 +490,7 @@ def set_attribute(self, key, value):
else:
self.ds.setncattr(key, value)

def encode_variable(self, variable):
def encode_variable(self, variable, name):
variable = _force_native_endianness(variable)
if self.format == "NETCDF4":
variable = _encode_nc4_variable(variable)
Expand Down
2 changes: 1 addition & 1 deletion xarray/backends/scipy_.py
Expand Up @@ -209,7 +209,7 @@ def set_attribute(self, key, value):
value = encode_nc3_attr_value(value)
setattr(self.ds, key, value)

def encode_variable(self, variable):
def encode_variable(self, variable, name):
variable = encode_nc3_variable(variable)
return variable

Expand Down
4 changes: 2 additions & 2 deletions xarray/backends/zarr.py
Expand Up @@ -569,8 +569,8 @@ def set_dimensions(self, variables, unlimited_dims=None):
def set_attributes(self, attributes):
_put_attrs(self.zarr_group, attributes)

def encode_variable(self, variable):
variable = encode_zarr_variable(variable)
def encode_variable(self, variable, name):
variable = encode_zarr_variable(variable, name=name)
return variable

def encode_attribute(self, a):
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_backends.py
Expand Up @@ -1251,6 +1251,16 @@ def test_multiindex_not_implemented(self) -> None:
with self.roundtrip(ds_reset) as actual:
assert_identical(actual, ds_reset)

def test_reset_multiindex_as_indexvariable(self) -> None:
# regression noted in https://github.com/xarray-contrib/xeofs/issues/148
ds = xr.Dataset(coords={"dim1": [1, 2, 3]})
ds = ds.stack(feature=["dim1"])
ds_reset = ds.reset_index("feature")
# Convert dim1 back to an IndexVariable, should still be able to serialize
ds_reset["dim1"] = ds_reset.dim1.variable.to_index_variable()
with self.roundtrip(ds_reset) as actual:
assert_identical(actual, ds_reset)


class NetCDFBase(CFEncodedBase):
"""Tests for all netCDF3 and netCDF4 backends."""
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_conventions.py
Expand Up @@ -449,7 +449,7 @@ def test_decode_cf_time_kwargs(self) -> None:


class CFEncodedInMemoryStore(WritableCFDataStore, InMemoryDataStore):
def encode_variable(self, var):
def encode_variable(self, var, name):
"""encode one variable"""
coder = coding.strings.EncodedStringCoder(allows_unicode=True)
var = coder.encode(var)
Expand Down