GridStat: Python Embedding to read and process ice cover

model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover.conf

Scientific Objective

This use case utilizes Python embedding to extract several statistics from the ice cover data over both pole regions, which was already being done in a closed system. By producing the same output via METplus, this use case provides standardization and reproducible results.

Datasets

Forecast: RTOFS ice cover file via Python Embedding script/file
Observation: OSTIA ice cover file via Python Embedding script/file
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: JPL’s PODAAC and NCEP’s FTPPRD data servers

External Dependencies

You will need to use a version of Python 3.6+ that has the following packages installed:

  • scikit-learn

  • pyproj

  • pyresample

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 utilizes the METplus GridStat wrapper to generate a command to run the MET tool GridStat with Python Embedding for the specified user hemispheres

METplus Workflow

GridStat is the only tool called in this example. This use case will pass in the two hemispheres via a custom loop list, with both the observation and forecast gridded data being pulled from the files via Python Embedding. All of the desired statistics reside in the CNT line type, so that is the only output requested. It processes the following run time:

Valid: 2021-03-05 0Z

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/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover.conf

[config]

# Documentation for this use case can be found at
# https://metplus.readthedocs.io/en/latest/generated/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover.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 = GridStat


###
# 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
VALID_BEG=20210305
VALID_END=20210305
VALID_INCREMENT = 1M

LEAD_SEQ = 0

GRID_STAT_CUSTOM_LOOP_LIST = north, south


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

FCST_GRID_STAT_INPUT_TEMPLATE = PYTHON_NUMPY

OBS_GRID_STAT_INPUT_TEMPLATE = PYTHON_NUMPY

GRID_STAT_OUTPUT_DIR = {OUTPUT_BASE}
GRID_STAT_OUTPUT_TEMPLATE = {valid?fmt=%Y%m%d}


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

GRID_STAT_ONCE_PER_FIELD = False

MODEL = RTOFS

OBTYPE = UKMO

CONFIG_DIR = {PARM_BASE}/use_cases/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover

FCST_VAR1_NAME = {CONFIG_DIR}/read_ice_data.py {INPUT_BASE}/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover/{valid?fmt=%Y%m%d}_rtofs_glo_2ds_n024_ice.nc {INPUT_BASE}/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover/{valid?fmt=%Y%m%d}12_UKMO_L4.nc {custom} fcst

OBS_VAR1_NAME = {CONFIG_DIR}/read_ice_data.py {INPUT_BASE}/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover/{valid?fmt=%Y%m%d}_rtofs_glo_2ds_n024_ice.nc {INPUT_BASE}/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover/{valid?fmt=%Y%m%d}12_UKMO_L4.nc {custom} obs


###
# GridStat Settings
# https://metplus.readthedocs.io/en/latest/Users_Guide/wrappers.html#gridstat
###

GRID_STAT_REGRID_TO_GRID = NONE

GRID_STAT_GRID_WEIGHT_FLAG = AREA

GRID_STAT_DESC = NA

GRID_STAT_NEIGHBORHOOD_WIDTH = 1
GRID_STAT_NEIGHBORHOOD_SHAPE = SQUARE
GRID_STAT_NEIGHBORHOOD_COV_THRESH = >=0.5

GRID_STAT_OUTPUT_PREFIX = {custom}

GRID_STAT_OUTPUT_FLAG_CNT = BOTH

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 GridStat MET Configuration section of the User’s Guide for more information on the environment variables used in the file below:

////////////////////////////////////////////////////////////////////////////////
//
// Grid-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}

//
// Output observation type to be written
//
// obtype =
${METPLUS_OBTYPE}

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

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

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

//censor_thresh =
${METPLUS_CENSOR_THRESH}
//censor_val =
${METPLUS_CENSOR_VAL}
cat_thresh  	 = [];
cnt_thresh  	 = [ NA ];
cnt_logic   	 = UNION;
wind_thresh 	 = [ NA ];
wind_logic  	 = UNION;
eclv_points      = 0.05;
//nc_pairs_var_name =
${METPLUS_NC_PAIRS_VAR_NAME}
nc_pairs_var_suffix = "";
//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}
}

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

