METplus Practical Session Guide (Version 5.0) | Session 9: Python Embedding > Python Embedding for Gridded Data

A Simple Gridded Data Example with MET Tools

To demonstrate how to use Python Embedding for gridded data, you will use some test data included with the MET installation, the MET plot_data_plane tool, and the sample Python Embedding script named my_gridded_pyembed.py that you created earlier in this session. There are four required elements to the command for using Python Embedding with plot_data_plane:

  1. The path to the plot_data_plane MET executable
  2. The Python Embedding keyword
  3. The plot_data_plane output_filename argument
  4. The plot_data_plane field_string argument, modified for Python Embedding

Let's build the command!

Element 1: Use your tutorial environment variable MET_BUILD_BASE to access plot_data_plane:

${MET_BUILD_BASE}/bin/plot_data_plane

Element 2: For this exercise, we are using plot_data_plane which uses gridded data as input, so the keyword will be PYTHON_NUMPY. The data are not contained in an Xarray object, so we will not use PYTHON_XARRAY.

PYTHON_NUMPY

Element 3: Choose your own output filename. The plot_data_plane tool outputs an image in PostScript format so typically the filename will end with ".ps":

my_gridded_pyembed_plot.ps

Element 4: The field_string argument for plot_data_plane. The field_string argument contains the name of the field being plotted, and optionally the level. For Python Embedding, the name of the field will be replaced with your Python Embedding script including any arguments that the Python Embedding script uses. The Python Embedding script being used here takes two arguments; the path to the input data file and any string you wish to represent the name of the data in the input file. No level information is used for Python Embedding:

'name="my_gridded_pyembed.py ${MET_BUILD_BASE}/data/python/fcst.txt FCST_DATA";'

Let's run the command!

Verify you are in the Python Embedding practice directory:

cd ${METPLUS_TUTORIAL_DIR}/python_embed

Copy each of the four elements from above to construct the full Python Embedding command for plot_data_plane:

${MET_BUILD_BASE}/bin/plot_data_plane PYTHON_NUMPY my_gridded_pyembed_plot.ps 'name="my_gridded_pyembed.py ${METPLUS_DATA}/met_test/data/python/fcst.txt FCST_DATA";'

View the output image

The output file is a PostScript graphic file that typically can only be viewed with certain software. If you do not have a display tool that can view PostScript files, you can use the ImageMagick convert command to convert to a PNG file type which may be easier to view:

convert -rotate 90 -background white my_gridded_pyembed_plot.ps my_gridded_pyembed_plot.png

A Simple Gridded Data Example with METplus Wrappers

Now instead of using plot_data_plane directly, you will practice the above example using the METplus Wrappers. For each of the four elements shown above, the equivalent configuration items for METplus Wrappers will be described. But first, you will need to set up a basic METplus Wrappers configuration file:

Verify you are in the Python Embedding practice directory:

cd ${METPLUS_TUTORIAL_DIR}/python_embed

Open a new file named my_gridded_pyembed.conf with a text editor:

vi my_gridded_pyembed.conf

Copy the following METplus configuration items into the file and save it when complete. Some basic elements are required to get the METplus wrappers to run. For this example, you need to provide some time looping information despite not actually looping over those times. You also need to provide a top-level output directory (OUTPUT_BASE):

[config]
LOOP_BY=VALID
VALID_BEG=20230101
VALID_END=20230101
VALID_TIME_FMT=%Y%m%d
OUTPUT_BASE={ENV[METPLUS_TUTORIAL_DIR]}/python_embed/wrappers_gridded_output

For Element 1 above, the METplus Wrappers will find plot_data_plane through the MET_INSTALL_DIR and PROCESS_LIST configuration items. We also need the Wrappers to find the data through the METPLUS_DATA:

MET_INSTALL_DIR={ENV[MET_BUILD_BASE]}
PROCESS_LIST=PlotDataPlane
METPLUS_DATA={ENV[METPLUS_DATA]}

For Element 2 above, we will use the PLOT_DATA_PLANE_INPUT_TEMPLATE configuration item:

PLOT_DATA_PLANE_INPUT_TEMPLATE=PYTHON_NUMPY

For Element 3 above, we will use the PLOT_DATA_PLANE_OUTPUT_TEMPLATE configuration item, which includes the OUTPUT_BASE configuration item:

PLOT_DATA_PLANE_OUTPUT_TEMPLATE={OUTPUT_BASE}/my_gridded_pyembed_wrappers_plot.ps

For Element 4 above, we will use the PLOT_DATA_PLANE_FIELD_NAME configuration item:

PLOT_DATA_PLANE_FIELD_NAME={ENV[METPLUS_TUTORIAL_DIR]}/python_embed/my_gridded_pyembed.py {METPLUS_DATA}/met_test/data/python/fcst.txt FCST_DATA

Now save the file, and run METplus wrappers:

run_metplus.py -c my_gridded_pyembed.conf

You can verify that the output image my_gridded_pyembed_wrappers_plot.ps (which is found in your METplus OUTPUT_BASE) directory, exactly matches the image above when using plot_data_plane directly.

Congratulations! You've run Python Embedding for gridded data using MET tools directly and with METplus Wrappers. Continue to the next section to practice Python Embedding for point data.