-------------------------------------------------------------------- IDL HELP FILE -------------------------------------------------------------------- This file is intended as a quick reference for how to do some things in IDL. Obviously there are many things not listed here. Google is your friend. The vast majority of the time, if there's something you want to do, there's a way. That doesn't mean IDL won't make it difficult, but persevere. Alternatively, ask Wei-Chun as he is the master. Also sorry if this doesn't work. Update history: 07/26/2012 - MRB initially posted the file. -------------------------------------------------------------------- Plotting symbols: -------------------------------------------------------------------- psym = (discrete points) 1 = + 2 = * 3 = . 4 = diamond 5 = triangle 6 = square 7 = x 8 = user defined (see below) linestyle = (continuous lines and curves) 0 = Solid - default 1 = Dotted 2 = Dashed 3 = Dash Dot 4 = Dash Dot Dot 5 = Long Dash Or you can use plotsym to define your own points: plotsym, N [,size, /FILL] N is the symbol you want to use (see below) Size is size of the symbol. 1 is default. Can be non-integer. If /fill is used, the symbols will be filled. N values: 0 circle 1 down arrow 2 up arrow 3 star 4 triangle 5 upside down triangle 6 left pointing arrow 7 right pointing arrow (6-7 switched?) 8 square http://idlastro.gsfc.nasa.gov/ftp/pro/plot/plotsym.pro When using plotsym, put the plotsym command on its own line. Then under plot (or oplot, etc) use psym = 8. -------------------------------------------------------------------- Plotting lines. It likely works for other curves as well. -------------------------------------------------------------------- Two ways to plot straight lines easily: Make a large array of increasing values. Then you can scale and shift it as you need. For example, to plot on the range [-0.5,0.5] do: num = findgen(1000) ;large array num = num/1000 ; makes length = 1, the length of the interval num = num - 0.5 ;shifts the array so that the minimum value is -0.5, maximum is 0.5 Then treat the array as you would "x" in an equation. You can square it, multiply by constants, etc. Once you're done, simply plot (or oplot) the array with the desired linestyle. Second way, only works for straight lines: given endpoints (x1,y1) and (x2,y2) oplot, [x1,x2],[y1,y2],linestyle=whatever -------------------------------------------------------------------- .ps to .jpg conversion (and .eps) -------------------------------------------------------------------- convert -rotate 0 -geometry 100% -density 400 file.ps file.jpg -------------------------------------------------------------------- How to plot histograms: -------------------------------------------------------------------- data is an array with your data, shockingly. plot, Histogram(data, binsize = 30.0), psym = 10 OR plothist, data, bin = 2 googling plothist will turn up more information about stuff you can use with that command (such as fills and stuff) -------------------------------------------------------------------- How to output plot to postscript file: -------------------------------------------------------------------- .ps files: !P.FONT = 0 set_plot, 'ps' device,SET_FONT='Palatino-Roman', filename = 'FILENAME.ps',/landscape [plotting stuff here] device,/close set_plot, 'x' (you can use different fonts or none at all) ENCAPSULATED (.eps) files: !P.FONT = 0 set_plot, 'ps' device, /encapsulated, SET_FONT='Palatino-Roman', filename = 'FILENAME.eps',/landscape [plotting stuff here] device,/close set_plot, 'x' -------------------------------------------------------------------- How to make a file interactive: -------------------------------------------------------------------- alf is simply a dummy variable. alf = 0 while (alf eq 0) do begin cursor, x, y, /down [here you will put the plotting code. Instead of the line that actually plots, however, have something like: ] if (rmj lt (x + .1)) and (rmj gt (x - .1)) and (hr2 lt (y + .1)) and (hr2 gt (y - .1)) then begin [this looks at variables rmj and hr2 and sees how close they are to the x and y positions, respectively. If they're close enough, the if statement code is excecuted.] endif i = i + 1 endwhile [to have it so that clicking in a certain area exits the window, use a line like: ] if (x lt -2) then begin alf = 64 endif [this means that if you click where x < -2, alf is set to a nonzero number, meaning the while loop (and hence, the interactive stuff) will exit and you can delete the window/do whatever] endwhile -------------------------------------------------------------------- Plotting a legend: -------------------------------------------------------------------- legend,['diamond','asterisk','square'],psym=[4,2,6]. /right_legend produces: ----------------- | | | <> diamond | | * asterisk | | [] square | | | ----------------- on the upper right part of the plotting area. As far as I know, you can only use one user-defined symbol in a legend. If you have more than one, plotting a dummy point and using xyouts may be a better solution. more info: http://astro.uni-tuebingen.de/software/idl/astrolib/plot/legend.html -------------------------------------------------------------------- Text formatting in axis titles: -------------------------------------------------------------------- http://www.stsci.edu/hst/training/events/IDLTopics/SSD98IDL/IDL_plotting2.html#Adding%20Text%20to%20Line%20Plots http://www.iac.es/sieinvens/SINFIN/CursoIDL/chap3.php -------------------------------------------------------------------- Loop Syntax: -------------------------------------------------------------------- for loops: for j = 0,1000 do begin stuff endfor while loops: i = 0 while (i lt 1000) do begin stuff i = i + 1 endwhile if statements: if (x lt 4) then begin: stuff endif This is just the basics. There are plenty of things to do with each loop type, so experimenting and exploring is encouraged. -------------------------------------------------------------------- Plotting spectra in IDL: -------------------------------------------------------------------- file.fits is the 1-d reduced spectrum frame1 = readfits('file.fits', header1) plot, frame1[*,0] there's a lot more you can do, though. For example, reading the lines in the header defining the starting wavelength and interval can produce a wavelength array, so you can plot the wavelength array versus the flux. You can also manipulate the spectrum (normalizing, comparing to other spectra, etc.). -------------------------------------------------------------------- LABELS, especially for line legends... -------------------------------------------------------------------- To make a label, use: xyouts, xpos, ypos, 'WORDS', charsize = SIZE To make a linestyle legend, just draw a little line segment of that style and put an xyouts next to it labeling it. Easy peasy. Similarly for plots using multiple user-defined symbols, you can make a dummy point and put some text next to it. -------------------------------------------------------------------- COLOR Plotting -------------------------------------------------------------------- Have fsc_color.pro in the directory. You can find it under /nfs/morgan/users/boyd/General.Work/ Define a colors array: colors = FSC_Color( ['Black', 'Orange', 'Green','Blue','Red'] ) More colors are available. Read the program itself to see a full list. Then, when plotting, do something like: oplot, [x],[y], color = colors[N], psym = s Where N is the position in the colors array of the desired color. Remember that array counting starts at zero, not one. -------------------------------------------------------------------- Running linux commands in an IDL program -------------------------------------------------------------------- spawn is your friend. for example, the following command: spawn, 'ls ~ > zzztempfile' will run the command in quotes as if you had typed it in the terminal. You can put any command you like in the quotes. -------------------------------------------------------------------- Basic file I/O -------------------------------------------------------------------- To read a file: openr, 1, FILENAME 1 is the number of file that's open. if you want to open another file, use: openr, 2, FILENAME2 if you've closed 1 (see below) then you can open a different file and call it 1, but it's probably best not to do that to save confusion. to actually read a line from a file, use: line = '' readf, 1, line This will read it in as a string. I'm not 100% sure how necessary the line='' line is, but it works for sure. I've found it's best to have the reading in a while loop using: while (eof(1) eq 0) do begin readf, 1, line STUFF endwhile where eof(1) says whether or not the end of file 1 has been encountered. To write to a file: open it as writable: openw, 1, FILENAME printf, 1, WHATEVER To close a file: close,1 There's also a command "openu" which does something, I think. I've found that it's useful to close a file before you open it, even if it hasn't been opened previously: close,1 openr,1,FILE This won't make the program barf, but it WILL prevent it from barfing if it crashes before closing file 1 and you have to re-run it after correcting stuff. As far as the computer is concerned the file is still open, so you're closing it instead of trying to open it again. for .fits files, you can use: array = readfits(file.fits, header) This will read in the .fits file data as a float array and the header as a string array. -------------------------------------------------------------------- Misc. commands -------------------------------------------------------------------- To get the number of elements in an array: aNumber = n_elements(array) to read in keyboard input: read, prompt = 'this reads an input as a nubmer?' or var = '' read, var, prompt = 'this reads an input as a string?' To pull out parts of a string: part = strmid(line, start, length) where line is the string you want part of, start is the character number you want to start with, and length is the length of the bit you want to pull out. if length isn't present, you'll just pull out the rest of the line.