-
Notifications
You must be signed in to change notification settings - Fork 29
/
fim_post_processing.sh
executable file
·292 lines (248 loc) · 9.28 KB
/
fim_post_processing.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
#!/bin/bash -e
:
usage()
{
echo "
Post processing for creating FIM hydrofabric.
Usage : fim_post_processing.sh [REQ: -n <run name> ] [OPT: -h -j <job limit>]
REQUIRED:
-n/--runName : A name to tag the output directories and log files.
OPTIONS:
-h/--help : help file
-j/--jobLimit : max number of concurrent jobs to run. Default 1 job at time. 1 outputs
stdout and stderr to terminal and logs. With >1 outputs progress and logs the rest.
Note: Not the same variable name as fim_pipeline or fim_pre_processing
and can be the multiplication of jobHucLimit and jobBranchLimit
"
exit
}
while [ "$1" != "" ]; do
case $1
in
-n|--runName)
shift
runName=$1
;;
-j|--jobLimit)
shift
jobLimit=$1
;;
-h|--help)
shift
usage
;;
*) ;;
esac
shift
done
# print usage if arguments empty
if [ "$runName" = "" ]
then
echo "ERROR: Missing -n run time name argument"
usage
exit 22
fi
outputDestDir=$outputsDir/$runName
## Check for output destination directory ##
if [ ! -d "$outputDestDir" ]; then
echo "Depends on output from units and branches. "
echo "Please provide an output folder name that has hucs/branches run."
exit 1
fi
#########################################################################################
# #
# PLEASE DO NOT USE the job limits coming in from the runtime_args.env #
# Most of the time, post processing will not be run on the same servers #
# that is running fim_process_unit_wb.sh and the processing power #
# used to run fim_post_processing.sh will be different (hence.. different job limit) #
# #
#########################################################################################
if [ "$jobLimit" = "" ]; then jobLimit=1; fi
# Clean out the other post processing files before starting
rm -rdf $outputDestDir/logs/src_optimization
rm -f $outputDestDir/logs/log_bankfull_indentify.log
rm -f $outputDestDir/logs/subdiv_src_.log
rm -f $log_file_name
# load up enviromental information
args_file=$outputDestDir/runtime_args.env
fim_inputs=$outputDestDir/fim_inputs.csv
source $args_file
source $outputDestDir/params.env
source $srcDir/bash_functions.env
source $srcDir/bash_variables.env
# Tell the system the name and location of the post processing log
log_file_name=$outputDestDir/post_proc.log
Set_log_file_path $log_file_name
l_echo ""
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
l_echo "---- Start of fim_post_processing"
l_echo "---- Started: `date -u`"
echo ""
T_total_start
post_proc_start_time=`date +%s`
## RUN UPDATE HYDROTABLE AND SRC ##
# Define the counter file
Tstart
COUNTER_FILE="${outputDestDir}/post_processing_attempt.txt"
# Function to clean up
cleanup() {
if [ "$SUCCESS" = true ]; then
if [ -f "$COUNTER_FILE" ]; then
COUNTER=$(cat "$COUNTER_FILE")
if [ "$COUNTER" -eq 1 ]; then
l_echo "Counter is 1. Removing the counter file."
rm "$COUNTER_FILE"
fi
fi
fi
}
# Set up trap to call cleanup on EXIT, ERR, and INT (interrupt signal)
trap cleanup EXIT ERR INT
# Initialize the counter file if it doesn't exist
if [ ! -f "$COUNTER_FILE" ]; then
echo 0 > "$COUNTER_FILE"
fi
# Read the current counter value
COUNTER=$(cat "$COUNTER_FILE")
# Increment the counter
COUNTER=$((COUNTER + 1))
# Save the new counter value
l_echo "$COUNTER" > "$COUNTER_FILE"
# Check if the counter is greater than one
if [ "$COUNTER" -gt 1 ]; then
# Execute the Python file
l_echo "Updating hydroTable & scr_full_crosswalked for branches"
python3 $srcDir/update_htable_src.py -d $outputDestDir
Tcount
else
l_echo "Execution count is $COUNTER, not executing the update_htable_src.py file."
fi
## AGGREGATE BRANCH LISTS INTO ONE ##
l_echo $startDiv"Start branch aggregation"
Tstart
python3 $srcDir/aggregate_branch_lists.py -d $outputDestDir -f "branch_ids.csv" -o $fim_inputs
Tcount
## GET NON ZERO EXIT CODES FOR BRANCHES ##
l_echo $startDiv"Start non-zero exit code checking"
find $outputDestDir/logs/branch -name "*_branch_*.log" -type f | \
xargs grep -E "Exit status: ([1-9][0-9]{0,2})" > \
"$outputDestDir/branch_errors/non_zero_exit_codes.log" &
## RUN AGGREGATE BRANCH ELEV TABLES ##
l_echo $startDiv"Processing usgs & ras2fim elev table aggregation"
Tstart
python3 $srcDir/aggregate_by_huc.py -fim $outputDestDir -i $fim_inputs -elev -ras -j $jobLimit
Tcount
## RUN BATHYMETRY ADJUSTMENT ROUTINE ##
if [ "$bathymetry_adjust" = "True" ]; then
l_echo $startDiv"Performing Bathymetry Adjustment routine"
Tstart
# Run bathymetry adjustment routine
python3 $srcDir/bathymetric_adjustment.py \
-fim_dir $outputDestDir \
-bathy $bathymetry_file \
-buffer $wbd_buffer \
-wbd $inputsDir/wbd/WBD_National_EPSG_5070_WBDHU8_clip_dem_domain.gpkg \
-j $jobLimit
Tcount
fi
## RUN SYNTHETIC RATING CURVE BANKFULL ESTIMATION ROUTINE ##
if [ "$src_bankfull_toggle" = "True" ]; then
l_echo $startDiv"Estimating bankfull stage in SRCs"
Tstart
# Run SRC bankfull estimation routine routine
python3 $srcDir/identify_src_bankfull.py \
-fim_dir $outputDestDir \
-flows $bankfull_flows_file \
-j $jobLimit
Tcount
fi
## RUN SYNTHETIC RATING SUBDIVISION ROUTINE ##
if [ "$src_subdiv_toggle" = "True" ] && [ "$src_bankfull_toggle" = "True" ]; then
l_echo $startDiv"Performing SRC channel/overbank subdivision routine"
# Run SRC Subdivision & Variable Roughness routine
Tstart
python3 $srcDir/subdiv_chan_obank_src.py \
-fim_dir $outputDestDir \
-mann $vmann_input_file \
-j $jobLimit
Tcount
fi
## RUN SYNTHETIC RATING CURVE CALIBRATION W/ USGS GAGE RATING CURVES ##
if [ "$src_adjust_usgs" = "True" ] && [ "$src_subdiv_toggle" = "True" ] && [ "$skipcal" = "0" ]; then
Tstart
l_echo $startDiv"Performing SRC adjustments using USGS rating curve database"
# Run SRC Optimization routine using USGS rating curve data (WSE and flow @ NWM recur flow values)
python3 $srcDir/src_adjust_usgs_rating_trace.py \
-run_dir $outputDestDir \
-usgs_rc $usgs_rating_curve_csv \
-nwm_recur $nwm_recur_file \
-j $jobLimit
Tcount
fi
## RUN SYNTHETIC RATING CURVE CALIBRATION W/ RAS2FIM CROSS SECTION RATING CURVES ##
if [ "$src_adjust_ras2fim" = "True" ] && [ "$src_subdiv_toggle" = "True" ] && [ "$skipcal" = "0" ]; then
Tstart
l_echo $startDiv"Performing SRC adjustments using ras2fim rating curve database"
# Run SRC Optimization routine using ras2fim rating curve data (WSE and flow @ NWM recur flow values)
python3 $srcDir/src_adjust_ras2fim_rating.py \
-run_dir $outputDestDir \
-ras_input $ras2fim_input_dir \
-ras_rc $ras_rating_curve_csv_filename \
-nwm_recur $nwm_recur_file \
-j $jobLimit
Tcount
fi
## RUN SYNTHETIC RATING CURVE CALIBRATION W/ BENCHMARK POINTS (.parquet files) ##
if [ "$src_adjust_spatial" = "True" ] && [ "$src_subdiv_toggle" = "True" ] && [ "$skipcal" = "0" ]; then
Tstart
l_echo $startDiv"Performing SRC adjustments using benchmark point .parquet files"
python3 $srcDir/src_adjust_spatial_obs.py -fim_dir $outputDestDir -j $jobLimit
Tcount
fi
## AGGREGATE BRANCH TABLES ##
l_echo $startDiv"Aggregating branch hydrotables"
Tstart
python3 $srcDir/aggregate_by_huc.py \
-fim $outputDestDir \
-i $fim_inputs \
-htable \
-bridge \
-j $jobLimit
Tcount
## PERFORM MANUAL CALIBRATION
if [ "$manual_calb_toggle" = "True" ] && [ -f $man_calb_file ]; then
l_echo $startDiv"Performing manual calibration"
Tstart
python3 $srcDir/src_manual_calibration.py \
-fim_dir $outputDestDir \
-calb_file $man_calb_file
Tcount
fi
l_echo $startDiv"Combining crosswalk tables"
Tstart
python3 $toolsDir/combine_crosswalk_tables.py \
-d $outputDestDir \
-o $outputDestDir/crosswalk_table.csv
Tcount
l_echo $startDiv"Resetting Permissions"
Tstart
# super slow to change chmod on the log folder. Not really manditory anyways
find $outputDestDir -maxdepth 1 -type f -exec chmod 777 {} + # just root level files
Tcount
l_echo $startDiv"Scanning logs for errors and warnings. This can take quite a few minutes so stand by."
echo "Results will be saved in root not inside the log folder."
Tstart
# grep -H -r -i -n "error" $outputDestDir/logs/ > $outputDestDir/all_errors_from_logs.log
find $outputDestDir -type f | grep -H -R -i -n ".*error.*" $outputDestDir/logs/ > \
$outputDestDir/all_errors_from_logs.log &
l_echo "error scan done, now on to warnings scan"
find $outputDestDir -type f | grep -H -R -i -n ".*warning.*" $outputDestDir/logs/ > \
$outputDestDir/all_warnings_from_logs.log &
l_echo "warning scan done"
Tcount
echo
l_echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
l_echo "---- End of fim_post_processing"
l_echo "---- Ended: `date -u`"
Calc_Duration "Post Processing Duration:" $post_proc_start_time
echo