//
// Climatology mean 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}

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

//
// 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     = "";
}

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

//
// Data smoothing methods
//
//interp = {
${METPLUS_INTERP_DICT}

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

//
// Neighborhood methods
//
nbrhd = {
   field      = BOTH;
   // shape =
   ${METPLUS_NBRHD_SHAPE}
   // width =
   ${METPLUS_NBRHD_WIDTH}
   // cov_thresh =
   ${METPLUS_NBRHD_COV_THRESH}
   vld_thresh = 1.0;
}

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

//
// Fourier decomposition
// May be set separately in each "obs.field" entry
//
//fourier = {
${METPLUS_FOURIER_DICT}

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

//
// Gradient statistics
// May be set separately in each "obs.field" entry
//
gradient = {
   dx = [ 1 ];
   dy = [ 1 ];
}

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

//
// Distance Map statistics
// May be set separately in each "obs.field" entry
//
//distance_map = {
${METPLUS_DISTANCE_MAP_DICT}

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

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

//
// NetCDF matched pairs output file
// May be set separately in each "obs.field" entry
//
// nc_pairs_flag = {
${METPLUS_NC_PAIRS_FLAG_DICT}

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

//seeps_p1_thresh =
${METPLUS_SEEPS_P1_THRESH}

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

//grid_weight_flag =
${METPLUS_GRID_WEIGHT_FLAG}

tmp_dir = "${MET_TMP_DIR}";

// output_prefix =
${METPLUS_OUTPUT_PREFIX}

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

${METPLUS_MET_CONFIG_OVERRIDES}

Python Embedding

This use case uses one Python script to read forecast and observation data

parm/use_cases/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover/read_ice_data.py

#!/bin/env python
"""
Code adapted from
Todd Spindler
NOAA/NWS/NCEP/EMC

Designed to read in RTOFS and OSTIA data
and based on user input, process Arctic or Antarctic regions
for ice cover, and pass back in memory the forecast or observation
data field
"""

import numpy as np
from sklearn.metrics import mean_squared_error
import xarray as xr
import pandas as pd
from pyproj import Geod
import pyresample as pyr
from datetime import datetime, date
import os, sys

#-------------------------------------
def iceArea(lon1,lat1,ice1):
    """
    Compute the cell side dimensions (Vincenty) and the cell surface areas.
    This assumes the ice has already been masked and subsampled as needed    
    returns ice_extent, ice_area, surface_area = iceArea(lon,lat,ice)
    surface_area is the computed grid areas in km**2)
    """
    lon=lon1.copy()
    lat=lat1.copy()
    ice=ice1.copy()
    g=Geod(ellps='WGS84')
    _,_,xdist=g.inv(lon,lat,np.roll(lon,-1,axis=1),np.roll(lat,-1,axis=1))
    _,_,ydist=g.inv(lon,lat,np.roll(lon,-1,axis=0),np.roll(lat,-1,axis=0))
    xdist=np.ma.array(xdist,mask=ice.mask)/1000.
    ydist=np.ma.array(ydist,mask=ice.mask)/1000.
    xdist=xdist[:-1,:-1]
    ydist=ydist[:-1,:-1]
    ice=ice[:-1,:-1]     # just to match the roll
    extent=xdist*ydist   # extent is surface area only
    area=xdist*ydist*ice # ice area is actual ice cover (area * concentration)
    return extent.flatten().sum(), area.flatten().sum(), extent

#--------------------------------------------------------

try:
    rtofsfile, icefile, hemisphere, file_flag = sys.argv[1:]
except ValueError:
    print("Must specify the following elements: fcst_file obs_file hemisphere file_flag")
    sys.exit(1)

HEMISPHERES = ['north', 'south']
FILE_FLAGS = ['fcst', 'obs']

if hemisphere not in HEMISPHERES or file_flag not in FILE_FLAGS:
    print(f"ERROR: Invalid hemisphere value ({hemisphere}) or file_flag value ({file_flag}) "
            f"Valid options are {HEMISPHERES} {FILE_FLAGS}")
    sys.exit(1)

