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

Add user documentation #40

Merged
merged 11 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ coverage.xml
.tox

docs/_build
docs/generated

# ide
.idea
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## Unreleased

Added:
- Documentation for the package, including readthedocs integration ([#40](https://github.com/EXCITED-CO2/xarray-regrid/pull/40)).

## v0.2.3 (2024-02-29)

Expand Down
16 changes: 16 additions & 0 deletions docs/.readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2

build:
os: ubuntu-22.04
tools:
python: "3.11"

sphinx:
configuration: docs/conf.py

python:
install:
- method: pip
path: .
extra_requirements:
- docs
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SPHINXPROJ = xarray-regrid
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2 changes: 2 additions & 0 deletions docs/changelog_link.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.. include:: ../CHANGELOG.md
:parser: myst_parser.sphinx_
115 changes: 115 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
#

import datetime
from importlib.metadata import version as get_version

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))

# General information about the project.
project = "xarray-regrid"
current_year = datetime.datetime.now().year
copyright = f"2023-{current_year}, Bart Schilperoort"
author = "Bart Schilperoort"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
# The short X.Y version.
version = get_version("xarray_regrid").split("+")[0]
# The full version, including alpha/beta/rc tags.
release = get_version("xarray_regrid")

# -- General configuration ------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named "sphinx.ext.*") or your custom
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.coverage",
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"sphinx.ext.mathjax",
"sphinx.ext.napoleon",
"sphinx.ext.todo",
"sphinx.ext.viewcode",
"autoapi.extension",
#"myst_parser",
"myst_nb",
]

extlinks = {
"issue": ("https://github.com/excited-CO2/xarray-regrid/issues/%s", "GH#%s"),
"pr": ("https://github.com/excited-CO2/xarray-regrid/pull/%s", "PR#%s"),
}

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "data"]

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False

# Myst_nb options
nb_execution_mode = "off"

# -- Use autoapi.extension to run sphinx-apidoc -------

autoapi_dirs = ["../src"]

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# This is required for the alabaster theme
# refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebars
html_sidebars = {
"**": [
"globaltoc.html",
"relations.html", # needs 'show_related': True theme option to display
"searchbox.html",
]
}

html_logo = "./assets/logo.png"

# -- Options for Intersphinx

intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
"numpy": ("https://numpy.org/doc/stable/", None),
"xarray": ("https://docs.xarray.dev/en/stable/", None),
}
38 changes: 38 additions & 0 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Quickstart
==========

``xarray-regrid`` allows you to regrid xarray Datasets or DataArrays to a new resolution.
You can install xarray-regrid with pip:

.. code:: shell

pip install xarray-regrid


To use the package, import ``xarray_regrid``. This will register the ``.regrid`` 'accessor' so it can be used.
Next load in the data you want to regrid, and the data with a grid you want to regrid to:

.. code:: python

import xarray_regrid
import xarray

ds = xr.open_dataset("input_data.nc")
ds_grid = xr.open_dataset("target_grid.nc")

ds = ds.regrid.linear(ds_grid)

# or, for example:
ds = ds.regrid.conservative(ds_grid, latitude_coord="lat")


Multiple regridding methods are available:

* `linear interpolation <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.linear>`_ (``.regrid.linear``)
* `nearest-neighbor <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.conservative>`_ (``.regrid.nearest``)
* `cubic interpolation <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.cubic>`_ (``.regrid.cubic``)
* `conservative regridding <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.conservative>`_ (``.regrid.conservative``)

Additionally, a zonal statistics `method to compute the most common value <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.most_common>`_
is available (``.regrid.most_common``).
This can be used to upscale very fine categorical data to a more course resolution.
65 changes: 65 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. toctree::
:maxdepth: 3
:hidden:

self


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

Quickstart <getting_started>
Example Notebooks <notebooks/index>


.. toctree::
:maxdepth: 3
:caption: Technical information
:hidden:

