Skip to content

Commit 7b05425

Browse files
Fixing African easterly wave density plots in TC analysis (#851)
* read in aew data without subsetting * exclude 6hourly data cross year bounds * use year range from stitch file name instead of data in file * fixtests * capture input data errors
1 parent 6b0a245 commit 7b05425

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

e3sm_diags/driver/tc_analysis_driver.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def run_diag(parameter: TCAnalysisParameter) -> TCAnalysisParameter:
8181
test_data_path,
8282
"aew_hist_{}_{}_{}.nc".format(test_name, test_start_yr, test_end_yr),
8383
)
84-
test_aew_hist = cdms2.open(test_aew_file)(
85-
"density", lat=(0, 35, "ccb"), lon=(-180, 0, "ccb"), squeeze=1
86-
)
84+
test_aew_hist = cdms2.open(test_aew_file)("density", squeeze=1)
8785

8886
test_data = collections.OrderedDict()
8987
ref_data = collections.OrderedDict()
@@ -134,9 +132,8 @@ def run_diag(parameter: TCAnalysisParameter) -> TCAnalysisParameter:
134132
"density", lat=(-60, 60, "ccb"), squeeze=1
135133
)
136134
ref_aew_file = os.path.join(reference_data_path, "aew_hist_ERA5_2010_2014.nc")
137-
ref_aew_hist = cdms2.open(ref_aew_file)(
138-
"density", lat=(0, 35, "ccb"), lon=(180, 360, "ccb"), squeeze=1
139-
)
135+
ref_aew_hist = cdms2.open(ref_aew_file)("density", squeeze=1)
136+
140137
ref_data["cyclone_density"] = ref_cyclones_hist
141138
ref_data["cyclone_num_years"] = 40 # type: ignore
142139
ref_data["aew_density"] = ref_aew_hist
@@ -163,13 +160,40 @@ def generate_tc_metrics_from_te_stitch_file(te_stitch_file: str) -> Dict[str, An
163160
"""
164161
logger.info("\nGenerating TC Metrics from TE Stitch Files")
165162
logger.info("============================================")
163+
if not os.path.exists(te_stitch_file):
164+
raise FileNotFoundError(f"The file {te_stitch_file} does not exist.")
165+
166166
with open(te_stitch_file) as f:
167-
lines = f.readlines()
167+
lines_orig = f.readlines()
168+
169+
if not lines_orig:
170+
raise ValueError(f"The file {te_stitch_file} is empty.")
171+
172+
line_ind = []
173+
data_start_year = int(te_stitch_file.split(".")[-2].split("_")[-2])
174+
data_end_year = int(te_stitch_file.split(".")[-2].split("_")[-1])
175+
for i in range(0, np.size(lines_orig)):
176+
if lines_orig[i][0] == "s":
177+
year = int(lines_orig[i].split("\t")[2])
178+
179+
if year <= data_end_year:
180+
line_ind.append(i)
181+
182+
# Remove excessive time points cross year bounds from 6 hourly data
183+
end_ind = line_ind[-1]
184+
lines = lines_orig[0:end_ind]
168185

169186
# Calculate number of storms and max length
170187
num_storms, max_len = _calc_num_storms_and_max_len(lines)
171188
# Parse variables from TE stitch file
172189
te_stitch_vars = _get_vars_from_te_stitch(lines, max_len, num_storms)
190+
# Add year info
191+
te_stitch_vars["year_start"] = data_start_year
192+
te_stitch_vars["year_end"] = data_end_year
193+
te_stitch_vars["num_years"] = data_end_year - data_start_year + 1
194+
logger.info(
195+
f"TE Start Year: {te_stitch_vars['year_start']}, TE End Year: {te_stitch_vars['year_end']}, Total Years: {te_stitch_vars['num_years']}"
196+
)
173197

174198
# Use E3SM land-sea mask
175199
mask_path = os.path.join(e3sm_diags.INSTALL_PATH, "acme_ne30_ocean_land_mask.nc")
@@ -246,15 +270,11 @@ def _get_vars_from_te_stitch(
246270
vars_dict = {k: np.empty((max_len, num_storms)) * np.nan for k in keys}
247271

248272
index = 0
249-
year_start = int(lines[0].split("\t")[2])
250-
year_end = year_start
251273

252274
for line in lines:
253275
line_split = line.split("\t")
254276
if line[0] == "s":
255277
index = index + 1
256-
year = int(line_split[2])
257-
year_end = max(year, year_start)
258278
k = 0
259279
else:
260280
k = k + 1
@@ -265,13 +285,6 @@ def _get_vars_from_te_stitch(
265285
vars_dict["yearmc"][k - 1, index - 1] = float(line_split[6])
266286
vars_dict["monthmc"][k - 1, index - 1] = float(line_split[7])
267287

268-
vars_dict["year_start"] = year_start # type: ignore
269-
vars_dict["year_end"] = year_end # type: ignore
270-
vars_dict["num_years"] = year_end - year_start + 1 # type: ignore
271-
logger.info(
272-
f"TE Start Year: {vars_dict['year_start']}, TE End Year: {vars_dict['year_end']}, Total Years: {vars_dict['num_years']}"
273-
)
274-
275288
return vars_dict
276289

277290

tests/e3sm_diags/drivers/test_tc_analysis_driver.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ def test_correct_output(self):
5959
"vsmc": np.array([[1.94, np.nan]]),
6060
"yearmc": np.array([[1, np.nan]]),
6161
"monthmc": np.array([[1, np.nan]]),
62-
"year_start": 90,
63-
"year_end": 90,
64-
"num_years": 1,
6562
}
6663
result = _get_vars_from_te_stitch(lines, max_len, num_storms)
6764

@@ -70,9 +67,6 @@ def test_correct_output(self):
7067
np.array_equal(result["vsmc"], expected["vsmc"])
7168
np.array_equal(result["yearmc"], expected["yearmc"])
7269
np.array_equal(result["monthmc"], expected["monthmc"])
73-
self.assertEqual(result["year_start"], expected["year_start"])
74-
self.assertEqual(result["year_end"], expected["year_end"])
75-
self.assertEqual(result["num_years"], expected["num_years"])
7670

7771

7872
class TestDeriveMetricsPerBasin(TestCase):

0 commit comments

Comments
 (0)