Showing posts with label idl. Show all posts
Showing posts with label idl. Show all posts

Friday, September 26, 2014

IDL Syntax

1. IDL Routines Syntax 

Refer to : http://www.physics.nyu.edu/grierlab/idl_html_help/idl_alph.html

 

 2. IDL Statement Syntax

Refer to :  https://www.astro.virginia.edu/class/oconnell/astr511/IDLresources/idl-syntx-sterner.html

Index

Definitions
Assignment
If
For
While
Repeat
Case
Goto
Block
Common
Procedure
Function

Some definitions


  • Operators: Items like +,-,*,/ and so on.
  • Constants: Items like 3, [3,2,5], "A string", AND, OR, and so on.
  • Variables: A named item used to store a value.
  • Expression: A constant, variable, or set of constants and/or variables combined by operators.
  • Statement: A single IDL statement or a statement block (see below).
  • Routine: A procedure or a function.


Assignment


  • Purpose: place a value in a variable.
  • Syntax: Variable = expression
  • Examples:
    • x = 7
    • num = [12,32,52,12]
    • y = 3*x^2 + 7*x - 5
    • cat = dog
  • Notes: expression may be a constant, variable, or combination of terms and operators


If


  • Purpose: Conditionally execute a statement.
  • Syntax:
    if expression then statement
    if expression then statement1 else statement2
  • Examples:
    • 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
  • Notes: For complicated expressions parentheses may be used to make sure expression has the desired meaning.


For Loops


  • Purpose: Repeat a statement a specified number of times.
  • Syntax: for variable = init, limit, step do statement
  • Examples:
    • 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 
  • Notes: The loop variable has the same data type as the initial value (init above). Make sure to use the correct data type for the initial value. A common error is: for t=0,1,.1 do ... which gives an infinite loop since .1 added to an integer variable does nothing. This is easily fixed: t=0.,1,.1 (note the 0. instead of 0). Another common error is not forcing a loop variable to be a long integer when the loop can go above 32767. The fix is: for i=0L,... A for loop may be executed 0 times if the loop variable starts beyond the loop limit.


While Loops


  • Purpose: Repeat a statement while some condition is true.
  • Syntax: while expression do statement
  • Examples:
    • while x gt 0 do x=x-1
    •   while not eof(lun) do begin
          readf,lun,txt
          print,txt
        endwhile
  • Notes: A while statement may be executed 0 or more times depending on the value of the expression.


Repeat Loops


  • Purpose: Repeat a statement until some condition is true.
  • Syntax: repeat statement until expression
  • Examples:
    • repeat x=x-1 until x le 0
    •   repeat begin
          readf, lun, x
          x = x-c
        endrep until x le 0
        
  • Notes: A repeat statement is always executed at least once.


Case


  • Purpose: Selectively execute a statement based on the value of an expression.
  • Syntax:
    case expression of
       expression:    statement
       . . .
       expression:    statement
       else:          statement
    endcase 
  • Examples:
    •        case animal of
      'cat':   print,'meow'
      'dog':   print,'arf arf'
      'bird':  print,'tweet tweet'
      else:    print,'??'
             endcase
    •        case t>0<2 of
      0:       begin
                 txt = 'red'
                 err = 0
               end
      1:       begin
                 txt = 'green'
                 err = 0
               end
      2:       begin
                 txt = 'blue'
                 err = 1
               end
             endcase
  • Notes: The expression following the word case is compared to a list of expressions. The statement corresponding to the first match is executed. If no match is found the statement following the else is executed. Else is optional but if no match is found and else is not included an error will result.


Goto


  • Purpose: Jump to a specified label in a routine.
  • Syntax: goto, label
  • Examples:
    •        . . .
      loop:
             . . .
             goto, loop
    •        . . .
             goto, err
             . . .
      err:   print,' Error ...'
             . . .  
  • Notes: May only be used in routines. Program flow jumps to the specified label. If label does not occur in the routine a compile error results.


