undo_xarray_floatification

typhon.utils.undo_xarray_floatification(ds, fields=None)[source]

convert floats back to ints in xarray dataset where appropriate

When xarray opens a NetCDF file with the default decode_cf=True, any integer values that have a _FillValue set are converted to float, such that any _FillValue-set values can be set to nan. Some datasets may have such _FillValue set even though they are never used. In this case, it may be desirable to convert those values back to the original dtype (which is preserved in the .encoding attribute), for example, when those integers are intended to be used as indices. This function takes an xarray Dataset, checks all the variables which originally have an integer dtype and a fillvalue set, and converts those back to int. Optionally only a subset of those is converted.

Use this function only when those fill values are not used. Behaviour when fill values are actually used is undefined.

Parameters
  • ds (xarray.Dataset) – xarray dataset to be converted. Will be

  • copied.

  • fields (Collection or None) – Describes what fields shall be converted. If not given or None (default), convert all fields that were originally ints but converted to float due to having a _FillValue set. Even when given, only fields meeting those criteria will be converted.

Returns

The same dataset but with changes as described above.