Skip to content

Commit e6cd40a

Browse files
authored
use cf-xarray's registry in the example (#128)
* use the unit registry provided by cf-xarray instead of modifying the attrs * install cf-xarray into the docs environment * update whats-new.rst * import cf-xarray before pint-xarray
1 parent 5902933 commit e6cd40a

File tree

3 files changed

+5
-23
lines changed

3 files changed

+5
-23
lines changed

docs/examples/plotting.ipynb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"outputs": [],
1717
"source": [
1818
"import xarray as xr\n",
19+
"# to be able to read unit attributes following the CF conventions\n",
20+
"import cf_xarray.units # must be imported before pint_xarray\n",
1921
"import pint_xarray\n",
2022
"from pint_xarray import unit_registry as ureg\n",
2123
"\n",
@@ -42,29 +44,6 @@
4244
"data"
4345
]
4446
},
45-
{
46-
"cell_type": "markdown",
47-
"id": "medium-backup",
48-
"metadata": {},
49-
"source": [
50-
"## convert units into a format understood by pint\n",
51-
"\n",
52-
"<div class=\"alert alert-info\">\n",
53-
"<strong>Note:</strong> this example uses the data provided by the <code>xarray.tutorial</code> functions. As such, the <code>units</code> attributes follow the CF conventions, which <code>pint</code> does not understand by default. To work around that, we are modifying the <code>units</code> attributes here, but in general it is better to use a library that adds support for the units used by the CF conventions to <code>pint</code>.\n",
54-
"</div>"
55-
]
56-
},
57-
{
58-
"cell_type": "code",
59-
"execution_count": null,
60-
"id": "published-powell",
61-
"metadata": {},
62-
"outputs": [],
63-
"source": [
64-
"data.lat.attrs[\"units\"] = \"degree\"\n",
65-
"data.lon.attrs[\"units\"] = \"degree\""
66-
]
67-
},
6847
{
6948
"cell_type": "markdown",
7049
"id": "banned-tolerance",

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pint>=0.14
22
xarray>=0.16.0
33
pooch
44
netCDF4
5+
cf-xarray>=0.6
56
sphinx<4
67
sphinx_rtd_theme
78
ipython

docs/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ What's new
1111
- convert the note about dimension coordinates saving their units in the attributes a
1212
warning (:issue:`124`, :pull:`126`)
1313
By `Justus Magin <https://github.com/keewis>`_.
14+
- use ``cf-xarray``'s unit registry in the plotting example (:issue:`107`, :pull:`128`).
15+
By `Justus Magin <https://github.com/keewis>`_.
1416

1517
0.2 (May 10 2021)
1618
-----------------

0 commit comments

Comments
 (0)