IDL Notes

The following are some notes on the syntax, commands, etc. for IDL programming and plotting.


A. STARTUP and SHUTDOWN
   1. in .tcshrc: setenv LM_LICENSE_FILE ####@computer.rice.edu
               where #### is the correct number for the license manager
                  setenv IDL_STARTUP /home/hartigan/.idlstartup
               to load any specific commands or path extensions
   2. open the development environment: idlde  
   3. to quit: exit
   
B. BASIC COMMANDS (case insensitive unless in a string)
   dir='/l1/hartigan/iraf'  ; Define a variable
   delvar,varname           ;  Delete a variable
   cd,'/home/hartigan/progs' ; Changes directories. Or type
   cd,dir                    ; if dir is defined
   $pwd                      ; Run OS commands with $ 
   ?                         ; Access on-line help utility
   &                         ; put more than one command on a single line
   help, varname             ; shows the array/vector size of varname
   save, filename='output.dat', varname ;saves the array varname onto disk
   save, /all, filename='session.sav'   ;saves entire IDL session for use later
   save, /variables, filename='session.var' ;saves all IDL variables for use later
   restore, 'session.sav'        ;restores the saves IDL session

C. PROCEDURES
   ; denotes a comment
   $ is a line continuation

   pro procedure_name ; Save this with a .pro extension
      ...code...      ;
   end                ;

                      ; click on OPEN to open it
  .compile procedure_name ; to compile a procedure, or click COMPILE
  .run procedure_name ; to run a procedure, or click RUN
  @list1              ; to run macro list1


D. PLOTTING
   Read ascii data: plotdata=read_ascii('/home/hartigan/filename')
   Print data to screen: print,plotdata
   See what is inside a structure: help,/str,plotdata
       Usually this has something like 'field1', which you can then print out to the screen, e.g.
   print,plotdata.field1(0,*) 
   define a variable: wavelength=plotdata.field1(0,*) ;note arrays begin at 0 !!
   specify values of variable: scores=[20,21,33,45,99,-12,2,18,54]
   plot,wavelength,flux ;plot flux vs. wavelength:
           ;various flags for plot, include these on the line with the plot command: 
   psym=2 ;specify symbol type (6:square,10:histogram)
   psym=sym(3),symsize=2 ; more symbols, including filled ones, need sym.pro
                         ;1=filled circle, 2=filled triangle, 3=filled inverted triangle
                         ;4=filled diamond, 5=filled square, 6=open circle
                         ;7=open triangle , 8=open inverted triangle, 9=open diamond
                         ;10=open square, 11=+, 12=X, 13=*, 14=right filled triangle
                         ;15=left filled triangle, 16=right open triangle, 17=left open triangle
                         ;showsym to print
   /ynozero       ; don't force y-axis to zero - also can set ystyle
   /noerase       ; add to existing plot - also use !p.noerase
   /polar         ; for a polar plot
   thick=2        ; line thickness
   yr=[13.4,15.2] ; set y-range (use xr for x-range) Do not set on a separate line. Should be on the line w/plot command
   ystyle=1       ; to plot exact yr values. = 13 to suppress axes   Also on line with the plot command
   xstyle=1       ; to plot exact xr values. = 13 to suppress axes   Also on line with the plot command
   max_value=1000 ; do not plot data > 1000
   ylog           ; for log axis in y-direction
   xtitle="wavelength" ; x-axis label,  put on same line as the plot command
   ytitle="flux"  ; y-axis label,  put on same line as the plot command
   title="two!Clines" ; 2-line title
   linestyle=5    ; change line type. 1 is dotted, 5 is dashed, 0 is solid
   xticks=3       ; plot 3 major tics on x-axis. Can specify beforehand with !x.ticval
   ticklen=1      ; draws a grid length of tic marks
   color=green    ; color of line or points
   arrow,0,2,3,4,/data,/solid ; draw arrow from (0,2) to (3,4)
 ;some variables that affect plot 
  !p.charsize=2.2 ; change size of font
  !x.style=4      ; suppress top and bottom x-axis. =1 to remove space at ends
  !x.minor=5       ; number of minor x-axis tics
  !x.tickname=" " ; to eliminate x-axis tick names
  !x.tickname=""  ; to restore default x-axis tick names
  !x.ticklen=0.05 ; specify tic lengths in units of graph size. Similar command for y
  !x.tickval=[0,0.5,1] ; specifies tic values.  Combine with xtics=3 on plot command
  !p.multi=[0,2,3] ; for 2 plots in x, 3 in y. reset with !p.multi=0
  !p.noerase=1    ; to plot without erasing. set = 0 to erase again
  !p.position=[-0.1,0.5,0.6,1.05] ; plot in location x=-0.1,0.6 y=0.6,1.05 on screen

   xyouts,100,30,"hi",orientation=45 ; puts label 'hi' at x=100,y=30, angled 45 degrees
   xyouts,100,30,'2"' ; prints 2", useful for arcseconds. Note "2 means octal 2...
   xyouts,100,30,'!7 D!XV' ; prints Delta V. The !7 changes font and !X changes it back
   xyouts,100,30,'V!Ljet!N' ; prints V subscript{jet} Use !U for superscript. The !N returns to regular font
   plots,100,20,psym=sym(6),symsize=2 ; plot a large open circle at x=100 y=20
   plots,[0,1],[0,3],linestyle=1 ; draw a dotted line from (0,0) to (1,3) *not* (0,1) to (0,3)
   oplot newvariable  ; overplots
   plot d,sin(d)/d    ; to plot out function sin(x)/x for x=1,2,...50
   temp=[-200,200] & temp1=[0,0] & oplot,temp,temp1 ; overplot horizontal line
   axis,yaxis=1,ystyle=1 ; plot only the right-side y-axis, with exact yr values

  ; to overplot errorbars (not the points), data are vectors x, y, and yerr :
   errplot,x,y+yerr,y-yerr

