Skip to content

Commit

Permalink
Merge branch 'main' into call_mdtf_from_adf.20240612
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-richling authored Jun 18, 2024
2 parents 6c7c779 + 5fb3350 commit 3c541f2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
32 changes: 26 additions & 6 deletions lib/adf_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,17 @@ def __init__(self, config_file, debug=False):
else:
#If not a list, then convert it to one:
self.__cam_climo_info[conf_var] = [conf_val]
# End if
# End for
# -------------------------------------------
#End if
#End for
#-------------------------------------------

#Read hist_str (component.hist_num) from the yaml file, or set to default
hist_str = self.__cam_climo_info('hist_str')
#If hist_str is not present, then default to 'cam.h0':
if not hist_str:
hist_str = [['cam.h0']]*self.__num_cases
#End if
self.__hist_str = hist_str

#Initialize ADF variable list:
self.__diag_var_list = self.read_config_var('diag_var_list', required=True)
Expand Down Expand Up @@ -328,7 +336,7 @@ def __init__(self, config_file, debug=False):
cam_hist_locs = self.get_cam_info('cam_hist_loc')

# Read hist_str (component.hist_num, eg cam.h0) from the yaml file
cam_hist_str = self.get_cam_info("hist_str")
cam_hist_str = self.__hist_str

#Check if using pre-made ts files
cam_ts_done = self.get_cam_info("cam_ts_done")
Expand Down Expand Up @@ -439,14 +447,20 @@ def __init__(self, config_file, debug=False):

#Set the final directory name and save it to plot_location:
direc_name = f"{case_name}_vs_{data_name}"
self.__plot_location.append(os.path.join(plot_dir, direc_name))
plot_loc = os.path.join(plot_dir, direc_name)
self.__plot_location.append(plot_loc)

#If first iteration, then save directory name for use by baseline:
first_case_dir = ''
if case_idx == 0:
first_case_dir = direc_name
#End if

#Go ahead and make the diag plot location if it doesn't exist already
diag_location = Path(plot_loc)
if not diag_location.is_dir():
print(f"\t {diag_location} not found, making new directory")
diag_location.mkdir(parents=True)
#End for

self.__syears = syears_fixed
Expand Down Expand Up @@ -615,6 +629,11 @@ def case_nicknames(self):

return {"test_nicknames":test_nicknames,"base_nickname":base_nickname}

@property
def hist_string(self):
""" Return the history string name to the user if requested."""
return self.__hist_str

#########

#Utility function to access expanded 'diag_basic_info' variables:
Expand Down Expand Up @@ -753,7 +772,8 @@ def get_climo_yrs_from_ts(self, input_ts_loc, case_name):
time = cam_ts_data['time']
#NOTE: force `load` here b/c if dask & time is cftime,
#throws a NotImplementedError:
time = xr.DataArray(cam_ts_data['time_bnds'].load().mean(dim='nbnd').values, dims=time.dims, attrs=time.attrs)
time = xr.DataArray(cam_ts_data['time_bnds'].load().mean(dim='nbnd').values,
dims=time.dims, attrs=time.attrs)
cam_ts_data['time'] = time
cam_ts_data.assign_coords(time=time)
cam_ts_data = xr.decode_cf(cam_ts_data)
Expand Down
4 changes: 0 additions & 4 deletions scripts/analysis/amwg_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ def amwg_table(adf):
raise AdfError(errmsg)
#Write to debug log if enabled:
adf.debug_log(f"DEBUG: location of files is {str(input_location)}")
#Check if analysis directory exists, and if not, then create it:
if not output_location.is_dir():
print(f"\t {output_locs[case_idx]} not found, making new directory")
output_location.mkdir(parents=True)

#Create output file name:
output_csv_file = output_location / f"amwg_table_{case_name}.csv"
Expand Down
45 changes: 29 additions & 16 deletions scripts/plotting/tape_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def tape_recorder(adfobj):
plot_location = adfobj.plot_location
plot_loc = Path(plot_location[0])

#Grab history string:
hist_str = adfobj.hist_string

#Grab test case name(s)
case_names = adfobj.get_cam_info('cam_case_name', required=True)

Expand All @@ -47,9 +50,7 @@ def tape_recorder(adfobj):
end_years = adfobj.climo_yrs["eyears"]

#Grab test case nickname(s)
test_nicknames = adfobj.get_cam_info('case_nickname')
if test_nicknames == None:
test_nicknames = case_names
test_nicknames = adfobj.case_nicknames['test_nicknames']

# CAUTION:
# "data" here refers to either obs or a baseline simulation,
Expand All @@ -64,9 +65,7 @@ def tape_recorder(adfobj):
data_ts_loc = adfobj.get_baseline_info("cam_ts_loc", required=True)
case_ts_locs = case_ts_locs+[data_ts_loc]

base_nickname = adfobj.get_baseline_info('case_nickname')
if base_nickname == None:
base_nickname = data_name
base_nickname = adfobj.case_nicknames['base_nickname']
test_nicknames = test_nicknames+[base_nickname]

