Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
forsyth2 committed Apr 14, 2023
1 parent 41e3966 commit 5fd2055
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ years = "1850:1854:2",
input_files = "eam.h0"
input_subdir = "archive/atm/hist"
mapping_file = "glb"
vars = "FSNTOA,FLUT,FSNT,FLNT,FSNS,FLNS,SHFLX,QFLX,TAUX,TAUY,PRECC,PRECL,PRECSC,PRECSL,TS,TREFHT,CLDTOT,CLDHGH,CLDMED,CLDLOW,U,CO2,CO2_FFF,CO2_OCN,CO2_LND,TMCO2,TMCO2_FFF,TMCO2_LND,TMCO2_OCN,TOTSOMC,TOTECOSYSC,NEE,TOTVEGC"
years = "1850:1860:5",

[[ land_monthly ]]
Expand Down
51 changes: 29 additions & 22 deletions zppy/templates/coupled_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ def plot_generic(ax, xlim, exps, var_name):
"shorten_year": False,
"title": var_name,
"use_getmoc": False,
"var": lambda exp: np.array(exp["annual"][var_name]),
"var": lambda exp: np.array(exp["annual"][var_name][0]),
"verbose": False,
"vol": False,
"ylabel": "???",
"ylabel": lambda exp: np.array(exp["annual"][var_name][1]),
}
plot(ax, xlim, exps, param_dict)

Expand All @@ -178,7 +178,7 @@ def plot_net_toa_flux_restom(ax, xlim, exps):
"shorten_year": False,
"title": "Net TOA flux (restom)",
"use_getmoc": False,
"var": lambda exp: np.array(exp["annual"]["RESTOM"]),
"var": lambda exp: np.array(exp["annual"]["RESTOM"][0]),
"verbose": False,
"vol": False,
"ylabel": "W m-2",
Expand All @@ -205,7 +205,7 @@ def plot_global_surface_air_temperature(ax, xlim, exps):
"shorten_year": False,
"title": "Global surface air temperature",
"use_getmoc": False,
"var": lambda exp: np.array(exp["annual"]["TREFHT"]) - 273.15,
"var": lambda exp: np.array(exp["annual"]["TREFHT"][0]) - 273.15,
"verbose": False,
"vol": False,
"ylabel": "degC",
Expand All @@ -232,7 +232,7 @@ def plot_toa_radiation(ax, xlim, exps):
"shorten_year": False,
"title": "TOA radiation: SW (solid), LW (dashed)",
"use_getmoc": False,
"var": lambda exp: np.array(exp["annual"]["FSNTOA"]),
"var": lambda exp: np.array(exp["annual"]["FSNTOA"][0]),
"verbose": None,
"vol": None,
"ylabel": "W m-2",
Expand All @@ -259,8 +259,8 @@ def plot_net_atm_energy_imbalance(ax, xlim, exps):
"shorten_year": False,
"title": "Net atm energy imbalance (restom-ressurf)",
"use_getmoc": False,
"var": lambda exp: np.array(exp["annual"]["RESTOM"])
- np.array(exp["annual"]["RESSURF"]),
"var": lambda exp: np.array(exp["annual"]["RESTOM"][0])
- np.array(exp["annual"]["RESSURF"][0]),
"verbose": False,
"vol": False,
"ylabel": "W m-2",
Expand Down Expand Up @@ -376,9 +376,12 @@ def plot_net_atm_water_imbalance(ax, xlim, exps):
365
* 86400
* (
np.array(exp["annual"]["QFLX"])
np.array(exp["annual"]["QFLX"][0])
- 1e3
* (np.array(exp["annual"]["PRECC"]) + np.array(exp["annual"]["PRECL"]))
* (
np.array(exp["annual"]["PRECC"][0])
+ np.array(exp["annual"]["PRECL"][0])
)
)
),
"verbose": False,
Expand All @@ -392,6 +395,9 @@ def plot(ax, xlim, exps, param_dict):
ax.set_xlim(xlim)
extreme_values = []
for exp in exps:
print("exp[annual]=")
print(exp["annual"].keys())
# dict_keys(['TREFHT', 'year', 'FSNTOA', 'FLUT', 'PRECC', 'PRECL', 'QFLX', 'TS', 'FSNT', 'FLNT', 'ohc', 'volume'])
if param_dict["check_exp_ocean"] and (exp["ocean"] is None):
continue
if param_dict["check_exp_vol"] and (exp["vol"] is None):
Expand Down Expand Up @@ -420,7 +426,7 @@ def plot(ax, xlim, exps, param_dict):
# Specifically for plot_toa_radiation
# TODO: if more plots require a 2nd variable, we can change `var` to be a list,
# but that will be a more significant refactoring.
var = np.array(exp["annual"]["FLUT"])
var = np.array(exp["annual"]["FLUT"][0])
ax.plot(year, var, lw=1.0, marker=None, ls=":", c=exp["color"])
continue
if param_dict["check_exp_year"] and exp["yr"] is None:
Expand Down Expand Up @@ -459,7 +465,10 @@ def plot(ax, xlim, exps, param_dict):
ax.axhline(y=param_dict["axhline_y"], lw=1, c="0.5")
ax.set_title(param_dict["title"])
ax.set_xlabel("Year")
ax.set_ylabel(param_dict["ylabel"])
units = param_dict["ylabel"]
if callable(units):
units = units(exps[1]) # How do we know which var's units to put as the ylabel?
ax.set_ylabel(units)
if param_dict["set_legend"]:
ax.legend(loc="best")

