Skip to content

Commit 6cb97f6

Browse files
Example using Coarsen.construct to split map into regions (#7192)
* example using coarsen.construct to split map into regions * whatsnew * moved to reshape page * PR number * fix internal links * Update doc/whats-new.rst Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: Deepak Cherian <[email protected]>
1 parent 8f34b32 commit 6cb97f6

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

doc/user-guide/computation.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,14 @@ and ``mean``, ``std`` and ``var`` return ``NaN``:
360360
``weights`` must be a :py:class:`DataArray` and cannot contain missing values.
361361
Missing values can be replaced manually by ``weights.fillna(0)``.
362362

363-
.. _comput.coarsen:
363+
.. _compute.coarsen:
364364

365365
Coarsen large arrays
366366
====================
367367

368368
:py:class:`DataArray` and :py:class:`Dataset` objects include a
369369
:py:meth:`~xarray.DataArray.coarsen` and :py:meth:`~xarray.Dataset.coarsen`
370-
methods. This supports the block aggregation along multiple dimensions,
370+
methods. This supports block aggregation along multiple dimensions,
371371

372372
.. ipython:: python
373373
@@ -403,6 +403,7 @@ function or method name to ``coord_func`` option,
403403
404404
da.coarsen(time=7, x=2, coord_func={"time": "min"}).mean()
405405
406+
You can also :ref:`use coarsen to reshape<reshape.coarsen>` without applying a computation.
406407

407408
.. _compute.using_coordinates:
408409

doc/user-guide/reshaping.rst

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Reshaping and reorganizing data
55
###############################
66

7-
These methods allow you to reorganize
7+
These methods allow you to reorganize your data by changing dimensions, array shape, order of values, or indexes.
88

99
.. ipython:: python
1010
:suppress:
@@ -292,3 +292,54 @@ As a shortcut, you can refer to existing coordinates by name:
292292
ds.sortby("x")
293293
ds.sortby(["y", "x"])
294294
ds.sortby(["y", "x"], ascending=False)
295+
296+
.. _reshape.coarsen:
297+
298+
Reshaping via coarsen
299+
---------------------
300+
301+
Whilst :py:class:`~xarray.DataArray.coarsen` is normally used for reducing your data's resolution by applying a reduction function
302+
(see the :ref:`page on computation<compute.coarsen>`),
303+
it can also be used to reorganise your data without applying a computation via :py:meth:`~xarray.core.rolling.DataArrayCoarsen.construct`.
304+
305+
Taking our example tutorial air temperature dataset over the Northern US
306+
307+
.. ipython:: python
308+
:suppress:
309+
310+
# Use defaults so we don't get gridlines in generated docs
311+
import matplotlib as mpl
312+
313+
mpl.rcdefaults()
314+
315+
.. ipython:: python
316+
317+
air = xr.tutorial.open_dataset("air_temperature")["air"]
318+
319+
@savefig pre_coarsening.png
320+
air.isel(time=0).plot(x="lon", y="lat")
321+
322+
we can split this up into sub-regions of size ``(9, 18)`` points using :py:meth:`~xarray.core.rolling.DataArrayCoarsen.construct`:
323+
324+
.. ipython:: python
325+
326+
regions = air.coarsen(lat=9, lon=18, boundary="pad").construct(
327+
lon=("x_coarse", "x_fine"), lat=("y_coarse", "y_fine")
328+
)
329+
regions
330+
331+
9 new regions have been created, each of size 9 by 18 points.
332+
The ``boundary="pad"`` kwarg ensured that all regions are the same size even though the data does not evenly divide into these sizes.
333+
334+
By plotting these 9 regions together via :ref:`faceting<plotting.faceting>` we can see how they relate to the original data.
335+
336+
.. ipython:: python
337+
338+
@savefig post_coarsening.png
339+
regions.isel(time=0).plot(
340+
x="x_fine", y="y_fine", col="x_coarse", row="y_coarse", yincrease=False
341+
)
342+
343+
We are now free to easily apply any custom computation to each coarsened region of our new dataarray.
344+
This would involve specifying that applied functions should act over the ``"x_fine"`` and ``"y_fine"`` dimensions,
345+
but broadcast over the ``"x_coarse"`` and ``"y_coarse"`` dimensions.

doc/whats-new.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Documentation
5858
Add :py:meth:`__str__` to surface the new :py:class:`BackendEntrypoint` ``description``
5959
and ``url`` attributes. (:issue:`6577`, :pull:`7000`)
6060
By `Jessica Scheick <https://github.com/jessicas11>`_.
61+
- Add example of using :py:meth:`DataArray.coarsen.construct` to User Guide. (:pull:`7192`)
62+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
6163

6264

6365
Internal Changes
@@ -2995,7 +2997,7 @@ Highlights include:
29952997
- Removed support for Python 2. This is the first version of xarray that is
29962998
Python 3 only!
29972999
- New :py:meth:`~xarray.DataArray.coarsen` and
2998-
:py:meth:`~xarray.DataArray.integrate` methods. See :ref:`comput.coarsen`
3000+
:py:meth:`~xarray.DataArray.integrate` methods. See :ref:`compute.coarsen`
29993001
and :ref:`compute.using_coordinates` for details.
30003002
- Many improvements to cftime support. See below for details.
30013003

@@ -3051,7 +3053,7 @@ Other enhancements
30513053
By `Ryan Abernathey <https://github.com/rabernat>`_
30523054
- :py:meth:`DataArray.coarsen` and
30533055
:py:meth:`Dataset.coarsen` are newly added.
3054-
See :ref:`comput.coarsen` for details.
3056+
See :ref:`compute.coarsen` for details.
30553057
(:issue:`2525`)
30563058
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
30573059
- Upsampling an array via interpolation with resample is now dask-compatible,

0 commit comments

Comments
 (0)