gsw-xarray
gsw-xarray copied to clipboard
Check function with reference_pressure
For some functions, we need to add a coordinate with a reference_pressure (e.g. sigma1, sigma2) From cf convention:
Sea water potential density is the density a parcel of sea water would have if moved adiabatically to a reference pressure, by default assumed to be sea level pressure. To specify the reference pressure to which the quantity applies, provide a scalar coordinate variable with standard name reference_pressure. The density of a substance is its mass per unit volume. For sea water potential density, if 1000 kg m-3 is subtracted, the standard name sea_water_sigma_theta should be chosen instead.
This is not very clear for me, should we add a dimension? Or something else?
This is saying that we need to do something like the following if the reference pressure is not 0 to conform to the CF convention (full example with toy/fake inputs):
>>> import xarray as xr
>>> from gsw_xarray import sigma1
>>> refp = xr.DataArray(1000.0, name="refp", attrs={"standard_name":"reference_pressure", "units":"dbar"})
>>> sig1 = sigma1(xr.DataArray([35.0]), 0)
>>> sig1.coords["refp"] = refp
>>> sig1
<xarray.DataArray (dim_0: 1)>
array([32.68557334])
Coordinates:
refp float64 1e+03
Dimensions without coordinates: dim_0
>>> sig1.coords["refp"]
<xarray.DataArray 'refp' ()>
array(1000.)
Coordinates:
refp float64 1e+03
Attributes:
standard_name: reference_pressure
units: dbar
The key thing to note is that the refp coordinate does not have a dimension. We would need to investigate how well doing this integrates with the xarray data model which forces variables (xr.DataArray objects) in an xr.Dataset which share the same dimensions to also share coordinates. i.e. we risk forcing all the variables in a dataset to have the same reference pressure. I suspect we may not be able to add the standard name at all due to this.
I confirm that if we add the sig1 to a dataset, then all dataarrays have the coordinate refp
Some docs that could be helpfull: cf convention scalar-coordinate-variables
I opened a discussion in the xarray repository https://github.com/pydata/xarray/discussions/6340
So the answer is that it is not possible with xarray, but possible with cf-xarray: https://github.com/pydata/xarray/discussions/6340#discussioncomment-2309193