Skip to content

Commit 69540b9

Browse files
1096 clean up code and examples follow up (#1097)
* made test local * moved hydrolib examples from scripts to notebook * moved hisfile examples from script to notebooks * restructured tests
1 parent 018325b commit 69540b9

12 files changed

+4444
-825
lines changed

docs/notebooks/postprocessing_example.ipynb

Lines changed: 3781 additions & 160 deletions
Large diffs are not rendered by default.

docs/notebooks/postprocessing_example_delft3d4.ipynb

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"metadata": {},
77
"source": [
88
"# Post-processing example Delft3D4\n",
9+
"To get Delft3D4 to write netCDF output instead of .dat files, add these lines to your model settings file (.mdf):\n",
10+
"- `FlNcdf= #maphis#`\n",
11+
"- `ncFormat=4`\n",
12+
"\n",
913
"If you prefer to interact with a Python script instead, you can convert this notebook to *.py with `jupyter nbconvert --to script postprocessing_example_delft3d4.ipynb`.\n"
1014
]
1115
},
@@ -16,14 +20,11 @@
1620
"metadata": {},
1721
"outputs": [],
1822
"source": [
19-
"# for Delft3D FM examples, https://github.com/Deltares/dfm_tools/blob/main/docs/notebooks/postprocessing_example.ipynb\n",
20-
"# to get Delft3D4 to write netCDF output instead of .dat files, add these lines to your mdf:\n",
21-
"# FlNcdf= #maphis#\n",
22-
"# ncFormat=4\n",
23-
"\n",
23+
"# imports\n",
2424
"import matplotlib.pyplot as plt\n",
2525
"plt.close('all')\n",
26-
"import dfm_tools as dfmt\n"
26+
"import dfm_tools as dfmt\n",
27+
"import xarray as xr\n"
2728
]
2829
},
2930
{
@@ -38,50 +39,97 @@
3839
"model = 'westernscheldt' # 'kivu', 'curvedbend', 'westernscheldt'\n",
3940
"\n",
4041
"if model == 'curvedbend':\n",
41-
" file_nc = dfmt.data.d3d_curvedbend_trim(return_filepath=True)\n",
42+
" file_map = dfmt.data.d3d_curvedbend_trim(return_filepath=True)\n",
43+
" file_his = dfmt.data.d3d_curvedbend_trih(return_filepath=True)\n",
4244
" timestep = 4\n",
4345
" layno = -1\n",
4446
" res = 100\n",
4547
" scale = 25\n",
4648
" figsize = (6,5)\n",
4749
" add_coastlines = False\n",
50+
" stations_requested = ['Outer-south', 'inner-south', 'inner-middle']\n",
4851
"elif model == 'westernscheldt':\n",
49-
" file_nc = dfmt.data.d3d_westernscheldt_trim(return_filepath=True)\n",
52+
" file_map = dfmt.data.d3d_westernscheldt_trim(return_filepath=True)\n",
53+
" file_his = None\n",
5054
" timestep = 10\n",
5155
" layno = 0\n",
5256
" res = 1/70\n",
5357
" scale = 30\n",
5458
" figsize = (10,4)\n",
5559
" add_coastlines = True\n",
60+
" stations_requested = []\n",
5661
"elif model == 'kivu':\n",
57-
" file_nc = r'p:\\archivedprojects\\1220688-lake-kivu\\3_modelling\\1_FLOW\\7_heatfluxinhis\\062_netcdf\\trim-thiery_002_coarse.nc'\n",
62+
" file_map = r'p:\\archivedprojects\\1220688-lake-kivu\\3_modelling\\1_FLOW\\7_heatfluxinhis\\062_netcdf\\trim-thiery_002_coarse.nc'\n",
63+
" file_his = r'p:\\archivedprojects\\1220688-lake-kivu\\3_modelling\\1_FLOW\\7_heatfluxinhis\\063_netcdf\\trih-thiery_002_coarse.nc'\n",
5864
" timestep = 10\n",
5965
" layno = -2\n",
6066
" res = 1/70\n",
6167
" scale = 3\n",
6268
" figsize = (6,7)\n",
6369
" add_coastlines = True\n",
70+
" stations_requested = ['ADCP1_final','ADCP2_final','KP1_016']\n",
6471
"else:\n",
6572
" raise Exception(f'undefined model: {model}')\n"
6673
]
6774
},
6875
{
6976
"cell_type": "code",
7077
"execution_count": 3,
78+
"id": "163bf92c-4905-4091-9685-ece1773997a7",
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"# open Delft3D4 netcdf his dataset\n",
83+
"if file_his:\n",
84+
" data_xr = xr.open_mfdataset(file_his, preprocess=dfmt.preprocess_hisnc)\n",
85+
" vars_pd = dfmt.get_ncvarproperties(data_xr)\n"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 4,
91+
"id": "b481b84c-90ce-47a9-b2aa-cae9a10917c0",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"# plot waterlevel from hisfile\n",
96+
"if file_his:\n",
97+
" da_zws = data_xr.ZWL.sel(NOSTAT=stations_requested)\n",
98+
" fig, ax = plt.subplots(figsize=(10,6))\n",
99+
" da_zws.plot.line('-', ax=ax, x='time')\n"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 5,
105+
"id": "7916acf2-1421-44e3-847f-c8b0ab85386f",
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"# plot u velocity from hisfile\n",
110+
"if file_his:\n",
111+
" da_dps = data_xr.ZCURU.sel(NOSTAT=stations_requested).isel(KMAXOUT_RESTR=0)\n",
112+
" fig, ax = plt.subplots(figsize=(10,6))\n",
113+
" da_dps.plot.line('-', ax=ax, x='time')\n"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 6,
71119
"id": "1a63798c-55ce-4e0b-8964-184986816d97",
72120
"metadata": {},
73121
"outputs": [
74122
{
75123
"name": "stdout",
76124
"output_type": "stream",
77125
"text": [
78-
">> rasterizing ugrid Dataset with 12 face variables to shape=(20,70): 0.50 sec\n"
126+
">> rasterizing ugrid Dataset with 12 face variables to shape=(20,70): 0.45 sec\n"
79127
]
80128
}
81129
],
82130
"source": [
83-
"# open delft3d4 netcdf dataset\n",
84-
"uds = dfmt.open_dataset_delft3d4(file_nc)\n",
131+
"# open Delft3D4 netcdf map dataset\n",
132+
"uds = dfmt.open_dataset_delft3d4(file_map)\n",
85133
"# select timestep\n",
86134
"uds_sel = uds.isel(time=timestep,KMAXOUT_RESTR=layno)\n",
87135
"# rasterize (for vector plots)\n",
@@ -90,15 +138,15 @@
90138
},
91139
{
92140
"cell_type": "code",
93-
"execution_count": 4,
141+
"execution_count": 7,
94142
"id": "fdf5cfcb-67a1-4e00-873c-c12206571106",
95143
"metadata": {},
96144
"outputs": [
97145
{
98146
"name": "stdout",
99147
"output_type": "stream",
100148
"text": [
101-
">> reading coastlines: 1.37 sec\n"
149+
">> reading coastlines: 1.31 sec\n"
102150
]
103151
},
104152
{
@@ -122,15 +170,15 @@
122170
},
123171
{
124172
"cell_type": "code",
125-
"execution_count": 5,
173+
"execution_count": 8,
126174
"id": "0c3a7d7d-2c06-4b3d-97b2-3a2cf53e30e0",
127175
"metadata": {},
128176
"outputs": [
129177
{
130178
"name": "stdout",
131179
"output_type": "stream",
132180
"text": [
133-
">> reading coastlines: 0.73 sec\n"
181+
">> reading coastlines: 0.68 sec\n"
134182
]
135183
},
136184
{
@@ -154,15 +202,15 @@
154202
},
155203
{
156204
"cell_type": "code",
157-
"execution_count": 6,
205+
"execution_count": 9,
158206
"id": "eccce136-29aa-4864-92f3-f20dfd92b82c",
159207
"metadata": {},
160208
"outputs": [
161209
{
162210
"name": "stdout",
163211
"output_type": "stream",
164212
"text": [
165-
">> reading coastlines: 1.31 sec\n"
213+
">> reading coastlines: 0.73 sec\n"
166214
]
167215
},
168216
{
@@ -188,15 +236,15 @@
188236
},
189237
{
190238
"cell_type": "code",
191-
"execution_count": 7,
239+
"execution_count": 10,
192240
"id": "ca644dd6-1e8a-4fb4-93c9-a9407a95a778",
193241
"metadata": {},
194242
"outputs": [
195243
{
196244
"name": "stdout",
197245
"output_type": "stream",
198246
"text": [
199-
">> reading coastlines: 1.30 sec\n"
247+
">> reading coastlines: 0.70 sec\n"
200248
]
201249
},
202250
{

docs/notebooks/preprocessing_example_hydrolib.ipynb

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

tests/examples/postprocess_hisnc.py

Lines changed: 0 additions & 133 deletions
This file was deleted.

tests/examples/preprocess_hydrolib_readtim.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/examples/preprocess_hydrolib_readxyz_readxyn_readcrs.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)