"""
Multi_Tool: Feature Relative by Lead using Multiple User-Defined Fields 
=======================================================================

model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics.conf

"""
##############################################################################
# .. contents::
#   :depth: 1
#   :local:
#   :backlinks: none

##############################################################################
# Scientific Objective
# --------------------
# This use case calls multiple tools to produce diagnostic plots of systematic errors relative to a
# feature (e.g. hurricane, MCS, etc...). This use case calls two user provided python scripts that
# calculate diagnostics of interest (e.g. integrated vapor transport, potential vorticity, etc...).
# These user diagnostics are then used to define the systematic errors. This example calculates
# statistics over varying forecast leads with the ability to define lead groupings.
# This use case is very similar to the Multi_Tools: Feature Relative by Lead use case and the
# Multi_Tools: Feature Relative by Lead using User-Defined Fields.
# (ADeck,GFS:BDeck,GFS:ATCF,Grib2)
#
# By maintaining focus of each evaluation time (or evaluation time series, in this case)
# on a user-defined area around a cyclone, the model statistical errors associated
# with cyclonic physical features (moisture flux, stability, strength of upper-level
# PV anomaly and jet, etc.) can be related directly to the model forecasts and provide
# improvement guidance by accurately depicting interactions with significant weather
# features around and within the cyclone. This is in contrast to the traditional
# method of regional averaging cyclone observations in a fixed grid, which
# "smooths out" system features and limits the meaningful metrics that can be gathered.
# Specifically, this use case creates bins of forecast lead times as specified by the
# given ranges which provides additional insight directly into forecast lead time accuracy.
#
# Additionally, the ability to calculate model statistical errors based on user provided diagnostics
# allows the user to customize the feature relative analysis to suit their needs.
#
# This use case compares the Global Forecast System (GFS) forecast to the GFS analysis for
# hurricane Dorian. It is based on three user provided python scripts that calculate the diagnostic 
# integrated vaport transport (IVT) baroclinic potential vorticity (PV), and saturation equivalent potential temperature (SEPT), respectively. 

##############################################################################
# Version Added
# -------------
#
# METplus version 3.1

##############################################################################
# Datasets
# --------
#
# **Forecast:** NOAA Global Forecast System (GFS) with the required variables
# at all levels >= 100 hPa:
#
# #. Temperature
# #. v- component of wind
# #. u- component of wind
# #. Geoopotential height
# #. Specific Humidity OR Relative Humidity 
#
# **Observation:** Automated Tropical Cyclone Forecasting System (ATCF) ADeck and BDeck data
# and NOAA Global Forecast System (GFS) with the reaquired variables at all levels >= 100 hPa:
#
# #. Temperature
# #. v- component of wind
# #. u- component of wind
# #. Geoopotential height
# #. Specific Humidity OR Relative Humidity 
#
# **Climatology:** None
#
# **Location:** All of the input data required for this use case can be 
# found in a sample data tarball. Each use case category will have 
# one or more sample data tarballs. It is only necessary to download 
# the tarball with the use case’s dataset and not the entire collection 
# of sample data. Click here to access the METplus releases page and download sample data 
# for the appropriate release: https://github.com/dtcenter/METplus/releases
# This tarball should be unpacked into the directory that you will 
# set the value of INPUT_BASE. See :ref:`running-metplus` section for more information.

##############################################################################
# External Dependencies
# ---------------------
#
# You will need to use a version of Python 3.7+ that has the following packages installed:
#
# * netCDF4
# * pygrib
# * cfgrib
# * metpy
# * xarray
#
# If the version of Python used to compile MET did not have these libraries at the time of compilation, you will need to add these packages or create a new Python environment with these packages.
#
# If this is the case, you will need to set the MET_PYTHON_EXE environment variable to the path of the version of Python you want to use. If you want this version of Python to only apply to this use case, set it in the [user_env_vars] section of a METplus configuration file.::
#
#    [user_env_vars]
#    MET_PYTHON_EXE = /path/to/python/with/required/packages/bin/python

