coyote’s guide to idl programming

66
Coyote’s Guide to IDL Programming David Fanning

Upload: salvador-spencer

Post on 30-Dec-2015

40 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Coyote’s Guide to IDL Programming

Coyote’sGuide toIDLProgramming

David Fanning

Page 2: Coyote’s Guide to IDL Programming

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

Page 3: Coyote’s Guide to IDL Programming

Six Essential IDL Resources

• Coyote’s Guide to IDL Programming

• IDL Newsgroupcomp.lang.idl-pvwave

http://www.dfanning.com

• Curve Fitting and Mathematicshttp://cow.physics.wisc.edu/~craigm/idl/idl.html

• IDL Emacs Mode (IDLWAVE)http://www.idlwave.org/

• NASA Astronomy Libraryhttp://idlastro.gsfc.nasa.gov/homepage.html

• JHUAPL IDL Libraryhttp://fermi.jhuapl.edu/s1r/idl/s1rlib/local_idl.html

Page 4: Coyote’s Guide to IDL Programming

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

Page 5: Coyote’s Guide to IDL Programming

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

Page 6: Coyote’s Guide to IDL Programming

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

Page 7: Coyote’s Guide to IDL Programming

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)

Page 8: Coyote’s Guide to IDL Programming

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

Page 9: Coyote’s Guide to IDL Programming

Constraining Parameters

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

Fix the constant parameter at 1000.

Page 10: Coyote’s Guide to IDL Programming

Constraining Parameters

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

Limit the mean to a minimum of 2.3.

Page 11: Coyote’s Guide to IDL Programming

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

Page 12: Coyote’s Guide to IDL Programming

Six Essential IDL Resources

• Coyote’s Guide to IDL Programming

• IDL Newsgroupcomp.lang.idl-pvwave

www.dfanning.com

• Curve Fitting and Mathematicshttp://cow.physics.wisc.edu/~craigm/idl/idl.html

• IDL Emacs Mode (IDLWAVE)http://www.idlwave.org/

• NASA Astronomy Libraryhttp://idlastro.gsfc.nasa.gov/homepage.html

• JHUAPL IDL Libraryhttp://fermi.jhuapl.edu/s1r/idl/s1rlib/local_idl.html

Page 13: Coyote’s Guide to IDL Programming

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

Page 14: Coyote’s Guide to IDL Programming

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

Page 15: Coyote’s Guide to IDL Programming

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

Page 16: Coyote’s Guide to IDL Programming

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

Page 17: Coyote’s Guide to IDL Programming

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

Page 18: Coyote’s Guide to IDL Programming

Steel Blue (70,130,180)

1 0 1 1 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 = 11829830Lredgreenblue

IDL> Device, Decomposed=1

IDL> Print, 70L + (130L * 2L^8) + (180L * 2L^16)

11829830

IDL> Plot, data, Color=11829830L, Background=‘ffffff’xL

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 = 255Lredgreenblue

IDL> Plot, data, Color=255

Page 19: Coyote’s Guide to IDL Programming

01234

70..

255

01234.

130.

255

01234..

180255

01234...

255

Index R G B

132522702...

45

674487

13038...

255

80178223180177

.

.

.128

01234...

255

Index R G B

24-Bit Color 8-Bit Color

Plot, data, Color= 11829830L Plot, data, Color=3

Color Decomposition ON Color Decomposition OFF

Page 20: Coyote’s Guide to IDL Programming

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

Page 21: Coyote’s Guide to IDL Programming

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

Page 22: Coyote’s Guide to IDL Programming

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’)

Page 23: Coyote’s Guide to IDL Programming

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()

Page 24: Coyote’s Guide to IDL Programming

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

Page 25: Coyote’s Guide to IDL Programming

Position Plots with POSITION

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

Always use Normal or Data coordinatesfor device-independent placement oftext and graphics.

Page 26: Coyote’s Guide to IDL Programming

Position Plots with POSITION

IDL> Plot, data

IDL> Plot, data, Position=Aspect(1.0)

Page 27: Coyote’s Guide to IDL Programming

Find a Positionable TV Command

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

•TVImage•ImgDisp•PlotImage

Page 28: Coyote’s Guide to IDL Programming

Avoid Auto-scaling Axes

Plot, data, XRange=[23.4, 78]

Plot, data, XRange=[23.4, 78], $XStyle=1

Page 29: Coyote’s Guide to IDL Programming

Avoid Auto-scaling Axes

Contour, image, NLevels=12

Contour, image, NLevels=12 $XStyle=1, YStyle=1

Page 30: Coyote’s Guide to IDL Programming

Plotting Symbols

Plot, data, PSym=2

Plot, data, PSym=-2

1 Plus sign (+)

2 Asterisk (*)

3 Period (.)

4 Diamond

5 Triangle

6 Square

7 X

8 User-defined.

Page 31: Coyote’s Guide to IDL Programming

Create Plotting Symbols(USERSYM)

Filled Circlephi = Findgen(32) * (!PI * 2 / 32.)

phi = [ phi, phi[0] ]

UserSym, Cos(phi), Sin(phi), /Fill

Plot, data, PSym=8, Symsize=1.25

