Skip to content

Commit 5b4acda

Browse files
authored
Merge pull request #35 from DocOtak/docs
Add attr table to docs
2 parents 934701d + 3c0337d commit 5b4acda

File tree

7 files changed

+102
-18
lines changed

7 files changed

+102
-18
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# These files are generated on doc build and shoudl not be kept in VC
2+
docs/_attr_table.rst
3+
14
# macOS noise
25
.DS_Store
36

README.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ gsw-xarray: Wrapper for gsw that adds CF attributes
1111

1212
gsw-xarray is a wrapper for `gsw python <https://github.com/TEOS-10/GSW-python>`_
1313
that will add CF attributes to xarray.DataArray outputs.
14+
It is meant to be a drop in wrapper for the upstream GSW-Python library and will only add these attributes if one argument to a function is an xarray.DataArray.
1415

1516
Usage
1617
-----
@@ -40,6 +41,19 @@ Outputs
4041

4142
{'standard_name': 'sea_water_sigma_t', 'units': 'kg/m^3'}
4243

44+
Don't worry about usage with non xarray array objects, just use in all places you would the upstream library:
45+
46+
.. code:: python
47+
48+
sigma0 = gsw.sigma0(id * 10, id * 0.1 + 34)
49+
print(type(sigma0), sigma0)
50+
51+
Outputs
52+
53+
::
54+
55+
<class 'numpy.ndarray'> [-5.08964499 2.1101098 9.28348219]
56+
4357
Installation
4458
------------
4559
Pip
@@ -54,7 +68,7 @@ Conda
5468
.....
5569

5670
For the moment gsw-xarray is not released in conda-forge, so you'll
57-
need to instal via pip: activate your conda environment, and then use ``pip install gsw_xarray``.
71+
need to install via pip: activate your conda environment, and then use ``pip install gsw_xarray``.
5872

5973
Pipenv
6074
......
@@ -68,7 +82,7 @@ Contributor guide
6882
All contributions, bug reports, bug fixes, documentation improvements,
6983
enhancements, and ideas are welcome.
7084
If you notice a bug or are missing a feature, fell free
71-
to open an issue in the `github issues page <https://github.com/DocOtak/gsw-xarray/issues>`_.
85+
to open an issue in the `GitHub issues page <https://github.com/DocOtak/gsw-xarray/issues>`_.
7286

7387
In order to contribute to gsw-xarray, please fork the repository and
7488
submit a pull request. A good step by step tutorial for starting with git can be found in the

docs/attrs.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Added Attributes
2+
================
3+
This rather long list shows all the attributes that will be set on the returned DataArray objects and their exact value.
4+
5+
There are some things to be aware of:
6+
7+
* Functions which return unitless values will have the ``units`` attribute set to ``"1"`` following the CF recommended practice (based on SI).
8+
Rather than omit the ``units`` attribute entirely, we want to explicitly state that something is unitless.
9+
10+
* The unit strings were selected to be compatible with both UDUNITS2 (the requirement in CF), and the python pint library.
11+
12+
* Not every returned value has a CF standard name, in which case we must not include the standard name attribute.
13+
As names are added to the CF table, we will attempt to update this list.
14+
The current standard names were taken from v78 last published on 2021-09-21.
15+
16+
Additionally, some standard names are only valid for specific input values e.g. z_from_p has geopotential parameters and the normal standard name does not apply if these are not 0.
17+
18+
* We include the OceanSITES ``reference_scale`` attribute for functions that return PSS-78 or ITS-90.
19+
20+
* Finally, we did our best to include the correct units and standard name based on the published TEOS-10 documentation.
21+
If errors are noted, please open an issue on GitHub so we can correct these errors.
22+
23+
.. include:: _attr_table.rst

docs/conf.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
# Add any Sphinx extension module names here, as strings. They can be
2828
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
2929
# ones.
30-
extensions = []
30+
extensions = [
31+
"sphinx.ext.autosectionlabel",
32+
]
33+
34+
autosectionlabel_prefix_document = True
3135

