4.2.1 Jython Methods

You have seen how to compute wind speed from u and v wind components,
using the Formulas
facility. IDV formulas are actually code in the Jython language. If your
calculations require more than a one-line mathematical expression you
can create Jython methods to make calculations then you can call these
methods in a formula. Here is an example of computing wind speed with a
four-line method.
- Invoking the Jython Library and Editor
- Click on the main menu item
`Edit→Formulas→Jython Library`

to bring up the`Jython Libraries`

Editor - Defining your own Jython Method
- In the
`Jython libraries`

Editor, click on the tab`User's Library`

- Create a new Jython method called windSpeed(u,v).
Jython methods have a first line beginning with "def" and
ending with the colon ":". The code block composing the method must
be indented at least one space. Indentation must be the same for
all line in a code block, except continued comment lines which
are set off with triple quotes.
Your code block will be something like:
def windSpeed(u,v): """ compute wind speed from u and v wind components """ usqd= u*u vsqd= v*v ws = sqrt(usqd + vsqd) return ws

- Click on
`Save`

to check for correct Jython syntax and save the method; you will get an error message if the syntax is not correct. If it is complete, your method is saved. - Use the
`File→Close`

menu to exit the editor. - Use the Method in a Formula
- Leave up your previous display made with formulas, and the same data source.
- From the main menu, click on
`Edit→Formulas→Create Formula`

. The`Formula Editor`

window appears. - Enter the
`Name`

`jythonWS`

. - Enter the method for the formula definition in the
`Formula`

entry field, simply`windSpeed(u,v)`

. - Open the
`Advanced`

panel by clicking on the down arrow. - Enter the
`Description`

"jython wind speed". - For
`Group`

select`Workshop`

from the drop down list. - In the
`Displays`

section, click on`Use selected`

and then the`All off`

radio button. Then select`Contour Plan View`

and`Contour Vertical Cross Section`

in the list. - Click on
`Add Formula`

. - An item
`jython wind speed`

should appear in the`Field Selector`

window's`Fields`

panel, under the`Workshop`

category. - Use the formula to make a contour display of wind speed using the same u and v wind components from the previous exercise.
- Check to see if the results appear reasonable.

The Jython Library window has two or more panes selected by the tabs at the top. Only the "User's Library" is editable; the others came from the site path and system defaults.

How to Edit in the Jython Library and for more info see How to write a Jython Method and Jython Methods and the IDV