Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs update #20

Merged
merged 29 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8ae029a
extend the docs a bit
keewis Aug 5, 2020
6b3f5e4
include a terminology page
keewis Aug 5, 2020
5823741
add xarray and pint to intersphinx
keewis Aug 5, 2020
0b21b65
use the new term to make the docstrings less ambiguous
keewis Aug 5, 2020
f2443e9
Merge branch 'master' into extend-docs
keewis Aug 5, 2020
8ecacfc
Merge branch 'master' into extend-docs
keewis Aug 9, 2020
619b419
update the docs requirements
keewis Aug 9, 2020
2bba268
set up a mapping of type aliases
keewis Aug 9, 2020
61faa84
avoid the use of :param:
keewis Aug 9, 2020
78c6abb
expand the index page
keewis Aug 9, 2020
133aeb5
fix a sidebar caption
keewis Aug 9, 2020
1f6c2df
don't use a custom link text for pint.Unit
keewis Aug 9, 2020
59674a9
move the type information from the definition to the description
keewis Aug 9, 2020
7cd582c
add ipython to the used extensions
keewis Aug 17, 2020
9583f49
add terminology to the toctree
keewis Aug 17, 2020
d38ed3d
add blackdoc to pre-commit
keewis Aug 17, 2020
1d8d414
add a rough draft of the quantify howto-guide
keewis Aug 17, 2020
79c3b3f
add netcdf4 to the requirements
keewis Aug 18, 2020
2389189
enable the type preprocessor
keewis Aug 18, 2020
ebb1ff7
use ipython prompts in the directives
keewis Aug 18, 2020
9ca8d9f
rephrase the reference to the github repository
keewis Aug 18, 2020
9dcf45d
add a howto-guide for converting units
keewis Aug 18, 2020
eed7d04
clean up the sphinx configuration a bit
keewis Aug 18, 2020
b241b9b
configure extlinks
keewis Aug 18, 2020
5b7a70f
add an item to the changelog
keewis Aug 18, 2020
99777b3
add some text to the empty examples section
keewis Aug 18, 2020
ca6a8e7
update the install instructions
keewis Aug 22, 2020
829d2be
avoid recommending a workflow that would break CF compliance
keewis Aug 24, 2020
78c9e62
rename the changelog to whats-new
keewis Aug 26, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ repos:
rev: stable
hooks:
- id: black
- repo: https://github.com/keewis/blackdoc
rev: stable
hooks:
- id: blackdoc
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.1
hooks:
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Changelog
=========

0.1 (*unreleased*)
------------------
- add documentation (:pull:`20`)
35 changes: 32 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
project = "pint-xarray"
author = f"{project} developers"
copyright = f"{year}, {author}"
github_url = "https://github.com/xarray-contrib/pint-xarray"


# -- General configuration ---------------------------------------------------
Expand All @@ -38,10 +39,13 @@
# ones.
extensions = [
"sphinx.ext.intersphinx",
"sphinx.ext.extlinks",
"sphinx.ext.autosummary",
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx_autosummary_accessors",
"IPython.sphinxext.ipython_directive",
"IPython.sphinxext.ipython_console_highlighting",
]

# Add any paths that contain templates here, relative to this directory.
Expand All @@ -68,13 +72,38 @@

# -- Extension configuration -------------------------------------------------

# extlinks
extlinks = {
"issue": (f"{github_url}/issues/%s", "GH"),
"pull": (f"{github_url}/pull/%s", "PR"),
}

# autosummary
autosummary_generate = True

# autodoc
autodoc_typehints = "none"

napoleon_use_param = True
# napoleon
napoleon_use_param = False
napoleon_use_rtype = True

napoleon_preprocess_types = True
napoleon_type_aliases = {
"dict-like": ":term:`dict-like <mapping>`",
"mapping": ":term:`mapping`",
"hashable": ":term:`hashable`",
# xarray
"Dataset": "~xarray.Dataset",
"DataArray": "~xarray.DataArray",
# pint / pint-xarray
"unit-like": ":term:`unit-like`",
}

