Huracan-project / huracanpy

Package for working with various forms of feature tracking data
MIT License
3 stars 1 forks source link

Error when a unit is misspecified in the netcdf files attributes #38

Open stella-bourdin opened 1 month ago

stella-bourdin commented 1 month ago

I have a file of tracks in NetCDF format (from TRACK). In there, slp units are "hpa" (which is incorrect for pint/metpy.units). When running get_pressure_cat, I can specify slp_units, but it will in fact ignore it... I get an error saying the unit 'hpa' is incorrect. I can fix it by rewriting the unit attribute, but I think it's desirable to be able to override it as an argument. It seems to have to do with the @preprocess_and_wrap, which I don't know how it works yet. Can you have a look at it @leosaffin ? Thanks!

>>> data = huracanpy.load("data/NATL.nc",)
>>> data.psl
<xarray.DataArray 'psl' (record: 24865)> Size: 199kB
array([1012.330017, 1015.466003, 1011.830994, ...,  993.535217,  996.716919,
        995.981079])
Dimensions without coordinates: record
Attributes:
    standard_name:  psl
    long_name:      air pressure at mean sea level
    units:          hpa

>>> huracanpy.utils.category.get_pressure_cat(data.psl, slp_units="hPa")
---------------------------------------------------------------------------
UndefinedUnitError                        Traceback (most recent call last)
Cell In[95], line 1
----> 1 huracanpy.utils.category.get_pressure_cat(data.psl, slp_units="hectoPascal")

[...]

UndefinedUnitError: 'hpa' is not defined in the unit registry

>>> data.psl.attrs["units"] = "hPa"
>>> huracanpy.utils.category.get_pressure_cat(data.psl,)
<xarray.DataArray (record: 24865)> Size: 199kB
<Quantity([-1. -1. -1. ...  0.  0.  0.], 'dimensionless')>
Dimensions without coordinates: record
leosaffin commented 2 weeks ago

This was intentional. The slp_units argument is only a backup the psl array doesn't already have units. preprocess_and_wrap calls metpy.quantify() which converts the input to a pint array with units from the array attributes. I think it's a good thing that it is flagging if the units are not valid rather than overwriting them, but we could flip it around so that it uses slp_units by default.