Skip to content

Commit 0e8db86

Browse files
author
Andrew Barna
committed
First attempt at some readme docs for pint support
1 parent 4cf63c9 commit 0e8db86

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

README.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,65 @@ Outputs
5656

5757
<class 'numpy.ndarray'> [-5.08964499 2.1101098 9.28348219]
5858

59+
60+
We support (but don't yet validate) the usage of pint.Quantities and the usage of xarray wrapped Quantities.
61+
Support for pint requires the installation of two optional dependencies: `pint` and `pint-xarray`.
62+
If any of the inputs to a gsw function are Quantities, the returned object will also be a Quantity belonging to the same UnitRegistry.
63+
64+
.. warning::
65+
Quantities must all belong to the same pint.UnitRegistry, a ValueError will be thrown if there are mixed registries.
66+
67+
.. code:: python
68+
import pint_xarray
69+
import gsw_xarray as gsw
70+
71+
# Create a xarray.Dataset
72+
import numpy as np
73+
import xarray as xr
74+
ds = xr.Dataset()
75+
id = np.arange(3)
76+
ds['id'] = xr.DataArray(id, coords={'id':id})
77+
ds['CT'] = ds['id'] * 10
78+
# make sure there are unit attrs this time
79+
ds['CT'].attrs = {'standard_name':'sea_water_conservative_temperature', 'units': 'degC'}
80+
ds['SA'] = ds['id'] * 0.1 + 34
81+
ds['SA'].attrs = {'standard_name':'sea_water_absolute_salinity', 'units': 'g/kg'}
82+
83+
# use the pint accessor to quantify things
84+
ds = ds.pint.quantify()
85+
86+
# Apply gsw functions
87+
sigma0 = gsw.sigma0(SA=ds['SA'], CT=ds['CT'])
88+
# outputs are now quantities!
89+
print(sigma0)
90+
91+
Outputs
92+
93+
::
94+
<xarray.DataArray 'sigma0' (id: 3)>
95+
<Quantity([27.17191038 26.12820162 24.03930887], 'kilogram / meter ** 3')>
96+
Coordinates:
97+
* id (id) int64 0 1 2
98+
Attributes:
99+
standard_name: sea_water_sigma_t
100+
101+
The usage of xarray wrapped Quantities is not required, you can use pint directly (though the `pint-xarray` dep still needs to be installed).
102+
103+
.. code:: python
104+
105+
import gsw_xarray as gsw
106+
import pint
107+
ureg = pint.UnitRegistry()
108+
SA = ureg.Quantity(35, ureg("g/kg"))
109+
CT = ureg.Quantity(10, ureg.degC)
110+
sigma0 = gsw.sigma0(SA=SA, CT=CT)
111+
print(sigma0)
112+
113+
Outputs
114+
115+
::
116+
26.824644457868317 kilogram / meter ** 3
117+
59118
Installation
60119
------------
61120
Pip

0 commit comments

Comments
 (0)