gsw-xarray icon indicating copy to clipboard operation
gsw-xarray copied to clipboard

Check function with reference_pressure

Open rcaneill opened this issue 3 years ago • 5 comments

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?

rcaneill avatar Mar 04 '22 08:03 rcaneill

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.

DocOtak avatar Mar 04 '22 09:03 DocOtak

I confirm that if we add the sig1 to a dataset, then all dataarrays have the coordinate refp

rcaneill avatar Mar 06 '22 06:03 rcaneill

Some docs that could be helpfull: cf convention scalar-coordinate-variables

rcaneill avatar Mar 07 '22 09:03 rcaneill

I opened a discussion in the xarray repository https://github.com/pydata/xarray/discussions/6340

rcaneill avatar Mar 07 '22 09:03 rcaneill

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

rcaneill avatar Mar 07 '22 16:03 rcaneill