next up previous contents
Next: A first IDL procedure: Up: Section 2: Introduction to Previous: Another example functions (of   Contents

Using keywords

We have passed in multiple parameters (pressure and temperature) into an IDL functon. Sometimes it is useful to specify optional things to pass in, which may be useful, but are not required. We do this using keywords.

For example, you can create a function temp_to_theta that uses a keyword to control what units are used for the input:

Function temp_to_theta,temp,press, degC=degC
;Function to convert temperature (K) and pressure (hPa) to potential
;temperaure (theta, K) using a reference pressure of 1000~hPa. 

;If keyword degC is set then converts input/output from/to Deg C. 

RoverCp=0.286
ref_press=1000.

if keyword_set(degC) then temp=temp+273.15

theta=temp*(ref_press/press)^RoverCp

if keyword_set(degC) then theta=theta-273.15

return,theta

end

We can give ``degC'' any value, but if it is not equal to zero, it is said to be ``set'', or ``defined''.

So,

.compile temp_to_theta
theta=temp_to_theta(273.15,900.)
use Kelvin.

And

.compile temp_to_theta
theta=temp_to_theta(0,900.,degC=1)
uses Celsius.

Try,

theta=temp_to_theta([0,0,0.],[900.,800.,700.])
We have written our function so it accepts a scalar (single number) or an array as inputs or outputs. Another good habit.



John Marsham 2009-12-07