Title: Coyote
1Coyotes Guide to IDL Programming
David Fanning
2Five 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
3Six Essential IDL Resources
http//idlastro.gsfc.nasa.gov/homepage.html
http//fermi.jhuapl.edu/s1r/idl/s1rlib/local_idl.h
tml
- Curve Fitting and Mathematics
http//cow.physics.wisc.edu/craigm/idl/idl.html
http//www.idlwave.org/
- Coyotes Guide to IDL Programming
http//www.dfanning.com
comp.lang.idl-pvwave
4NASA 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
5JHUAPL IDL Library (http//fermi.jhuapl.edu/s1r/i
dl/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
6Craig Markwardts 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
7MPFIT 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)
8Using MPFit
PlotErr, t, r, rerr, PSym3 expr P0
Gauss1(X, P13) start 950D, 2.5, 1.0,
1000.0) result MPFitExpr(expr, t, r, rerr,
start) OPlot, t, result0 Gauss1(t,
result13) Print, result 997.61864
2.1550703 1.4488421 3040.2411
9Constraining Parameters
Fix the constant parameter at 1000.
PlotErr, t, r, rerr, PSym3 expr P0
Gauss1(X, P13) pinfo Replicate( fixed0,
limited0,0, limits0.D, 0.D , 4
) pinfo0.fixed 1 start 1000.D, 2.5, 1.0,
1000.0 result MPFitExpr(expr, t, r, rerr,
start, PARINFOpinfo) OPlot, t, result0
Gauss1(t, result13) Print, result
1000.0000 2.1549624 1.4427532 3021.8174
10Constraining Parameters
Limit the mean to a minimum of 2.3.
PlotErr, t, r, rerr, PSym3 expr P0
Gauss1(X, P13) pinfo Replicate( fixed0,
limited0,0, limits0.D, 0.D , 4
) pinfo1.limited0 1 pinfo1.limits0
2.3 start 1000.D, 2.5, 1.0, 1000.0 result
MPFitExpr(expr, t, r, rerr, start,
PARINFOpinfo) OPlot, t, result0 Gauss1(t,
result13) Print, result 997.56563
2.3000000 1.4557194 3035.0424
11IDLWAVE 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
12Six Essential IDL Resources
http//idlastro.gsfc.nasa.gov/homepage.html
http//fermi.jhuapl.edu/s1r/idl/s1rlib/local_idl.h
tml
- Curve Fitting and Mathematics
http//cow.physics.wisc.edu/craigm/idl/idl.html
http//www.idlwave.org/
- Coyotes Guide to IDL Programming
www.dfanning.com
comp.lang.idl-pvwave
13Coyotes Guide to IDL Programming(http//www.dfan
ning.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 Coyotes adventures
14Must-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
15IDL 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
16Five 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
17True-Color Color Decomposition
If your graphic output looks like this youdont
understand color decomposition.With 24-bit
graphics, color decompositionis always ON by
default.
IDLgt Plot, data, Color255 IDLgt 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
18Steel Blue (70,130,180)
IDLgt Device, Decomposed1 IDLgt Print, 70L (130L
2L8) (180L 2L16) 11829830 IDLgt Plot,
data, Color11829830L, BackgroundffffffxL
IDLgt Plot, data, Color255
19Color Decomposition ON
Color Decomposition OFF
24-Bit Color
8-Bit Color
Plot, data, Color 11829830L
Plot, data, Color3
20Steel Blue (70,130,180)
24-bit Non-Decomposed Color
IDLgt TVLCT, 70, 130, 180, 240 IDLgt Device,
Decomposed0 IDLgt Plot, data, Color240,
Background255
21Device Independent Color
- Use FSC_Color for specifying colors by name
IDLgt Plot, data, ColorFSC_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
22Example Code
bg FSC_Color(ivory) fg FSC_Color(navy)
Plot, data, Colorfg, Backgroundbg,
/NoData Oplot, data1, ColorFSC_Color(saddle
brown) Oplot, data2, Color FSC_Color(indian
red) Oplot, data3, Color FSC_Color(forest
green)
23IDLgt Device, Decomposed1 IDLgt Loadct, 13 IDLgt
TV, image
IDLgt Device, Decomposed0 IDLgt Loadct, 13 IDLgt
TV, image
IDLgt TVImage, image IDLgt image TVRead()
24Five 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
25Position Plots with POSITION
Always use Normal or Data coordinatesfor
device-independent placement oftext and graphics.
Plot, data, Position0.10, 0.10, 0.45,
0.90 Plot, data, Position0.55, 0.10, 0.95,
0.65, /NoErase XYOutS, 0.75, 0.8, Position
Plots Here, Alignment0.5, /Normal, Font0
26Position Plots with POSITION
IDLgt Plot, data
IDLgt Plot, data, PositionAspect(1.0)
27Find a Positionable TV Command
- TVImage
- ImgDisp
- PlotImage
IDLgt Pos 0.1, 0.1, 0.9, 0.9 IDLgt TVImage,
image, Positionpos, /Keep_Aspect,
/NoInterpolation IDLgt Plot, findgen(100),
/NoData, Positionpos, /NoErase
28Avoid Auto-scaling Axes
Plot, data, XRange23.4, 78
Plot, data, XRange23.4, 78, XStyle1
29Avoid Auto-scaling Axes
Contour, image, NLevels12
Contour, image, NLevels12 XStyle1, YStyle1
30Plotting Symbols
1 Plus sign () 2 Asterisk () 3 Period
(.) 4 Diamond 5 Triangle 6 Square 7 X
8 User-defined.
Plot, data, PSym2
Plot, data, PSym-2
31Create Plotting Symbols(USERSYM)
Filled Circle
phi Findgen(32) (!PI 2 / 32.) phi phi,
phi0 UserSym, Cos(phi), Sin(phi), /Fill Plot,
data, PSym8, Symsize1.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.
32Color with Symbols
phi Findgen(32) (!PI 2 / 32.) phi phi,
phi0 UserSym, Cos(phi), Sin(phi), /Fill Plot,
data, /NoData OPlot, data, ColorFSC_Color('firebr
ick'), Oplot, data, colorFSC_Color('forest
green'), PSym8, Symsize1.5
33Keyword Inheritance (_Extra)
PRO MyPlot, data, Colorcolor, DataColordataColor
IF N_Elements(color) NE 0 THEN Message,
Color is replaced by DataColor keyword IF
N_Elements(dataColor) EQ 0 THEN dataColorsteel
blue Plot, data, ColorFSC_Color(dataColor) EN
D
PRO MyPlot, data, Colorcolor, DataColordataColor
, _Extraextra IF N_Elements(color) NE 0 THEN
Message, Color is replaced by DataColor
keyword IF N_Elements(dataColor) EQ 0 THEN
dataColorsteel blue Plot, data,
ColorFSC_Color(dataColor), _Extraextra END
34Keyword Inheritance (_Extra)
IDLgt MyPlot, findgen(11), Charsize2.0, TitleMy
Color Plot, DataColorgreen
extra charsize 2.0, titleMy Color Plot
IDLgt kw PSConfig(/European) IDLgt Set_Plot,
PS IDLgt Device, _Extrakw
35Five 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
36Avoid FOR Loops if Possible
IDLgt array Indgen(3,4) IDLgt Print, array
0 1 2 3 4 5
6 7 8 9 10 11
Multiply array by 3
Fortran Way For j0,2 Do Begin For k0,3 Do
Begin array(j,k) array(j,k) 3
Endfor Endfor
IDL Way array array 3
37Array Operators
Set all values greater than 5 to 5.
Fortran Way For j0,2 Do Begin For k0,3 Do
Begin IF array(j,k) GT 5 THEN array(j,k)
5 Endfor Endfor
IDL Way array array lt 5
IDLgt array Indgen(3,4) IDLgt Print, array lt 5
0 1 2 3 4 5
5 5 5 5 5 5
38WHERE much faster than IF
Set all values between 5 to 8 equal 15.
Fortran Way For j0,2 Do Begin For k0,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 arrayindex 15
39Convert 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 arrayindex 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
40Convert 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 arraycol, 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
41Convert 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,
ColorFSC_Color(yellow) ENDIF
42Dimensional Juggling
Multiply each column of array by a vector, b.
IDLgt array Indgen(3,4) IDLgt b
Fix(RandomU(-1L, 4) 12 IDLgt Print, array
0 1 4 4 5 6
7 8 9 10 11
12 IDLgt Print, b 4 1 9 6
Fortran Way var IntArr(3,4) For j0,2 Do
Begin varj, arrayj, b EndFor
43Dimensional Juggling
Multiply each column of array by a vector, b.
IDLgt Print, b 4 1 9 6 IDLgt Print,
Reform(b, 1, 4) 4 1 9 6 IDLgt
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 )
44Dimensional Juggling
Can extend this to any number of dimensions.
IDLgt 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
IDLgt 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
45Array Concatenation
IDLgt a Make_Array(4, 4, Value1B) IDLgt Print,
a 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 IDLgt b Make_Array(4, 4, Value2B) IDLgt Print,
b 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2
2 IDLgt Print, a, b 1 1 1 1
2 2 2 2 1 1 1 1 2
2 2 2 1 1 1 1 2 2
2 2 1 1 1 1 2 2 2
2
46Array Concatenation
IDLgt Print, a, b IDLgt 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 IDLgt Help,
a, b INT Array4, 8
IDLgt Print, a , b IDLgt 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 IDLgt
Help, a , b INT Array4,
4, 2
47Array Concatenation
Add rows to an array.
Add columns to an array.
IDLgt b (Indgen(4) 1) 4 IDLgt 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 IDLgt Help, a, b, b INT
Array4, 6
IDLgt b Reform((Indgen(4) 1) 4, 1, 4) IDLgt
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 IDLgt Help, b, a, b INT
Array6, 4
48Array Concatenation
Create a true-color image
IDLgt image24 image_1 , image_2 ,
image_3 IDLgt Help, image24 INT
Array400, 600, 3
Convert band interleaved image to pixel
interleaved
IDLgt image24 Transpose(image24,2,0,1) IDLgt
Help, image24 INT Array3, 400, 600
49Index Manipulation
Quick look at two images simultaneously.
img1 BytScl(Loaddata(4), Top99) img2
BytScl(Loaddata(5), Top99)100B LoadCT, 13,
NColors100 LoadCT, 3, NColors100,
Bottom100 index Where((Indgen(256L256L) MOD
2) EQ 0) img1index img2index Window,
XSize256, YSize256 TV, img1
50Index Manipulation
Reverse the even rows in a 2D array.
IDLgt data Indgen(8,4) 1 IDLgt 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 IDLgt
evenRowIndex (Indgen(4/2) 2) 1 IDLgt
data, evenRowIndex Reverse(data,
evenRowIndex) IDLgt 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
51Histogram Magic
A histogram is used to count how many elements
fall into bins.
IDLgt a Indgen(10) IDLgt Print, Histogram(a,
Binsize5) 5 5 IDLgt Print,
Histogram(a) 1 1 1 1 1 1
1 1 1 1 IDLgt data Randomu(-3L,
100) IDLgt Print, Histogram(data, Binsize0.20)
25 17 22 18 18
52Histogram Magic
If binning data was all Histogram did, it would
be useful, but not spectacular! Think of
Histogram as an index manipulator and it becomes
a lot more exciting.
IDLgt datafix(randomu(101,25)12) Print, data
5 2 6 11 7 1 4 0 10 11 4 0 1
10 0 4 3 11 9 4 1 1 11 4
3 IDLgt h Histogram(data, REVERSE_INDICESri)
3 4 1 2 5 1 1 1 0 1 2 4
6
5 10
3 7 12 15
9 11 20 16 19
8 17 14 21 1 24 23
0 2 4 18 3 22 ---------------
--------------------- 0 1 2 3
4 5 6 7 8 9 10 11
53Histogram Magic
Histogram is fast, much faster than Where.
3 4 1 2 5 1 1 1 0 1 2 4
6
5 10
3 7 12 15
9 11 20 16 19
8 17 14 21 1 24 23
0 2 4 18 3 22 ---------------
--------------------- 0 1 2 3
4 5 6 7 8 9 10 11
The I vector The O
vector ri iiiiiiiiiiiiiiiiiiiooooooooooooooooooo
ooooooooooo ------------------------------
--------------- 0 nhnh1
nhtotal(h)
The o-vector contains the data indices, in order,
and the i-vector just shows us where to go to get
them.
54Reverse_Indices looks ugly, but works great!
6
5 10 3
7 12 15 9
11 20 16 19 8 17
14 21 1 24 23 0 2 4 18 3 22
------------------------------------
0 1 2 3 4 5 6 7 8 9 10
11
The I vector The O
vector ri iiiiiiiiiiiiiiiiiiiooooooooooooooooooo
ooooooooooo ------------------------------
--------------- 0 nhnh1
nhtotal(h)
IDLgt Print, riri4ri5-1 6
10 15 19 23 IDLgt
Print, datariri4ri5-1 4 4
4 4 4
IDLgt if ri4 ne ri5 then Print,
datariri4ri5-1 else Print, 'No data in
bin 4 4 4 4 4
4 IDLgt if ri8 ne ri9 then Print,
datariri8ri8-1 else Print, 'No data in
bin 8' No data in bin 8
55Using Histogram
Find the intersection (common elements) of two
vectors.
IDLgt sd -3L IDLgt a Fix( Randomu(sd, 8) 20
) IDLgt b Fix( Randomu(sd, 8) 20 ) IDLgt Print,
a, b 17 11 15 11 1 19 0 4
2 19 17 18 16 3 4 16 IDLgt
Print, Where(Histogram(a, OMinom) gt 0 AND
Histogram(b, Minom) gt 0) om 4 17 19
Find the union (elements in either vector) of two
vectors.
IDLgt Print, Where(Histogram(a,b, OMin om))
om 0 1 2 3 4 11 15 16 17
18 19
56Using Histogram
Remove elements, listed in random order, from a
vector.
IDLgt vec Randomu(sd,10) IDLgt remove
3,7,2,8 IDLgt keep Where(Histogram(remove,MIN
0,MAXN_Elements(vec)-1) eq 0,cnt) IDLgt if cnt
ne 0 then vec veckeep IDLgt print,keep
0 1 4 5 6 9
Find the median value for each quartile of a data
set.
IDLgt data Randomu(sd,100)100 IDLgt h
Histogram(data,Binsize25, Reverse_Indicesri) IDL
gt med FltArr(4) IDLgt for j0L,3L do if rij1
gt rij then medj Median(dataririjrij1
-1) IDLgt print,med 15.2426 40.9219
63.8255 86.1637
57Five 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
58Set up Window on PostScript Page
Set_Plot, PS Device, XSizexs, YSizeys,
XOffsetxoff, YOffsetyoff, /Landscape
59Create a PostScript Window with the Correct
Aspect Ratio
Plot, data keywords PSWindow() Help, keywords,
/Structure XSIZE FLOAT
5.95000 YSIZE FLOAT
5.95000 XOFFSET FLOAT
1.27500 YOFFSET FLOAT
2.52500 INCHES INT 1
PORTRAIT INT 1 LANDSCAPE
INT 0 thisDevice
!D.Name Set_Plot, PS Device, _Extrakeywords Plo
t, data Device, /Close_File Set_Plot, thisDevice
60Position Plots with POSITION
Always use Normal or Data coordinatesfor
device-independent placement oftext and graphics.
Plot, data, Position0.10, 0.10, 0.45,
0.90 Plot, data, Position0.55, 0.10, 0.95,
0.65, /NoErase XYOutS, 0.75, 0.8, Position
Plots Here, Alignment0.5, /Normal, Font0
61Use a Positionable TV Command
- TVImage
- ImgDisp
- PlotImage
IDLgt Pos 0.1, 0.1, 0.9, 0.9 IDLgt TVImage,
image, Positionpos, /Keep_Aspect,
/NoInterpolation IDLgt Plot, findgen(100),
/NoData, Positionpos, /NoErase
62PostScript Setup
keywords PSConfig(Cancelcancelled) IF
cancelled THEN RETURN thisDevice
!D.Name Set_Plot, PS Device, _Extrakeywords Plo
t, data Device, /Close Set_Plot, thisDevice
63Load Drawing Colors Properly
bg FSC_Color(ivory) fg FSC_Color(navy)
Plot, data, Colorfg, Backgroundbg,
/NoData Oplot, data1, ColorFSC_Color(saddle
brown) Oplot, data2, Color FSC_Color(indian
red) Oplot, data3, Color FSC_Color(forest
green)
64Use PostScript or True-Type Fonts
You will have better looking plots if you use
PostScript or True-Type fonts. If you use
TeXtoIDL be sure to set the PostScriptkeyword.
Plot, data, Font0 !P.Font 0 Device,
Set_FontArial14, /TT_Font Plot, data,
Font1 XYOutS, 0.75, 0.8, Position Plots Here,
/Normal, Font1
65Protect Device-Specific Code
IF (!D.Flags AND 256) NE 0 THEN Windows
supportedWindow, XSize400, YSize400 bg
FSC_Color(ivory) fg FSC_Color(navy) Plot,
data, Colorfg, Backgroundbg, /NoData Oplot,
data1, ColorFSC_Color(saddle brown) Oplot,
data2, Color FSC_Color(indian red) Oplot,
data3, Color FSC_Color(forest green)
- Other commands to protect
- WSet, wid
- Device, Decomposed0
66Coyotes Guide to IDL Programming
David Fanning