Note
Click here to download the full example code
5.2.9.4. UserScript: Make a Cross Spectra plot
model_applications/ s2s/ UserScript_obsPrecip_obsOnly_CrossSpectraPlot.py
Scientific Objective
This use case calls the METplotpy space time plot to create a sample cross spectra diagram using sample data created by METcalcpy cross spectra functions
The space time plot and cross spectra calculations were created by Maria Gehne at the Physical Sciences Labratory in NOAA
Datasets
METplus Components
This use case runs the UserScript wrapper tool to run a user provided script, in this case, cross_spectra_plot.py.
METplus Workflow
This use case does not loop but plots the entire time period of data
UserScript
METplus Configuration
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_CrossSpectraPlot.conf
[config]
# time looping - options are INIT, VALID, RETRO, and REALTIME
# If set to INIT or RETRO:
# INIT_TIME_FMT, INIT_BEG, INIT_END, and INIT_INCREMENT must also be set
# If set to VALID or REALTIME:
# VALID_TIME_FMT, VALID_BEG, VALID_END, and VALID_INCREMENT must also be set
LOOP_BY = REALTIME
# %Y = 4 digit year, %m = 2 digit month, %d = 2 digit day, etc.
# see www.strftime.org for more information
# %Y%m%d%H expands to YYYYMMDDHH
VALID_TIME_FMT = %Y%m%d%H
# BLank for this usecase but the parameter still needs to be there
VALID_BEG =
# BLank for this usecase but the parameter still needs to be there
VALID_END =
# BLank for this usecase but the parameter still needs to be there
VALID_INCREMENT =
# List of forecast leads to process for each run time (init or valid)
# In hours if units are not specified
# If unset, defaults to 0 (don't loop through forecast leads)
LEAD_SEQ =
# Order of loops to process data - Options are times, processes
# Not relevant if only one item is in the PROCESS_LIST
# times = run all wrappers in the PROCESS_LIST for a single run time, then
# increment the run time and run all wrappers again until all times have
# been evaluated.
# processes = run the first wrapper in the PROCESS_LIST for all times
# specified, then repeat for the next item in the PROCESS_LIST until all
# wrappers have been run
LOOP_ORDER = processes
PROCESS_LIST = UserScript
USER_SCRIPT_RUNTIME_FREQ = RUN_ONCE
USER_SCRIPT_COMMAND = {PARM_BASE}/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot/cross_spectra_plot.py
[user_env_vars]
# Difficulty index specific variables
LOG_FILE = "cross_spectra_plot.log"
LOG_LEVEL = "INFO"
INPUT_FILE_NAMES = {INPUT_BASE}/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot/SpaceTimeSpectra_ERAI_P_D200_symm_2spd.nc,{INPUT_BASE}/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot/SpaceTimeSpectra_ERAI_TRMM_P_symm_2spd.nc,{INPUT_BASE}/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot/SpaceTimeSpectra_ERAI_P_D850_symm_2spd.nc
METPLOTPY_BASE = {METPLUS_BASE}/METplotpy/metplotpy/
YAML_CONFIG_NAME = {METPLUS_BASE}/parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot/spectra_plot.yaml
OUTPUT_DIR = {OUTPUT_BASE}/plots/
MET Configuration
There are no MET tools used in this use case.
Python Embedding
There is no python embedding in this use case
Python Scripts
This use case uses a Python script to perform plotting
#!/usr/bin/env python3
"""
This is an example script for plotting cross spectral components. The script reads in output files computed
by the example_cross_spectra.py script and uses the plotly plotting routines in spacetime_plot.py to generate
a panel plot of coherence spectra.
"""
import numpy as np
import os
import xarray as xr
import metplotpy.contributed.spacetime_plot.spacetime_plot as stp
import metcalcpy.util.read_env_vars_in_config as readconfig
# Read in the YAML config file
# user can use their own, if none specified at the command line,
# use the "default" example YAML config file, spectra_plot_coh2.py
# Using a custom YAML reader so we can use environment variables
plot_config_file = os.getenv("YAML_CONFIG_NAME","spectra_plot.yaml")
config_dict = readconfig.parse_config(plot_config_file)
# Retrieve settings from config file
#pathdata is now set in the METplus conf file
#pathdata = config_dict['pathdata'][0]
plotpath = config_dict['plotpath'][0]
print("Output path ",plotpath)
# plot layout parameters
flim = 0.5 # maximum frequency in cpd for plotting
nWavePlt = 20 # maximum wavenumber for plotting
contourmin = 0.1 # contour minimum
contourmax = 0.8 # contour maximum
contourspace = 0.1 # contour spacing
N = [1, 2] # wave modes for plotting
source = ""
spd = 2
symmetry = "symm" #("symm", "asymm", "latband")
filenames = os.environ.get("INPUT_FILE_NAMES","ERAI_TRMM_P_symn,ERAI_P_D850_symn,ERAI_P_D200_symn").split(",")
#filenames = ['ERAI_TRMM_P_symm_'+str(spd)+'spd',
# 'ERAI_P_D850_symm_'+str(spd)+'spd',
# 'ERAI_P_D200_symm_'+str(spd)+'spd']
vars1 = ['ERAI P', 'ERAI P', 'ERAI P']
vars2 = ['TRMM', 'ERAI D850', 'ERAI D200']
nplot = len(vars1)
for pp in np.arange(0, nplot, 1):
# read data from file
var1 = vars1[pp]
var2 = vars2[pp]
print("Filename ",filenames[pp])
fin = xr.open_dataset(filenames[pp])
STC = fin['STC'][:, :, :]
wnum = fin['wnum']
freq = fin['freq']
ifreq = np.where((freq[:] >= 0) & (freq[:] <= flim))
iwave = np.where(abs(wnum[:]) <= nWavePlt)
STC[:, freq[:] == 0, :] = 0.
STC = STC.sel(wnum=slice(-nWavePlt, nWavePlt))
STC = STC.sel(freq=slice(0, flim))
coh2 = np.squeeze(STC[4, :, :])
phs1 = np.squeeze(STC[6, :, :])
phs2 = np.squeeze(STC[7, :, :])
phs1.where(coh2 <= contourmin, drop=True)
phs2.where(coh2 <= contourmin, drop=True)
pow1 = np.squeeze(STC[0, :, :])
pow2 = np.squeeze(STC[1, :, :])
pow1.where(pow1 <= 0, drop=True)
pow2.where(pow2 <= 0, drop=True)
if pp == 0:
Coh2 = np.empty([nplot, len(freq[ifreq]), len(wnum[iwave])])
Phs1 = np.empty([nplot, len(freq[ifreq]), len(wnum[iwave])])
Phs2 = np.empty([nplot, len(freq[ifreq]), len(wnum[iwave])])
Pow1 = np.empty([nplot, len(freq[ifreq]), len(wnum[iwave])])
Pow2 = np.empty([nplot, len(freq[ifreq]), len(wnum[iwave])])
k = wnum[iwave]
w = freq[ifreq]
Coh2[pp, :, :] = coh2
Phs1[pp, :, :] = phs1
Phs2[pp, :, :] = phs2
Pow1[pp, :, :] = np.log10(pow1)
Pow2[pp, :, :] = np.log10(pow2)
phstmp = Phs1
phstmp = np.square(Phs1) + np.square(Phs2)
phstmp = np.where(phstmp == 0, np.nan, phstmp)
scl_one = np.sqrt(1 / phstmp)
Phs1 = scl_one * Phs1
Phs2 = scl_one * Phs2
# create output directory if it does not exist
if not os.path.exists(plotpath):
print(f"Creating output directory: {plotpath}")
os.makedirs(plotpath)
# plot coherence
stp.plot_coherence(Coh2, Phs1, Phs2, symmetry, source, vars1, vars2, plotpath, flim, 20, contourmin, contourmax,
contourspace, nplot, N)
# check if output file exists since plotting function
# doesn't return an error code on failure
expected_file = os.path.join(plotpath,
'SpaceTimeCoherence_.png')
if not os.path.exists(expected_file):
print(f"ERROR: Could not create output file: {expected_file}")
sys.exit(1)
Running METplus
This use case can be run two ways:
1) Passing in UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf, then a user-specific system configuration file:
run_metplus.py \
-c /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf \
-c /path/to/user_system.conf
Modifying the configurations in parm/metplus_config, then passing in UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf:
run_metplus.py \ -c /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.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
OUTPUT_BASE - Path where METplus output will be written. This must be in a location where you have write permissions
MET_INSTALL_DIR - Path to location where MET is installed locally
and for the [exe] section, you will need to define the location of NON-MET executables. If the executable is in the user’s path, METplus will find it from the name. If the executable is not in the path, specify the full path to the executable here (i.e. RM = /bin/rm) The following executables are required for performing series analysis use cases:
Example User Configuration File:
[dir]
INPUT_BASE = /path/to/sample/input/data
OUTPUT_BASE = /path/to/output/dir
MET_INSTALL_DIR = /path/to/met-X.Y
[exe]
RM = /path/to/rm
CUT = /path/to/cut
TR = /path/to/tr
NCAP2 = /path/to/ncap2
CONVERT = /path/to/convert
NCDUMP = /path/to/ncdump
Expected Output
A successful run will output the following both to the screen and to the logfile:
INFO: METplus has successfully finished running.
Keywords
Note
sphinx_gallery_thumbnail_path = ‘_static/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.png’
Total running time of the script: ( 0 minutes 0.000 seconds)