Introduction to IDL® A Kiewit Minicourse Simon Shepherd Thayer School of Engineering [email protected] http://thayer.dartmouth.edu/~simon IDL Agenda What is IDL? How to run IDL Simple IDL Commands Arrays in IDL Data I/O Basic Plotting Built-in Analysis Functions Mapping Minicourse: Introduction to IDL Example February 2005 IDL Scientific Computing Today Traditional programming languages: Basic, Pascal, FORTRAN, C, C++, Java Scripting languages: sh, csh, bash, Tcl, Perl Numeric/graphics: IDL, Matlab, Octave Symbolic: Maple, Mathematica, MathCad Dataflow: LabView, Simulink, AVS, DX, OpenDX Minicourse: Introduction to IDL February 2005 IDL What is IDL? Interactive Data Language Minicourse: Introduction to IDL February 2005 IDL What is IDL? Interactive Data Language Proprietary software distributed by Research Systems, Inc. of Boulder, CO now a division of Kodak. Minicourse: Introduction to IDL February 2005 IDL What is IDL? Interactive Data Language Proprietary software distributed by Research Systems, Inc. of Boulder, 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. Minicourse: Introduction to IDL February 2005 IDL What is IDL? Interactive Data Language Proprietary software distributed by Research Systems, Inc. of Boulder, 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. Minicourse: Introduction to IDL February 2005 IDL What is IDL? Interactive Data Language Proprietary software distributed by Research Systems, Inc. of Boulder, 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. Platform Independent: Unix, linux, Windows, Macintosh Minicourse: Introduction to IDL February 2005 IDL What is IDL? Interactive Data Language Offers all the power, adaptability, and programmability of high level languages like FORTRAN, C, and C++. Minicourse: Introduction to IDL February 2005 IDL What is IDL? Interactive Data Language Offers all the power, adaptability, and programmability of high level languages like FORTRAN, C, and C++. But it adds two capabilities which are essential for modern data analysis: interactivity and graphics display. Minicourse: Introduction to IDL February 2005 IDL What is IDL? User-written .pro files User-written functions in C or Fortran Numerical and graphical .pro files User interface Core numerics and graphics Minicourse: Introduction to IDL February 2005 IDL Ways to Run IDL Interactively Development Environment (DE) Command Line Batch mode Minicourse: 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 line No multiline statements such as Minicourse: Introduction to IDL begin ... end February 2005 IDL Ways to Run IDL Command Line prompt> idl IDL> command IDL> each command entered is interpreted and executed and the prompt is returned Minicourse: Introduction to IDL February 2005 IDL Ways to Run IDL Development Environment prompt> idlde or select from desktop, panel, toolbar, or launcher Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) menu Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) toolbar Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) editor Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) project Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) output Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) variables Minicourse: Introduction to IDL February 2005 IDL Graphical User Interface (DE) command line Minicourse: Introduction to IDL February 2005 IDL Using IDL as a calculator IDL> a = 3 ; assignment statement ; semicolon is used for comments IDL> print, a + 4 ; builtin print command 7 IDL> print, a + 4.; variables dynamically typed 7.00000 ; take on highest precision IDL> print, a + 4D; double precision 7.0000000 IDL> print, a * 1e9 3.00000e09 ; scientific notation Note: variable names are case insensitive (A = a) Minicourse: Introduction to IDL February 2005 IDL Data Types 1 Byte nB 8-bit unsigned 2 Integer n 16-bit signed 3 Longword nL 32-bit signed 4 Floating Point n.n 32-bit (+1038) 5 Double-Precision n.nD 64-bit (+1038) 6 Complex COMPLEX(n.n,n.n) real/imaginary pairs 7 String 'ssss' 0-32k in length 8 Struct 9 D-P Complex 10 Pointer Pointer 11 ObjRef Object Reference 12 Unsigned Int nU 16-bit unsigned 13 Unsigned Long nUL 32-bit unsigned 14 64-bit Long LONG64(n) 64-bit signed longword 15 64-bit Unsigned Long ULONG64(n) 64-bit unsigned longword Minicourse: Introduction to IDL User-defined Structure DCOMPLEX(n.n,n.n) real/imaginary pairs double-precision February 2005 IDL Unary and Compound Operators IDL> a = 3 ; assignment statement IDL> a += 5 IDL> a = a + 5 IDL> print, a 8 ; C compound operators ; equivalent but rather tedious IDL> a++ IDL> print, a 8 ; C unary operators IDL> print, a 8 IDL> print, a 7 ; careful... Minicourse: Introduction to IDL February 2005 IDL Arrays in IDL Array-oriented language operations are performed on arrays Any data type (but not mixed) Dynamically sized (and resized) Minicourse: Introduction to IDL February 2005 IDL Vectors in IDL IDL> vec = [3,5,1,4,5] ; assignment statement IDL> print, vec ; 5 element vector 3 5 1 4 5 vec[0] vec[1] vec[2] vec[3] vec[4] IDL> vec[3] = 9 ; square brackets to access IDL> print, vec[3] 9 ; elements of array Minicourse: Introduction to IDL February 2005 IDL Matrices in IDL IDL> mat = [[3,5,1,4,5],$ ; $ is line continuation IDL> [8,3,2,9,1]] IDL> print, mat ; 5column 2row array 3 5 1 4 5 8 3 2 9 1 IDL> help, mat ; useful command... MAT INT = Array[5, 2] IDL> print, mat[3,1] 9 ; access elements of matrix column row Minicourse: Introduction to IDL February 2005 IDL Matrices in IDL IDL> print, mat[3,1] 9 column row IDL> print, mat[5*1+3] 9 column row format... ; access elements of matrix ; equivalent... M by N M M Minicourse: Introduction to IDL February 2005 IDL Matrices in IDL column 0 1 2 3 4 M by N 5 by 2 row 0 3 5 1 4 5 1 8 3 2 9 1 ; nth row: n*M mth column: m 1*5 + 3 = 8 mat[3,1] = mat[5*1+3] Minicourse: Introduction to IDL February 2005 IDL Declaring Matrices IDL> mat = [[3,5,1,4,5],[8,3,2,9,1]] ; explicitly IDL> 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] Minicourse: Introduction to IDL 40 + 16 + 6 February 2005 IDL Filling Up Arrays IDL> darr = dblarr(8,5,2) ; all zeros IDL> iarr = indgen(8,5,2) ; sequential IDL> print, iarr 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 also: bindgen(), lindgen(), findgen(), dindgen(), cindgen(), dcindgen(), uindgen(), ulindgen() sindgen() Minicourse: Introduction to IDL February 2005 IDL Accessing Array Elements IDL> mat = [[3,5,1,4,5],[8,3,2,9,1]] ; 5 by 2 IDL> print, mat[3,*] 4 9 ; print column m=3 IDL> print, mat[2:4,0] 1 4 5 ; print columns 24 ; of row 0 IDL> q = [3,2,0] ; another intarr IDL> print, mat[q,0] 4 1 3 ; index with another ; array or indices... Minicourse: Introduction to IDL February 2005 IDL Searching Arrays IDL> 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 <= 2 IDL> print, q ; indices of the array 2 7 9 IDL> print, mat[q] 1 2 1 ; values of the array IDL> print, nq 3 ; optional, but good ; to check... also: eq, ge, gt, lt Minicourse: Introduction to IDL February 2005 IDL Array Size IDL> mat = [[3,5,1,4,5],[8,3,2,9,1]] ; 5 by 2 IDL> print, n_elements(mat[0,*]) 2 IDL> print, n_elements(mat) 10 ; vector ; total number for ; multidimensional IDL> print, size(mat) ; more information 2 5 2 2 10 # dimensions 1st 2nd data type total elements elements in x dimension Minicourse: Introduction to IDL February 2005 IDL Array Calculations IDL> n = 100 ; declare num IDL> x = findgen(n+1)*2*!pi/n ; x = [0,2π] in ; steps of 2π/100 IDL> y = cos(x) ; elementbyelement ; assignment IDL> y = print, y[0:5] 1.00000 0.998027 0.992115 0.982287 0.968583 0.951057 No looping necessary cleaner looking code faster execution Note: !pi is a system variable Also: system structures such as !p, !x, !y, etc. Minicourse: Introduction to IDL e.g. !p.multi = [0,ncol,nrow,nz,order] February 2005 IDL Array Calculations Using C #define N 101 int n; float pi=3.1415926; float x[N], y[N]; dx = 2*pi/N; for(n=0;n<N;n++) { x[n] = n*dx; y[n] = cos(x[n]); } Minicourse: Introduction to IDL February 2005 IDL Array Calculations Using C Using IDL (looping) #define N 101 int n; float pi=3.1415926; float x[N], y[N]; n = 101 dx = 2*!pi/n for i=0,n1 do begin x[n] = n*dx y[n] = cos(x[n]) endfor end dx = 2*pi/N; for(n=0;n<N;n++) { x[n] = n*dx; y[n] = cos(x[n]); } Minicourse: Introduction to IDL February 2005 IDL Array Calculations Using C Using IDL (looping) #define N 101 int n; float pi=3.1415926; float x[N], y[N]; n = 101 dx = 2*!pi/n for i=0,n1 do begin x[n] = n*dx y[n] = cos(x[n]) endfor end dx = 2*pi/N; for(n=0;n<N;n++) { x[n] = n*dx; y[n] = cos(x[n]); } Minicourse: Introduction to IDL Using IDL (array) n = 101 x = findgen(n)*2*!pi/(n1) y = cos(x) February 2005 IDL Array Calculations n times slower than C Using C Using IDL (looping) #define N 101 int n; float pi=3.1415926; float x[N], y[N]; n = 101 dx = 2*!pi/n for i=0,n1 do begin x[n] = n*dx y[n] = cos(x[n]) endfor end dx = 2*pi/N; for(n=0;n<N;n++) { x[n] = n*dx; y[n] = cos(x[n]); } Matlab: 23 looping 21 array Minicourse: Introduction to IDL Using IDL (array) 58 8 n = 101 x = findgen(n)*2*!pi/(n1) y = cos(x) February 2005 IDL Executive Commands (or Dot Commands) file loop.pro n = 101 dx = 2*!pi/n for i=0,n1 do begin x[n] = n*dx y[n] = cos(x[n]) endfor end to execute commands in loop.pro IDL> .run loop.pro IDL> Other executive commands: (can only be used at the IDL command line) .compile, .edit, .run, .go, .continue, .out, .return, .run, .skip, .step, .stepover, .trace 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 Minicourse: Introduction to IDL February 2005 IDL Reading Data from a File IDL> filename = 'ssn.dat' IDL> nlines = 3072 IDL> record = {time:0.0, ssn:0.0} IDL> data = replicate(record, nlines) IDL> openr, ifile, filename, /get_lun IDL> readf, ifile, data IDL> free_lun, ifile IDL> print, data[0:9].time ; declare filename ; cheat... ; declare struct ; make array of structs ; open file for read ; read block of data ; clean up... ; access range of data 1749.05 1749.13 1749.21 1749.29 1749.38 1749.46 1749.54 1749.63 1749.71 1749.80 Note: must know number of lines ... can do with or by looping Minicourse: Introduction to IDL spawn, 'wc l '+filename while not eof(ifile) do begin readf, ifile, record data[i++] = record endwhile February 2005 IDL Reading Data from a File Interactively with the DE Minicourse: Introduction to IDL February 2005 IDL 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 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' Minicourse: Introduction to IDL February 2005 IDL Plotting Features in IDL IDL> sm_data = smooth(data.ssn, 6) ; smooth the data with a 6 point (month) ; boxcar average IDL> loadct, 39 IDL> oplot, data.time, sm_data, $ color=200, thick=4 ; overplot the smoothed data in a ; different color and thicker line Minicourse: Introduction to IDL February 2005 IDL Spectral Analysis Estimate the power spectrum (see Numerical Recipes) IDL> H = fft(data.ssn) ; use the builtin fft IDL> ndat = n_elements(data.ssn) IDL> q = indgen(ndat/21)+1 IDL> p = ndat q ; number of data points ; negative frequencies ; positive frequencies IDL> PSD = fltarr(ndat/2+1) ; declare PSD holder IDL> PSD[0] = abs(H[0])^2 ; load the IDL> 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, xtitle='Period (years)' ; init plot IDL> oplot, per, PSD, color=250, thick=2 ; plot PSD Minicourse: Introduction to IDL February 2005 IDL Discrete Math Functions Correlation Analysis Curve and Surface Fitting Differentiation and Integration Eigenvalues and Eigenvectors Gridding and Interpolation Linear Systems including LAPACK Routines Multivariate Analysis Nonlinear Equations Optimization Statistics and Probability Minicourse: Introduction to IDL February 2005 IDL More Complicated Plots Minicourse: Introduction to IDL February 2005 IDL More Complicated Plots Minicourse: Introduction to IDL February 2005 IDL Mapping Features in IDL IDL has built-in mapping capabilities IDL> map_set, /orthographic,/grid, /continent,$ IDL> /noborder orthographic map projection basic view Minicourse: Introduction to IDL February 2005 IDL Mapping Features in IDL IDL> map_set, 70, 75, /orthographic,/grid, $ IDL> /continent,/noborder orthographic map projection rotated view Minicourse: Introduction to IDL February 2005 IDL Mapping Features in IDL IDL> 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 Minicourse: Introduction to IDL February 2005 IDL Map Projections in IDL stereographic Lambert's Minicourse: Introduction to IDL gnomonic azimuthal Hammer-Aitoff sinusoidal February 2005 IDL Data on Maps Minicourse: Introduction to IDL February 2005 IDL Further Topics to Explore Writing IDL Programs Calling external functions (C and FORTRAN) from IDL IDL Widgets Plotting 2D and 3D Object Graphics Debugging Data Structures Minicourse: Introduction to IDL February 2005 IDL How to Get IDL Help Online: prompt> idlhelp Help menu in the DE Internet: http://www.dfanning.com/documents/tips.html Listserve: IDL users at Dartmouth IDL[email protected] Books: Minicourse: Introduction to IDL February 2005 IDL Where is IDL at Dartmouth? Download onto your own computer: Minicourse: Introduction to IDL ~25 licenses February 2005

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement