next up previous contents
Next: Using keywords Up: Section 2: Introduction to Previous: An example function (of   Contents

Another example functions (of multiple variables)

Creating and using a function that takes more than one input variable is essentially no different.

For example, we would like to convert temperature (T) and pressure (p) into potential temperature (theta). Again this is something we will use over and over again, so its good to write a small program to do it.

We know,
$\theta = T (p_0/p)^{R/C_p}$
where $p_0$ is the reference pressure (normally 1000 hPa) and $R/C_p$=0.286

Make a file called temp_to_theta.pro

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

RoverCp=0.286
ref_press=1000.

theta=temp*(ref_press/press)^RoverCp

return,theta

end

You can use this to convert zero degrees Celsius (273.15 K) at 900 hPa to a potential temperature using,

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

Now write a program to convert potential temperature to temperature.



John Marsham 2009-12-07