libera_utils.io.netcdf.apply_fill_and_valid_range#

libera_utils.io.netcdf.apply_fill_and_valid_range(raw: ndarray, fill_value: float | int | None = None, valid_range: tuple[float, float] | None = None) → ndarray#

Return raw as float64 with fill / out-of-range values set to NaN.

netCDF variables flag missing data in two ways that must both be honored: an explicit _FillValue sentinel (e.g. -999.0 for a float field, 32767 for int16) and a valid_range attribute outside of which values are invalid. The fill comparison is made on the raw array, in its own dtype, before any cast, so an integer sentinel that float32 cannot represent exactly still matches. A fill the raw integer dtype cannot hold (-999 on uint8, 1.5 on int16) matches nothing, rather than wrapping onto a valid value.

Parameters:
  • raw (np.ndarray) – Raw values read from the file. May be a numpy.ma.MaskedArray.

  • fill_value (float or int, optional) – Sentinel value to treat as missing, compared in the raw dtype. None skips the fill check.

  • valid_range (tuple of (float, float), optional) – Inclusive (low, high) bounds; values outside are set to NaN. The order is normalized internally, so (1100, 10) is treated the same as (10, 1100). None skips the range check.

Returns:

float64 array, same shape as raw, with invalid entries as NaN.

Return type:

np.ndarray