Transcript Slide 1

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
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/
• Coyote’s Guide to IDL Programming
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
• 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/
• Coyote’s Guide to IDL Programming
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 you
don’t understand color decomposition.
With 24-bit graphics, color decomposition
is 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
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
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
blue
green
red
IDL> Plot, data, Color=255
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
Color Decomposition ON
Color Decomposition OFF
24-Bit Color
Index R
0
0
1
1
2
2
3
3
4
4
.
70
.
.
.
.
255 255
G
0
1
2
3
4
.
130
.
255
8-Bit Color
B
0
1
2
3
4
.
.
180
255
Plot, data, Color= 11829830L
Index
0
1
2
3
4
.
.
.
255
R
13
25
22
70
2
.
.
.
45
G
67
44
87
130
38
.
.
.
255
B
80
178
223
180
177
.
.
.
128
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 coordinates
for device-independent placement of
text 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
2
3
4
5
6
7
8
Plus sign (+)
Asterisk (*)
Period (.)
Diamond
Triangle
Square
X
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
3
6
9
1
4
7
10
2
5
8
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:
IDL Way:
array = array < 5
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> array = Indgen(3,4)
IDL> Print, array < 5
0
3
5
5
1
4
5
5
2
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
10
9
8
7
6
0
9
1
0
8
2
8
7
2
6
1
2
0
1
1
10
2
1
3
indices = Where((array GE 3) AND (array LE 7), count)
IF count GT 0 THEN array[index] = 99
Print, array
9
10
9
8
99
99
0
9
1
0
8
2
8
99
2
99
1
2
0
1
1
10
2
1
3
Convert 1D Indices to 2D Indices
array = Round(Randomu(-3L, 5, 5) * 10)
Print, array
9
10
9
8
7
6
0
9
1
0
8
2
8
7
2
6
1
2
0
1
1
10
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
10
9
8
99
99
0
9
1
0
8
2
8
99
2
99
1
2
0
1
1
10
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
4
7
10
1
5
8
11
4
6
9
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
1
9
6
4
1
9
6
4
1
9
6
IDL Way:
Print, var = array * Rebin( Reform(b, 1, 4), 3, 4 )
col = Transpose(b)
col = Rotate(b,1)
col = 1 # b
col = b ## 1
Dimensional Juggling
Can extend this to any number of dimensions.
IDL> array = Indgen(3,4,3) IDL> Print, Rebin( Reform(b,1,4,1), 3, 4, 3)
0
3
6
9
1
4
7
10
2
5
8
11
4
1
9
6
4
1
9
6
4
1
9
6
12
15
18
21
13
16
19
22
14
17
20
23
4
1
9
6
4
1
9
6
4
1
9
6
24
27
30
33
25
28
31
34
26
29
32
35
4
1
9
6
4
1
9
6
4
1
9
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
1
1
1
1
1
1
1
1
IDL> Print, [a, b]
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
Array Concatenation
IDL> Print, [ [a], [b] ]
IDL> Print, a
1
1
1
1
2
2
2
2
1
1
1
1
2
2
2
2
1
1
1
1
2
2
2
2
IDL> Print, [ [ [a] ], [ [b] ] ]
IDL> Print, a
1
1
1
1
2
2
2
2
IDL> Help, [ [a], [b] ]
INT = Array[4, 8]
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.
IDL> b = (Indgen(4) + 1) * 4
IDL> Print, [ [a], [b], [b] ]
1
1
1
1
4
4
1
1
1
1
8
8
1
1
1
1
12
12
1
1
1
1
16
16
IDL> Help, [ [a], [b], [b] ]
INT = Array[4, 6]
Add columns to an array.
IDL> b = Reform((Indgen(4) + 1) * 4, 1, 4)
IDL> Print, [ b, a, b ]
4
8
12
16
1
1
1
1
1
1
1
1
1
1
1
1
IDL> Help, [ b, a, b ]
INT = Array[6, 4]
1
1
1
1
4
8
12
16
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
9
17
25
2
10
18
26
3
4
5
11 12 13
19 20 21
227 28 29
6
14
22
30
7
15
23
31
8
16
24
32
IDL> evenRowIndex = (Indgen(4/2) * 2) + 1
IDL> data[*, evenRowIndex] = Reverse(data[*, evenRowIndex])
IDL> Print, data
1
16
17
32
2
15
18
31
3
14
19
30
4
13
20
29
5
12
21
28
6
11
22
27
7
10
23
26
8
9
24
25
Histogram Magic
A histogram is used to count how many elements fall
into “bins”.
IDL> a = Indgen(10)
IDL> Print, Histogram(a, Binsize=5)
5
5
IDL> Print, Histogram(a)
1
1
1
1
1
1
1
1
IDL> data = Randomu(-3L, 100)
IDL> Print, Histogram(data, Binsize=0.20)
25
17
22
18
18
1
1
Histogram 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.
IDL> data=fix(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
IDL> h = Histogram(data, REVERSE_INDICES=ri)
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
Histogram 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 = iiiiiiiiiiiiiiiiiiioooooooooooooooooooooooooooooo
|-----------------||----------------------------|
|0
nh||nh+1
nh+total(h)|
The o-vector contains the data indices, in order, and the i-vector
just shows us where to go to get them.
Reverse_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 = iiiiiiiiiiiiiiiiiiioooooooooooooooooooooooooooooo
|-----------------||----------------------------|
|0
nh||nh+1
nh+total(h)|
IDL> Print, ri[ri[4]:ri[5]-1]
6
10
15
19
23
IDL> Print, data[ri[ri[4]:ri[5]-1]]
4
4
4
4
4
IDL> if ri[4] ne ri[5] then Print, data[ri[ri[4]:ri[5]-1]] else Print, 'No data in bin 4‘
4
4
4
4
4
IDL> if ri[8] ne ri[9] then Print, data[ri[ri[8]:ri[8]-1]] else Print, 'No data in bin 8'
No data in bin 8
Using Histogram
Find the intersection (common elements) of two vectors.
IDL> sd = -3L
IDL> a = Fix( Randomu(sd, 8) * 20 )
IDL> b = Fix( Randomu(sd, 8) * 20 )
IDL> Print, a, b
17
2
11
19
15
17
11
18
1
16
19
3
0
4
4
16
IDL> Print, Where(Histogram(a, OMin=om) gt 0 AND Histogram(b, Min=om) gt 0) + om
4
17
19
Find the union (elements in either vector) of two vectors.
IDL> Print, Where(Histogram([a,b], OMin = om)) + om
0
1
2
3
4
11
15
16
17
18
19
Using Histogram
Remove elements, listed in random order, from a vector.
IDL> vec = Randomu(sd,10)
IDL> remove = [3,7,2,8]
IDL> keep = Where(Histogram(remove,MIN=0,MAX=N_Elements(vec)-1) eq 0,cnt)
IDL> if cnt ne 0 then vec = vec[keep]
IDL> print,keep
0
1
4
5
6
9
Find the median value for each quartile of a data set.
IDL> data = Randomu(sd,100)*100
IDL> h = Histogram(data,Binsize=25, Reverse_Indices=ri)
IDL> med = FltArr(4)
IDL> for j=0L,3L do if ri[j+1] gt ri[j] then med[j] = Median(data[ri[ri[j]:ri[j+1]-1]])
IDL> print,med
15.2426
40.9219
63.8255
86.1637
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
Set up Window on PostScript Page
Set_Plot, ‘PS’
Device, XSize=xs, YSize=ys, XOffset=xoff, YOffset=yoff, /Landscape
Create a PostScript Window with
the Correct Aspect Ratio
Plot, data
keywords = PSWindow()
Help, keywords, /Structure
XSIZE
YSIZE
XOFFSET
YOFFSET
INCHES
PORTRAIT
LANDSCAPE
FLOAT
FLOAT
FLOAT
FLOAT
INT
INT
INT
thisDevice = !D.Name
Set_Plot, ‘PS’
Device, _Extra=keywords
Plot, data
Device, /Close_File
Set_Plot, thisDevice
5.95000
5.95000
1.27500
2.52500
1
1
0
Position Plots with POSITION
Always use Normal or Data coordinates
for device-independent placement of
text 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
Use 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
keywords = PSConfig(Cancel=cancelled)
IF cancelled THEN RETURN
thisDevice = !D.Name
Set_Plot, ‘PS’
Device, _Extra=keywords
Plot, data
Device, /Close
Set_Plot, thisDevice
PostScript Setup
Load Drawing Colors Properly
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’)
Use 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 PostScript
keyword.
Plot, data, Font=0
!P.Font = 0
Device, Set_Font=‘Arial*14’, /TT_Font
Plot, data, Font=1
XYOutS, 0.75, 0.8, ‘Position Plots Here’, /Normal, Font=1
Protect Device-Specific Code
IF (!D.Flags AND 256) NE 0 THEN $ ; Windows supported
Window, XSize=400, YSize=400
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’)
Other commands to protect:
• WSet, wid
• Device, Decomposed=0
Coyote’s
Guide to
IDL
Programming
David Fanning