# tutorial.jythonScripting

Purpose: Lead the audience through the use of Jython scripts and data sources.

Audience: Scientists and students looking for an introduction to scripting.

# 1. Introduction

Autoplot includes Jython scripting as a way to both read in strange data formats and to make scripts that control the application. For example, suppose you have found the data you want, but the sign needs to be flipped. You could easily load the data, negate the data, and plot the data in a script:

ds= getDataSet( 'vap+cdaweb:ds=DE2_PLASMA500MS_LANG&id=satPotential&timerange=1983-02-16' )
ds= -ds
plot( ds )
dom.plots[0].title='-1 * Spacecraft Potential'


Also scripts can serve as a new data source. In this case, instead of loading a URI like vap+cdaweb:ds=DE2_PLASMA500MS_LANG&id=satPotential&timerange=1983-02-16, you would point to the script itself. The following script does essentially the same thing, but defines a new data source:

ds= getDataSet( 'vap+cdaweb:ds=DE2_PLASMA500MS_LANG&id=satPotential&timerange=1983-02-16' )
ds= -ds
ds= putProperty( ds, QDataSet.TITLE, '-1 * Spacecraft Potential' )
result= ds


This script could then be saved to the name "/tmp/flipsign.jyds" and the script could be run by typing this name into the URI address bar.

# 2. First play with scripts

Let's make a control that loads in our favorite dataset and applies a filter to the timeseries.

Enable the script tab and the console tab. The script tab is a convenient Jython editor, and the console is a useful place to print debugging information.

First here's a script that applies the Butterworth filter to a time series:

magz= getDataSet( 'http://emfisis.physics.uiowa.edu/Flight/RBSP-A/Quick-Look/2013/01/16/rbsp-a_magnetometer_uvw_emfisis-Quick-Look_20130116_v1.2.1.cdf?Mag&slice1=2', monitor )
from org.das2.math.filter import Butterworth
myfilter= Butterworth( magz, 2, datum('10 Hz'), False )
result= myfilter.filter()
plot( result )


The first line goes and gets the data, providing feedback via the monitor. This is a progress monitor that is made available to the script, which will communicate the activity of slow processes to users who want to know that something is happening.

You can apply an FFT to the data as well:

plot( 1, fftPower( result,128, monitor ) )


What's the "1" argument to the plot function? It's a way to allow plotting to multiple plots. These are basically identifiers for "slots" you can put the data in to, where 0 is the top, and it will add plots below until a slot is made. (It's a bit more complex than this, but this is a sufficient description for now. What is really happening is that slots are added for data, and then plots are added to point at the given slot. You can also load a vap with the slots identified already, or use the "setLayout(n,m)" to get a more useful layout.

Now let's add a control to the script. This is done with a call to the "getParam" function:

cutoff= getParam( 'cutoff', '10 Hz', 'The cutoff frequency' )


This adds a convenient way to pass information into the script. Hitting the execute button uses the default setting '10 Hz' and the user can hold shift while pressing the execute button and a parameters GUI is presented.

# 3. Make this into a menu button

Any script can be added to the Autoplot menu bar, under the tools tab. Copy the name of the .jy script from the editor and paste it into the address bar. This brings up the "Run Script" dialog, which shows the script before it is run. (Scripts can be malicious, for example deleting local files, so the user ought to inspect them before they are run if the source is not trusted.) Note there is also a checkbox "Install in tools folder" that will put an item in the menu. Now whenever the script is run from the menu bar, the parameters GUI is shown.