3236
# Add any paths that contain templates here, relative to this directory.
3337
templates_path = ["_templates"]
@@ -49,3 +53,11 @@
4953
# relative to this directory. They are copied after the builtin static files,
5054
# so a file named "default.css" will overwrite the builtin "default.css".
5155
html_static_path = ["_static"]
56+
57+
58+
try:
59+
exec(open("gen_attr_table.py").read())
60+
except NameError:
61+
from runpy import run_path
62+
63+
run_path("gen_attr_table.py")

docs/gen_attr_table.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from sphinx.util import progress_message
2+
3+
from gsw_xarray._names import _names
4+
from gsw_xarray._attributes import _func_attrs
5+
6+
list_table = ""
7+
8+
9+
def _add_attrs(list_table, attrs, label):
10+
if (label_value := attrs.get(label)) is not None:
11+
list_table += f" * {label}: ``{label_value}``\n"
12+
13+
return list_table
14+
15+
16+
with progress_message("Generating gsw attribute table"):
17+
for name, result_name in _names.items():
18+
list_table += f"{name}\n{'-' * len(name)}\n"
19+
if isinstance(result_name, tuple):
20+
list_table += f"Has {len(result_name)} outputs\n\n"
21+
for i, result in enumerate(result_name):
22+
list_table += f"#. **{result}**\n\n"
23+
attrs = _func_attrs[name][i]
24+
list_table = _add_attrs(list_table, attrs, "standard_name")
25+
list_table = _add_attrs(list_table, attrs, "units")
26+
list_table = _add_attrs(list_table, attrs, "reference_scale")
27+
list_table += "\n"
28+
29+
else:
30+
attrs = _func_attrs[name]
31+
list_table += "Has 1 output\n\n"
32+
list_table += f"#. **{result_name}**\n\n"
33+
list_table = _add_attrs(list_table, attrs, "standard_name")
34+
list_table = _add_attrs(list_table, attrs, "units")
35+
list_table = _add_attrs(list_table, attrs, "reference_scale")
36+
list_table += "\n"
37+
38+
with open("_attr_table.rst", "w", encoding="utf8") as f:
39+
f.write(list_table)

docs/index.rst

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
Welcome to gsw-xarray's documentation!
7-
======================================
6+
.. include:: ../README.rst
87

9-
``gsw-xarray`` is an xarray aware wrapper of the functions contained in ``GSW-Python``.
10-
It will add CF the appropriate CF Standard Name (when one exists) and unit attributes to the resulting Dataarray.
11-
12-
Installation
13-
------------
14-
``gsw-xarray`` can be installed with pip
15-
16-
.. code:: shell
17-
18-
pip install gsw-xarray
8+
For the specific list of what attributes are added for each function, see the :ref:`attrs:Added Attributes` section.
199

10+
Contents
11+
--------
2012

2113
.. toctree::
2214
:maxdepth: 2
23-
:caption: Contents:
15+
16+
attrs
2417

2518

2619

gsw_xarray/_names.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"CT_first_derivatives": ("CT_SA", "CT_pt"),
33
"CT_first_derivatives_wrt_t_exact": ("CT_SA_wrt_t", "CT_T_wrt_t", "CT_P_wrt_t"),
44
"CT_freezing": "CT_freezing",
5-
"CT_freezing_first_derivatives": ("CTfreezing_SA", "CTfreezing_P"),
6-
"CT_freezing_first_derivatives_poly": ("CTfreezing_SA", "CTfreezing_P"),
5+
"CT_freezing_first_derivatives": ("CT_freezing_SA", "CT_freezing_P"),
6+
"CT_freezing_first_derivatives_poly": ("CT_freezing_SA", "CT_freezing_P"),
77
"CT_freezing_poly": "CT_freezing",
88
"CT_from_enthalpy": "CT",
99
"CT_from_enthalpy_exact": "CT",

0 commit comments

Comments
 (0)