Note
Go to the end to download the full example code
PlotDataPlane: Python Embedding of tripolar coordinate file
model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar.conf
Scientific Objective
By producing a postscript image from a file that utilizes a tripolar coordinate system, this use case shows METplus can utilize python embedding to ingest and utilize file structures on the same coordinate system.
Datasets
External Dependencies
You will need to use a version of Python 3.6+ that has the following packages installed:
xesmf
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 PlotDataPlane wrapper to generate a command to run the MET tool PlotDataPlane with Python Embedding if all required files are found.
METplus Workflow
PlotDataPlane is the only tool called in this example. It processes the following run time:
As it is currently set, the configuration file will pass in the path to the observation data, as well as a path to the weights for the coordinate system. This is done in an effort to speed up running the use case. These weight files are not required to run at the time of executing the use case, but will be made via Python Embedding if they are not found/passed in at run time. Additional user configurations, including the lat/lon spacing, can be found in the python script.
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/PlotDataPlane_obsHYCOM_coordTripolar.conf
[config]
# Documentation for this use case can be found at
# https://metplus.readthedocs.io/en/latest/generated/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar.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 = PlotDataPlane
###
# 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 = 20200127
VALID_END = 20200127
VALID_INCREMENT = 1M
LEAD_SEQ = 0
PLOT_DATA_PLANE_CUSTOM_LOOP_LIST = north, south
###
# File I/O
# https://metplus.readthedocs.io/en/latest/Users_Guide/systemconfiguration.html#directory-and-filename-template-info
###
PLOT_DATA_PLANE_INPUT_TEMPLATE = PYTHON_NUMPY
PLOT_DATA_PLANE_OUTPUT_TEMPLATE = {OUTPUT_BASE}/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar/HYCOM_iceCoverage_{custom}.ps
###
# PlotDataPlane Settings
# https://metplus.readthedocs.io/en/latest/Users_Guide/wrappers.html#plotdataplane
###
LOG_PLOT_DATA_PLANE_VERBOSITY = 1
PLOT_DATA_PLANE_FIELD_NAME = {PARM_BASE}/use_cases/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar/read_tripolar_grid.py {INPUT_BASE}/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar/rtofs_glo_2ds_n048_daily_diag.nc ice_coverage {custom} {INPUT_BASE}/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar/weight_{custom}.nc
PLOT_DATA_PLANE_TITLE = Tripolar via Python
PLOT_DATA_PLANE_COLOR_TABLE =
PLOT_DATA_PLANE_RANGE_MIN_MAX =
MET Configuration
This tool does not use a MET configuration file.
Python Embedding
This use case uses one Python script to read input data, passed through two times
parm/use_cases/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar/read_tripolar_grid.py
import os
import sys
import pandas as pd
import xarray as xr
import xesmf as xe
###############################################################################
# This script reads in tripolar grid ice data from the rtofs model and
# passes it to MET tools through python embedding.
# Written by George McCabe, NCAR
# January 2021
# Python embedding structure adapted from read_PostProcessed_WRF.py from
# the DTC MET User's Page.
# Tripolar grid logic adapted from ice_cover.py
# from Todd Spindler, NOAA/NCEP/EMC.
# Based on a script written by Lindsay Blank, NCAR in April 2020
# Arguments:
# input filename - path to input NetCDF file to process
# field name - name of field to read (ice_coverage or ice thickness)
# hemisphere - hemisphere to process (north or south)
# Example call: read_tripolar_grid.py /path/to/file.nc ice_coverage north
###############################################################################
# degrees between lat/lon points in output grid
LATITUDE_SPACING = 0.25
LONGITUDE_SPACING = 0.25
# set DEBUG to True to get debugging output
DEBUG = False
# latitude boundaries where curved data begins
# we are only concerned with data outside of the boundary for this case
# so we crop data that is below (for north) or above (for south)
LAT_BOUND_NORTH = 30.98
LAT_BOUND_SOUTH = -39.23
# list of valid values to specify for hemisphere
HEMISPHERES = ['north', 'south']
def print_min_max(ds):
print(f"MIN LAT: {float(ds['lat'].min())} and "
f"MIN LON: {float(ds['lon'].min())}")
print(f"MAX LAT: {float(ds['lat'].max())} and "
f"MAX LON: {float(ds['lon'].max())}")
if len(sys.argv) < 4:
print("Must specify exactly one input file and variable name.")
sys.exit(1)
# Read the input file as the first argument
input_file = os.path.expandvars(sys.argv[1])
var = sys.argv[2]
hemisphere = sys.argv[3]
# read optional weight file if provided
if len(sys.argv) > 4:
weight_file = sys.argv[4]
else:
weight_file = f'weight_{hemisphere}.nc'
if hemisphere not in HEMISPHERES:
print(f"ERROR: Invalid hemisphere value ({hemisphere}) "
f"Valid options are {HEMISPHERES}")
sys.exit(1)
try:
# Print some output to verify that this script ran
print(f"Input File: {repr(input_file)}")
print(f"Variable: {repr(var)}")
print(f"Hemisphere: {repr(hemisphere)}")
# read input file
xr_dataset = xr.load_dataset(input_file,
decode_times=True)
except NameError:
print("Trouble reading data from input file")
sys.exit(1)
# get time information
dt = pd.to_datetime(str(xr_dataset.MT[0].values))
valid_time = dt.strftime('%Y%m%d_%H%M%S')
# rename Latitude and Longitude to format that xesmf expects
xr_dataset = xr_dataset.rename({'Longitude': 'lon', 'Latitude': 'lat'})
# drop singleton time dimension for this example
xr_dataset = xr_dataset.squeeze()
# print out input data for debugging
if DEBUG:
print("INPUT DATASET:")
print(xr_dataset)
print_min_max(xr_dataset)
print('\n\n')
# get field name values to read into attrs
standard_name = xr_dataset[var].standard_name
long_name = xr_dataset[var].long_name.strip()
# trim off row of data
xr_dataset = xr_dataset.isel(Y=slice(0,-1))
# remove data inside boundary latitude to get only curved data
if hemisphere == 'north':
xr_out_bounds = xr_dataset.where(xr_dataset.lat >= LAT_BOUND_NORTH,
drop=True)
lat_min = xr_out_bounds.lat.min()
lat_max = 90
else:
xr_out_bounds = xr_dataset.where(xr_dataset.lat <= LAT_BOUND_SOUTH,
drop=True)
lat_min = max(-79, xr_out_bounds.lat.min())
lat_max = xr_out_bounds.lat.max()
if DEBUG:
print("OUTSIDE BOUNDARY LAT")
print(xr_out_bounds)
print_min_max(xr_out_bounds)
print('\n\n')
# create output grid using lat/lon bounds of data outside boundary
out_grid = xe.util.grid_2d(0,
360,
LONGITUDE_SPACING,
lat_min,
lat_max,
LATITUDE_SPACING)
# create regridder using cropped data and output grid
# NOTE: this creates a temporary file in the current directory!
# consider supplying path to file in tmp directory using filename arg
# set reuse_weights=True to read temporary weight file if it exists
regridder = xe.Regridder(xr_out_bounds,
out_grid,
'bilinear',
ignore_degenerate=True,
reuse_weights=True,
filename=weight_file)
# regrid data
xr_out_regrid = regridder(xr_out_bounds)
met_data = xr_out_regrid[var]
# flip the data
met_data = met_data[::-1, ]
if DEBUG:
print("PRINT MET DATA")
print(met_data)
print("Data Shape: " + repr(met_data.shape))
print("Data Type: " + repr(met_data.dtype))
print("Max: " + repr(met_data.max))
print_min_max(met_data)
print('\n\n')
# Calculate attributes
lat_lower_left = float(met_data['lat'].min())
lon_lower_left = float(met_data['lon'].min())
n_lat = met_data['lat'].shape[0]
n_lon = met_data['lon'].shape[1]
delta_lat = (float(met_data['lat'].max()) - float(met_data['lat'].min()))/float(n_lat)
delta_lon = (float(met_data['lon'].max()) - float(met_data['lon'].min()))/float(n_lon)
# create the attributes dictionary to describe the data to pass to MET
met_data.attrs = {
'valid': valid_time,
'init': valid_time,
'lead': "00",
'accum': "00",
'name': var,
'standard_name': standard_name,
'long_name': long_name,
'level': "SURFACE",
'units': "UNKNOWN",
# Definition for LatLon grid
'grid': {
'type': "LatLon",
'name': "RTOFS Grid",
'lat_ll': lat_lower_left,
'lon_ll': lon_lower_left,
'delta_lat': delta_lat,
'delta_lon': delta_lon,
'Nlat': n_lat,
'Nlon': n_lon,
}
}
attrs = met_data.attrs
print("Attributes: " + repr(met_data.attrs))
Running METplus
This use case can be run two ways:
Passing in PlotDataPlane_obsHYCOM_coordTripolar.conf then a user-specific system configuration file:
run_metplus.py -c /path/to/METplus/parm/use_cases/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar.conf -c /path/to/user_system.conf
Modifying the configurations in parm/metplus_config, then passing in PlotDataPlane_obsHYCOM_coordTripolar.conf:
run_metplus.py -c /path/to/METplus/parm/use_cases/model_applications/marine_and_cryosphere/PlotDataPlane_obsHYCOM_coordTripolar.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 model_applications/PlotDataPlane_obsHYCOM_coordTripolar (relative to OUTPUT_BASE) and will contain the following files:
HYCOM_iceCoverage_north.ps
HYCOM_iceCoverage_south.ps
Keywords
Note
PlotDataPlaneToolUseCase
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-PlotDataPlane_obsHYCOM_coordTripolar.png’
Total running time of the script: (0 minutes 0.000 seconds)