Skip to content

Commit

Permalink
Merge pull request #275 from justin-richling/h-number-update
Browse files Browse the repository at this point in the history
Add flexibility for globbing different h-numbers
  • Loading branch information
justin-richling authored Jan 18, 2024
2 parents fe04d98 + 043a8be commit e2447be
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions lib/adf_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,41 @@ def __init__(self, config_file, debug=False):
if baseline_hist_locs:

starting_location = Path(baseline_hist_locs)
files_list = sorted(starting_location.glob('*'+hist_str+'.*.nc'))
base_climo_yrs_str = sorted(np.unique([i.stem[-7:-3] for i in files_list]))
base_climo_yrs = []
for year in base_climo_yrs_str:
base_climo_yrs.append(int(year))

file_list = sorted(starting_location.glob('*'+hist_str+'.*.nc'))
#Partition string to find exactly where h-number is
#This cuts the string before and after the `{hist_str}.` sub-string
# so there will always be three parts:
# before sub-string, sub-string, and after sub-string
#Since the last part always includes the time range, grab that with last index (2)
#NOTE: this is based off the current CAM file name structure in the form:
# $CASE.cam.h#.YYYY<other date info>.nc
base_climo_yrs = [int(str(i).partition(f"{hist_str}.")[2][0:4]) for i in file_list]
base_climo_yrs = sorted(np.unique(base_climo_yrs))

#Check if start or end year is missing. If so then just assume it is the
#start or end of the entire available model data.
if syear_baseline is None:
print(f"No given start year for {data_name}, using first found year...")
syear_baseline = int(base_climo_yrs[0])
elif (syear_baseline) not in base_climo_yrs:
print(f"Given start year '{syear_baseline}' is not in current dataset {data_name}, using first found year:",base_climo_yrs[0],"\n")
msg = f"Given start year '{syear_baseline}' is not in current dataset "
msg += f"{data_name}, using first found year: {base_climo_yrs[0]}\n"
print(msg)
syear_baseline = int(base_climo_yrs[0])
#End if
if eyear_baseline is None:
print(f"No given end year for {data_name}, using last found year...")
eyear_baseline = int(base_climo_yrs[-1])
elif (eyear_baseline) not in base_climo_yrs:
print(f"Given end year '{eyear_baseline}' is not in current dataset {data_name}, using last found year:",base_climo_yrs[-1],"\n")
msg = f"Given end year '{eyear_baseline}' is not in current dataset "
msg += f"{data_name}, using last found year: {base_climo_yrs[-1]}\n"
print(msg)
eyear_baseline = int(base_climo_yrs[-1])
#End if

#Grab baseline nickname
base_nickname = self.get_baseline_info('case_nickname')
if base_nickname == None:
if base_nickname is None:
base_nickname = data_name

else:
Expand All @@ -219,7 +228,7 @@ def __init__(self, config_file, debug=False):

#Grab baseline nickname
base_nickname = self.get_baseline_info('case_nickname')
if base_nickname == None:
if base_nickname is None:
base_nickname = data_name

#Update baseline case name:
Expand Down Expand Up @@ -268,34 +277,42 @@ def __init__(self, config_file, debug=False):
for case_idx, case_name in enumerate(case_names):

syear = int(f"{str(syears[case_idx]).zfill(4)}")
syears_fixed.append(syear)
eyear = int(f"{str(eyears[case_idx]).zfill(4)}")
eyears_fixed.append(eyear)

#Check if history file path exists:
if cam_hist_locs:
#Get climo years for verification or assignment if missing
starting_location = Path(cam_hist_locs[case_idx])
files_list = sorted(starting_location.glob('*'+hist_str+'.*.nc'))
case_climo_yrs_str = sorted(np.unique([i.stem[-7:-3] for i in files_list]))
case_climo_yrs = []
for year in case_climo_yrs_str:
case_climo_yrs.append(int(year))
file_list = sorted(starting_location.glob('*'+hist_str+'.*.nc'))
#Partition string to find exactly where h-number is
#This cuts the string before and after the `{hist_str}.` sub-string
# so there will always be three parts:
# before sub-string, sub-string, and after sub-string
#Since the last part always includes the time range, grab that with last index (2)
#NOTE: this is based off the current CAM file name structure in the form:
# $CASE.cam.h#.YYYY<other date info>.nc
case_climo_yrs = [int(str(i).partition(f"{hist_str}.")[2][0:4]) for i in file_list]
case_climo_yrs = sorted(np.unique(case_climo_yrs))


#Check if start or end year is missing. If so then just assume it is the
#start or end of the entire available model data.
if syear is None:
print(f"No given start year for {case_name}, using first found year...")
syear = int(case_climo_yrs[0])
elif (syear) not in case_climo_yrs:
print(f"Given start year '{syear}' is not in current dataset {case_name}, using first found year:",case_climo_yrs[0],"\n")
msg = f"Given start year '{syear}' is not in current dataset "
msg += f"{case_name}, using first found year: {case_climo_yrs[0]}\n"
print(msg)
syear = int(case_climo_yrs[0])
#End if
if eyear is None:
print(f"No given end year for {case_name}, using last found year...")
eyear = int(case_climo_yrs[-1])
elif (eyear) not in case_climo_yrs:
print(f"Given end year '{eyear}' is not in current dataset {case_name}, using last found year:",case_climo_yrs[-1],"\n")
msg = f"Given end year '{eyear}' is not in current dataset "
msg += f"{case_name}, using last found year: {case_climo_yrs[-1]}\n"
print(msg)
eyear = int(case_climo_yrs[-1])
#End if

Expand All @@ -311,6 +328,10 @@ def __init__(self, config_file, debug=False):
#End if
#End if

#Update climo year lists in case anything changed
syears_fixed.append(syear)
eyears_fixed.append(eyear)

#Update case name with provided/found years:
case_name += f"_{syear}_{eyear}"

Expand Down

0 comments on commit e2447be

Please sign in to comment.