PointStat: Using Python Embedding

met_tool_wrapper/PointStat/PointStat_python_embedding.conf

Scientific Objective

Read forecast data using a Python embedding script. Compare with point observation data.

Datasets

Forecast: NRL binary data (v-wind component)
Observation: prepBUFR data that has been converted to NetCDF format via PB2NC
Location: All of the input data required for this use case can be found in the met_test sample data tarball. Click here to 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 ‘Running METplus’ section for more information.
Data Source: Unknown

METplus Components

This use case utilizes the METplus PointStat wrapper to search for files that are valid at a given run time and generate a command to run the MET tool point_stat if all required files are found. The forecast data is read into PointStat using a Python embedding script

METplus Workflow

PointStat is the only tool called in this example. It processes the following run times:

Valid: 2020-09-06 6Z

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/met_tool_wrapper/PointStat/PointStat_python_embedding.conf

[config]

# Documentation for this use case can be found at
# https://metplus.readthedocs.io/en/latest/generated/met_tool_wrapper/PointStat/PointStat_python_embedding.html

# For additional information, please see the METplus Users Guide.
# https://metplus.readthedocs.io/en/latest/Users_Guide

###
# Processes to run
# https://metplus.readthedocs.io/en/latest/Users_Guide/systemconfiguration.html#process-list
###

PROCESS_LIST = PointStat


###
# Time Info
# LOOP_BY 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
# LEAD_SEQ is the list of forecast leads to process
# https://metplus.readthedocs.io/en/latest/Users_Guide/systemconfiguration.html#timing-control
###

LOOP_BY = VALID
VALID_TIME_FMT = %Y%m%d%H
VALID_BEG = 2020090606
VALID_END = 2020090606
VALID_INCREMENT = 1M

LEAD_SEQ = 0H


###
# File I/O
# https://metplus.readthedocs.io/en/latest/Users_Guide/systemconfiguration.html#directory-and-filename-template-info
###

FCST_POINT_STAT_INPUT_DIR = {INPUT_BASE}/met_test/new/point_stat_input/vvwind
FCST_POINT_STAT_INPUT_TEMPLATE = PYTHON_NUMPY

OBS_POINT_STAT_INPUT_DIR = {INPUT_BASE}/met_test/new/point_stat_input/prepbufr
OBS_POINT_STAT_INPUT_TEMPLATE = gdas.{valid?fmt=%Y%m%d}.t{valid?fmt=%H}z.nc

POINT_STAT_CLIMO_MEAN_INPUT_DIR =
POINT_STAT_CLIMO_MEAN_INPUT_TEMPLATE =

POINT_STAT_CLIMO_STDEV_INPUT_DIR =
POINT_STAT_CLIMO_STDEV_INPUT_TEMPLATE =

POINT_STAT_OUTPUT_DIR = {OUTPUT_BASE}/point_stat_py_embed


###
# Field Info
# https://metplus.readthedocs.io/en/latest/Users_Guide/systemconfiguration.html#field-info
###

POINT_STAT_ONCE_PER_FIELD = False

SCRIPT_DIR = {PARM_BASE}/use_cases/met_tool_wrapper/PointStat/PointStat_python_embedding
FCST_VAR1_NAME = {SCRIPT_DIR}/read_NRL_binary.py {FCST_POINT_STAT_INPUT_DIR}/vvwind_zht_0010.0_0000.0_glob360x181_{init?fmt=%Y%m%d%H}_{lead?fmt=%4H}0000_fcstfld

OBS_VAR1_NAME = VGRD
OBS_VAR1_LEVELS = Z0


###
# PointStat Settings
# https://metplus.readthedocs.io/en/latest/Users_Guide/wrappers.html#pointstat
###

#LOG_POINT_STAT_VERBOSITY = 2

POINT_STAT_CONFIG_FILE ={PARM_BASE}/met_config/PointStatConfig_wrapped