Blocks


  • Purpose: Allows multiple statements to be executed anywhere a single statement is allowed.
  • Syntax:
      begin
        statement 1
        . . .
        statement n
      end
      
  • Examples:
    • if x lt 0 then begin print,x & a=2 & endif
    • for i=0, 10 do begin readf, lun, txt & print,txt & endfor
  • Notes: The plain end statement may be replaced by a more specific end statement for the following cases: if, else, for, while, and repeat. The corresponding end statements are: endif, endelse, endfor, endwhile, and endrep. While not enforced, these should always be used so the compiler can do better error checking. Only the case statement uses the plain begin/end pair to execute multiple statements for a match (the endcase is not really one of the end statements).


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.
  • Examples:
    • 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. Some examples of where this is useful: to remember default values, to remember a seed value for the randomu (or randomn) function since the system clock is used if no seed is given and for fast computers the same seed may be used for several calls. 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


  • Purpose: Specify a procedure name and parameters.
  • Syntax: pro name, parameter_1, parameter_2, ... parameter_n name is the name of the procedure.
  • Examples:
    • 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. Example calls to the above procedures:
    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.
  • Examples:
    • 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. Example calls to the above procedures:
    a = test(2, 3, 5)
    t = compute(x, y, z, /flag)

Thursday, March 20, 2014

IDL operators

IDL Operators

Parentheses
Parentheses are used to group expressions and to enclose function parameter lists.
;Parentheses enclose function argument lists.
SIN(ANG * PI/180.)
;Parentheses specify order of operator evaluation.
(A + 5)/B

Square Brackets
Square brackets are used to create arrays and to enclose array subscripts.
;Use brackets when assigning elements to an array.
ARRAY = [1, 2, 3, 4, 5]
ARRAY = [1, array_1, 3, array_2, 5] 

;Brackets enclose subscripts.
ARRAY[X, Y]
ARRAY(X, Y)   : works in version prior to version 5.0

Mathematical Operators

There are seven basic IDL mathematical operators, described below.

Assignment

A = 32
Compound Assignment Operators ( +=, -=, etc. )
A = A + 100 A += 100

Addition
;Store the sum of 3 and 6 in B. B = 3 + 6

;Store the string value of "John Doe" in B.
B = 'John' + ' ' + 'Doe'

Subtraction and Negation
;Store the value of 5 subtracted from 9 in C. C = 9 - 5
;Change the sign of C. C = -C

Multiplication
; Store the product of 2 and 5 in variable C: C = 2 * 5

Division
; Store the result of 10.0 divided by 3.2 in variable D: D = 10.0/3.2

Exponentiation

The caret (^) is the exponentiation operator. A^B is equal to A raised to the B power.

For real numbers, A^B is evaluated as follows:

    If A is a real number and B is of integer type, repeated multiplication is applied.

    If both A and B are real (non-integer), the formula AB = eBlnA is evaluated.

    A0 is defined as 1.

For complex numbers, A^B is evalutated as follows. The complex number A can be represented as A = a + ib, where a is the real part, and ib is the imaginary part. In polar form, we can represent the complex number as A = reiq = r cosq + ir sinq, where r cosq is the real part, and ir sinq is the imaginary part:

    If A is complex and B is real, the formula AB = (reiq)B = rB (cosBq + isinBq) is evaluated.

    If A is real and B is complex, the formula AB = eBlnA is evaluated.
    If both A and B are complex, the formula AB = eBlnA is evaluated, and the natural logarithm is computed to be ln(A) = ln(reiq) = ln(r) + iq.

Modulo
;Assign the value of 9 modulo 5 (4) to A. A = 9 MOD 5

;Compute angle modulo 2p.
A =(ANGLE + B) MOD (2 * !PI)

Increment/Decrement
Increment and decrement operators can be used, along with a variable, as standalone statements
    A++ or ++A

    A-- or --A

Increment/Decrement Expressions
B = 27
A = B++

