Skip to content

Commit

Permalink
Add check for percent diff levels
Browse files Browse the repository at this point in the history
This will add a default contour level range for percent difference if not included in defaults yaml file
  • Loading branch information
justin-richling committed Dec 9, 2024
1 parent f28e669 commit d880a0e
Showing 1 changed file with 16 additions and 35 deletions.
51 changes: 16 additions & 35 deletions lib/plotting_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,8 @@ def make_polar_plot(wks, case_nickname, base_nickname,
assert len(kwargs['pct_diff_contour_range']) == 3, "pct_diff_contour_range must have exactly three entries: min, max, step"
levelspctdiff = np.arange(*kwargs['pct_diff_contour_range'])
else:
# set levels for difference plot (with a symmetric color bar):
levelspctdiff = np.linspace(-1*absmaxpct, absmaxpct, 15)
levelspctdiff = [-100,-75,-50,-40,-30,-20,-10,-8,-6,-4,-2,0,2,4,6,8,10,20,30,40,50,75,100]
pctnorm = mpl.colors.BoundaryNorm(levelspctdiff,256)

#NOTE: Sometimes the contour levels chosen in the defaults file
#can result in the "contourf" software stack generating a
Expand Down Expand Up @@ -773,16 +773,9 @@ def make_polar_plot(wks, case_nickname, base_nickname,

# Pct Difference options -- Check in kwargs for colormap and levels
if "pct_diff_colormap" in kwargs:
cmappct = kwargs["pct_diff_colormap"]
if "pct_diff_contour_levels" in kwargs:
pnorm = mpl.colors.BoundaryNorm(levelspctdiff,256)
else:
pnorm, _ = get_difference_colors(levelspctdiff) # color map output ignored

cmappct = kwargs["pct_diff_colormap"]
else:
pnorm, cmappct = get_difference_colors(levelspctdiff)
if "pct_diff_contour_levels" in kwargs:
pnorm = mpl.colors.BoundaryNorm(levelspctdiff,256)
cmappct = "PuOr_r"
#End if

# -- end options
Expand Down Expand Up @@ -812,10 +805,10 @@ def make_polar_plot(wks, case_nickname, base_nickname,
img2 = ax2.contourf(lons, lats, d2_cyclic, transform=ccrs.PlateCarree(), cmap=cmap1, norm=norm1, levels=levels1)

if len(levs_pctdiff) < 2:
img3 = ax3.contourf(lons, lats, pct_cyclic, transform=ccrs.PlateCarree(), colors="w", norm=pnorm, transform_first=True)
img3 = ax3.contourf(lons, lats, pct_cyclic, transform=ccrs.PlateCarree(), colors="w", norm=pctnorm, transform_first=True)
ax3.text(0.4, 0.4, empty_message, transform=ax3.transAxes, bbox=props)
else:
img3 = ax3.contourf(lons, lats, pct_cyclic, transform=ccrs.PlateCarree(), cmap=cmappct, norm=pnorm, levels=levelspctdiff, transform_first=True)
img3 = ax3.contourf(lons, lats, pct_cyclic, transform=ccrs.PlateCarree(), cmap=cmappct, norm=pctnorm, levels=levelspctdiff, transform_first=True)

if len(levs_diff) < 2:
img4 = ax4.contourf(lons, lats, dif_cyclic, transform=ccrs.PlateCarree(), colors="w", norm=dnorm)
Expand Down Expand Up @@ -1281,7 +1274,7 @@ def plot_map_and_save(wks, case_nickname, base_nickname,
elif i == len(wrap_fields)-2:
levels = cp_info['levelspctdiff']
cmap = cp_info['cmappct']
norm = cp_info['normpct']
norm = cp_info['pctnorm']
else:
levels = cp_info['levels1']
cmap = cp_info['cmap1']
Expand Down Expand Up @@ -1920,22 +1913,17 @@ def prep_contour_plot(adata, bdata, diffdata, pctdata, **kwargs):
if "pct_diff_colormap" in kwargs:
cmappct = kwargs["pct_diff_colormap"]
else:
cmappct = 'coolwarm'
cmappct = "PuOr_r"
#End if

if "pct_diff_contour_levels" in kwargs:
levelspctdiff = kwargs["pct_diff_contour_levels"] # a list of explicit contour levels
elif "pct_diff_contour_range" in kwargs:
assert len(kwargs['pct_diff_contour_range']) == 3, \
"pct_diff_contour_range must have exactly three entries: min, max, step"

levelspctdiff = np.arange(*kwargs['pct_diff_contour_range'])
assert len(kwargs['pct_diff_contour_range']) == 3, "pct_diff_contour_range must have exactly three entries: min, max, step"
levelspctdiff = np.arange(*kwargs['pct_diff_contour_range'])
else:
# set a symmetric color bar for diff:
absmaxpct = np.max(np.abs(pctdata))
# set levels for difference plot:
levelspctdiff = np.linspace(-1*absmaxpct, absmaxpct, 12)
#End if
levelspctdiff = [-100,-75,-50,-40,-30,-20,-10,-8,-6,-4,-2,0,2,4,6,8,10,20,30,40,50,75,100]
pctnorm = mpl.colors.BoundaryNorm(levelspctdiff,256)

if "plot_log_pressure" in kwargs:
plot_log_p = kwargs["plot_log_pressure"]
Expand All @@ -1947,13 +1935,6 @@ def prep_contour_plot(adata, bdata, diffdata, pctdata, **kwargs):
normdiff = normfunc(vmin=np.min(levelsdiff), vmax=np.max(levelsdiff), vcenter=0.0)
else:
normdiff = mpl.colors.Normalize(vmin=np.min(levelsdiff), vmax=np.max(levelsdiff))

# color normalization for percent difference
if "pct_diff_contour_levels" in kwargs:
normpct = mpl.colors.BoundaryNorm(levelspctdiff,256)
else :
normpct = mpl.colors.Normalize(vmin=np.min(levelspctdiff), vmax=np.max(levelspctdiff))
#End if

subplots_opt = {}
contourf_opt = {}
Expand All @@ -1977,7 +1958,7 @@ def prep_contour_plot(adata, bdata, diffdata, pctdata, **kwargs):
'normdiff': normdiff,
'cmapdiff': cmapdiff,
'levelsdiff': levelsdiff,
'normpct': normpct,
'pctnorm': pctnorm,
'cmappct': cmappct,
'levelspctdiff':levelspctdiff,
'cmap1': cmap1,
Expand Down Expand Up @@ -2093,7 +2074,7 @@ def plot_zonal_mean_and_save(wks, case_nickname, base_nickname,
img3, ax[3] = zonal_plot(adata['lat'], pct, ax=ax[3])
ax[3].text(0.4, 0.4, empty_message, transform=ax[3].transAxes, bbox=props)
else:
img3, ax[3] = zonal_plot(adata['lat'], pct, ax=ax[3], norm=cp_info['normpct'],cmap=cp_info['cmappct'],levels=cp_info['levelspctdiff'],**cp_info['contourf_opt'])
img3, ax[3] = zonal_plot(adata['lat'], pct, ax=ax[3], norm=cp_info['pctnorm'],cmap=cp_info['cmappct'],levels=cp_info['levelspctdiff'],**cp_info['contourf_opt'])
fig.colorbar(img3, ax=ax[3], location='right',**cp_info['pct_colorbar_opt'])

ax[0].set_title(case_title, loc='left', fontsize=tiFontSize)
Expand Down Expand Up @@ -2326,7 +2307,7 @@ def plot_meridional_mean_and_save(wks, case_nickname, base_nickname,
img3, ax[3] = pltfunc(adata[xdim], pct, ax=ax[3])
ax[3].text(0.4, 0.4, empty_message, transform=ax[3].transAxes, bbox=props)
else:
img3, ax[3] = pltfunc(adata[xdim], pct, ax=ax[3], norm=cp_info['normpct'],cmap=cp_info['cmappct'],levels=cp_info['levelspctdiff'],**cp_info['contourf_opt'])
img3, ax[3] = pltfunc(adata[xdim], pct, ax=ax[3], norm=cp_info['pctnorm'],cmap=cp_info['cmappct'],levels=cp_info['levelspctdiff'],**cp_info['contourf_opt'])
cb3 = fig.colorbar(img3, ax=ax[3], location='right',**cp_info['colorbar_opt'])

#Set plot titles
Expand Down Expand Up @@ -2565,4 +2546,4 @@ def square_contour_difference(fld1, fld2, **kwargs):
return fig

#####################
#END HELPER FUNCTIONS
#END HELPER FUNCTIONS

0 comments on commit d880a0e

Please sign in to comment.