740 likes | 1.12k Views
Interactive Data Language (IDL). Margit Haberreiter ( haberreiter@lasp.colorado.edu ) LASP, room 135 Acknowledgement: Marty Snow. Outline. What is IDL? Basic Syntax Functions, Procedures, and Library Reading & Writing Plotting & Printing Image Processing And Stuff…. Pro test
E N D
Interactive Data Language (IDL) Margit Haberreiter (haberreiter@lasp.colorado.edu) LASP, room 135 Acknowledgement: Marty Snow
Outline • What is IDL? • Basic Syntax • Functions, Procedures, and Library • Reading & Writing • Plotting & Printing • Image Processing • And Stuff…. IDL for REU students Margit Haberreiter
Pro test Save it as test.pro .compile filename filename IDL for REU students Margit Haberreiter
Interactive Data Language • IDL was born here at LASP • Very popular among scientists: • Handles arrays of data easily (spectra, time series, images, etc.) • Easy to learn • Portable • Large existing user library • Online Help – type “?” at prompt • Type help at prompt for contents of variables • Interactive or Compiled, your choice! IDL for REU students Margit Haberreiter
Data Types • Integer (and Long Integer) • Floating Point (and Double Precision) • String • Structure • Pointer • Also, complex, unsigned integers, etc. IDL is “dynamically typed” which means a variable can be defined as one type, and then be changed to another within the same program unit. IDL for REU students Margit Haberreiter
Scalars, Vectors, ArraysProblem 1 • Scalar • 42 a=5 & b=0. & c=0.d • ‘help!’ • Vector • [1,2,3,5] • [‘red’,’blue’,’happy’,’Burma Shave’] • Array • Up to 8 dimensions Definition of arrays arr1=intarr(3) arr2=fltarr(2,a) arr3=dblarr(a,b,c,d…) arr4=strarr(11) IDL for REU students Margit Haberreiter
“tag A, B, C” Structures - Problem 2 • Can hold mixed data types • S={day:0,time:0d0,label:’ ‘} • S.day=5 • S.label=‘happy time’ • Can make arrays of structures • S2=replicate({time:0d0,label:’’,values:fltarr(100)},n_obs) • S2[4].values=findgen(100) • Names or Numbers • S2.time is equivalent to S2.(0) IDL for REU students Margit Haberreiter
Rules of the Road • Always start counting at zero. • For help at any time, type help. • Case Insensitive (EXC: filenames). • Odd is true, Even or Zero is false. • An array of length 1 is not the same as a scalar. But you can reform it. IDL for REU students Margit Haberreiter
Squiggly or Square? • Subscripting variables -- [] square • Parameters of a function – () round • Definition of a structure – {} curly IDL is lax about enforcing these rules, but it is to your benefit to follow them. Example: a=exp[5] ;6th element of variable “exp” a=exp(5) ;e^5 You might have named a variable the same name as a library function! IDL for REU students Margit Haberreiter
Subscripting - Problem 3 • z=a[5] • z=a[0:2] • z=a[3:*] • mask=[5,6,7,8,9] & z=a[mask] • Range=where(mask gt 2 and mask le 4) • Mask2=mask(range) a=indgen(10) z=a[5] z=a[0:2] z=a[3:*] mask=[5,6,7,8,9] & z=a[mask] Range=where((mask gt 6) and (mask le 8)) Mask2=mask(range) IDL for REU students Margit Haberreiter
The Size of Arrays • Size(x) • n_elements returns the number of elements in an array (vector, scalar). • An undefined variable has zero elements. • Often takes the place of size(). • Used to check if user has supplied parameters • Used to index loops. IDL for REU students Margit Haberreiter
Basic IDL SyntaxProblem 4 a=‘Hello World’ print,a ;This is a comment a=5;dynamic data types b=4.5*findgen(100) z=a+$ ;continuation sqrt(b) IDL for REU students Margit Haberreiter
Arithmetic + - * / ^ mod Min/Max < (a<b is the lesser) > (a>b is the greater) Matrix Multiply # ## Boolean Logic and not or xor Relational Operators eq (a=b) ge (ab) gt (ab) le (ab) lt (ab) ne (ab) Operators Note that you can’t use any of these letter combinations as variable names! IDL for REU students Margit Haberreiter
FOR loop for variable=init,limit[,increment] do statement for i=0,100 do print,i,vector[i] for variable=init,limit[,increment] do begin statements endfor Loops in IDL execute slowly (relatively speaking), so try to use something else, like where. IDL for REU students Margit Haberreiter
As IF…. Problem 5 if expression then statement if expression then statement else statement2 if expression then begin statements endif else begin statements endelse • for x=0,5 do begin • if x gt 5 then begin • z=dblarr(10,10) • z=z+.1 • z[3,2]=1. • endif else begin • z=dblarr(10) • z[x]=!pi • endelse • endfor Good programming style avoid hardcoded numbers in procedures IDL for REU students Margit Haberreiter
meanWHILE while expression do statement while expression do begin statements endwhile IDL for REU students Margit Haberreiter
case expression of expression1: statement expression2: begin statements end expression3: statement else: statement endcase Example: x=3 CASE x OF 1: PRINT, 'one' 2: PRINT, 'two' 3: PRINT, 'three' 4: PRINT, 'four' ENDCASE just in CASE IDL for REU students Margit Haberreiter
Other Flow Control Statements • STOP – excellent way to debug (.con or .c to continue) • GOTO • goto,label • label: • SWITCH • CONTINUE • BREAK • MESSAGE – stops program and prints message • IDL Help: This example illustrates how, unlike CASE, SWITCH executes the first matching statement and any following statements in the SWITCH block: • x=2 • SWITCH x OF • 1: PRINT, 'one' • 2: PRINT, 'two' • 3: PRINT, 'three' • 4: PRINT, 'four' • ENDSWITCH IDL for REU students Margit Haberreiter
Procedures and Functions • A list of IDL statements that could have been entered at the command prompt. • Contained in separate file or files. • Compiled automatically if files are properly named. Filename matches procedure with a .pro extension. USE LOWER CASE! • All procedures compiled from start of file but stops when procedure name matches file name. • Environmental variables define directories that IDL will automatically search. USER LIBRARY IDL for REU students Margit Haberreiter
Compile and Run • Type procedure name at command prompt to run it. IDL will search for an appropriately named file and automatically compile and run the procedure. • .r or .compile will compile the procedure, but not run it. • IDL will compile all procedures in file until it hits the one matching the file name, then it will stop. Put the named procedure last in the file. IDL for REU students Margit Haberreiter
Simple Procedure pro simpleprocedure,a,b,c c=a+b end To run it, type: simpleprocedure,var1,var2,result print,result IDL for REU students Margit Haberreiter
Simple Function function eve,number if number mod 2 eq 0 then return,0 return,1 end To run it, type: result=eve(var) print,eve(var) print, ‘The result is ‘, result IDL for REU students Margit Haberreiter
What’s the difference? Not much. Use a function to get a value that you’ll use right away (like an expression). EXAMPLE: simpleprocedure,eve(0),eve(5),result Use a procedure when you return multiple results and don’t want to just wrap them together in a structure or array. IDL for REU students Margit Haberreiter
Parameters and KeywordsProblem 7 pro proc2,input1,input2,result,doplot=doplot if n_elements(doplot) eq 0 then doplot=0 result=input1*sin(input2/!pi) if doplot gt 0 then begin set_plot,'win' !P.CHARSIZE=3. ; system setting !P.MULTI=[0,1,3] ; system setting plot,input2,result,psym=-4 plot,input1 plot,sin(input2/!pi) endif end Order of Parameters is critical. Order of Keywords is irrelevant. Keywords can normally be abbreviated xtit instead of xtitle xs instead of xstyle IDL for REU students Margit Haberreiter
Variables: Global or Local?Problem 8 Variables are local….sort of (Attention!!) A procedure can modify any of its parameters, which will change the variable in the calling procedure. pro proc1 v=0 print,v proc3,v print,v end pro proc3,var2 var2=var2+1 end IDL for REU students Margit Haberreiter
Error and stop • If the IDL interpreter hits a STOP or encounters an error, flow halts at the local level. You can type ‘help’ to view contents of variables and which line of code you’re at. • To return to the top level – retall(return all the way) • To clear everything, type .f (full reset) or .reset -> Attention: all variables deleted IDL for REU students Margit Haberreiter
Reading & Writing & Saving • openr,openw,openu (Read, Write, Update) • readf,readu (Formatted, Unformatted) • close or free_lun • IDL save files: • save,file=‘data.idl’,var1,var2 • restore,’data.idl’ openr,lu,’file.txt’,/get_lun while not eof(lu) do begin readf,lu,data endwhile free_lun,lu IDL for REU students Margit Haberreiter
Slight Gotcha… • pro wont_work • openr,lu,’file.txt’,/get_lun • data=fltarr(100) • for i=0,n_elements(data)-1 do begin • readf,lu,data[i] • endfor • free_lun,lu • end IDL passes by value, not by reference, so this won’t work. IDL for REU students Margit Haberreiter
Slight Gotcha… • pro will_work • openr,lu,’file.txt’,/get_lun • data=fltarr(100) • i=0 • while not eof(lu) do begin • readf,lu,temporary • data[i]=temporary • i=i+1; counter • endwhile • free_lun,lu • data=data[0:i-1] • end IDL passes by value, so this will make IDL happy. IDL for REU students Margit Haberreiter
Task: Read data from fileProblem 9 pro willwork,filename,data openr,lu,filename,/get_lun ; get_lun: sets file unit number data=fltarr(2,1000) i=0 while not eof(lu) do begin readf,lu,temporary1,temporary2 data[0,i]=temporary1 data[1,i]=temporary2 i=i+1 ; counter endwhile free_lun,lu data=data[*,0:i-1] set_plot,'win' !P.MULTI=0 plot,data(0,*),data(1,*),title='Data from file' end Run the procedure Add a plot using a parameter (see slide 22) IDL for REU students Margit Haberreiter
Homework 1—LISIRD Data • Go to http://lasp.colorado.edu/lisird and retrieve solar Lyman alpha (121.5 nm) data for at least two missions. • Save data as text file. • Write an IDL procedure to read data. IDL for REU students Margit Haberreiter
More on plotting…Basic IDL Plotting Procedures • Line plots • plot • oplot • histogram • Contour plots • contour • Surface plots • surface • shade_surf IDL for REU students Margit Haberreiter
Line ‘em up Problem 10 • X=FINDGEN(360) • Y=SIN(X*!DTOR) • PLOT, X, Y, XRANGE=[0,360], /XSTYLE, XTIT=‘X’, $YTIT=‘Y’, TIT=‘Sample Line Plot • Z=COS(X*!DTOR) • OPLOT, X, Z, LINESTYLE=2 IDL for REU students Margit Haberreiter
Adding text to plot • X=FINDGEN(360) • Y=SIN(X*!DTOR) • PLOT, X, Y, XRANGE=[0,360], /XSTYLE, XTIT='X', $YTIT='Y', TIT='Sample Line Plot' • Z=COS(X*!DTOR) • OPLOT, X, Z, LINES=2 • XYOUTS, 100, 0, 'cos(x)' • XYOUTS, 190, 0, 'sin(x)' IDL for REU students Margit Haberreiter
Histograms • Y=RANDOMN(SEED, 100, /NORMAL) • H=HISTOGRAM(Y, BINSIZE=0.2, LOCATIONS=L) • PLOT, L, H, PSYM=10 ; histogram mode IDL for REU students Margit Haberreiter
Graphics Keywords • change the style of the data • add a title • manipulate an axis/change tick marks • change the format of text • change coordinate systems • etc. IDL for REU students Margit Haberreiter
Lines LINESTYLE={0,1,2,3,4,5} THICK: change the thickness of the line (default is 1.0) Symbols PSYM={1,2,3,…10} SYMSIZE: change the size of the symbol USERSYM: procedure to define PSYM=8 PSYM= -{1,2,3,…8}: solid line connecting the symbols N.B.: PSYM=3 (period) does NOT show up well in postscript plots and does not scale nicely with symsize. Better to use PSYM=1, SYMSIZE=.3 (or similar) Negative value of “psym” creates line+symbols Value Meaning 1 Plus sign (+) 2 Asterisk (*) 3 Period (.) 4 Diamond 5 Triangle 6 Square 7 X 8 User defined 9 Undefined Value Meaning 10 Histogram mode 0 Solid 1 Dotted 2 Dash 3 Dash Dot 4 Dash Dot Dot 5 Long Dashes Styles and Symbols Keywords IDL for REU students Margit Haberreiter
Titletown USA Keywords • TITLE: place a string of text at the top of a plot • TITLE=‘This is my title’ • TITLE_STRING=‘This is my title’TITLE=TITLE_STRING • SUBTITLE: place a title below the x-axis • [XYZ]title places title on x, y, or z axis IDL for REU students Margit Haberreiter
Axes & Tick Mark Keywords • To manipulate individual axes: • Set the range: • [XYZ]RANGE=[min, max] • Change the axis style • [XYZ]STYLE=number • Multiple effects can be achieved by adding values together • Label an axis: • [XYZ]TITLE=string IDL for REU students Margit Haberreiter
Axes & Ticks Keywords 2 • To manipulate individual axes (cont.): • Change the axis thickness • [XYZ]THICK=number (default is 1.0) • Use axis procedure to draw axes with different scales • X=FINDGEN(360) • Y=SIN(X*!DTOR) ; system variable π/180 • PLOT, X, Y, XRANGE=[0,360], XSTYLE=9, XTIT='X (Degrees)' • AXIS, XAXIS=1, XRANGE=[0,2*!PI], XSTYLE=1, XTITLE='(Radians)' IDL for REU students Margit Haberreiter
Clipped title IDL for REU students Margit Haberreiter
Fixed clipping… plot,x,y,xstyle=9,xtit='X (degrees)',ymargin=[4,4] IDL for REU students Margit Haberreiter
Text Formatting Keywords • To change the size of text • CHARSIZE=number (default is 1.0) • To change the thickness of the text • CHARTHICK=number (default is 1.0) • Or set system variable: • !P.Charsize=3 IDL for REU students Margit Haberreiter
Coordinate systems • IDL has 3 coordinate systems • DATA (default) • Uses the range of values of the data • DEVICE • Uses the device (X window or Postscript device) coordinates (i.e. pixels) • NORMAL • Normalized coordinates from 0 to 1 over the 3 axes IDL for REU students Margit Haberreiter
Adding color • Color tables • LOADCT[, number] • XLOADCT (widget) • Graphics keywords: • BACKGROUND=number • COLOR=number • Number from 0 to 255, corresponding to the color table • color=fsc_color(‘red’) ;using library routine • On Mac, Windows: DEVICE, DECOMPOSED=0 IDL for REU students Margit Haberreiter
Contour • X=FINDGEN(360) • Z=SIN(X*!DTOR)#COS(2*X*!DTOR) • CONTOUR, Z, NLEVELS=6, XRANGE=[0, 360], $YRANGE=[0, 360], /XSTYLE, /YSTYLE, XTIT='X', YTIT='Y', $TIT=‘Sample Contour Plot’ IDL for REU students Margit Haberreiter
Surface plots (shaded surf) • SHADE_SURF, Z, XRANGE=[0, 360], $YRANGE=[0, 360], /XSTYLE, /YSTYLE, XTIT='X', $YTIT='Y', TIT='Sample Surface Plot 2' IDL for REU students Margit Haberreiter
Colored surface loadct,3 shade_surf,z,tit='Sample Surface Plot 3',shades=bytscl(z+1) Bytscl translates the values of the array into a byte array from 0 to 255 IDL for REU students Margit Haberreiter
Surface plots 2 (wire mesh) • SURFACE, Z, XRANGE=[0, 360], $YRANGE=[0, 360], /XSTYLE, /YSTYLE, XTIT='X', $YTIT='Y', TIT='Sample Surface Plot 2' IDL for REU students Margit Haberreiter
Changing appearance of mesh z2=rebin(z,180,180) surface,z2 z3=rebin(z,90,90) surface,z3 The possibilities are endless! IDL for REU students Margit Haberreiter