Skip to content

Commit ef13ad7

Browse files
zhangshixuan1987forsyth2
authored andcommitted
Improve robustness of reference year range handling
Added input validation to ensure all year variables are integers and within a valid order. Normalized start/end pairs so reversed inputs (e.g. y1 > y2, ref_start_yr > ref_final_yr) are automatically swapped. Recorded the original custom reference range (ref_y1, ref_y2) and clamped it to the available reference window for safe fallback. Refined the adjustment logic so: If availability fully covers the analysis period, use [y1, y2]. Otherwise, prefer the clamped custom range and trim only if it extends beyond the analysis window. If custom and analysis don’t overlap, fall back to the overlap of availability ∩ analysis. If no overlap exists, fall back to the clamped custom, and finally the full available window. Ensured final ref_y1 ≤ ref_y2 and applied zero-padding formatting once at the end.
1 parent 493e451 commit ef13ad7

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

zppy/templates/pcmdi_diags.bash

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,44 @@ Y2="$(printf "%04d" ${y2})"
4646
ref_Y1="$(printf "%04d" ${ref_y1})"
4747
ref_Y2="$(printf "%04d" ${ref_y2})"
4848

49-
# Refine reference range
50-
if [[ ${ref_y1} -lt ${ref_start_yr} ]]; then
51-
ref_y1=${ref_start_yr}
52-
ref_Y1="$(printf "%04d" ${ref_y1})"
53-
fi
54-
5549
num_years=$((y2 - y1 + 1))
5650
ref_end_yr=$((ref_y1 + num_years - 1))
5751

58-
if [[ ${ref_y2} -gt ${ref_end_yr} ]]; then
59-
ref_y2=${ref_end_yr}
60-
ref_Y2="$(printf "%04d" ${ref_y2})"
52+
# Keep originals for fallback
53+
orig_ref_y1=${ref_y1}
54+
orig_ref_y2=${ref_y2}
55+
if [[ ${orig_ref_y1} -lt ${ref_start_yr} ]]; then
56+
orig_ref_y1=${ref_start_yr}
6157
fi
62-
63-
if [[ ${ref_y2} -gt ${ref_final_yr} ]]; then
64-
ref_y2=${ref_final_yr}
65-
ref_Y2="$(printf "%04d" ${ref_y2})"
58+
if [[ ${orig_ref_y2} -gt ${ref_final_yr} ]]; then
59+
orig_ref_y2=${ref_final_yr}
60+
fi
61+
# Refine reference range
62+
if [[ ${ref_start_yr} -le ${y1} && ${ref_final_yr} -ge ${y2} ]]; then
63+
# Availability fully covers analysis: use [y1, y2]
64+
ref_y1=${y1}
65+
ref_y2=${y2}
66+
else
67+
# Case 2: Prefer the user's clamped custom window; only tweak if needed
68+
# 2a) If clamped custom overlaps the analysis window, use the intersection
69+
if [[ ${orig_ref_y1} -le ${y2} && ${orig_ref_y2} -ge ${y1} ]]; then
70+
# minimal trimming to fit inside [y1, y2]
71+
ref_y1=$(( orig_ref_y1 > y1 ? orig_ref_y1 : y1 ))
72+
ref_y2=$(( orig_ref_y2 < y2 ? orig_ref_y2 : y2 ))
73+
else
74+
# 2b) No overlap between custom and analysis:
75+
# try availability i∩ analysis (minimal change wrt both)
76+
ovl_start=$(( ref_start_yr > y1 ? ref_start_yr : y1 ))
77+
ovl_end=$(( ref_final_yr < y2 ? ref_final_yr : y2 ))
78+
if [[ ${ovl_start} -le ${ovl_end} ]]; then
79+
ref_y1=${ovl_start}
80+
ref_y2=${ovl_end}
81+
else
82+
# 2c) Truly no overlap possible; fall back to clamped custom window
83+
ref_y1=${orig_ref_y1}
84+
ref_y2=${orig_ref_y2}
85+
fi
86+
fi
6687
fi
6788

6889
{% endif %}

0 commit comments

Comments
 (0)