|
| 1 | +--- |
| 2 | +file_format: mystnb |
| 3 | +kernelspec: |
| 4 | + name: python3 |
| 5 | +--- |
| 6 | +# Xarray |
| 7 | + |
| 8 | +Cubed can work with Xarray datasets via the [`cubed-xarray`](https://github.com/cubed-dev/cubed-xarray) package. |
| 9 | + |
| 10 | +Install by running the following: |
| 11 | + |
| 12 | +```shell |
| 13 | +pip install cubed cubed-xarray xarray pooch netCDF4 |
| 14 | +``` |
| 15 | + |
| 16 | +Note that `pooch` and `netCDF4` are needed to access the Xarray tutorial datasets that we use in the example below. |
| 17 | + |
| 18 | +## Open dataset |
| 19 | + |
| 20 | +Start by importing Xarray - note that we don't need to import Cubed or `cubed-xarray`, since they will be picked up automatically. |
| 21 | + |
| 22 | +```{code-cell} ipython3 |
| 23 | +import xarray as xr |
| 24 | +
|
| 25 | +xr.set_options(display_expand_attrs=False, display_expand_data=True); |
| 26 | +``` |
| 27 | + |
| 28 | +We open an Xarray dataset (in netCDF format) using the usual `open_dataset` function. By specifying `chunks={}` we ensure that the dataset is chunked using the on-disk chunking (here it is the netCDF file chunking). The `chunked_array_type` argument specifies which chunked array type to use - Cubed in this case. |
| 29 | + |
| 30 | +```{code-cell} ipython3 |
| 31 | +ds = xr.tutorial.open_dataset( |
| 32 | + "air_temperature", chunked_array_type="cubed", chunks={} |
| 33 | +) |
| 34 | +ds |
| 35 | +``` |
| 36 | + |
| 37 | +Notice that the `air` data variable is a `cubed.Array`. Since Cubed has a lazy computation model, this array is not loaded from disk until a computation is run. |
| 38 | + |
| 39 | +## Convert to Zarr |
| 40 | + |
| 41 | +We can use Cubed to convert the dataset to Zarr format by calling `to_zarr` on the dataset: |
| 42 | + |
| 43 | +```{code-cell} ipython3 |
| 44 | +ds.to_zarr("air_temperature_cubed.zarr", mode="w", consolidated=True); |
| 45 | +``` |
| 46 | + |
| 47 | +This will run a computation that loads the input data and writes it out to a Zarr store on the local filesystem. |
| 48 | + |
| 49 | +## Compute the mean |
| 50 | + |
| 51 | +We can also use Xarray's API to run computations on the dataset using Cubed. Here we find the mean air temperature over time, for each location: |
| 52 | + |
| 53 | +```{code-cell} ipython3 |
| 54 | +mean = ds.air.mean("time", skipna=False) |
| 55 | +mean |
| 56 | +``` |
| 57 | + |
| 58 | +To run the computation we need to call `compute`: |
| 59 | + |
| 60 | +```{code-cell} ipython3 |
| 61 | +mean.compute() |
| 62 | +``` |
| 63 | + |
| 64 | +This is fine for outputs that fit in memory like the example here, but sometimes we want to write the output of the computation to Zarr, which we do by calling `to_zarr` on the dataset instead of `compute`: |
| 65 | + |
| 66 | +```{code-cell} ipython3 |
| 67 | +mean.to_zarr("mean_air_temperature.zarr", mode="w", consolidated=True); |
| 68 | +``` |
| 69 | + |
| 70 | +We can check that the Zarr file was created by loading it from disk using `xarray.open_dataset`: |
| 71 | + |
| 72 | +```{code-cell} ipython3 |
| 73 | +xr.open_dataset( |
| 74 | + "mean_air_temperature.zarr", chunked_array_type="cubed", chunks={} |
| 75 | +) |
| 76 | +``` |
0 commit comments