-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIHA_Calculator_v2.sh
400 lines (328 loc) · 13.6 KB
/
IHA_Calculator_v2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/sh
echo "This script was developed by the Deparment of Physical Geography of Utrecht University, the Netherlands"
dis1="$1" # Discharge pre-alterations input
dis2="$2" # Discharge post-alterations input
# IDEA: Could add output folder to save all files directly in the right location, but would require a lot of editing.
# Start up
echo ""
echo "Starting the calculator for Indicators of Hydrologic Alterations (Martinez Santa-Maria et al., 2008)."
echo "Using $1 as pre-alterations database, and $2 as post-alterations database."
echo "For a description of each of the indicators see: Quantifying sustainable water resources in the Danube catchment by implementing environmental flow requirements in the PCR-GLOBWB model by Hosper & van Beek, 2022."
echo ""
# load CDO module
module load CDO
# Enable or disable equal distribution of parameters
# To-Do: Add this functionality to the corresponding IAHs (IAH1b, IAH3, IAH12, IAH18, IAH19)
echo "There are a total of 15 parameters in this version of the IHA Calculator."
echo "Three of these have monthly values that could be given their own weight in the final calculation."
echo "This would mean that in total there would be 59 parameters (4 par x 12 months + 11 par)."
echo "The monthly parameters would then contribute to 80%+ of the final average."
read -p "Should monthly parameters be 12 different IAH values (y/n): " multipar
case "$multipar" in
Y|y|N|n)
;;
*)
echo "Wrong input, use y or n."
exit;;
esac
# Ask for information
read -p "Environmental starting year: " ESY
read -p "Environmental end year: " EEY
read -p "Altered starting year: " ASY
read -p "Altered end year: " AEY
envs=$ESY
alts=$ASY
enve=$EEY
alte=$AEY
# Create map of ones, needed for IAH ratio calculation
echo ""
echo "Creating maps required for the calculations of the Indicators."
cdo -L -timavg -ifthenc,1 -eq $dis1 $dis1 tmp_1.nc
# Calculate min & max for yearly and daily
cdo -L -selyear,$envs/$enve -yearmax $dis1 tmp_max1.nc
cdo -L -selyear,$envs/$enve -yearmin $dis1 tmp_min1.nc
cdo -L -selyear,$alts/$alte -yearmax $dis2 tmp_max2.nc
cdo -L -selyear,$alts/$alte -yearmin $dis2 tmp_min2.nc
# Habitual Data
echo ""
echo "Starting Calculation: The Habitual Data Indicators"
echo ""
echo "Calculation: IAH1"
# IAH 1: Mean Annual Volume
cdo -L -selyear,$envs/$enve -yearavg $dis1 tmp_ya1.nc
cdo -L -selyear,$alts/$alte -yearavg $dis2 tmp_ya2.nc
# IAH 1 Ratio
cdo -L -div -timavg tmp_ya2.nc -timavg tmp_ya1.nc tmp_IAH1.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH1.nc -div tmp_1.nc tmp_IAH1.nc tmp_IAH1.nc IAH1.nc
echo "Completed: IAH1"
echo ""
echo "Calculation: IAH1b"
# IAH 1b: Monthly Mean Volume
cdo -L -ymonavg -selyear,$envs/$enve $dis1 tmp_ma1.nc
cdo -L -ymonavg -selyear,$alts/$alte $dis2 tmp_ma2.nc
# IAH 1b Ratio
if [[ $multipar =~ ^[Yy]$ ]]; then
cdo -L -div tmp_ma2.nc tmp_ma1.nc tmp_IAH1b.nc
elif [[ $multipar =~ ^[Nn]$ ]]; then
cdo -L -timavg -div tmp_ma2.nc tmp_ma1.nc tmp_IAH1b.nc
else
echo "Wrong input, next time choose y or n. Average taken as default."
cdo -L -timavg -div tmp_ma2.nc tmp_ma1.nc tmp_IAH1b.nc
fi
cdo -L -ifthenelse -gtc,1 tmp_IAH1b.nc -div tmp_1.nc tmp_IAH1b.nc tmp_IAH1b.nc IAH1b.nc
echo "Completed: IAH1b"
echo ""
echo "Calculation: IAH2"
# IAH 2: Difference between Annual Max & Annual Min
cdo -L -timavg -sub tmp_max1.nc tmp_min1.nc tmp_diff1.nc # First calculate the difference between max and min, then find the average.
cdo -L -timavg -sub tmp_max2.nc tmp_min2.nc tmp_diff2.nc
# IAH 2 Ratio
cdo div tmp_diff2.nc tmp_diff1.nc tmp_IAH2.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH2.nc -div tmp_1.nc tmp_IAH2.nc tmp_IAH2.nc IAH2.nc
echo "Completed: IAH2"
# IAH 3: Month with min and max water volume
# I can find when the monthly maximums are (which month) but I can only show these dates, not calculate anything with them...
# The calculation for this parameter is 1 - 1/6*(change in months)
echo ""
echo "IAH3 is missing for now."
#~ cdo ymonmax $dis1 tmp_mmax1.nc
#~ cdo ymonmax $dis2 tmp_mmax2.nc
#~ cdo ymonmin $dis1 tmp_mmin1.nc
#~ cdo ymonmin n$dis2 tmp_mmin2.nc
# Can maybe use a python script with netCDF4 to calculate the change of months
echo ""
echo "Calculation: IAH4"
# IAH 4: Difference between daily 10% and daily 90%
cdo -L -yearpctl,10 -selyear,$envs/$enve $dis1 tmp_min1.nc tmp_max1.nc tmp_1d10.nc
cdo -L -yearpctl,90 -selyear,$envs/$enve $dis1 tmp_min1.nc tmp_max1.nc tmp_1d90.nc
cdo sub tmp_1d90.nc tmp_1d10.nc tmp_ddiff1.nc
cdo timavg tmp_ddiff1.nc tmp_ddiffavg1.nc
cdo -L -yearpctl,10 -selyear,$alts/$alte $dis2 tmp_min2.nc tmp_max2.nc tmp_2d10.nc
cdo -L -yearpctl,90 -selyear,$alts/$alte $dis2 tmp_min2.nc tmp_max2.nc tmp_2d90.nc
cdo sub tmp_2d90.nc tmp_2d10.nc tmp_ddiff2.nc
cdo timavg tmp_ddiff2.nc tmp_ddiffavg2.nc
# IAH 4 Ratio
cdo div tmp_ddiffavg2.nc tmp_ddiffavg1.nc tmp_IAH4.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH4.nc -div tmp_1.nc tmp_IAH4.nc tmp_IAH4.nc IAH4.nc
echo "Completed: IAH4"
echo ""
echo "Calculating: Extreme Data Floods"
echo ""
echo "Calculation: IAH5"
# Extreme Data
# IAH 5: Average of maximum daily flows (Qc)
# IAH 5 Ratio
cdo -L -div -timavg tmp_max2.nc -timavg tmp_max1.nc tmp_IAH5.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH5.nc -div tmp_1.nc tmp_IAH5.nc tmp_IAH5.nc IAH5.nc
echo "Completed: IAH5"
# IAH 6: Effective discharge (??) -> Still no clue after reading Perez-Sanchez et al. (2020)
echo ""
echo "IAH6 is missing for now."
# IAH 7: Connectivity discharge (Qconec) -> Still no clue after reading Perez-Sanchez et al. (2020)
echo ""
echo "IAH7 is missing for now."
echo ""
echo "Calculation: IAH8"
# IAH 8: Q5% exceedance probability
cdo -L -yearpctl,95 -selyear,$envs/$enve $dis1 tmp_min1.nc tmp_max1.nc tmp_1y95.nc
cdo timavg tmp_1y95.nc tmp_1ya95.nc
cdo -L -yearpctl,95 -selyear,$alts/$alte $dis2 tmp_min2.nc tmp_max2.nc tmp_2y95.nc
cdo timavg tmp_2y95.nc tmp_2ya95.nc
# IAH 8 Ratio
cdo div tmp_2ya95.nc tmp_1ya95.nc tmp_IAH8.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH8.nc -div tmp_1.nc tmp_IAH8.nc tmp_IAH8.nc IAH8.nc
echo "Completed: IAH8"
echo ""
echo "Calculation: IAH9"
# IAH 9: CV Qc
cdo -L -mulc,100 -div -timstd tmp_max1.nc -timmean tmp_max1.nc tmp_cvMax1.nc
cdo -L -mulc,100 -div -timstd tmp_max2.nc -timmean tmp_max2.nc tmp_cvMax2.nc
# IAH 9 Ratio
cdo div tmp_cvMax2.nc tmp_cvMax1.nc tmp_IAH9.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH9.nc -div tmp_1.nc tmp_IAH9.nc tmp_IAH9.nc IAH9.nc
echo "Completed: IAH9"
echo ""
echo "Calculation: IAH10"
# IAH 10: CV Q5%
cdo -L -mulc,100 -div -timstd tmp_1y95.nc -timmean tmp_1y95.nc tmp_1cv95.nc
cdo -L -mulc,100 -div -timstd tmp_2y95.nc -timmean tmp_2y95.nc tmp_2cv95.nc
# IAH 10 Ratio
cdo div tmp_2cv95.nc tmp_1cv95.nc tmp_IAH10.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH10.nc -div tmp_1.nc tmp_IAH10.nc tmp_IAH10.nc IAH10.nc
echo "Completed: IAH10"
# IAH 11: Flood duration = consecutive days Q > Q5%
echo ""
echo "IAH 11 is missing for now"
# IAH 12: Flood seasonality = (5 - ABS(NATURAL - ALTERED) / 5)
# With 12 different values, one for each month, and NATURAL and ALTERED being the average days per month where Q > Q95%
echo ""
echo "Calculation: IAH 12"
echo "Calculating the Environmental Regime"
tmp_envs=$envs
while [ $tmp_envs -le $enve ]; do
echo ""
echo "Calculating year: $tmp_envs"
cdo -L -gt -selyear,$tmp_envs $dis1 -timavg -selyear,$tmp_envs tmp_1y95.nc tmp_env95$tmp_envs.nc
let "tmp_envs = $tmp_envs + 1"
done
cdo mergetime tmp_env95* tmp_env_final.nc
tmp_alts=$alts
echo "Calculating the Altered Regime"
while [ $tmp_alts -le $alte ]; do
echo ""
echo "Calculating year: $tmp_alts"
cdo -L -gt -selyear,$tmp_alts $dis2 -timavg -selyear,$tmp_alts tmp_2y95.nc tmp_alt95$tmp_alts.nc
let "tmp_alts = $tmp_alts + 1"
done
cdo mergetime tmp_alt95* tmp_alt_final.nc
# IAH 12 Ratio
cdo -L -sub tmp_1.nc -mulc,0.2 -abs -sub -ymonavg -monsum tmp_alt_final.nc -ymonavg -monsum tmp_env_final.nc tmp_IAH12.nc
if [[ $multipar =~ ^[Yy]$ ]]; then
cdo -L -setrtoc,-inf,0,0 tmp_IAH12.nc IAH12.nc
elif [[ $multipar =~ ^[Nn]$ ]]; then
cdo -L -timavg -setrtoc,-inf,0,0 tmp_IAH12.nc IAH12.nc
else
echo "Average taken as default."
cdo -L -timavg -setrtoc,-inf,0,0 tmp_IAH12.nc IAH12.nc
fi
# Remove excess files
rm tmp_alt*
rm tmp_env*
echo ""
echo "Completed: Extreme Data Floods"
echo ""
echo "Starting Calculation: Extreme Data Drought"
echo ""
echo "Calculation: IAH13"
# Extreme Data
# IAH 13: Average of minimum daily flows (Qs)
# IAH 13 Ratio
cdo -L -div -timavg tmp_min2.nc -timavg tmp_min1.nc tmp_IAH13.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH13.nc -div tmp_1.nc tmp_IAH13.nc tmp_IAH13.nc IAH13.nc
echo "Completed: IAH13"
echo ""
echo "Calculation: IAH14"
# IAH 14: Q95% exceedance probability
cdo -L -yearpctl,5 -selyear,$envs/$enve $dis1 tmp_min1.nc tmp_max1.nc tmp_1y05.nc
cdo timavg tmp_1y05.nc tmp_1ya05.nc
cdo -L -yearpctl,5 -selyear,$alts/$alte $dis2 tmp_min2.nc tmp_max2.nc tmp_2y05.nc
cdo timavg tmp_2y05.nc tmp_2ya05.nc
# IAH 14 Ratio
cdo div tmp_2ya05.nc tmp_1ya05.nc tmp_IAH14.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH14.nc -div tmp_1.nc tmp_IAH14.nc tmp_IAH14.nc IAH14.nc
echo "Completed: IAH14"
echo ""
echo "Calculation: IAH15"
# IAH 15: CV Qs
cdo -L -mulc,100 -div -timstd tmp_min1.nc -timmean tmp_min1.nc tmp_cvMin1.nc
cdo -L -mulc,100 -div -timstd tmp_min2.nc -timmean tmp_min2.nc tmp_cvMin2.nc
# IAH 15 Ratio
cdo div tmp_cvMin2.nc tmp_cvMin1.nc tmp_IAH15.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH15.nc -div tmp_1.nc tmp_IAH15.nc tmp_IAH15.nc IAH15.nc
echo "Completed: IAH15"
echo ""
echo "Calculation: IAH16"
# IAH 16: CV Q95%
cdo -L -mulc,100 -div -timstd tmp_1y05.nc -timmean tmp_1y05.nc tmp_1cv05.nc
cdo -L -mulc,100 -div -timstd tmp_2y05.nc -timmean tmp_2y05.nc tmp_2cv05.nc
# IAH 16 Ratio
cdo div tmp_2cv05.nc tmp_1cv05.nc tmp_IAH16.nc
cdo -L -ifthenelse -gtc,1 tmp_IAH16.nc -div tmp_1.nc tmp_IAH16.nc tmp_IAH16.nc IAH16.nc
echo "Completed: IAH16"
# IAH 17: Maximum consecutive days Q < Q95% in regime on average
echo ""
echo "Calculation: IAH 18"
# IAH 18: Days per month no flow - Q = 0
# IAH 18: Days per month no flow - Q = 0
cdo -L -selyear,$envs/$enve -eqc,0 $dis1 tmp_1days0.nc
cdo -L -ymonavg -monsum tmp_1days0.nc tmp_1avgdays0.nc
cdo -L -selyear,$alts/$alte -eqc,0 $dis2 tmp_2days0.nc
cdo -L -ymonavg -monsum tmp_2days0.nc tmp_2avgdays0.nc
# IAH 18 Ratio
cdo -L -sub tmp_1.nc -mulc,0.2 -abs -sub -ymonsum tmp_2avgdays0.nc -ymonsum tmp_1avgdays0.nc tmp_IAH18.nc
if [[ $multipar =~ ^[Yy]$ ]]; then
cdo -L -setrtoc,-inf,0,0 tmp_IAH18.nc IAH18.nc
elif [[ $multipar =~ ^[Nn]$ ]]; then
cdo -L -timavg -setrtoc,-inf,0,0 tmp_IAH18.nc IAH18.nc
else
echo "Average taken as default."
cdo -L -timavg -setrtoc,-inf,0,0 tmp_IAH18.nc IAH18.nc
fi
echo ""
echo "Calculation: IAH 19"
# IAH 19: Days per month flow Q < Q95%
tmp_envs=$envs
while [ $tmp_envs -le $enve ]; do
echo ""
echo "Calculating year: $tmp_envs"
cdo -L -lt -selyear,$tmp_envs $dis1 -timavg -selyear,$tmp_envs tmp_1y05.nc tmp_env05$tmp_envs.nc
let "tmp_envs = $tmp_envs + 1"
done
cdo mergetime tmp_env05* tmp_env_final.nc
tmp_alts=$alts
echo "Calculating the Altered Regime"
while [ $tmp_alts -le $alte ]; do
echo ""
echo "Calculating year: $tmp_alts"
cdo -L -lt -selyear,$tmp_alts $dis2 -timavg -selyear,$tmp_alts tmp_2y05.nc tmp_alt05$tmp_alts.nc
let "tmp_alts = $tmp_alts + 1"
done
cdo mergetime tmp_alt05* tmp_alt_final.nc
# IAH 19 Ratio
cdo -L -sub tmp_1.nc -mulc,0.2 -abs -sub -ymonavg -monsum tmp_alt_final.nc -ymonavg -monsum tmp_env_final.nc tmp_IAH19.nc
if [[ $multipar =~ ^[Yy]$ ]]; then
cdo -L -setrtoc,-inf,0,0 tmp_IAH19.nc IAH19.nc
elif [[ $multipar =~ ^[Nn]$ ]]; then
cdo -L -timavg -setrtoc,-inf,0,0 tmp_IAH19.nc IAH19.nc
else
echo "Average taken as default."
cdo -L -timavg -setrtoc,-inf,0,0 tmp_IAH19.nc IAH19.nc
fi
# Remove excess files
rm tmp_alt*
rm tmp_env*
echo ""
read -p "Lower limit discharge for the average IAH output in m3/s: " variable1
echo "Creating a mask for all flows that have an extreme drought value above $variable1 m3/s."
# Create mask for rivers
cdo -L -ifthen -gtc,$variable1 -timavg $dis1 tmp_1.nc river_mask.nc
echo ""
echo "Calculating the average alteration factor."
# Average all IAHs
# Check if IAH Averages already exist, if so, deleted them.
if test -f IAH_*; then
rm IAH_*
fi
cdo -L -timavg -ensavg IAH* IndicatorsTotalAverage.nc
cdo -L -timavg -ifthen river_mask.nc -ensavg IAH* IAH_average.nc
# Habitual, Flood and Drought for in-depth analysis
echo "Calculating Habitual, Flood, and Drought averages"
# Habitual
#~ cdo -L -timavg -ensavg IAH1.nc IAH1b.nc IAH2.nc IAH4.nc IndicatorsHabitualTotalAverage.nc
cdo -L -timavg -ifthen river_mask.nc -ensavg IAH1.nc IAH1b.nc IAH2.nc IAH4.nc IAH_HabitualAverage.nc
# Flood
#~ cdo -L -timavg -ensavg IAH5.nc IAH8.nc IAH9.nc IAH10.nc IAH12.nc IndicatorsFloodTotalAverage.nc
cdo -L -timavg -ifthen river_mask.nc -ensavg IAH5.nc IAH8.nc IAH9.nc IAH10.nc IAH12.nc IAH_FloodAverage.nc
# Drought
#~ cdo -L -timavg -ensavg IAH13.nc IAH14.nc IAH15.nc IAH16.nc IAH18.nc IAH19.nc IndicatorsDroughtTotalAverage.nc
cdo -L -timavg -ifthen river_mask.nc -ensavg IAH13.nc IAH14.nc IAH15.nc IAH16.nc IAH18.nc IAH19.nct IAH_DroughtAverage.nc
echo ""
echo "Indicators of Hydrologic Alteration completed"
# Remove all temporal files.
read -p "Remove all temporary files created for calculations (y/n):" variable2
if [ $variable2 == "y" ]; then
rm tmp*
echo "removed temporary files."
elif [ $variable2 == "n" ]; then
echo "Temporary files not removed."
else
echo "False input, temporary files not removed; use: rm tmp* if you would like to remove all temporary files created"
fi
cdo chname,discharge,IAH IndicatorsTotalAverage.nc tmp_description_IndicatorsTotalAverage.nc
cdo chunit,m3.s-1,"(-)" tmp_description_IndicatorsTotalAverage.nc IndicatorsTotalAverage.nc
# TO-DO: Change all descriptions and units for the IAHs
for IAH in IAH*; do
cdo chname,discharge,IAH $IAH tmp_description_$IAH
cdo chunit,m3.s-1,"(-)" tmp_description_$IAH $IAH
rm tmp_description_$IAH
done