Skip to content

Commit

Permalink
Add hemispheric time series plots (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
forsyth2 authored Jul 18, 2023
1 parent d3ad171 commit a2a464f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
35 changes: 29 additions & 6 deletions zppy/templates/coupled_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ def plot(ax, xlim, exps, param_dict):


# -----------------------------------------------------------------------------
def run(parameters):
# FIXME: C901 'run' is too complex (19)
def run(parameters, rgn): # noqa: C901
# These are the "Tableau 20" colors as RGB.
t20: List[Tuple[float, float, float]] = [
(31, 119, 180),
Expand Down Expand Up @@ -542,7 +543,15 @@ def run(parameters):
# 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)
if rgn == "glb":
n = 0
elif rgn == "n":
n = 1
elif rgn == "s":
n = 2
else:
raise RuntimeError(f"Invalid rgn={rgn}")
v = v[:, n] # Just use nth column
exp["annual"][var] = v
if "year" not in exp["annual"]:
time = v.getTime()
Expand Down Expand Up @@ -577,7 +586,7 @@ def run(parameters):

i = 0
# https://stackoverflow.com/questions/58738992/save-multiple-figures-with-subplots-into-a-pdf-with-multiple-pages
pdf = matplotlib.backends.backend_pdf.PdfPages(f"{figstr}.pdf")
pdf = matplotlib.backends.backend_pdf.PdfPages(f"{figstr}_{rgn}.pdf")
for page in range(num_pages):
fig = plt.figure(1, figsize=[13.5, 16.5])
for j in range(plots_per_page):
Expand All @@ -592,12 +601,26 @@ def run(parameters):
fig.tight_layout()
pdf.savefig(1)
if num_pages > 1:
fig.savefig(figstr + f"_{page}.png", dpi=150)
fig.savefig(f"{figstr}_{rgn}_{page}.png", dpi=150)
else:
fig.savefig(figstr + ".png", dpi=150)
fig.savefig(f"{figstr}_{rgn}.png", dpi=150)
plt.clf()
pdf.close()


def run_by_region(parameters):
regions = parameters[10].split(",")
for rgn in regions:
if rgn.lower() in ["glb", "global"]:
rgn = "glb"
elif rgn.lower() in ["n", "north", "northern"]:
rgn = "n"
elif rgn.lower() in ["s", "south", "southern"]:
rgn = "s"
else:
raise RuntimeError(f"Invalid rgn={rgn}")
run(parameters, rgn)


if __name__ == "__main__":
run(sys.argv)
run_by_region(sys.argv)
2 changes: 2 additions & 0 deletions zppy/templates/default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ input_subdir = string(default="archive/ocn/hist")
moc_file = string(default="")
# The names of the plots you want displayed
plot_names = string(default="net_toa_flux_restom,global_surface_air_temperature,toa_radiation,net_atm_energy_imbalance,change_ohc,max_moc,change_sea_level,net_atm_water_imbalance")
# regions to plot: glb, n, s (global, northern hemisphere, southern hemisphere)
regions = string(default="glb,n,s")
# The number of years in a time-series file
ts_num_years = integer(default=10)
ts_years = string_list(default=list(""))
Expand Down
4 changes: 2 additions & 2 deletions zppy/templates/global_time_series.bash
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fi

echo 'Update time series figures'
cd ${global_ts_dir}
python coupled_global.py ${case_dir} ${experiment_name} ${figstr} ${start_yr} ${end_yr} {{ color }} ${ts_num_years} ${atmosphere_only} "{{ plot_names }}"
python coupled_global.py ${case_dir} ${experiment_name} ${figstr} ${start_yr} ${end_yr} {{ color }} ${ts_num_years} ${atmosphere_only} "{{ plot_names }}" {{ regions }}
if [ $? != 0 ]; then
cd {{ scriptDir }}
echo 'ERROR (5)' > {{ prefix }}.status
Expand All @@ -99,7 +99,7 @@ echo 'Copy images to directory'
results_dir={{ prefix }}_results
results_dir_absolute_path={{ scriptDir }}/${results_dir}
mkdir -p ${results_dir_absolute_path}
cp ${figstr}.pdf ${results_dir_absolute_path}/${figstr}.pdf
cp *.pdf ${results_dir_absolute_path}
cp *.png ${results_dir_absolute_path}

################################################################################
Expand Down

0 comments on commit a2a464f

Please sign in to comment.