Skip to content

Commit 9823781

Browse files
committed
Moved KPP-Standalone plotting code to gcpy/kpp folder
gcpy/examples/__init__.py - Removed "from ,kpp import*" gcpy/kpp/__init__.py - Added this import script for the gcpy/kpp folder gcpy/kpp/kppsa_utils.py - Moved most of the routines from the former gcpy/examples/kpp/kppsa_visualize.py script here so that they can be reused gcpy/kpp/kppsa_plot_sites.py - Added this script to plot KPP-Standalone box model output (Ref vs. Dev versions) from 500-1000 hPa. Based on the former kppsa_visualize.py script. gcpy/kpp/kppsa_quick_look.py - Added this script to create "quick-look" output from the KPP-Standalone box model output CHANGELOG.md - Updated accordingly Signed-off-by: Bob Yantosca <yantosca@seas.harvard.edu>
1 parent 598514c commit 9823781

File tree

7 files changed

+744
-2
lines changed

7 files changed

+744
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010
- Added module `benchmark_gcclassic_stats.py` for scraping statistics from GEOS-Chem Classic cloud benchmarks
1111
- Added dry deposition velocity comparison plots in 1-month cloud benchmarks
1212
- Added `gcpy/benchmark/modules/benchmark_species_changes.py` to compute the table of species changes between versions
13-
- Added `gcpy/examples/kpp/kppsa_visualize.py` script to plot output from the KPP-Standalone box model
13+
- Added `gcpy/kpp/` folder containing scripts to plot output from the KPP-Standalone box model
1414

1515
### Changed
1616
- Changed format of `% diff` column from `12.3e` to `12.3f` in benchmark timing tables

gcpy/examples/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#from .bpch_to_nc import *
66
from .diagnostics import *
77
from .dry_run import *
8-
from .kpp import *
98
from .plotting import *
109
from .timeseries import *
1110
from .working_with_files import *

