Skip to content

Commit

Permalink
fix extremes
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Jul 18, 2023
1 parent 548e00a commit 6d9736b
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions notebooks/wp5/era5_cerra_extremes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"month = 8\n",
"assert month in range(1, 13), f\"{month=} is invalid\"\n",
"\n",
"# Select region of interest\n",
"# Select region of interest (lon: 0-360)\n",
"region = \"Lazio\"\n",
"region_slices = {\n",
" \"Alps\": {\"lon_slice\": slice(5, 15), \"lat_slice\": slice(48, 43)},\n",
Expand All @@ -86,6 +86,11 @@
"metadata": {},
"outputs": [],
"source": [
"lon_slice = region_slices[region][\"lon_slice\"]\n",
"lat_slice = region_slices[region][\"lat_slice\"]\n",
"lons = tuple((lon + 180) % 360 - 180 for lon in (lon_slice.start, lon_slice.stop))\n",
"lats = (lat_slice.start, lat_slice.stop)\n",
"\n",
"requests_dict = {\n",
" \"reanalysis-cerra-single-levels\": {\n",
" \"variable\": \"2m_temperature\",\n",
Expand All @@ -106,6 +111,7 @@
" \"format\": \"grib\",\n",
" \"variable\": \"2m_temperature\",\n",
" \"time\": [f\"{i:02d}:00\" for i in range(24)],\n",
" \"area\": [max(lats), min(lons), min(lats), max(lons)],\n",
" },\n",
"}"
]
Expand All @@ -126,10 +132,16 @@
"metadata": {},
"outputs": [],
"source": [
"def regionalised_max(ds, lon_slice, lat_slice):\n",
"def regionalised_max(ds, lon_slice=None, lat_slice=None):\n",
" if lon_slice and lat_slice:\n",
" ds = utils.regionalise(ds, lon_slice=lon_slice, lat_slice=lat_slice)\n",
" else:\n",
" assert lon_slice == lat_slice is None\n",
" time = ds[\"forecast_reference_time\"].mean()\n",
" ds = utils.regionalise(ds, lon_slice=lon_slice, lat_slice=lat_slice)\n",
" ds = ds.max(\"forecast_reference_time\", keep_attrs=True)\n",
" if \"leadtime\" in ds.dims:\n",
" time += ds[\"leadtime\"].mean()\n",
" ds = ds.max(\"leadtime\", keep_attrs=True)\n",
" return ds.expand_dims(time=[time.values])"
]
},
Expand All @@ -151,6 +163,7 @@
"source": [
"dataarrays = {}\n",
"for collection_id, request in requests_dict.items():\n",
" transform_func_kwargs = region_slices[region] if \"area\" not in region else {}\n",
" requests = []\n",
" for year in range(year_start, year_stop + 1):\n",
" time_request = {\n",
Expand All @@ -163,8 +176,7 @@
" collection_id,\n",
" requests,\n",
" transform_func=regionalised_max,\n",
" transform_func_kwargs=region_slices[region],\n",
" backend_kwargs={\"time_dims\": (\"time\",)},\n",
" transform_func_kwargs=transform_func_kwargs,\n",
" )\n",
" (varname,) = ds.data_vars\n",
" da = ds[varname].max(\"time\", keep_attrs=True)\n",
Expand Down

0 comments on commit 6d9736b

Please sign in to comment.