Skip to content

Commit d31bba8

Browse files
authored
Better docs (#265)
1 parent def3afa commit d31bba8

17 files changed

+468
-132
lines changed

cf_xarray/scripts/make_doc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ def make_criteria_csv():
3333
df = df.sort_index(axis=0).sort_index(axis=1)
3434

3535
# All criteria
36-
df.to_csv(os.path.join(csv_dir, "all_criteria.csv"))
36+
df.transpose().to_csv(os.path.join(csv_dir, "all_criteria.csv"))
3737

3838
# Axes and coordinates
3939
for keys, name in zip([_AXIS_NAMES, _COORD_NAMES], ["axes", "coords"]):
4040
subdf = df[sorted(keys)].dropna(axis=1, how="all")
4141
subdf = subdf.dropna(axis=1, how="all").transpose()
42-
subdf.to_csv(os.path.join(csv_dir, f"{name}_criteria.csv"))
42+
subdf.transpose().to_csv(os.path.join(csv_dir, f"{name}_criteria.csv"))
4343

4444

4545
def make_regex_csv():
@@ -51,7 +51,7 @@ def make_regex_csv():
5151
csv_dir = "_build/csv"
5252
os.makedirs(csv_dir, exist_ok=True)
5353
df = DataFrame(regex, index=[0])
54-
df = df.applymap(lambda x: f"``{x}``")
54+
df = df.applymap(lambda x: f"``{str(x)[11:-1]}``")
5555
df = df.sort_index(axis=1).transpose()
5656
df.to_csv(os.path.join(csv_dir, "all_regex.csv"), header=False)
5757

ci/doc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies:
1717
- ipywidgets
1818
- pandas
1919
- pooch
20-
- pydata-sphinx-theme
2120
- pip:
2221
- git+https://github.com/xarray-contrib/cf-xarray
22+
- myst_nb
23+
- sphinx-book-theme

doc/bounds.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Bounds Variables
2+

doc/conf.py

Lines changed: 20 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@
4747
"sphinx.ext.intersphinx",
4848
"sphinx.ext.extlinks",
4949
"numpydoc",
50-
"IPython.sphinxext.ipython_console_highlighting",
51-
"IPython.sphinxext.ipython_directive",
52-
"nbsphinx",
5350
"sphinx_autosummary_accessors",
51+
"IPython.sphinxext.ipython_directive",
52+
"myst_nb",
5453
]
5554