POINT_STAT_CLIMO_MEAN_TIME_INTERP_METHOD = NEAREST

POINT_STAT_INTERP_TYPE_METHOD = BILIN
POINT_STAT_INTERP_TYPE_WIDTH = 2

POINT_STAT_OUTPUT_FLAG_SL1L2 = STAT
POINT_STAT_OUTPUT_FLAG_VL1L2 = STAT

OBS_POINT_STAT_WINDOW_BEGIN = -5400
OBS_POINT_STAT_WINDOW_END = 5400

POINT_STAT_OFFSETS = 0

#MODEL =

#POINT_STAT_DESC = NA

#POINT_STAT_REGRID_TO_GRID =
#POINT_STAT_REGRID_METHOD =
#POINT_STAT_REGRID_WIDTH =

POINT_STAT_OUTPUT_PREFIX =

#POINT_STAT_OBS_VALID_BEG = {valid?fmt=%Y%m%d_%H}
#POINT_STAT_OBS_VALID_END = {valid?fmt=%Y%m%d_%H}

POINT_STAT_GRID = FULL

POINT_STAT_POLY =
POINT_STAT_STATION_ID =

POINT_STAT_MESSAGE_TYPE = ADPUPA

MET Configuration

METplus sets environment variables based on user settings in the METplus configuration file. See How METplus controls MET config file settings 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: Overriding Unsupported MET config file settings

Note

See the PointStat MET Configuration section of the User’s Guide for more information on the environment variables used in the file below:

////////////////////////////////////////////////////////////////////////////////
//
// Point-Stat configuration file.
//
// For additional information, see the MET_BASE/config/README file.
//
////////////////////////////////////////////////////////////////////////////////

//
// Output model name to be written
//
// model =
${METPLUS_MODEL}

//
// Output description to be written
// May be set separately in each "obs.field" entry
//
// desc =
${METPLUS_DESC}

////////////////////////////////////////////////////////////////////////////////

//
// Verification grid
//
// regrid = {
${METPLUS_REGRID_DICT}

////////////////////////////////////////////////////////////////////////////////

//
// May be set separately in each "field" entry
//
censor_thresh = [];
censor_val    = [];
cat_thresh    = [ NA ];
cnt_thresh    = [ NA ];
cnt_logic     = UNION;
wind_thresh   = [ NA ];
wind_logic    = UNION;
eclv_points   = 0.05;
//hss_ec_value =
${METPLUS_HSS_EC_VALUE}
rank_corr_flag = FALSE;

//
// Forecast and observation fields to be verified
//
fcst = {
  ${METPLUS_FCST_FILE_TYPE}
  ${METPLUS_FCST_FIELD}
}

obs = {
  ${METPLUS_OBS_FILE_TYPE}
  ${METPLUS_OBS_FIELD}
}
////////////////////////////////////////////////////////////////////////////////

//
// Point observation filtering options
// May be set separately in each "obs.field" entry
//
// message_type =
${METPLUS_MESSAGE_TYPE}
sid_exc        = [];

//obs_quality_inc =
${METPLUS_OBS_QUALITY_INC}

//obs_quality_exc =
${METPLUS_OBS_QUALITY_EXC}

duplicate_flag = NONE;
obs_summary    = NONE;
obs_perc_value = 50;

//
// Mapping of message type group name to comma-separated list of values.
//
//message_type_group_map =
${METPLUS_MESSAGE_TYPE_GROUP_MAP}

////////////////////////////////////////////////////////////////////////////////

//
// Climatology data
//
//climo_mean = {
${METPLUS_CLIMO_MEAN_DICT}


//climo_stdev = {
${METPLUS_CLIMO_STDEV_DICT}

//
// May be set separately in each "obs.field" entry
//
//climo_cdf = {
${METPLUS_CLIMO_CDF_DICT}

////////////////////////////////////////////////////////////////////////////////

//
// Point observation time window
//
// obs_window = {
${METPLUS_OBS_WINDOW_DICT}

