Skip to content

[FEATURE]: convenience functions to access variable coords #200

@durack1

Description

@durack1

Is your feature request related to a problem?

This somewhat expands on #199 (which could be closed as a dupe - feel free to do this). Accessing information from loaded variables appears to be a little inconsistent, so for e.g.

>>> from xcdat import dataset
>>> f = "/p/css03/esgf_publish/CMIP6/CMIP/CSIRO/ACCESS-ESM1-5/historical/r1i1p1f1/Amon/ta/gn/v20191115/ta_Amon_ACCESS-ESM1-5_historical_r1i1p1f1_gn_185001-194912.nc"
>>> ta = dataset.open_dataset(f)
>>> ta._coord_names
{'lon', 'time', 'plev', 'lat'}
>>> ta
<xarray.Dataset>
Dimensions:    (time: 1200, bnds: 2, plev: 19, lat: 144, lon: 192)
Coordinates:
  * time       (time) datetime64[ns] 1850-01-16T12:00:00 ... 1949-12-16T12:00:00
  * plev       (plev) float64 1e+05 9.25e+04 8.5e+04 7e+04 ... 1e+03 500.0 100.0
  * lat        (lat) float64 -89.38 -88.12 -86.88 -85.62 ... 86.88 88.12 89.38
  * lon        (lon) float64 0.9375 2.812 4.688 6.562 ... 355.3 357.2 359.1
...

So to get the names of the axes I need to call _coord_names, which returns the axes in an order that doesn't reflect the dimension ordering which is [time, plev, lat, lon] (above) rather than {'lon', 'time', 'plev', 'lat'} (returned by _coord_names). If we had a ,getTime, .getLevel,.getLatitude, and .getLongitude function (or alternatively another function that could return all dimensions, their names and their order).

Such functionality doesn't appear to exist inxarray at least from what I have found, the closest is https://docs.xarray.dev/en/latest/generated/xarray.DataArray.get_axis_num.html

Describe the solution you'd like

Something like:

>>> from xcdat import dataset
>>> f = "/p/css03/esgf_publish/CMIP6/CMIP/CSIRO/ACCESS-ESM1-5/historical/r1i1p1f1/Amon/ta/gn/v20191115/ta_Amon_ACCESS-ESM1-5_historical_r1i1p1f1_gn_185001-194912.nc"
>>> ta = dataset.open_dataset(f)
>>> ta.getDimensions
{'time': 1200, 'plev': 19, 'lat': 144, 'lon': 192}
>>> ta.getLevel  # would return any vertical coordinate, so depth, height, lev, level, plev, ... (we could pull all instances of level coord names from the CMIP6/5/3 archive)
<xarray.DataArray 'plev' (plev: 19)>
array([100000.,  92500.,  85000.,  70000.,  60000.,  50000.,  40000.,  30000.,
        25000.,  20000.,  15000.,  10000.,   7000.,   5000.,   3000.,   2000.,
         1000.,    500.,    100.])
Coordinates:
  * plev     (plev) float64 1e+05 9.25e+04 8.5e+04 7e+04 ... 1e+03 500.0 100.0
Attributes:
    bounds:         plev_bnds
    units:          Pa
    axis:           Z
    positive:       down
    long_name:      pressure
    standard_name:  air_pressure

And we could implement a similar example for ta.getTime, ta.getLatitude, ta.getLongitude

Describe alternatives you've considered

NA

Additional context

NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions