@@ -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 ("\n Generating 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
0 commit comments