Interactive Data Language (IDL) Margit Haberreiter ( LASP, room 135 Acknowledgement: Marty Snow

Download Interactive Data Language (IDL) Margit Haberreiter ( LASP, room 135 Acknowledgement: Marty Snow

Post on 27-Dec-2015




0 download

Embed Size (px)


<ul><li><p>Interactive Data Language (IDL)Margit Haberreiter (, room 135</p><p>Acknowledgement: Marty Snow</p></li><li><p>OutlineWhat is IDL?Basic SyntaxFunctions, Procedures, and LibraryReading &amp; WritingPlotting &amp; PrintingImage ProcessingAnd Stuff.</p></li><li><p>Pro testSave it as filename</p><p>filename</p></li><li><p>Interactive Data LanguageIDL was born here at LASPVery popular among scientists:Handles arrays of data easily (spectra, time series, images, etc.)Easy to learnPortable Large existing user library Online Help type ? at promptType help at prompt for contents of variablesInteractive or Compiled, your choice!</p></li><li><p>Data TypesInteger (and Long Integer)Floating Point (and Double Precision)StringStructurePointerAlso, 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. </p></li><li><p>Scalars, Vectors, Arrays Problem 1Scalar 42a=5 &amp; b=0. &amp; c=0.dhelp!Vector [1,2,3,5][red,blue,happy,Burma Shave]ArrayUp to 8 dimensions</p><p>Definition of arraysarr1=intarr(3) arr2=fltarr(2,a) arr3=dblarr(a,b,c,d)arr4=strarr(11)</p></li><li><p>Structures - Problem 2Can hold mixed data typesS={day:0,time:0d0,label: } timeCan make arrays of structuresS2=replicate({time:0d0,label:,values:fltarr(100)},n_obs)S2[4].values=findgen(100)Names or NumbersS2.time is equivalent to S2.(0)</p></li><li><p>Rules of the RoadAlways 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.</p></li><li><p>Squiggly or Square?Subscripting variables -- [] squareParameters of a function () roundDefinition of a structure {} curlyIDL is lax about enforcing these rules, but it is to your benefit to follow them.</p><p>Example: a=exp[5] ;6th element of variable expa=exp(5) ;e^5</p><p>You might have named a variable the same name as a library function!</p></li><li><p>Subscripting - Problem 3z=a[5]z=a[0:2]z=a[3:*]mask=[5,6,7,8,9] &amp; 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] &amp; z=a[mask]Range=where((mask gt 6) and (mask le 8))Mask2=mask(range)</p></li><li><p>The Size of ArraysSize(x)n_elements returns the number of elements in an array (vector, scalar).An undefined variable has zero elements.</p><p>Often takes the place of size().Used to check if user has supplied parametersUsed to index loops.</p></li><li><p>Basic IDL Syntax Problem 4a=Hello World print,a ;This is a commenta=5;dynamic data typesb=4.5*findgen(100) z=a+$ ;continuationsqrt(b) </p></li><li><p>OperatorsArithmetic+ -*/^modMin/Max&lt; (a (a&gt;b is the greater)Matrix Multiply###Boolean LogicandnotorxorRelational Operatorseq (a=b)ge (ab) gt (ab)le (ab)lt (ab)ne (ab)Note that you cant use any of these letter combinations as variable names!</p></li><li><p>FOR loopfor variable=init,limit[,increment] do statementfor i=0,100 do print,i,vector[i]</p><p>for variable=init,limit[,increment] do beginstatementsendfor</p><p>Loops in IDL execute slowly (relatively speaking), so try to use something else, like where.</p></li><li><p>As IF. Problem 5if expression then statementif expression then statement else statement2</p><p>if expression then beginstatementsendif else beginstatementsendelsefor x=0,5 do beginif x gt 5 then beginz=dblarr(10,10)z=z+.1z[3,2]=1.endif else beginz=dblarr(10)z[x]=!piendelseendforGood programming style avoid hardcoded numbers in procedures</p></li><li><p>meanWHILEwhile expression do statement</p><p>while expression do beginstatementsendwhile</p></li><li><p>just in CASEcase expression ofexpression1: statementexpression2: beginstatementsendexpression3: statementelse: statementendcaseExample:x=3 CASE x OF 1: PRINT, 'one' 2: PRINT, 'two' 3: PRINT, 'three' 4: PRINT, 'four' ENDCASE </p></li><li><p>Other Flow Control StatementsSTOP excellent way to debug (.con or .c to continue)GOTO goto,labellabel:SWITCHCONTINUE BREAKMESSAGE stops program and prints message</p><p>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 </p></li><li><p>Procedures and FunctionsA 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 </p></li><li><p>Compile and RunType 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.</p></li><li><p>Simple Procedurepro simpleprocedure,a,b,cc=a+bendTo run it, type:</p><p>simpleprocedure,var1,var2,resultprint,result</p></li><li><p>Simple Functionfunction eve,numberif number mod 2 eq 0 then return,0return,1endTo run it, type:result=eve(var)print,eve(var)print, The result is , result</p></li><li><p>Whats the difference?Not much. </p><p>Use a function to get a value that youll use right away (like an expression).EXAMPLE: simpleprocedure,eve(0),eve(5),result</p><p>Use a procedure when you return multiple results and dont want to just wrap them together in a structure or array.</p></li><li><p>Parameters and Keywords Problem 7pro 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) endifendOrder of Parameters is critical.Order of Keywords is irrelevant.</p><p>Keywords can normally be abbreviatedxtit instead of xtitlexs instead of xstyle</p></li><li><p>Variables: Global or Local? Problem 8Variables are local.sort of (Attention!!)A procedure can modify any of its parameters, which will change the variable in the calling procedure.</p><p>pro proc1v=0print,vproc3,vprint,vendpro proc3,var2var2=var2+1end</p></li><li><p>Error and stopIf 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 youre at.To return to the top level retall (return all the way)To clear everything, type .f (full reset) or .reset -&gt; Attention: all variables deleted</p></li><li><p>Reading &amp; Writing &amp; Savingopenr,openw,openu (Read, Write, Update)readf,readu (Formatted, Unformatted)close or free_lunIDL save files:save,file=data.idl,var1,var2restore,data.idl</p><p>openr,lu,file.txt,/get_lunwhile not eof(lu) do beginreadf,lu,dataendwhilefree_lun,lu</p></li><li><p>Slight Gotchapro wont_workopenr,lu,file.txt,/get_lundata=fltarr(100)for i=0,n_elements(data)-1 do beginreadf,lu,data[i]endforfree_lun,luend</p><p>IDL passes by value, not by reference, so this wont work.</p></li><li><p>Slight Gotchapro will_workopenr,lu,file.txt,/get_lundata=fltarr(100)i=0while not eof(lu) do beginreadf,lu,temporarydata[i]=temporaryi=i+1; counterendwhilefree_lun,ludata=data[0:i-1]end</p><p>IDL passes by value, so this will make IDL happy.</p></li><li><p>Task: Read data from file Problem 9pro willwork,filename,dataopenr,lu,filename,/get_lun ; get_lun: sets file unit numberdata=fltarr(2,1000)i=0while not eof(lu) do begin readf,lu,temporary1,temporary2 data[0,i]=temporary1 data[1,i]=temporary2 i=i+1 ; counterendwhilefree_lun,ludata=data[*,0:i-1]set_plot,'win'!P.MULTI=0plot,data(0,*),data(1,*),title='Data from file'end</p><p>Run the procedureAdd a plot using a parameter (see slide 22)</p></li><li><p>Homework 1LISIRD DataGo to 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.</p></li><li><p>More on plottingBasic IDL Plotting ProceduresLine plotsplotoplothistogramContour plotscontourSurface plotssurfaceshade_surf</p></li><li><p>Line em up Problem 10X=FINDGEN(360)Y=SIN(X*!DTOR)PLOT, X, Y, XRANGE=[0,360], /XSTYLE, XTIT=X, $ YTIT=Y, TIT=Sample Line PlotZ=COS(X*!DTOR)OPLOT, X, Z, LINESTYLE=2</p></li><li><p>Adding text to plotX=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=2XYOUTS, 100, 0, 'cos(x)'XYOUTS, 190, 0, 'sin(x)'</p></li><li><p>HistogramsY=RANDOMN(SEED, 100, /NORMAL)H=HISTOGRAM(Y, BINSIZE=0.2, LOCATIONS=L)PLOT, L, H, PSYM=10 ; histogram mode</p></li><li><p>Graphics Keywordschange the style of the dataadd a titlemanipulate an axis/change tick markschange the format of textchange coordinate systemsetc.</p></li><li><p>Styles and Symbols KeywordsLinesLINESTYLE={0,1,2,3,4,5}THICK: change the thickness of the line (default is 1.0)SymbolsPSYM={1,2,3,10}SYMSIZE: change the size of the symbolUSERSYM: procedure to define PSYM=8PSYM= -{1,2,3,8}: solid line connecting the symbolsN.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</p><p>ValueMeaning1Plus sign (+)2Asterisk (*)3Period (.)4Diamond5Triangle6Square7X8User defined9Undefined10Histogram mode</p></li><li><p>Titletown USA KeywordsTITLE: place a string of text at the top of a plotTITLE=This is my titleTITLE_STRING=This is my title TITLE=TITLE_STRINGSUBTITLE: place a title below the x-axis[XYZ]title places title on x, y, or z axis</p></li><li><p>Axes &amp; Tick Mark KeywordsTo manipulate individual axes:Set the range:[XYZ]RANGE=[min, max]Change the axis style[XYZ]STYLE=numberMultiple effects can be achieved by adding values togetherLabel an axis:[XYZ]TITLE=string</p><p>ValueMeaning1Force exact axis range2Extend axis range4Suppress entire axis8Suppress box style axis16Inhibit setting y-axis minimum to zero</p></li><li><p>Axes &amp; Ticks Keywords 2To manipulate individual axes (cont.):Change the axis thickness[XYZ]THICK=number (default is 1.0)Use axis procedure to draw axes with different scalesX=FINDGEN(360)Y=SIN(X*!DTOR) ; system variable /180PLOT, X, Y, XRANGE=[0,360], XSTYLE=9, XTIT='X (Degrees)'AXIS, XAXIS=1, XRANGE=[0,2*!PI], XSTYLE=1, XTITLE='(Radians)'</p></li><li><p>Clipped title</p></li><li><p>Fixed clippingplot,x,y,xstyle=9,xtit='X (degrees)',ymargin=[4,4]</p></li><li><p>Text Formatting KeywordsTo change the size of textCHARSIZE=number (default is 1.0)To change the thickness of the textCHARTHICK=number (default is 1.0)Or set system variable:!P.Charsize=3</p></li><li><p>Coordinate systemsIDL has 3 coordinate systemsDATA (default)Uses the range of values of the dataDEVICEUses the device (X window or Postscript device) coordinates (i.e. pixels)NORMALNormalized coordinates from 0 to 1 over the 3 axes</p></li><li><p>Adding colorColor tablesLOADCT[, number]XLOADCT (widget)Graphics keywords:BACKGROUND=numberCOLOR=numberNumber from 0 to 255, corresponding to the color tablecolor=fsc_color(red) ;using library routineOn Mac, Windows: DEVICE, DECOMPOSED=0</p></li><li><p>ContourX=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</p></li><li><p>Surface plots (shaded surf)SHADE_SURF, Z, XRANGE=[0, 360], $ YRANGE=[0, 360], /XSTYLE, /YSTYLE, XTIT='X', $ YTIT='Y', TIT='Sample Surface Plot 2'</p></li><li><p>Colored surfaceloadct,3shade_surf,z,tit='Sample Surface Plot 3',shades=bytscl(z+1)</p><p>Bytscl translates the values of the array into a byte array from 0 to 255 </p></li><li><p>Surface plots 2 (wire mesh)SURFACE, Z, XRANGE=[0, 360], $ YRANGE=[0, 360], /XSTYLE, /YSTYLE, XTIT='X', $ YTIT='Y', TIT='Sample Surface Plot 2'</p></li><li><p>Changing appearance of meshz2=rebin(z,180,180)surface,z2z3=rebin(z,90,90)surface,z3The possibilities are endless!</p></li><li><p>Multiple plots on a page!p.multi=[a,b,c]a: number of plots left on pageb: number of columnsc: number of rowsExample: 6 plots per page (3x2), begin plotting in the top left!p.multi=[0,3,2]To return to a single plot per window, !p.multi=0</p></li><li><p>Making postscript filesSET_PLOT, PSDEVICE,, /LANDSCAPE, /PORTRAIT, YOFFSET=1, YSIZE=9, XSIZE=6, /INCHES, /COLOR, BITS_PER_PIXEL=8Plotting commandsDEVICE, /CLOSESET_PLOT, X</p><p>For Windows, SET_PLOT, WIN</p></li><li><p>Detailed plotting...set_plot,'ps'tek_color!p.font=1!p.color=0!p.background=1;!p.MULTI=[0,2,4]!P.MULTI=0ang = string("305B); Angstrom symboldevice,filename=fileout+'*.ps',/color,ysize=12, xsize=17,xoffset=1,yoffset=10;*****************************************************************plot,x1,y1,linestyle=1,/ylog,thick=6,$; /nodata does not plot the data tit='',CHARSIZE=1.5, xtit='!Ml!N (nm)',XCHARSIZE=1.5,$ytit='intensity (erg s!E-1!Ncm!E-2!NHz!E-1!N)',YCHARSIZE=1.5,$; !E -&gt;exponent; !I-&gt; index; !N -&gt;normal fontxrange=[x0,x1],xs=1, $; !M -&gt; mathfont, e.g. !Ml -&gt;lambdayrange=[y0,y1],ys=1;*****************************************************************; overplot;*****************************************************************oplot,x2,y2,linestyle=0,thick=3legend_mh01,textarr,charsize=charsize,textcolor=colors,colors=colors,$box=0,position=[.5,.55],/normalxyouts,0.85,0.85,a,charsize=1.5; label of figure panel, i.e. a, b, c, d....print,'Have a look at the plot :)'end</p></li><li><p>Making other types of graphics filesUse tvread function from David Fannings library</p><p>plot,x,yim=tvread(/png,file=filename,/nodialog)</p></li><li><p>Putting it all togetherHomework assignment 2Using LISIRD, create a composite Lyman- time series using SORCE, TIMED SEE, UARS SOLSTICE, and SME dataPlot each instrument as a different colorLabel each instrumentX-axis: year Y-axis: W/m2/nm</p></li><li><p>Sample Solution</p></li><li><p>Imaging basicsImages in IDL are just arrays. All standard mathematical operations are available.Images are usually saved in FITS formatHeader and array of integers (image)Use Goddard IDL Astronomy Library procedures to read both the header and imageRaw images must be processed before they can be used for science. Example:Remove the background counts (dark)Correct for variations across the CCD (flatfield)</p></li><li><p>Displaying imagesBasic IDL commands to display an array/imageTV, imageTVSCL, imageResize an imageREBIN(image, xsize, ysize)xsize and ysize must be integer factors of the original size of the imageAdd color by loading a color tableE.g. SOHO EIT has its own color table for each wavelength</p></li><li><p>and Stuffwheresizehistogramstring manipulationfile_searchmap projectionsn_elementsreading file formats (CDF, FITS, PNG, etc.)pointers</p></li><li><p>WhereUse WHERE to find data values meeting some criterion.good=where(data gt 0.)better=where(data gt 1.5 and $time gt julday(1,1,2005))data2=data[good] ;new variable data2data=data[better] ;redefine variable data</p></li><li><p>SizeThe size function returns lots of good information about your variable.(dimensions, n_elements, data type, etc.)</p><p>s=size(data)</p><p>An undefined variable is a valid type, so check for size or n_elements.</p></li><li><p>HistogramThere are billions and billions of uses for histogram.</p><p>h=histogram(data,binsize=2.5,omin=omin)bins=findgen(n_elements(h))+omin</p></li><li><p>StringsIDL understands regular expressionsConcatenate strings with +myname=yournameresult=strpos(myname,o',0)result=strupcase(myname)result=strlen(myname)</p><p>strsplit,...</p></li></ul>