Skip to content

updates to unit support #225

Open
Open

Description

@dcherian

Current todo list remaining:

  • add documentation
  • add tests of this registry's quantities being used within xarray, and not just correctly parsing units with the customized features.

Originally posted by @jthielen in #197 (comment)

Activity

  1. kthyng commented on Jun 2, 2021

    @kthyng
    Contributor

    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.

  2. keewis commented on Jun 2, 2021

    @keewis
    Member

    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.

  3. kthyng commented on Jun 2, 2021

    @kthyng
    Contributor

    Thanks @keewis! I haven't actually used pint before today so it's slow going, but I'll see what I can figure out.

  4. keewis commented on Jun 2, 2021

    @keewis
    Member

    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.

  5. kthyng commented on Jun 3, 2021

    @kthyng
    Contributor

    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?

  6. keewis commented on Jun 3, 2021

    @keewis
    Member

    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.

  7. kthyng commented on Jun 3, 2021

    @kthyng
    Contributor

    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:

    import pint
    # order apparently matters
    from cf_xarray.units import units
    import pint_xarray
    
    ds = xr.open_dataset([your dataset])
    ds = ds.pint.quantify()  # to interpret units
    # units like m s-1 will be interpretable by pint with the cf_xarray unit registry
    ds['temp'] = ds['temp'].pint.to('degree_Celsius')  # convert units
    ds = ds.pint.dequantify()  # to go back to normal use
  8. kthyng commented on Jun 3, 2021

    @kthyng
    Contributor

    @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?

  9. keewis commented on Jun 3, 2021

    @keewis
    Member

    a tip:

    ds['temp'] = ds['temp'].pint.to('degree_Celsius')

    can also be written as

    ds = ds.pint.to({"temp": "degree_Celsius"})

    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).

  10. dcherian commented on Jun 7, 2021

    @dcherian
    ContributorAuthor

    Can I add a line in the new units file for salinity units of 1e-3?

    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-3 as a synonym for psu.

    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

    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.

  11. kthyng commented on Jun 10, 2021

    @kthyng
    Contributor

    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")

    units.define("practical_salinity_unit = [] = psu  = 1 PSS-78 = 1 Practical Salinity Scale 78 = 1.e-3 = 1E-3")
    
  12. kthyng commented on Jun 16, 2021

    @kthyng
    Contributor

    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.

  13. dcherian commented on Jun 16, 2021

    @dcherian
    ContributorAuthor

    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?

  14. kthyng commented on Jun 16, 2021

    @kthyng
  15. kthyng commented on Jun 16, 2021

    @kthyng
  16. keewis commented on Aug 9, 2021

    @keewis
  17. ocefpaf commented on Oct 11, 2024

    @ocefpaf
    Contributor

    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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions