Skip to content

Commit

Permalink
Clean up print statements to align consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-richling committed Feb 4, 2025
1 parent 7045c81 commit 0fc138d
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 83 deletions.
7 changes: 5 additions & 2 deletions lib/adf_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def load_timeseries_da(self, case, variablename):
"""
add_offset, scale_factor = self.get_value_converters(case, variablename)
fils = self.get_timeseries_file(case, variablename)
if not fils:
warnings.warn(f"\t WARNING: Did not find case time series file(s), variable: {variablename}")
return None
return self.load_da(fils, variablename, add_offset=add_offset, scale_factor=scale_factor)

def load_reference_timeseries_da(self, field):
Expand All @@ -157,7 +160,7 @@ def load_reference_timeseries_da(self, field):
"""
fils = self.get_ref_timeseries_file(field)
if not fils:
warnings.warn(f"\t WARNING: Did not find time series file(s), variable: {field}")
warnings.warn(f"\t WARNING: Did not find reference time series file(s), variable: {field}")
return None
#Change the variable name from CAM standard to what is
# listed in variable defaults for this observation field
Expand Down Expand Up @@ -363,4 +366,4 @@ def get_value_converters(self, case, variablename):





48 changes: 30 additions & 18 deletions lib/adf_diag.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,25 @@ def call_ncrcat(cmd):
# Notify user of new time series file:
print(f"\t - time series for {var}")

# Create full path name, file name template:
# $cam_case_name.$hist_str.$variable.YYYYMM-YYYYMM.nc
ts_outfil_str = (
ts_dir
+ os.sep
+ ".".join([case_name, hist_str, var, time_string, "nc"])
)

# Check if clobber is true for file
if Path(ts_outfil_str).is_file():
if overwrite_ts[case_idx]:
Path(ts_outfil_str).unlink()
else:
#msg = f"[{__name__}] Warning: '{var}' file was found "
msg = f"\t INFO: '{var}' file was found "
msg += "and overwrite is False. Will use existing file."
print(msg)
continue

# Set error messages for printing/debugging
# Derived variable, but missing constituent list
constit_errmsg = f"create time series for {case_name}:"
Expand Down Expand Up @@ -622,26 +641,19 @@ def call_ncrcat(cmd):
# Lastly, raise error if the variable is not a derived quanitity
# but is also not in the history file(s)
else:
msg = f"\t WARNING: {var} is not in the file {hist_files[0]} "
msg += "nor can it be derived.\n"
msg += "\t ** No time series will be generated."
msg = f"\t WARNING: {var} is not in the history file for case '{case_name}' "
msg += "nor can it be derived. Script will continue to next variable."
print(msg)
logmsg = f"create time series for {case_name}:"
logmsg += f"\n {var} is not in the file {hist_files[0]} "
self.debug_log(logmsg)
continue
# End if
# End if (var in var_diag_list)

# Check if variable has a "lev" dimension according to first file:
has_lev = bool("lev" in hist_file_ds[var].dims or "ilev" in hist_file_ds[var].dims)

# Create full path name, file name template:
# $cam_case_name.$hist_str.$variable.YYYYMM-YYYYMM.nc

ts_outfil_str = (
ts_dir
+ os.sep
+ ".".join([case_name, hist_str, var, time_string, "nc"])
)

# Check if files already exist in time series directory:
ts_file_list = glob.glob(ts_outfil_str)

