Skip to content

Commit 0bc4974

Browse files
committed
Clean up code
1 parent ccb7835 commit 0bc4974

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

zppy/templates/tc_analysis.bash

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,50 @@ first_file=`echo $(ls ${drc_in}/${caseid}.{{ input_files }}.*.nc | head -n 1)`
2929
echo "first_file=${first_file}"
3030
topography_file=`echo $(ncks --trd -M -m ${first_file} | grep -E -i "^global attribute [0-9]+: topography_file" | cut -f 11- -d ' ' | sort)`
3131
echo "topography_file=${topography_file}"
32-
res={{ res }}
33-
pg2=false
3432
filename="${topography_file##*/}"
3533
echo "filename=${filename}"
36-
if [[ $filename =~ ne[0-9]+np4pg2+ ]]; then
34+
# Default values for res and pg2:
35+
res={{ res }}
36+
pg2=false
37+
# We keep pg2=false unless it appears in this exact part of the filename:
38+
if [[ $filename =~ ne([0-9]+)np4pg2 ]]; then
39+
# v3 datasets and many v2 datasets match neXnp4pg2
40+
#
41+
# Note: this block is repeated below, but it is
42+
# hard to extract as a function because res may be empty,
43+
# and an empty string parameter will confuse bash.
3744
grid="${BASH_REMATCH[0]}"
3845
echo "grid=${grid}"
3946
if [[ -z "${res}" ]]; then
40-
echo "Inferring res from grid"
41-
if [[ $grid =~ ne([0-9]*) ]]; then
42-
res=${BASH_REMATCH[1]}
43-
fi
47+
echo "Inferring res from ${filename}"
48+
res=${BASH_REMATCH[1]}
4449
fi
4550
pg2=true
46-
elif [[ $filename =~ ne[0-9]+np4 ]]; then
51+
elif [[ $filename =~ ne([0-9]+)np4 ]]; then
52+
# v1 datasets match neXnp4
4753
grid="${BASH_REMATCH[0]}"
4854
echo "grid=${grid}"
4955
if [[ -z "${res}" ]]; then
50-
echo "Inferring res from grid"
51-
if [[ $grid =~ ne([0-9]*) ]]; then
52-
res=${BASH_REMATCH[1]}
53-
fi
56+
echo "Inferring res from ${filename}"
57+
res=${BASH_REMATCH[1]}
5458
fi
55-
pg2=false
5659
else
57-
echo "Pattern not found in filename: $filename"
60+
echo "Pattern not found in filename=${filename}"
61+
error_num=1
62+
cd {{ scriptDir }}
63+
echo "ERROR (${error_num})" > {{ prefix }}.status
64+
exit ${error_num}
5865
fi
5966
echo "res=${res}"
6067
echo "pg2=${pg2}"
68+
if [[ -z "${res}" ]]; then
69+
echo "Could not infer res from ${filename}"
70+
echo "Please set res in the cfg."
71+
error_num=2
72+
cd {{ scriptDir }}
73+
echo "ERROR (${error_num})" > {{ prefix }}.status
74+
exit ${error_num}
75+
fi
6176

6277
mkdir -p $result_dir
6378
file_name=${caseid}_${start}_${end}

0 commit comments

Comments
 (0)