Skip to content

Commit fec4995

Browse files
committed
Merge PR #377 (Bug fixes for stretched grid rst file generation/benchmarking)
This merge brings PR #377 (Bug fixes for stretched grid restart file generation and benchmarking, by @lizziel) into the GCPy 1.7.0 development stream. PR #377 adds the following updates: 1. Remove requirement to pass additional arguments to compare_single_level and compare_zonal_mean functions for the case of stretched grid datasets. The stretch parameters are now read from the ref and dev datasets passed to the functions. If not found the default values are used which correspond to no stretching. This enables using stretched grid files in benchmarking which previously did not work. 2. Call function to drop GC-Classic variables when regridding a GC-Classic restart file to GCHP. This fixes a bug where ilev appeared in GCHP restart files, inadvertently triggering MAPL to vertically flip all GCHP restart files upon read. Signed-off-by: Bob Yantosca <yantosca@seas.harvard.edu>
2 parents 835017e + e410c91 commit fec4995

File tree

5 files changed

+71
-89
lines changed

5 files changed

+71
-89
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111
- Added "Lint" GitHub Action to check other actions for security issues
1212
- Added `gcpy_environment_py314.ym1` to specify the GCPy environment packages with Python 3.14
1313
- Added GitHub action `build-gcpy-environment-py314.yml` to test building the GCPy environment with Python 3.14
14+
- Added call to drop GC-Classic variables when regridding a GC-Classic restart file to cubed-sphere
1415

1516
### Changed
1617
- 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

