@@ -109,22 +109,7 @@ def _parse_args():
109109 if not os .path .exists (filename ):
110110 raise FileNotFoundError (f"Input file not found: { filename } " )
111111
112- # Process time slice
113- # Assumes CESM behavior where data for e.g. 1987 is saved as 1988-01-01.
114- # It would be more robust, accounting for upcoming behavior (where timestamp for a year is the
115- # middle of that year), to do slice("YEAR1-01-03", "YEARN-01-02"), but that's not compatible
116- # with ctsm_pylib as of the version using python 3.7.9. See safer_timeslice() in cropcal_utils.
117- if args .first_year is not None :
118- date_1 = f"{ args .first_year + 1 } -01-01"
119- else :
120- date_1 = "0000-01-01"
121- if args .last_year is not None :
122- date_n = f"{ args .last_year + 1 } -01-01"
123- else :
124- date_n = "9999-12-31"
125- time_slice = slice (date_1 , date_n )
126-
127- return args , time_slice
112+ return args
128113
129114
130115def _get_cft_list (crop_list ):
@@ -182,7 +167,7 @@ def _get_gddn_for_cft(cft_str, variable):
182167
183168
184169def _get_output_varname (cft_str ):
185- cft_int = utils .vegtype_str2int (cft_str )[ 0 ]
170+ cft_int = utils .vegtype_str2int (cft_str )
186171 return f"gdd20bl_{ cft_int } "
187172
188173
@@ -232,7 +217,23 @@ def setup_output_dataset(input_files, author, variable, year_args, ds_in):
232217 return ds_out
233218
234219
235- def generate_gdd20_baseline (input_files , output_file , author , time_slice , variable , year_args ):
220+ def _get_time_slice (year_args ):
221+ """
222+ Based on years from input arguments, return a time slice for selecting from dataset
223+ """
224+ first_year = year_args [0 ]
225+ last_year = year_args [1 ]
226+ date_1 = f"{ first_year } -01-01"
227+ date_n = f"{ last_year } -12-31"
228+ if first_year is None :
229+ date_1 = "0000-01-01"
230+ if last_year is None :
231+ date_n = "9999-12-31"
232+ time_slice = slice (date_1 , date_n )
233+ return time_slice
234+
235+
236+ def generate_gdd20_baseline (input_files , output_file , author , variable , year_args ):
236237 """
237238 Generate stream_fldFileName_gdd20_baseline file from CTSM outputs
238239 """
@@ -252,6 +253,9 @@ def generate_gdd20_baseline(input_files, output_file, author, time_slice, variab
252253 input_files = list (set (input_files ))
253254 input_files .sort ()
254255
256+ # Process time slice
257+ time_slice = _get_time_slice (year_args )
258+
255259 # Import history files and ensure they have lat/lon dims
256260 ds_in = import_ds (input_files , my_vars = var_list_in + GRIDDING_VAR_LIST , time_slice = time_slice )
257261 if not all (x in ds_in .dims for x in ["lat" , "lon" ]):
@@ -275,7 +279,7 @@ def generate_gdd20_baseline(input_files, output_file, author, time_slice, variab
275279 # Process all crops
276280 encoding_dict = {}
277281 for cft_str in MGDCROP_LIST :
278- cft_int = utils .vegtype_str2int (cft_str )[ 0 ]
282+ cft_int = utils .vegtype_str2int (cft_str )
279283 print (f"{ cft_str } ({ cft_int } )" )
280284
281285 # Which GDDN history variable does this crop use? E.g., GDD0, GDD10
@@ -323,12 +327,11 @@ def main():
323327 """
324328 main() function for calling generate_gdd20_baseline.py from command line.
325329 """
326- args , time_slice = _parse_args ()
330+ args = _parse_args ()
327331 generate_gdd20_baseline (
328332 args .input_files ,
329333 args .output_file ,
330334 args .author ,
331- time_slice ,
332335 args .variable ,
333336 [args .first_year , args .last_year ],
334337 )
0 commit comments