In contrast, after executing the following statements, both A and B have a value of 26:

B = 27
A = --B

Minimum and Maximum Operators
The Minimum Operator

The "less than" sign (<) is the IDL minimum operator. The value of "A < B" is equal to the smaller of A or B. For example:

;Set A equal to 3. 
A = 5 < 3

;Set A equal to -6. 
A = 5 < (-6)

;Syntax Error. IDL attempts to perform a subtraction operation if 
;the "-6" is not enclosed in parentheses.
A = 5 < -6

;Set all points in array ARR that are larger than 100 to 100.
ARR = ARR < 100

;Set X to the smallest of the three operands.
X = X0 < X1 < X2


For complex numbers the absolute value (or modulus) is used to determine which value is smaller. If both values have the same magnitude then the first value is returned.

For example:

; Set A equal to 1+2i, since ABS(1+2i) is less than ABS(2-4i)
A = COMPLEX(1,2) < COMPLEX(2,-4)

; Set A equal to 1-2i, since ABS(1-2i) equals ABS(-2+i)
A = COMPLEX(1,-2) < COMPLEX(-2,1)

The Maximum Operator

The "greater than" sign (>) is the IDL maximum operator. "A > B" is equal to the larger of A or B. For example:

;'>' is used to avoid taking the log of zero or negative numbers.
C = ALOG(D > 1E - 6)

;Plot positive points only. Negative points are plotted as zero.
PLOT, ARR > 0

For complex numbers the absolute value (or modulus) is used to determine which value is larger. If both values have the same magnitude then the first value is returned. For example:

; Set A equal to 2-4i, since ABS(2-4i) is greater than ABS(1+2i)
A = COMPLEX(1,2) > COMPLEX(2,-4)

; Set A equal to 1-2i, since ABS(1-2i) equals ABS(-2+i)
A = COMPLEX(1,-2) > COMPLEX(-2,1)

Matrix Multiplication

IDL has two operators used to multiply arrays and matrices.

The # Operator

The # operator computes array elements by multiplying the columns of the first array by the rows of the second array. The second array must have the same number of columns as the first array has rows. The resulting array has the same number of columns as the first array and the same number of rows as the second array.

syntax:  M x N # P x M =  P x N   ( multiple col by row )

The ## Operator  (normal matrix multiplication)

The ## operator does what is commonly referred to as matrix multiplication. It computes array elements by multiplying the rows of the first array by the columns of the second array. The second array must have the same number of rows as the first array has columns. The resulting array has the same number of rows as the first array and the same number of columns as the second array.

For an example illustrating the difference between the two, see Multiplying Arrays.

syntax:  M x N ## N x P =  M x P     ( multiple row by col )


Array Concatenation

The square brackets are used as array concatenation operators.

The expression [A,B] is an array formed by concatenating A and B, which can be scalars or arrays, along the first dimension.

The second and third dimensions can be concatenated by nesting the bracket levels;

[[1,2],[3,4]] is a 2-element by 2-element array with the first row containing 1 and 2 and the second row containing 3 and 4. Operands must have compatible dimensions; all dimensions must be equal except the dimension that is to be concatenated, e.g., [2,INTARR(2,2)] are incompatible. Examples:

;Define C as three-point vector.
C = [-1, 1, -1]

;Add 12 to the end of C.
C = [C, 12]

;Insert 12 at the beginning of C.
C = [12, C]

;Plot ARR2 appended to ARR1.   
PLOT, [ARR1, ARR2]

;Define a 3x3 matrix.
KER = [[1,2,1], [2,4,2], [1,2,1]]

Logical Operators
There are three logical operators in IDL: &&, ||, and ~.

&& (and)

The logical && operator performs the logical short-circuiting "and" operation on two scalars or one-element arrays, returning 1 if both operands are true and 0 if either operand is false.

||  (or)

The logical || operator performs the logical short-circuiting "or" operation on two scalars or one-element arrays, returning 1 if either of the operands is true and 0 if both are false.

