Skip to content

Commit

Permalink
adjust future
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Mar 25, 2024
1 parent f220ca0 commit 6e19746
Showing 1 changed file with 72 additions and 28 deletions.
100 changes: 72 additions & 28 deletions notebooks/wp4/extreme_temperature_indices_future.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@
"outputs": [],
"source": [
"# Time period\n",
"year_start = 2015\n",
"year_stop = 2099\n",
"historical_slice = slice(1971, 2000)\n",
"future_slice = slice(2015, 2099)\n",
"assert future_slice.start > historical_slice.stop\n",
"\n",
"# Choose annual or seasonal timeseries\n",
"timeseries = \"JJA\"\n",
Expand Down Expand Up @@ -196,7 +197,6 @@
"request_cordex = {\n",
" \"format\": \"zip\",\n",
" \"domain\": cordex_domain,\n",
" \"experiment\": \"rcp_8_5\",\n",
" \"horizontal_resolution\": \"0_11_degree_x_0_11_degree\",\n",
" \"temporal_resolution\": \"daily_mean\",\n",
" \"variable\": \"maximum_2m_temperature_in_the_last_24_hours\",\n",
Expand All @@ -208,28 +208,22 @@
"request_cmip6 = {\n",
" \"format\": \"zip\",\n",
" \"temporal_resolution\": \"daily\",\n",
" \"experiment\": \"ssp5_8_5\",\n",
" \"variable\": \"daily_maximum_near_surface_air_temperature\",\n",
" \"year\": [\n",
" str(year)\n",
" for year in range(year_start - int(timeseries == \"DJF\"), year_stop + 1)\n",
" ], # Include D(year-1)\n",
" \"month\": [f\"{month:02d}\" for month in range(1, 13)],\n",
" \"day\": [f\"{day:02d}\" for day in range(1, 32)],\n",
" \"area\": area,\n",
"}\n",
"\n",
"\n",
"def get_cordex_years(\n",
" year_start,\n",
" year_stop,\n",
" start_years=list(range(2006, 2097, 5)),\n",
" end_years=list(range(2010, 2101, 5)),\n",
" year_slice,\n",
" start_years=list(range(1971, 2097, 5)),\n",
" end_years=list(range(1975, 2101, 5)),\n",
"):\n",
" start_year = []\n",
" end_year = []\n",
" years = set(\n",
" range(year_start - int(timeseries == \"DJF\"), year_stop + 1)\n",
" range(year_slice.start - int(timeseries == \"DJF\"), year_slice.stop + 1)\n",
" ) # Include D(year-1)\n",
" for start, end in zip(start_years, end_years):\n",
" if years & set(range(start, end + 1)):\n",
Expand All @@ -238,6 +232,16 @@
" return start_year, end_year\n",
"\n",
"\n",
"def get_cmip6_years(year_slice):\n",
" return [\n",
" str(year)\n",
" for year in range(\n",
" year_slice.start - int(timeseries == \"DJF\"), # Include D(year-1)\n",
" year_slice.stop + 1,\n",
" )\n",
" ]\n",
"\n",
"\n",
"if collection_id == \"CORDEX\":\n",
" models = models_cordex\n",
" model_key = \"rcm_model\"\n",
Expand All @@ -246,22 +250,44 @@
" [\n",
" {\n",
" **request_cordex,\n",
" \"experiment\": \"historical\",\n",
" \"start_year\": start_year,\n",
" \"end_year\": end_year,\n",
" }\n",
" for start_year, end_year in zip(*get_cordex_years(historical_slice))\n",
" ]\n",
" + [\n",
" {\n",
" **request_cordex,\n",
" \"experiment\": \"ssp5_8_5\",\n",
" \"start_year\": start_year,\n",
" \"end_year\": end_year,\n",
" }\n",
" for start_year, end_year in zip(*get_cordex_years(year_start, year_stop))\n",
" for start_year, end_year in zip(*get_cordex_years(future_slice))\n",
" ],\n",
" )\n",
"elif collection_id == \"CMIP6\":\n",
" models = models_cmip6\n",
" model_key = \"model\"\n",
"\n",
" requests_historical = download.split_request(\n",
" request_cmip6\n",
" | {\"year\": get_cmip6_years(historical_slice), \"experiment\": \"historical\"},\n",
" chunks=chunks,\n",
" )\n",
" requests_future = download.split_request(\n",
" request_cmip6\n",
" | {\"year\": get_cmip6_years(future_slice), \"experiment\": \"ssp5_8_5\"},\n",
" chunks=chunks,\n",
" )\n",
" request_sim = (\n",
" \"projections-cmip6\",\n",
" download.split_request(request_cmip6, chunks=chunks),\n",
" requests_historical + requests_future,\n",
" )\n",
"else:\n",
" raise ValueError(f\"{collection_id=}\")\n",
"\n",
"\n",
"request_grid_out = (\n",
" request_sim[0],\n",
" request_sim[1][0] | {model_key: model_regrid},\n",
Expand All @@ -288,14 +314,21 @@
},
"outputs": [],
"source": [
"def select_timeseries(ds, timeseries, year_start, year_stop):\n",
"def select_timeseries(ds, timeseries, year_slice):\n",
" if timeseries == \"annual\":\n",
" return ds.sel(time=slice(str(year_start), str(year_stop)))\n",
" ds = ds.sel(time=slice(f\"{year_start-1}-12\", f\"{year_stop}-11\"))\n",
" return ds.sel(time=slice(str(year_slice.start), str(year_slice.stop)))\n",
" ds = ds.sel(time=slice(f\"{year_slice.start-1}-12\", f\"{year_slice.stop}-11\"))\n",
" return ds.where(ds[\"time\"].dt.season == timeseries, drop=True)\n",
"\n",
"\n",
"def compute_indices(ds, index_names, timeseries, tmpdir):\n",
"def compute_indices(\n",
" ds,\n",
" index_names,\n",
" timeseries,\n",
" tmpdir,\n",
" historical_slice,\n",
" future_slice,\n",
"):\n",
" labels, datasets = zip(*ds.groupby(\"time.year\"))\n",
" paths = [f\"{tmpdir}/{label}.nc\" for label in labels]\n",
" datasets = [ds.chunk(-1) for ds in datasets]\n",
Expand All @@ -306,12 +339,17 @@
" chunks = {dim: -1 if dim == \"time\" else \"auto\" for dim in ds.dims}\n",
" ds.chunk(chunks).to_zarr(in_files)\n",
"\n",
" time_range = (f\"{future_slice.start}-01-01\", f\"{future_slice.stop}-12-31\")\n",
" base_range = (f\"{historical_slice.start}-01-01\", f\"{historical_slice.stop}-12-31\")\n",
"\n",
" datasets = [\n",
" icclim.index(\n",
" index_name=index_name,\n",
" in_files=in_files,\n",
" out_file=f\"{tmpdir}/{index_name}.nc\",\n",
" slice_mode=\"year\" if timeseries == \"annual\" else timeseries,\n",
" time_range=time_range,\n",
" base_period_time_range=base_range if index_name == \"TX90p\" else None,\n",
" )\n",
" for index_name in index_names\n",
" ]\n",
Expand Down Expand Up @@ -362,12 +400,12 @@
" return grid_out\n",
"\n",
"\n",
"def compute_indices_and_trends(\n",
"def compute_indices_and_trends_future(\n",
" ds,\n",
" index_names,\n",
" timeseries,\n",
" year_start,\n",
" year_stop,\n",
" historical_slice,\n",
" future_slice,\n",
" resample,\n",
" request_grid_out=None,\n",
" **regrid_kwargs,\n",
Expand All @@ -388,11 +426,15 @@
" else:\n",
" bounds = []\n",
"\n",
" ds = select_timeseries(ds, timeseries, year_start, year_stop)\n",
" ds_historical = select_timeseries(ds, timeseries, historical_slice)\n",
" ds_future = select_timeseries(ds, timeseries, future_slice)\n",
" ds = xr.concat([ds_historical, ds_future], \"time\")\n",
" if resample:\n",
" ds = ds.resample(time=\"1D\").max(keep_attrs=True)\n",
" with tempfile.TemporaryDirectory() as tmpdir:\n",
" ds_indices = compute_indices(ds, index_names, timeseries, tmpdir).compute()\n",
" ds_indices = compute_indices(\n",
" ds, index_names, timeseries, tmpdir, historical_slice, future_slice\n",
" ).compute()\n",
" ds_trends = compute_trends(ds_indices)\n",
" ds = ds_indices.mean(\"time\", keep_attrs=True)\n",
" ds = ds.merge(ds_trends)\n",
Expand Down Expand Up @@ -428,13 +470,13 @@
" \"collection_id\": request_sim[0],\n",
" \"chunks\": chunks if collection_id == \"CMIP6\" else None,\n",
" \"transform_chunks\": False,\n",
" \"transform_func\": compute_indices_and_trends,\n",
" \"transform_func\": compute_indices_and_trends_future,\n",
"}\n",
"transform_func_kwargs = {\n",
" \"index_names\": sorted(index_names),\n",
" \"timeseries\": timeseries,\n",
" \"year_start\": year_start,\n",
" \"year_stop\": year_stop,\n",
" \"historical_slice\": historical_slice,\n",
" \"future_slice\": future_slice,\n",
" \"resample\": False,\n",
"}\n",
"ds_regrid = download.download_and_transform(\n",
Expand Down Expand Up @@ -708,7 +750,9 @@
" return fig\n",
"\n",
"\n",
"common_title = f\"{year_start=} {year_stop=} {collection_id=} {timeseries=}\""
"common_title = (\n",
" f\"{future_slice.start=} {future_slice.stop=} {collection_id=} {timeseries=}\"\n",
")"
]
},
{
Expand Down

0 comments on commit 6e19746

Please sign in to comment.