#!/usr/bin/env python3
r"""
Creates a "quick-look" plot from KPP-Standalone box model output.
Command-line arguments
----------------------
.. option:: -d <str>, --dirname <str>
Folder containing KPP-Standalone output files.
.. option:: [-l <str>, --label <str>]
Descriptive label used in the plot legend.
Default: ``"KPP-Standalone output"``
.. option:: [-p <str>, --pattern <str>]
Glob pattern used to match KPP-Standalone log filenames.
.. option:: [-s <str>, --species <str>]
Name of the species to plot.
Examples
--------
.. code-block:: console
$ conda activate gcpy_env
(gcpy_env) $ python -m gcpy.kpp.kppsa_quick_look \
--dirname /path/to/KPP-Standalone/output \
--label Rosenbrock \
--pattern Beijing*20190701_0040.log \
--species O3
"""
# Imports
import argparse
import matplotlib.pyplot as plt
from gcpy.util import verify_variable_type
from gcpy.kpp.kppsa_utils import \
kppsa_get_file_list, kppsa_get_unique_site_names, \
kppsa_plot_single_site, kppsa_prepare_site_data, \
kppsa_read_csv_files
[docs]
def kppsa_make_quick_look_plot(file_list, label, species):
"""
Creates a quick-look plot from KPP-Standalone box model output.
The site name is determined automatically from the first unique
site found in the data files. Only a single site is plotted.
Parameters
----------
file_list : list of str
Paths to KPP-Standalone log files to be read.
label : str
Descriptive label for the data, used in the plot legend.
species : str
Name of the species to plot.
Notes
-----
The plot is displayed interactively via :func:`matplotlib.pyplot.show`.
The seaborn ``darkgrid`` style is applied during plotting and reset
to ``default`` afterwards to avoid affecting other plotting scripts.
The figure is landscape-oriented (11" × 8").
"""
verify_variable_type(file_list, list)
verify_variable_type(label, str)
verify_variable_type(species, str)
# Read data
dframe = kppsa_read_csv_files(file_list)
# Get the site name from the DataFrame
site_name = kppsa_get_unique_site_names(dframe)[0]
# Get the data for the given species and site
site_data, site_title = kppsa_prepare_site_data(
dframe,
site_name,
species,
)
# Figure setup — landscape width: 11" x 8"
plt.style.use("seaborn-v0_8-darkgrid")
fig = plt.figure(figsize=(11, 8))
fig.tight_layout()
# Plot species vertical profile at the site
kppsa_plot_single_site(
fig,
rows_per_page=1,
cols_per_page=1,
subplot_index=0,
subplot_title=site_title,
ref_data=site_data,
ref_label=label,
dev_data=None,
dev_label=None,
species=species,
font_scale=2.0,
)
# Add top-of-page legend
plt.legend(
ncol=3,
bbox_to_anchor=(0.5, 0.98),
bbox_transform=fig.transFigure,
loc='upper center'
)
# Show the plot
plt.show()
# Reset the plot style (this prevents the seaborn style from
# being applied to other model vs. obs plotting scripts)
plt.style.use("default")
[docs]
def main():
"""
Parses command-line arguments and calls
:func:`kppsa_make_quick_look_plot` to generate a quick-look
plot from KPP-Standalone box model output.
"""
# Tell the parser which arguments to look for
parser = argparse.ArgumentParser(
description="Single-panel plotting example program"
)
parser.add_argument(
"-d", "--dirname",
metavar="DIRNAME",
type=str,
required=True,
help="Directory containing KPP-Standalone output files"
)
parser.add_argument(
"-l", "--label",
metavar="LABEL",
type=str,
required=False,
help="Descriptive label",
default="KPP-Standalone output"
)
parser.add_argument(
"-p", "--pattern",
metavar="PATTERN",
type=str,
required=False,
help="Search for file names matching this pattern",
)
parser.add_argument(
"-s", "--species",
metavar="SPECIES",
type=str,
required=True,
help="Species to plot"
)
# Parse command-line arguments
args = parser.parse_args()
# Get a list of KPP-Standalone files matching the criteria
file_list = kppsa_get_file_list(
args.dirname,
args.pattern,
)
# Create the quick look plot from KPP-Standalone output
kppsa_make_quick_look_plot(
file_list,
args.label,
args.species
)
if __name__ == '__main__':
main()