Skip to content

Commit

Permalink
fix future
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Mar 13, 2024
1 parent e854020 commit bc8d152
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions notebooks/wp4/extreme_temperature_indices_future.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"assert timeseries in (\"annual\", \"DJF\", \"MAM\", \"JJA\", \"SON\")\n",
"\n",
"# Choose CORDEX or CMIP6\n",
"collection_id = \"CMIP6\"\n",
"collection_id = \"CORDEX\"\n",
"assert collection_id in (\"CORDEX\", \"CMIP6\")\n",
"\n",
"# Define region for analysis\n",
Expand All @@ -104,16 +104,6 @@
"chunks = {\"year\": 1}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if collection_id == \"CORDEX\":\n",
" raise NotImplementedError(f\"{collection_id=}\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down Expand Up @@ -206,7 +196,7 @@
"request_cordex = {\n",
" \"format\": \"zip\",\n",
" \"domain\": cordex_domain,\n",
" \"experiment\": \"historical\",\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 @@ -233,8 +223,8 @@
"def get_cordex_years(\n",
" year_start,\n",
" year_stop,\n",
" start_years=[1971, 1976, 1981, 1986, 1991],\n",
" end_years=[1975, 1980, 1985, 1990, 1995],\n",
" start_years=list(range(2006, 2097, 5)),\n",
" end_years=list(range(2010, 2101, 5)),\n",
"):\n",
" start_year = []\n",
" end_year = []\n",
Expand Down Expand Up @@ -308,9 +298,8 @@
"\n",
" ds = xr.open_mfdataset(paths)\n",
" in_files = f\"{tmpdir}/rechunked.zarr\"\n",
" ds.chunk({dim: -1 if dim == \"time\" else \"auto\" for dim in ds.dims}).to_zarr(\n",
" in_files\n",
" )\n",
" chunks = {dim: -1 if dim == \"time\" else \"auto\" for dim in ds.dims}\n",
" ds.chunk(chunks).to_zarr(in_files)\n",
"\n",
" datasets = [\n",
" icclim.index(\n",
Expand Down Expand Up @@ -418,7 +407,6 @@
" \"year_stop\": year_stop,\n",
" \"resample\": False,\n",
"}\n",
"\n",
"ds_regrid = download.download_and_transform(\n",
" requests=[request | {model_key: model_regrid} for request in request_sim[1]],\n",
" **kwargs,\n",
Expand All @@ -445,31 +433,36 @@
},
"outputs": [],
"source": [
"grid_out = ds_regrid[[\"latitude\", \"longitude\"]].reset_coords(drop=True)\n",
"coords = [\"latitude\", \"longitude\"]\n",
"grid_out = ds_regrid[coords]\n",
"coords_to_drop = set(grid_out.coords) - set(coords) - set(grid_out.dims)\n",
"grid_out = ds_regrid[coords].reset_coords(coords_to_drop, drop=True)\n",
"grid_out.attrs = {}\n",
"\n",
"interpolated_datasets = []\n",
"model_datasets = {}\n",
"for model in models:\n",
" print(f\"{model=}\")\n",
" # Original model\n",
" model_datasets[model] = download.download_and_transform(\n",
" requests=[request | {model_key: model} for request in request_sim[1]],\n",
" **kwargs,\n",
" transform_func_kwargs=transform_func_kwargs,\n",
" )\n",
"\n",
" # Interpolated model\n",
" ds = download.download_and_transform(\n",
" requests=[request | {model_key: model} for request in request_sim[1]],\n",
" **kwargs,\n",
" transform_func_kwargs=transform_func_kwargs\n",
" | {\n",
" \"grid_out\": grid_out,\n",
" \"method\": interpolation_method,\n",
" \"skipna\": True,\n",
" },\n",
" transform_func_kwargs=transform_func_kwargs,\n",
" )\n",
" model_datasets[model] = ds\n",
"\n",
" if model != model_regrid:\n",
" # Interpolated model\n",
" ds = download.download_and_transform(\n",
" requests=[request | {model_key: model} for request in request_sim[1]],\n",
" **kwargs,\n",
" transform_func_kwargs=transform_func_kwargs\n",
" | {\n",
" \"grid_out\": grid_out,\n",
" \"method\": interpolation_method,\n",
" \"skipna\": True,\n",
" },\n",
" )\n",
" interpolated_datasets.append(ds.expand_dims(model=[model]))\n",
"\n",
"ds_interpolated = xr.concat(interpolated_datasets, \"model\")"
Expand Down Expand Up @@ -499,7 +492,7 @@
"}\n",
"\n",
"# Remove boundaries\n",
"isel = {coord: slice(5, -5) for coord in (\"latitude\", \"longitude\")}\n",
"isel = {dim: slice(5, -5) for dim in set(ds.dims) - {\"model\", \"index\"}}\n",
"ds_interpolated = ds_interpolated.isel(isel)\n",
"\n",
"# Edit attributes\n",
Expand Down Expand Up @@ -828,7 +821,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit bc8d152

Please sign in to comment.