IDL_Intro

download IDL_Intro

of 61

Transcript of IDL_Intro

  • 8/2/2019 IDL_Intro

    1/61

    Introduction to IDL

    Thayer School of EngineeringSimon Shepherd

    [email protected]://thayer.dartmouth.edu/~simon

    A Kiewit Minicourse

  • 8/2/2019 IDL_Intro

    2/61

    Minicourse: Introduction to IDL February 2005

    IDL

    AgendaWhat is IDL ?How to run IDL

    Simple IDL CommandsArrays in IDL

    Data I/OBasic PlottingBuilt-in Analysis FunctionsMapping

    Example

  • 8/2/2019 IDL_Intro

    3/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Scientific Computing Today

    Numeric/graphics: IDL, Matlab, Octave

    Symbolic: Maple, Mathematica, MathCad

    Dataflow: LabView, Simulink, AVS, DX, OpenDX

    Scripting languages: sh, csh, bash, Tcl, Perl

    Traditional programming languages:Basic, Pascal, FORTRAN, C, C++, Java

  • 8/2/2019 IDL_Intro

    4/61

    Minicourse: Introduction to IDL February 2005

    IDL

    What is IDL ?Interactive D ata L anguage

  • 8/2/2019 IDL_Intro

    5/61

    Minicourse: Introduction to IDL February 2005

    IDL

    What is IDL ?Interactive D ata L anguage

    Proprietary software distributed by Research S ystems, Inc. ofBoulder, CO now a division of Kodak.

  • 8/2/2019 IDL_Intro

    6/61

    Minicourse: Introduction to IDL February 2005

    IDL

    What is IDL ?Interactive D ata L anguage

    Proprietary software distributed by Research S ystems, Inc. ofBoulder, CO now a division of Kodak.

    Grew out of programs written for analysis of data from NASA missions such as Mariner and the International Ultraviolet Explorer.

  • 8/2/2019 IDL_Intro

    7/61Minicourse: Introduction to IDL February 2005

    IDL

    What is IDL ?Interactive D ata L anguage

    Proprietary software distributed by Research S ystems, Inc. ofBoulder, CO now a division of Kodak.

    Grew out of programs written for analysis of data from NASA missions such as Mariner and the International Ultraviolet Explorer.

    Oriented toward use by scientists and engineers in the analysis and visualization of multi-dimensional data sets.

  • 8/2/2019 IDL_Intro

    8/61Minicourse: Introduction to IDL February 2005

    IDL

    What is IDL ?Interactive D ata L anguage

    Platform Independent: Unix, linux, Windows, Macintosh

    Proprietary software distributed by Research S ystems, Inc. ofBoulder, CO now a division of Kodak.

    Grew out of programs written for analysis of data from NASA missions such as Mariner and the International Ultraviolet Explorer.

    Oriented toward use by scientists and engineers in the analysis and visualization of multi-dimensional data sets.

  • 8/2/2019 IDL_Intro

    9/61Minicourse: Introduction to IDL February 2005

    IDL

    What is IDL ?Interactive D ata L anguage

    Offers all the power , adaptability , and programmability ofhigh level languages like FORTRAN, C, and C++.

  • 8/2/2019 IDL_Intro

    10/61Minicourse: Introduction to IDL February 2005

    IDL

    What is IDL ?Interactive D ata L anguage

    Offers all the power , adaptability , and programmability ofhigh level languages like FORTRAN, C, and C++.

    But it adds two capabilities which are essential for moderndata analysis: interactivity and graphics display.

  • 8/2/2019 IDL_Intro

    11/61

  • 8/2/2019 IDL_Intro

    12/61Minicourse: Introduction to IDL February 2005

    IDL

    Ways to Run IDL

    Interactively

    Batch mode

    Development Environment ( DE)

    Command Line

  • 8/2/2019 IDL_Intro

    13/61Minicourse: Introduction to IDL February 2005

    IDL

    Ways to Run IDL

    Batch mode

    prompt> idl filename

    where filename contains a list of IDL commands

    commands are interpreted as if they were entered

    at the command lineNo multiline statements such as

    begin...end

  • 8/2/2019 IDL_Intro

    14/61Minicourse: Introduction to IDL February 2005

    IDL

    Ways to Run IDL

    Command Line

    prompt> idl

    each command entered is interpreted and executedand the prompt is returned

    IDL> command

    IDL>

  • 8/2/2019 IDL_Intro

    15/61

  • 8/2/2019 IDL_Intro

    16/61

    Minicourse: Introduction to IDL February 2005

    IDL Graphical User Interface (DE)

  • 8/2/2019 IDL_Intro

    17/61

    Minicourse: Introduction to IDL February 2005

    IDL

    menu

    Graphical User Interface (DE)

  • 8/2/2019 IDL_Intro

    18/61

    Minicourse: Introduction to IDL February 2005

    IDL

    toolbar

    Graphical User Interface (DE)

  • 8/2/2019 IDL_Intro

    19/61

  • 8/2/2019 IDL_Intro

    20/61

    Minicourse: Introduction to IDL February 2005

    IDL

    project

    Graphical User Interface (DE)

  • 8/2/2019 IDL_Intro

    21/61

    Minicourse: Introduction to IDL February 2005

    IDL

    output

    Graphical User Interface (DE)

  • 8/2/2019 IDL_Intro

    22/61

    Minicourse: Introduction to IDL February 2005

    IDL

    variables

    Graphical User Interface (DE)

  • 8/2/2019 IDL_Intro

    23/61

    Minicourse: Introduction to IDL February 2005

    IDL

    command line

    Graphical User Interface (DE)

  • 8/2/2019 IDL_Intro

    24/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Using IDL as a calculatorIDL> a = 3 ; assignment statement

    ; semicolon is used for comments

    IDL> print , a + 4 ; built-in print command7

    IDL> print , a + 4. ; variables dynamically typed7.00000 ; take on highest precision

    IDL> print , a + 4D; double precision

    7.0000000IDL> print , a * 1e-9 ; scientific notation

    3.00000e-09

    Note: variable names are case insensitive ( A = a )

  • 8/2/2019 IDL_Intro

    25/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Data Types1 Byte n B 8-bit unsigned

    2 Integer n 16-bit signed

    3 Longword n L 32-bit signed

    4 Floating Point n.n 32-bit (+10 38 )

    5 Double-Precision n.n D 64-bit (+10 38 )

    6 Complex COMPLEX (n.n,n.n ) real/imaginary pairs7 String 'ssss' 0-32k in length

    8 Struct User-defined Structure

    9 D-P Complex DCOMPLEX (n.n,n.n ) real/imaginary pairs double-precision

    10 Pointer Pointer

    11 ObjRef Object Reference12 Unsigned Int n U 16-bit unsigned

    13 Unsigned Long n UL 32-bit unsigned

    14 64-bit Long LONG64 (n ) 64-bit signed longword

    15 64-bit Unsigned Long ULONG64 (n ) 64-bit unsigned longword

  • 8/2/2019 IDL_Intro

    26/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Unary and Compound OperatorsIDL> a = 3 ; assignment statement

    IDL> a += 5 ; C compound operatorsIDL> a = a + 5 ; equivalent but rather tedious

    IDL> print , a-- ; careful...8

    IDL> print , a8

    IDL> a++ ; C unary operatorsIDL> print , a

    8

    IDL> print , a7

  • 8/2/2019 IDL_Intro

    27/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Arrays in IDL

    Dynamically sized (and resized)

    Any data type (but not mixed)

    Array-oriented language

    operations are performed on arrays

  • 8/2/2019 IDL_Intro

    28/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Vectors in IDLIDL> vec = [ 3 , 5 , 1 , 4 , 5] ; assignment statement

    IDL> print , vec ; 5 element vector 3 5 1 4 5

    IDL> print , vec[ 3] ; elements of array 9

    vec[ 0] vec[ 1] vec[ 2] vec[ 3] vec[ 4]

    IDL> vec[ 3] = 9 ; square brackets to access

  • 8/2/2019 IDL_Intro

    29/61

  • 8/2/2019 IDL_Intro

    30/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Matrices in IDLIDL> print , mat[ 3, 1] ; access elements of matrix

    9

    column row

    IDL> print , mat[ 5*1+3] ; equivalent...9

    column row format... M

    M by N

    M

  • 8/2/2019 IDL_Intro

    31/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Matrices in IDL

    ; n th row: n*M m th column: m

    1*5 + 3 = 8

    3 5 1 4 5

    8 3 2 9 1

    column 0 1 2 3 4row

    0

    1

    M by N

    5 by 2

    mat[ 3, 1] = mat[ 5*1+3]

  • 8/2/2019 IDL_Intro

    32/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Declaring MatricesIDL> mat = [[ 3 , 5 , 1 , 4 , 5],[ 8 , 3 , 2 , 9 , 1]] ; explicitlyIDL> mat = fltarr ( 5, 2) ; all zeros

    IDL> arr = dblarr ( 8, 5 , 2) ; other types

    P by M by N array

    arr[ 6 , 2 , 1] = arr[n*P*M + m*P + p]

    40 + 16 + 6

  • 8/2/2019 IDL_Intro

    33/61

  • 8/2/2019 IDL_Intro

    34/61

  • 8/2/2019 IDL_Intro

    35/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Searching ArraysIDL> mat = [[ 3 , 5 , 1, 4 , 5],[ 8 , 3 , 2, 9 , 1]] ; 5 by 2

    IDL> q = where (mat le 2 , nq) ; find all elements; in mat that are print , q ; indices of the array2 7 9

    IDL> print , mat[q] ; values of the array1 2 1

    IDL> print , nq ; optional, but good3 ; to check...

    also: eq , ge , gt , lt

  • 8/2/2019 IDL_Intro

    36/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Array SizeIDL> mat = [[ 3 , 5 , 1, 4 , 5],[ 8 , 3 , 2, 9 , 1]] ; 5 by 2

    IDL> print , n_elements (mat[ 0,*]) ; vector2

    IDL> print , n_elements (mat) ; total number for10 ; multi-dimensional

    IDL> print , size (mat) ; more information2 5 2 2 10

    # dimensions

    elements inx dimension

    1 st 2nd data type total elements

  • 8/2/2019 IDL_Intro

    37/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Array Calculations

    IDL> x = findgen (n+ 1)* 2*!pi /n ; x = [0,2 ] in; steps of 2 /100

    IDL> n = 100 ; declare num

    IDL> y = cos (x) ; element-by-element; assignment

    Note: !pi is a system variable

    No looping necessarycleaner looking codefaster execution

    IDL> y = print , y[ 0: 5]1.00000 0.998027 0.992115 0.982287 0.968583 0.951057

    Also: system structures such as !p , !x , !y , etc.e.g. !p.multi = [ 0, ncol , nrow , nz , order ]

  • 8/2/2019 IDL_Intro

    38/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Array CalculationsUsing C

    #define N 101int n;

    float pi= 3.1415926 ;float x[N], y[N];

    dx = 2*pi/N;for (n= 0;n

  • 8/2/2019 IDL_Intro

    39/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Array CalculationsUsing C

    #define N 101int n;

    float pi= 3.1415926 ;float x[N], y[N];

    dx = 2*pi/N;for (n= 0;n

  • 8/2/2019 IDL_Intro

    40/61

  • 8/2/2019 IDL_Intro

    41/61

  • 8/2/2019 IDL_Intro

    42/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Executive Commands

    file loop.pro

    n = 101dx = 2*!pi /nfor i= 0,n- 1 do begin

    x[n] = n*dxy[n] = cos (x[n])

    endforend

    IDL> .run loop.proIDL>

    to execute commands in loop.pro

    Other executive commands: (can only be used at the IDL command line ).compile , .edit ,.run , .go , .continue , .out , .return , .run , .skip , .step , .stepover , .trace

    (or Dot Commands)

  • 8/2/2019 IDL_Intro

    43/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Reading/Writing Files in IDL

    Read in data from file:

    openr , readf

    Interactively with the DE

    Write data to a file:

    openw , writef

  • 8/2/2019 IDL_Intro

    44/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Reading Data from a FileIDL> filename = 'ssn.dat' ; declare filenameIDL> nlines = 3072 ; cheat...IDL> record = {time: 0.0 , ssn: 0.0 } ; declare structIDL> data = replicate (record, nlines) ; make array of structsIDL> openr , ifile, filename, /get_lun ; open file for readIDL> readf , ifile, data ; read block of dataIDL> free_lun , ifile ; clean up...IDL> print , data[ 0: 9]. time ; access range of data

    1749.05 1749.13 1749.21 1749.29 1749.38 1749.461749.54 1749.63 1749.71 1749.80

    Note: must know number of lines ...can do with spawn , 'wc -l ' +filename

    or by looping while not eof (ifile) do beginreadf , ifile, record

    data[i++] = record

    endwhile

  • 8/2/2019 IDL_Intro

    45/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Interactively with the DE

    Reading Data from a File

  • 8/2/2019 IDL_Intro

    46/61

  • 8/2/2019 IDL_Intro

    47/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Plotting Features in IDL

    IDL has built-in 2D and 3D plotting capabilities

    plot , [X,] Y [, /ISOTROPIC ][, /XLOG][, /YLOG]

    IDL> plot , data.time , data.ssn

    IDL

  • 8/2/2019 IDL_Intro

    48/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Plotting Features in IDL

    Many options for making fancier plots...

    IDL> plot , data.time , data.ssn , $

    xrange= [ 1980 , 2005 ], xtitle= 'Year' , $ytitle= 'Monthly Observed Sun Spot Number'

    IDL

  • 8/2/2019 IDL_Intro

    49/61

    Minicourse: Introduction to IDL February 2005

    IDL

    IDL> sm_data = smooth (data.ssn, 6)

    IDL> oplot , data.time , sm_data, $

    color= 200 , thick= 4

    ; smooth the data with a 6 point (month); boxcar average

    ; overplot the smoothed data in a; different color and thicker line

    IDL> loadct , 39

    Plottin g Features in IDL

    IDL

  • 8/2/2019 IDL_Intro

    50/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Spectral Analysis

    IDL> H = fft ( data.ssn ) ; use the built-in fftIDL> ndat = n_elements (data.ssn ) ; number of data pointsIDL> q = indgen (ndat/ 2- 1)+ 1 ; negative frequenciesIDL> p = ndat - q ; positive frequencie s

    IDL> PSD = fltarr (ndat/ 2+1) ; declare PSD holderIDL> PSD[ 0] = abs (H[ 0])^ 2 ; load theIDL> PSD[q] = abs (H[q])^ 2abs (H[p])^ 2 ;IDL> PSD[ndat/2] = abs (H[ndat/2])^ 2 ;

    IDL> per = ndat * 1. / 12 / findgen (ndat/ 2+1) ; in years

    IDL> plot , per, PSD, xrange= [ 0, 20 ], /nodata , ; init plotxtitle= 'Period (years)'

    IDL> oplot , per, PSD, color= 250 , thick=2 ; plot PSD

    Estimate the power spectrum (see Numerical Recipes )

    IDL

  • 8/2/2019 IDL_Intro

    51/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Correlation Analysis

    Discrete Math Functions

    Curve and Surface Fitting

    Differentiation and Integration

    Eigenvalues and EigenvectorsGridding and Interpolation

    Optimization

    Linear Systems including LAPACK Routines

    Nonlinear Equations

    Statistics and Probability

    Multivariate Analysis

    IDL M C li d Pl

  • 8/2/2019 IDL_Intro

    52/61

    Minicourse: Introduction to IDL February 2005

    IDL More Complicated Plots

  • 8/2/2019 IDL_Intro

    53/61

  • 8/2/2019 IDL_Intro

    54/61

  • 8/2/2019 IDL_Intro

    55/61

    IDL

  • 8/2/2019 IDL_Intro

    56/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Mapping Features in IDLIDL> map_set , 70 , -75 , /orthographic , /grid , $IDL> /continent , /noborder, $IDL> e_continents= {fill: 1 , color: 150 }, $IDL> e_horizon= {fill: 1 , color: 50 }

    orthographic map projection

    rotated view

    colored

  • 8/2/2019 IDL_Intro

    57/61

    IDL

  • 8/2/2019 IDL_Intro

    58/61

    Minicourse: Introduction to IDL February 2005

    IDL

    Data on Maps

  • 8/2/2019 IDL_Intro

    59/61

  • 8/2/2019 IDL_Intro

    60/61

  • 8/2/2019 IDL_Intro

    61/61