Expand Down Expand Up @@ -1167,7 +1179,7 @@ def derive_variables(self, res=None, hist_str=None, vars_to_derive=None, ts_dir=

# Check if all the necessary constituent files were found
if len(constit_files) != len(constit_list):
ermsg = f"\t ** Not all constituent files present; {var} cannot be calculated."
ermsg = f"\t WARNING: Not all constituent files present; {var} cannot be calculated."
ermsg += f" Please remove {var} from 'diag_var_list' or find the "
ermsg += "relevant CAM files.\n"
print(ermsg)
Expand Down Expand Up @@ -1201,7 +1213,7 @@ def derive_variables(self, res=None, hist_str=None, vars_to_derive=None, ts_dir=
if overwrite:
Path(derived_file).unlink()
else:
msg = f"[{__name__}] Warning: '{var}' file was found "
msg = f"\t INFO: '{var}' file was found "
msg += "and overwrite is False. Will use existing file."
print(msg)
continue
Expand Down Expand Up @@ -1233,19 +1245,19 @@ def derive_variables(self, res=None, hist_str=None, vars_to_derive=None, ts_dir=
ds_pmid = _load_dataset(glob.glob(os.path.join(ts_dir, "*.PMID.*"))[0])
ds_pmid_done = True
if not ds_pmid:
errmsg = "Missing necessary files for dry air density"
errmsg = "\t WARNING: Missing necessary files for dry air density"
errmsg += " (rho) calculation.\n"
errmsg += "Please make sure 'PMID' is in the CAM run"
errmsg += "\t Please make sure 'PMID' is in the CAM run"
errmsg += " for aerosol calculations"
print(errmsg)
continue
if not ds_t_done:
ds_t = _load_dataset(glob.glob(os.path.join(ts_dir, "*.T.*"))[0])
ds_t_done = True
if not ds_t:
errmsg = "Missing necessary files for dry air density"
errmsg = "\t WARNING: Missing necessary files for dry air density"
errmsg += " (rho) calculation.\n"
errmsg += "Please make sure 'T' is in the CAM run"
errmsg += "\t Please make sure 'T' is in the CAM run"
errmsg += " for aerosol calculations"
print(errmsg)
continue
Expand Down
2 changes: 1 addition & 1 deletion scripts/analysis/aerosol_gas_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def aerosol_gas_tables(adfobj):

#Notify user that script has started:
msg = "\n Calculating chemistry/aerosol budget tables..."
print(f"{msg}\n{'-' * len(msg)}")
print(f"{msg}\n {'-' * (len(msg)-3)}")

# Inputs
#-------
Expand Down
12 changes: 8 additions & 4 deletions scripts/averaging/create_climo_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def create_climo_files(adf, clobber=False, search=None):
# and check whether it is there (don't do computation if we don't want to overwrite):
output_file = output_location / f"{case_name}_{var}_climo.nc"
if (not clobber) and (output_file.is_file()):
print(f"\t INFO: Found climo file and clobber is False, so skipping {var} and moving to next variable.")
msg = f"\t INFO: '{var}' file was found "
msg += "and overwrite is False. Will use existing file."
print(msg)
continue
elif (clobber) and (output_file.is_file()):
print(f"\t INFO: Climo file exists for {var}, but clobber is {clobber}, so will OVERWRITE it.")
Expand All @@ -175,10 +177,12 @@ def create_climo_files(adf, clobber=False, search=None):
#If no files exist, try to move to next variable. --> Means we can not proceed with this variable,
# and it'll be problematic later unless there are multiple hist file streams and the variable is in the others
if not ts_files:
errmsg = f"\t **Time series files for variable '{var}' not found. Script will continue to next variable."
print(f"\t The input location searched was: {input_location}. The glob pattern was {ts_filenames}.")
errmsg = f"\t WARNING: Time series files for variable '{var}' not found. Script will continue to next variable."
print(errmsg)
logmsg = f"climo file generation: The input location searched was: {input_location}. The glob pattern was {ts_filenames}."
#Write to debug log if enabled:
adf.debug_log(logmsg)
# end_diag_script(errmsg) # Previously we would kill the run here.
warnings.warn(errmsg)
continue

list_of_arguments.append((adf, ts_files, syr, eyr, output_file))
Expand Down
10 changes: 5 additions & 5 deletions scripts/plotting/global_latlon_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ def global_latlon_map(adfobj):

# probably want to do this one variable at a time:
for var in var_list:
#Notify user of variable being plotted:
print(f"\t - lat/lon maps for {var}")

if var not in adfobj.data.ref_var_nam:
dmsg = f"\t WARNING: No reference data found for variable `{var}`, global lat/lon mean plotting skipped."
adfobj.debug_log(dmsg)
print(dmsg)
continue

#Notify user of variable being plotted:
print(f"\t - lat/lon maps for {var}")
continue

# Check res for any variable specific options that need to be used BEFORE going to the plot:
if var in res:
Expand Down Expand Up @@ -921,4 +921,4 @@ def regrid_to_obs(adfobj, model_arr, obs_arr):
#######

##############
#END OF SCRIPT
#END OF SCRIPT
22 changes: 15 additions & 7 deletions scripts/plotting/global_latlon_vect_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ def global_latlon_vect_map(adfobj):
continue
#End if

#Notify user of variable being plotted:
print(f"\t - lat/lon vector maps for {var},{var_pair}")

if var not in adfobj.data.ref_var_nam:
dmsg = f"\t WARNING: No reference data found for variable `{var}`, lat/lon vector map plotting skipped."
adfobj.debug_log(dmsg)
print(dmsg)
continue

#Add variables to "skipped vars" set:
skip_vars.add(var)
skip_vars.add(var_pair)
Expand All @@ -189,8 +198,9 @@ def global_latlon_vect_map(adfobj):
#Extract target variable name:
data_var = [var_obs_dict[var]["obs_var"]]
else:
dmsg = f"\t WARNING: No obs found for variable `{var}`, lat/lon vector map plotting skipped."
dmsg = f"\t WARNING: No reference found for variable `{var}`, lat/lon vector map plotting skipped."
adfobj.debug_log(dmsg)
print(dmsg)
continue
#End if
#Check if obs exist for vector pair variable:
Expand All @@ -202,8 +212,9 @@ def global_latlon_vect_map(adfobj):
#Extract target variable name:
data_var.append(var_obs_dict[var_pair]["obs_var"])
else:
dmsg = f"\t WARNING: No obs found for variable `{var}`, lat/lon vector map plotting skipped."
dmsg = f"\t WARNING: No reference found for variable `{var}`, lat/lon vector map plotting skipped."
adfobj.debug_log(dmsg)
print(dmsg)
continue
#End if

Expand All @@ -215,9 +226,6 @@ def global_latlon_vect_map(adfobj):
base_name = adfobj.data.ref_case_label
#End if

#Notify user of variable being plotted:
print(f"\t - lat/lon vector maps for {var},{var_pair}")

#loop over different data sets to plot model against:
for data_src in data_list:

Expand All @@ -237,7 +245,7 @@ def global_latlon_vect_map(adfobj):
sfil = str(uoclim_fils[0])
uoclim_ds = xr.open_dataset(sfil)
else:
print("\t ERROR: Did not find any oclim_fils. Will try to skip.")
print("\t WARNING: Did not find any oclim_fils. Will try to skip.")
print(f"\t INFO: Data Location, dclimo_loc is {dclimo_loc}")
print(f"\t INFO: The glob is: {data_src}_{data_var[0]}_*.nc")
continue
Expand All @@ -249,7 +257,7 @@ def global_latlon_vect_map(adfobj):
sfil = str(voclim_fils[0])
voclim_ds = xr.open_dataset(sfil)
else:
print("\t ERROR: Did not find any oclim_fils. Will try to skip.")
print("\t WARNING: Did not find any oclim_fils. Will try to skip.")
print(f"\t INFO: Data Location, dclimo_loc is {dclimo_loc}")
print(f"\t INFO: The glob is: {data_src}_{data_var[1]}_*.nc")
continue
Expand Down
34 changes: 19 additions & 15 deletions scripts/plotting/global_mean_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ def global_mean_timeseries(adfobj):

# Check to see if this field is available
if ref_ts_da is None:
print(
f"\t WARNING: Variable {field} for case '{base_name}' provides Nonetype. Skipping this variable"
)
if not adfobj.compare_obs:
print(
f"\t WARNING: Variable {field} for case '{base_name}' provides Nonetype. Skipping this variable"
)
continue
else:
# check data dimensions:
valdims = pf.zm_validate_dims(ref_ts_da)
Expand Down Expand Up @@ -125,16 +127,6 @@ def global_mean_timeseries(adfobj):

skip_var = False
for case_idx,case_name in enumerate(adfobj.data.case_names):
## SPECIAL SECTION -- CESM2 LENS DATA:
#Check if case years are close to LENS, if not don't plot the LENS data
if (syear_cases[case_idx] > 1800) and ((syear_baseline > 1800) or (adfobj.compare_obs)):
lens2_data = Lens2Data(
field
) # Provides access to LENS2 dataset when available (class defined below)
else:
print(f"\t ** Some model years for {field} are outside LENS years, will skip plotting LENS data for clarity")
lens2_data = None
# End if - LENS

c_ts_da = adfobj.data.load_timeseries_da(case_name, field)

Expand Down Expand Up @@ -170,6 +162,7 @@ def global_mean_timeseries(adfobj):
print(
f"\t WARNING: Variable {field} is missing a lat dimension for '{case_name}', cannot continue to plot."
)
skip_var = True
continue
# End if

Expand All @@ -181,6 +174,17 @@ def global_mean_timeseries(adfobj):
if skip_var:
continue

## SPECIAL SECTION -- CESM2 LENS DATA:
#Check if case years are close to LENS, if not don't plot the LENS data
if (syear_cases[case_idx] > 1800) and ((syear_baseline > 1800) or (adfobj.compare_obs)):
lens2_data = Lens2Data(
field
) # Provides access to LENS2 dataset when available (class defined below)
else:
print(f"\t ** Some model years for {field} are outside LENS years, will skip plotting LENS data for clarity")
lens2_data = None
# End if - LENS

# Plot the timeseries
fig, ax = make_plot(
field, case_ts, lens2_data, label=adfobj.data.ref_nickname, ref_ts_da=ref_ts_da
Expand Down Expand Up @@ -280,7 +284,7 @@ def _include_lens(self):
lens2 = xr.open_mfdataset(lens2_fil)
has_lens = True
else:
warnings.warn(f"\t WARNING: Did not find LENS2 file for {self.field}.")
warnings.warn(f"\t INFO: Did not find LENS2 file for {self.field}.")
has_lens = False
lens2 = None
return has_lens, lens2
Expand Down Expand Up @@ -335,4 +339,4 @@ def make_plot(field, case_ts, lens2=None, label=None, ref_ts_da=None):


##############
#END OF SCRIPT
#END OF SCRIPT
5 changes: 2 additions & 3 deletions scripts/plotting/meridional_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def meridional_mean(adfobj):

#Loop over variables:
for var in var_list:
#Notify user of variable being plotted:
print(f"\t - meridional mean plots for {var}")

if adfobj.compare_obs:
#Check if obs exist for the variable:
Expand All @@ -125,9 +127,6 @@ def meridional_mean(adfobj):
data_var = var
#End if

#Notify user of variable being plotted:
print(f"\t - meridional mean plots for {var}")

# Check res for any variable specific options that need to be used BEFORE going to the plot:
if var in res:
vres = res[var]
Expand Down
11 changes: 8 additions & 3 deletions scripts/plotting/polar_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ def polar_map(adfobj):

# probably want to do this one variable at a time:
for var in var_list:
#Notify user of variable being plotted:
print(f"\t - polar maps for {var}")

if var not in adfobj.data.ref_var_nam:
dmsg = f"\t WARNING: No reference data found for variable `{var}`, polar lat/lon mean plotting skipped."
adfobj.debug_log(dmsg)
print(dmsg)
continue

if adfobj.compare_obs:
#Check if obs exist for the variable:
Expand All @@ -125,9 +133,6 @@ def polar_map(adfobj):
data_var = var
#End if

#Notify user of variable being plotted:
print(f"\t - polar maps for {var}")

# Check res for any variable specific options that need to be used BEFORE going to the plot:
if var in res:
vres = res[var]
Expand Down
10 changes: 10 additions & 0 deletions scripts/plotting/qbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ def qbo(adfobj):
print(f"\t WARNING: Variable U is missing a lat dimension for '{case_loc[i]}', cannot continue to plot.")
else:
casedatzm.append(casedat[i].U.mean("lon"))
if len(casedatzm) == 0:
print(f"\t WARNING: No available cases found, exiting script.")
exitmsg = "\tNo QBO plots will be made."
print(exitmsg)
return
if len(casedatzm) != ncases:
print(f"\t WARNING: Number of available cases does not match number of cases. Will exit script for now.")
exitmsg = "\tNo QBO plots will be made."
print(exitmsg)
return

#----Calculate the 5S-5N average
casedat_5S_5N = [ cosweightlat(casedatzm[i],-5,5) for i in range(0,ncases,1) ]
Expand Down
Loading

0 comments on commit 0fc138d

Please sign in to comment.