MCPcopy Index your code
hub / github.com/pydata/xarray / set_variables

Method set_variables

xarray/backends/zarr.py:1200–1348  ·  view source on GitHub ↗

This provides a centralized method to set the variables on the data store. Parameters ---------- variables : dict-like Dictionary of key/value (variable name / xr.Variable) pairs check_encoding_set : list-like List of variable

(
        self,
        variables: dict[str, Variable],
        check_encoding_set,
        writer,
        unlimited_dims=None,
    )

Source from the content-addressed store, hash-verified

1198 return zarr_array
1199
1200 def set_variables(
1201 self,
1202 variables: dict[str, Variable],
1203 check_encoding_set,
1204 writer,
1205 unlimited_dims=None,
1206 ):
1207 """
1208 This provides a centralized method to set the variables on the data
1209 store.
1210
1211 Parameters
1212 ----------
1213 variables : dict-like
1214 Dictionary of key/value (variable name / xr.Variable) pairs
1215 check_encoding_set : list-like
1216 List of variables that should be checked for invalid encoding
1217 values
1218 writer
1219 unlimited_dims : list-like
1220 List of dimension names that should be treated as unlimited
1221 dimensions.
1222 """
1223
1224 existing_keys = self.array_keys()
1225 is_zarr_v3_format = _zarr_v3() and self.zarr_group.metadata.zarr_format == 3
1226
1227 for vn, v in variables.items():
1228 name = _encode_variable_name(vn)
1229 attrs = v.attrs.copy()
1230 dims = v.dims
1231 dtype = v.dtype
1232 shape = v.shape
1233
1234 if self._use_zarr_fill_value_as_mask:
1235 fill_value = attrs.pop("_FillValue", None)
1236 else:
1237 fill_value = v.encoding.pop("fill_value", None)
1238 if fill_value is None and v.dtype.kind == "f":
1239 # For floating point data, Xarray defaults to a fill_value
1240 # of NaN (unlike Zarr, which uses zero):
1241 # https://github.com/pydata/xarray/issues/10646
1242 fill_value = np.nan
1243 if "_FillValue" in attrs:
1244 # replace with encoded fill value
1245 fv = attrs.pop("_FillValue")
1246 if fv is not None:
1247 attrs["_FillValue"] = FillValueCoder.encode(fv, dtype)
1248
1249 # _FillValue is never a valid encoding for Zarr
1250 # TODO: refactor this logic so we don't need to check this here
1251 if "_FillValue" in v.encoding:
1252 if v.encoding.get("_FillValue") is not None:
1253 raise ValueError("Zarr does not support _FillValue in encoding.")
1254 else:
1255 del v.encoding["_FillValue"]
1256
1257 zarr_shape = None

Callers 1

storeMethod · 0.95

Calls 15

array_keysMethod · 0.95
_open_existing_arrayMethod · 0.95
encode_attributeMethod · 0.95
_create_new_arrayMethod · 0.95
_encode_variable_nameFunction · 0.90
grid_rechunkFunction · 0.90
_zarr_v3Function · 0.85
itemsMethod · 0.80
copyMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected