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

Function _encode_unsigned_fill_value

xarray/coding/variables.py:239–266  ·  view source on GitHub ↗
(
    name: T_Name,
    fill_value: Any,
    encoded_dtype: np.dtype,
)

Source from the content-addressed store, hash-verified

237
238
239def _encode_unsigned_fill_value(
240 name: T_Name,
241 fill_value: Any,
242 encoded_dtype: np.dtype,
243) -> Any:
244 try:
245 if hasattr(fill_value, "item"):
246 # if numpy type, convert to python native integer to determine overflow
247 # otherwise numpy unsigned ints will silently cast to the signed counterpart
248 fill_value = fill_value.item()
249 # passes if provided fill value fits in encoded on-disk type
250 new_fill = encoded_dtype.type(fill_value)
251 except OverflowError:
252 encoded_kind_str = "signed" if encoded_dtype.kind == "i" else "unsigned"
253 warnings.warn(
254 f"variable {name!r} will be stored as {encoded_kind_str} integers "
255 f"but _FillValue attribute can't be represented as a "
256 f"{encoded_kind_str} integer.",
257 SerializationWarning,
258 stacklevel=3,
259 )
260 # user probably provided the fill as the in-memory dtype,
261 # convert to on-disk type to match CF standard
262 orig_kind = "u" if encoded_dtype.kind == "i" else "i"
263 orig_dtype = np.dtype(f"{orig_kind}{encoded_dtype.itemsize}")
264 # use view here to prevent OverflowError
265 new_fill = np.array(fill_value, dtype=orig_dtype).view(encoded_dtype).item()
266 return new_fill
267
268
269class CFMaskCoder(VariableCoder):

Callers 1

encodeMethod · 0.85

Calls 1

dtypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…