Skip to content

Commit c1d9b8d

Browse files
Fix different vert level conflict when plotting
Remove this code because if the cases have different number of vertical levels, then the concat datasets will cause problems (they will combine the levels into the new lev dim). ```python #Check to see if any cases were successful if runname_LT: runname_LT=xr.DataArray(runname_LT, dims='run', coords=[np.arange(0,len(runname_LT),1)], name='run') ``` Also clean up code for readability, and change data read in (plus CESM time fix) to new data class method.
1 parent 1393004 commit c1d9b8d

File tree

1 file changed

+38
-53
lines changed

1 file changed

+38
-53
lines changed

scripts/plotting/tape_recorder.py

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def tape_recorder(adfobj):
8989
# Filter the list to include only strings that are exactly in the substrings list
9090
base_hist_strs = [string for string in baseline_hist_strs if string in substrings]
9191
hist_strs = case_hist_strs + base_hist_strs
92+
else:
93+
hist_strs = case_hist_strs
9294
#End if
9395

9496
if not case_ts_locs:
@@ -126,6 +128,7 @@ def tape_recorder(adfobj):
126128

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

131134
# Check redo_plot. If set to True: remove old plot, if it already exists:
@@ -138,11 +141,8 @@ def tape_recorder(adfobj):
138141
elif (redo_plot) and plot_name.is_file():
139142
plot_name.unlink()
140143

141-
#Make dictionary for case names and associated timeseries file locations and hist strings
142-
runs_LT2={}
143-
for i,val in enumerate(test_nicknames):
144-
runs_LT2[val] = [case_ts_locs[i], hist_strs[i]]
145-
144+
# Plotting
145+
#---------
146146
# MLS data
147147
mls = xr.open_dataset(obs_loc / "mls_h2o_latNpressNtime_3d_monthly_v5.nc")
148148
mls = mls.rename(x='lat', y='lev', t='time')
@@ -158,38 +158,7 @@ def tape_recorder(adfobj):
158158
era5 = era5.groupby('time.month').mean('time')
159159
era5_data = era5.Q
160160

161-
alldat=[]
162-
runname_LT=[]
163-
for idx,key in enumerate(runs_LT2):
164-
# Search for files
165-
ts_loc = Path(runs_LT2[key][0])
166-
hist_str = runs_LT2[key][1]
167-
fils= sorted(ts_loc.glob(f'*{hist_str}.{var}.*.nc'))
168-
dat = pf.load_dataset(fils)
169-
if not dat:
170-
dmsg = f"\t No data for `{var}` found in {fils}, case will be skipped in tape recorder plot."
171-
print(dmsg)
172-
adfobj.debug_log(dmsg)
173-
continue
174-
dat = fixcesmtime(dat,start_years[idx],end_years[idx])
175-
datzm = dat.mean('lon')
176-
dat_tropics = cosweightlat(datzm[var], -10, 10)
177-
dat_mon = dat_tropics.groupby('time.month').mean('time').load()
178-
alldat.append(dat_mon)
179-
runname_LT.append(key)
180-
181-
#Check to see if any cases were successful
182-
if runname_LT:
183-
runname_LT=xr.DataArray(runname_LT, dims='run', coords=[np.arange(0,len(runname_LT),1)], name='run')
184-
alldat_concat_LT = xr.concat(alldat, dim=runname_LT)
185-
else:
186-
msg = f"WARNING: No cases seem to be available, please check time series files for {var}."
187-
msg += "\n\tNo tape recorder plots will be made."
188-
print(msg)
189-
#End tape recorder plotting script:
190-
return
191-
192-
#Set up figure for plots
161+
#Set up figure and plot MLS and ERA5 data
193162
fig = plt.figure(figsize=(16,16))
194163
x1, x2, y1, y2 = get5by5coords_zmplots()
195164

@@ -205,15 +174,42 @@ def tape_recorder(adfobj):
205174
'ERA5',x1[1],x2[1],y1[1],y2[1], cmap=cmap, paxis='pre',
206175
taxis='month',climo_yrs="1980-2020")
207176

177+
208178
#Loop over case(s) and start count at 2 to account for MLS and ERA5 plots above
179+
runname_LT=[]
209180
count=2
210-
for irun in np.arange(0,alldat_concat_LT.run.size,1):
211-
title = f"{alldat_concat_LT.run.isel(run=irun).values}"
212-
ax = plot_pre_mon(fig, alldat_concat_LT.isel(run=irun),
213-
plot_step, plot_min, plot_max, title,
181+
for idx,key in enumerate(test_nicknames):
182+
# Search for files
183+
ts_loc = Path(case_ts_locs[idx])
184+
hist_str = hist_strs[idx]
185+
fils = sorted(ts_loc.glob(f'*{hist_str}.{var}.*.nc'))
186+
dat = adfobj.data.load_timeseries_dataset(fils)
187+
188+
if not dat:
189+
dmsg = f"\t No data for `{var}` found in {fils}, case will be skipped in tape recorder plot."
190+
print(dmsg)
191+
adfobj.debug_log(dmsg)
192+
continue
193+
194+
#Grab time slice based on requested years (if applicable)
195+
dat = dat.sel(time=slice(str(start_years[idx]).zfill(4),str(end_years[idx]).zfill(4)))
196+
datzm = dat.mean('lon')
197+
dat_tropics = cosweightlat(datzm[var], -10, 10)
198+
dat_mon = dat_tropics.groupby('time.month').mean('time').load()
199+
ax = plot_pre_mon(fig, dat_mon,
200+
plot_step, plot_min, plot_max, key,
214201
x1[count],x2[count],y1[count],y2[count],cmap=cmap, paxis='lev',
215-
taxis='month',climo_yrs=f"{start_years[irun]}-{end_years[irun]}")
202+
taxis='month',climo_yrs=f"{start_years[idx]}-{end_years[idx]}")
216203
count=count+1
204+
runname_LT.append(key)
205+
206+
#Check to see if any cases were successful
207+
if not runname_LT:
208+
msg = f"WARNING: No cases seem to be available, please check time series files for {var}."
209+
msg += "\n\tNo tape recorder plots will be made."
210+
print(msg)
211+
#End tape recorder plotting script:
212+
return
217213

218214
#Shift colorbar if there are less than 5 subplots
219215
# There will always be at least 2 (MLS and ERA5)
@@ -348,17 +344,6 @@ def precip_cmap(n, nowhite=False):
348344

349345
#########
350346

351-
def fixcesmtime(dat,syear,eyear):
352-
"""
353-
Fix the CESM timestamp with a simple set of dates
354-
"""
355-
timefix = pd.date_range(start=f'1/1/{syear}', end=f'12/1/{eyear}', freq='MS') # generic time coordinate from a non-leap-year
356-
dat = dat.assign_coords({"time":timefix})
357-
358-
return dat
359-
360-
#########
361-
362347
def get5by5coords_zmplots():
363348
"""
364349
positioning for 5x5 plots

0 commit comments

Comments
 (0)