# -- Options for intersphinx extension ---------------------------------------

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"https://docs.python.org/3/": None}
intersphinx_mapping = {
"python": ("https://docs.python.org/3/", None),
"xarray": ("https://xarray.pydata.org/en/stable", None),
"pint": ("https://pint.readthedocs.io/en/stable", None),
}
12 changes: 12 additions & 0 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Contributing
============
``pint-xarray`` is developed on `github <https://github.com/xarray-contrib/pint-xarray>`_.

Linters / Autoformatters
------------------------
In order to keep code consistent, we use

- `Black <https://black.readthedocs.io/en/stable/>`_ for standardized code formatting
- `blackdoc <https://blackdoc.readthedocs.io/en/stable/>`_ for standardized code formatting in documentation
- `Flake8 <http://flake8.pycqa.org/en/latest/>`_ for general code quality
- `isort <https://github.com/timothycrosley/isort>`_ for standardized order in imports. See also `flake8-isort <https://github.com/gforcada/flake8-isort>`_.
30 changes: 30 additions & 0 deletions docs/conversion.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.. currentmodule:: xarray

Converting units
================
.. ipython:: python
:suppress:

import xarray as xr

When working with :py:class:`Dataset` or :py:class:`DataArray` objects with
units, we frequently might want to convert the units. Suppose we have:

.. ipython::

In [1]: ds = xr.Dataset(
...: {"a": ("x", [4, 8, 12, 16])}, coords={"u": ("x", [10, 20, 30, 40])}
...: ).pint.quantify({"a": "m", "u": "s"})
...: ds

In [2]: da = ds.a
...: da

To convert the data to different units, we can use the
:py:meth:`Dataset.pint.to` and :py:meth:`DataArray.pint.to` methods:

.. ipython::

In [3]: ds.pint.to(a="feet", u="ks")

In [4]: da.pint.to({da.name: "nautical_mile", "u": "ms"})
93 changes: 93 additions & 0 deletions docs/creation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
.. currentmodule:: xarray

Creating and saving objects with units
======================================

Attaching units
---------------
.. ipython:: python
:suppress:

import xarray as xr

Usually, when loading data from disk we get a :py:class:`Dataset` or
:py:class:`DataArray` with units in attributes:

.. ipython::

In [1]: ds = xr.tutorial.open_dataset("air_temperature")
...: ds

In [2]: da = ds.air
...: da

In order to get :py:class:`pint.Quantity` instances, we can use the
:py:meth:`Dataset.pint.quantify` or :py:meth:`DataArray.pint.quantify` methods:

.. ipython::
:okexcept:

In [3]: ds.pint.quantify()

As we can see, the dataset uses units like ``degree_north`` or ``degree_east``,
which `pint`_ doesn't know about. To fix that, we can override the units of
specific variables:

.. ipython::

In [4]: ds.pint.quantify(lat="degree", lon="degree")

In [5]: da.pint.quantify({"lat": "degree", "lon": "degree"})

Overriding works, even if there is no ``units`` attribute, so we could use this
to attach units to a ordinary :py:class:`Dataset`:

.. ipython::

In [6]: temporary_ds = xr.Dataset({"a": ("x", [0, 5, 10])}, coords={"x": [1, 2, 3]})
...: temporary_ds.pint.quantify({"a": "m"})

Of course, we could use :py:class:`pint.Unit` instances instead of strings to
specify units, too. If we wanted to change the units of the data of a
:py:class:`DataArray`, we could do so using the :py:attr:`DataArray.name`
attribute:

.. ipython::

In [7]: da.pint.quantify({da.name: "J", "lat": "degree", "lon": "degree"})

However, `xarray`_ currently doesn't support `units in indexes`_, so the new units were set
as attributes. To really observe the changes the ``quantify`` methods make, we
have to first swap the dimensions:

.. ipython::

In [8]: ds_with_units = ds.swap_dims({"lon": "x", "lat": "y"}).pint.quantify(
...: {"lat": "degree", "lon": "degree"}
...: )
...: ds_with_units

In [9]: da_with_units = da.swap_dims({"lon": "x", "lat": "y"}).pint.quantify(
...: {"lat": "degree", "lon": "degree"}
...: )
...: da_with_units
keewis marked this conversation as resolved.
Show resolved Hide resolved

Saving with units
-----------------
In order to not lose the units when saving to disk, we first have to call the
:py:meth:`Dataset.pint.dequantify` and :py:meth:`DataArray.pint.dequantify`
methods:

