Skip to content

Commit

Permalink
Update regrid_and_vert_interp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-richling authored Feb 18, 2025
1 parent b1f2bd7 commit b4b35d5
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions scripts/regridding/regrid_and_vert_interp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#Import standard modules:
import xarray as xr

#adf.set_warning_filter
from adf_diag import set_warning_filter
#set_warning_filter(enable=True) # Suppress warnings
def regrid_and_vert_interp(adf):

"""
Expand Down Expand Up @@ -58,7 +60,7 @@ def regrid_and_vert_interp(adf):

#CAM simulation variables (these quantities are always lists):
case_names = adf.get_cam_info("cam_case_name", required=True)
input_climo_locs = adf.get_cam_info("cam_climo_loc", required=True)
input_climo_locs = adf.climo_locs["test"]

#Grab case years
syear_cases = adf.climo_yrs["syears"]
Expand Down Expand Up @@ -112,7 +114,7 @@ def regrid_and_vert_interp(adf):
else:

#Extract model baseline variables:
target_loc = adf.get_baseline_info("cam_climo_loc", required=True)
target_loc = adf.climo_locs["baseline"]
target_list = [adf.get_baseline_info("cam_case_name", required=True)]
#End if

Expand Down Expand Up @@ -142,7 +144,7 @@ def regrid_and_vert_interp(adf):
for case_idx, case_name in enumerate(case_names):

#Notify user of model case being processed:
print(f"\t Regridding case '{case_name}' :")
print(f"\n\t Regridding case '{case_name}' :")

#Set case climo data path:
mclimo_loc = Path(input_climo_locs[case_idx])
Expand Down Expand Up @@ -233,8 +235,6 @@ def regrid_and_vert_interp(adf):
#Combine all cam files together into a single data set:
mclim_ds = xr.open_mfdataset(mclim_fils, combine='by_coords')
elif len(mclim_fils) == 0:
#wmsg = f"\t WARNING: Unable to find climo file for '{var}'."
#wmsg += " Continuing to next variable."
wmsg= f"\t WARNING: regridding {var} failed, no climo file for case '{case_name}'. Continuing to next variable."
print(wmsg)
continue
Expand Down Expand Up @@ -282,7 +282,7 @@ def regrid_and_vert_interp(adf):
#End if
else:
#Currently only an ocean mask is supported, so print warning here:
wmsg = "\t WARNING: Currently the only variable mask option is 'ocean',"
wmsg = "\t INFO: Currently the only variable mask option is 'ocean',"
wmsg += f"not '{var_default_dict['mask'].lower()}'"
print(wmsg)
#End if
Expand All @@ -297,8 +297,17 @@ def regrid_and_vert_interp(adf):
#Convert the list of Path objects to a list of strings
climatology_files_str = [str(path) for path in mclim_fils]
climatology_files_str = ', '.join(climatology_files_str)

#Interpolate from hybrid sigma-pressure to the standard pressure levels:
#pf.lev_to_plev -> geocat.comp.interpolation.interp_hybrid_to_pressure

#Interpolate variable using mid-level pressure (PMID):
#pf.pmid_to_plev -> numpy.interp
vert_regrid_info = "hybrid sigma-pressure to the standard pressure levels"
test_attrs_dict = {
"adf_user": adf.user,
"adf_horizontal_regrid_info": "Climatology files have been regridded in the horizontal using xarray's 'interp_like'",
"adf_vertical_regrid_info": "Climatology files have been interpolated in the vertical using numpy's 'interp'. ",
"climo_yrs": f"{case_name}: {syear}-{eyear}",
"climatology_files": climatology_files_str,
}
Expand Down Expand Up @@ -374,6 +383,7 @@ def regrid_and_vert_interp(adf):
# Create a dictionary of attributes
base_attrs_dict = {
"adf_user": adf.user,
"adf_horizontal_regrid_info": "Climatology files have been regridded in the horizontal using xarray's 'interp_like'",
"climo_yrs": f"{case_name}: {syear}-{eyear}; {base_climo_yrs_attr}",
"climatology_files": climatology_files_str,
}
Expand Down Expand Up @@ -473,15 +483,15 @@ def _regrid_and_interpolate_levs(model_dataset, var_name, regrid_dataset=None, r
vert_coord_type = "height"
else:
#Print a warning, and skip variable re-gridding/interpolation:
wmsg = "WARNING! Unable to determine the vertical coordinate"
wmsg = "\t WARNING: Unable to determine the vertical coordinate"
wmsg +=f" type from the 'lev' long name, which is:\n'{lev_long_name}'"
print(wmsg)
return None
#End if

else:
#Print a warning, and assume hybrid levels (for now):
wmsg = "WARNING! No long name found for the 'lev' dimension,"
wmsg = "\t WARNING: No long name found for the 'lev' dimension,"
wmsg += f" so no re-gridding/interpolation will be done."
print(wmsg)
return None
Expand All @@ -499,14 +509,14 @@ def _regrid_and_interpolate_levs(model_dataset, var_name, regrid_dataset=None, r
# Need hyam, hybm, and P0 for vertical interpolation of hybrid levels:
if 'lev' in mdata.dims:
if ('hyam' not in model_dataset) or ('hybm' not in model_dataset):
print(f"!! PROBLEM -- NO hyam or hybm for 3-D variable {var_name}, so it will not be re-gridded.")
print(f"\t ERROR: NO hyam or hybm for 3-D variable {var_name}, so it will not be re-gridded.")
return None #Return None to skip to next variable.
#End if
mhya = model_dataset['hyam']
mhyb = model_dataset['hybm']
elif 'ilev' in mdata.dims:
if ('hyai' not in model_dataset) or ('hybi' not in model_dataset):
print(f"!! PROBLEM -- NO hyai or hybi for 3-D variable {var_name}, so it will not be re-gridded.")
print(f"\t ERROR: NO hyai or hybi for 3-D variable {var_name}, so it will not be re-gridded.")
return None #Return None to skip to next variable.
#End if
mhya = model_dataset['hyai']
Expand Down Expand Up @@ -544,7 +554,7 @@ def _regrid_and_interpolate_levs(model_dataset, var_name, regrid_dataset=None, r
#This mid-level pressure field has already been regridded:
regridded_pmid = True
else:
print(f"!! PROBLEM -- NO PMID for 3-D variable {var_name}, so it will not be re-gridded.")
print(f"\t ERROR: NO PMID for 3-D variable {var_name}, so it will not be re-gridded.")
return None
#End if
#End if
Expand All @@ -562,7 +572,7 @@ def _regrid_and_interpolate_levs(model_dataset, var_name, regrid_dataset=None, r
#This surface pressure field has already been regridded:
regridded_ps = True
else:
print(f"!! PROBLEM -- NO PS for 3-D variable {var_name}, so it will not be re-gridded.")
print(f"\t ERROR: NO PS for 3-D variable {var_name}, so it will not be re-gridded.")
return None
#End if
#End if
Expand Down Expand Up @@ -627,7 +637,7 @@ def _regrid_and_interpolate_levs(model_dataset, var_name, regrid_dataset=None, r
else:
#The vertical coordinate type is un-recognized, so print warning and
#skip vertical interpolation:
wmsg = f"WARNING! Un-recognized vertical coordinate type: '{vert_coord_type}',"
wmsg = f"\t WARNING: Un-recognized vertical coordinate type: '{vert_coord_type}',"
wmsg += f" for variable '{var_name}'. Skipping vertical interpolation."
print(wmsg)
#Don't process variable:
Expand Down Expand Up @@ -709,4 +719,4 @@ def regrid_data(fromthis, tothis, method=1):
return result
#End if

#####
#####

0 comments on commit b4b35d5

Please sign in to comment.