diff --git a/scripts/run_OQStandard.sh b/scripts/run_OQStandard.sh index 8f832ed..5692da9 100644 --- a/scripts/run_OQStandard.sh +++ b/scripts/run_OQStandard.sh @@ -2,9 +2,13 @@ # ========================================================================== # Script for running scenario calculations in the National Canada Risk Model # ========================================================================== + +set -exo pipefail + usage() { -echo "Script for running scenario calculations in OpenQuake, using Canadian data. -You need to have created the hazard, damage, and risk files already, as well as + cat < ./${OUTDIR}/s_Hazard_${NAME}.log; + oq engine --run "${JOBDIR}/s_Hazard_${NAME}.ini" 2>&1 | tee "./${OUTDIR}/s_Hazard_${NAME}.log" oq export gmf_data -1 -e csv -d temp - CALCID=`basename temp/*gmf* .csv | awk -F'_' '{print $NF}'` - python3 $AVGHAZLOC $NAME $CALCID + CALCID=$(basename temp/*gmf* .csv | awk -F'_' '{print $NF}') + python3 "$AVGHAZLOC" "$NAME" "$CALCID" SMSFILE="${OUTDIR}/s_sitemesh_${NAME}_${CALCID}.csv" - mv temp/*sitemesh* ${SMSFILE} - rm -f temp/sigma_epsilon_* temp/*gmf*.csv temp/realizations_${CALCID}.csv + mv temp/*sitemesh* "${SMSFILE}" + rm -f temp/sigma_epsilon_* temp/*gmf*.csv "temp/realizations_${CALCID}.csv" fi @@ -166,42 +175,42 @@ if [[ $DMGFLAG == "1" ]]; then echo "------------------------------------------------" echo "RUNNING DAMAGE & CONSEQUENCE CALCULATIONS" echo "------------------------------------------------" - if [[ $RETROFLAG == "1" ]]; then + if [[ $RETROFLAG == "1" ]]; then # RUN TWO CALCS WITH SHARED HAZ CALC - oq engine --run ${JOBDIR}/s_Damage_${NAME}_${arr[0]}_${expoSuffix}.ini ${JOBDIR}/s_Damage_${NAME}_${arr[1]}_${expoSuffix}.ini &> ./${OUTDIR}/s_Damage_${NAME}_b0r1_${expoSuffix}.log; - + oq engine --run "${JOBDIR}/s_Damage_${NAME}_${arr[0]}_${expoSuffix}.ini" "${JOBDIR}/s_Damage_${NAME}_${arr[1]}_${expoSuffix}.ini" 2>&1 | tee "./${OUTDIR}/s_Damage_${NAME}_b0r1_${expoSuffix}.log" + # EXPORT BASELINE, WHICH IS SECOND TO LAST CALC oq export damages-rlzs -2 -e csv -d temp oq export realizations -2 -e csv -d temp - CALCID=` ls -t temp/avg_damages-rlz-???_* | head -1 | awk -F'[_.]' '{print $(NF-1)}'` - python3 $AVG_LOC $NAME ${arr[0]} $CALCID $expoSuffix damage - python3 $CONSQ_LOC -2 - declare -a files=(`ls consequences-rlz-???_-2.csv`) #conseq script doesn't know real calc id, so need to replace the "-2" - for file in $files; do mv $file "temp/"`basename $file "-2.csv"`${CALCID}".csv"; done - python3 $AVG_LOC $NAME ${arr[0]} $CALCID $expoSuffix consequence - rm -f temp/consequences-rlz-???_${CALCID}.csv temp/realizations_${CALCID}.csv temp/avg_damages-rlz-???_${CALCID}.csv #clear temp dir - + CALCID=$(ls -t temp/avg_damages-rlz-???_* | head -1 | awk -F'[_.]' '{print $(NF-1)}') + python3 "$AVG_LOC" "$NAME" "${arr[0]}" "$CALCID" $expoSuffix damage + python3 "$CONSQ_LOC" -2 + declare -a files=($(ls consequences-rlz-???_-2.csv)) #conseq script doesn't know real calc id, so need to replace the "-2" + for file in "${files[@]}"; do mv "$file" "temp/$(basename "$file" "-2.csv")${CALCID}.csv"; done + python3 "$AVG_LOC" "$NAME" "${arr[0]}" "$CALCID" $expoSuffix consequence + rm -f temp/consequences-rlz-???_"${CALCID}".csv temp/realizations_"${CALCID}".csv temp/avg_damages-rlz-???_"${CALCID}".csv #clear temp dir + # EXPORT RETROFIT 1, LAST CALC oq export damages-rlzs -1 -e csv -d temp oq export realizations -1 -e csv -d temp - CALCID=` ls -t temp/avg_damages-rlz-???_* | head -1 | awk -F'[_.]' '{print $(NF-1)}'` - python3 $AVG_LOC $NAME ${arr[1]} $CALCID $expoSuffix damage - python3 $CONSQ_LOC -1 - mv consequences-rlz-???_${CALCID}.csv temp/ - python3 $AVG_LOC $NAME ${arr[1]} $CALCID $expoSuffix consequence - rm -f temp/consequences-rlz-???_${CALCID}.csv temp/realizations_${CALCID}.csv temp/avg_damages-rlz-???_${CALCID}.csv #clear temp dir - + CALCID=$(ls -t temp/avg_damages-rlz-???_* | head -1 | awk -F'[_.]' '{print $(NF-1)}') + python3 "$AVG_LOC" "$NAME" "${arr[1]}" "$CALCID" $expoSuffix damage + python3 "$CONSQ_LOC" -1 + mv consequences-rlz-???_"${CALCID}".csv temp/ + python3 "$AVG_LOC" "$NAME" "${arr[1]}" "$CALCID" $expoSuffix consequence + rm -f temp/consequences-rlz-???_"${CALCID}".csv temp/realizations_"${CALCID}".csv temp/avg_damages-rlz-???_"${CALCID}".csv #clear temp dir + else # RUN A SINGLE CALC - oq engine --run ${JOBDIR}/s_Damage_${NAME}_${arr[0]}_${expoSuffix}.ini > ./${OUTDIR}/s_Damage_${NAME}_${arr[0]}_${expoSuffix}.log + oq engine --run "${JOBDIR}/s_Damage_${NAME}_${arr[0]}_${expoSuffix}.ini" 2>&1 | tee "./${OUTDIR}/s_Damage_${NAME}_${arr[0]}_${expoSuffix}.log" oq export damages-rlzs -1 -e csv -d temp oq export realizations -1 -e csv -d temp - CALCID=` ls -t temp/avg_damages-rlz-???_* | head -1 | awk -F'[_.]' '{print $(NF-1)}'` - python3 $AVG_LOC $NAME ${arr[0]} $CALCID $expoSuffix damage + CALCID=$(ls -t temp/avg_damages-rlz-???_* | head -1 | awk -F'[_.]' '{print $(NF-1)}') + python3 $AVG_LOC "$NAME" "${arr[0]}" "$CALCID" $expoSuffix damage python3 $CONSQ_LOC -1 - mv consequences-rlz-???_${CALCID}.csv temp/ - python3 $AVG_LOC $NAME ${arr[0]} $CALCID $expoSuffix consequence - rm -f temp/consequences-rlz-???_${CALCID}.csv temp/realizations_${CALCID}.csv temp/avg_damages-rlz-???_${CALCID}.csv #clear temp dir + mv consequences-rlz-???_"${CALCID}".csv temp/ + python3 $AVG_LOC "$NAME" "${arr[0]}" "$CALCID" $expoSuffix consequence + rm -f temp/consequences-rlz-???_"${CALCID}".csv temp/realizations_"${CALCID}".csv temp/avg_damages-rlz-???_"${CALCID}".csv #clear temp dir fi fi @@ -212,29 +221,29 @@ if [[ $RSKFLAG == "1" ]]; then echo "------------------------------------------------" if [[ $RETROFLAG == "1" ]]; then # RUN TWO CALCS WITH SHARED HAZ CALC - oq engine --run ${JOBDIR}/s_Risk_${NAME}_${arr[0]}_${expoSuffix}.ini ${JOBDIR}/s_Risk_${NAME}_${arr[1]}_${expoSuffix}.ini &> ./${OUTDIR}/s_Risk_${NAME}_b0r1_${expoSuffix}.log; - + oq engine --run "${JOBDIR}/s_Risk_${NAME}_${arr[0]}_${expoSuffix}.ini" "${JOBDIR}/s_Risk_${NAME}_${arr[1]}_${expoSuffix}.ini" 2>&1 | tee "./${OUTDIR}/s_Risk_${NAME}_b0r1_${expoSuffix}.log" + # EXPORT BASELINE, WHICH IS SECOND TO LAST CALC - oq export avg_losses-rlzs -2 -e csv -d temp; - CALCID=`ls -t temp/avg_losses* | head -1 | awk -F'[_.]' '{print $(NF-1)}'` + oq export avg_losses-rlzs -2 -e csv -d temp + CALCID=$(ls -t temp/avg_losses* | head -1 | awk -F'[_.]' '{print $(NF-1)}') #Run weighted average script, save only that - python3 $AVG_LOC $NAME ${arr[0]} $CALCID $expoSuffix loss - rm -f temp/avg_losses-rlz-???_${CALCID}.csv temp/realizations_${CALCID}.csv - + python3 $AVG_LOC "$NAME" "${arr[0]}" "$CALCID" $expoSuffix loss + rm -f temp/avg_losses-rlz-???_"${CALCID}".csv temp/realizations_"${CALCID}".csv + # EXPORT RETROFIT 1, LAST CALC - oq export avg_losses-rlzs -1 -e csv -d temp; - CALCID=`ls -t temp/avg_losses* | head -1 | awk -F'[_.]' '{print $(NF-1)}'` + oq export avg_losses-rlzs -1 -e csv -d temp + CALCID=$(ls -t temp/avg_losses* | head -1 | awk -F'[_.]' '{print $(NF-1)}') #Run weighted average script, save only that - python3 $AVG_LOC $NAME ${arr[1]} $CALCID $expoSuffix loss - rm -f temp/avg_losses-rlz-???_${CALCID}.csv temp/realizations_${CALCID}.csv + python3 $AVG_LOC "$NAME" "${arr[1]}" "$CALCID" $expoSuffix loss + rm -f temp/avg_losses-rlz-???_"${CALCID}".csv temp/realizations_"${CALCID}".csv else # RUN A SINGLE CALC - oq engine --run ${JOBDIR}/s_Risk_${NAME}_${arr[0]}_${expoSuffix}.ini &> ./${OUTDIR}/s_Risk_${NAME}_${arr[0]}_${expoSuffix}.log; - oq export avg_losses-rlzs -1 -e csv -d temp; - CALCID=`ls -t temp/avg_losses* | head -1 | awk -F'[_.]' '{print $(NF-1)}'` + oq engine --run "${JOBDIR}/s_Risk_${NAME}_${arr[0]}_${expoSuffix}.ini" 2>&1 | tee "./${OUTDIR}/s_Risk_${NAME}_${arr[0]}_${expoSuffix}.log" + oq export avg_losses-rlzs -1 -e csv -d temp + CALCID=$(ls -t temp/avg_losses* | head -1 | awk -F'[_.]' '{print $(NF-1)}') #Run weighted average script, save only that - python3 $AVG_LOC $NAME ${arr[0]} $CALCID $expoSuffix loss - rm -f temp/avg_losses-rlz-???_${CALCID}.csv temp/realizations_${CALCID}.csv + python3 $AVG_LOC "$NAME" "${arr[0]}" "$CALCID" $expoSuffix loss + rm -f temp/avg_losses-rlz-???_"${CALCID}".csv temp/realizations_"${CALCID}".csv fi fi @@ -249,11 +258,11 @@ exit 0 # ================================================================= # Spit out the 4D's # ================================================================= -DOLLARS=`awk -F',' 'dollars=$22+$24+$28 {sum += dollars} END {printf "%f",sum}' ${RISKFILE}` -DEATH=`awk -F',' '{sum += $26} END {printf "%f",sum}' ${RISKFILE}` -REDTAG=`awk -F',' 'dmg=$28+$30 {sum =+ dmg} END {printf "%f",sum}' ${DMGFILE}` -DOWNTIME=`awk -F',' '{sum += $12} END {printf "%f",sum/NR}' ${CONSFILE}` -echo 'Dollars = $'"${DOLLARS}" +DOLLARS=$(awk -F',' 'dollars=$22+$24+$28 {sum += dollars} END {printf "%f",sum}' "${RISKFILE}") +DEATH=$(awk -F',' '{sum += $26} END {printf "%f",sum}' "${RISKFILE}") +REDTAG=$(awk -F',' 'dmg=$28+$30 {sum =+ dmg} END {printf "%f",sum}' "${DMGFILE}") +DOWNTIME=$(awk -F',' '{sum += $12} END {printf "%f",sum/NR}' "${CONSFILE}") +echo "Dollars = \$${DOLLARS}" echo "Deaths = ${DEATH}" echo "Damage = ${REDTAG} red tagged buildings" echo "Downtime = ${DOWNTIME} days, on average"