Changelog <changelog_link>

xarray-regrid: Regridding utilities for xarray
**********************************************

|PyPI| |DOI|

Overview
========

``xarray-regrid`` extends xarray with regridding methods, making it possibly to easily and effiently regrid between two rectilinear grids.

The following methods are supported:

* `Linear <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.linear>`_
* `Nearest-neighbor <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.nearest>`_
* `Conservative <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.conservative>`_
* `Cubic <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.cubic>`_
* `"Most common value" (zonal statistics) <autoapi/xarray_regrid/regrid/index.html#xarray_regrid.regrid.Regridder.most_common>`_

Note that "Most common value" is designed to regrid categorical data to a coarse resolution. For regridding categorical data to a finer resolution, please use "nearest-neighbor" regridder.

For usage examples, please refer to the `quickstart guide <getting_started>`_ and the `example notebooks <notebooks/index>`_.

Installing
==========

.. code:: shell

pip install xarray-regrid


Acknowledgements
================

This package was developed under Netherlands eScience Center grant `NLESC.OEC.2022.017 <https://research-software-directory.org/projects/excited>`_.

Some methods were inspired by discussions in the `Pangeo <https://pangeo.io>`_ community.

.. |PyPI| image:: https://img.shields.io/pypi/v/xarray-regrid.svg?style=flat
:target: https://pypi.python.org/pypi/xarray-regrid/

.. |DOI| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.10203304.svg
:target: https://doi.org/10.5281/zenodo.10203304
36 changes: 36 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build
set SPHINXPROJ=xarray-regrid

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%

:end
popd
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions docs/notebooks/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

Example Notebooks
~~~~~~~~~~~~~~~~~

To benchmark the library against some more standard tools, we have created some notebooks.

Most notebooks compare the methods implemented in xarray-regrid against more standard tools (CDO, ESMF).

.. toctree::
:maxdepth: 1
:caption: Benchmarks

benchmarks/benchmarking_bilinear.ipynb
benchmarks/benchmarking_conservative.ipynb
benchmarks/benchmarking_nearest.ipynb

.. toctree::
:maxdepth: 1
:caption: Demos

demos/demo_most_common.ipynb
21 changes: 20 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,20 @@ dev = [
"mypy",
"pytest",
"pytest-cov",
"cftime", # required for decode time of test netCDF files
"pandas-stubs", # Adds typing for pandas.
]
docs = [ # Required for ReadTheDocs
"myst_parser",
"sphinx",
"myst-nb",
"sphinx_rtd_theme",
"sphinx-autoapi",
"coverage[toml]",
]

[tool.hatch.version]
path = "src/xarray_regrid/__about__.py"
path = "src/xarray_regrid/__init__.py"

[tool.hatch.envs.default]
features = ["dev", "benchmarking"]
Expand All @@ -70,9 +79,18 @@ coverage = [
"pytest --cov --cov-report term --cov-report xml --junitxml=xunit-result.xml tests/",
]

[tool.hatch.envs.docs]
features = ["docs"]

[tool.hatch.envs.docs.scripts]
build = ["(cd docs && make html)"]

[tool.ruff]
target-version = "py310"
line-length = 88
exclude = [
"docs",
]

[tool.ruff.lint]
select = [
Expand Down Expand Up @@ -158,3 +176,4 @@ check_untyped_defs = true
warn_return_any = true
warn_unused_ignores = true
show_error_codes = true
exclude = ["tests/*", "docs"]
1 change: 0 additions & 1 deletion src/xarray_regrid/__about__.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/xarray_regrid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
"create_regridding_dataset",
"methods",
]

__version__ = "0.2.3"
3 changes: 3 additions & 0 deletions src/xarray_regrid/methods/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Implementation of the regridding methods."""
# Note: this file is only required due to sphinx autoapi
# https://github.com/readthedocs/sphinx-autoapi/issues/298
Loading
Loading