5655
extlinks = {
@@ -145,22 +144,32 @@
145144

146145
# The theme to use for HTML and HTML Help pages. See the documentation for
147146
# a list of builtin themes.
148-
# html_theme = "sphinx_book_theme"
149-
html_theme = "pydata_sphinx_theme"
147+
html_theme = "sphinx_book_theme"
148+
# html_theme = "pydata_sphinx_theme"
150149

151150
# Theme options are theme-specific and customize the look and feel of a theme
152151
# further. For a list of options available for each theme, see the
153152
# documentation.
154-
html_theme_options = {
155-
"github_url": "https://github.com/xarray-contrib/cf-xarray",
156-
"use_edit_page_button": True,
157-
"navbar_end": "search-field.html",
158-
}
153+
html_theme_options = dict(
154+
# analytics_id='' this is configured in rtfd.io
155+
# canonical_url="",
156+
repository_url="https://github.com/xarray-contrib/cf-xarray",
157+
repository_branch="main",
158+
path_to_docs="doc",
159+
use_edit_page_button=True,
160+
use_repository_button=True,
161+
use_issues_button=True,
162+
home_page_in_toc=False,
163+
extra_navbar="",
164+
navbar_footer_text="",
165+
navbar_end="search-field.html",
166+
)
167+
159168

160169
html_context = {
161170
"github_user": "xarray-contrib",
162171
"github_repo": "cf-xarray",
163-
"github_version": "master",
172+
"github_version": "main",
164173
"doc_path": "doc",
165174
}
166175

@@ -232,48 +241,6 @@
232241
# Output file base name for HTML help builder.
233242
htmlhelp_basename = "cf_xarraydoc"
234243

235-
236-
# -- Options for LaTeX output --------------------------------------------------
237-
238-
# latex_elements = {
239-
# The paper size ('letterpaper' or 'a4paper').
240-
# 'papersize': 'letterpaper',
241-
242-
# The font size ('10pt', '11pt' or '12pt').
243-
# 'pointsize': '10pt',
244-
245-
# Additional stuff for the LaTeX preamble.
246-
# 'preamble': '',
247-
# }
248-
249-
# Grouping the document tree into LaTeX files. List of tuples
250-
# (source start file, target name, title, author, documentclass [howto/manual]).
251-
# latex_documents = [
252-
# ('index', 'cf_xarray.tex', u'cf_xarray Documentation',
253-
# u'Deepak Cherian', 'manual'),
254-
# ]
255-
256-
# The name of an image file (relative to this directory) to place at the top of
257-
# the title page.
258-
# latex_logo = None
259-
260-
# For "manual" documents, if this is true, then toplevel headings are parts,
261-
# not chapters.
262-
# latex_use_parts = False
263-
264-
# If true, show page references after internal links.
265-
# latex_show_pagerefs = False
266-
267-
# If true, show URL addresses after external links.
268-
# latex_show_urls = False
269-
270-
# Documents to append as an appendix to all manuals.
271-
# latex_appendices = []
272-
273-
# If false, no module index is generated.
274-
# latex_domain_indices = True
275-
276-
277244
# -- Options for manual page output --------------------------------------------
278245

279246
# One entry per manual page. List of tuples
@@ -283,30 +250,6 @@
283250
# If true, show URL addresses after external links.
284251
# man_show_urls = False
285252

286-
287-
# -- Options for Texinfo output ------------------------------------------------
288-
289-
# Grouping the document tree into Texinfo files. List of tuples
290-
# (source start file, target name, title, author,
291-
# dir menu entry, description, category)
292-
# texinfo_documents = [
293-
# ('index', 'cf_xarray', u'cf_xarray Documentation',
294-
# author, 'cf_xarray', 'One line description of project.',
295-
# 'Miscellaneous'),
296-
# ]
297-
298-
# Documents to append as an appendix to all manuals.
299-
# texinfo_appendices = []
300-
301-
# If false, no module index is generated.
302-
# texinfo_domain_indices = True
303-
304-
# How to display URL addresses: 'footnote', 'no', or 'inline'.
305-
# texinfo_show_urls = 'footnote'
306-
307-
# If true, do not generate a @detailmenu in the "Top" node's menu.
308-
# texinfo_no_detailmenu = False
309-
310253
intersphinx_mapping = {
311254
"python": ("https://docs.python.org/3/", None),
312255
"xarray": ("http://xarray.pydata.org/en/stable/", None),

doc/coord_axes.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
format_name: myst
5+
kernelspec:
6+
display_name: Python 3
7+
name: python3
8+
---
9+
```{eval-rst}
10+
.. currentmodule:: xarray
11+
```
12+
13+
# Axes and Coordinates
14+
15+
One powerful feature of ``cf_xarray`` is the ability to refer to named dimensions by standard `axis` or `coordinate` names in Dataset or DataArray methods.
16+
17+
For example, one can call `ds.cf.mean("latitude")` instead of `ds.mean("lat")`
18+
```{code-cell}
19+
from cf_xarray.datasets import airds
20+
21+
# identical to airds.mean("lat")
22+
airds.cf.mean("latitude")
23+
```
24+
25+
```{tip}
26+
Most xarray methods are wrapped by cf-xarray. Simply access them as `DataArray.cf.method(dim="latitude")` for example and try it! If something does not work, please raise an issue.
27+
```
28+
29+
## Coordinate Names
30+
How does this work? `cf_xarray` has an internal table of criteria (mostly copied from MetPy) that lets it identify specific coordinate names `"latitude", "longitude", "vertical", "time"`.
31+
32+
33+
```{tip}
34+
See {ref}`custom_criteria` to find out how to define your own custom criteria.
35+
```
36+
37+
This table lists these internal criteria
38+
```{eval-rst}
39+
.. csv-table::
40+
:file: _build/csv/coords_criteria.csv
41+
:header-rows: 1
42+
:stub-columns: 1
43+
```
44+
Any DataArray that has `standard_name: "latitude"` or `_CoordinateAxisType: "Lat"` or `"units": "degrees_north"` in its `attrs` will be identified as the `"latitude"` variable by cf-xarray. Similarly for other coordinate names.
45+
46+
47+
## Axis Names
48+
Similar criteria exist for the concept of "axes".
49+
50+
```{eval-rst}
51+
.. csv-table::
52+
:file: _build/csv/axes_criteria.csv
53+
:header-rows: 1
54+
:stub-columns: 1
55+
```
56+
57+
## `.axes` and `.coordinates` properties
58+
Alternatively use the special properties {py:attr}`DataArray.cf.axes` or {py:attr}`DataArray.cf.coordinates` to access the variable names. These properties return dictionaries that map "CF names" to a list of variable names. Note that a list is always returned even if only one variable name matches the name `"latitude"` (for example).
59+
```{code-cell}
60+
airds.cf.axes
61+
```
62+
63+
```{code-cell}
64+
airds.cf.coordinates
65+
```
66+
67+
## Axes or Coordinate?
68+
69+
TODO describe latitude vs Y; longitude vs X; vertical vs Z
70+
71+
## Checking presence of axis or coordinate
72+
73+
Note that a given "CF name" is only present if there is at least one variable that can be identified with that name. The `airds` dataset has no `"vertical"` coordinate or `"Z"` axis, so those keys are not present. So to check whether a `"vertical"` coordinate or `"Z"` axis is present, one can
74+
```{code-cell}
75+
"Z" in airds.cf.axes
76+
```
77+
```{code-cell}
78+
"vertical" in airds.cf.coordinates
79+
```
80+
81+
Or one can check the dataset as a whole:
82+
```{code-cell}
83+
"Z" in airds.cf
84+
```
85+
86+
87+
88+
## Using the repr
89+
It is always useful to check the variables identified by cf-xarray using the `repr`
90+
```{code-cell}
91+
airds.cf
92+
```

doc/criteria.rst

Lines changed: 0 additions & 36 deletions
This file was deleted.

doc/custom-criteria.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
format_name: myst
5+
kernelspec:
6+
display_name: Python 3
7+
name: python3
8+
---
9+
```{eval-rst}
10+
.. currentmodule:: xarray
11+
```
12+
13+
(custom_criteria)=
14+
# Custom Criteria

doc/faq.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Frequently Asked Questions
2+
--------------------------

doc/flags.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
format_name: myst
5+
kernelspec:
6+
display_name: Python 3
7+
name: python3
8+
---
9+
```{eval-rst}
10+
.. currentmodule:: xarray
11+
```
12+
13+
(flags)=
14+
# Flag Variables
15+
16+
`cf_xarray` has some support for [flag variables](http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#flags).
17+
18+
```{code-cell}
19+
import cf_xarray
20+
import xarray as xr
21+
22+
da = xr.DataArray(
23+
[1, 2, 1, 1, 2, 2, 3, 3, 3, 3],
24+
dims=("time",),
25+
attrs={
26+
"flag_values": [1, 2, 3],
27+
"flag_meanings": "atlantic_ocean pacific_ocean indian_ocean",
28+
"standard_name": "region",
29+
},
30+
name="region",
31+
)
32+
da.cf
33+
```
34+
35+
Now you can perform meaningful boolean comparisons that take advantage of the `flag_meanings` attribute:
36+
```{code-cell}
37+
# compare to da == 1
38+
da.cf == "atlantic_ocean"
39+
```
40+
41+
Similarly with membership tests using {py:meth}`DataArray.cf.isin`
42+
```{code-cell}
43+
# compare to da.isin([2, 3])
44+
da.cf.isin(["indian_ocean", "pacific_ocean"])
45+
```
46+
47+
You can also check whether a DataArray has the appropriate attributes to be recognized as a flag variable.
48+
```{code-cell}
49+
da.cf.is_flag_variable
50+
```
51+
52+
```{tip}
53+
`cf_xarray` does not support [flag masks](http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#flags) yet but a Pull Request to add this functionality is very welcome!
54+
```

doc/howtouse.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
```{eval-rst}
2+
.. currentmodule:: xarray
3+
```
4+
5+
# How to use cf_xarray
6+
7+
There are four ways one can use cf_xarray.
8+
9+
## Use CF standard names
10+
11+
Use "CF names" like `standard_name`, coordinates like `"latitude"`, axes like `"X"` instead of actual variable names. For e.g.
12+
13+
## Extract actual variable names
14+
15+
Use `cf_xarray` to extract the appropriate variable name through the properties:
16+
17+
18+
## Rename to a custom vocabulary
19+
20+
Use {py:meth}`Dataset.rename`, or {py:meth}`Dataset.cf.rename_like` to rename variables to your preferences
21+
22+
## Define custom criteria for a custom vocabulary
23+
24+
Define custom criteria to avoid explicity renaming but still work with your datasets using a custom vocabulary.

0 commit comments

Comments
 (0)