Skip to content

Commit c498298

Browse files
committed
Merge PR #371 (Bug fixes for stretched grid comparison plots)
This merge brings PR # (Bug fixes for stretched grid comparison plots, by @lizziel) into the GCPy development stream. PR #371 brings in the following changes to successfully compare GCHP stretched grid against GCHP non-stretched grid in 6-panel plots. Signed-off-by: Bob Yantosca <yantosca@seas.harvard.edu>
2 parents ba030d8 + edb54ce commit c498298

File tree

5 files changed

+97
-66
lines changed

5 files changed

+97
-66
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1616
- Modified criteria for terminating read of log files in `benchmark_scrape_gcclassic_timers.py` to avoid being spoofed by output that is attached by Intel VTune
1717
- Moved `gprofng_text_to_data_units` to function `text_to_data_units` in `gcpy/plot/core.py` so that it can be used by `gprofng_functions` and `vtune_plot_hotspots`
1818
- Updated GitHub badges in `README.md` and `docs/source/index.rst`
19+
- Expanded possible stretched grid attribute names to include STRETCH_FACTOR, TARGET_LAT, and TARGET_LON
20+
- Changed regridding for plots to always compare stretched-grid to uniform CS grid on the uniform CS grid rather than whatever grid is ref
1921

2022
### Fixed
21-
- Fix grid area calculation scripts of `grid_area` in `gcpy/gcpy/cstools.py`
23+
- Fixed grid area calculation scripts of `grid_area` in `gcpy/gcpy/cstools.py`
2224
- Fixed various security issues in GitHub Actions workflows
25+
- Fixed colorbar bounds for case of comparing cubed-sphere grids
2326

2427
## [1.6.2] - 2025-06-12
2528
### Added