~  (not)

The logical ~ operator performs the logical "not" operation on a scalar or array operand. If the operand is a scalar, it returns scalar 1 if the operand is false or scalar 0 if the operand is true. If the operand is an array, it returns an array containing a 1 for each element of the operand array that is false, and a 0 for each element that is true.

Note

Programmers familiar with the C programming language, and the many languages that share its syntax, may expect ~ to perform bitwise negation (1's complement), and for ! to be used for logical negation. This is not the case in IDL: ! is used to reference system variables, the NOT operator performs bitwise negation, and ~ performs logical negation.

When is an Operand True?

When evaluated by a logical operator, an expression is considered to be "true" under the following conditions:

    For numerical operands, if the value is non-zero.
    For string operands, if the value is non-null.
    For heap variables (pointers and object references), if the value is non-null.

Logical Operator Examples

Results of relational expressions can be combined into more complex expressions using the logical operators. Some examples of relational and logical expressions are as follows:

;True if A is between 25 and 50. If A is an array, then the result 
;is an array of zeros and ones.
(A LE 50) && (A GE 25)

;True if A is less than 25 or greater than 50. This is the inverse 
;of the first.
(A GT 50) || (A LT 25)

Bitwise Operators

AND
NOT
OR
XOR

Relational Operators

The IDL relational operators can be used to test the relationship between two arguments. The six relational operators are described in the following table:
EQ
NE
GE
GT
LE
LT

In IDL, the value "true" is represented by the following:

    Any odd, nonzero value for byte, integer, and longword data types

    Any nonzero value for single, double-precision, and the real part of a complex number (the  
          imaginary part is ignored)

    Any non-null string


Using Relational Operators with Arrays

Relational operators can be applied to arrays, and the resulting array of ones and zeroes can be used as an operand. For example, the expression, ARR * (ARR LE 100) is an array equal to ARR except that all points greater than 100 have been reduced to zero. The expression (ARR LE 100) is an array that contains a 1 where the corresponding element of ARR is less than or equal to 100, and zero otherwise. For example, to print the number of positive elements in the array ARR:

PRINT,TOTAL(ARR GT 0)

Using Relational Operators with Infinity and NaN Values

On Windows and Solaris x86 platforms, using relational operators with the values infinity or NaN (Not a Number) causes an "illegal operand" error. The FINITE function's INFINITY and NAN keywords can be used to perform comparisons involving infinity and NaN values. For more information, see FINITE and Special Floating-Point Values.

Conditional Expression

The conditional expression-written with the ternary operator ?:-has the lowest precedence of all the operators. It provides a way to write simple constructions of the IF...THEN...ELSE statement in expression form. In the following example, Z receives the larger of the values contained by A and B:

IF (A GT B) THEN Z = A ELSE Z = B

This statement can be written more concisely using a conditional expression:

Z = (A GT B) ? A : B

The general form of a conditional expression is:

expr1 ? expr2 : expr3

Friday, February 28, 2014

IDL experience accumulated

1. Output device
1) SET_PLOT : specify the output device.
    Normally it's 'X' that is screen, however, you can specify other devices such as PostScript.
    Normal screen is black background. PostScript is white background.
2) DEVICE : access or control abilities a device provides.

2. Variables have be defined somewhere, whether it's in procedure/function or it's in the main program. What it mean by "defined" is that a variable needs to be assigned with a value.
You can feed less parameters to a procedure/function than what the procedure/function expects as long as you don't refer to these missing variables in your code.
eg:
  PRO proc1,  i1, i2, o1, o2
    o1=i1 + i2   ; o1 and o2 are defined within proc1
    o2=i1 * i2  
  END

  i1=1       ; i1 and i2 are defined within main.
  i2=2 
  proc1 i1, i2, o1   ; you don't feed o2 when calling proc1
  print o1              ;  and that's okay to miss o2 as long as you don't refer o2
  print o2              ; This is an error because you refer to o2.
                            ;  to correct the issue, you need to define o2 either in main or in proc1.


