Skip to content

Commit

Permalink
Merge the latest updates from PR geoschem#200 into the branch for PR g…
Browse files Browse the repository at this point in the history
…eoschem#204

This brings some later fixes for issues with the creation of
text saying whether or not 2 version have identical results in
the table output.

Signed-off-by: Bob Yantosca <[email protected]>
  • Loading branch information
yantosca committed Feb 9, 2023
2 parents db21c40 + feddbf8 commit 0c3fcec
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
47 changes: 30 additions & 17 deletions gcpy/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ def create_total_emissions_table(

# Write a placeholder to the file that denotes where
# the list of species with differences will be written
placeholder = "@%% insert diff_list here %%@"
if "Inv" in template:
print(f"Inventories that differ btw {refstr} and {devstr}:", file=f)
else:
print(f"Species that differ btw {refstr} and {devstr}:", file=f)
placeholder = "@%% insert diff status here %%@"
print(f"{placeholder}\n\n", file=f)

# Define a list for differences
Expand Down Expand Up @@ -359,7 +355,10 @@ def create_total_emissions_table(
util.insert_text_into_file(
filename=outfilename,
search_text=placeholder,
replace_text=diff_list_to_text(diff_list),
replace_text=diff_list_to_text(
refstr,
devstr,
diff_list),
width=90
)

Expand Down Expand Up @@ -472,18 +471,16 @@ def create_global_mass_table(
if trop_only:
title1 = f"### Global mass (Gg) {label} (Trop only)"
title2 = f"### Ref = {refstr}; Dev = {devstr}"
title3 = f"### Species that differ btw {refstr} and {devstr}:"

# Write a placeholder to the file that denotes where
# the list of species with differences will be written
placeholder = "@%% insert diff_list here %%@"
placeholder = "@%% insert diff status here %%@"

# Print header to file
print("#" * 89, file=f)
print(f"{title1 : <86}{'###'}", file=f)
print(f"{title2 : <86}{'###'}", file=f)
print(f"{'###' : <86}{'###'}", file=f)
print(f"{title3 : <86}{'###'}", file=f)
print(f"{placeholder}", file=f)
print("#" * 89, file=f)

Expand Down Expand Up @@ -585,6 +582,8 @@ def create_global_mass_table(
filename=outfilename,
search_text=placeholder,
replace_text=diff_list_to_text(
refstr,
devstr,
diff_list,
fancy_format=True
),
Expand Down Expand Up @@ -4509,8 +4508,7 @@ def create_benchmark_summary_table(

# Variables to skip
skip_vars = gcon.skip_these_vars
skip_vars.append("corner_lats")
skip_vars.append("corner_lons")
skip_vars.append("AREA")

# Pick the proper function to read the data
reader = util.dataset_reader(
Expand Down Expand Up @@ -4562,9 +4560,14 @@ def create_benchmark_summary_table(
diff_list = []

# Keep track of which variables are different
# Loop over the common variables
# NOTE: Use 32-point float for comparisons since this is
# the precision used for History diagnostics.
for v in vardict["commonvarsData"]:
if not util.array_equals(refdata[v], devdata[v]):
if not util.array_equals(
refdata[v],
devdata[v],
dtype=np.float32
):
diff_list.append(v)

# Drop duplicate values from diff_list
Expand Down Expand Up @@ -4592,6 +4595,8 @@ def create_benchmark_summary_table(


def diff_list_to_text(
refstr,
devstr,
diff_list,
fancy_format=False
):
Expand All @@ -4617,11 +4622,19 @@ def diff_list_to_text(
# Strip out duplicates from diff_list
# Prepare a message about species differences (or alternate msg)
diff_list = util.unique_values(diff_list, drop=[None])
diff_text = util.wrap_text(diff_list, width=85)
if len(diff_text) > 85:
diff_text = "... Too many diffs to print (see below for details)"

# Print the text
n_diff = len(diff_list)
if n_diff > 0:
diff_text = f"{devstr} and {refstr} show {n_diff} differences"
else:
diff_text = f"{devstr} and {refstr} are identical"
diff_text = util.wrap_text(
diff_text,
width=83
)

if fancy_format:
diff_text = f"### {diff_text : <82}{'###'}"

return diff_text.strip()
49 changes: 23 additions & 26 deletions gcpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ def compare_varnames(
refdata and devdata, and that have lat,
lon, and level dimensions.
commonvarsData List of all commmon 2D or 3D data variables,
excluding index variables.
excluding index variables. This is the
list of "plottable" variables.
refonly List of 2D or 3D variables that are only
present in refdata.
devonly List of 2D or 3D variables that are only
Expand All @@ -858,37 +859,29 @@ def compare_varnames(
refonly = [v for v in refvars if v not in devvars]
devonly = [v for v in devvars if v not in refvars]
dimmismatch = [v for v in commonvars if refdata[v].ndim != devdata[v].ndim]
commonvarsOther = [
# Assume plottable data has lon and lat
# This is OK for purposes of benchmarking
# -- Bob Yantosca (09 Feb 2023)
commonvarsData = [
v for v in commonvars if (
(
("lat" not in refdata[v].dims or "Xdim" not in refdata[v].dims)
and
("lon" not in refdata[v].dims or "Ydim" not in refdata[v].dims)
("lat" in refdata[v].dims or "Ydim" in refdata[v].dims)
and
("lev" not in refdata[v].dims)
)
or
(
("hyam" in v or "hybm" in v) # Omit these from plottable data
)
("lon" in refdata[v].dims or "Xdim" in refdata[v].dims)
)
]
commonvarsOther = [
v for v in commonvars if (
v not in commonvarsData
)
]
commonvars2D = [
v for v in commonvars if (
("lat" in refdata[v].dims or "Xdim" in refdata[v].dims)
and
("lon" in refdata[v].dims or "Ydim" in refdata[v].dims)
and
("lev" not in refdata[v].dims)
(v in commonvarsData) and ("lev" not in refdata[v].dims)
)
]
commonvars3D = [
v for v in commonvars if (
("lat" in refdata[v].dims or "Xdim" in refdata[v].dims)
and
("lon" in refdata[v].dims or "Ydim" in refdata[v].dims)
and
("lev" in refdata[v].dims)
(v in commonvarsData) and ("lev" in refdata[v].dims)
)
]

Expand Down Expand Up @@ -2280,7 +2273,8 @@ def insert_text_into_file(

def array_equals(
refdata,
devdata
devdata,
dtype=np.float64
):
"""
Tests two arrays for equality. Useful for checking which
Expand All @@ -2292,6 +2286,9 @@ def array_equals(
The first array to be checked.
devdata: xarray DataArray or numpy ndarray
The second array to be checked.
dtype : np.float32 or np.float64
The precision that will be used to make the evaluation.
Default: np.float64
Returns:
--------
Expand All @@ -2314,9 +2311,9 @@ def array_equals(

# This method will work if the arrays hve different dimensions
# but an element-by-element search will not!
refsum = np.sum(refdata, dtype=np.float64)
devsum = np.sum(devdata, dtype=np.float64)
return np.abs(devsum - refsum) > np.float64(0.0)
refsum = np.nansum(refdata, dtype=dtype)
devsum = np.nansum(devdata, dtype=dtype)
return (not np.abs(devsum - refsum) > dtype(0.0))


def make_directory(
Expand Down

0 comments on commit 0c3fcec

Please sign in to comment.