Skip to content

Commit

Permalink
METplus-Internal issue #57 modified logging for skew T to use common …
Browse files Browse the repository at this point in the history
…logger
  • Loading branch information
bikegeek committed Sep 30, 2024
1 parent 3904ca5 commit 44cc6c2
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions metplotpy/plots/skew_t/skew_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@
import re
import logging
import warnings
import shutil
from datetime import datetime

import pandas
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from metpy.plots import SkewT
import yaml

from metplotpy.plots import util

logger = logging.getLogger(__name__)
# Suppress debug messages from Matplotlib
logging.getLogger(name='matplotlib').setLevel(logging.ERROR)

"""
Generate a skew T-log P diagram from TC Diag output
Currently supports reading in TC Diag ASCII.
Expand Down Expand Up @@ -444,7 +439,7 @@ def retrieve_height(sounding_data: pd.DataFrame, hour_of_interest: str,
return hgt_by_hour


def check_for_all_na(soundings_df: pandas.DataFrame) -> bool:
def check_for_all_na(soundings_df: pandas.DataFrame, logger) -> bool:
''' Checks if the file consists entirely of the fill/missing data value of 9999.
Args:
Expand Down Expand Up @@ -488,7 +483,7 @@ def check_list_for_all_nan(input_list: list) -> bool:
return False


def create_skew_t(input_file: str, config: dict) -> None:
def create_skew_t(input_file: str, config: dict, logger: logging) -> None:
'''
Create a skew T diagram from the TC Diag output. Check for a file that is
Expand All @@ -501,6 +496,7 @@ def create_skew_t(input_file: str, config: dict) -> None:
indicated in the configuration file.
config: A dictionary representation of the settings and their values
in the configuration file.
logger: A common logger
Return:
None, generate plots as png files in the specified output file directory.
Expand All @@ -518,7 +514,7 @@ def create_skew_t(input_file: str, config: dict) -> None:
sounding_df, plevs = extract_sounding_data(input_file, config['output_directory'])

# Check if sounding data consists entirely of na-values.
all_na = check_for_all_na(sounding_df)
all_na = check_for_all_na(sounding_df, logger)
if all_na:
logger.warning(f"NO DATA to plot for {file_only}. NO PLOT GENERATED.")
return
Expand Down Expand Up @@ -671,7 +667,6 @@ def create_skew_t(input_file: str, config: dict) -> None:
filename_only = os.path.basename(full_filename_only)
renamed = filename_only + "_" + cur_time + "_hr.png"
plot_file = os.path.join(output_dir, renamed)
logging.info(f"Saving file {plot_file}")

plt.savefig(plot_file)
# Close any open figures to prevent having too many open figures.
Expand All @@ -693,38 +688,23 @@ def main(config_filename=None):
config = util.get_params(config_filename)

# Set up the logging.
start = datetime.now()
log_dir = config['log_directory']
log_file = config['log_filename']
log_level = config['log_level']
log_full_path = os.path.join(log_dir, log_file)
logger = util.get_common_logger( log_level, log_full_path)

try:
os.makedirs(log_dir, exist_ok=True)
except FileExistsError:
# If directory already exists, this is OK. Continue.
pass

log_level = config['log_level']
format_str = "'%(asctime)s||%(levelname)s||%(funcName)s||%(message)s'"
if log_level == 'DEBUG':
logging.basicConfig(filename=log_full_path, level=logging.DEBUG,
format=format_str,
filemode='w')
elif log_level == 'INFO':
logging.basicConfig(filename=log_full_path, level=logging.INFO,
format=format_str,
filemode='w')
elif log_level == 'WARNING':
logging.basicConfig(filename=log_full_path, level=logging.WARNING,
format=format_str,
filemode='w')
else:
# log_level == 'ERROR'
logging.basicConfig(filename=log_full_path, level=logging.ERROR,
format=format_str,
filemode='w')

# Get the list of input files to visualize.
input_dir = config['input_directory']
file_ext = config['input_file_extension']
logger.info("Skew-T log-P plotting")
files_of_interest = []

for root, _, files in os.walk(input_dir):
Expand All @@ -733,8 +713,11 @@ def main(config_filename=None):
files_of_interest.append(os.path.join(root, item))
# Create skew T diagrams for each input file.
for file_of_interest in files_of_interest:
create_skew_t(file_of_interest, config)
logger.info(f"Creating a skew T diagram for {file_of_interest}")
create_skew_t(file_of_interest, config, logger)

total_time = datetime.now() - start
logger.info(f"All skew T- log P plotting completed in {total_time} seconds ")

if __name__ == "__main__":
main()

0 comments on commit 44cc6c2

Please sign in to comment.