From 5cf8a79bd147b5b26ee3ceed7d5aba485c746dcb Mon Sep 17 00:00:00 2001 From: justin-richling Date: Tue, 19 Nov 2024 13:35:05 -0700 Subject: [PATCH] Update adf_info.py Add logging for failed history file location and climo years --- lib/adf_info.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/adf_info.py b/lib/adf_info.py index 36661a0dc..dd4270f6b 100644 --- a/lib/adf_info.py +++ b/lib/adf_info.py @@ -254,9 +254,32 @@ def __init__(self, config_file, debug=False): #Grab first possible hist string, just looking for years of run base_hist_str = baseline_hist_str[0] - starting_location = Path(baseline_hist_locs) file_list = sorted(starting_location.glob("*" + base_hist_str + ".*.nc")) + + #Check if the history file location exists + if not starting_location.is_dir(): + msg = "Checking history file location:\n" + msg += f"\tThere is no history file location: '{starting_location}'." + self.debug_log(msg) + emsg = f"{data_name} starting_location: History file location not found!\n" + emsg += "\tTry checking the path 'cam_hist_loc' in 'diag_cam_baseline_climo' " + emsg += "section in your config file is correct..." + self.end_diag_fail(emsg) + file_list = sorted(starting_location.glob("*" + base_hist_str + ".*.nc")) + + #Check if there are any history files + if len(file_list) == 0: + msg = "Checking history files:\n" + msg += f"\tThere are no history files in '{starting_location}'." + self.debug_log(msg) + emsg = f"{data_name} starting_location {starting_location}: " + emsg += f"No history files found for {base_hist_str}!\n" + emsg += "\tTry checking the path 'cam_hist_loc' or the 'hist_str' " + emsg += " in 'diag_cam_baseline_climo' " + emsg += "section in your config file are correct..." + self.end_diag_fail(emsg) + # 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: @@ -265,6 +288,10 @@ def __init__(self, config_file, debug=False): #NOTE: this is based off the current CAM file name structure in the form: # $CASE.cam.h#.YYYY.nc base_climo_yrs = [int(str(i).partition(f"{base_hist_str}.")[2][0:4]) for i in file_list] + if not base_climo_yrs: + msg = f"No climo years found in {baseline_hist_locs}, " + raise AdfError(msg) + base_climo_yrs = sorted(np.unique(base_climo_yrs)) base_found_syr = int(base_climo_yrs[0]) @@ -421,7 +448,33 @@ def __init__(self, config_file, debug=False): #Get climo years for verification or assignment if missing starting_location = Path(cam_hist_locs[case_idx]) + print(f"Checking history files in '{starting_location}'") + file_list = sorted(starting_location.glob('*'+hist_str+'.*.nc')) + + #Check if the history file location exists + if not starting_location.is_dir(): + msg = "Checking history file location:\n" + msg += f"\tThere is no history file location: '{starting_location}'." + self.debug_log(msg) + emsg = f"{case_name} starting_location: History file location not found!\n" + emsg += "\tTry checking the path 'cam_hist_loc' in 'diag_cam_climo' " + emsg += "section in your config file is correct..." + self.end_diag_fail(emsg) + + #Check if there are any history files + file_list = sorted(starting_location.glob('*'+hist_str+'.*.nc')) + if len(file_list) == 0: + msg = "Checking history files:\n" + msg += f"\tThere are no history files in '{starting_location}'." + self.debug_log(msg) + emsg = f"{case_name} starting_location {starting_location}: " + emsg += f"No history files found for {hist_str}!\n" + emsg += "\tTry checking the path 'cam_hist_loc' or the 'hist_str' " + emsg += "in 'diag_cam_climo' " + emsg += "section in your config file are correct..." + self.end_diag_fail(emsg) + #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: @@ -430,6 +483,9 @@ def __init__(self, config_file, debug=False): #NOTE: this is based off the current CAM file name structure in the form: # $CASE.cam.h#.YYYY.nc case_climo_yrs = [int(str(i).partition(f"{hist_str}.")[2][0:4]) for i in file_list] + if not case_climo_yrs: + msg = f"No climo years found in {cam_hist_locs[case_idx]}, " + raise AdfError(msg) case_climo_yrs = sorted(np.unique(case_climo_yrs)) case_found_syr = int(case_climo_yrs[0]) @@ -802,7 +858,7 @@ def get_climo_yrs_from_ts(self, input_ts_loc, case_name): break else: logmsg = "get years for time series:" - logmsg = f"\tVar '{var}' not in dataset, skip to next to try and find climo years..." + logmsg = f"\n\tVar '{var}' not in dataset, skip to next to try and find climo years..." self.debug_log(logmsg) #Read in file(s)