Skip to content

Commit

Permalink
fix trend cmap
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Jul 19, 2023
1 parent df518c6 commit 8b4e618
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions notebooks/wp4/trend_cordex_cmip6.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"source": [
"# Time period\n",
"year_start = 1971\n",
"year_stop = 2005\n",
"year_stop = 1972\n",
"\n",
"# Climatology period\n",
"clima_year_start = 1971\n",
"clima_year_stop = 1980\n",
"clima_year_stop = 1971\n",
"\n",
"# Choose annual or seasonal timeseries\n",
"timeseries = \"annual\"\n",
Expand Down Expand Up @@ -617,7 +617,16 @@
" da_trend *= 100 / ds_all_regr[f\"{variable}_climatology\"]\n",
" units = \"%/decade\"\n",
"da_trend.name = \"\"\n",
"da_trend.attrs.update({\"units\": units})"
"da_trend.attrs.update({\"units\": units})\n",
"\n",
"# Compute bias\n",
"da_bias = (ds_all_regr[\"trend\"].drop_sel(model=\"ERA5\") - ds_era[\"trend\"].squeeze()) * 10\n",
"units = ds_all_regr[f\"{variable}_climatology\"].attrs[\"units\"] + \"/decade\"\n",
"if variable == \"precipitation\":\n",
" da_bias *= 100 / ds_era[f\"{variable}_climatology\"].squeeze()\n",
" units = \"%/decade\"\n",
"da_bias.name = \"\"\n",
"da_bias.attrs.update({\"units\": units})"
]
},
{
Expand All @@ -640,16 +649,20 @@
" and abs(lat_slice.stop - lat_slice.start) >= 180\n",
" else ccrs.PlateCarree\n",
")\n",
"shading_kwargs = xr.plot.utils._determine_cmap_params(\n",
"projection = Projection(central_longitude=(lon_slice.stop + lon_slice.start) / 2)\n",
"\n",
"plot_kwargs = {\"levels\": 11, \"robust\": True, \"extend\": \"both\"}\n",
"trend_kwargs = xr.plot.utils._determine_cmap_params(\n",
" da_trend.values,\n",
" levels=11,\n",
" robust=True,\n",
" cmap=\"PuOr\" if variable == \"precipitation\" else \"PuOr_r\",\n",
" **plot_kwargs,\n",
") | {\"projection\": projection}\n",
"bias_kwargs = xr.plot.utils._determine_cmap_params(\n",
" da_bias.values,\n",
" cmap=\"bwr_r\" if variable == \"precipitation\" else \"bwr\",\n",
" extend=\"both\",\n",
")\n",
"shading_kwargs[\"projection\"] = Projection(\n",
" central_longitude=(lon_slice.stop + lon_slice.start) / 2\n",
")\n",
" **plot_kwargs,\n",
") | {\"projection\": projection}\n",
"\n",
"cbar_ax = [0.05, -0.04, 0.95, 0.04]\n",
"\n",
"hatches = [\"\", \"/\" * 5]\n",
Expand Down Expand Up @@ -683,7 +696,7 @@
"metadata": {},
"outputs": [],
"source": [
"plot.projected_map(da_trend.sel(model=\"ERA5\"), **shading_kwargs)\n",
"plot.projected_map(da_trend.sel(model=\"ERA5\"), **trend_kwargs)\n",
"plot.projected_map(\n",
" ds_all_regr[\"p\"].sel(model=\"ERA5\").drop_vars([\"height\", \"season\"], errors=\"ignore\"),\n",
" **p_hatches_kwargs,\n",
Expand Down Expand Up @@ -718,7 +731,7 @@
"plot.projected_map(\n",
" da_trend.sel(model=collection_id).drop_vars([\"height\", \"season\"], errors=\"ignore\"),\n",
" stats_weights=weights,\n",
" **shading_kwargs,\n",
" **trend_kwargs,\n",
")\n",
"plot.projected_map(\n",
" is_signif_ratio.drop_vars([\"height\", \"season\"], errors=\"ignore\").squeeze(),\n",
Expand Down Expand Up @@ -748,7 +761,7 @@
" col=\"model\",\n",
" col_wrap=3,\n",
" add_colorbar=False,\n",
" **shading_kwargs,\n",
" **trend_kwargs,\n",
")\n",
"for ax, sel in zip(facet.axs.flatten(), facet.name_dicts.flatten()):\n",
" if not sel:\n",
Expand Down Expand Up @@ -785,21 +798,14 @@
"metadata": {},
"outputs": [],
"source": [
"da_bias = (ds_all_regr[\"trend\"].drop_sel(model=\"ERA5\") - ds_era[\"trend\"].squeeze()) * 10\n",
"units = ds_all_regr[f\"{variable}_climatology\"].attrs[\"units\"] + \"/decade\"\n",
"if variable == \"precipitation\":\n",
" da_bias *= 100 / ds_era[f\"{variable}_climatology\"].squeeze()\n",
" units = \"%/decade\"\n",
"da_bias.name = \"\"\n",
"da_bias.attrs.update({\"units\": units})\n",
"facet = plot.projected_map(\n",
" da_bias.drop_sel(model=collection_id).drop_vars(\n",
" [\"height\", \"season\"], errors=\"ignore\"\n",
" ),\n",
" col=\"model\",\n",
" col_wrap=3,\n",
" add_colorbar=False,\n",
" **shading_kwargs,\n",
" **bias_kwargs,\n",
")\n",
"for ax in facet.axs.flatten():\n",
" ax.set_extent((lon_slice.start, lon_slice.stop, lat_slice.start, lat_slice.stop))\n",
Expand Down Expand Up @@ -832,7 +838,7 @@
"plot.projected_map(\n",
" da_bias.sel(model=collection_id).drop_vars([\"height\", \"season\"], errors=\"ignore\"),\n",
" stats_weights=weights,\n",
" **shading_kwargs,\n",
" **bias_kwargs,\n",
")\n",
"_ = plt.suptitle(\n",
" f\"{variable.title()} trend bias ({year_start}-{year_stop}) - {timeseries.upper()}\"\n",
Expand Down

0 comments on commit 8b4e618

Please sign in to comment.