Skip to content

Commit

Permalink
Feature internal 57 logging remaining (#468)
Browse files Browse the repository at this point in the history
* Internal Issue #57 added logging support and changed names of settings to enable the plot to work with the current inheritance structure.

* Internal Issue #57 logging support

* Internal Issue #57 updates for logging and to work with current inheritance structure

* Replace printing to stdout with logging for both exceptions and warnings, and instead of logging when there is an error with reading the config file, exit.

* remove unused catch_warnings()

* Added logging and better error handling

* Added logging settings with description

* Remove unused logging assignment

* Make config file generic

* METplus-Internal #57 Added logging for the polar ice plot script

* METplus-Internal issue #57 modified logging for skew T to use common logger

* Added default config file to test

* Added the default config filename to constructor

* Fix import statement

* Address a SonarQube issue in make_maps() wrt logger

* Update metplotpy/plots/polar_plot/polar_ice.yaml

Co-authored-by: Julie Prestopnik <jpresto@ucar.edu>

---------

Co-authored-by: Julie Prestopnik <jpresto@ucar.edu>
  • Loading branch information
bikegeek and jprestop authored Oct 1, 2024
1 parent 5c189be commit 66768ef
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 231 deletions.
42 changes: 36 additions & 6 deletions metplotpy/contributed/tc_rmw/plot_cross_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
__author__ = 'David Fillmore'

import os
import sys
import argparse
import datetime
import yaml
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

matplotlib.use('Agg')

from metplotpy.plots import util

def plot_cross_section(config, data_set, args):
"""
Expand All @@ -44,7 +46,24 @@ def plot_cross_section(config, data_set, args):
# Keep the use of 'ax' as this is commonly used
# pylint: disable=invalid-name

# Use default log level of INFO if unspecified
if args.loglevel is not None:
log_level = args.loglevel
else:
log_level = "INFO"

plot_outdir = args.plotdir
start = datetime.datetime.now()
# Create the log file using the same name as the plot name with '_log' and in the
# same location.
try:
os.makedirs(plot_outdir, exist_ok=True)
except FileExistsError:
pass
log_filename = os.path.join(plot_outdir, config['plot_filename'] + '_log' + '.txt')
logger = util.get_common_logger(log_level, log_filename)

logger.info("Begin plotting the cross-section")
plot_width = config['plot_size_width']
plot_height = config['plot_size_height']
fig, ax = plt.subplots(figsize=(plot_width, plot_height))
Expand All @@ -56,7 +75,6 @@ def plot_cross_section(config, data_set, args):
# (Github issue:https://github.com/dtcenter/METcalcpy/issues/308)
field_azi_mean = np.mean(field, axis=0)[:, :, itime]


# originally, the transpose of the field_azi_mean was used, but this is no
# longer necessary. If the transpose is used, the dimensions are incorrect
# and a TypeError will be raised by the contour plot.
Expand All @@ -80,10 +98,13 @@ def plot_cross_section(config, data_set, args):
config['y_tick_stepsize']))
ax.set_yscale(config['y_scale'])
ax.set_ylim(config['y_lim_start'], config['y_lim_end'])
plot_outdir = args.plotdir

logger.info("Saving cross-section plot")
fig.savefig(os.path.join(plot_outdir, config['plot_filename'] + '.png'), dpi=config['plot_res'])
fig.savefig(os.path.join(plot_outdir, config['plot_filename'] + '.pdf'))

logger.info(f"Finished generating cross-section plot in {datetime.datetime.now() - start} seconds")


if __name__ == '__main__':
parser = argparse.ArgumentParser(
Expand All @@ -101,17 +122,26 @@ def plot_cross_section(config, data_set, args):
parser.add_argument('--config', type=str,
required=True,
help='configuration file')
parser.add_argument('--loglevel', type=str,
required=False)

input_args = parser.parse_args()

"""
Read YAML configuration file
"""
plotting_config = yaml.load(
open(input_args.config), Loader=yaml.FullLoader)
try:
plotting_config = yaml.load(
open(input_args.config), Loader=yaml.FullLoader)
except yaml.YAMLError as exc:
sys.exit(1)

"""
Read dataset and call for plotting
"""
input_data = xr.open_dataset(os.path.join(input_args.datadir, input_args.filename))
try:
input_data = xr.open_dataset(os.path.join(input_args.datadir, input_args.filename))
except (ValueError, FileNotFoundError, PermissionError):
sys.exit(1)
plot_cross_section(plotting_config, input_data, input_args)

15 changes: 10 additions & 5 deletions metplotpy/contributed/tc_rmw/test_plot_cross_section.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
# ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
# ============================*

export datadir=/path/to/vertically-interpolated-input-data
export plotdir=/path/to/output/plots
export filename=tc_rmw_example_vertical_interp.nc
export configfile=/path/to/configuration-file
export datadir=/Users/minnawin/AF_STIG/feature_57_METplotpy_logging/METplotpy/metplotpy/contributed/tc_rmw/Data
export plotdir=/Users/minnawin/AF_STIG/feature_57_METplotpy_logging/METplotpy/metplotpy/contributed/tc_rmw/plots
export filename=vertically_interpolated.nc
export configfile=/Users/minnawin/AF_STIG/feature_57_METplotpy_logging/METplotpy/metplotpy/contributed/tc_rmw/plot_cross_section.yaml
# Default is set to INFO in code, set to any other value here and add to the arguments
# in the call to the plot_cross_section.py below
export loglevel="ERROR"


python plot_cross_section.py \
--datadir=$datadir \
--plotdir=$plotdir \
--filename=$filename \
--config=$configfile
--config=$configfile \
# --loglevel=$loglevel
15 changes: 12 additions & 3 deletions metplotpy/plots/config/scatter_defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@ scatters:
- name: Scatter 1
data_file: ./scatter1_plot_data.txt
color: black
width: 1
dash:
line_width: 1
type: horiz_line
position: 0
line_style: dash

- name: Scatter 2
data_file: ./scatter2_plot_data.txt
color: black
line_width: 1
type: horiz_line
position: 0
line_style: dash

log_level: ERROR
log_filename: ./scatter_log.txt
width: 1
dash:

log_filename: stdout
log_level: ERROR

line_type: N/A
line_type: N/A
4 changes: 3 additions & 1 deletion metplotpy/plots/polar_plot/polar_ice.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
input_file: "~/grid_stat_north_000000L_20210305_120000V_pairs.nc"
input_file: "path-to-data/grid_stat_north_000000L_20210305_120000V_pairs.nc"
forecast_netcdf_var_name: "FCST_ice_coverage_SURFACE_FULL"
obs_netcdf_var_name: "OBS_ice_coverage_SURFACE_FULL"
diff_netcdf_var_name: "DIFF_ice_coverage_SURFACE_ice_coverage_SURFACE_FULL"
log_level: info
log_filename: ./polar_ice_log.txt
Loading

0 comments on commit 66768ef

Please sign in to comment.