gwexpy.interop.metpy_

Interoperate with MetPy xarray data arrays.


Interoperability with MetPy xarray DataArrays.

MetPy uses Pint quantities via .metpy.quantify() / .metpy.dequantify() and CF Convention axis attributes (_metpy_axis). This module strips the Pint layer, converts units to astropy via gwexpy.utils.units, and delegates to gwexpy.interop.xarray_.from_xarray_field().

References

https://unidata.github.io/MetPy/latest/

Functions

from_metpy_dataarray(cls, da, *[, ...])

Convert a MetPy-enhanced xarray.DataArray to a ScalarField.

gwexpy.interop.metpy_.from_metpy_dataarray(cls: type, da: Any, *, dequantify: bool = True, axis0_domain: Literal['time', 'frequency'] = 'time') ScalarField[source]

Convert a MetPy-enhanced xarray.DataArray to a ScalarField.

MetPy attaches Pint units to data arrays via .metpy.quantify(). This function strips the Pint layer (dequantify) and converts the unit to astropy.units before creating a ScalarField.

Parameters:
  • cls (type) – ScalarField class.

  • da (xarray.DataArray) – MetPy-enhanced DataArray. May have Pint-backed data or plain float64 data with a "units" attribute.

  • dequantify (bool, default True) – Call .metpy.dequantify() to strip Pint units and move them to attrs["units"]. Set to False if the array is already plain.

  • axis0_domain ({"time", "frequency"}, default "time") – Physical domain of axis0.

Return type:

ScalarField

Notes

MetPy must be installed for full functionality, but the converter also works with plain xarray DataArrays that have "units" and "_metpy_axis" attributes.