Skip to content

Commit d9235e2

Browse files
committed
Add function
1 parent ccb7835 commit d9235e2

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

zppy/templates/tc_analysis.bash

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,42 @@ 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
37-
grid="${BASH_REMATCH[0]}"
34+
35+
infer_res_from_grid()
36+
{
37+
# Local to avoid using outer scope res, pg2
38+
local res=$1
39+
local grid=$2
3840
echo "grid=${grid}"
3941
if [[ -z "${res}" ]]; then
4042
echo "Inferring res from grid"
4143
if [[ $grid =~ ne([0-9]*) ]]; then
4244
res=${BASH_REMATCH[1]}
4345
fi
4446
fi
47+
echo ${res}
48+
}
49+
50+
res={{ res }}
51+
pg2=false
52+
# We set pg2=false unless it appears in this exact part of the filename:
53+
if [[ $filename =~ ne[0-9]+np4pg2+ ]]; then
54+
grid="${BASH_REMATCH[0]}"
55+
res=$(infer_res_from_grid "${res}" "${grid}")
4556
pg2=true
4657
elif [[ $filename =~ ne[0-9]+np4 ]]; then
4758
grid="${BASH_REMATCH[0]}"
48-
echo "grid=${grid}"
59+
res=$(infer_res_from_grid "${res}" "${grid}")
60+
# pg2 is default value
61+
else
4962
if [[ -z "${res}" ]]; then
50-
echo "Inferring res from grid"
51-
if [[ $grid =~ ne([0-9]*) ]]; then
52-
res=${BASH_REMATCH[1]}
53-
fi
63+
echo "Could not infer ${res} from ${filename}"
64+
exit 1
5465
fi
55-
pg2=false
56-
else
57-
echo "Pattern not found in filename: $filename"
66+
# res, in this case, is whatever the user defined it as
67+
# pg2 is default value
5868
fi
5969
echo "res=${res}"
6070
echo "pg2=${pg2}"

0 commit comments

Comments
 (0)