Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/adf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
Calculate annual averages from time series data.
seasonal_mean(data, season=None, is_climo=None):
Calculates the time-weighted seasonal average (or average over all time).
seasonal_diffs(test_data, ref_data, season):
Seasonal means of two climatologies, their difference and percent difference.
domain_stats(data, domain):
Provides statistics in specified region.
pres_from_hybrid(psfc, hya, hyb, p0=100000.):
Expand Down Expand Up @@ -973,6 +975,54 @@ def seasonal_mean(data, season=None, is_climo=None):
#######


def seasonal_diffs(test_data, ref_data, season):
"""Seasonal means of two climatologies, their difference and percent difference.

Parameters
----------
test_data : xarray.DataArray
test case climatology, with 12 months on a ``time`` or ``month`` dimension
ref_data : xarray.DataArray
reference (baseline or observations) climatology, same layout as `test_data`
season : str
season to average, one of ANN, DJF, JJA, MAM, SON

Returns
-------
test_season : xarray.DataArray
month-length weighted seasonal mean of `test_data`
ref_season : xarray.DataArray
month-length weighted seasonal mean of `ref_data`
diff_season : xarray.DataArray
``test_season - ref_season``, with the units of `test_data` when it has any
pct_season : xarray.DataArray
``100 * diff_season / abs(ref_season)``, in units of ``%``

Notes
-----
Points where the percent difference cannot be computed -- masked or
missing in either field, or a reference value of zero -- are left
missing (NaN), so that they plot as blank rather than as 0%.
"""
test_season = seasonal_mean(test_data, season=season, is_climo=True)
ref_season = seasonal_mean(ref_data, season=season, is_climo=True)

diff_season = test_season - ref_season
# Some CAM fields (e.g. AODVISdn) are written with no units at all.
if "units" in test_season.attrs:
diff_season.attrs["units"] = test_season.attrs["units"]

pct_season = diff_season / np.abs(ref_season) * 100.0
# Dividing by a zero reference gives +/-inf; treat it like a masked point.
pct_season = pct_season.where(np.isfinite(pct_season))
pct_season.attrs["units"] = "%"

return test_season, ref_season, diff_season, pct_season


#######


