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:
- Open the NetCDF data file
- Convert the units of the gridded data from degrees Kelvin to degrees Celsius
- Construct the required data attributes
- Prepare the data for plot_data_plane
Verify you are in the Python Embedding practice directory:
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:
Obtain the command line argument controlling whether we will use a NUMPY or XARRAY object for Python Embedding:
Next, add the path to the input file:
Open the file with Xarray:
Construct a Python dictionary named attrs that contains the required attributes needed for Python Embedding:
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['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)
Add the grid_info to the attrs dictionary:
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:
met_data = satellite_temperature['channel_14_brightness_temperature']-273.15
met_data.attrs = attrs
met_data = satellite_temperature['channel_14_brightness_temperature'].values-273.15
print("FATAL! MUST PROVIDE EITHER xarray OR numpy AS AN ARGUMENT TO practice_gridded_pyembed.py")