Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more robust error handling and notifications #344

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions lib/adf_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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<other date info>.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])
Expand Down Expand Up @@ -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:
Expand All @@ -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<other date info>.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])
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions scripts/analysis/amwg_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ def amwg_table(adf):
# VARIABLE-NAME, RUN VALUE, OBS VALUE, RUN-OBS, RMSE
#----------------------

#Notify user that script has started:
print("\n Calculating AMWG variable table...")


#Extract needed quantities from ADF object:
#-----------------------------------------
var_list = adf.diag_var_list
Expand Down Expand Up @@ -167,6 +163,9 @@ def amwg_table(adf):
#Write to debug log if enabled:
adf.debug_log(f"DEBUG: location of files is {str(input_location)}")

#Notify user that script has started:
print(f"\n Calculating AMWG variable table for '{case_name}'...")

#Create output file name:
output_csv_file = output_location / f"amwg_table_{case_name}.csv"

Expand Down Expand Up @@ -216,7 +215,7 @@ def amwg_table(adf):

#Check if variable has a vertical coordinate:
if 'lev' in data.coords or 'ilev' in data.coords:
print(f"\t Variable '{var}' has a vertical dimension, "+\
print(f"\t ** Variable '{var}' has a vertical dimension, "+\
"which is currently not supported for the AMWG Table. Skipping...")
#Skip this variable and move to the next variable in var_list:
continue
Expand Down
Loading