3. multiple statements can be joined together using "&"
eg:
    if deyong eq 1 then print, 1 &  print , 2 & print, 3
is the same as :
    if deyong eq 1 then begin
        print, 1
        print, 2
        print, 3
    endif


4. summary about "SIZE" function: return type info of a variable
  t1 = size(deyong, /type)
  print , "t1 is " , t1          ;  undefined
  deyong=3L
  t1 = size(deyong, /type)
  print , "t1 is " , t1          ;  3 -> long int
  deyong=4.5          
  t1 = size(deyong, /type)
  print , "t1 is " , t1           ; 4   -> float

5 "POSITION"
Allows direct specification of the plot window.
POSITION is a 4-element vector giving, in order, the coordinates [(X 0 , Y 0 ), (X 1 , Y 1 )], of the lower left and upper right corners of the data window. Coordinates are expressed in normalized units ranging from 0.0 to 1.0, unless the DEVICE keyword is present, in which case they are in actual device units. The value of POSITION is never specified in data units, even if the DATA keyword is present.
   Y /\ 
 1.0 |
       |
0.0  |________________\    X
      0.0                      1.0  /

6. PLOT
PLOT, [X,] Y
X :  A vector argument. If X is not specified, Y is plotted as a function of point number (starting at zero). If both arguments are provided, Y is plotted as a function of X .
Y :  A vector argument.

7. !PATH.MULTI
In IDL, the !P.Multi system variable can be used to create multiple plots in a display window. !P.Multi is a five element vector defined as follows:


!P.Multi(0) Contains the number of plots remaining on the page. Start with this as 0 to clear the page.
!P.Multi(1) The number of plot columns on the page.
!P.Multi(2) The number of plot rows on the page.
!P.Multi(3) The number of plots stacked in the Z direction.
!P.Multi(4) If 0, plots are displayed from left to right and top to bottom, i.e., the plots are displayed in rows. If 1, plots are displayed from top to bottom and from left to right, (i.e., the plots are displayed in columns).

To display four plots on a page in two columns and two rows, and the plots should appear in columns, the !P.Multi array should look like:
IDL> !P.Multi = [0, 2, 2, 0, 1]
8. COMMON block 
Common blocks are useful (a) when there are variables that need to be accessed by several IDL procedures or (b) when the value of a variable within a procedure must be preserved across calls.

Variables in a common statement have a global scope within procedures defining the same common block. Unlike local variables, variables in common blocks are not destroyed when a procedure is exited.

There are two types of common block statements:
    1. definition statements
          common block_name, v1, v2
    2. reference statements.      
          dxu: duplicates the COMMON block and variable names from a previous definition.
          common block_name            ; same as   common block_name, v1, v2

Variables in IDL COMMON blocks do not actually have names.

The number of variables appearing in the common block cannot change after the common block has been defined. ( Fixed once defined:  )


The "first program unit" (the one which gets compiled first : main program, function, or procedure, NOT order who gets executed ) to define the common block sets the number of included variables;
eg:
    common block_a , v1, v2, v3       ; there are 3 vars in block.



Other program units can reference the common block with any number of variables up to the number originally specified.
eg:
    common block_a , v1, v2         ; number could be less than the original number.

Different program units can give the variables different names.
eg:
    common block_a ,  M1, M2    ; name is NOT important, position IS.  so M1 = v1, M2=v2.

9. Array assignment. 
a=make_array(2,3, /integer, value=100)
b=make_array(2,3, /integer, value=200)
a) slice operation 
   b(0,0:1) = a(0, 0:1)   ; copy first two element in row 0
  b(0:1, 0) = a(0:1, 0)   ; copy first two element in column 0 
b) copy 1 row or 1 column
    b(*,1) = a(*,1)         ; copy column 1
   b(1,*) = a(1,*)          ; copy row 1
