Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescale era5 wind speed with GWA3.1 #377

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added scale_wind_data/gwa_era5.pdf
Binary file not shown.
176 changes: 176 additions & 0 deletions scale_wind_data/scaling_script.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import atlite\n",
"import rioxarray\n",
"import numpy as np\n",
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.axes_grid1 import AxesGrid\n",
"import rasterio"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name=\"shiftedcmap\"):\n",
" \"\"\"\n",
" Function to offset the \"center\" of a colormap. Useful for\n",
" data with a negative min and positive max and you want the\n",
" middle of the colormap's dynamic range to be at zero.\n",
"\n",
" Input\n",
" -----\n",
" cmap : The matplotlib colormap to be altered\n",
" start : Offset from lowest point in the colormap's range.\n",
" Defaults to 0.0 (no lower offset). Should be between\n",
" 0.0 and `midpoint`.\n",
" midpoint : The new center of the colormap. Defaults to\n",
" 0.5 (no shift). Should be between 0.0 and 1.0. In\n",
" general, this should be 1 - vmax / (vmax + abs(vmin))\n",
" For example if your data range from -15.0 to +5.0 and\n",
" you want the center of the colormap at 0.0, `midpoint`\n",
" should be set to 1 - 5/(5 + 15)) or 0.75\n",
" stop : Offset from highest point in the colormap's range.\n",
" Defaults to 1.0 (no upper offset). Should be between\n",
" `midpoint` and 1.0.\n",
" \"\"\"\n",
" cdict = {\"red\": [], \"green\": [], \"blue\": [], \"alpha\": []}\n",
"\n",
" # regular index to compute the colors\n",
" reg_index = np.linspace(start, stop, 257)\n",
"\n",
" # shifted index to match the data\n",
" shift_index = np.hstack(\n",
" [\n",
" np.linspace(0.0, midpoint, 128, endpoint=False),\n",
" np.linspace(midpoint, 1.0, 129, endpoint=True),\n",
" ]\n",
" )\n",
"\n",
" for ri, si in zip(reg_index, shift_index):\n",
" r, g, b, a = cmap(ri)\n",
"\n",
" cdict[\"red\"].append((si, r, r))\n",
" cdict[\"green\"].append((si, g, g))\n",
" cdict[\"blue\"].append((si, b, b))\n",
" cdict[\"alpha\"].append((si, a, a))\n",
"\n",
" newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict)\n",
"\n",
" return newcmap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load cutout and gwa raster. files need to be downloaded manually\n",
"cutout = atlite.Cutout(\"europe-2013-era5.nc\")\n",
"gwa_data = rioxarray.open_rasterio(\"gwa3_250_wind-speed_100m.tif\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# use version 1.2.9 of rasterio for the working Resampling by mean\n",
"# downsamples the gwa data to match the raster of the cutout. The resampling is done with averaging the wind speed.\n",
"ds = gwa_data.sel(\n",
" band=1, x=slice(*cutout.extent[[0, 1]]), y=slice(*cutout.extent[[3, 2]])\n",
")\n",
"ds = ds.where(ds != -999)\n",
"ds = atlite.gis.regrid(\n",
" ds, cutout.data.x, cutout.data.y, resampling=rasterio.warp.Resampling.average\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, 3, figsize=(20, 10))\n",
"\n",
"cutout.data.wnd100m.mean(\"time\").plot(ax=ax[0], vmin=0, vmax=15)\n",
"\n",
"ds.plot(ax=ax[1], vmin=0, vmax=15)\n",
"\n",
"newcmap = shiftedColorMap(cmap=matplotlib.cm.PiYG, start=0, stop=1, midpoint=0.5)\n",
"# gwa wind speed mean divided by cutout wind speed mean -> corresponds to the bias correction factor\n",
"(ds / cutout.data.wnd100m.mean(\"time\")).plot(ax=ax[2], cmap=newcmap, vmax=2, vmin=0)\n",
"\n",
"ax[0].set_title(\"era5\")\n",
"ax[1].set_title(\"gwa3.1\")\n",
"ax[2].set_title(\"ratio gwa3.1/era5\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(ds / cutout.data.wnd100m.mean(\"time\")).mean().compute()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig.savefig(\"gwa_era5.pdf\", dpi=400, facecolor=\"white\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "",
"language": "python",
"name": ""
},
"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.9.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "cbf508f5955c6c5a1d60300bdfc4af0658e2d7e289dab24c7eb997efd92e3877"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}