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

Method decode

xarray/coding/variables.py:373–428  ·  view source on GitHub ↗
(self, variable: Variable, name: T_Name = None)

Source from the content-addressed store, hash-verified

371 return Variable(dims, data, attrs, encoding, fastpath=True)
372
373 def decode(self, variable: Variable, name: T_Name = None):
374 raw_fill_dict, encoded_fill_values = _check_fill_values(
375 variable.attrs, name, variable.dtype
376 )
377 if "_Unsigned" not in variable.attrs and not raw_fill_dict:
378 return variable
379
380 dims, data, attrs, encoding = unpack_for_decoding(variable)
381
382 # Even if _Unsigned is used, retain on-disk _FillValue
383 for attr, value in raw_fill_dict.items():
384 safe_setitem(encoding, attr, value, name=name)
385
386 if "_Unsigned" in attrs:
387 unsigned = pop_to(attrs, encoding, "_Unsigned")
388 data = _convert_unsigned_fill_value(
389 name,
390 data,
391 unsigned,
392 raw_fill_dict.get("_FillValue"),
393 encoded_fill_values,
394 )
395
396 if encoded_fill_values:
397 dtype: np.typing.DTypeLike
398 decoded_fill_value: Any
399 # in case of packed data we have to decode into float
400 # in any case
401 if "scale_factor" in attrs or "add_offset" in attrs:
402 dtype, decoded_fill_value = (
403 _choose_float_dtype(data.dtype, attrs),
404 np.nan,
405 )
406 else:
407 # in case of no-packing special case DateTime/Timedelta to properly
408 # handle NaT, we need to check if time-like will be decoded
409 # or not in further processing
410 is_time_like = _is_time_like(attrs.get("units"))
411 if (
412 (is_time_like == "datetime" and self.decode_times)
413 or (is_time_like == "timedelta" and self.decode_timedelta)
414 ) and data.dtype.kind in "iu":
415 dtype = np.int64
416 decoded_fill_value = np.iinfo(np.int64).min
417 else:
418 dtype, decoded_fill_value = dtypes.maybe_promote(data.dtype)
419
420 transform = partial(
421 _apply_mask,
422 encoded_fill_values=encoded_fill_values,
423 decoded_fill_value=decoded_fill_value,
424 dtype=dtype,
425 )
426 data = lazy_elemwise_func(data, transform, dtype)
427
428 return Variable(dims, data, attrs, encoding, fastpath=True)
429
430

Calls 11

unpack_for_decodingFunction · 0.90
safe_setitemFunction · 0.90
pop_toFunction · 0.90
lazy_elemwise_funcFunction · 0.90
VariableClass · 0.90
_check_fill_valuesFunction · 0.85
_choose_float_dtypeFunction · 0.85
_is_time_likeFunction · 0.85
itemsMethod · 0.80
getMethod · 0.45