c) copy entire array
   b(*,*) = a(*,*)          ; copy all the rows / copy all the columns
   b = a                           ; same as above
d) shriek array
   a = a (*, 0:1)          ; array shrieked to first two columns.
e) Playing an array is easy.eg: 
   a = a + a     ; doubling all the elements in an array
   a = 3* a      ; tripling all the elements in an array
   a= a * 3      ; same as above
   a= a / 3       ; divide elements by 3
   a= a ^ 3      ; square elements

 f) print, a                     ; column-base: print column1 , then print column2, so on.
 g) raise initial value of array to 1-base
eg:
   a = INDGEN(3)+1
   print, a
output: 
   1       2       3

10. Index variable used in for-statement will still incremented/decremented one more time after for-statement exits.
eg:
   for i=2, 1, -1 do begin
      j=i    &   print, i , j
   endfor
   print, i , j

result: 
       2       2     ; i, j
       1       1     ; i, j
             1     ; i, j    , note : i will be decremented one more time after for-statement.

11. Conversion to string
Use strtrim or string:  str
eg:
a=strtrim("  34  ", 2)    ; 2nd parameter 2 means removal of both leading and trailing spaces.
help ,a
a=strtrim(23, 2)            ;  convert int to string
help ,a
a=string(23.234, format='(f5.2)')      ; convert float to string,
help ,a                                                ; f5.2 : 5 digits in total including dot with 2 digits after dot.
a=string(232.232)                 
help ,a
output:
A               STRING    = '34'
A               STRING    = '23'
A               STRING    = '23.23'
A               STRING    = '      232.232'

12. Catch message from IDL code when running IDL code from bash.
eg:
runIDL.sh 
#!/bin/bash
idl <<EOF
   .run  ${mainCode}
   7
   1
EOF

$ ./runIDL.sh   >log  2>> log2
Note:   a) Normal prints from IDL commands go to log.
            b) Messages from compiling IDL code go to log2.
$ tf log       # to monitor the process of IDL code. 

13. Logical/Mathematical operations involving array 
Basically it applies an operation to each element of an array and generates a new array. 
eg: 
a=indgen(5)
b=a>2          ; max value (mathematical operation)
c=a ge 2       ; logical operation.
print, a
print, b
print, c
       0       1       2       3       4
       2       2       2       3       4
      0   0   1   1   1

14. Device must close at the end of  plotting, otherwise, missing data could happen because these data are not flushed into the graphics if Device is not closed.
So remember to do "DEVICE, /CLOSE" explicitly when needed.

15. Make font better in PS output
aspect_ratio=1.5  ; rectangle shape
xsize=9
ysize=xsize/aspect_ratio
set_plot, 'ps'
!p.font=0
device, filename='fig_better.eps', encapsulated=1, /helvetica
device, xsize=xsize, ysize=ysize
plot, a, b, xtitle='X Title', ytitle='Y Title'
device, /close
set_plot, 'x'
!p.font=-1
 
16. where 
If "where" doesn't find any match, then what "where" returns is -1L, not an long array. 
In this case, using the filter return from "where" actually will copy the last 
element of input array to the new array. 
eg: 
var.a=[1,2,3,4] 
fil=where(var.a gt 4)
help, fil
print, "filter " , fil, n_elements(fil)
a.a = var.a(fil) 
print , a.a
 
output: 

FIL             LONG      =           -1
filter           -1           1
       4       4       4       4

This action could be used as a trick to initialize an array with the last element 
of an input array. 
 
eg: 
a = indgen(10) + 1 
b = make_array(10, /int, value=-999) 
filt = where ( a GT 3 ) 
help, b
print, b 
b = a(filt) ;; What this does is to copy data that satisfies the condition 
            ;; to beginning of array b and truncate tail of b off. 
help, b
print, b
 
Output:  
B               INT       = Array[10]
    -999    -999    -999    -999    -999    -999    -999    -999    -999    -999
