UserScript: Compue Cross Spectra and make a plot

model_applications/ s2s/ UserScript_fcstS2S_obsERAI_CrossSpectra.py

Scientific Objective

This use case calls the METcalcpy cross spectra function and then the METplotpy space time plot to compute cross-spectra and create a sample cross spectra diagram using sample data.

The space time plot and cross spectra calculations were created by Maria Gehne at the Physical Sciences Labratory in NOAA.

Datasets

  • Forecast dataset: UFS Prototype 7

  • Observation dataset: ERAI

METplus Components

This use case runs the UserScript wrapper tool to run a user provided script, in this case, cross_spectra.py and cross_spectra_plot.py.

METplus Workflow

This use case computes spectra and plots for the entire time period of data. The use case loops over two processes, computing and plotting the cross-spectra. The user is able to edit the process list to turn off the computation part if only plotting is desired, or vice versa.

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_fcstS2S_obsERAI_CrossSpectra.conf

[config]

PROCESS_LIST = UserScript(comp_spectra), UserScript(plot_spectra)

# Note: time looping is not used in this use case
LOOP_BY = REALTIME
VALID_TIME_FMT = %Y
VALID_BEG = 2014

USER_SCRIPT_RUNTIME_FREQ = RUN_ONCE

[user_env_vars]
# Make output base avabilable to the script
COMP_SPECTRA_SCRIPT_OUTPUT_DIR = {OUTPUT_BASE}/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/output

# YAML configuration file for the cross spectra calculation
COMP_SPECTRA_YAML_CONFIG_NAME = {METPLUS_BASE}/parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/spectra_comp.yaml

# Input files for the cross spectra calculation
COMP_SPECTRA_INPUT_FILE_NAMES = {INPUT_BASE}/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/data/precip.erai.sfc.1p0.2x.2014-2016.nc,{INPUT_BASE}/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/data/prate_avg_ufs_p7_2014040100.nc,{INPUT_BASE}/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/data/u850_ufs_p7_2014040100.nc,{INPUT_BASE}/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/data/u200_ufs_p7_2014040100.nc

PLOT_SPECTRA_INPUT_FILE_NAMES = {OUTPUT_BASE}/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/output/SpaceTimeSpectra_ufs_p7_P_D850_symm_4spd.nc,{OUTPUT_BASE}/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/output/SpaceTimeSpectra_ufs_p7_P_D200_symm_4spd.nc,{OUTPUT_BASE}/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/output/SpaceTimeSpectra_ufs_p7_P_D200_symm_4spd.nc

PLOT_SPECTRA_YAML_CONFIG_NAME = {METPLUS_BASE}/parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/spectra_plot.yaml

PLOT_SPECTRA_OUTPUT_DIR = {OUTPUT_BASE}/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/plots/


[comp_spectra]
# Settings for computing the cross-spectra
USER_SCRIPT_COMMAND = python {METCALCPY_BASE}/metcalcpy/contributed/spacetime/cross_spectra.py

LOG_FILE = "cross_spectra.log"
LOG_LEVEL = "DEBUG"

METCALCPY_BASE = {METPLUS_BASE}/../METcalcpy


[plot_spectra]
# settings for plotting the cross-spectra

USER_SCRIPT_COMMAND = python {METPLUS_BASE}/parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/cross_spectra_plot.py

LOG_FILE = "cross_spectra_plot.log"
LOG_LEVEL = "INFO"

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 sys
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("PLOT_SPECTRA_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)
model = config_dict['model']
var2 = config_dict['var2']
var3 = config_dict['var3']

# 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 = 4

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(",")
#vars1 = ['ERAI P', 'ERAI P', 'ERAI P']
#vars2 = ['TRMM', 'ERAI D850', 'ERAI D200']
filenames = os.environ.get("PLOT_SPECTRA_INPUT_FILE_NAMES","ERAI_P_D850_symn,ERAI_P_D200_symn,ERAI_P_D200_symn").split(",")
vars1 = [model+' P',model+' P',model+' P']
vars2 = [model+' '+var2,model+' '+var3,model+' '+var3]
nplot = len(vars1)

npanel =3
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:
        ifreq = np.where((freq[:] >= 0) & (freq[:] <= flim))
        iwave = np.where(abs(wnum[:]) <= nWavePlt)
        Coh2 = np.full([npanel, len(freq[ifreq]), len(wnum[iwave])], np.nan)
        Phs1 = np.full([npanel, len(freq[ifreq]), len(wnum[iwave])], np.nan)
        Phs2 = np.full([npanel, len(freq[ifreq]), len(wnum[iwave])], np.nan)
        Pow1 = np.full([npanel, len(freq[ifreq]), len(wnum[iwave])], np.nan)
        Pow2 = np.full([npanel, len(freq[ifreq]), len(wnum[iwave])], np.nan)
        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, npanel, 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_fcstS2S_obsERAI_CrossSpectra.conf, then a user-specific system configuration file:

run_metplus.py -c /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra.conf -c /path/to/user_system.conf
  1. Modifying the configurations in parm/metplus_config, then passing in UserScript_fcstS2S_obsERAI_CrossSpectra.conf:

    run_metplus.py -c /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra.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

  • UserScriptUseCase

  • S2SAppUseCase

  • METcalcpyUseCase

  • METplotpyUseCase

Navigate to the METplus Quick Search for Use Cases page to discover other similar use cases.

sphinx_gallery_thumbnail_path = ‘_static/s2s-UserScript_fcstS2S_obsERAI_CrossSpectra.png’

Total running time of the script: (0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery