Learn IDL

Document Sample
Learn IDL Powered By Docstoc
					                  Learn IDL

                     Wei Huang
                 huangwei@ucar.edu
                Consulting Services Group
               Scientific Computing Division



June 9, 2006
                          What is IDL?
• IDL - Interactive Data Language

• The company
      – ITT Visual Information Solutions
               • http://rsinc.com/idl/




June 9, 2006
                    Start IDL
• Start IDL
      – idl, or
      – idlde
• Command Line
      – PRINT, “Hello, World!”
• Quit IDL
      – exit

June 9, 2006
                   Reference/Tutorial

• Tutorial over Internet
      – http://www.msi.umn.edu/software/idl/tutorial/
      – http://amath.colorado.edu/scico/tutorials/idl/


• An Introduction to Using IDL in Meteorology
      – http://cgam.nerc.ac.uk/visualisation/idl/guide/idl.pdf




June 9, 2006
                   IDL Variables
• Commonly Used Variables
       Variables   bits                Zero Value
       BYTE        8                   0B
       INT         16                  0
       LONG        32                  0L
       LONG64      64                  0LL
       FLOAT       32                  0.
       DOUBLE      64                  0.D
       STRING      Up to 32767 chars   „‟

June 9, 2006
               IDL Variables (Continued)
• User can define new type (structure)

• Dynamic (automatic) type definition

• Array index starts from 0 (like C)



June 9, 2006
               Getting Started with IDL
                   -Command Line Input
• Start with XY-Line Plot
• Making a Dataset
      original = SIN((FINDGEN(200)/35)^2.5)


• PLOT
      PLOT, original


June 9, 2006
               Plot and Processing
• Making a New Dataset
      noisy = ((RANDOMU(SEED, 200) - .5) / 2)
• Plot
      PLOT, noisy
• Plot them together
      PLOT, original, THICK = 3, $
       XTITLE = “Time”, YTITLE = “Amplitude”
      OPLOT, original+noisy

June 9, 2006
               Files and Input/Output
• Write data to a file, formatted
      OPENW, 1, „original.dat‟
      OPENW, 2, „noisy.dat‟
      PRINTF, 1, original
      PRINTF, 2, noisy
      CLOSE, 1
      CLOSE, 2

June 9, 2006
        Files and Input/Output (Continue)
• Read data from a file, formatted
      base = FLTARR(200)
      pert = FLTARR(200)
      OPENR, 1, „original.dat‟
      OPENR, 2, „noisy.dat‟
      READF, 1, base
      READF, 2, pert
      CLOSE, 1
      CLOSE, 2



June 9, 2006
       Files and Input/Output (Continued)
• IDL can handle lots data format
      – ASCII
               • Formatted, free-format
      – Binary
               • C-binary, Fortran-binary, etc.
      – NetCDF
               • Network Common Data Form
      – And More


June 9, 2006
       More on Plot and Processing
• Processing with SMOOTH
      full = base + pert
      smoothed = SMOOTH(full, 5)
      PLOT, smoothed, TITLE = “Smoothed Data”

• Filtering
      y = FINDGEN(200)
      y[101:199] = - REVERSE(y[1:99])
      filter = 1.0 / (1 + (y/40)^10)
      PLOT, filter
      lowpass = FFT(FFT(full, 1) * filter, -1)
      PLOT, lowpass
      highpass = FFT(FFT(full, 1) * ( 1.0 - filter), -1)
      PLOT, highpass




June 9, 2006
       More on Plot and Processing
• Put Together
      !P.MULTI = [0, 2, 3]
      PLOT, base, TITLE = 'Original "Ideal" Data'
      PLOT, full, TITLE = 'Noisy Data'
      PLOT, SHIFT(filter, 100), TITLE = 'Filter Function'
      PLOT, lowpass, TITLE = 'Lowpass Filtered'
      PLOT, highpass, TITLE = 'Highpass Filtered'
      PLOT, smoothed, TITLE = 'Smoothed with boxcar average'




June 9, 2006
                    Basic Programming
• DO-loop
      – FOR n=0, number DO a[n] = fix(n)

      – FOR n=0, number DO BEGIN
               • a[n] = fix(n)
               • B[n] = 2.0*a[n]^2 + 4.0*a[n] + 6.0 $
               •      + 8.0*a[n]^(-2.5)
      – ENDFOR

      – NOTE: $ is for continuation.
June 9, 2006
               Basic Programming
• IF-THEN BLOCK
      – IF (logical-statement) THEN something

      – IF (logical-statement) THEN BEGIN
          • Something
          • Other thing
      – ENDIF

      – IF (logical-statement) THEN BEGIN
          • Something
          • Other thing
      – ENDIF ELSE some-other-thing
June 9, 2006
                   Basic Programming
• CASE
      – CASE name OF
               • Match_1: do-something
               • Match_2: do-other thing
               • ELSE: do-things not-matched
      – ENDCASE




June 9, 2006
                       IDL Programming
                                -An Example
      PRO xplot

               sockeye = [463, 459, 437, 433, 431, 433, 431, 428, 430, 431, 430]
               coho = [468, 461, 431, 430, 427, 425, 424, 420, 418, 421, 420]
               chinook = [514, 509, 495, 497, 497, 494, 493, 492, 492, 493, 493]
               humpback= [467, 465, 449, 446, 445, 444, 443, 443, 443, 443, 445]
               year = [1967, 1970, INDGEN(9) + 1975]

               allpts = [[coho], [sockeye], [humpback], [chinook]]
               names = ['Coho', 'Sockeye', 'Humpback', 'Chinook']
               n1 = N_ELEMENTS(year) - 1




June 9, 2006
                IDL Programming (continued)

               PLOT, year, coho, yrange = [MIN(allpts), MAX(allpts)], $
                       title = 'Salmon Population', $
                       xtitle = 'Year', xrange = [1975, 1990], /XSTYLE, $
                       ytitle = 'Fish (thousands)‟

               FOR i = 1, 3 DO OPLOT, year, allpts[*, i], LINE = I

               FOR i = 0, 3 DO XYOUTS, 1984, allpts[n1, i], names[i]

      END




June 9, 2006
                IDL Programming (Exercise)
• Run the above program
      – Save it as xplot.pro
               • All IDL programs have suffix .pro


      – At IDL pommand line prompt:
               idl>.compile xplot
               idl>xplot



June 9, 2006
                  Contour Plot Example
      PRO contour_plot
               nx = 400
               ny = 300
               ax=fltarr(nx)
               ay=fltarr(ny)
               x=findgen(nx)
               y=findgen(ny)
               ax=2*!PI/nx*x
               ay=2*!PI/ny*y
               z = fltarr(nx, ny)

               FOR j=0, ny-1 DO BEGIN
                 FOR i=0, nx-1 DO BEGIN
                  z[i,j] = 100.0 * COS(ax[i]) * SIN(ay[j])
                 ENDFOR
               ENDFOR
June 9, 2006
        Contour Plot Example (continued)
           CONTOUR, z, NLEVELS = 21, /FILL

           CONTOUR, /FOLLOW, z, $
             LEVELS = -80 + findgen(5) * 40, $
             XSTYLE = 1, YSTYLE = 1, YMARGIN= 5, MAX_VALUE = 100, $
             C_LINESTYLE = [2, 1, 0], $
             C_THICK = [1, 1, 3, 1, 1], $
             C_LABELS = [1, 1, 3, 1, 1], $
             C_CHARSIZE = 1.25, FONT = 0, $
             TITLE = 'COS x SIN', $
             SUBTITLE = 'Interval: 10', $
             XTITLE = 'X - AXIS', $
             YTITLE = 'Y - AXIS', $
             /NOERASE
     END

June 9, 2006
               Check Before You Write
• IDL has lots of functions
      – SIN, COS, MIN, MAX, etc.
• Check if there is a function you can use,
  before writing your own
• Get help from IDL
      >idlhelp       on-site line user guide
      >idldemo       see what IDL has done


June 9, 2006
               Look Beyond the Basics
• We have covered xy-plot, contour
      – IDL is not just this
• Advanced IDL Features
      –   Surface plot
      –   Iso-surface
      –   Volume
      –   Image
      –   And more …

June 9, 2006
                  Questions?
• Send email to us:
      huangwei@ucar.edu
      consult1@ucar.edu
• Call us:
      303-497-1278 (consulting)
      303-497-8924 (Wei Huang)


June 9, 2006