Skip to content

Commit

Permalink
Feature 627 coherence spectra (#891)
Browse files Browse the repository at this point in the history
Co-authored-by: Hank Fisher <[email protected]>
Co-authored-by: George McCabe <[email protected]>
Co-authored-by: MET Tools Test Account <[email protected]>
Co-authored-by: George McCabe <[email protected]>
  • Loading branch information
5 people authored May 3, 2021
1 parent 9a29e56 commit 3e49e2d
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ jobs:
- "s2s:0"
- "s2s:1-3"
- "s2s:4"
- "s2s:6:NEW"
- "space_weather:0-1"
- "tc_and_extra_tc:0-2"
- "tc_and_extra_tc:3"
Expand Down
1 change: 1 addition & 0 deletions ci/jobs/python_requirements/get_metplotpy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pip3 install matplotlib
pip3 install scipy
pip3 install cmocean
pip3 install plotly==4.9.0
pip3 install kaleido

basedir=$(dirname "$0")
Expand Down
8 changes: 7 additions & 1 deletion ci/jobs/python_requirements/get_miniconda.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

script_dir=$(dirname "$0")

python_version=`${script_dir}/../print_python_version.py`
if [ -z "${1+x}" ]; then
python_version=`${script_dir}/../print_python_version.py`
else
python_version=$1
fi

echo Creating Conda environment using $python_version

# these are used to obtain version of MiniConda3
# the version determines the default version of Python
Expand Down
25 changes: 25 additions & 0 deletions ci/jobs/python_requirements/get_spacetime.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /bin/bash

script_dir=$(dirname "$0")

work_dir=$script_dir/../../..

echo Installing environment for UserScript_obsPrecip_obsOnly_CrossSpectraPlot with conda

# create a MiniConda environemtn using Python 3.8
${script_dir}/get_miniconda.sh 3.8

conda install -y pip

# run manage externals to obtain METcalcpy
${work_dir}/manage_externals/checkout_externals -e ${work_dir}/ci/parm/Externals_metplotpy.cfg
python3 -m pip install ${work_dir}/../METplotpy

# run manage externals to obtain METcalcpy
${work_dir}/manage_externals/checkout_externals -e ${work_dir}/ci/parm/Externals_metcalcpy.cfg

python3 -m pip install ${work_dir}/../METcalcpy

# install required packages for use case
conda install -y -c conda-forge python-dateutil netCDF4 xarray scipy matplotlib pyngl
conda install -c anaconda pyyaml
1 change: 1 addition & 0 deletions docs/Users_Guide/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7060,6 +7060,7 @@ METplus Configuration Glossary
Specify the value for 'climo_stdev.hour_interval' in the MET configuration file for SeriesAnalysis.

| *Used by:* SeriesAnalysis
PB2NC_PB_REPORT_TYPE
Specify the value for 'pb_report_type' in the MET configuration file for PB2NC.

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
"""
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
#
# .. highlight:: bash
# .. literalinclude:: ../../../../parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf
#

#############################################################################
# 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
#
# .. highlight:: python
# .. literalinclude:: ../../../../parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot/cross_spectra_plot.py
#

##############################################################################
# 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
#
# 2) 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::
# `UserScriptUseCase <https://dtcenter.github.io/METplus/develop/search.html?q=UserScriptUseCase&check_keywords=yes&area=default>`_,
# `S2SAppUseCase <https://dtcenter.github.io/METplus/search.html?q=S2SAppUseCase&check_keywords=yes&area=default>`_,
#
# sphinx_gallery_thumbnail_path = '_static/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.png'
1 change: 1 addition & 0 deletions internal_tests/use_cases/all_use_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Category: s2s
3::UserScript_obsERA_obsOnly_WeatherRegime:: model_applications/s2s/UserScript_obsERA_obsOnly_WeatherRegime.conf::cartopy,METplotpy,METcalcpy,eofs
4::TCGen_fcstGFSO_obsBDECKS_GDF_TDF:: model_applications/s2s/TCGen_fcstGFSO_obsBDECKS_GDF_TDF.conf:: cartopy,METplotpy
5::UserScript_obsPrecip_obsOnly_Hovmoeller:: model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf:: requests,psutil,pyyaml,plotly,METplotpy,METcalcpy
6:: UserScript_obsPrecip_obsOnly_CrossSpectraPlot:: model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf:: spacetime


Category: space_weather
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[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/
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plotpath:
- !ENV '${OUTPUT_DIR}'

0 comments on commit 3e49e2d

Please sign in to comment.