Applying your own post processing

This demo shows you how to use the post formatoption to apply your own post processing scripts.

It requires the '' netCDF file, netCDF4 and the psy-maps plugin.

In [1]:
import psyplot.project as psy
%matplotlib inline
%config InlineBackend.close_figures = False


The post formatoption let's you apply your own script to modify your data. Let's start with a simple plot of the wind speed:

In [2]:
sp = psy.plot.mapvector('', name=[['u', 'v']], plot='stream', 
                        color='red', title='%b %Y')

psyplot.project.Project([    arr0: 3-dim DataArray of u, v, with (variable, lat, lon)=(2, 96, 192), lev=100000.0, time=1979-01-31T18:00:00])

It is hard to see the continents below this amount of arrows. So we might want to enhance our plot with cartopy's stock_img.

But since there is now formatoption for it, we can now either define a new plotter and add the formatoption, or we use the post formatoption.

In [3]:
          post='self.stock_img =')

The first parameter enable_post=True sets the enable_post attribute of the plotter to True. This attribute is by default set to False because it is always a security vulnerability to use the built-in exec function which is used by the post formatoption. We could, however, already have included this in our first definition of the project via

sp = psy.plot.mapvector('', name=[['u', 'v']], plot='stream', color='red',

The second parameter post='' updates the post formatoption. It accepts an executable python script as a string. Note that we make use of the self variable, the only variable that is given to the script. It is the Formatoption instance that performs the update. Hence you can access all the necessary attributes and informations:

  • the axes through
  • the figure through
  • the data that is plotted through
  • the raw data from the dataset through self.raw_data
  • the plotter through self.plotter
  • any other formatoption in the plotter, e.g. the title through self.plotter.title

For example, let's add another feature that adds the mean of the plotted variables to the plot. For this, let's first have a look into the plot_data attribute of the plotter which includes the data that is visualized and that is accessible through the data attribute of the post formatoption:

In [4]:

<xarray.DataArray (variable: 2, lat: 96, lon: 192)>
array([[[-5.548854, -5.470729, ..., -5.680202, -5.618678],
        [-4.17483 , -4.246608, ..., -3.992702, -4.089869],
        [-2.664088, -2.547389, ..., -2.911159, -2.785182],
        [-3.48733 , -3.371119, ..., -3.708034, -3.599635]],

       [[ 2.322004,  2.529036, ...,  1.901594,  2.112532],
        [ 1.343489,  1.620344, ...,  0.820051,  1.075911],
        [-2.204363, -2.282   , ..., -2.04323 , -2.125261],
        [-1.864519, -1.976824, ..., -1.624773, -1.746843]]], dtype=float32)
    lev       float64 1e+05
  * lat       (lat) float64 88.57 86.72 84.86 83.0 81.13 79.27 77.41 75.54 ...
  * variable  (variable) <U1 'u' 'v'
  * lon       (lon) float64 0.0 1.875 3.75 5.625 7.5 9.375 11.25 13.12 15.0 ...
    time      datetime64[ns] 1979-01-31T18:00:00
    CDI:          Climate Data Interface version 1.6.8 (
    Conventions:  CF-1.4
    history:      Mon Aug 17 22:51:40 2015: cdo -r copy test-...
    title:        Test file
    CDO:          Climate Data Operators version 1.6.8rc2 (http://mpimet.mpg....
    long_name:    zonal wind-velocity
    units:        m/s
    code:         131
    table:        128
    grid_type:    gaussian

It is a 3-dimensional array, where the first dimension consists of the zonal wind speed 'u' and the meridional wind speed 'v'. So let's add their means as a text to the plot:

In [5]:
self.stock_img =
umean =[0].mean().values
vmean =[1].mean().values
abs_mean = (([0]**2 +[1]**2)**0.5).mean().values
self.text =
    0., -0.15, 
    'u: %1.4f m/s, v: %1.4f m/s, wind speed: %1.4f m/s' % (
        umean, vmean, abs_mean),""")


psyplot is intended to work interactively. By default, the post formatoption is only updated when you personally update it. However, you can modify this timing using the post_timing formatoption. It can be either

  • 'never': The default which requires a manual update
  • 'replot': To update it when the data changes
  • 'always': To always update it.

For example, in the current setting, when we change the data to the second time step via

In [6]:

psyplot.project.Project([    arr0: 3-dim DataArray of u, v, with (variable, lat, lon)=(2, 96, 192), lev=100000.0, time=1979-02-28T18:00:00])

Our means are not updated, for this, we have to

  1. set the post_timing to 'replot'
  2. slightly modify our post script to not plot two texts above each other

In [7]:
sp.update(post_timing='replot', post="""
self.stock_img =
umean =[0].mean().values
vmean =[1].mean().values
abs_mean = (([0]**2 +[1]**2)**0.5).mean().values
if hasattr(self, 'text'):
    text = self.text
    text =, -0.15, '', 
    'u: %1.4f m/s, v: %1.4f m/s, wind speed: %1.4f m/s' % (
        umean, vmean, abs_mean))""")

Now, if we update to the third timestep, our means are also calculated

In [8]:

Saving and loading

Saving a project is straight forward via the save_project method

In [9]:
d = sp.save_project()

However, when loading the project, the enable_post attribute is (for security reasons) again set to False. So if you are sure you can trust the post processing scripts in the post formatoption, load your project with enable_post=True

In [10]:
sp = psy.Project.load_project(d, enable_post=True)