Expand All @@ -477,7 +486,8 @@ def plot(ax, xlim, exps, param_dict):


# -----------------------------------------------------------------------------
def run(parameters):
# FIXME: C901 'run' is too complex (21)
def run(parameters): # noqa: C901
# These are the "Tableau 20" colors as RGB.
t20: List[Tuple[float, float, float]] = [
(31, 119, 180),
Expand Down Expand Up @@ -566,23 +576,20 @@ def run(parameters):
ts = TS(exp["atmos"])
exp["annual"] = {}
for var in vars:
print(var)
try:
print("AAA")
v = ts.globalAnnual(var)
print("BBB")
except:
v, units = ts.globalAnnual(var)
except Exception:
print(f"Invalid var ={var}")
counter_invalid_vars += 1
continue
print("CCC")
if len(v.shape) > 1:
# number of years x 3 regions = v.shape
# 3 regions = global, northern hemisphere, southern hemisphere
# We get here if we used the updated `ts` task
# (using `rgn_avg` rather than `glb_avg`).
v = v[:, 0] # Just use 1st column (global)
exp["annual"][var] = v
print(f"var={var} units={units}")
exp["annual"][var] = (v, units)
if "year" not in exp["annual"]:
time = v.getTime()
exp["annual"]["year"] = [x.year for x in time.asComponentTime()]
Expand All @@ -591,13 +598,13 @@ def run(parameters):
# Optionally read ohc
if exp["ocean"] is not None:
ts = TS(exp["ocean"])
exp["annual"]["ohc"] = ts.globalAnnual("ohc")
exp["annual"]["ohc"], _ = ts.globalAnnual("ohc")
# annomalies with respect to first year
exp["annual"]["ohc"][:] = exp["annual"]["ohc"][:] - exp["annual"]["ohc"][0]

if exp["vol"] is not None:
ts = TS(exp["vol"])
exp["annual"]["volume"] = ts.globalAnnual("volume")
exp["annual"]["volume"], _ = ts.globalAnnual("volume")
# annomalies with respect to first year
exp["annual"]["volume"][:] = (
exp["annual"]["volume"][:] - exp["annual"]["volume"][0]
Expand Down Expand Up @@ -636,7 +643,7 @@ def run(parameters):
ax = plt.subplot(nrows, ncols, j + 1)
try:
plot_generic(ax, xlim, exps, extra_plots[counter_extra_plots])
except:
except Exception:
print(f"Invalid var={extra_plots[counter_extra_plots]}")
counter_extra_plots += 1

Expand Down
35 changes: 19 additions & 16 deletions zppy/templates/readTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,56 @@ def __del__(self):

def globalAnnual(self, var):

units = None

# Constants, from AMWG diagnostics
Lv = 2.501e6
Lf = 3.337e5

# Is this a derived variable?
if var == "RESTOM":

FSNT = self.globalAnnual("FSNT")
FLNT = self.globalAnnual("FLNT")
FSNT, _ = self.globalAnnual("FSNT")
FLNT, _ = self.globalAnnual("FLNT")
v = FSNT - FLNT

elif var == "RESTOA":

print("NOT READY")
FSNTOA = self.globalAnnual("FSNTOA")
FLUT = self.globalAnnual("FLUT")
FSNTOA, _ = self.globalAnnual("FSNTOA")
FLUT, _ = self.globalAnnual("FLUT")
v = FSNTOA - FLUT

elif var == "LHFLX":

QFLX = self.globalAnnual("QFLX")
PRECC = self.globalAnnual("PRECC")
PRECL = self.globalAnnual("PRECL")
PRECSC = self.globalAnnual("PRECSC")
PRECSL = self.globalAnnual("PRECSL")
QFLX, _ = self.globalAnnual("QFLX")
PRECC, _ = self.globalAnnual("PRECC")
PRECL, _ = self.globalAnnual("PRECL")
PRECSC, _ = self.globalAnnual("PRECSC")
PRECSL, _ = self.globalAnnual("PRECSL")
v = (Lv + Lf) * QFLX - Lf * 1.0e3 * (PRECC + PRECL - PRECSC - PRECSL)

elif var == "RESSURF":

FSNS = self.globalAnnual("FSNS")
FLNS = self.globalAnnual("FLNS")
SHFLX = self.globalAnnual("SHFLX")
LHFLX = self.globalAnnual("LHFLX")
FSNS, _ = self.globalAnnual("FSNS")
FLNS, _ = self.globalAnnual("FLNS")
SHFLX, _ = self.globalAnnual("SHFLX")
LHFLX, _ = self.globalAnnual("LHFLX")
v = FSNS - FLNS - SHFLX - LHFLX

elif var == "PREC":

PRECC = self.globalAnnual("PRECC")
PRECL = self.globalAnnual("PRECL")
PRECC, _ = self.globalAnnual("PRECC")
PRECL, _ = self.globalAnnual("PRECL")
v = 1.0e3 * (PRECC + PRECL)

else:

# Read variable
v = self.f(var)
units = v.units

# Annual average
v = cdutil.YEAR(v)

return v
return v, units

0 comments on commit 5fd2055

Please sign in to comment.