print('processing',hemisphere+'ern hemisphere')
if hemisphere == 'north':
    bounding_lat=30.98
else:
    bounding_lat=-39.23
        

# load rtofs data and subset to hemisphere of interest and ice cover min value
print('reading rtofs ice')
if not os.path.exists(rtofsfile):
    print('missing rtofs file',rtofsfile)
    sys.exit(1)
rtofs=xr.open_dataset(rtofsfile,decode_times=True)
rtofs=rtofs.ice_coverage[0,:-1,]
            
# load OSTIA data
print('reading OSTIA ice')
if not os.path.exists(icefile):
    print('missing OSTIA ice file',icefile)
    sys.exit(1)
ncep=xr.open_dataset(icefile,decode_times=True)
ncep=ncep.rename({'lon':'Longitude','lat':'Latitude'})
ncep=ncep.sea_ice_fraction.squeeze()
    
# trim to polar regions
if hemisphere == 'north':
    rtofs=rtofs.where((rtofs.Latitude>=bounding_lat),drop=True) 
    ncep=ncep.where((ncep.Latitude>=bounding_lat),drop=True) 
else:
    rtofs=rtofs.where((rtofs.Latitude<=bounding_lat),drop=True) 
    ncep=ncep.where((ncep.Latitude<=bounding_lat),drop=True) 
    
# now it's back to masked arrays for the RTOFS data
rlon=rtofs.Longitude.values
rlat=rtofs.Latitude.values
rice=rtofs.to_masked_array()

nlon=ncep.Longitude.values%360. # shift from -180 - 180 to 0-360
nlat=ncep.Latitude.values
nlon,nlat=np.meshgrid(nlon,nlat)  # shift from 1-d to 2-d arrays
nice=ncep.to_masked_array()
    
# mask out values below 15%
rice.mask=np.ma.mask_or(rice.mask,rice<0.15)
nice.mask=np.ma.mask_or(nice.mask,nice<0.15)

# compute ice area on original grids
print('computing ice area')
ncep_extent,ncep_area,ncep_surface_area=iceArea(nlon,nlat,nice)
rtofs_extent,rtofs_area,rtofs_surface_area=iceArea(rlon,rlat,rice)
    
# interpolate rtofs to ncep grid
print('interpolating rtofs to OSTIA grid')            
    
# pyresample gausssian-weighted kd-tree interp
rlon1=pyr.utils.wrap_longitudes(rlon)
rlat1=rlat.copy()
nlon1=pyr.utils.wrap_longitudes(nlon)
nlat1=nlat.copy()
# define the grids
orig_def = pyr.geometry.GridDefinition(lons=rlon1,lats=rlat1)
targ_def = pyr.geometry.GridDefinition(lons=nlon1,lats=nlat1)
radius=50000
sigmas=25000    
rice2=pyr.kd_tree.resample_gauss(orig_def,rice,targ_def,
                                     radius_of_influence=radius,
                                     sigmas=sigmas,
                                     nprocs=8,
                                     neighbours=8,
                                     fill_value=None)
            
print('creating combined mask')
combined_mask=np.logical_and(nice.mask,rice2.mask)
nice2=nice.filled(fill_value=0.0)
rice2=rice2.filled(fill_value=0.0)
nice2=np.ma.array(nice2,mask=combined_mask)
rice2=np.ma.array(rice2,mask=combined_mask)