E. ARRAYS
   d=replicate(3.0,4,5) ;creates 4x5 matrix with all elements set=3.0
   d=findgen(50) ;creates vector with d[0]=0,d[1]=1,...d[49]=49
   d=findgen(10,5) ;creates 10(column)x5(row) matrix:
                   ;d[0,0]=0, ... d[9,0]=9, d[0,1]=10, ... d[8,3]=38 ...
                   ;NOTE --> IDL convention of column,row is opposite to standard
                   ;         mathematical specification of row,column
   array=[1.0,2.0,3.0,4.0] ;assign values to a vector
   array=[[1.0,2.0,3.0],[4.0,5.0,6.0]] ;assign values to a 3(column)x2(row) matrix
   y[100:199]=reverse(y[0:99]) ; sets y[100]=y[99], y[101]=y[98], ...y[199]=y[0]
   sz=size(a) ; sz[0] is dimension of array. For 2d, array is sz[1] by sz[2] 
              ; sz[3] shows data type and sz[4] is total number of elements
   num=n_elements(a) ;total number of elements within a

   d=a##b  ;matrix d equals a times b, a and b matrices
   d=a#b   ;matrix d equals b times a, a and b matrices
           ;may want to use matrix_multiply to multiply and transpose simultaneously
   c=v##w  ;matrix c equals outer product of vectors v and w
   d=transpose(c) ; to transpose a matrix
   d=determ(c) ; determinant of matrix c
   ev=hqr(elmhes(c),/double) ; stores eigenvalues of matrix c into ev
   evec=eigenvec(a,ev,residual=resid) ; evec is eigenvector of matrix a with eigenvalue ev

   d=invert(c) ;to find inverse of matrix c. Use /double=1 to get double precision
   pm,array ;prints transpose of the array
   rm,array,3,2 ;prompts for input values to a 3(column)x2(row) array
                ;the prompt 'row n:' really means 'column n'

F. FITTING FUNCTIONS
   coeff=linfit(x,y)        ; fits a line
   yfit=coeff[0]+coeff[1]*x ; defines fitted y-values in variable yfit
   smoothdata=smooth(original,5) ;boxcar smooth

G. SPECIAL CHARACTERS
   !C   ;carriage return
   !pi  ;3.14159...
   ;    ;denotes a comment
   $    ;is a line continuation
   &    ;put more than one command on a single line
   # and ## ;matrix multiplication
   cents ; string(162b)
   British pound ; string(163b)
   sun symbol ; combine psym=sym(1),symsize=0.2 and psym=sym(6)
   earth symbol ; combine psym=sym(11) and psym=sym(6)

H. PRINTING
   ps_open,'plot.ps' ;makes postscript file
   ...plot commands...
   ps_close

   openw,1,"filename"  ; opens unit 1 for writing
   printf,1,lambda     ; writes IDL variable lambda to 'filename'
   close,1             ; closes unit 1

I. PROGRAMMING
   fits_write,'test.fits',array ;write array to file test.fits with minimal fits header
   fits_read,filename,array,hdr ;read a fits file from filename to array,
                                ;fits header in hdr
   sxaddpar,hdr,'CRVAL1',1.0     ;add parameter CRVAL1 to hdr
   crval1=sxpar(hdr,'CRVAL1') ;read a keyword from a fits header
   plotdata=read_ascii('filename') ;read ascii data
   img=filepath('myimage.gif') ;load an image
   make_array name[nx,ny,nz] ; make a 3-D array
   n_elements(a) ; dimension of array a
   total(a)      ; sum of all values of array a
   negs=where(a lt 0)  ; array values where a<0

   for i=0,n-1 do begin ...statements... ;loops
   endfor

   for i=0,3 do print,i    ;loop on a single line
   for i=10,3,-1 do print,i    ;loop on a single line, step=-1

   if (a eq 2) and (b eq 5) then begin  ; if/then/else statements
      print,a-b
   endif else begin
      print,b
   endelse

   d=randomu(seed,20) ;creates 20 random numbers between 0 and 1