MCPcopy
hub / github.com/pydata/xarray / encode

Method encode

xarray/coding/times.py:1502–1522  ·  view source on GitHub ↗
(self, variable: Variable, name: T_Name = None)

Source from the content-addressed store, hash-verified

1500 self.decode_via_dtype = decode_via_dtype
1501
1502 def encode(self, variable: Variable, name: T_Name = None) -> Variable:
1503 if np.issubdtype(variable.dtype, np.timedelta64):
1504 dims, data, attrs, encoding = unpack_for_encoding(variable)
1505 dtype = encoding.get("dtype", None)
1506 units = encoding.pop("units", None)
1507
1508 # in the case of packed data we need to encode into
1509 # float first, the correct dtype will be established
1510 # via CFScaleOffsetCoder/CFMaskCoder
1511 if "add_offset" in encoding or "scale_factor" in encoding:
1512 dtype = data.dtype if data.dtype.kind == "f" else "float64"
1513
1514 resolution, _ = np.datetime_data(variable.dtype)
1515 attrs_dtype = f"timedelta64[{resolution}]"
1516 safe_setitem(attrs, "dtype", attrs_dtype, name=name)
1517
1518 data, units = encode_cf_timedelta(data, units, dtype)
1519 safe_setitem(attrs, "units", units, name=name)
1520 return Variable(dims, data, attrs, encoding, fastpath=True)
1521 else:
1522 return variable
1523
1524 def decode(self, variable: Variable, name: T_Name = None) -> Variable:
1525 units = variable.attrs.get("units", None)

Callers 1

Calls 5

unpack_for_encodingFunction · 0.90
safe_setitemFunction · 0.90
VariableClass · 0.90
encode_cf_timedeltaFunction · 0.85
getMethod · 0.45

Tested by 1