Filled TrianglesUserSym, [ -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.

Page 32: Coyote’s Guide to IDL Programming

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

Page 33: Coyote’s Guide to IDL Programming

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

Page 34: Coyote’s Guide to IDL Programming

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

Page 35: Coyote’s Guide to IDL Programming

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

Page 36: Coyote’s Guide to IDL Programming

Avoid FOR Loops if PossibleIDL> array = Indgen(3,4)

IDL> Print, array 0 1 2

3 4 5

6 7 8

9 10 11

Fortran Way:

For j=0,2 Do Begin

For k=0,3 Do Begin

array(j,k) = array(j,k) * 3

Endfor

Endfor

Multiply array by 3IDL Way:

array = array * 3

Page 37: Coyote’s Guide to IDL Programming

Array OperatorsSet 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

Page 38: Coyote’s Guide to IDL Programming

WHERE much faster than IFSet 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

Page 39: Coyote’s Guide to IDL Programming

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

Page 40: Coyote’s Guide to IDL Programming

Convert 1D Indices to 2D Indicesarray = 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

Page 41: Coyote’s Guide to IDL Programming

Convert 1D Indices to 2D Indicesindices = 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

Page 42: Coyote’s Guide to IDL Programming

Dimensional JugglingMultiply each column of array by a vector, b.

Fortran Way:

var = IntArr(3,4)

For j=0,2 Do Begin

var[j,*] = array[j,*] * b

EndFor

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

Page 43: Coyote’s Guide to IDL Programming

Dimensional JugglingMultiply 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

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

Page 44: Coyote’s Guide to IDL Programming

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

Page 45: Coyote’s Guide to IDL Programming

Array ConcatenationIDL> 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 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

Page 46: Coyote’s Guide to IDL Programming

Array ConcatenationIDL> 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]

Page 47: Coyote’s Guide to IDL Programming

Array Concatenation

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]

Add rows to an array.

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]

Add columns to an array.

Page 48: Coyote’s Guide to IDL Programming

Array Concatenation

IDL> image24 = [ [ [image_1] ], [ [image_2] ], [ [image_3] ] ]

IDL> Help, image24 INT = Array[400, 600, 3]

Create a true-color image

Convert band interleaved image to pixel interleaved

IDL> image24 = Transpose(image24,[2,0,1])

IDL> Help, image24 INT = Array[3, 400, 600]

Page 49: Coyote’s Guide to IDL Programming

Index Manipulation

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

Quick look at two images simultaneously.

Page 50: Coyote’s Guide to IDL Programming

Index Manipulation

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

Reverse the even rows in a 2D array.

Page 51: Coyote’s Guide to IDL Programming

Histogram Magic

IDL> a = Indgen(10)

IDL> Print, Histogram(a, Binsize=5) 5 5

IDL> Print, Histogram(a) 1 1 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

A histogram is used to count how many elements fall into “bins”.

Page 52: Coyote’s Guide to IDL Programming

Histogram MagicIf binning data was all Histogram did, it would beuseful, 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 3IDL> 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

Page 53: Coyote’s Guide to IDL Programming

Histogram MagicHistogram 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” vectorri = 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.

Page 54: Coyote’s Guide to IDL Programming

| | | | |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

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

Reverse_Indices looks ugly, but works great!

The “I” vector The “O” vectorri = iiiiiiiiiiiiiiiiiiioooooooooooooooooooooooooooooo |-----------------||----------------------------| |0 nh||nh+1 nh+total(h)|

Page 55: Coyote’s Guide to IDL Programming

Using Histogram

Find the intersection (common elements) of two vectors.

IDL> sd = -3LIDL> a = Fix( Randomu(sd, 8) * 20 )IDL> b = Fix( Randomu(sd, 8) * 20 )IDL> Print, a, b 17 11 15 11 1 19 0 4 2 19 17 18 16 3 4 16IDL> 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

Page 56: Coyote’s Guide to IDL Programming

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)*100IDL> 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

Page 57: Coyote’s Guide to IDL Programming

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

Page 58: Coyote’s Guide to IDL Programming

Set up Window on PostScript Page

Set_Plot, ‘PS’Device, XSize=xs, YSize=ys, XOffset=xoff, YOffset=yoff, /Landscape

Page 59: Coyote’s Guide to IDL Programming

Create 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, _Extra=keywords

Plot, data

Device, /Close_File

Set_Plot, thisDevice

Page 60: Coyote’s Guide to IDL Programming

Position Plots with POSITION

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

Always use Normal or Data coordinatesfor device-independent placement oftext and graphics.

Page 61: Coyote’s Guide to IDL Programming

Use a Positionable TV Command

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

•TVImage•ImgDisp•PlotImage

Page 62: Coyote’s Guide to IDL Programming

PostScript Setup

keywords = PSConfig(Cancel=cancelled)

IF cancelled THEN RETURN

thisDevice = !D.Name

Set_Plot, ‘PS’

Device, _Extra=keywords

Plot, data

Device, /Close

Set_Plot, thisDevice

Page 63: Coyote’s Guide to IDL Programming

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’)

Page 64: Coyote’s Guide to IDL Programming

Use PostScript or True-Type Fonts

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

You will have better looking plots if you use PostScript or True-Type fonts. If youuse TeXtoIDL be sure to set the PostScriptkeyword.

Page 65: Coyote’s Guide to IDL Programming

Protect Device-Specific Code

IF (!D.Flags AND 256) NE 0 THEN $ ; Windows supportedWindow, 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

Page 66: Coyote’s Guide to IDL Programming

Coyote’sGuide toIDLProgramming

David Fanning