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

Method prepare_variable

xarray/backends/h5netcdf_.py:372–449  ·  view source on GitHub ↗
(
        self, name, variable, check_encoding=False, unlimited_dims=None
    )

Source from the content-addressed store, hash-verified

370 return _encode_nc4_variable(variable, name=name)
371
372 def prepare_variable(
373 self, name, variable, check_encoding=False, unlimited_dims=None
374 ):
375 import h5py
376
377 _ensure_no_forward_slash_in_name(name)
378 attrs = variable.attrs.copy()
379 dtype = _get_datatype(
380 variable, nc_format=self.format, raise_on_invalid_encoding=check_encoding
381 )
382
383 fillvalue = attrs.pop("_FillValue", None)
384
385 if dtype is str:
386 dtype = h5py.special_dtype(vlen=str)
387
388 # check enum metadata and use h5netcdf.core.EnumType
389 if (
390 hasattr(self.ds, "enumtypes")
391 and (meta := np.dtype(dtype).metadata)
392 and (e_name := meta.get("enum_name"))
393 and (e_dict := meta.get("enum"))
394 ):
395 dtype = _build_and_get_enum(self, name, dtype, e_name, e_dict)
396 encoding = _extract_h5nc_encoding(variable, raise_on_invalid=check_encoding)
397 kwargs = {}
398
399 # Convert from NetCDF4-Python style compression settings to h5py style
400 # If both styles are used together, h5py takes precedence
401 # If set_encoding=True, raise ValueError in case of mismatch
402 if encoding.pop("zlib", False):
403 if check_encoding and encoding.get("compression") not in (None, "gzip"):
404 raise ValueError("'zlib' and 'compression' encodings mismatch")
405 encoding.setdefault("compression", "gzip")
406
407 if (
408 check_encoding
409 and "complevel" in encoding
410 and "compression_opts" in encoding
411 and encoding["complevel"] != encoding["compression_opts"]
412 ):
413 raise ValueError("'complevel' and 'compression_opts' encodings mismatch")
414 complevel = encoding.pop("complevel", 0)
415 if complevel != 0:
416 encoding.setdefault("compression_opts", complevel)
417
418 encoding["chunks"] = encoding.pop("chunksizes", None)
419
420 # Do not apply compression, filters or chunking to scalars.
421 if variable.shape:
422 for key in [
423 "compression",
424 "compression_opts",
425 "shuffle",
426 "chunks",
427 "fletcher32",
428 ]:
429 if key in encoding:

Callers

nothing calls this directly

Calls 9

_get_datatypeFunction · 0.90
_build_and_get_enumFunction · 0.90
encode_nc3_attr_valueFunction · 0.90
itemsMethod · 0.80
copyMethod · 0.45
dtypeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected