Skip to content

Commit

Permalink
add climatology
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Aug 3, 2023
1 parent 37fbdf9 commit c1869cd
Showing 1 changed file with 239 additions and 0 deletions.
239 changes: 239 additions & 0 deletions notebooks/wp5/era5_t2m_variability.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "18885bce",
"metadata": {},
"source": [
"# ERA5 2m temperature variability"
]
},
{
"cell_type": "markdown",
"id": "8988c8f5",
"metadata": {},
"source": [
"## Import packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "572c1812-a770-423f-be79-496c5db28f2f",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import xarray as xr\n",
"from c3s_eqc_automatic_quality_control import diagnostics, download, utils\n",
"\n",
"plt.style.use(\"seaborn-v0_8-notebook\")\n",
"plt.rcParams[\"figure.figsize\"] = [15, 5]"
]
},
{
"cell_type": "markdown",
"id": "50bf1006",
"metadata": {},
"source": [
"## Define Parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b01646ef-b1db-448e-93d0-d5ff3c4f247b",
"metadata": {},
"outputs": [],
"source": [
"# Time period\n",
"year_start = 1940\n",
"year_stop = 2022\n",
"\n",
"# Climatology periods\n",
"climatologies = (slice(\"1991\", \"2020\"), slice(\"1981\", \"2010\"), slice(\"1961\", \"1990\"))\n",
"\n",
"# Regions\n",
"regions = {\n",
" \"Global\": {\"lon_slice\": slice(0, 360), \"lat_slice\": slice(90, -90)},\n",
" \"Tropics\": {\"lon_slice\": slice(0, 360), \"lat_slice\": slice(30, -30)},\n",
" \"NH mid latitude\": {\"lon_slice\": slice(0, 360), \"lat_slice\": slice(60, 30)},\n",
" \"SH mid latitude\": {\"lon_slice\": slice(0, 360), \"lat_slice\": slice(-30, -60)},\n",
" \"Northern pole\": {\"lon_slice\": slice(0, 360), \"lat_slice\": slice(90, 60)},\n",
" \"Southern pole\": {\"lon_slice\": slice(0, 360), \"lat_slice\": slice(-60, -90)},\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "ccee61a1",
"metadata": {},
"source": [
"## Define request"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1dbc778-ca4e-4fa3-aa76-24d1fd653081",
"metadata": {},
"outputs": [],
"source": [
"collection_id = \"reanalysis-era5-single-levels-monthly-means\"\n",
"request = {\n",
" \"product_type\": \"monthly_averaged_reanalysis\",\n",
" \"variable\": \"2m_temperature\",\n",
" \"year\": [str(year) for year in range(year_start, year_stop + 1)],\n",
" \"month\": [f\"{month:02d}\" for month in range(1, 12 + 1)],\n",
" \"time\": \"00:00\",\n",
" \"format\": \"grib\",\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "45a70520",
"metadata": {},
"source": [
"## Functions to cache"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "286155da-33a4-4ad8-b98a-10e78e02424b",
"metadata": {},
"outputs": [],
"source": [
"def regionalised_spatial_weighted_mean(ds, lon_slice, lat_slice, **kwargs):\n",
" ds = utils.regionalise(ds, lon_slice=lon_slice, lat_slice=lat_slice)\n",
" return diagnostics.spatial_weighted_mean(ds, **kwargs)"
]
},
{
"cell_type": "markdown",
"id": "6d8a5b5c",
"metadata": {},
"source": [
"## Download and transform"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d439e28d-7723-40bb-b2fd-6c7d70146b8c",
"metadata": {},
"outputs": [],
"source": [
"datasets = []\n",
"for region, transform_func_kwargs in regions.items():\n",
" print(f\"{region=}\")\n",
" ds = download.download_and_transform(\n",
" collection_id,\n",
" request,\n",
" chunks={\"year\": 1},\n",
" transform_func=regionalised_spatial_weighted_mean,\n",
" transform_func_kwargs=transform_func_kwargs,\n",
" )\n",
" datasets.append(ds.expand_dims(region=[region]))\n",
"\n",
"da = xr.concat(datasets, \"region\")[\"t2m\"].reset_coords(drop=True).compute()"
]
},
{
"cell_type": "markdown",
"id": "c5e11f69",
"metadata": {},
"source": [
"## Plot monthly climatologies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82ae0a0d-f985-4602-8c56-e27d17ce1d38",
"metadata": {},
"outputs": [],
"source": [
"dataarrays = []\n",
"for clima_slice in climatologies:\n",
" da_clima = da.sel(forecast_reference_time=clima_slice)\n",
" da_monthly = diagnostics.monthly_weighted_mean(da_clima, weights=False)\n",
" dataarrays.append(\n",
" da_monthly.expand_dims(period=[f\"{clima_slice.start}-{clima_slice.stop}\"])\n",
" )\n",
"da_clima_monthly = xr.concat(dataarrays, \"period\")\n",
"da_clima_monthly.sel(region=\"Global\").plot(hue=\"period\")\n",
"_ = plt.grid()"
]
},
{
"cell_type": "markdown",
"id": "2ec03335",
"metadata": {},
"source": [
"## Plot monthly anomalies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d7a5188e-ea84-4a0f-92e7-452a97a0ab35",
"metadata": {},
"outputs": [],
"source": [
"with xr.set_options(keep_attrs=True):\n",
" da_anoma_monthly = da.groupby(\"forecast_reference_time.month\") - da_clima_monthly\n",
"da_anoma_monthly.attrs[\"long_name\"] += \" anomaly\"\n",
"da_anoma_monthly.sel(region=\"Global\").plot(hue=\"period\")\n",
"_ = plt.grid()"
]
},
{
"cell_type": "markdown",
"id": "37dcb4fa",
"metadata": {},
"source": [
"## Plot regional anomalies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4b05ea5-4f48-4566-b3f7-3b90e8dd12e3",
"metadata": {},
"outputs": [],
"source": [
"for regions_to_plot in (\n",
" [\"Tropics\", \"NH mid latitude\", \"SH mid latitude\"],\n",
" [\"Northern pole\", \"Southern pole\"],\n",
"):\n",
" da_anoma_monthly.sel(region=regions_to_plot, period=\"1991-2020\").plot(hue=\"region\")\n",
" plt.grid()\n",
" plt.show()"
]
}
],
"metadata": {
"celltoolbar": "Raw Cell Format",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit c1869cd

Please sign in to comment.