gcpy/plot/compare_single_level.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,35 @@ def compare_single_level(
253253
# Get stretched-grid info if passed
254254
if sg_ref_path != '':
255255
sg_ref_attrs = xr.open_dataset(sg_ref_path).attrs
256-
sg_ref_params = [
257-
sg_ref_attrs['stretch_factor'],
258-
sg_ref_attrs['target_longitude'],
259-
sg_ref_attrs['target_latitude']]
256+
if 'stretch_factor' in sg_ref_attrs:
257+
sg_ref_params = [
258+
sg_ref_attrs['stretch_factor'],
259+
sg_ref_attrs['target_lon'],
260+
sg_ref_attrs['target_lat']]
261+
elif 'STRETCH_FACTOR' in sg_ref_attrs:
262+
sg_ref_params = [
263+
sg_ref_attrs['STRETCH_FACTOR'],
264+
sg_ref_attrs['TARGET_LON'],
265+
sg_ref_attrs['TARGET_LAT']]
266+
else:
267+
msg = "Stretched grid global parameters missing from ref file"
268+
raise ValueError(msg)
260269

261270
if sg_dev_path != '':
262271
sg_dev_attrs = xr.open_dataset(sg_dev_path).attrs
263-
sg_dev_params = [
264-
sg_dev_attrs['stretch_factor'],
265-
sg_dev_attrs['target_longitude'],
266-
sg_dev_attrs['target_latitude']]
272+
if 'stretch_factor' in sg_dev_attrs:
273+
sg_dev_params = [
274+
sg_dev_attrs['stretch_factor'],
275+
sg_dev_attrs['target_lon'],
276+
sg_dev_attrs['target_lat']]
277+
elif 'STRETCH_FACTOR' in sg_dev_attrs:
278+
sg_dev_params = [
279+
sg_dev_attrs['STRETCH_FACTOR'],
280+
sg_dev_attrs['TARGET_LON'],
281+
sg_dev_attrs['TARGET_LAT']]
282+
else:
283+
msg = "Stretched grid global parameters missing from dev file"
284+
raise ValueError(msg)
267285

268286
# Get grid info and regrid if necessary
269287
[refres, refgridtype, devres, devgridtype, cmpres, cmpgridtype, regridref,
@@ -505,14 +523,9 @@ def compare_single_level(
505523
for i in range(n_var):
506524
ds_refs[i] = reshape_MAPL_CS(ds_refs[i])
507525
ds_devs[i] = reshape_MAPL_CS(ds_devs[i])
508-
#ds_ref_cmps[i] = reshape_MAPL_CS(ds_ref_cmps[i])
509-
#ds_dev_cmps[i] = reshape_MAPL_CS(ds_dev_cmps[i])
510526
if diff_of_diffs:
511527
frac_ds_refs[i] = reshape_MAPL_CS(frac_ds_refs[i])
512528
frac_ds_devs[i] = reshape_MAPL_CS(frac_ds_devs[i])
513-
#frac_ds_ref_cmps[i] = reshape_MAPL_CS(frac_ds_ref_cmps[i])
514-
#frac_ds_dev_cmps[i] = reshape_MAPL_CS(frac_ds_dev_cmps[i])
515-
516529

517530
# ==================================================================
518531
# Create arrays for each variable in Ref and Dev datasets
@@ -696,7 +709,8 @@ def call_reshape(cmp_data):
696709
frac_ds_dev_cmp_reshaped = call_reshape(frac_ds_dev_cmp)
697710

698711
# ==============================================================
699-
# Get min and max values for use in the colorbars
712+
# Get min and max values for use in the top-row plot colorbars
713+
# and also flag if Ref and/or Dev are all zero or all NaN.
700714
# ==============================================================
701715

702716
# Choose from values within plot extent
@@ -753,31 +767,25 @@ def get_extent_for_colors(dset, minlon, maxlon, minlat, maxlat):
753767
min_max_maxlat
754768
)
755769

756-
# Ref
757-
vmin_ref = float(np.nanmin(ds_ref_reg.data))
758-
vmax_ref = float(np.nanmax(ds_ref_reg.data))
770+
# Use global data to determine cbar bounds if plotting cubed-sphere
771+
if refgridtype == "cs":
772+
vmin_ref = float(np.nanmin(ds_ref.data))
773+
vmax_ref = float(np.nanmax(ds_ref.data))
774+
else:
775+
vmin_ref = float(np.nanmin(ds_ref_reg.data))
776+
vmax_ref = float(np.nanmax(ds_ref_reg.data))
777+
778+
if devgridtype == "cs":
779+
vmin_dev = float(np.nanmin(ds_dev.data))
780+
vmax_dev = float(np.nanmax(ds_dev.data))
781+
else:
782+
vmin_dev = float(np.nanmin(ds_dev_reg.data))
783+
vmax_dev = float(np.nanmax(ds_dev_reg.data))
784+
785+
# Set vmin_both and vmax_both to use if match_cbar=True
786+
vmin_both = np.nanmin([vmin_ref, vmin_dev])
787+
vmax_both = np.nanmax([vmax_ref, vmax_dev])
759788

760-
# Dev
761-
vmin_dev = float(np.nanmin(ds_dev_reg.data))
762-
vmax_dev = float(np.nanmax(ds_dev_reg.data))
763-
764-
# Pylint says that these are unused variables, so comment out
765-
# -- Bob Yantosca (15 Aug 2023)
766-
# # Comparison
767-
# if cmpgridtype == "cs":
768-
# vmin_ref_cmp = float(np.nanmin(ds_ref_cmp))
769-
# vmax_ref_cmp = float(np.nanmax(ds_ref_cmp))
770-
# vmin_dev_cmp = float(np.nanmin(ds_dev_cmp))
771-
# vmax_dev_cmp = float(np.nanmax(ds_dev_cmp))
772-
# vmin_cmp = np.nanmin([vmin_ref_cmp, vmin_dev_cmp])
773-
# vmax_cmp = np.nanmax([vmax_ref_cmp, vmax_dev_cmp])
774-
# else:
775-
# vmin_cmp = np.nanmin([np.nanmin(ds_ref_cmp), np.nanmin(ds_dev_cmp)])
776-
# vmax_cmp = np.nanmax([np.nanmax(ds_ref_cmp), np.nanmax(ds_dev_cmp)])
777-
778-
# Get overall min & max
779-
vmin_abs = np.nanmin([vmin_ref, vmin_dev])#, vmin_cmp])
780-
vmax_abs = np.nanmax([vmax_ref, vmax_dev])#, vmax_cmp])
781789
# ==============================================================
782790
# Test if Ref and/or Dev contain all zeroes or all NaNs.
783791
# This will have implications as to how we set min and max
@@ -1064,8 +1072,8 @@ def get_extent_for_colors(dset, minlon, maxlon, minlat, maxlat):
10641072
other_all_nans = [dev_is_all_nan, ref_is_all_nan,
10651073
False, False, False, False]
10661074

1067-
mins = [vmin_ref, vmin_dev, vmin_abs]
1068-
maxs = [vmax_ref, vmax_dev, vmax_abs]
1075+
mins = [vmin_ref, vmin_dev, vmin_both]
1076+
maxs = [vmax_ref, vmax_dev, vmax_both]
10691077

10701078
ratio_logs = [False, False, False, False, True, True]
10711079

gcpy/plot/compare_zonal_mean.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,35 @@ def compare_zonal_mean(
316316
# Get stretched-grid info if passed
317317
if sg_ref_path != '':
318318
sg_ref_attrs = xr.open_dataset(sg_ref_path).attrs
319-
sg_ref_params = [
320-
sg_ref_attrs['stretch_factor'],
321-
sg_ref_attrs['target_longitude'],
322-
sg_ref_attrs['target_latitude']]
319+
if 'stretch_factor' in sg_ref_attrs:
320+
sg_ref_params = [
321+
sg_ref_attrs['stretch_factor'],
322+
sg_ref_attrs['target_lon'],
323+
sg_ref_attrs['target_lat']]
324+
elif 'STRETCH_FACTOR' in sg_ref_attrs:
325+
sg_ref_params = [
326+
sg_ref_attrs['STRETCH_FACTOR'],
327+
sg_ref_attrs['TARGET_LON'],
328+
sg_ref_attrs['TARGET_LAT']]
329+
else:
330+
msg = "Stretched grid global parameters missing from ref file"
331+
raise ValueError(msg)
323332

324333
if sg_dev_path != '':
325334
sg_dev_attrs = xr.open_dataset(sg_dev_path).attrs
326-
sg_dev_params = [
327-
sg_dev_attrs['stretch_factor'],
328-
sg_dev_attrs['target_longitude'],
329-
sg_dev_attrs['target_latitude']]
335+
if 'stretch_factor' in sg_dev_attrs:
336+
sg_dev_params = [
337+
sg_dev_attrs['stretch_factor'],
338+
sg_dev_attrs['target_lon'],
339+
sg_dev_attrs['target_lat']]
340+
elif 'STRETCH_FACTOR' in sg_dev_attrs:
341+
sg_dev_params = [
342+
sg_dev_attrs['STRETCH_FACTOR'],
343+
sg_dev_attrs['TARGET_LON'],
344+
sg_dev_attrs['TARGET_LAT']]
345+
else:
346+
msg = "Stretched grid global parameters missing from dev file"
347+
raise ValueError(msg)
330348

331349
[refres, refgridtype, devres, devgridtype, cmpres, cmpgridtype,
332350
regridref, regriddev, regridany, refgrid, devgrid, cmpgrid,
@@ -723,15 +741,17 @@ def createfig(ivar, temp_dir=''):
723741
zm_dev = ds_dev.mean(dim="lon")
724742
else:
725743
zm_dev = ds_dev.mean(axis=2)
744+
726745
# Comparison
727746
zm_dev_cmp = ds_dev_cmp.mean(axis=2)
728747
zm_ref_cmp = ds_ref_cmp.mean(axis=2)
729748
if diff_of_diffs:
730749
frac_zm_dev_cmp = frac_ds_dev_cmp.mean(axis=2)
731750
frac_zm_ref_cmp = frac_ds_ref_cmp.mean(axis=2)
751+
732752
# ==============================================================
733-
# Get min and max values for use in the colorbars
734-
# and also flag if Ref and/or Dev are all zero or all NaN
753+
# Get min and max values for use in the top-row plot colorbars
754+
# and also flag if Ref and/or Dev are all zero or all NaN.
735755
# ==============================================================
736756

737757
# Ref
@@ -742,13 +762,9 @@ def createfig(ivar, temp_dir=''):
742762
vmin_dev = float(zm_dev.min())
743763
vmax_dev = float(zm_dev.max())
744764

745-
# Comparison
746-
vmin_cmp = np.min([zm_ref_cmp.min(), zm_dev_cmp.min()])
747-
vmax_cmp = np.max([zm_ref_cmp.max(), zm_dev_cmp.max()])
748-
749-
# Take min/max across all grids
750-
vmin_abs = np.min([vmin_ref, vmin_dev, vmin_cmp])
751-
vmax_abs = np.max([vmax_ref, vmax_dev, vmax_cmp])
765+
# Set vmin_both and vmax_both to use if match_cbar=True
766+
vmin_both = np.min([vmin_ref, vmin_dev])
767+
vmax_both = np.max([vmax_ref, vmax_dev])
752768

753769
# ==============================================================
754770
# Test if Ref and/or Dev contain all zeroes or all NaNs.
@@ -955,8 +971,8 @@ def createfig(ivar, temp_dir=''):
955971
pedge_inds = [ref_pedge_ind, dev_pedge_ind, pedge_ind,
956972
pedge_ind, pedge_ind, pedge_ind]
957973

958-
mins = [vmin_ref, vmin_dev, vmin_abs]
959-
maxs = [vmax_ref, vmax_dev, vmax_abs]
974+
mins = [vmin_ref, vmin_dev, vmin_both]
975+
maxs = [vmax_ref, vmax_dev, vmax_both]
960976

961977
ratio_logs = [False, False, False, False, True, True]
962978
# Plot

gcpy/plot/six_plot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ def six_plot(
103103
"ll" for lat/lon or "cs" for cubed-sphere.
104104
vmins: list of float
105105
list of length 3 of minimum ref value, dev value,
106-
and absdiff value.
106+
and minimum of both (for use with match_cbar=True).
107107
vmaxs: list of float
108108
list of length 3 of maximum ref value, dev value,
109-
and absdiff value.
109+
and maximum of both (for use with match_cbar=True)
110110
use_cmap_RdBu: bool
111111
Set this flag to True to use a blue-white-red colormap
112112
match_cbar: bool
@@ -425,7 +425,7 @@ def vmin_vmax_for_ref_dev_plots(
425425
#---------------------------------------------------------------
426426
if use_cmap_RdBu:
427427

428-
# Ref supblot, diff-of-diffs
428+
# Ref subplot, diff-of-diffs
429429
if subplot in "ref":
430430
vmax = max([np.abs(vmins[0]), np.abs(vmaxs[0])])
431431
if match_cbar and not other_all_nan:

gcpy/regrid.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,16 @@ def create_regridders(
431431
"Warning: zonal mean comparison must be lat-lon. Defaulting to 1x1.25")
432432
cmpres = '1x1.25'
433433
cmpgridtype = "ll"
434-
elif sg_ref_params != [1, 170, -90] or sg_dev_params != [1, 170, -90]:
435-
# pick ref grid when a stretched-grid and non-stretched-grid
436-
# are passed
434+
elif sg_ref_params == [1, 170, -90] and sg_dev_params != [1, 170, -90]:
435+
# pick ref grid if dev is stretched and ref is not
437436
cmpres = refres
438437
cmpgridtype = "cs"
439438
sg_cmp_params = sg_ref_params
439+
elif sg_ref_params != [1, 170, -90] and sg_dev_params == [1, 170, -90]:
440+
# pick dev grid if ref is stretched and dev is not
441+
cmpres = devres
442+
cmpgridtype = "cs"
443+
sg_cmp_params = sg_dev_params
440444
else:
441445
# pick higher resolution CS grid out of two standard
442446
# cubed-sphere grids

0 commit comments

Comments
 (0)