Skip to content

Commit

Permalink
Feature 1650 update hovmoeller (#1666)
Browse files Browse the repository at this point in the history
Co-authored-by: George McCabe <23407799+georgemccabe@users.noreply.github.com>
  • Loading branch information
bikegeek and georgemccabe authored Jun 22, 2022
1 parent 3ea6668 commit 41500be
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
# This use case runs the UserScript wrapper tool to run a user provided script,
# in this case, hovmoeller.py.
#
# It also requires the METcalcpy and METplotpy source code to generate the plot.
# Clone the METcalcpy repository (https://github.com/dtcenter/METcalcpy) and the
# METplotpy repository (https://github.com/dtcenter/METplotpy) under the same base
# directory as the METPLUS_BASE directory so that the METplotpy, METcalcpy, and
# METplotpy directories are under the same base directory (i.e. if the METPLUS_BASE directory is
# /home/username/working/METplus, then clone the METcalcpy and METplotpy source
# code into the /home/username/working directory).
#


##############################################################################
# METplus Workflow
Expand All @@ -48,7 +57,7 @@
#
# METplus first loads all of the configuration files found in parm/metplus_config,
# then it loads any configuration files passed to METplus via the command line
# with the -c option, i.e. -c parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf
# parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf
#
# .. highlight:: bash
# .. literalinclude:: ../../../../parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf
Expand Down Expand Up @@ -78,14 +87,14 @@
# then a user-specific system configuration file::
#
# run_metplus.py \
# -c /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf \
# -c /path/to/user_system.conf
# /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf \
# /path/to/user_system.conf
#
# 2) Modifying the configurations in parm/metplus_config, then passing in UserScript_obsPrecip_obsOnly_Hovmoeller.conf::
#
# run_metplus.py \
# -c /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf
#
# /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf
#
# The former method is recommended. Whether you add them to a user-specific configuration file or modify the metplus_config files, the following variables must be set correctly:
#
# * **INPUT_BASE** - Path to directory where sample data tarballs are unpacked (See Datasets section to obtain tarballs). This is not required to run METplus, but it is required to run the examples in parm/use_cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ LOG_FILE = "Hovmoeller_diagram.log"

LOG_LEVEL = "INFO"

INPUT_FILE_NAME = {INPUT_BASE}/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller/precip.erai.sfc.1p0.2x.2014-2016.nc
YAML_CONFIG_NAME = {PARM_BASE}/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller/hovmoeller.yaml

INPUT_FILE_NAME = {INPUT_BASE}/model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller/precip.erai.sfc.1p0.2x.2014-2016.nc

METPLOTPY_BASE = {METPLUS_BASE}/../METplotpy
OUTPUT_DIR = {OUTPUT_BASE}/plots
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
height: 800
width: 1200
plot_filename: !ENV '${OUTPUT_DIR}/erai_precip2.png'
input_data_file: !ENV '${INPUT_FILE_NAME}'
plot_height: 800
plot_width: 1200
title: ERAI Precipitation

font_size: 20
xy_label_font_size: 20
title_size: 20

xaxis_title: Longitude
yaxis_title: Time
xaxis: Longitude
yaxis: Time

date_start: 2016-01-01
date_end: 2016-03-31
Expand All @@ -21,5 +24,3 @@ contour_min: 0.2
contour_max: 1.6
contour_del: 0.2
colorscale: 'BuPu'
plot_filename:
- !ENV '${OUTPUT_DIR}/erai_precip2.png'
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,8 @@
"""
import os
import sys
import logging
import yaml
import xarray as xr # http://xarray.pydata.org/
import metplotpy.plots.hovmoeller.hovmoeller as Hovmoeller
import metcalcpy.util.read_env_vars_in_config as readconfig


def main():
"""
Use existing default Hovmoeller config file found int METplotpy to
create a default plot, using data in the METplus data store
"""

"""
Read METplus config file paramaters
"""
input_file_name = os.environ.get("INPUT_FILE_NAME","precip.erai.sfc.1p0.2x.2014-2016.nc")
plot_config_file = os.getenv("YAML_CONFIG_NAME","hovmoeller.yaml")
input_file = input_file_name

"""
Read Hovmoeller YAML configuration file
"""
try:
config = readconfig.parse_config(plot_config_file)
logging.info(config)
except yaml.YAMLError as exc:
logging.error(exc)


"""
Setup logging
"""
logfile = "Hovmoeller_diagram.log"
logging_level = os.environ.get("LOG_LEVEL","logging.INFO")
logging.basicConfig(stream=logfile, level=logging_level)

# Plot and save difficulty index figures
"""
Read dataset
"""
try:
logging.info('Opening ' + input_file)
ds = xr.open_dataset(input_file)
except IOError as exc:
logging.error('Unable to open ' + input_file)
logging.error(exc)
sys.exit(1)
logging.debug(ds)

data = ds[config['var_name']]
logging.debug(data)

data = data.sel(time=slice(config['date_start'], config['date_end']))
time = ds.time.sel(time=slice(config['date_start'], config['date_end']))
lon = ds.lon

data = data * config['unit_conversion']
data.attrs['units'] = config['var_units']

plot_filename = config['plot_filename'][0]
print("Plot name",plot_filename)
logging.info(plot_filename)

# create output directory if it does not exist
plot_dir = os.path.dirname(plot_filename)
if not os.path.exists(plot_dir):
logging.info(f"Creating output directory: {plot_dir}")
os.makedirs(plot_dir)

custom_param_dict = {"plot_filename": plot_filename}
plot = Hovmoeller.Hovmoeller(custom_param_dict, time, lon, data)
plot.save_to_file()

# check if output file exists since save_to_file doesn't return
# an error code on failure
if not os.path.exists(plot_filename):
logging.error(f"Could not create output file: {plot_filename}")
sys.exit(1)

if __name__ == '__main__':
main()
config_file = os.environ.get('YAML_CONFIG_NAME')
Hovmoeller.main(config_file)

0 comments on commit 41500be

Please sign in to comment.