-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_forecast_plots.sh
executable file
·285 lines (268 loc) · 13.9 KB
/
run_forecast_plots.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
#!/usr/bin/env bash
source ENVS
if [ $# -lt 2 ] || [ "$1" == "-h" ]; then
echo -e "\nUsage $0 <forecast_name> <hemisphere>"
echo -e "\nArguments"
echo "<forecast_name> name of forecast"
echo "<hemisphere> hemisphere to use"
echo -e "\nOptions"
echo "-m <metrics> string of metrics separated by commas, by default \"binacc,sie,mae,rmse,sic\". Options: \"binacc\", \"sie\", \"mae\", \"mse\", \"rmse\", \"sic\""
echo "-r <region> region arguments, by default uses full hemisphere"
echo "-e compare forecast performance with ECMWF"
echo "-l produce leadtime averaged plots"
echo "-v produce video using the individual metric plots by stitching them together with ffmpeg"
echo "-t <threshold> SIC threshold to use (must be between 0 and 1), by default 0.15"
echo "-g <grid_area_size> grid area resolution to use - i.e. the length of the sides in km, by default 25 (i.e. 25km^2)"
echo "-o <output_dir> output directory path to store plots, by default \"plot/<forecast_name>\""
echo -e "\nList of outputs generated"
echo "* If \"binacc\" is included in the requested metrics, will generate all binary accuracy plots for dates in <forecast_name>_<hemisphere>.csv"
echo "- these will be saved in the format \"<output_dir>/binacc.t_<threshold>.<date>.png\""
echo "If \"-l\" is passed, leadtime averaged plots for binary accuracy will be generated too:"
echo " - averaging over all: \"<output_dir>/binacc.t_<threshold>_leadtime_avg_all.png\""
echo " - averaging by month and for initalisation date: \"<output_dir>/binacc.t_<threshold>_leadtime_avg_init_month.png\""
echo " - averaging by day and for initalisation date: \"<output_dir>/binacc.t_<threshold>_leadtime_avg_init_day.png\""
echo " - averaging by month and for target date: \"<output_dir>/binacc.t_<threshold>_leadtime_avg_target_month.png\""
echo " - averaging by day and for target date: \"<output_dir>/binacc.t_<threshold>_leadtime_avg_target_day.png\""
echo "If \"-v\" is passed, a video will be produced to stitch all these plots together and saved in \"<output_dir>/binacc.t_<threshold>.mp4\""
echo "* If \"sie\" is included in the requested metrics, will generate all SIE error plots for dates in <forecast_name>_<hemisphere>.csv"
echo "(these will be saved in the format \"<output_dir>/sie.t_<threshold>.g_<grid_area_size>.<date>.png\")"
echo "If \"-l\" is passed, leadtime averaged plots for SIE error will be generated too:"
echo " - averaging over all: \"<output_dir>/sie.t_<threshold>.g_<grid_area_size>_leadtime_avg_all.png\""
echo " - averaging by month and for initalisation date: \"<output_dir>/sie.t_<threshold>.g_<grid_area_size>_leadtime_avg_init_month.png\""
echo " - averaging by day and for initalisation date: \"<output_dir>/sie.t_<threshold>.g_<grid_area_size>_leadtime_avg_init_day.png\""
echo " - averaging by month and for target date: \"<output_dir>/sie.t_<threshold>.g_<grid_area_size>_leadtime_avg_target_month.png\""
echo " - averaging by day and for target date: \"<output_dir>/sie.t_<threshold>.g_<grid_area_size>_leadtime_avg_target_day.png\""
echo "If \"-v\" is passed, a video will be produced to stitch all these plots together and saved in \"<output_dir>/sie.t_<threshold>.g_<grid_area_size>.mp4\""
echo "* If \"mae\", \"mse\", or \"rmse\" is included in the requested metrics, will generate all MAE, MSE, or RMSE plots for dates in <forecast_name>_<hemisphere>.csv"
echo "the names for the plots follow a similar convention as above but without the threshold or grid-area-size being saved in the name..."
echo "for instance, for a given <metric>, these will be saved in the format \"<output_dir>/<metric>.<date>.png\""
echo "If \"-l\" is passed, leadtime averaged plots for <metric> will be generated too:"
echo " - averaging over all: \"<output_dir>/<metric>_leadtime_avg_all.png\""
echo " - averaging by month and for initalisation date: \"<output_dir>/<metric>_leadtime_avg_init_month.png\""
echo " - averaging by day and for initalisation date: \"<output_dir>/<metric>_leadtime_avg_init_day.png\""
echo " - averaging by month and for target date: \"<output_dir>/<metric>_leadtime_avg_target_month.png\""
echo " - averaging by day and for target date: \"<output_dir>/<metric>_leadtime_avg_target_day.png\""
echo "Note that if $\"-e\" is passed, all of these will have \"_comp\" after \"avg\""
echo "The plot of the standard deviation of the metric for each forecast will also be generated"
echo "If \"-v\" is passed, a video will be produced to stitch all these plots together and saved in \"<output_dir>/<metric>.mp4\""
echo "* If \"sic\" is included in the requested metrics, will generate all SIC error videos for dates in <forecast_name>_<hemisphere>.csv"
echo "(these will be saved in the format \"<output_dir>/sic.<date>.mp4\")"
exit 1
fi
# default values
METRICS="binacc,sic"
REGION=""
ECMWF="false"
LEADTIME_AVG="false"
VIDEO="false"
THRESHOLD="-t 0.15"
GRID_AREA_SIZE="-ga 25"
REQUESTED_OUTPUT_DIR=""
OPTIND=1
while getopts "m:r:elvt:g:o:" opt; do
case "$opt" in
m) METRICS=${OPTARG} ;;
r) REGION="-r ${OPTARG}" ;;
e) ECMWF="true" ;;
l) LEADTIME_AVG="true" ;;
v) VIDEO="true" ;;
t) THRESHOLD="-t ${OPTARG}" ;;
g) GRID_AREA_SIZE="-ga ${OPTARG}" ;;
o) REQUESTED_OUTPUT_DIR=${OPTARG}
esac
done
# split on commas
METRICS=(${METRICS//,/ })
# check if metric is valid
VALID_METRICS=("binacc" "sie" "mae" "mse" "rmse" "sic")
for element in "${METRICS[@]}"
do
if [[ ! "${VALID_METRICS[*]}" =~ "${element}" ]]; then
# element is not in VALID_METRICS
echo "'${element}' is not a valid metric"
exit 1
fi
done
# determine whether or not to compare with ECMWF
if [[ "${ECMWF}" == true ]]; then
echo "Generating (${METRICS[@]}) plots for forecast (with comparison with ECMWF)"
E_FLAG="-e"
else
echo "Generating (${METRICS[@]}) plots for forecast (without comparison with ECMWF)"
E_FLAG=""
fi
if [[ "${LEADTIME_AVG}" == true ]]; then
echo "Also generating leadtime averaged plots for the above metrics"
fi
shift $((OPTIND-1))
# echo "Leftovers from getopt: $@"
FORECAST="$1"
HEMI="$2"
FORECAST_NAME=${FORECAST}_${HEMI}
FORECAST_FILE="results/predict/${FORECAST_NAME}.nc"
LOG_PREFIX="logs/${FORECAST_NAME}"
BINACC_LOG="${LOG_PREFIX}_binacc.log"
SIE_LOG="${LOG_PREFIX}_sie.log"
MAE_LOG="${LOG_PREFIX}_mae.log"
MSE_LOG="${LOG_PREFIX}_mse.log"
RMSE_LOG="${LOG_PREFIX}_rmse.log"
SICERR_LOG="${LOG_PREFIX}_sic.log"
if [ "${REQUESTED_OUTPUT_DIR}" == "" ]; then
OUTPUT_DIR="plot/${FORECAST_NAME}"
else
OUTPUT_DIR=${REQUESTED_OUTPUT_DIR}
fi
# if [ -d $OUTPUT_DIR ]; then
# # remove existing log files if they exist
# rm -v -f $BINACC_LOG $SIE_LOG $MAE_LOG $MSE_LOG $RMSE_LOG $SICERR_LOG
# fi
mkdir -p $OUTPUT_DIR
echo "Saving plots in ${OUTPUT_DIR}"
echo "Reading ${FORECAST_NAME}.csv"
# create metric plots for each forecast date
cat ${FORECAST_NAME}.csv | while read -r FORECAST_DATE; do
for element in "${METRICS[@]}"
do
OUTPUT="${OUTPUT_DIR}/${element}.${FORECAST_DATE}.png"
if [ "${element}" == "binacc" ]; then
OUTPUT="${OUTPUT_DIR}/${element}.t_${THRESHOLD:3}.${FORECAST_DATE}.png"
echo "Producing binary accuracy plot for $FORECAST_DATE (${OUTPUT})"
icenet_plot_bin_accuracy -b $E_FLAG -v $REGION -o $OUTPUT $THRESHOLD \
$HEMI $FORECAST_FILE $FORECAST_DATE >> $BINACC_LOG 2>&1
elif [ "${element}" == "sie" ]; then
OUTPUT="${OUTPUT_DIR}/${element}.t_${THRESHOLD:3}.ga_${GRID_AREA_SIZE:4}.${FORECAST_DATE}.png"
echo "Producing sea ice extent error plot for $FORECAST_DATE (${OUTPUT})"
icenet_plot_sie_error -b $E_FLAG -v $REGION -o $OUTPUT $THRESHOLD $GRID_AREA_SIZE \
$HEMI $FORECAST_FILE $FORECAST_DATE >> $SIE_LOG 2>&1
elif [ "${element}" == "mae" ]; then
echo "Producing MAE plot for $FORECAST_DATE (${OUTPUT})"
icenet_plot_metrics -b $E_FLAG -v $REGION -m $element -o $OUTPUT \
$HEMI $FORECAST_FILE $FORECAST_DATE >> $MAE_LOG 2>&1
elif [ "${element}" == "mse" ]; then
echo "Producing MSE plot for $FORECAST_DATE (${OUTPUT})"
icenet_plot_metrics -b $E_FLAG -v $REGION -m $element -o $OUTPUT \
$HEMI $FORECAST_FILE $FORECAST_DATE >> $MSE_LOG 2>&1
elif [ "${element}" == "rmse" ]; then
echo "Producing RMSE plot for $FORECAST_DATE (${OUTPUT})"
icenet_plot_metrics -b $E_FLAG -v $REGION -m $element -o $OUTPUT \
$HEMI $FORECAST_FILE $FORECAST_DATE >> $RMSE_LOG 2>&1
elif [ "${element}" == "sic" ]; then
OUTPUT="${OUTPUT_DIR}/${element}.${FORECAST_DATE}.mp4"
echo "Producing SIC error video for $FORECAST_DATE (${OUTPUT})"
icenet_plot_sic_error -v $REGION -o $OUTPUT \
$HEMI $FORECAST_FILE $FORECAST_DATE >> $SICERR_LOG 2>&1
fi
done
done
# produce leadtime averaged plots
if [[ "${LEADTIME_AVG}" == true ]]; then
for element in "${METRICS[@]}"
do
if [ "${element}" == "sic" ]; then
continue
fi
PATH_START="${OUTPUT_DIR}/${element}"
if [ "${element}" == "binacc" ]; then
echo "Producing leadtime averaged binary accuracy plots..."
PATH_START="${PATH_START}.t_${THRESHOLD:3}"
LOGFILE="${BINACC_LOG}"
elif [ "${element}" == "sie" ]; then
echo "Producing leadtime averaged sea ice extent error plots..."
PATH_START="${PATH_START}.t_${THRESHOLD:3}.ga_${GRID_AREA_SIZE:4}"
LOGFILE="${SIE_LOG}"
elif [ "${element}" == "mae" ]; then
echo "Producing leadtime averaged MAE plots..."
LOGFILE="${MAE_LOG}"
elif [ "${element}" == "mse" ]; then
echo "Producing leadtime averaged MSE plots..."
LOGFILE="${MSE_LOG}"
elif [ "${element}" == "rmse" ]; then
echo "Producing leadtime averaged RMSE plots..."
LOGFILE="${RMSE_LOG}"
fi
# determining the path to save metrics dataframe and the beginning of the output paths
if [[ "${ECMWF}" == true ]]; then
DATA_PATH="${PATH_START}_leadtime_avg_df_comp.csv"
OUTPUT_PATH_START="${PATH_START}_leadtime_avg_comp"
else
DATA_PATH="${PATH_START}_leadtime_avg_df.csv"
OUTPUT_PATH_START="${PATH_START}_leadtime_avg"
fi
echo "Will produce metrics dataframe in ${DATA_PATH}"
echo "Plots produced:"
# averaging over all
OUTPUT="${OUTPUT_PATH_START}_all.png"
icenet_plot_leadtime_avg $HEMI $FORECAST_FILE $REGION \
-m $element -ao "all" -s -sm 1 $E_FLAG $THRESHOLD $GRID_AREA_SIZE \
-dp $DATA_PATH -o $OUTPUT >> $LOGFILE 2>&1
echo "* ${OUTPUT}"
##### initialisation day
# averaging over monthly
OUTPUT="${OUTPUT_PATH_START}_init_month.png"
icenet_plot_leadtime_avg $HEMI $FORECAST_FILE $REGION \
-m $element -ao "month" -s $E_FLAG $THRESHOLD $GRID_AREA_SIZE \
-dp $DATA_PATH -o $OUTPUT >> $LOGFILE 2>&1
echo "* ${OUTPUT}"
# averaging over daily
OUTPUT="${OUTPUT_PATH_START}_init_day.png"
icenet_plot_leadtime_avg $HEMI $FORECAST_FILE $REGION \
-m $element -ao "day" -s $E_FLAG $THRESHOLD $GRID_AREA_SIZE \
-dp $DATA_PATH -o $OUTPUT >> $LOGFILE 2>&1
echo "* ${OUTPUT}"
##### target day
# averaging over monthly
OUTPUT="${OUTPUT_PATH_START}_target_month.png"
icenet_plot_leadtime_avg $HEMI $FORECAST_FILE $REGION \
-m $element -ao "month" -s -td $E_FLAG $THRESHOLD $GRID_AREA_SIZE \
-dp $DATA_PATH -o $OUTPUT >> $LOGFILE 2>&1
echo "* ${OUTPUT}"
# averaging over daily
OUTPUT="${OUTPUT_PATH_START}_target_day.png"
icenet_plot_leadtime_avg $HEMI $FORECAST_FILE $REGION \
-m $element -ao "day" -s -td $E_FLAG $THRESHOLD $GRID_AREA_SIZE \
-dp $DATA_PATH -o $OUTPUT >> $LOGFILE 2>&1
echo "* ${OUTPUT}"
done
fi
# stitch together metric plots if requested
if [[ "${VIDEO}" == true ]]; then
for element in "${METRICS[@]}"
do
if [ "${element}" == "sic" ]; then
continue
fi
PATH_START="${OUTPUT_DIR}/${element}"
# add to PATH_SPART if we're working with binacc or SIE
if [ "${element}" == "binacc" ]; then
PATH_START="${PATH_START}.t_${THRESHOLD:3}"
elif [ "${element}" == "sie" ]; then
PATH_START="${PATH_START}.t_${THRESHOLD:3}.ga_${GRID_AREA_SIZE:4}"
fi
# print out where the plot will be saved
OUTPUT="${PATH_START}.mp4"
if [ "${element}" == "binacc" ]; then
echo "Producing rolling binary accuracy plot (${OUTPUT})"
LOGFILE="${BINACC_LOG}"
elif [ "${element}" == "sie" ]; then
echo "Producing rolling sea ice extent error plot (${OUTPUT})"
LOGFILE="${SIE_LOG}"
elif [ "${element}" == "mae" ]; then
echo "Producing rolling MAE plot (${OUTPUT})"
LOGFILE="${MAE_LOG}"
elif [ "${element}" == "mse" ]; then
echo "Producing rolling MSE plot (${OUTPUT})"
LOGFILE="${MSE_LOG}"
elif [ "${element}" == "rmse" ]; then
echo "Producing rolling RMSE plot (${OUTPUT})"
LOGFILE="${RMSE_LOG}"
fi
# determine whether or not to stitch the leadtime averaged plots
if [[ "${LEADTIME_AVG}" == true ]]; then
INPUTS="${PATH_START}*.png"
else
INPUTS="${PATH_START}.*.png"
fi
ffmpeg -framerate 10 -y -pattern_type glob -i "${INPUTS}" \
-vcodec libx264 -pix_fmt yuv420p $OUTPUT >> $LOGFILE 2>&1
done
fi