gcpy/file_regrid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(including stretched grids).
44
"""
55
import argparse
6+
import logging
67
import os
78
import warnings
89
import numpy as np
@@ -1294,6 +1295,7 @@ def drop_classic_vars(
12941295
"""
12951296
with xr.set_options(keep_attrs=True):
12961297
if towards_gchp:
1298+
logging.info("Dropping GC-Classic variables")
12971299
dset = dset.drop_vars(
12981300
["P0",
12991301
"hyam",

gcpy/plot/compare_single_level.py

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ def compare_single_level(
6565
second_ref=None,
6666
second_dev=None,
6767
spcdb_dir=os.path.dirname(__file__),
68-
sg_ref_path='',
69-
sg_dev_path='',
7068
ll_plot_func='imshow',
7169
**extra_plot_args
7270
):
@@ -177,14 +175,6 @@ def compare_single_level(
177175
spcdb_dir: str
178176
Directory containing species_database.yml file.
179177
Default value: Path of GCPy code repository
180-
sg_ref_path: str
181-
Path to NetCDF file containing stretched-grid info
182-
(in attributes) for the ref dataset
183-
Default value: '' (will not be read in)
184-
sg_dev_path: str
185-
Path to NetCDF file containing stretched-grid info
186-
(in attributes) for the dev dataset
187-
Default value: '' (will not be read in)
188178
ll_plot_func: str
189179
Function to use for lat/lon single level plotting with
190180
possible values 'imshow' and 'pcolormesh'. imshow is much
@@ -249,40 +239,39 @@ def compare_single_level(
249239
quiet=True
250240
)
251241

252-
sg_ref_params = [1, 170, -90]
253-
sg_dev_params = [1, 170, -90]
254-
# Get stretched-grid info if passed
255-
if sg_ref_path != '':
256-
sg_ref_attrs = xr.open_dataset(sg_ref_path).attrs
257-
if 'stretch_factor' in sg_ref_attrs:
258-
sg_ref_params = [
259-
sg_ref_attrs['stretch_factor'],
260-
sg_ref_attrs['target_lon'],
261-
sg_ref_attrs['target_lat']]
262-
elif 'STRETCH_FACTOR' in sg_ref_attrs:
263-
sg_ref_params = [
264-
sg_ref_attrs['STRETCH_FACTOR'],
265-
sg_ref_attrs['TARGET_LON'],
266-
sg_ref_attrs['TARGET_LAT']]
267-
else:
268-
msg = "Stretched grid global parameters missing from ref file"
269-
raise ValueError(msg)
270-
271-
if sg_dev_path != '':
272-
sg_dev_attrs = xr.open_dataset(sg_dev_path).attrs
273-
if 'stretch_factor' in sg_dev_attrs:
274-
sg_dev_params = [
275-
sg_dev_attrs['stretch_factor'],
276-
sg_dev_attrs['target_lon'],
277-
sg_dev_attrs['target_lat']]
278-
elif 'STRETCH_FACTOR' in sg_dev_attrs:
279-
sg_dev_params = [
280-
sg_dev_attrs['STRETCH_FACTOR'],
281-
sg_dev_attrs['TARGET_LON'],
282-
sg_dev_attrs['TARGET_LAT']]
283-
else:
284-
msg = "Stretched grid global parameters missing from dev file"
285-
raise ValueError(msg)
242+
# Get stretched grid info, if any.
243+
# Parameter order is stretch factor, target longitude, target latitude.
244+
# Stretch factor 1 corresponds with no stretch.
245+
246+
# Ref stretch attributes
247+
print(refdata.attrs)
248+
if 'stretch_factor' in refdata.attrs:
249+
sg_ref_params = [
250+
refdata.attrs['stretch_factor'],
251+
refdata.attrs['target_lon'],
252+
refdata.attrs['target_lat']]
253+
elif 'STRETCH_FACTOR' in refdata.attrs:
254+
sg_ref_params = [
255+
refdata.attrs['STRETCH_FACTOR'],
256+
refdata.attrs['TARGET_LON'],
257+
refdata.attrs['TARGET_LAT']]
258+
else:
259+
sg_ref_params = [1, 170, -90]
260+
261+
# Dev stretch attributes
262+
print(devdata.attrs)
263+
if 'stretch_factor' in devdata.attrs:
264+
sg_dev_params = [
265+
devdata.attrs['stretch_factor'],
266+
devdata.attrs['target_lon'],
267+
devdata.attrs['target_lat']]
268+
elif 'STRETCH_FACTOR' in devdata.attrs:
269+
sg_dev_params = [
270+
devdata.attrs['STRETCH_FACTOR'],
271+
devdata.attrs['TARGET_LON'],
272+
devdata.attrs['TARGET_LAT']]
273+
else:
274+
sg_dev_params = [1, 170, -90]
286275

287276
# Get grid info and regrid if necessary
288277
[refres, refgridtype, devres, devgridtype, cmpres, cmpgridtype, regridref,

gcpy/plot/compare_zonal_mean.py

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ def compare_zonal_mean(
6565
second_ref=None,
6666
second_dev=None,
6767
spcdb_dir=os.path.dirname(__file__),
68-
sg_ref_path='',
69-
sg_dev_path='',
7068
ref_vert_params=None,
7169
dev_vert_params=None,
7270
**extra_plot_args
@@ -179,14 +177,6 @@ def compare_zonal_mean(
179177
spcdb_dir: str
180178
Directory containing species_database.yml file.
181179
Default value: Path of GCPy code repository
182-
sg_ref_path: str
183-
Path to NetCDF file containing stretched-grid info
184-
(in attributes) for the ref dataset
185-
Default value: '' (will not be read in)
186-
sg_dev_path: str
187-
Path to NetCDF file containing stretched-grid info
188-
(in attributes) for the dev dataset
189-
Default value: '' (will not be read in)
190180
ref_vert_params: list(AP, BP) of list-like types
191181
Hybrid grid parameter A in hPa and B (unitless).
192182
Needed if ref grid is not 47 or 72 levels.
@@ -312,40 +302,39 @@ def compare_zonal_mean(
312302
fracrefdata = fracrefdata.isel(lev=ref_pmid_ind)
313303
fracdevdata = fracdevdata.isel(lev=dev_pmid_ind)
314304

315-
sg_ref_params = [1, 170, -90]
316-
sg_dev_params = [1, 170, -90]
317-
# Get stretched-grid info if passed
318-
if sg_ref_path != '':
319-
sg_ref_attrs = xr.open_dataset(sg_ref_path).attrs
320-
if 'stretch_factor' in sg_ref_attrs:
321-
sg_ref_params = [
322-
sg_ref_attrs['stretch_factor'],
323-
sg_ref_attrs['target_lon'],
324-
sg_ref_attrs['target_lat']]
325-
elif 'STRETCH_FACTOR' in sg_ref_attrs:
326-
sg_ref_params = [
327-
sg_ref_attrs['STRETCH_FACTOR'],
328-
sg_ref_attrs['TARGET_LON'],
329-
sg_ref_attrs['TARGET_LAT']]
330-
else:
331-
msg = "Stretched grid global parameters missing from ref file"
332-
raise ValueError(msg)
333-
334-
if sg_dev_path != '':
335-
sg_dev_attrs = xr.open_dataset(sg_dev_path).attrs
336-
if 'stretch_factor' in sg_dev_attrs:
337-
sg_dev_params = [
338-
sg_dev_attrs['stretch_factor'],
339-
sg_dev_attrs['target_lon'],
340-
sg_dev_attrs['target_lat']]
341-
elif 'STRETCH_FACTOR' in sg_dev_attrs:
342-
sg_dev_params = [
343-
sg_dev_attrs['STRETCH_FACTOR'],
344-
sg_dev_attrs['TARGET_LON'],
345-
sg_dev_attrs['TARGET_LAT']]
346-
else:
347-
msg = "Stretched grid global parameters missing from dev file"
348-
raise ValueError(msg)
305+
# Get stretched grid info, if any.
306+
# Parameter order is stretch factor, target longitude, target latitude.
307+
# Stretch factor 1 corresponds with no stretch.
308+
309+
# Ref stretch attributes
310+
print(refdata.attrs)
311+
if 'stretch_factor' in refdata.attrs:
312+
sg_ref_params = [
313+
refdata.attrs['stretch_factor'],
314+
refdata.attrs['target_lon'],
315+
refdata.attrs['target_lat']]
316+
elif 'STRETCH_FACTOR' in refdata.attrs:
317+
sg_ref_params = [
318+
refdata.attrs['STRETCH_FACTOR'],
319+
refdata.attrs['TARGET_LON'],
320+
refdata.attrs['TARGET_LAT']]
321+
else:
322+
sg_ref_params = [1, 170, -90]
323+
324+
# Dev stretch attributes
325+
print(devdata.attrs)
326+
if 'stretch_factor' in devdata.attrs:
327+
sg_dev_params = [
328+
devdata.attrs['stretch_factor'],
329+
devdata.attrs['target_lon'],
330+
devdata.attrs['target_lat']]
331+
elif 'STRETCH_FACTOR' in devdata.attrs:
332+
sg_dev_params = [
333+
devdata.attrs['STRETCH_FACTOR'],
334+
devdata.attrs['TARGET_LON'],
335+
devdata.attrs['TARGET_LAT']]
336+
else:
337+
sg_dev_params = [1, 170, -90]
349338

350339
[refres, refgridtype, devres, devgridtype, cmpres, cmpgridtype,
351340
regridref, regriddev, regridany, refgrid, devgrid, cmpgrid,

gcpy/regrid_restart_file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import sparselt.esmf
3737
import sparselt.xr
3838
import requests
39-
39+
from gcpy.file_regrid import drop_classic_vars
4040

4141
TEMP_FILES = []
4242

@@ -515,6 +515,7 @@ def regrid_restart_file(
515515
is_conversion = input_is_gchp != output_is_gchp
516516
if is_conversion:
517517
to_gchp = output_is_gchp
518+
dataset = drop_classic_vars(dataset, to_gchp)
518519
dataset = rename_variables(dataset, to_gchp)
519520
dataset = reverse_lev(dataset, to_gchp)
520521

0 commit comments

Comments
 (0)