data_start_year = adfobj.climo_yrs["syear_baseline"]
Expand All @@ -88,17 +87,24 @@ def tape_recorder(adfobj):
# check if existing plots need to be redone
redo_plot = adfobj.get_basic_info('redo_plot')
print(f"\t NOTE: redo_plot is set to {redo_plot}")

#Set location for observations
obs_loc = Path(adfobj.get_basic_info("obs_data_loc"))
#-----------------------------------------

#Set var to Q for now
#TODO: add option to look for H2O if Q is not available, and vice-versa
var = "Q"

#This may have to change if other variables are desired in this plot type?
plot_name = plot_loc / f"Q_TapeRecorder_ANN_Special_Mean.{plot_type}"
print(f"\t - Plotting annual tape recorder for Q")
plot_name = plot_loc / f"{var}_TapeRecorder_ANN_Special_Mean.{plot_type}"
print(f"\t - Plotting annual tape recorder for {var}")

# Check redo_plot. If set to True: remove old plot, if it already exists:
if (not redo_plot) and plot_name.is_file():
#Add already-existing plot to website (if enabled):
adfobj.debug_log(f"'{plot_name}' exists and clobber is false.")
adfobj.add_website_data(plot_name, "Q_TapeRecorder", None, season="ANN", multi_case=True)
adfobj.add_website_data(plot_name, f"{var}_TapeRecorder", None, season="ANN", multi_case=True)
return

elif (redo_plot) and plot_name.is_file():
Expand All @@ -110,7 +116,7 @@ def tape_recorder(adfobj):
runs_LT2[val] = case_ts_locs[i]

# MLS data
mls = xr.open_dataset("/glade/campaign/cgd/cas/islas/CAM7validation/MLS/mls_h2o_latNpressNtime_3d_monthly_v5.nc")
mls = xr.open_dataset(obs_loc / "mls_h2o_latNpressNtime_3d_monthly_v5.nc")
mls = mls.rename(x='lat', y='lev', t='time')
time = pd.date_range("2004-09","2021-11",freq='M')
mls['time'] = time
Expand All @@ -120,17 +126,22 @@ def tape_recorder(adfobj):
mls = mls*18.015280/(1e6*28.964)

# ERA5 data
era5 = xr.open_dataset("/glade/campaign/cgd/cas/islas/CAM7validation/ERA5/ERA5_Q_10Sto10N_1980to2020.nc")
era5 = xr.open_dataset(obs_loc / "ERA5_Q_10Sto10N_1980to2020.nc")
era5 = era5.groupby('time.month').mean('time')
era5_data = era5.Q

alldat=[]
runname_LT=[]
for idx,key in enumerate(runs_LT2):
fils= sorted(Path(runs_LT2[key]).glob('*h0.Q.*.nc'))
fils= sorted(Path(runs_LT2[key]).glob(f'*{hist_str}.{var}.*.nc'))
dat = pf.load_dataset(fils)
if not dat:
dmsg = f"No data for `{var}` found in {fils}, case will be skipped in tape recorder plot."
print(dmsg)
continue
dat = fixcesmtime(dat,start_years[idx],end_years[idx])
datzm = dat.mean('lon')
dat_tropics = cosweightlat(datzm.Q, -10, 10)
dat_tropics = cosweightlat(datzm[var], -10, 10)
dat_mon = dat_tropics.groupby('time.month').mean('time').load()
alldat.append(dat_mon)
runname_LT.append(key)
Expand All @@ -149,10 +160,12 @@ def tape_recorder(adfobj):
x1[0],x2[0],y1[0],y2[0],cmap=cmap, paxis='lev',
taxis='month',climo_yrs="2004-2021")

ax = plot_pre_mon(fig, era5.Q, plot_step,plot_min,plot_max,
ax = plot_pre_mon(fig, era5_data, plot_step,plot_min,plot_max,
'ERA5',x1[1],x2[1],y1[1],y2[1], cmap=cmap, paxis='pre',
taxis='month',climo_yrs="1980-2020")



#Start count at 2 to account for MLS and ERA5 plots above
count=2
for irun in np.arange(0,alldat_concat_LT.run.size,1):
Expand Down Expand Up @@ -181,15 +194,15 @@ def tape_recorder(adfobj):
y1_loc = y1[count]-0.03
y2_loc = y1[count]-0.02

ax = plotcolorbar(fig, plot_step, plot_min, plot_max, 'Q (kg/kg)',
ax = plotcolorbar(fig, plot_step, plot_min, plot_max, f'{var} (kg/kg)',
x1_loc, x2_loc, y1_loc, y2_loc,
cmap=cmap)

#Save image
fig.savefig(plot_name, bbox_inches='tight', facecolor='white')

#Add plot to website (if enabled):
adfobj.add_website_data(plot_name, "Q_TapeRecorder", None, season="ANN", multi_case=True)
adfobj.add_website_data(plot_name, f"{var}_TapeRecorder", None, season="ANN", multi_case=True)

#Notify user that script has ended:
print(" ...Tape recorder plots have been generated successfully.")
Expand Down

0 comments on commit 3c541f2

Please sign in to comment.