gcpy/kpp/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
GCPy import script
3+
"""
4+
from .kppsa_utils import *
5+
from .kppsa_quick_look import *
6+
from .kppsa_plot_sites import *

gcpy/kpp/kppsa_plot_sites.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Script to visualize output from the KPP standalone box model.
4+
"""
5+
6+
# Imports
7+
import argparse
8+
from matplotlib.backends.backend_pdf import PdfPages
9+
import matplotlib.pyplot as plt
10+
from gcpy.util import verify_variable_type
11+
from gcpy.kpp.kppsa_utils import \
12+
kppsa_get_file_list, kppsa_get_unique_site_names,\
13+
kppsa_plot_one_page, kppsa_read_csv_files
14+
15+
16+
def kppsa_plot_species_at_sites(
17+
ref_file_list,
18+
ref_label,
19+
dev_file_list,
20+
dev_label,
21+
species,
22+
pdfname,
23+
):
24+
"""
25+
TBD
26+
"""
27+
verify_variable_type(ref_file_list, list)
28+
verify_variable_type(ref_label, str)
29+
verify_variable_type(dev_file_list, list)
30+
verify_variable_type(dev_label, str)
31+
verify_variable_type(species, str)
32+
verify_variable_type(pdfname, str)
33+
34+
# Read data
35+
ref_data = kppsa_read_csv_files(ref_file_list)
36+
dev_data = kppsa_read_csv_files(dev_file_list)
37+
38+
# Get a list of site names sorted from N to S
39+
site_names = kppsa_get_unique_site_names(ref_data)
40+
41+
# Figure setup
42+
plt.style.use("seaborn-v0_8-darkgrid")
43+
rows_per_page = 3
44+
cols_per_page = 2
45+
plots_per_page = rows_per_page * cols_per_page
46+
47+
# Open the plot as a PDF document
48+
if ".pdf" not in pdfname:
49+
pdfname += ".pdf"
50+
pdf = PdfPages(f"{pdfname}")
51+
52+
# Loop over the number of obs sites that fit on a page
53+
for start in range(0, len(site_names), plots_per_page):
54+
end = start + plots_per_page - 1
55+
kppsa_plot_one_page(
56+
pdf,
57+
site_names[start:end+1],
58+
ref_data,
59+
ref_label,
60+
dev_data,
61+
dev_label,
62+
species,
63+
rows_per_page,
64+
cols_per_page,
65+
font_scale=1.0,
66+
)
67+
68+
# Close the PDF file
69+
pdf.close()
70+
71+
# Reset the plot style (this prevents the seaborn style from
72+
# being applied to other model vs. obs plotting scripts)
73+
plt.style.use("default")
74+
75+
76+
def main():
77+
"""
78+
TBD
79+
"""
80+
# Tell the parser which arguments to look for
81+
parser = argparse.ArgumentParser(
82+
description="Single-panel plotting example program"
83+
)
84+
parser.add_argument(
85+
"--refdir",
86+
metavar="REFDIR",
87+
type=str,
88+
required=True,
89+
help="Directory w/ KPP-Standalone log files (Ref version)"
90+
)
91+
parser.add_argument(
92+
"--reflabel",
93+
metavar="REFLABEL",
94+
type=str,
95+
required=False,
96+
help="Descriptive label for the Ref data",
97+
default="Ref"
98+
)
99+
parser.add_argument(
100+
"--devdir",
101+
metavar="DEVDIR",
102+
type=str,
103+
required=True,
104+
help="Directory w/ KPP-Standalone log files (Dev version)"
105+
)
106+
parser.add_argument(
107+
"--devlabel",
108+
metavar="DEVLABEL",
109+
type=str,
110+
required=False,
111+
help="Descriptive label for the Ref data",
112+
default="Dev"
113+
)
114+
parser.add_argument(
115+
"--pattern",
116+
metavar="PATTERN",
117+
type=str,
118+
required=False,
119+
help="Search for file names matching this pattern",
120+
)
121+
parser.add_argument(
122+
"--species",
123+
metavar="SPECIES",
124+
type=str,
125+
required=True,
126+
help="Species to plot"
127+
)
128+
parser.add_argument(
129+
"--pdfname",
130+
metavar="PDF-FILE-NAME",
131+
type=str,
132+
required=False,
133+
help="Name of the PDF file to be created",
134+
default="kppsa_output.pdf"
135+
)
136+
137+
# Parse command-line arguments
138+
args = parser.parse_args()
139+
140+
# Get a list of KPP-Standalone files matching the criteria (Ref)
141+
ref_file_list = kppsa_get_file_list(
142+
args.refdir,
143+
args.pattern,
144+
)
145+
if len(ref_file_list) == 0:
146+
msg = "Could not find any files matching {pattern} for Ref!"
147+
raise ValueError(msg)
148+
149+
dev_file_list = kppsa_get_file_list(
150+
args.devdir,
151+
args.pattern,
152+
)
153+
if len(ref_file_list) == 0:
154+
msg = "Could not find any files matching {pattern} for Dev!"
155+
raise ValueError(msg)
156+
157+
# Plot data
158+
kppsa_plot_species_at_sites(
159+
ref_file_list,
160+
args.reflabel,
161+
dev_file_list,
162+
args.devlabel,
163+
args.species,
164+
args.pdfname,
165+
)
166+
167+
if __name__ == '__main__':
168+
main()

