|
| 1 | +.. _weather-climate: |
| 2 | + |
| 3 | +Weather and climate data |
| 4 | +======================== |
| 5 | + |
| 6 | +.. ipython:: python |
| 7 | + :suppress: |
| 8 | +
|
| 9 | + import xarray as xr |
| 10 | +
|
| 11 | +``xarray`` can leverage metadata that follows the `Climate and Forecast (CF) conventions`_ if present. Examples include automatic labelling of plots with descriptive names and units if proper metadata is present (see :ref:`plotting`) and support for non-standard calendars used in climate science through the ``cftime`` module (see :ref:`CFTimeIndex`). There are also a number of geosciences-focused projects that build on xarray (see :ref:`related-projects`). |
| 12 | + |
| 13 | +.. _Climate and Forecast (CF) conventions: http://cfconventions.org |
| 14 | + |
| 15 | +.. _metpy_accessor: |
| 16 | + |
| 17 | +CF-compliant coordinate variables |
| 18 | +--------------------------------- |
| 19 | + |
| 20 | +`MetPy`_ adds a ``metpy`` accessor that allows accessing coordinates with appropriate CF metadata using generic names ``x``, ``y``, ``vertical`` and ``time``. There is also a `cartopy_crs` attribute that provides projection information, parsed from the appropriate CF metadata, as a `Cartopy`_ projection object. See `their documentation`_ for more information. |
| 21 | + |
| 22 | +.. _`MetPy`: https://unidata.github.io/MetPy/dev/index.html |
| 23 | +.. _`their documentation`: https://unidata.github.io/MetPy/dev/tutorials/xarray_tutorial.html#coordinates |
| 24 | +.. _`Cartopy`: https://scitools.org.uk/cartopy/docs/latest/crs/projections.html |
| 25 | + |
| 26 | +.. _CFTimeIndex: |
| 27 | + |
| 28 | +Non-standard calendars and dates outside the Timestamp-valid range |
| 29 | +------------------------------------------------------------------ |
| 30 | + |
| 31 | +Through the standalone ``cftime`` library and a custom subclass of |
| 32 | +:py:class:`pandas.Index`, xarray supports a subset of the indexing |
| 33 | +functionality enabled through the standard :py:class:`pandas.DatetimeIndex` for |
| 34 | +dates from non-standard calendars commonly used in climate science or dates |
| 35 | +using a standard calendar, but outside the `Timestamp-valid range`_ |
| 36 | +(approximately between years 1678 and 2262). |
| 37 | + |
| 38 | +.. note:: |
| 39 | + |
| 40 | + As of xarray version 0.11, by default, :py:class:`cftime.datetime` objects |
| 41 | + will be used to represent times (either in indexes, as a |
| 42 | + :py:class:`~xarray.CFTimeIndex`, or in data arrays with dtype object) if |
| 43 | + any of the following are true: |
| 44 | + |
| 45 | + - The dates are from a non-standard calendar |
| 46 | + - Any dates are outside the Timestamp-valid range. |
| 47 | + |
| 48 | + Otherwise pandas-compatible dates from a standard calendar will be |
| 49 | + represented with the ``np.datetime64[ns]`` data type, enabling the use of a |
| 50 | + :py:class:`pandas.DatetimeIndex` or arrays with dtype ``np.datetime64[ns]`` |
| 51 | + and their full set of associated features. |
| 52 | + |
| 53 | +For example, you can create a DataArray indexed by a time |
| 54 | +coordinate with dates from a no-leap calendar and a |
| 55 | +:py:class:`~xarray.CFTimeIndex` will automatically be used: |
| 56 | + |
| 57 | +.. ipython:: python |
| 58 | +
|
| 59 | + from itertools import product |
| 60 | + from cftime import DatetimeNoLeap |
| 61 | + dates = [DatetimeNoLeap(year, month, 1) for year, month in |
| 62 | + product(range(1, 3), range(1, 13))] |
| 63 | + da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo') |
| 64 | +
|
| 65 | +xarray also includes a :py:func:`~xarray.cftime_range` function, which enables |
| 66 | +creating a :py:class:`~xarray.CFTimeIndex` with regularly-spaced dates. For |
| 67 | +instance, we can create the same dates and DataArray we created above using: |
| 68 | + |
| 69 | +.. ipython:: python |
| 70 | +
|
| 71 | + dates = xr.cftime_range(start='0001', periods=24, freq='MS', calendar='noleap') |
| 72 | + da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo') |
| 73 | +
|
| 74 | +For data indexed by a :py:class:`~xarray.CFTimeIndex` xarray currently supports: |
| 75 | + |
| 76 | +- `Partial datetime string indexing`_ using strictly `ISO 8601-format`_ partial |
| 77 | + datetime strings: |
| 78 | + |
| 79 | +.. ipython:: python |
| 80 | +
|
| 81 | + da.sel(time='0001') |
| 82 | + da.sel(time=slice('0001-05', '0002-02')) |
| 83 | +
|
| 84 | +- Access of basic datetime components via the ``dt`` accessor (in this case |
| 85 | + just "year", "month", "day", "hour", "minute", "second", "microsecond", |
| 86 | + "season", "dayofyear", and "dayofweek"): |
| 87 | + |
| 88 | +.. ipython:: python |
| 89 | +
|
| 90 | + da.time.dt.year |
| 91 | + da.time.dt.month |
| 92 | + da.time.dt.season |
| 93 | + da.time.dt.dayofyear |
| 94 | + da.time.dt.dayofweek |
| 95 | +
|
| 96 | +- Group-by operations based on datetime accessor attributes (e.g. by month of |
| 97 | + the year): |
| 98 | + |
| 99 | +.. ipython:: python |
| 100 | +
|
| 101 | + da.groupby('time.month').sum() |
| 102 | +
|
| 103 | +- Interpolation using :py:class:`cftime.datetime` objects: |
| 104 | + |
| 105 | +.. ipython:: python |
| 106 | +
|
| 107 | + da.interp(time=[DatetimeNoLeap(1, 1, 15), DatetimeNoLeap(1, 2, 15)]) |
| 108 | +
|
| 109 | +- Interpolation using datetime strings: |
| 110 | + |
| 111 | +.. ipython:: python |
| 112 | +
|
| 113 | + da.interp(time=['0001-01-15', '0001-02-15']) |
| 114 | +
|
| 115 | +- Differentiation: |
| 116 | + |
| 117 | +.. ipython:: python |
| 118 | +
|
| 119 | + da.differentiate('time') |
| 120 | +
|
| 121 | +- Serialization: |
| 122 | + |
| 123 | +.. ipython:: python |
| 124 | +
|
| 125 | + da.to_netcdf('example-no-leap.nc') |
| 126 | + xr.open_dataset('example-no-leap.nc') |
| 127 | +
|
| 128 | +- And resampling along the time dimension for data indexed by a :py:class:`~xarray.CFTimeIndex`: |
| 129 | + |
| 130 | +.. ipython:: python |
| 131 | +
|
| 132 | + da.resample(time='81T', closed='right', label='right', base=3).mean() |
| 133 | +
|
| 134 | +.. note:: |
| 135 | + |
| 136 | + |
| 137 | + For some use-cases it may still be useful to convert from |
| 138 | + a :py:class:`~xarray.CFTimeIndex` to a :py:class:`pandas.DatetimeIndex`, |
| 139 | + despite the difference in calendar types. The recommended way of doing this |
| 140 | + is to use the built-in :py:meth:`~xarray.CFTimeIndex.to_datetimeindex` |
| 141 | + method: |
| 142 | + |
| 143 | + .. ipython:: python |
| 144 | + :okwarning: |
| 145 | +
|
| 146 | + modern_times = xr.cftime_range('2000', periods=24, freq='MS', calendar='noleap') |
| 147 | + da = xr.DataArray(range(24), [('time', modern_times)]) |
| 148 | + da |
| 149 | + datetimeindex = da.indexes['time'].to_datetimeindex() |
| 150 | + da['time'] = datetimeindex |
| 151 | +
|
| 152 | + However in this case one should use caution to only perform operations which |
| 153 | + do not depend on differences between dates (e.g. differentiation, |
| 154 | + interpolation, or upsampling with resample), as these could introduce subtle |
| 155 | + and silent errors due to the difference in calendar types between the dates |
| 156 | + encoded in your data and the dates stored in memory. |
| 157 | + |
| 158 | +.. _Timestamp-valid range: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#timestamp-limitations |
| 159 | +.. _ISO 8601-format: https://en.wikipedia.org/wiki/ISO_8601 |
| 160 | +.. _partial datetime string indexing: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#partial-string-indexing |
0 commit comments