You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've run across this discussion and work now and am interested in using it! Is the pint unit stuff sort of separate from the other cf-xarray stuff? I am looking to interpret units in xarray Datasets but with units including psu and for example m s-1.
indeed, the only thing it does for now is provide a special pint.UnitRegistry object so datasets with cfunits style "units" attributes can be used with pint-xarray.
The documentation for this feature of cf-xarray is still missing, though, and we need to get pint's application registry to work reliably but it should be usable right now. In any case I encourage you to try it out and report any issues you run into.
if you're willing to take the time I'd also be interested in feedback on what you found difficult to understand: we didn't check the documentation of pint-xarray in that regard, yet.
I've gotten a little bit into this and get something to work, so that has been exciting. I've immediately hit another unit problem though: salinity units as 1e-3. Is this one that can be added into the registry here?
I don't know a lot about the units used in oceanography (for example, what is the difference between those and the "psu" that are already in the registry?) so I will leave this request to @dcherian or anyone else with more knowledge.
In general, if you can provide a unit name and the dimensionality / scale I think that should be possible.
Here is just a basic example I was able to cobble together from the pint-xarray docs and looking at the commits in the new unit changes here in cf-xarray got me far:
importpint# order apparently mattersfromcf_xarray.unitsimportunitsimportpint_xarrayds=xr.open_dataset([yourdataset])
ds=ds.pint.quantify() # to interpret units# units like m s-1 will be interpretable by pint with the cf_xarray unit registryds['temp'] =ds['temp'].pint.to('degree_Celsius') # convert unitsds=ds.pint.dequantify() # to go back to normal use
and while right now most operations are supported, some of the most important ones (e.g. sel and interp) are not. For that, there's wrapper methods on the .pint accessors. In the future, I hope we can eliminate the dequantify call (altogether or except from immediately before saving / converting to a different library).
If the field of oceanography is to become a recognized science, it must adopt the units that are basic to the fields of chemistry and physics. It also should not adopt new units for variables that are unitless.
Folks. The amazing Phil Elson started https://github.com/pelson/pyudunits2 recently to cover all CF units by implementing udunits2 in pure Python.
I'm not 100% if it is a best alternative to those already using pint. I'd love if you all could comment on https://github.com/pelson/pyudunits2/issues if you believe that pyudunits2 can be a unifying units library for those aiming at support CF units and if you believe that it can replace the many pint-cf-like units we have out there (xclim, metpy, cf-xarray, and my prototype in compliance-checker.)
Activity
kthyng commentedon Jun 2, 2021
I've run across this discussion and work now and am interested in using it! Is the pint unit stuff sort of separate from the other cf-xarray stuff? I am looking to interpret units in xarray Datasets but with units including psu and for example m s-1.
keewis commentedon Jun 2, 2021
indeed, the only thing it does for now is provide a special
pint.UnitRegistryobject so datasets withcfunitsstyle"units"attributes can be used withpint-xarray.The documentation for this feature of
cf-xarrayis still missing, though, and we need to getpint's application registry to work reliably but it should be usable right now. In any case I encourage you to try it out and report any issues you run into.kthyng commentedon Jun 2, 2021
Thanks @keewis! I haven't actually used
pintbefore today so it's slow going, but I'll see what I can figure out.keewis commentedon Jun 2, 2021
if you're willing to take the time I'd also be interested in feedback on what you found difficult to understand: we didn't check the documentation of
pint-xarrayin that regard, yet.kthyng commentedon Jun 3, 2021
I've gotten a little bit into this and get something to work, so that has been exciting. I've immediately hit another unit problem though: salinity units as 1e-3. Is this one that can be added into the registry here?
keewis commentedon Jun 3, 2021
I don't know a lot about the units used in oceanography (for example, what is the difference between those and the
"psu"that are already in the registry?) so I will leave this request to @dcherian or anyone else with more knowledge.In general, if you can provide a unit name and the dimensionality / scale I think that should be possible.
kthyng commentedon Jun 3, 2021
Here is just a basic example I was able to cobble together from the
pint-xarraydocs and looking at the commits in the new unit changes here in cf-xarray got me far:kthyng commentedon Jun 3, 2021
@keewis Unfortunately salinity is represented by a bunch of "essentially" interchangeable units so of course I immediately have one!
@dcherian Can I add a line in the new units file for salinity units of 1e-3?
keewis commentedon Jun 3, 2021
a tip:
can also be written as
and while right now most operations are supported, some of the most important ones (e.g.
selandinterp) are not. For that, there's wrapper methods on the.pintaccessors. In the future, I hope we can eliminate thedequantifycall (altogether or except from immediately before saving / converting to a different library).dcherian commentedon Jun 7, 2021
At first I was skeptical but it looks like this is widespread (Unidata/UDUNITS-2#27).
So i think it's OK to have
1e-3as a synonym forpsu.There are a few other aliases in this comment: PCMDI/pcmdi_metrics#162 (comment)
(
1 psu = 1 PSS-78 = 1 Practical Salinity Scale 78 = 1.e-3)@keewis if you want to waste some time on the history of salinity measurements see http://www.coastalwiki.org/wiki/Salinity
I like the rant by frank Millero which ends with
kthyng commentedon Jun 10, 2021
Ok so should I change the line
https://github.com/xarray-contrib/cf-xarray/blob/main/cf_xarray/units.py#L44 to be the following (not sure if I need the change in capitalization for "E")
kthyng commentedon Jun 16, 2021
If anyone has some time, I would like to move this forward but I am stuck on knowing where to start. I tried my suggestion above and that didn't work.
dcherian commentedon Jun 16, 2021
May be easier if you open a PR and posted the error message. I assume you've looked at https://pint.readthedocs.io/en/stable/defining.html?
kthyng commentedon Jun 16, 2021
kthyng commentedon Jun 16, 2021
keewis commentedon Aug 9, 2021
ocefpaf commentedon Oct 11, 2024
Folks. The amazing Phil Elson started https://github.com/pelson/pyudunits2 recently to cover all CF units by implementing udunits2 in pure Python.
I'm not 100% if it is a best alternative to those already using pint. I'd love if you all could comment on https://github.com/pelson/pyudunits2/issues if you believe that pyudunits2 can be a unifying units library for those aiming at support CF units and if you believe that it can replace the many pint-cf-like units we have out there (xclim, metpy, cf-xarray, and my prototype in compliance-checker.)