Skip to content

Commit

Permalink
Merge pull request #74 from DocOtak/standard_name_fix
Browse files Browse the repository at this point in the history
Use CF Standard Names for standard_name attribute
  • Loading branch information
gmaze authored Dec 10, 2020
2 parents 627933e + b786116 commit 464ab42
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
43 changes: 35 additions & 8 deletions argopy/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,33 @@ def interp_std_levels(self, std_lev):
def teos10(self, vlist: list = ['SA', 'CT', 'SIG0', 'N2', 'PV', 'PTEMP'], inplace: bool = True):
""" Add TEOS10 variables to the dataset
By default, add: 'SA', 'CT', 'SIG0', 'N2', 'PV', 'PTEMP'
Rely on the gsw library.
By default, adds: 'SA', 'CT', 'SIG0', 'N2', 'PV', 'PTEMP'
Relies on the gsw library.
If one exists, the correct CF standard name will be added to the attrs.
Parameters
----------
vlist: list(str)
List with the name of variables to add.
Must be a list containing one or more of the following string values:
* `"SA"`
Adds an absolute salinity variable
* `"CT"`
Adds a conservative temperature variable
* `"SIG0"`
Adds a potential density anomaly variable referenced to 0 dbar
* `"N2"`
Adds a buoyancy (Brunt-Vaisala) frequency squared variable.
This variable has been regridded to the original pressure levels in the Dataset using a linear interpolation.
* `"PV"`
Adds a planetary vorticity variable calculated from :math:`\\frac{f N^2}{\\text{gravity}}`.
This is not a TEOS-10 variable from the gsw toolbox, but is provided for convenience.
This variable has been regridded to the original pressure levels in the Dataset using a linear interpolation.
* `"PTEMP"`
Adds a potential temperature variable
inplace: boolean, True by default
If True, return the input :class:`xarray.Dataset` with new TEOS10 variables added as a new :class:`xarray.DataArray`
If False, return a :class:`xarray.Dataset` with new TEOS10 variables
Expand All @@ -709,6 +729,10 @@ def teos10(self, vlist: list = ['SA', 'CT', 'SIG0', 'N2', 'PV', 'PTEMP'], inplac
raise ModuleNotFoundError(
"This functionality requires the gsw library")

allowed = ['SA', 'CT', 'SIG0', 'N2', 'PV', 'PTEMP']
if any(var not in allowed for var in vlist):
raise ValueError(f"vlist must be a subset of {allowed}, instead found {vlist}")

this = self._obj

to_profile = False
Expand Down Expand Up @@ -762,38 +786,41 @@ def mid(x):
that = []
if 'SA' in vlist:
SA = xr.DataArray(sa, coords=this['PSAL'].coords, name='SA')
SA.attrs['standard_name'] = 'Absolute Salinity'
SA.attrs['long_name'] = 'Absolute Salinity'
SA.attrs['standard_name'] = 'sea_water_absolute_salinity'
SA.attrs['unit'] = 'g/kg'
that.append(SA)

if 'CT' in vlist:
CT = xr.DataArray(ct, coords=this['TEMP'].coords, name='CT')
CT.attrs['standard_name'] = 'Conservative Temperature'
CT.attrs['long_name'] = 'Conservative Temperature'
CT.attrs['standard_name'] = 'sea_water_conservative_temperature'
CT.attrs['unit'] = 'degC'
that.append(CT)

if 'SIG0' in vlist:
SIG0 = xr.DataArray(sig0, coords=this['TEMP'].coords, name='SIG0')
SIG0.attrs['long_name'] = 'Potential density anomaly with reference pressure of 0 dbar'
SIG0.attrs['standard_name'] = 'Potential Density'
SIG0.attrs['standard_name'] = 'sea_water_sigma_theta'
SIG0.attrs['unit'] = 'kg/m^3'
that.append(SIG0)

if 'N2' in vlist:
N2 = xr.DataArray(n2, coords=this['TEMP'].coords, name='N2')
N2.attrs['standard_name'] = 'Squared buoyancy frequency'
N2.attrs['long_name'] = 'Squared buoyancy frequency'
N2.attrs['unit'] = '1/s^2'
that.append(N2)

if 'PV' in vlist:
PV = xr.DataArray(pv, coords=this['TEMP'].coords, name='PV')
PV.attrs['standard_name'] = 'Planetary Potential Vorticity'
PV.attrs['long_name'] = 'Planetary Potential Vorticity'
PV.attrs['unit'] = '1/m/s'
that.append(PV)

if 'PTEMP' in vlist:
PTEMP = xr.DataArray(pt, coords=this['TEMP'].coords, name='PTEMP')
PTEMP.attrs['standard_name'] = 'Potential Temperature'
PTEMP.attrs['long_name'] = 'Potential Temperature'
PTEMP.attrs['standard_name'] = 'sea_water_potential_temperature'
PTEMP.attrs['unit'] = 'degC'
that.append(PTEMP)

Expand Down
4 changes: 4 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ v0.1.7 (XX Nov. 2020)
**Breaking changes with previous versions**

- In the teos10 xarray accessor, the ``standard_name`` attribute will now be populated using values from the `CF Standard Name table <https://cfconventions.org/Data/cf-standard-names/76/build/cf-standard-name-table.html>`_ if one exists.
The previous values of ``standard_name`` have been moved to the ``long_name`` attribute.
(:pr:`74`) by `A. Barna <https://github.com/docotak>`_.

- The unique resource identifier property is now named ``uri`` for all data fetchers, it is always a list of strings.

**Internals**
Expand Down

0 comments on commit 464ab42

Please sign in to comment.