gcpy/kpp/kppsa_quick_look.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Creates a "quick-look" plot from KPP-Standalone box model output.
4+
"""
5+
6+
# Imports
7+
import argparse
8+
import matplotlib.pyplot as plt
9+
from gcpy.util import verify_variable_type
10+
from gcpy.kpp.kppsa_utils import \
11+
kppsa_get_file_list, kppsa_get_unique_site_names, \
12+
kppsa_plot_single_site, kppsa_prepare_site_data, \
13+
kppsa_read_csv_files
14+
15+
16+
def kppsa_make_quick_look_plot(file_list, label, species):
17+
"""
18+
Creates a quick-look plot from KPP-Standalone box model output.
19+
20+
Args
21+
file_list : list : List of KPP-Standalone log files
22+
site_name : str : Name of the site that you wish to plot
23+
label : str : Descriptive label for the data
24+
species : str : Name of the species that you wish to plot
25+
"""
26+
verify_variable_type(file_list, list)
27+
verify_variable_type(label, str)
28+
verify_variable_type(species, str)
29+
30+
# Read data
31+
dframe = kppsa_read_csv_files(file_list)
32+
33+
# Get the site name from the DataFrame
34+
site_name = kppsa_get_unique_site_names(dframe)[0]
35+
36+
# Get the data for the given species and site
37+
site_data, site_title = kppsa_prepare_site_data(
38+
dframe,
39+
site_name,
40+
species,
41+
)
42+
43+
# Figure setup
44+
plt.style.use("seaborn-v0_8-darkgrid")
45+
46+
# Define a new matplotlib.figure.Figure object for this page
47+
# Landscape width: 11" x 8"
48+
fig = plt.figure(figsize=(11, 8))
49+
fig.tight_layout()
50+
51+
# Figure setup
52+
plt.style.use("seaborn-v0_8-darkgrid")
53+
54+
# Plot species vertical profile at a given site
55+
kppsa_plot_single_site(
56+
fig,
57+
rows_per_page=1,
58+
cols_per_page=1,
59+
subplot_index=0,
60+
subplot_title=site_title,
61+
ref_data=site_data,
62+
ref_label=label,
63+
dev_data=None,
64+
dev_label=None,
65+
species=species,
66+
font_scale=2.0,
67+
)
68+
69+
# Add top-of-page legend
70+
plt.legend(
71+
ncol=3,
72+
bbox_to_anchor=(0.5, 0.98),
73+
bbox_transform=fig.transFigure,
74+
loc='upper center'
75+
)
76+
77+
# Show the plot
78+
plt.show()
79+
80+
# Reset the plot style (this prevents the seaborn style from
81+
# being applied to other model vs. obs plotting scripts)
82+
plt.style.use("default")
83+
84+
85+
def main():
86+
"""
87+
Parses arguments and calls function kppsa_make_quick_look_plot
88+
to generate a "quick-look" plot from KPP-Standalone box model
89+
output.
90+
"""
91+
92+
# Tell the parser which arguments to look for
93+
parser = argparse.ArgumentParser(
94+
description="Single-panel plotting example program"
95+
)
96+
parser.add_argument(
97+
"-d", "--dirname",
98+
metavar="DIRNAME",
99+
type=str,
100+
required=True,
101+
help="Directory containing KPP-Standalone output files"
102+
)
103+
parser.add_argument(
104+
"-l", "--label",
105+
metavar="LABEL",
106+
type=str,
107+
required=False,
108+
help="Descriptive label",
109+
default="KPP-Standalone output"
110+
)
111+
parser.add_argument(
112+
"-p", "--pattern",
113+
metavar="PATTERN",
114+
type=str,
115+
required=False,
116+
help="Search for file names matching this pattern",
117+
)
118+
parser.add_argument(
119+
"-s", "--species",
120+
metavar="SPECIES",
121+
type=str,
122+
required=True,
123+
help="Species to plot"
124+
)
125+
126+
# Parse command-line arguments
127+
args = parser.parse_args()
128+
129+
# Get a list of KPP-Standalone files matching the criteria
130+
file_list = kppsa_get_file_list(
131+
args.dirname,
132+
args.pattern,
133+
)
134+
135+
# Create the quick look plot from KPP-Standalone output
136+
kppsa_make_quick_look_plot(
137+
file_list,
138+
args.label,
139+
args.species
140+
)
141+
142+
143+
if __name__ == '__main__':
144+
main()

0 commit comments

Comments
 (0)