////////////////////////////////////////////////////////////////////////////////

//
// Verification masking regions
//
//mask = {
${METPLUS_MASK_DICT}

////////////////////////////////////////////////////////////////////////////////

//
// Confidence interval settings
//
ci_alpha  = [ 0.05 ];

boot = {
   interval = PCTILE;
   rep_prop = 1.0;
   n_rep    = 0;
   rng      = "mt19937";
   seed     = "";
}

////////////////////////////////////////////////////////////////////////////////

//
// Interpolation methods
//
//interp = {
${METPLUS_INTERP_DICT}

////////////////////////////////////////////////////////////////////////////////

//
// HiRA verification method
//
//hira = {
${METPLUS_HIRA_DICT}

////////////////////////////////////////////////////////////////////////////////

//
// Statistical output types
//
//output_flag = {
${METPLUS_OUTPUT_FLAG_DICT}

////////////////////////////////////////////////////////////////////////////////
// Threshold for SEEPS p1 (Probability of being dry)

//seeps_p1_thresh =
${METPLUS_SEEPS_P1_THRESH}

////////////////////////////////////////////////////////////////////////////////

tmp_dir = "${MET_TMP_DIR}";

// output_prefix =
${METPLUS_OUTPUT_PREFIX}
//version        = "V10.0.0";

////////////////////////////////////////////////////////////////////////////////

${METPLUS_MET_CONFIG_OVERRIDES}

Python Embedding

This use case calls a Python script to read the binary input data. /parm/use_cases/met_tool_wrapper/PointStat/PointStat_python_embedding/read_NRL_binary.py

import os
import sys
import re
import numpy as np
import datetime as dt

# var_info values are tuples (units, long_name)
# Taken from synoptic_files.f, with some units SI standardized
# e.g. mb->hPa
# Some of the long_names are unknown to me, hopefully
# someone more knowledgeable will fill these in
var_info = {
      'airtmp': ('C','Air Temperature'),
      'geopht': ('gpm','Geopotential Height'),
      'uuwind': ('m/s','Zonal Wind'),
      'vvwind': ('m/s','Meridional Wind'),
      'wndspd': ('m/s','Wind Speed'),
      'vpress': ('hPa','Vapor Pressure'),
      'prch2o': ('kg/m**2','Unknown'),
      'slpres': ('hPa','Sea Level Pressure'),
      'grdwet': ('percent','Ground Wetness'),
      'prtend': ('cPa/s','Unknown'),
      'grdtmp': ('K','Ground Temperature'),
      'terrht': ('m','Terrain Height'),
      'totcls': ('percent','Unknown'),
      'lowcld': ('percent','Low Cloud'),
      'midcld': ('percent','Mid Cloud'),
      'hghcld': ('percent','High Cloud'),
      'cupflx': ('kg/m**2/s','Unknown'),
      'conpcp': ('cm','Unknown'),
      'sblpcp': ('cm','Unknown'),
      'trpres': ('hPa','Terrain Pressure'),
      'snowdp': ('cm','Snow Depth'),
      'icecon': ('percent','Ice Concentration'),
      'conpcp': ('kg/m**2','Unknown'),
      'trdval': ('dval_m','Unkown'),
      'solflx': ('W/m**2','Solar Flux'),
      'cupcap': ('J/m**2','Unknown'),
      'irrflx': ('W/m**2','Unknown'),
      'slhflx': ('W/m**2','Unknown'),
      'sehflx': ('W/m**2','Unknown'),
      'totpcp': ('cm','Unknown'),
      'bouflx': ('W/m**2','Unknown'),
      'totflx': ('W/m**2','Total Flux'),
      'irflux': ('W/m**2','Infrared Flux'),
      'liftcl': ('m','Lifting Condensation Level'),
      'ht_sfc': ('m/s','Surface Height'),
      'uustrs': ('N/m**2','Zonal Wind Stress'),
      'vvstrs': ('N/m**2','Meridional Wind Stress'),
      'wngust': ('m/s','Wind Gust'),
      'dwptdp': ('C','Dewpoint Depression'),
      'diverg': ('1/s','Divergence'),
      'absvor': ('1/s','Vorticity'),
      'womega': ('cPa/s','Vertical Velocity'),
      'stmfun': ('m**2/s','Stream Function'),
      'velpot': ('m**2/s','Velocity Potential'),
      'stacld': ('percent','Stable Cloud'),
      'concld': ('percent','Convective Cloud'),
      'clouds': ('percent','Total Cloud'),
    }