def domain_stats(data, domain):
"""Provides statistics in specified region.

Expand Down
54 changes: 25 additions & 29 deletions lib/plotting_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def make_polar_plot(
else:
pct = pctchange

# check if pct has NaN's or Inf values and if so set them to 0 to prevent plotting errors
pct = pct.where(np.isfinite(pct), np.nan)
pct = pct.fillna(0.0)
# Leave masked or undefined (divide by zero) points missing, so they plot
# as blank rather than as 0%:
pct = pct.where(np.isfinite(pct))

if hemisphere.upper() == "NH":
proj = ccrs.NorthPolarStereo()
Expand Down Expand Up @@ -412,13 +412,6 @@ def make_polar_plot(
)
ax4.set_title(r"$\mathbf{Test} - \mathbf{Baseline}$", loc="left", fontsize=8)

if "units" in kwargs:
ax2.set_ylabel(kwargs["units"])
ax4.set_ylabel(kwargs["units"])
else:
ax2.set_ylabel(f"{d1.units}")
ax4.set_ylabel(f"{d1.units}")

for a in [ax1, ax2, ax3, ax4]:
a.set_extent(domain, ccrs.PlateCarree())
a.coastlines()
Expand All @@ -444,7 +437,8 @@ def make_polar_plot(
bbox_transform=ax2.transAxes,
borderpad=0,
)
fig.colorbar(img1, cax=cb_mean_ax)
units = kwargs.get("units", d1.attrs.get("units", ""))
fig.colorbar(img1, cax=cb_mean_ax, label=units)

cb_pct_ax = inset_axes(
ax3,
Expand All @@ -466,9 +460,9 @@ def make_polar_plot(
borderpad=0,
)

fig.colorbar(img3, cax=cb_pct_ax)
fig.colorbar(img3, cax=cb_pct_ax, label="%")

fig.colorbar(img4, cax=cb_diff_ax)
fig.colorbar(img4, cax=cb_diff_ax, label=units)

# Save files
fig.savefig(wks, bbox_inches="tight", dpi=300)
Expand Down Expand Up @@ -1045,7 +1039,10 @@ def plot_map_and_save(
bbox_transform=ax2.transAxes,
borderpad=0,
)
fig.colorbar(img[1], cax=cb_mean_ax, **cp_info["colorbar_opt"])
# A label set in the variable defaults ("mpl: colorbar: label") wins.
units = kwargs.get("units", mdlfld.attrs.get("units", ""))
cbar_opt = {"label": units, **cp_info["colorbar_opt"]}
fig.colorbar(img[1], cax=cb_mean_ax, **cbar_opt)

cb_pct_ax = inset_axes(
ax3,
Expand All @@ -1056,8 +1053,7 @@ def plot_map_and_save(
bbox_transform=ax3.transAxes,
borderpad=0,
)
pct_cb = fig.colorbar(img[2], cax=cb_pct_ax, **cp_info["colorbar_opt"])
pct_cb.ax.set_ylabel = "%"
fig.colorbar(img[2], cax=cb_pct_ax, **{**cp_info["colorbar_opt"], "label": "%"})

cb_diff_ax = inset_axes(
ax4,
Expand All @@ -1068,7 +1064,7 @@ def plot_map_and_save(
bbox_transform=ax4.transAxes,
borderpad=0,
)
fig.colorbar(img[3], cax=cb_diff_ax, **cp_info["colorbar_opt"])
fig.colorbar(img[3], cax=cb_diff_ax, **cbar_opt)

# Write final figure to file
fig.savefig(wks, bbox_inches="tight", dpi=300)
Expand Down Expand Up @@ -1242,9 +1238,9 @@ def plot_zonal_mean_and_save(

# calculate the percent change
pct = (azm - bzm) / np.abs(bzm) * 100.0
# check if pct has NaN's or Inf values and if so set them to 0 to prevent plotting errors
pct = pct.where(np.isfinite(pct), np.nan)
pct = pct.fillna(0.0)
# Leave masked or undefined (divide by zero) points missing, so they plot
# as blank rather than as 0%:
pct = pct.where(np.isfinite(pct))

# generate dictionary of contour plot settings:
cp_info = plot_utils.prep_contour_plot(azm, bzm, diff, pct, **kwargs)
Expand Down Expand Up @@ -1364,9 +1360,9 @@ def plot_zonal_mean_and_save(

# calculate the percent change
pct = (azm - bzm) / np.abs(bzm) * 100.0
# check if pct has NaN's or Inf values and if so set them to 0 to prevent plotting errors
pct = pct.where(np.isfinite(pct), np.nan)
pct = pct.fillna(0.0)
# Leave masked or undefined (divide by zero) points missing, so they plot
# as blank rather than as 0%:
pct = pct.where(np.isfinite(pct))

fig, ax = plt.subplots(nrows=3)
ax = [ax[0], ax[1], ax[2]]
Expand Down Expand Up @@ -1557,9 +1553,9 @@ def plot_meridional_mean_and_save(

# calculate the percent change
pct = (adata - bdata) / np.abs(bdata) * 100.0
# check if pct has NaN's or Inf values and if so set them to 0 to prevent plotting errors
pct = pct.where(np.isfinite(pct), np.nan)
pct = pct.fillna(0.0)
# Leave masked or undefined (divide by zero) points missing, so they plot
# as blank rather than as 0%:
pct = pct.where(np.isfinite(pct))

# plot-controlling parameters:
xdim = "lon" # the name used for the x-axis dimension
Expand Down Expand Up @@ -1852,9 +1848,9 @@ def square_contour_difference(fld1, fld2, **kwargs):
diff = fld1 - fld2

pct = (fld1 - fld2) / np.abs(fld2) * 100.0
# check if pct has NaN's or Inf values and if so set them to 0 to prevent plotting errors
pct = pct.where(np.isfinite(pct), np.nan)
pct = pct.fillna(0.0)
# Leave masked or undefined (divide by zero) points missing, so they plot
# as blank rather than as 0%:
pct = pct.where(np.isfinite(pct))

## USE A DIVERGING COLORMAP CENTERED AT ZERO
## Special case is when min > 0 or max < 0
Expand Down
75 changes: 75 additions & 0 deletions lib/test/unit_tests/test_seasonal_diffs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Collection of python unit tests
for "adf_utils.seasonal_diffs".

The lat/lon and polar maps both use this for their seasonal means and
differences. It has to work for fields written with no units (e.g. AODVISdn),
and has to leave the percent difference missing where it cannot be computed,
so masked points plot as blank rather than as 0%.

NOTE: these tests import adf_utils, which imports xarray, so they are skipped
in CI, which installs only PyYAML and pytest. They run in a full ADF
environment.
"""

import os
import os.path
import sys
import unittest

# Set relevant path variables:
_CURRDIR = os.path.abspath(os.path.dirname(__file__))
_ADF_LIB_DIR = os.path.join(_CURRDIR, os.pardir, os.pardir)

# Add ADF "lib" directory to python path:
sys.path.append(_ADF_LIB_DIR)

try:
import numpy as np
import xarray as xr
from adf_utils import seasonal_diffs

_HAS_ADF_UTILS = True
except ImportError:
_HAS_ADF_UTILS = False


def _climo(vals, **attrs):
"""A 12-month climatology on a 2-point lat grid, constant in time."""
data = np.broadcast_to(np.asarray(vals, dtype="f8"), (12, len(vals)))
return xr.DataArray(
data.copy(),
dims=("time", "lat"),
coords={"time": np.arange(1, 13), "lat": [10.0, 20.0]},
attrs=attrs,
)


@unittest.skipUnless(_HAS_ADF_UTILS, "adf_utils dependencies not available")
class SeasonalDiffsTestRoutine(unittest.TestCase):
"""Unit tests for the seasonal means, difference and percent difference."""