#Create the MET grids based on the file_flag
if file_flag == 'fcst':
    met_data = rice2[:-1,:-1]
    met_data = met_data[::-1,]
    #trim the lat/lon grids so they match the data fields
    #note that nice1 lat/lon fields are valid, since rice2 is interpolated to nice2
    lat_met = nlat1[:-1,:-1]
    lon_met = nlon1[:-1,:-1]
    print("Data shape: "+repr(met_data.shape))
    v_str = rtofsfile.split('_')[-6].split('/')[-1]
    v_str = v_str + '_120000'
    lat_ll = float(lat_met.min())
    lon_ll = float(lon_met.min())
    n_lat = lat_met.shape[0]
    n_lon = lon_met.shape[1]
    delta_lat = (float(lat_met.max()) - float(lat_met.min()))/float(n_lat)
    delta_lon = (float(lon_met.max()) - float(lon_met.min()))/float(n_lon)
    print(f"variables:"
            f"lat_ll: {lat_ll} lon_ll: {lon_ll} n_lat: {n_lat} n_lon: {n_lon} delta_lat: {delta_lat} delta_lon: {delta_lon}")
    met_data.attrs = {
            'valid': v_str,
            'init': v_str,
            'lead': "00",
            'accum': "00",
            'name': 'ice_coverage',
            'standard_name': rtofs.standard_name,
            'long_name': rtofs.long_name.strip(),
            'level': "SURFACE",
            'units': "UNKNOWN",

            'grid': {
                'type': "LatLon",
                'name': "RTOFS Grid",
                'lat_ll': lat_ll,
                'lon_ll': lon_ll,
                'delta_lat': delta_lat,
                'delta_lon': delta_lon,
                'Nlat': n_lat,
                'Nlon': n_lon,
                }
            }
    attrs = met_data.attrs
if file_flag == 'obs':
    met_data = nice2[:-1,:-1]
    met_data = met_data[::-1,]
    #modify the lat and lon grids since they need to match the data dimensions, and code cuts the last row/column of data
    lat_met = nlat1[:-1,:-1]
    lon_met = nlon1[:-1,:-1]
    print("Data shape: " +repr(met_data.shape))
    v_str = icefile.split('_')[-3].split('/')[-1]
    v_str = v_str[:-2]+'_120000'
    lat_ll = float(lat_met.min())
    lon_ll = float(lon_met.min())
    n_lat = lat_met.shape[0]
    n_lon = lon_met.shape[1]
    delta_lat = (float(lat_met.max()) - float(lat_met.min()))/float(n_lat)
    delta_lon = (float(lon_met.max()) - float(lon_met.min()))/float(n_lon)
    print(f"variables:"
            f"lat_ll: {lat_ll} lon_ll: {lon_ll} n_lat: {n_lat} n_lon: {n_lon} delta_lat: {delta_lat} delta_lon: {delta_lon}")
    met_data.attrs = {
            'valid': v_str,
            'init': v_str,
            'lead': "00",
            'accum': "00",
            'name': 'ice_coverage',
            'standard_name': ncep.standard_name,
            'long_name': ncep.long_name.strip(),
            'level': "SURFACE",
            'units': "UNKNOWN",

            'grid': {
                'type': "LatLon",
                'name': "RTOFS Grid",
                'lat_ll': lat_ll,
                'lon_ll': lon_ll,
                'delta_lat': delta_lat,
                'delta_lon': delta_lon,
                'Nlat': n_lat,
                'Nlon': n_lon,
                }
            }
    attrs = met_data.attrs

Running METplus

This use case can be run two ways:

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

    run_metplus.py -c /path/to/METplus/parm/use_cases/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover.conf -c /path/to/user_system.conf
    
  2. Modifying the configurations in parm/metplus_config, then passing in GridStat_fcstRTOFS_obsOSTIA_iceCover.conf:

    run_metplus.py -c /path/to/METplus/parm/use_cases/model_applications/marine_and_cryosphere/GridStat_fcstRTOFS_obsOSTIA_iceCover.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 thisIce use case will be found in 20210305 (relative to OUTPUT_BASE) and will contain the following files:

  • grid_stat_north_000000L_20210305_120000V_cnt.txt

  • grid_stat_south_000000L_20210305_120000V_cnt.txt

  • grid_stat_north_000000L_20210305_120000V.stat

  • grid_stat_south_000000L_20210305_120000V.stat

Keywords

Note

  • GridStatToolUseCase

  • PythonEmbeddingFileUseCase

  • MarineAndCryosphereAppUseCase

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

sphinx_gallery_thumbnail_path = ‘_static/marine_and_cryosphere-GridStat_fcstRTOFS_obsOSTIA_iceCover.png’

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

Gallery generated by Sphinx-Gallery