Wednesday, February 26, 2014

IDL systax

Assignment
Syntax: Variable = expression
eg:
  x = 7
  num = [12,32,52,12]
 

If
Syntax:

   way1: 
        if expression then statement

   way2:
       if expression then statement1 else statement2

   way3: 
       if expression then begin
           statement1
       endif  
    way4: 
       if expression then begin
           statement1
       endif else be  begin
           statement2
      endelse
     
eg:
if y lt 0 then t=2

if y lt 0 then t=2 else t=3

if y lt 0 then begin
    t=2
   txt='Negative'
endif

if y lt 0 then begin
    t=2
   txt='Negative'
endif else begin
    t=3
   txt='Non-negative'
endelse

if ((x gt -2) and (x lt 3)) and ((y gt 5) and (y lt 8)) then t=2

For Loops
Syntax: 

     for variable = init, limit, step do statement
eg:
for i=0,9 do print,i

for t=1.0, 0.01, -.01 do plots,x*t,y*t

for ix=0L, n, 10 do begin
   x(j) = xx(ix)
   j = j+1
   print,ix
endfor

While Loops
Syntax: 

    while expression do statement
eg:
while x gt 0 do x=x-1
 

while not eof(lun) do begin
    readf,lun,txt
    print,txt
endwhile


Repeat Loops
Syntax: 


      repeat statement until expression
 

eg:
 repeat x=x-1 until x le 0

repeat begin
   readf, lun, x
    x = x-c
endrep until x le 0

Case
Syntax:
   case expression of
       expression: statement
         . . .
        expression: statement
        else: statement
   endcase

eg:
case animal of
    'cat': print,'meow'
    'dog': print,'arf arf'
    'bird': print,'tweet tweet'
    else: print,'??'
endcase

Goto
Syntax: 

    goto, label
 

eg: 
 . . .
loop:
. . .
goto, loop

. . .
goto, err
. . .
err: print,' Error ...'
. . .

Blocks
    Syntax:

      begin
        statement 1
        . . .
        statement n
      end
     
eg:
        if x lt 0 then begin print,x & a=2 & endif

        for i=0, 10 do begin readf, lun, txt & print,txt & endfor

Common

Purpose: Share variables between routines or remember values between calls to a routine. 

Syntax: 
        common     name, variable_1, variable_2, . . . variable_n,

name is the name of the common block. Variables are matched by position so need NOT have the same name in each routine.
eg:
        common xkodak_com, x, y, dx, dy, file, count
        common random_plot_com, seed

Notes: 

      A single routine may use a common to save the value of a variable between calls.
      To remember default values,
      to remember a seed value for the randomu function

    Several routines may use a common to share status values.
    In such cases it is useful to store the common in a separate file and
    include it in each routine (@filename where @ is in column 1).
    This way only a single copy of the common need be maintained.

    A good way to name commons is to use the main routine name followed by _com,
    like xkodak_com. This helps prevent the accidental use of the same name for
    diffrent commons.

Procedure definition
 Syntax: 

      pro     name, parameter_1, parameter_2, ... parameter_n

 Note:    name is the name of the procedure.
eg:

Definition:
        pro test, a, b, c

        pro compute, x, y, z, flag=flg, help=hlp

    Notes: A procedure must end with an "end" statement and MAY have one or more return statements inside.
    If program flow reaches the final end statement a return is implied.

Calling procedure:
    test, 2, 3, out
    compute, x, y, z, /flag

Function definition
    Purpose: Specify a function name and parameters.
Syntax: 

       function     name, parameter_1, parameter_2, ... parameter_n
    name is the name of the function.
eg:
        function test, a, b, c
        function compute, x, y, z, flag=flg, help=hlp

Notes: A function must end with an end statement and MUST have one or more return 

statements inside.     
A return statement in a function must include the return value: return, value.
eg:
    a = test(2, 3, 5)
    t = compute(x, y, z, /flag)

No comments:

Post a Comment