def test_values_and_units(self):
"""Difference and percent difference, with the test case's units."""
_, _, dif, pct = seasonal_diffs(
_climo([3.0, 6.0], units="K"), _climo([2.0, 4.0], units="K"), "DJF"
)
np.testing.assert_allclose(dif.values, [1.0, 2.0])
np.testing.assert_allclose(pct.values, [50.0, 50.0])
self.assertEqual(dif.attrs["units"], "K")
self.assertEqual(pct.attrs["units"], "%")

def test_no_units(self):
"""A field with no units attribute (e.g. AODVISdn) does not fail."""
_, _, dif, pct = seasonal_diffs(_climo([3.0, 6.0]), _climo([2.0, 4.0]), "JJA")
self.assertNotIn("units", dif.attrs)
self.assertEqual(pct.attrs["units"], "%")

def test_missing_stays_missing(self):
"""Masked points and a zero reference give NaN, not 0%."""
_, _, _, pct = seasonal_diffs(_climo([1.0, 1.0]), _climo([np.nan, 0.0]), "ANN")
self.assertTrue(np.isnan(pct.values).all())


if __name__ == "__main__":
unittest.main()
27 changes: 4 additions & 23 deletions scripts/plotting/global_latlon_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def global_latlon_map(adfobj):
determine central longitude for global plots
utils.lat_lon_validate_dims()
makes sure latitude and longitude are valid
utils.seasonal_mean()
calculate seasonal mean
utils.seasonal_diffs()
seasonal means, difference and percent difference
plot_utils.plot_map_and_save()
send information to make the plot and save the file
utils.zm_validate_dims()
Expand Down Expand Up @@ -188,25 +188,6 @@ def get_plot_config(adfobj):
}


def process_seasonal_data(mdata, odata, season, weight_season=True):
"""Helper function to calculate seasonal means and differences."""
if weight_season:
mseason = utils.seasonal_mean(mdata, season=season, is_climo=True)
oseason = utils.seasonal_mean(odata, season=season, is_climo=True)
else:
mseason = mdata.sel(time=season).mean(dim='time')
oseason = odata.sel(time=season).mean(dim='time')

# Calculate differences
dseason = mseason - oseason

# Calculate percent change
pseason = (mseason - oseason) / np.abs(oseason) * 100.0
pseason = pseason.where(np.isfinite(pseason), np.nan)

return mseason, oseason, dseason, pseason


def plot_file_op(adfobj, plot_name, var, case_name, season, web_category, redo_plot, plot_type):
"""Check if output plot needs to be made or remade.

Expand Down Expand Up @@ -375,7 +356,7 @@ def process_2d_plots(adfobj, mdata, odata, case_name, case_nickname,

# Calculate seasonal means and differences
mseasons[s], oseasons[s], dseasons[s], pseasons[s] = \
process_seasonal_data(mdata, odata, s)
utils.seasonal_diffs(mdata, odata, s)

# Generate plot
pf.plot_map_and_save(plot_name, case_nickname, adfobj.data.ref_nickname,
Expand Down Expand Up @@ -410,7 +391,7 @@ def process_3d_plots(adfobj, mdata, odata, case_name, case_nickname,

# Calculate seasonal means and differences
mseasons[s], oseasons[s], dseasons[s], pseasons[s] = \
process_seasonal_data(mdata, odata, s)
utils.seasonal_diffs(mdata, odata, s)

# Generate plot
pf.plot_map_and_save(plot_name, case_nickname, adfobj.data.ref_nickname,
Expand Down
40 changes: 2 additions & 38 deletions scripts/plotting/polar_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,6 @@ def get_hemisphere(hemi_type):
"""
return "NH" if hemi_type == "NHPolar" else "SH"

def process_seasonal_data(mdata, odata, season):
"""Helper function to calculate seasonal means and differences.
Parameters
----------
mdata : xarray.DataArray
test case data
odata : xarray.DataArray
reference case data
season : str
season (JJA, DJF, MAM, SON)

Returns
-------
mseason : xarray.DataArray
oseason : xarray.DataArray
dseason : xarray.DataArray
pseason : xarray.DataArray
Seasonal means for test, reference, difference, and percent difference
"""
mseason = utils.seasonal_mean(mdata, season=season, is_climo=True)
oseason = utils.seasonal_mean(odata, season=season, is_climo=True)

# Calculate differences
dseason = mseason - oseason
dseason.attrs['units'] = mseason.attrs['units']

# Calculate percent change
pseason = (mseason - oseason) / np.abs(oseason) * 100.0
pseason.attrs['units'] = '%'
pseason = pseason.where(np.isfinite(pseason), np.nan)
pseason = pseason.fillna(0.0)

return mseason, oseason, dseason, pseason

def polar_map(adfobj):
"""Generate polar maps of model fields with continental overlays."""
#Notify user that script has started:
Expand Down Expand Up @@ -270,10 +236,8 @@ def polar_map(adfobj):

# Calculate seasonal means and differences
use_odata = odata_level if has_lev else odata
mseason, oseason, dseason, pseason = process_seasonal_data(
mdata,
use_odata,
plot['season']
mseason, oseason, dseason, pseason = utils.seasonal_diffs(
mdata, use_odata, plot['season']
)

# Create plot
Expand Down
Loading