{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Climatology calculations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Imports\n", "from earthkit import transforms as ekt\n", "from earthkit import data as ekd\n", "from earthkit.data.testing import earthkit_remote_test_data_file\n", "ekd.settings.set(\"cache-policy\", \"user\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load some test data\n", "\n", "In this example we will use hourly ERA5 2m temperature data on a 0.5x0.5 spatial grid for the year 2015 as\n", "our physical data; and we will use the NUTS geometries which are stored in a geojson file.\n", "\n", "All `earthkit-transforms` methods can be called with `earthkit-data` objects (Readers and Wrappers) or with a pre-loaded `xarray`. To reduce the number of conversions in the example, we will convert to xarray in the first cell and use that data object for all subsequent steps." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Get some demonstration ERA5 data, this could be any url or path to an ERA5 grib or netCDF file.\n", "remote_era5_file = earthkit_remote_test_data_file(\"era5_temperature_france_2015_2016_2017_3deg.grib\")\n", "era5_data = ekd.from_source(\"url\", remote_era5_file)\n", "\n", "# convert to xarray to save repeated conversion in further steps\n", "era5_xr = era5_data.to_xarray(time_dim_mode=\"valid_time\")\n", "era5_xr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate the climatologies of the ERA5 data\n", "\n", "Monthly mean" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "climatology_monthly_mean = ekt.climatology.monthly_mean(era5_xr)\n", "# # The following line would also work, but we have already converted the data to xarray,\n", "# # so do not need to do it again.\n", "# climatology = ek_aggregate.climatology.monthly_mean(era5_xr)\n", "climatology_monthly_mean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Climatology of the daily mean:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "climatology_daily_mean = ekt.climatology.daily_mean(era5_xr)\n", "climatology_daily_mean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Repeat for the monthly maximum, minimum and standard deviation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clim_max = ekt.climatology.monthly_max(era5_xr)\n", "clim_min = ekt.climatology.monthly_min(era5_xr)\n", "clim_std = ekt.climatology.monthly_std(era5_xr)\n", "clim_std" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Quantiles\n", "\n", "Please note the api for quantiles is slightly different, it requires an additional argument `q` which is a list of the quantiles to return.\n", "\n", "Additionally, the returned object has `quantiles` dimension which is for each of the quantiles returned." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "quantiles = ekt.climatology.quantiles(era5_xr, [0.1, 0.5, 0.9], frequency='month')\n", "quantiles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot the output for a random location" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from datetime import datetime, timedelta\n", "import xarray as xr\n", "isel_kwargs = {\"latitude\":2, \"longitude\":4}\n", "\n", "fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10,5))\n", "\n", "for data in [climatology_monthly_mean, clim_min, clim_max]:\n", " var_name = list(data.data_vars.keys())[0]\n", " c_point = data[var_name].isel(**isel_kwargs)\n", " c_time = [datetime(2000,i,15) for i in c_point.month.values]\n", " ax.plot(c_time, c_point.values.flat, label=f'Monthly {var_name}')\n", "\n", "\n", "data = climatology_daily_mean\n", "var_name = list(data.data_vars.keys())[0]\n", "c_point = data[var_name].isel(**isel_kwargs)\n", "c_time = [datetime(2000,1,1)+timedelta(days=int(i-1)) for i in c_point.dayofyear.values]\n", "ax.plot(c_time, c_point.values.flat, label=f'Daily {var_name}')\n", "\n", "ax.legend()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "DEVELOP", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 4 }