Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures...

8
Functions and Procedures

Transcript of Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures...

Page 1: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

Functions and Procedures

Page 2: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

Introduction

• IDL programs fall into two classes: functions and procedures

• Functions: can encapsulate only one operation, and return one result

• Procedures: can encapsulate several operations, and return several results

• Therefore, procedures are preferred than functions

• Functions and procedures need to be compiled first before they are called in the main driver program. You may put all your functions and procedures in a separate file, and compile it before running your main program

Page 3: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

Functions I: Intrinsic functions in IDL

• IDL provides over 750 intrinsic math and other functions. Please see IDL reference guide for a list of all intrinsic functions: http://idlastro.gsfc.nasa.gov/idl_html_help/Functional_List_of_IDL_Routines.html

• Often-used math functions: abs, sqrt, exp, alog, alog10, sin, cos, tan, asin, acos, atan, randomn, randomu

print, sin(!pi/3.0)

y=randomu(seed, 100)

plot, y

Page 4: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

Functions I: Intrinsic functions in IDL(cont.)

• Often-used data-processing functions: interpol, smooth, histogram

y2=smooth(y, 11, /edge_truncate)

• Linear analysis: a_correlate, c_correlate, regress, linfit, curvefit

lag=indgen(10)

a=a_correlate(y,lag)

plot,lag,a

c=c_correlate(y,y2,lag)

plot,lag,c

• Spectral analysis: FFT, wavelet

f=fft(y)

plot, f

• EOF/SVD analysis: Eigenvec, SVDC

• Statistical distributions: binomial, gauss_pdf, gauss_cvf, TM_test, CTI_test

Page 5: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

Functions II: User-defined Functions

• A function begins with a “Function” statement containing function name, a list of input arguments, and/or a list of keywords. A function also includes a “return” statement to return the result to the caller, and ends with an “end” statement

Function f_to_c, deg_f

return, (deg_f - 32.0)*(5.0/9.0)

end

• Calling sequence: Result = Function_name(argument1, argument2, …)

deg_f = 80.0

deg_c = f_to_c(deg_f)

Page 6: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

Procedures

• A procedure begins with a “Pro” statement containing procedure name, an argument list and/or a keyword list. A procedure ends with an “end” statement

Pro f_to_c2, deg_f, deg_c

deg_c = (deg_f - 32.0)*(5.0/9.0)

end

• Calling sequence: Procedure_name, argument1, argument2, …)

deg_f = 80.0

f_to_c2, deg_f, deg_c

print, deg_c

Example: A subroutine for overplotting a square box

Page 7: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

Line plots: Logarithm, histogram and bar plots

• Logarithm: Plot, x, y, /xlog, /ylog• Calculate histogram: density=histogram(data, max=max, min=min, $ nbins=nbins, locations=loc) binsize=(max-min)/(nbins-1) locations gives the locations of the starting point of

each bin• Plot histogram:

plot, loc, density, psym=10• Bar plot: bar_plot, data, barnames=bn, $ title=tit, xtitle=xtit, ytitle=ytit

Page 8: Functions and Procedures. Introduction IDL programs fall into two classes: functions and procedures Functions: can encapsulate only one operation, and.

In-class assignment III• Calculate y = x0.5 + ex/100.0 + ln(x) + sin(x) +

cos(2x) for x=3.0

• Do the above calculation using a function for x=1.0, 3.0, 5.0

• Do the above calculation using a procedure for x=2.2, 3.0, 6.0

• X=findgen(200)/50.0+1.0, calculate y using one of the above methods. Assume lag=indgen(10),

(1) apply an 11-point smoothing to y and get y2

(2) calculate and plot auto-correlation of y

(3) calculate and plot cross-correlation of y and y2

(4) calculate and plot the histogram of y

• Write a subroutine for overplotting a triangle