###########################################

   ##
   ##  input file specified on the command line
   ##  load the data into the numpy array
   ##

if len(sys.argv) == 2:

    # Store the input file and record number
    input_file = os.path.expandvars(sys.argv[1])
    tokens = os.path.basename(input_file).replace('-', '_').split('_');
    varname = tokens[0]
    nlons = int(tokens[4][4:7])  # Usually 360
    nlats = int(tokens[4][8:])   # Usually 181
    try:
        # Print some output to verify that this script ran
        print("Input File: " + repr(input_file))

        # Read input file
        data = np.float64(np.fromfile(input_file, '>f'))

        # Read and re-orient the data
        met_data = data[::-1].reshape(nlats, nlons)[:,::-1].copy()

        print("Data Shape: " + repr(met_data.shape))
        print("Data Type:  " + repr(met_data.dtype))
        print("Data Range: " + repr(min(data)) + " to " + repr(max(data)) +
              " " + var_info[varname][0])
    except NameError:
        print("Trouble reading input file: " + input_file)
else:
    print("Must specify exactly one input file.")
    sys.exit(1)

###########################################

   ##
   ##  create the metadata dictionary
   ##

for token in tokens:
   if(re.search("[0-9]{10,10}", token)):
       ymdh = dt.datetime.strptime(token[0:10],"%Y%m%d%H")
   elif(re.search("[0-9]{8,8}", token)):
       fhr = int(token) / 10000

init  = ymdh
valid = init + dt.timedelta(hours=fhr)
lead, rem = divmod((valid-init).total_seconds(), 3600)

attrs = {
   'valid': valid.strftime("%Y%m%d_%H%M%S"),
   'init':  init.strftime("%Y%m%d_%H%M%S"),
   'lead':  str(int(lead)),
   'accum': '00',

   'name':      varname,
   'long_name': var_info[varname][1],
   'level':     tokens[1]+'_'+tokens[2],
   'units':     var_info[varname][0],

   'grid': {
       'name': 'Global 1 Degree',
       'type' : 'LatLon',
       'lat_ll' :    -90.0,
       'lon_ll' :      0.0,
       'delta_lat' :   1.0,
       'delta_lon' :   1.0,
       'Nlat' :      nlats,
       'Nlon' :      nlons,
   }
}

print("Attributes: " + repr(attrs))

Running METplus

This use case can be run two ways:

  1. Passing in PointStat_python_embedding.conf then a user-specific system configuration file:

    run_metplus.py -c /path/to/METplus/parm/use_cases/met_tool_wrapper/PointStat/PointStat_python_embedding.conf -c /path/to/user_system.conf
    
  2. Modifying the configurations in parm/metplus_config, then passing in PointStat_python_embedding.conf:

    run_metplus.py -c /path/to/METplus/parm/use_cases/met_tool_wrapper/PointStat/PointStat_python_embedding.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

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

NOTE: All of these items must be found under the [dir] section.

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 point_stat_py_embed (relative to OUTPUT_BASE) and will contain the following files:

  • point_stat_000000L_20200906_060000V.stat

Keywords

Note

  • PointStatToolUseCase

  • PythonEmbeddingFileUseCase

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

sphinx_gallery_thumbnail_path = ‘_static/met_tool_wrapper-PointStat.png’

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

Gallery generated by Sphinx-Gallery