METplus Practical Session Guide (Version 5.0) | Session 9: Python Embedding > Writing a Python Script for Python Embedding

Writing a Python Script for Python Embedding

In this section, you will use Python Embedding to open a NetCDF file containing weather satellite brightness temperature data, and use plot_data_plane to create an image of this field. The reason you will use Python Embedding is because the brightness temperature data have units of degrees Kelvin, but you need to create an image with data plotted in degrees Celsius, so the Python Embedding script will perform the following functions:

  1. Open the NetCDF data file
  2. Convert the units of the gridded data from degrees Kelvin to degrees Celsius
  3. Construct the required data attributes
  4. Prepare the data for plot_data_plane

Verify you are in the Python Embedding practice directory:

cd ${METPLUS_TUTORIAL_DIR}/python_embed

Open an empty text file with the name practice_gridded_pyembed.pywith a text editor of your choice:


Now copy and paste each of the following code snippets into the file, then save it:


First, we will add the necessary modules:

import xarray as xr
import os
import sys

Obtain the command line argument controlling whether we will use a NUMPY or XARRAY object for Python Embedding:

numpy_or_xarray = sys.argv[1]

Next, add the path to the input file:

input_file = os.path.join(os.environ['METPLUS_DATA'],'model_applications/short_range/brightness_temperature/2019_05_21_141/')

Open the file with Xarray:

satellite_temperature = xr.open_dataset(input_file)

Construct a Python dictionary named attrs that contains the required attributes needed for Python Embedding:

attrs = {}
attrs['valid'] = '20190521_010000'
attrs['init'] = '20190521_010000'
attrs['lead'] = '000000'                     
attrs['accum'] = '000000'
attrs['name'] = 'Temperature'
attrs['long_name'] = 'Brightness Temperature in Celsius'
attrs['level'] = 'Surface'
attrs['units'] = 'Celsius'

Construct a Python dictionary named grid_info that contains the required grid information needed for Python Embedding. In this example, some of these attributes are included in the sample file being read, so we will re-use those attributes where possible:

grid_info = {}
grid_info['type'] = satellite_temperature.attrs['Projection']
grid_info['hemisphere'] = satellite_temperature.attrs['hemisphere']
grid_info['name'] = 'GOES-16'
grid_info['scale_lat_1'] = float(satellite_temperature.attrs['scale_lat_1'])
grid_info['scale_lat_2'] = float(satellite_temperature.attrs['scale_lat_2'])
grid_info['lat_pin'] = float(satellite_temperature.attrs['lat_pin'])
grid_info['lon_pin'] = float(satellite_temperature.attrs['lon_pin'])
grid_info['x_pin'] = float(satellite_temperature.attrs['x_pin'])
grid_info['y_pin'] = float(satellite_temperature.attrs['y_pin'])
grid_info['lon_orient'] = float(satellite_temperature.attrs['lon_orient'])
grid_info['d_km'] = float(satellite_temperature.attrs['d_km'])
grid_info['r_km'] = float(satellite_temperature.attrs['r_km'])
grid_info['nx'] = float(1620)
grid_info['ny'] = float(1120)
NOTE: Constructing the grid attributes is a manual process, and requires understanding of what attributes are required by MET for the type of grid your data are on. Please refer to the MET user's guide for more information about which attributes to include based upon the grid you are using since they vary from grid to grid.

Add the grid_info to the attrs dictionary:

attrs['grid'] = grid_info

Add an if/else block to change between PYTHON_NUMPY and PYTHON_XARRAY. If PYTHON_XARRAY is requested. then the Xarray DataArray is subset so a single variable is selected, and those data are converted to Celsius and renamed to met_data, and then the attrs are added to the Xarray DataArray object. If PYTHON_NUMPY is requested, then the Xarray DataArray is subset so a single variable is selected, and a NumPy N-dimensional array representation is requested from Xarray using .values on the DataArray, and it is renamed to met_data. Note that for PYTHON_NUMPY, no attrs are attached to met_data in any manner, it is simply enough for attrs to exist as a variable in your Python script:

if numpy_or_xarray=='xarray':
  met_data = satellite_temperature['channel_14_brightness_temperature']-273.15
  met_data.attrs = attrs
elif numpy_or_xarray=='numpy':
  met_data = satellite_temperature['channel_14_brightness_temperature'].values-273.15

Congratulations, You Just Wrote a Python Embedding Script!

Save the file before exiting, and then advance to either the next section to practice calling your script directly with MET tools, or the final section to practice calling your script with METplus Wrappers, or practice both!