680 likes | 945 Vues
Coyote’s Guide to IDL Programming. David Fanning. Five Ways To Improve Your IDL Programming Now. Learn the Essential Web Resources Learn How Colors Work in IDL Learn 10 Essential Graphics Keywords Learn the “IDL Way” to Avoid FOR Loops Learn to Produce Perfect PostScript Output.
E N D
Coyote’s Guide to IDL Programming David Fanning
Five Ways To Improve Your IDL Programming Now • Learn the Essential Web Resources • Learn How Colors Work in IDL • Learn 10 Essential Graphics Keywords • Learn the “IDL Way” to Avoid FOR Loops • Learn to Produce Perfect PostScript Output
Six Essential IDL Resources • NASA Astronomy Library • Coyote’s Guide to IDL Programming http://idlastro.gsfc.nasa.gov/homepage.html • JHUAPL IDL Library http://fermi.jhuapl.edu/s1r/idl/s1rlib/local_idl.html • Curve Fitting and Mathematics http://cow.physics.wisc.edu/~craigm/idl/idl.html • IDL Emacs Mode (IDLWAVE) http://www.idlwave.org/ http://www.dfanning.com • IDL Newsgroup comp.lang.idl-pvwave
NASA Astronomy Library (http://idlastro.gsfc.nasa.gov/homepage.html) • Only way to read FITS data • Hundreds of useful, and well-tested routines for math, statistics, and astronomical utilities • Should already be on your path • The Astronomy Links page is the source of IDL software used for astronomy
JHUAPL IDL Library(http://fermi.jhuapl.edu/s1r/idl/s1rlib/local_idl.html) • Solid, well-written library maintained by Ray Sterner • Excellent for time-series data • Date and time routines are best anywhere • Array processing code, filters • Math routines • Often used in conjunction with Astronomy Lib
Craig Markwardt’s IDL Library (http://cow.physics.wisc.edu/~craigm/idl/idl.html) • Statistical routines • Specialized peak and ellipse fitting applications • Non-linear optimization routines • Optimized set operations (intersect, union, XOR) • Mathematics (array operators, quaternions, etc.) • Integration and differentiation routines • Non-linear least-squares curve fitting
MPFIT Curve Fitting • Non-linear least-squares curve fitting • Replacement for CURVEFIT • Robust MINPACK-1 FORTRAN subroutine • Unlimited number of parameters • Each parameter can be held fixed or limited to defined max and min values • Easy access to MPFIT through wrappers • An interactive interface to MPFIT is available (PAN from NIST)
Using MPFit PlotErr, t, r, rerr, PSym=3 expr = ‘P[0] + Gauss1(X, P[1:3]) start = [950D, 2.5, 1.0, 1000.0) result = MPFitExpr(expr, t, r, rerr, start) OPlot, t, result[0] + Gauss1(t, result[1:3]) Print, result 997.61864 2.1550703 1.4488421 3040.2411
Constraining Parameters Fix the constant parameter at 1000. PlotErr, t, r, rerr, PSym=3 expr = ‘P[0] + Gauss1(X, P[1:3]) pinfo = Replicate( { fixed:0, limited:[0,0], limits:[0.D, 0.D] }, 4 ) pinfo[0].fixed = 1 start = [1000.D, 2.5, 1.0, 1000.0] result = MPFitExpr(expr, t, r, rerr, start, PARINFO=pinfo) OPlot, t, result[0] + Gauss1(t, result[1:3]) Print, result 1000.0000 2.1549624 1.4427532 3021.8174
Constraining Parameters Limit the mean to a minimum of 2.3. PlotErr, t, r, rerr, PSym=3 expr = ‘P[0] + Gauss1(X, P[1:3]) pinfo = Replicate( { fixed:0, limited:[0,0], limits:[0.D, 0.D] }, 4 ) pinfo[1].limited[0] = 1 pinfo[1].limits[0] = 2.3 start = [1000.D, 2.5, 1.0, 1000.0] result = MPFitExpr(expr, t, r, rerr, start, PARINFO=pinfo) OPlot, t, result[0] + Gauss1(t, result[1:3]) Print, result 997.56563 2.3000000 1.4557194 3035.0424
IDLWAVE Emacs Mode(http://www.idlwave.org/) • Color-coded editor • Outstanding HTML help • Optional auto-corrected typing for enforcing syntax • Code completion and checking • Several types of interactive debugging tools • Catalog system can scan ALL IDL libraries! • Outstanding support via IDL newsgroup
Six Essential IDL Resources • NASA Astronomy Library http://idlastro.gsfc.nasa.gov/homepage.html • Coyote’s Guide to IDL Programming • JHUAPL IDL Library http://fermi.jhuapl.edu/s1r/idl/s1rlib/local_idl.html • Curve Fitting and Mathematics http://cow.physics.wisc.edu/~craigm/idl/idl.html • IDL Emacs Mode (IDLWAVE) http://www.idlwave.org/ www.dfanning.com • IDL Newsgroup comp.lang.idl-pvwave
Coyote’s Guide to IDL Programming(http://www.dfanning.com) • 300+ articles in the IDL Tips and Tricks section • Tutorials on essential subjects • 75+ IDL example programs • IDL bug reports • Ask an IDL Question • Order an IDL book • Links to other sites • Learn the latest about Coyote’s adventures
Must-Read Articles for Astronomers • Dimensional Juggling Tutorial • Array Concatenation Tutorial • Histogram: The Breathless Horror and Disgust • Are FOR Loops the Embodiment of Pure Evil? • Are FOR Loops Really Evil? • Average Astrophysicist Increased Program Speed by a Factor of 8100! (http://tinyurl.com/5w3y9) • How to Get Perfect PostScript Output
IDL Newsgroup(comp.lang.idl-pvwave) • Friendliest newsgroup on the Internet • 20+ IDL experts share their knowledge • RSI engineers lurk on the newsgroup • Searchable database on groups.google.com • Hitting SEND button is best way to learn IDL • Info on IDL Expert Programmers Association
Five Ways To Improve Your IDL Programming Now • Learn the Essential Web Resources • Learn How Colors Work in IDL • Learn 10 Essential Graphics Keywords • Learn the “IDL Way” to Avoid FOR Loops • Learn to Produce Perfect PostScript Output
True-Color Color Decomposition If your graphic output looks like this youdon’t understand color decomposition.With 24-bit graphics, color decompositionis always ON by default. IDL> Plot, data, Color=255 IDL> Help, /Device Simultaneously displayable colors: 16777216 Number of allowed color values: 16777216 IDL Color Table Entries: 256 NOTE: this is a TrueColor device Using Decomposed color
1 0 1 1 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 = 11829830L 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 = 255L blue green red blue green red Steel Blue (70,130,180) IDL> Device, Decomposed=1 IDL> Print, 70L + (130L * 2L^8) + (180L * 2L^16) 11829830 IDL> Plot, data, Color=11829830L, Background=‘ffffff’xL IDL> Plot, data, Color=255
Index R G B 0 1 2 3 4 . . . 255 13 25 22 70 2 . . . 45 67 44 87 130 38 . . . 255 80 178 223 180 177 . . . 128 Index R G B 0 1 2 3 4 . . . 255 0 1 2 3 4 70 . . 255 0 1 2 3 4 . 130 . 255 0 1 2 3 4 . . 180 255 Color Decomposition ON Color Decomposition OFF 24-Bit Color 8-Bit Color Plot, data, Color= 11829830L Plot, data, Color=3
Steel Blue (70,130,180) 24-bit Non-Decomposed Color IDL> TVLCT, 70, 130, 180, 240 IDL> Device, Decomposed=0 IDL> Plot, data, Color=240, Background=255
Device Independent Color • Use FSC_Color for specifying colors by name IDL> Plot, data, Color=FSC_Color(‘Steel Blue’) • Color decomposition independent • Works in PostScript, Z-graphics buffer, display • Stay away from top and bottom of color table for drawing colors • 88 colors are available. Can easily add your own colors or load them from a file
Example Code bg = FSC_Color(‘ivory’) fg = FSC_Color(‘navy’) Plot, data, Color=fg, Background=bg, /NoData Oplot, data1, Color=FSC_Color(‘saddle brown’) Oplot, data2, Color= FSC_Color(‘indian red’) Oplot, data3, Color= FSC_Color(‘forest green’)
IDL> Device, Decomposed=1 IDL> Loadct, 13 IDL> TV, image IDL> Device, Decomposed=0 IDL> Loadct, 13 IDL> TV, image IDL> TVImage, image IDL> image = TVRead()
Five Ways To Improve Your IDL Programming Now • Learn the Essential Web Resources • Learn How Colors Work in IDL • Learn 10 Essential Graphics Keywords • Learn the “IDL Way” to Avoid FOR Loops • Learn to Produce Perfect PostScript Output
Position Plots with POSITION Always use Normal or Data coordinatesfor device-independent placement oftext and graphics. Plot, data, Position=[0.10, 0.10, 0.45, 0.90] Plot, data, Position=[0.55, 0.10, 0.95, 0.65], /NoErase XYOutS, 0.75, 0.8, ‘Position Plots Here’, Alignment=0.5, $ /Normal, Font=0
Position Plots with POSITION IDL> Plot, data IDL> Plot, data, Position=Aspect(1.0)
Find a Positionable TV Command • TVImage • ImgDisp • PlotImage IDL> Pos = [0.1, 0.1, 0.9, 0.9] IDL> TVImage, image, Position=pos, /Keep_Aspect, /NoInterpolation IDL> Plot, findgen(100), /NoData, Position=pos, /NoErase
Avoid Auto-scaling Axes Plot, data, XRange=[23.4, 78] Plot, data, XRange=[23.4, 78], $XStyle=1
Avoid Auto-scaling Axes Contour, image, NLevels=12 Contour, image, NLevels=12 $XStyle=1, YStyle=1
Plotting Symbols 1 Plus sign (+) 2 Asterisk (*) 3 Period (.) 4 Diamond 5 Triangle 6 Square 7 X 8 User-defined. Plot, data, PSym=2 Plot, data, PSym=-2
Create Plotting Symbols(USERSYM) Filled Circle phi = Findgen(32) * (!PI * 2 / 32.) phi = [ phi, phi[0] ] UserSym, Cos(phi), Sin(phi), /Fill Plot, data,PSym=8,Symsize=1.25 Filled Triangles UserSym, [ -1, 1, -1, -1 ], [1, 0, -1, 1 ], /Fill ; Filled right-facing triangle. UserSym, [ 1, -1, 1, 1 ], [1, 0, -1, 1 ], /Fill ; Filled left-facing triangle. UserSym, [ -1, 1, -1, -1 ], [1, 0, -1, 1 ] ; Open right-facing triangle.
Color with Symbols phi = Findgen(32) * (!PI * 2 / 32.) phi = [ phi, phi[0] ] UserSym, Cos(phi), Sin(phi), /Fill Plot, data, /NoData OPlot, data, Color=FSC_Color('firebrick'), Oplot, data, color=FSC_Color('forest green'), $ PSym=8, Symsize=1.5
Keyword Inheritance (_Extra) PRO MyPlot, data, Color=color, DataColor=dataColor IF N_Elements(color) NE 0 THEN $ Message, ‘Color is replaced by DataColor keyword’ IF N_Elements(dataColor) EQ 0 THEN dataColor=‘steel blue’ Plot, data, Color=FSC_Color(dataColor) END PRO MyPlot, data, Color=color, DataColor=dataColor, _Extra=extra IF N_Elements(color) NE 0 THEN $ Message, ‘Color is replaced by DataColor keyword’ IF N_Elements(dataColor) EQ 0 THEN dataColor=‘steel blue’ Plot, data, Color=FSC_Color(dataColor), _Extra=extra END
Keyword Inheritance (_Extra) IDL> MyPlot, findgen(11), Charsize=2.0, Title=‘My Color Plot’, DataColor=‘green’ extra = { charsize: 2.0, title=‘My Color Plot’ } IDL> kw = PSConfig(/European) IDL> Set_Plot, ‘PS’ IDL> Device, _Extra=kw
Five Ways To Improve Your IDL Programming Now • Learn the Essential Web Resources • Learn How Colors Work in IDL • Learn 10 Essential Graphics Keywords • Learn the “IDL Way” to Avoid FOR Loops • Learn to Produce Perfect PostScript Output
Avoid FOR Loops if Possible IDL> array = Indgen(3,4) IDL> Print, array 0 1 2 3 4 5 6 7 8 9 10 11 Multiply array by 3 Fortran Way: For j=0,2 Do Begin For k=0,3 Do Begin array(j,k) = array(j,k) * 3 Endfor Endfor IDL Way: array = array * 3
Array Operators Set all values greater than 5 to 5. Fortran Way: For j=0,2 Do Begin For k=0,3 Do Begin IF array(j,k) GT 5 THEN array(j,k) = 5 Endfor Endfor IDL Way: array = array < 5 IDL> array = Indgen(3,4) IDL> Print, array < 5 0 1 2 3 4 5 5 5 5 5 5 5
WHERE much faster than IF Set all values between 5 to 8 equal = 15. Fortran Way: For j=0,2 Do Begin For k=0,3 Do Begin IF (array(j,k) GE 5) AND (array(j,k) LT 8) THEN array(j,k) = 15 EndFor EndFor IDL Way: index = Where((array GE 5) AND (array LE 8), count) IF count GT 0 THEN array[index] = 15
Convert 1D Indices to 2D Indices array = Round(Randomu(-3L, 5, 5) * 10) Print, array 9 6 8 6 1 10 0 2 1 10 9 9 8 2 2 8 1 7 0 1 7 0 2 1 3 indices = Where((array GE 3) AND (array LE 7), count) IF count GT 0 THEN array[index] = 99 Print, array 9 99 8 99 1 10 0 2 1 10 9 9 8 2 2 8 1 99 0 1 99 0 2 1 3
Convert 1D Indices to 2D Indices array = Round(Randomu(-3L, 5, 5) * 10) Print, array 9 6 8 6 1 10 0 2 1 10 9 9 8 2 2 8 1 7 0 1 7 0 2 1 3 indices = Where((array GE 3) AND (array LE 7), count) result = Array_Indices(array, indices) col = Reform(result [0,*]) row = Reform(result [1,*]) IF count GT 0 THEN array[col, row] = 99 Print, array 9 99 8 99 1 10 0 2 1 10 9 9 8 2 2 8 1 99 0 1 99 0 2 1 3
Convert 1D Indices to 2D Indices indices = Where((image GE 0.55) AND (image LE 0.65), count) IF count GT 0 THEN BEGIN result = Array_Indices(image, indices) col = Reform(result [0,*]) row = Reform(result [1,*]) TV, image PlotS, col, row, /Device, Color=FSC_Color(‘yellow’) ENDIF
Dimensional Juggling Multiply each column of array by a vector, b. IDL> array = Indgen(3,4) IDL> b = Fix(RandomU(-1L, 4) * 12 IDL> Print, array 0 1 4 4 5 6 7 8 9 10 11 12 IDL> Print, b 4 1 9 6 Fortran Way: var = IntArr(3,4) For j=0,2 Do Begin var[j,*] = array[j,*] * b EndFor
Dimensional Juggling Multiply each column of array by a vector, b. IDL> Print, b 4 1 9 6 IDL> Print, Reform(b, 1, 4) 4 1 9 6 IDL> Print, Rebin( Reform(b, 1, 4), 3, 4) 4 4 4 1 1 1 9 9 9 6 6 6 col = Transpose(b) col = Rotate(b,1) col = 1 # b col = b ## 1 IDL Way: Print, var = array * Rebin( Reform(b, 1, 4), 3, 4 )
Dimensional Juggling Can extend this to any number of dimensions. IDL> array = Indgen(3,4,3) 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 IDL> Print, Rebin( Reform(b,1,4,1), 3, 4, 3) 4 4 4 1 1 1 9 9 9 6 6 6 4 4 4 1 1 1 9 9 9 6 6 6 4 4 4 1 1 1 9 9 9 6 6 6
Array Concatenation IDL> a = Make_Array(4, 4, Value=1B) IDL> Print, a 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 IDL> b = Make_Array(4, 4, Value=2B) IDL> Print, b 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 IDL> Print, [a, b] 1 1 1 12 2 2 2 1 1 1 12 2 2 2 1 1 1 12 2 2 2 1 1 1 12 2 2 2
Array Concatenation IDL> Print, [ [a], [b] ] IDL> Print, a 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 IDL> Help, [ [a], [b] ] INT = Array[4, 8] IDL> Print, [ [ [a] ], [ [b] ] ] IDL> Print, a 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 IDL> Help, [ [ [a] ], [ [b] ] ] INT = Array[4, 4, 2]
Array Concatenation Add rows to an array. Add columns to an array. IDL> b = (Indgen(4) + 1) * 4 IDL> Print, [ [a], [b], [b] ] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 8 12 16 4 8 12 16 IDL> Help, [ [a], [b], [b] ] INT = Array[4, 6] IDL> b = Reform((Indgen(4) + 1) * 4, 1, 4) IDL> Print, [ b, a, b ] 4 1 1 1 1 4 8 1 1 1 1 8 12 1 1 1 1 12 16 1 1 1 1 16 IDL> Help, [ b, a, b ] INT = Array[6, 4]
Array Concatenation Create a true-color image IDL> image24 = [ [ [image_1] ], [ [image_2] ], [ [image_3] ] ] IDL> Help, image24 INT = Array[400, 600, 3] Convert band interleaved image to pixel interleaved IDL> image24 = Transpose(image24,[2,0,1]) IDL> Help, image24 INT = Array[3, 400, 600]
Index Manipulation Quick look at two images simultaneously. img1 = BytScl(Loaddata(4), Top=99) img2 = BytScl(Loaddata(5), Top=99)+100B LoadCT, 13, NColors=100 LoadCT, 3, NColors=100, Bottom=100 index = Where((Indgen(256L*256L) MOD 2) EQ 0) img1[index] = img2[index] Window, XSize=256, YSize=256 TV, img1
Index Manipulation Reverse the even rows in a 2D array. IDL> data = Indgen(8,4) + 1 IDL> Print, data 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 227 28 29 30 31 32 IDL> evenRowIndex = (Indgen(4/2) * 2) + 1 IDL> data[*, evenRowIndex] = Reverse(data[*, evenRowIndex]) IDL> Print, data 1 2 3 4 5 6 7 8 16 15 14 13 12 11 10 9 17 18 19 20 21 22 23 24 32 31 30 29 28 27 26 25