##############################################################################
# METplus Components
# ------------------
#
# This use case first runs PyEmbedIngest to run the user provided python scripts to calculate the
# desired diagnostics (in this example, IVT, PV and SEPT). PyEmbedIngest runs the RegridDataPlane tool 
# to write IVT, PV, and SEPTto a MET readable netCDF file. Then TCPairs and ExtractTiles are run to 
# generate matched tropical cyclone data and regrid them into appropriately-sized tiles
# along a storm track. The MET tc-stat tool is used to filter the track data and the MET 
# regrid-dataplane tool is used to regrid the data (GRIB1 or GRIB2 into netCDF). 
# Next, a series analysis by lead time is performed on the results and plots (.ps and .png) are 
# generated for all variable-level-stat combinations from the specified variables, levels, 
# and requested statistics. If lead grouping is turned on, the final results are aggregated into 
# forecast hour groupings as specified by the start, end and increment in the METplus configuration 
# file, as well as labels to identify each forecast hour grouping. If lead grouping is not turned out
# the final results will be written out for each requested lead time.

##############################################################################
# METplus Workflow
# ----------------
#
# **Beginning time (INIT_BEG):** 2019083000
#
# **End time (INIT_END):** 2019083023
#
# **Increment between beginning and end times (INIT_INCREMENT):** 21600
#
# **Sequence of forecast leads to process (LEAD_SEQ):** 90, 96, 102, 108, 114
#
# This use case loops by process which means that each tool is run for all times before moving to the
# next tool. The tool order is as follows:
# 
# PyEmbedIngest, TCPairs, ExtractTiles, SeriesByLead
#
# This example loops by forecast/lead time (with begin, end, and increment as specified in the METplus
# TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_Multiple_Diagnostics.conf file). 
#
# 4 initialization times will be run over 5 lead times:
#
# | **Init:** 20190830_00Z
# | **Forecast lead:** 90, 96, 102, 108, 114
# |
# | **Init:** 20190830_06Z
# | **Forecast lead:** 90, 96, 102, 108, 114
# |
# | **Init:** 20190830_12Z
# | **Forecast lead:** 90, 96, 102, 108, 114
# |
# | **Init:** 20190830_18Z
# | **Forecast lead:** 90, 96, 102, 108, 114

##############################################################################
# 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,
# i.e. parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_Multiple_Diagnostics.conf
#
# .. highlight:: bash
# .. literalinclude:: ../../../../parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics.conf
#

#############################################################################
# MET Configuration
# -----------------
#
# METplus sets environment variables based on user settings in the METplus configuration file. 
# See :ref:`How METplus controls MET config file settings<metplus-control-met>` for more details. 
#
# **YOU SHOULD NOT SET ANY OF THESE ENVIRONMENT VARIABLES YOURSELF! THEY WILL BE OVERWRITTEN BY METPLUS WHEN IT CALLS THE MET TOOLS!**
#
# If there is a setting in the MET configuration file that is currently not supported by METplus you'd like to control, please refer to:
# :ref:`Overriding Unsupported MET config file settings<met-config-overrides>`
#
# .. dropdown:: TCPairsConfig_wrapped
#
#   .. literalinclude:: ../../../../parm/met_config/TCPairsConfig_wrapped
#
# .. dropdown:: TCStatConfig_wrapped
#
#   .. literalinclude:: ../../../../parm/met_config/TCStatConfig_wrapped
#
# .. dropdown:: SeriesAnalysisConfig_wrapped
#
#   .. literalinclude:: ../../../../parm/met_config/SeriesAnalysisConfig_wrapped