B               INT       = Array[7]
       4       5       6       7       8       9      10
 
  
16. Create array as 1-based, or 2-based, etc. 
Eg: 
  a = INDGEN(10) + 1  ; 1-based array of 10 elements.   
  a = INDGEN(10) + 2  ; 2-based array of 10 elements
 
17. Read a line as string. 
   ; Either way, it works to read a line as string. 
   READF, iu, a
   ;READF, iu, form='(a0)',  a
   b=strsplit(a,/extract) ; /extract to return sub-string, w/o it it returns 
                          ; an array of the position of the substrings is returned.
   print, b(-1)    ; -1: print last element of an array
 
18. [] operator 
   ; This is so wrong. Here is the reason why.
   ; [1, nPos] is 2-element array
   ; [bias(*,iChan,0), bias(*,iChan,0)] is 60-element array.
   ; because * represents 30 scan positions.
   ; What happens is it gets the two values from the first
   ; two scan positions. I don't know what the initial intent is for.
   ; but this doesn't seem to do whatever the intent is for.
   ;;; PLOTS,[1, nPos], [bias(*,iChan,0), bias(*,iChan,0)]
   ; Change to below to connect first and last
;     PLOTS,[1, nPos], [bias(0,iChan,0), bias(nPos-1,iChan,0)]
 
19. if-else 
Doing so to simplify complicated if_else_statement. 
if (3 gt 4 ) then    $
    print, 'big '    $           ; concatenation is NOT allowed 
else                 $
    print, 'small' & $           ; concatenation IS allowed in else-statement
    print, 'small' & $
    print, 'small' & $
    print, 'small'
 
20. CD command 
The CD procedure is used to set and/or change the current working directory.  
CD, new_dir, CURRENT=old_dir   ; save current directory inot var old_dir 
CD, 'data'
 
; how to get the current working directory info 

IDL> cd , current=old          
IDL> print, old
/data/home001/dxu/graphic

IDL> CD, CURRENT=c & PRINT, c    ; do it in one line

IDL> cd, 'graphic'     ; take relative path
IDL> cd , '../../'          ; take relative path


IDL> cd , '/data/home001/dxu  '                ; Do NOT have whitespace in the directory string.
% CD: Unable to change current directory to /data/home001/dxu .
  No such file or directory
% Execution halted at: $MAIN$
IDL> cd , '/data/home001/dxu'       ; Only work when there is NO white-space.

21. Plot map with data 
eg: 
LOADCT, 39
MAP_SET, 0,0, charsize=1,  /label, latlab=-180, lonlab = -90, latdel=30, londel=60
for i =0, num-1 do begin
   OPLOT, [x(i)], [y(i)], psym=2, symsize = 5, color=y[i]
endfor
MAP_CONTINENTS, /hires, fill_continents=0,  MLINESTYLE=0, MLINETHICK=1, color=210

22. index in where. 
a=indgen(30)+1
print, a
print,'---------------'

ff=where(a ge 20)    ; ff is index of a.
b= a(ff)
print, ff
print,'====='
print, b
print,'---------------'

ff2 = where (b ge 25)  ; ff2 is index of new array b
c= b(ff2)
print, ff2
print,'====='
print, c


23. Set IDL path 
Rather than having to type that !path statement every time you start IDL, you can put it in your .idlstartup file.

$ vi   ~/.idl/.idlstartup

Just add:
!PATH=!PATH+':'+Expand_Path('./src/idl/coyote/')

$ cd  ~/.idl/itt/pref-10-idl_8_0-unix/idl.pref
$ vi     idl.pref

You add:
IDL_STARTUP : ~/.idl/.idlstartup

Maybe there's an easier way to do it, but at least you don't have to assign !PATH every time you start IDL.

If you want to access coyote from other IDL instances, then just replace that path with the absolute path.

24. Here is another way thread about setting up IDL path

Adding Programs to Your IDL Path