.. ipython::

In [10]: ds_with_units.pint.dequantify()

In [11]: da_with_units.pint.dequantify()

This will get the string representation of a :py:class:`pint.Unit` instance and
attach it as a ``units`` attribute. The data of the variable will now be
whatever `pint`_ wrapped.

.. _pint: https://pint.readthedocs.org/en/stable
.. _xarray: https://xarray.pydata.org/en/stable
.. _units in indexes: https://github.com/pydata/xarray/issues/1603
3 changes: 3 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Examples
========
There are no examples yet.
56 changes: 48 additions & 8 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
.. accessors documentation master file, created by
sphinx-quickstart on Thu Jul 2 01:49:50 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
pint-xarray
===========
A convenience wrapper for using `pint`_ in `xarray`_ objects.

Welcome to accessors's documentation!
=====================================
.. _pint: https://pint.readthedocs.io/en/stable
.. _xarray: https://xarray.pydata.org/en/stable

Documentation
-------------

**Getting Started**:

- :doc:`installation`
- :doc:`examples`

.. toctree::
:maxdepth: 1
:caption: Getting Started
:hidden:

installation
examples

**User Guide**:

- :doc:`terminology`
- :doc:`creation`
- :doc:`conversion`

.. toctree::
:maxdepth: 1
:caption: User Guide
:hidden:

terminology
creation
conversion


**Help & Reference**:

- :doc:`changelog`
- :doc:`api`
- :doc:`contributing`

.. toctree::
:maxdepth: 2
:caption: Contents:
:maxdepth: 1
:caption: Help & Reference
:hidden:

changelog
api
contributing
26 changes: 26 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Installation
------------
Install from ``conda-forge``:

.. code:: sh

conda install -c conda-forge pint-xarray

or from ``PyPI``:

.. code:: sh

python -m pip install pint-xarray

or from source, either directly from github:

.. code:: sh

python -m pip install git+https://github.com/xarray-contrib/pint-xarray

or from a local copy:

.. code:: sh

git clone https://github.com/xarray-contrib/pint-xarray
python -m pip install ./pint-xarray
8 changes: 5 additions & 3 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pint>=0.13
xarray>=0.15.1
sphinx>=3.0
pint>=0.14
xarray>=0.16.0
netCDF4
sphinx>=3.2
sphinx_rtd_theme
ipython
nbsphinx
sphinx-autosummary-accessors
10 changes: 10 additions & 0 deletions docs/terminology.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Terminology
===========

.. glossary::

unit-like
A `pint`_ unit definition, as accepted by :py:class:`pint.Unit`.
May be either a :py:class:`str` or a :py:class:`pint.Unit` instance.

.. _pint: https://pint.readthedocs.io/en/stable
8 changes: 4 additions & 4 deletions pint_xarray/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def quantify(

Parameters
----------
units : pint.Unit or str or mapping of hashable to pint.Unit or str, optional
units : unit-like or mapping of hashable to unit-like, optional
Physical units to use for this DataArray. If a str or
pint.Unit, will be used as the DataArray's units. If a
dict-like, it should map a variable name to the desired
Expand Down Expand Up @@ -275,7 +275,7 @@ def to(self, units=None, **unit_kwargs):

Parameters
----------
units : str or pint.Unit or mapping of hashable to str or pint.Unit, optional
units : unit-like or mapping of hashable to unit-like, optional
The units to convert to. If a unit name or ``pint.Unit``
object, convert the DataArray's data. If a dict-like, it
has to map a variable name to a unit name or ``pint.Unit``
Expand Down Expand Up @@ -440,7 +440,7 @@ def quantify(

Parameters
----------
units : mapping of hashable to pint.Unit or str, optional
units : mapping of hashable to unit-like, optional
Physical units to use for particular DataArrays in this
Dataset. It should map variable names to units (unit names
or ``pint.Unit`` objects). If not provided, will try to
Expand Down Expand Up @@ -532,7 +532,7 @@ def to(self, units=None, **unit_kwargs):

Parameters
----------
units : mapping of hashable to str or pint.Unit, optional
units : mapping of hashable to unit-like, optional
Maps variable names to the unit to convert to.
**unit_kwargs
The kwargs form of ``units``. Can only be used for
Expand Down