##############################################################################
# Python Embedding
# ----------------
#
# This use case uses four Python embedding scripts to read input data, two for the forecast data and two for the analysis data.
# The multiple datatype input requires the two-script approach.
#
# .. dropdown:: parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_ivt_fcst.py
#
#   .. highlight:: python
#   .. literalinclude:: ../../../../parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_ivt_fcst.py
#
# .. dropdown:: parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_pv_fcst.py
#
#   .. highlight:: python
#   .. literalinclude:: ../../../../parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_pv_fcst.py
#
# .. dropdown:: parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_sept_fcst.py
#
#   .. highlight:: python
#   .. literalinclude:: ../../../../parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_sept_fcst.py
#
# .. dropdown:: parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_ivt_analysis.py
#
#   .. highlight:: python
#   .. literalinclude:: ../../../../parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_ivt_analysis.py
#
# .. dropdown:: parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_pv_analysis.py
#
#   .. highlight:: python
#   .. literalinclude:: ../../../../parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_pv_analysis.py
#
# .. dropdown:: parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_sept_analysis.py
#
#   .. highlight:: python
#   .. literalinclude:: ../../../../parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics/gfs_sept_analysis.py
#
# 
# For more information on the basic requirements to utilize Python Embedding in METplus, 
# please refer to the MET User’s Guide section on `Python embedding <https://met.readthedocs.io/en/latest/Users_Guide/appendixF.html#appendix-f-python-embedding>`_.

##############################################################################
# User Scripting
# --------------
#
# User Scripting is not used in this use case.

##############################################################################
# Running METplus
# ---------------
#
# Pass the use case configuration file to the run_metplus.py script along 
# with any user-specific system configuration files if desired::
#
#   run_metplus.py /path/to/METplus/parm/use_cases/model_applications/medium_range/TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multiple_Diagnostics.conf /path/to/user_system.conf
#
# See :ref:`running-metplus` for more information.

##############################################################################
# Expected Output
# ---------------
#
# A successful run will output the following both to the screen and to the logfile::
#
#   INFO: METplus has successfully finished running.
#
# Refer to the value set for **OUTPUT_BASE** to find where the output data was generated.
# Output for this use case will be found in subdirectories of the 'series_analysis_lead' directory (relative to **OUTPUT_BASE**):
# 
# * series_animate
# * series_F090
# * series_F096
# * series_F102
# * series_F108
# * series_F114
#
# | The series_animate directory contains the animations of the series analysis in .gif format for all variable, level, and statistics combinations:
#
#    series_animate_<varname>_<level>_<stat>.gif
#
# | The series_FHHH directories contains files that have the following format:
# 
#   ANLY_FILES_FHHH
#
#   FCST_ASCII_FILES_FHHH
#
#   series_FHHH_<varname>_<level>_<stat>.png
#
#   series_FHHH_<varname>_<level>_<stat>.ps
#
#   series_FHHH_<varname>_<level>_<stat>.nc
#
#   Where:
#
#    **HHH** is the forecast hour/lead time in hours
#
#    **varname** is the variable of interest, as specified in the METplus series_by_lead_all_fhrs config file
#
#    **level**  is the level of interest, as specified in the METplus series_by_lead_all_fhrs config file
#
#    **stat** is the statistic of interest, as specified in the METplus series_by_lead_all_fhrs config file.
#

##############################################################################
# Keywords
# --------
#
# .. note::
#
#   * TCPairsToolUseCase
#   * SeriesByLeadUseCase
#   * TCStatToolUseCase
#   * RegridDataPlaneToolUseCase
#   * PyEmbedIngestToolUseCase
#   * MediumRangeAppUseCase
#   * SeriesAnalysisUseCase
#   * GRIB2FileUseCase
#   * FeatureRelativeUseCase
#   * SBUOrgUseCase
#   * DiagnosticsUseCase
#   * RuntimeFreqUseCase
#   * TropicalCycloneUseCase
#
#   Navigate to the :ref:`quick-search` page to discover other similar use cases.
#
#
#
# sphinx_gallery_thumbnail_path = '_static/medium_range-TCStat_SeriesAnalysis_fcstGFS_obsGFS_FeatureRelative_SeriesByLead_PyEmbed_Multivariate_Diagnostics.png'
