Skip to content

Commit

Permalink
Feature 2136 cross spectra (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKalb committed Jun 12, 2023
1 parent 1bf1858 commit c4ba46a
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/parm/use_case_groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
{
"category": "s2s",
"index_list": "3",
"run": false
"run": true
},
{
"category": "s2s",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
"""
UserScript: Make a Cross Spectra plot
UserScript: Compue Cross Spectra and make a plot
========================================================================
model_applications/
s2s/
UserScript_obsPrecip_obsOnly_CrossSpectraPlot.py
UserScript_fcstS2S_obsERAI_CrossSpectra.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
# 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
# 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_plot.py.
# 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 does not loop but plots the entire time period of data
#
# UserScript
#
# 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.
#

##############################################################################
Expand All @@ -49,10 +51,10 @@
#
# 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
# with the -c option, i.e. -c parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra.conf
#
# .. highlight:: bash
# .. literalinclude:: ../../../../parm/use_cases/model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf
# .. literalinclude:: ../../../../parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra.conf
#

#############################################################################
Expand All @@ -76,7 +78,7 @@
# 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
# .. literalinclude:: ../../../../parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra/cross_spectra_plot.py
#

##############################################################################
Expand All @@ -85,14 +87,14 @@
#
# This use case can be run two ways:
#
# 1) Passing in UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf,
# 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_obsPrecip_obsOnly_CrossSpectraPlot.conf -c /path/to/user_system.conf
# run_metplus.py -c /path/to/METplus/parm/use_cases/model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra.conf -c /path/to/user_system.conf
#
# 2) Modifying the configurations in parm/metplus_config, then passing in UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf::
# 2) 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_obsPrecip_obsOnly_CrossSpectraPlot.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:
#
Expand Down Expand Up @@ -143,4 +145,4 @@
#
#
#
# sphinx_gallery_thumbnail_path = '_static/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.png'
# sphinx_gallery_thumbnail_path = '_static/s2s-UserScript_fcstS2S_obsERAI_CrossSpectra.png'
2 changes: 1 addition & 1 deletion internal/tests/use_cases/all_use_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Category: s2s
0::GridStat_SeriesAnalysis_fcstNMME_obsCPC_seasonal_forecast:: model_applications/s2s/GridStat_SeriesAnalysis_fcstNMME_obsCPC_seasonal_forecast.conf:: netcdf4_env
1::TCGen_fcstGFSO_obsBDECKS_GDF_TDF:: model_applications/s2s/TCGen_fcstGFSO_obsBDECKS_GDF_TDF.conf:: metplotpy_env,cartopy,metplus
2::UserScript_obsPrecip_obsOnly_Hovmoeller:: model_applications/s2s/UserScript_obsPrecip_obsOnly_Hovmoeller.conf:: metplotpy_env,cartopy
3:: UserScript_obsPrecip_obsOnly_CrossSpectraPlot:: model_applications/s2s/UserScript_obsPrecip_obsOnly_CrossSpectraPlot.conf:: spacetime_env
3:: UserScript_fcstS2S_obsERAI_CrossSpectra:: model_applications/s2s/UserScript_fcstS2S_obsERAI_CrossSpectra.conf:: spacetime_env
4:: UserScript_obsERA_obsOnly_Stratosphere:: model_applications/s2s/UserScript_obsERA_obsOnly_Stratosphere.conf:: metplotpy_env,metdataio
5::SeriesAnalysis_fcstCFSv2_obsGHCNCAMS_climoStandardized_MultiStatisticTool:: model_applications/s2s/SeriesAnalysis_fcstCFSv2_obsGHCNCAMS_climoStandardized_MultiStatisticTool.conf:: netcdf4_env
6::GridStat_fcstCFSv2_obsGHCNCAMS_MultiTercile:: model_applications/s2s/GridStat_fcstCFSv2_obsGHCNCAMS_MultiTercile.conf:: netcdf4_env
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[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"
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# 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")
plot_config_file = os.getenv("PLOT_SPECTRA_YAML_CONFIG_NAME","spectra_plot.yaml")

config_dict = readconfig.parse_config(plot_config_file)

Expand All @@ -27,6 +27,9 @@
#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
Expand All @@ -36,17 +39,18 @@
contourspace = 0.1 # contour spacing
N = [1, 2] # wave modes for plotting
source = ""
spd = 2
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(",")
#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']
#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
Expand All @@ -58,8 +62,8 @@
wnum = fin['wnum']
freq = fin['freq']

ifreq = np.where((freq[:] >= 0) & (freq[:] <= flim))
iwave = np.where(abs(wnum[:]) <= nWavePlt)
#ifreq = np.where((freq[:] >= 0) & (freq[:] <= flim))
#iwave = np.where(abs(wnum[:]) <= nWavePlt)

STC[:, freq[:] == 0, :] = 0.
STC = STC.sel(wnum=slice(-nWavePlt, nWavePlt))
Expand All @@ -75,11 +79,13 @@
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])])
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]

Expand All @@ -103,7 +109,7 @@

# plot coherence
stp.plot_coherence(Coh2, Phs1, Phs2, symmetry, source, vars1, vars2, plotpath, flim, 20, contourmin, contourmax,
contourspace, nplot, N)
contourspace, npanel, N)

# check if output file exists since plotting function
# doesn't return an error code on failure
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pathout:
- !ENV '${COMP_SPECTRA_SCRIPT_OUTPUT_DIR}'

model: 'ufs_p7'
spd : 4
nperseg : 15 # in days
segOverLap : -5 # in days
Symmetry : 'symm'
latMin : -15.
latMax : 15.
datestrt : '2014-04-01T06:00:00' # start date, format: yyyy-mm-ddTHH:MM:ss
datelast : '2014-05-04T18:00:00' # end date, format: yyyy-mm-ddTHH:MM:ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plotpath:
- !ENV '${PLOT_SPECTRA_OUTPUT_DIR}'
model: 'ufs_p7'
var2: 'u850'
var3: 'u200'

This file was deleted.

This file was deleted.

0 comments on commit c4ba46a

Please sign in to comment.