{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# More climatology reductions using Cubed\n", "\n", "This is the Cubed equivalent of [More climatology reductions](climatology-hourly.ipynb).\n", "\n", "The task is to compute an hourly climatology from an hourly dataset with 744 hours in each chunk, using the \"map-reduce\" strategy." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "import cubed\n", "import cubed.array_api as xp\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "\n", "import flox.xarray" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## Create data\n", "\n", "Note that we use fewer lat/long points so the computation can be run locally." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "spec = cubed.Spec(allowed_mem=\"2GB\")\n", "ds = xr.Dataset(\n", " {\n", " \"tp\": (\n", " (\"time\", \"latitude\", \"longitude\"),\n", " xp.ones((8760, 72, 144), chunks=(744, 5, 144), dtype=np.float32, spec=spec),\n", " )\n", " },\n", " coords={\"time\": pd.date_range(\"2021-01-01\", \"2021-12-31 23:59\", freq=\"h\")},\n", ")\n", "ds" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## Computation" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "hourly = flox.xarray.xarray_reduce(ds.tp, ds.time.dt.hour, func=\"mean\", reindex=True)\n", "hourly" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "hourly.compute()" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "## Other climatologies: resampling by month\n", "\n", "This uses the \"blockwise\" strategy." ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "monthly = ds.tp.resample(time=\"ME\").sum(method=\"blockwise\")\n", "monthly" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "monthly.compute()" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }