Skip to content

Commit 2586a5b

Browse files
committed
merge
2 parents 81a6cea + aec5492 commit 2586a5b

10 files changed

+3396
-3341
lines changed

data/01_download_basins.sh

100644100755
File mode changed.

data/02_make_basin_masks.sh

100644100755
File mode changed.

data/04_itslive2pism.sh

100644100755
File mode changed.

data/05_download_calfin.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
#!/bin/bash
3+
4+
mkdir -p calfin
5+
wget -nc https://datadryad.org/stash/downloads/file_stream/458630
6+
mv 458630 calfin/
7+
cd calfin
8+
unzip 458630
9+
rm 458630
10+
cd ..
104 KB
Binary file not shown.

notebooks/analyze_ensembles_2.ipynb

Lines changed: 2557 additions & 3324 deletions
Large diffs are not rendered by default.

notebooks/analyze_scalars.ipynb

Lines changed: 538 additions & 17 deletions
Large diffs are not rendered by default.

notebooks/create_retreat_mask.ipynb

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "6ba2c915-a0cf-4672-ad58-839841728c22",
6+
"metadata": {},
7+
"source": [
8+
"# Create a retreat mask from basins and Calfin"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "91ea63ac-a54a-4608-a4fa-0046e9bb40d4",
14+
"metadata": {},
15+
"source": [
16+
"Make sure all data sets have been downloaded prior to run this notetook"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "5bdf99df-94cc-40b4-af2a-51fcefb6aca8",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"import geopandas as gp\n",
27+
"gp.options.io_engine = \"pyogrio\"\n",
28+
"import pandas as pd\n",
29+
"from tqdm.auto import tqdm\n",
30+
"import xarray as xr\n",
31+
"from geocube.api.core import make_geocube\n",
32+
"import geocube\n",
33+
"import numpy as np\n",
34+
"from joblib import Parallel, delayed\n",
35+
"from pathlib import Path\n",
36+
"from typing import Union"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"id": "4654d547-0358-4f48-9b16-617f10b28c14",
42+
"metadata": {},
43+
"source": [
44+
"## Prepare the IMBIE basins. Load, make valid geometry"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"id": "2a0005de-da43-4563-8283-131931ec86bf",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"fronts_path = Path(\"../data/fronts\")\n",
55+
"fronts_path.mkdir(parents=True, exist_ok=True)\n",
56+
"intermediate_path = Path(\"../data/intermediate\")\n",
57+
"intermediate_path.mkdir(parents=True, exist_ok=True)\n",
58+
"basins_path = Path(\"../data/basins/\")\n",
59+
"calfin_path = Path(\"../data/calfin\")"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"id": "52f8bca6-a5ef-4457-99c0-ed1586cc01d8",
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"imbie = gp.read_file(basins_path / Path(\"GRE_Basins_IMBIE2_v1.3.shp\")).to_crs(\"EPSG:3413\")\n",
70+
"valid_geometry = imbie.make_valid()\n",
71+
"imbie.geometry = valid_geometry\n",
72+
"imbie = imbie[imbie[\"SUBREGION1\"] != \"ICE_CAP\"].dissolve()\n",
73+
"valid_geometry = imbie.make_valid()\n",
74+
"imbie.geometry = valid_geometry\n",
75+
"imbie_gp = gp.GeoDataFrame(geometry=imbie.geometry, crs=\"EPSG:3413\")\n",
76+
"imbie_gp.to_file(basins_path / Path(\"imbie.gpkg\"))"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"id": "b5ae1805-1f7b-415d-a4ec-e3e8c4b3a17b",
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"shelves = gp.read_file(basins_path / Path(\"GRE_Basins_shelf_extensions.gpkg\")).to_crs(\"EPSG:3413\").dissolve()\n",
87+
"valid_geometry = shelves.make_valid()\n",
88+
"shelves.geometry = valid_geometry\n",
89+
"imbie = imbie.union(shelves)\n",
90+
"imbie_gp = gp.GeoDataFrame(geometry=imbie.geometry, crs=\"EPSG:3413\")\n",
91+
"imbie_gp.to_file(basins_path / Path(\"imbie_shelves.gpkg\"))"
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"id": "a54b0bb2-0030-4a5a-a0c4-f18123e3b5ca",
97+
"metadata": {},
98+
"source": [
99+
"## Prepare the IMBIE basins. Load, make valid, and dissolve"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"id": "c96c8167-bb81-4fa9-bbac-36feac8045d3",
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"calfin = gp.read_file(calfin_path / Path(\"termini_1972-2019_Greenland_closed_v1.0.shp\")).to_crs(\"EPSG:3413\")\n",
110+
"valid_geometry = calfin.make_valid()\n",
111+
"calfin.geometry = valid_geometry"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"id": "881bda8d-fb39-46b6-92fd-19fd4c369d86",
117+
"metadata": {},
118+
"source": [
119+
"## Merge dissolved IMBIE with closed, dissolved Calfin polygons\n",
120+
"\n",
121+
"This creates the outline from which we are going to subtract front positions"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"id": "f1d4117c-e51f-446b-9302-2e8d891e086f",
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"calfin_dissolved = calfin.dissolve()\n",
132+
"calfin_dissolved.to_file(intermediate_path / Path(\"calfin_dissolved.gpkg\"))"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": null,
138+
"id": "44b9712c-e7a7-47c7-a033-28fd160a88dd",
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"union = imbie.union(calfin_dissolved)\n",
143+
"union_gp = gp.GeoDataFrame(geometry=union, crs=\"EPSG:3413\").dissolve()\n",
144+
"union_gp.to_file(intermediate_path / Path(\"union.gpkg\"))"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"id": "d8afb4c3-8599-455f-bf7f-87d410d6f960",
151+
"metadata": {},
152+
"outputs": [],
153+
"source": [
154+
"# Create the geometry / bounding box\n",
155+
"# This was taken from\n",
156+
"# https://gis.stackexchange.com/questions/436243/how-to-properly-use-geocube-geom-parameter-with-crs-other-than-4326\n",
157+
"bbox = (-653000.0, -3384350., 879700.0, -632750.0)\n",
158+
"geom = geocube.geo_utils.geobox.mapping(geocube.geo_utils.geobox.box(*bbox))\n",
159+
"geom[\"crs\"] = {\"properties\": {\"name\": \"EPSG:3413\"}}"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": null,
165+
"id": "7d237f74-0c4e-42d1-848d-07c7ec5c0432",
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
"def create_ds(ds1, ds2, date, geom, crs: str = \"EPSG:3413\", grid: float = 150, out_path: Path = Path(\"../data/fronts\")):\n",
170+
" \n",
171+
" front = ds1.dissolve().buffer(5)\n",
172+
" front.to_file(out_path / Path(f\"g{grid}m_terminus_{date.year}-{date.month}.gpkg\"))\n",
173+
" diff = ds2.difference(front)\n",
174+
" n = len(diff)\n",
175+
" diff_df = {\"land_ice_area_fraction_retreat\": np.ones(n)}\n",
176+
" diff_gp = gp.GeoDataFrame(data=diff_df, geometry=diff, crs=crs)\n",
177+
" diff_gp.to_file(out_path / Path(f\"g{grid}m_frontretreat_{date.year}-{date.month}.gpkg\"))\n",
178+
" ds = make_geocube(vector_data=diff_gp, geom=geom, resolution=[grid, grid]).fillna(0)\n",
179+
" ds = ds.expand_dims(\"time\")\n",
180+
" ds[\"time\"] = (\"time\", [pd.to_datetime(f\"{date.year}-{date.month}-01\")], \n",
181+
" {\n",
182+
" \"_FillValue\": False,\n",
183+
" \"units\": \"days since 1972-01-01\",\n",
184+
" \"calendar\": \"gregorian_proleptic\",\n",
185+
" \"axis\": \"T\",\n",
186+
" \"long_name\": \"time\",\n",
187+
" \"unlimited\": True\n",
188+
" },\n",
189+
" )\n",
190+
" ds[\"time_bounds\"] = ([\"time\", \"bounds\"], [[pd.to_datetime(f\"{date.year}-{date.month}-01\") , \n",
191+
" pd.to_datetime(f\"{date.year}-{date.month+1}-01\")]], \n",
192+
" { \"_FillValue\": False, \"coordinates\": False\n",
193+
" },\n",
194+
" )\n",
195+
" ds[\"time\"].attrs = {\"bounds\": \"time_bounds\"}\n",
196+
" ds[\"land_ice_area_fraction_retreat\"].attrs = {\"units\": \"1\"}\n",
197+
" fn = out_path / Path(f\"g{grid}m_frontretreat_{date.year}-{date.month}.nc\")\n",
198+
" comp = dict(zlib=True, complevel=2)\n",
199+
" encoding = {var: comp for var in ds.data_vars if var != \"time_bounds\"}\n",
200+
" ds.time.encoding = {\"units\": \"days since 1972-01-01\"}\n",
201+
" del ds.time_bounds.attrs[\"coordinates\"]\n",
202+
" ds.time_bounds.encoding = {\"units\": \"days since 1972-01-01\", \"coordinates\": None}\n",
203+
" ds.to_netcdf(fn, encoding=encoding)\n",
204+
" del ds\n",
205+
" return fn"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"id": "a7fbc0b0-dede-423c-91ab-151a63ff78b2",
212+
"metadata": {},
213+
"outputs": [],
214+
"source": [
215+
"grid = 150"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": null,
221+
"id": "9379554a-3ac6-4401-8758-006eae1621e8",
222+
"metadata": {},
223+
"outputs": [],
224+
"source": [
225+
"calfin[\"Date\"] = pd.to_datetime(calfin[\"Date\"])\n",
226+
"calfin_ds = calfin.set_index(\"Date\").groupby(by=pd.Grouper(freq='ME'))\n",
227+
"nt = len(calfin_ds)\n",
228+
"\n",
229+
"crs: str = \"EPSG:3413\"\n",
230+
"\n",
231+
"result = []\n",
232+
"i = 0\n",
233+
"for date, ds in tqdm(calfin_ds):\n",
234+
" if len(ds) > 0:\n",
235+
" if i > 0:\n",
236+
" new_geom = old_ds.dissolve().union(ds.dissolve())\n",
237+
" ds = gp.GeoDataFrame(geometry=new_geom, crs=crs)\n",
238+
" fn = create_ds(ds, imbie, date, geom, grid=grid, out_path=fronts_path)\n",
239+
" result.append(fn)\n",
240+
" old_ds = ds\n",
241+
" i += 1\n"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": null,
247+
"id": "74d4e6ed-0166-4432-9130-71b10bf66e54",
248+
"metadata": {},
249+
"outputs": [],
250+
"source": [
251+
"ds = xr.open_mfdataset(result, parallel=True,)\n",
252+
"comp = dict(zlib=True, complevel=2)\n",
253+
"encoding = {var: comp for var in ds.data_vars if var != \"time_bounds\"}\n",
254+
"ds.time.encoding.update({\"_FillValue\": None})\n",
255+
"ds.time_bounds.encoding.update({\"_FillValue\": None, \"coordinates\": None})\n",
256+
"ds.to_netcdf(fronts_path / Path(f\"pism_g{grid}m_frontretreat_calfin_1972_2019.nc\"), encoding=encoding, unlimited_dims='time')"
257+
]
258+
}
259+
],
260+
"metadata": {
261+
"kernelspec": {
262+
"display_name": "Python 3 (ipykernel)",
263+
"language": "python",
264+
"name": "python3"
265+
},
266+
"language_info": {
267+
"codemirror_mode": {
268+
"name": "ipython",
269+
"version": 3
270+
},
271+
"file_extension": ".py",
272+
"mimetype": "text/x-python",
273+
"name": "python",
274+
"nbconvert_exporter": "python",
275+
"pygments_lexer": "ipython3",
276+
"version": "3.11.6"
277+
}
278+
},
279+
"nbformat": 4,
280+
"nbformat_minor": 5
281+
}

requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ pyDOE2
2121
cftime
2222
nc-time-axis
2323
netCDF4
24+
seaborn
25+
pyarrow
26+
rioxarray
27+
geocube
28+
geopandas
29+
cf-xarray
30+
nco
31+
cdo
32+
gsl

uq/ensemble_gris_ragis_ctrl.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ id,climate,hydrology,frontal_melt,ocean_file,climate_file,runoff_file,gamma_T,sa
22
BAYES-MEDIAN,given_smb,routing,discharge_given,MAR3.9_CNRM-ESM2_ssp585_ocean_1960-2100_v4.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,0.000125,34,0.7508220999999999,2.608046,3.309718,200,,0.50,false,-369.6359,243.82395,7.193718,42.79528,0.01845403,th,
33
CTRL-BAYES-MEDIAN,given_smb,routing,discharge_given,MAR3.9_CNRM-ESM2_ssp585_ocean_1960-2100_v4.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,0.0001,34,0.7508220999999999,2.608046,3.309718,200,,0.50,false,-369.6359,243.82395,7.193718,42.79528,0.01845403,th,
44
BAYES-MEDIAN-FR,given_smb,routing,discharge_given,MAR3.9_CNRM-ESM2_ssp585_ocean_1960-2100_v4.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,0.000125,34,0.7508220999999999,2.608046,3.309718,200,,0.50,false,-369.6359,243.82395,7.193718,42.79528,0.01845403,th,pism_g450m_frontretreat_calfin_1972_2019.nc
5+
BAYES-MEDIAN-FR-OFF,given_smb,diffuse,off,MAR3.9_CNRM-ESM2_ssp585_ocean_1960-2100_v4.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,0.000125,34,0.7508220999999999,2.608046,3.309718,50,,1.0,false,-369.6359,243.82395,7.193718,42.79528,0.01845403,constant,pism_g450m_frontretreat_calfin_1972_2019.nc
56
CTRL-BAYES-MEDIAN-FR,given_smb,routing,discharge_given,MAR3.9_CNRM-ESM2_ssp585_ocean_1960-2100_v4.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,DMI-HIRHAM5_ERA_1980_2020_EPSG3413_4500M_MM.nc,0.0001,34,0.7508220999999999,2.608046,3.309718,200,,0.50,false,-369.6359,243.82395,7.193718,42.79528,0.01845403,th,pism_g450m_frontretreat_calfin_1980_2019.nc

0 commit comments

Comments
 (0)