IDL looks for programs (*.pro  or *.sav) in a list of directories called a "path". If a program is in a folder that is in your path then IDL knows to look for it when you try to call it in your program or from the command line. By default your present working directory (pwd a.k.a. ./) are in your path as well as some default IDL folders. I suggest that you create a folder called "idl" to keep all your programs in to keep it organized. You then need to add that folder to your path in one of the ways described below.

dxu: Note that this is only to specify the location where IDL can fine a IDL code (*.pro or *.sav). It has nothing to do with a specific subroutine/function within an IDL file.
For example, if you are calling a subroutine called "generateOutput", which is in a.pro file,  then you still need to include @a.pro in your main IDL code, so the module "generateOutput" is available to your main code to use. When IDL looks for "a.pro" file, it will go through a list of directories called a '!path' to find it.
1) So to make a module (function/subroutine) available to your main code, you need to use "@" statement to include a IDL code (eg: a.pro)  that contains the module. 
2) To let IDL find that specific file (eg: a.pro) , you need to specify the location where that file is located in !path, which is used by IDL to find all the IDL code (*.pro and *.sav)

The easiest way to do add a folder to the path that is independent of architecture is the following:
1) Make an IDL startup file. If you have already done this then skip to step 2. You can make an IDL startup file by
     a) creating a file called .idlstartup  in /path/to folder.
     b) within IDL type:
     IDL> pref_set, 'IDL_STARTUP', '/path/to/.idlstartup',/commit
         where you replace '/path/to/.idlstartup' with the path to the file you created for step 1a

2) Now edit your .idlstartup file to include the following lines to include a folder which for example is located in ~/example/
           !PATH=!PATH+':'+Expand_Path('~/example/')
If I wanted to include all sub-directories I would add the following line instead (notice the + sign in front):
     !PATH=!PATH+':'+Expand_Path('+~/example/')

You can also do the same actions in your bash or csh profiles without the use of the .idlstartup file as follows:
bash:
      export IDL_PATH=$IDL_PATH:+'~/example/'
csh:
     setenv IDL_PATH +$IDL_PATH:~/examples

where you can again include all subdirectores by leading the name with a + sign



IDL array

Here are arrays I'm interested at:
1. Create array with initialization to subscription
B-INDGEN       : byte                         1
U-INDGEN       : unsigned int            2
INDGEN          : int                            2 
UL-INDGEN     : unsigned long int    4 
L-INDGEN        : long int                    4
 
UL64-INDGEN : unsigned 64-bit int   8
L64-INDGEN    : 64-bit int                  8
 
F-INDGEN        : float                          4
D-INDGEN        : double                      8
S-INDGEN         : string                       2.1GB

Eg:
I = INDGEN(5)

2. Create array only
BYTARR   
UINTARR   
INTARR   
 
ULONARR   
LONARR
   
ULON64ARR
LON64ARR
 
FLTARR
DBLARR
STRARR

Eg: Create I, a 3-element by 3-element integer array with each element set to 0 by entering:
I = INTARR(3, 3)

3. MAKE_ARRAY
The MAKE_ARRAY function returns an array of the specified type, dimensions, and initialization. This function enables you to dynamically create an array whose characteristics are not known until run time.
Eg: 
 To create M, a 3-element by 4-element, integer array with each element set to the value 5, enter:
M = MAKE_ARRAY(3, 4, /INTEGER, VALUE = 5)


4. REPLICATE 
   Creates an array of given dimensions, filled with specified value.
Eg: Create D, a 5-element by 5-element array with every element set to the string "IDL" by entering:
D = REPLICATE('IDL', 5, 5)

Thursday, February 27, 2014

IDL Command reference


A B C D E F G H I J K L M N O P Q R S T U V W X Z
 
ABS
CD
COS
EOF
EXP
FFT
FIX
HLS
HQR
HSV
MAX
MIN
RK4
ROT
SIN
T3D
TAN
TV
VEL
WTN