introduction to idl dealing with data

67
Introduction to IDL Dealing with Data Week 2 Rachel McCrary 3-31-2009

Upload: alair

Post on 02-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Introduction to IDL Dealing with Data. Week 2 Rachel McCrary 3-31-2009. Goals for today!. Part 1 How to open a file for reading and writing How to read/write ASCII or formatted data How to read/write unformatted or binary data How to read NetCDF data files Part 2 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to IDL Dealing with Data

Introduction to IDLDealing with Data

Week 2Rachel McCrary

3-31-2009

Page 2: Introduction to IDL Dealing with Data

Goals for today!

• Part 1- How to open a file for reading and writing

- How to read/write ASCII or formatted data

- How to read/write unformatted or binary data

- How to read NetCDF data files

• Part 2- How to plot to the display window

- How to use the plot command and modify it

- How to plot multiple data sets, multiple axes, and multiple plots on the same page

- How to add color to your plots

Page 3: Introduction to IDL Dealing with Data

Opening Files

• All input/output in IDL is done with logical unit numbers.

• A logical unit number is a pipe or conduit that is connected between IDL and the data file that you want to read from or write to.

• There are three types of open commands

• openr - Open a file for Reading

• openw - Open a file for Writing

• openu - Open a file for Updating

Page 4: Introduction to IDL Dealing with Data

Opening Files• The syntax of these three commands is the same: name

of the command, logical unit number (lun), file

openw, 20, ‘filename.txt’

• You can use logical a unit numbers directly (and manage them yourself) ... as in the above example.

• You can also have IDL manage logical unit numbers by using /GET_LUN

openr, lun, ‘filename.txt’, /GET_LUN

• When you are finished with the logical unit numbers you can:

• close it using the close command (in the first example) - close, 20

• or free the lun using FREE_LUN - FREE_LUN, lun

Page 5: Introduction to IDL Dealing with Data

Reading/Writing Formatted Data

• IDL distinguishes between two types of formatted files with regard to reading and writing data:

• Free File Format: uses either commas or whitespace (tabs and spaces) to distinguish each element in the file. It is more informal than an explicit file format.

• An explicit format file is formatted according to rules specified in a format statement. The IDL format statement is similar to format statements you might use in FORTRAN.

Page 6: Introduction to IDL Dealing with Data

Writing a Free Format File• Writing a free format file in IDL is easy ... you

just use the printf command to write the variables into a file. (This is the same as using print to write variables to the display)

• see example: “write_free_format.pro”

• IDL puts white space between each element of the array variables and starts each variable on its own line.

• IDL uses the 80 column width default, you can use can change this using the width keyword in the openw command

Page 7: Introduction to IDL Dealing with Data

Reading a Free Format File

• Many ASCII files are free format. In IDL you want to use the readf command to read free format data from a file.

• IDL uses 7 (fairly simple) rules to read free format data

Page 8: Introduction to IDL Dealing with Data

Rule #11.If reading into a string variable, all characters remaining on

the current line are read into the variable.

• Example: try to read in the first line of the file we just created called “test_free_format.txt” with the following

IDL> OPENR, lun, 'test_free_format.txt', /GET_LUN IDL> header = ''IDL> READF, lun, headerIDL> print, headerTest data file.

• You can then read the first two lines (or any number of lines) in a similar way using the following

IDL> header = strarr(2)IDL> READF, lun, headerIDL> print, headerTest data file. Created: Sun Mar 29 13:35:52 2009

Page 9: Introduction to IDL Dealing with Data

Rule #2

2.Input data must be separated by commas or whitespace (spaces or tabs).

• This is exactly the kind of data that is in the test_free_format.txt file (after the header)!

Page 10: Introduction to IDL Dealing with Data

Rules #3, #4 and #53.Input is performed on scalar variables, Arrays and

structures are treated as a collection of scalar variables.

• Translation - if the variable you are reading into contains 10 elements, then IDL will try to read 10 separate values from the data file. It will then use rules 4 & 5 to determine where those values are located in the file.

4.If the current input line is empty, and there are still variables left requiring input, read another line.

5.If the current input line is not empty, but there are no variables left requiring input, ignore the remainder of the line.

Page 11: Introduction to IDL Dealing with Data

Rule #4 & #5• Try the following:

IDL> data = fltarr(8)IDL> READF, lun, dataIDL> print, data0.00000 1.00000 2.00000 3.00000 4.00000 5.000006.00000 7.00000

• IDL reads 8 separate data values from the file. When it got to the end of the first row, it went to the second row (rule 4) because more data remained to be read. IDL read to the middle of the second row.

• Now rule 5 comes into play. If you read more data, you will start on the third data line, because the rest of the second line is ignored.

IDL> vector3 = fltarr(3)IDL> READF, lun, vector3IDL> print, vector312.0000 13.0000 14.0000

Page 12: Introduction to IDL Dealing with Data

Rule #66.Make every effort to convert data into the data type expected

by the variable.

• To see what this means, read the 4th and 5th lines into a string array, so that the file pointer is positioned at the sixth data line (starts 33.6000). Try this:

IDL> dummy = strarr(2)IDL> READF, lun, dummy

IDL> ints = intarr(2)IDL> READF, lun, intsIDL> print, ints 33 77

• Now suppose you want to read two integer values. IDL makes every effort to convert the data (floating point values in this case) into integers.

• See how the floating point values have simply been truncated.

Page 13: Introduction to IDL Dealing with Data

Rule #77.Complex data must have a real part and an imaginary part

separated by a comma and enclosed in parentheses. If only a single variable is provided, it is considered the real value and the imaginary value is set to 0.

IDL> value = complexarr(2)IDL> read, value: (3,4): (4.4,25.5)IDL> print, value( 3.00000, 4.00000)( 4.40000, 25.5000)

Page 14: Introduction to IDL Dealing with Data

Always make sure...

• When you are done with a file, make sure that you close it! To do this type:

IDL> FREE_LUN, lun

Page 15: Introduction to IDL Dealing with Data

• Lets start by reading in the file we just created “test_free_format.txt”

• example: read_free_format.pro

• notice that the data variable is a 5x5 array in this case. It was put into the file as a 25-element vector. Reading the data this way is equivalent to reading a 25 element vector and reformatting the vector into a 5x5 array.

• Remember that data in IDL is stored in row order.

Reading a Free Format File

Page 16: Introduction to IDL Dealing with Data

Writing a column-format data file

• It is not uncommon to see data stored in files in columns. It is a good idea to know how to read/write this sort of data in IDL ... but IDL has some surprises for you :)

• Example: write_column_ascii.pro

• As you can see, the first way does not give us what we want.

• Must use a loop to get the desired column format in our output file.

Page 17: Introduction to IDL Dealing with Data

Reading ASCII72469 DNR Denver Observations at 12Z 15 Mar 2009----------------------------------------------------------------------------- PRES HGHT TEMP DWPT RELH MIXR DRCT SKNT THTA THTE THTV hPa m C C % g/kg deg knot K K K ----------------------------------------------------------------------------- 1000.0 44 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 925.0 705 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 850.0 1413 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 830.0 1625 0.6 -15.4 29 1.40 165 4 288.7 293.1 289.0 824.0 1683 2.8 -16.2 23 1.32 209 4 291.6 295.8 291.9 819.0 1732 6.0 -15.0 21 1.46 247 4 295.5 300.2 295.8 816.0 1761 6.6 -15.4 19 1.42 269 4 296.5 301.1 296.7 809.2 1829 6.8 -18.0 15 1.15 320 4 297.4 301.2 297.6 804.0 1882 7.0 -20.0 13 0.98 319 5 298.2 301.4 298.3 785.0 2076 6.0 -19.0 15 1.09 316 10 299.1 302.7 299.3 779.3 2134 5.5 -19.0 15 1.10 315 12 299.3 302.9 299.5 750.3 2438 3.1 -19.0 18 1.14 300 15 299.9 303.7 300.1 726.0 2702 1.0 -19.0 21 1.18 291 12 300.4 304.3 300.6 722.2 2743 0.8 -19.9 20 1.10 290 12 300.7 304.3 300.9 700.0 2991 -0.3 -25.3 13 0.70 250 9 302.1 304.5 302.2 692.0 3083 -0.7 -28.7 10 0.52 244 9 302.7 304.5 302.8 643.2 3658 -5.9 -30.1 13 0.49 205 10 303.1 304.9 303.2

filename - “sounding.txt”

Page 18: Introduction to IDL Dealing with Data

sounding.txt - info

• File info:

• 5 lines of header

• missing data filled with -9999

• 11 columns (floating pt. and integers)

• 117 rows of data

Page 19: Introduction to IDL Dealing with Data

Read as a “free-format” file• The “quick and dirty” way of reading the file

“sounding.txt” into IDL is to treat it as a “free-format” data file, where every element is separated by a space.

• See example: “read_free_format_sounding.pro”

- treat all the data in the file as floating point data

- read all of the data into an 11 x 117 floating point data array

- you will have to keep track of which column corresponds with which variable.

- This will not work if there are character flags in your data file

Page 20: Introduction to IDL Dealing with Data

Reading a Column-Formatted Data file

• What if you want to read the file “sounding.txt” and maintain the variable type of each column, and put the information into different variables (not just one block)?

• You could try what his shown in “read_column_wrong.pro”

• This will not give an error, but nothing is read into the variables.

• Why doesn’t this work? - apparently there is a “rule” in idl that you cannot read into a subscripted variable.

• Why not? - IDL passes subscripted variables into IDL procedures like readf by value and not by reference. Data that is passed by value cannot be changed inside of the called routine, since the routine has a copy of the data and not a pointer to the data itself.

Page 21: Introduction to IDL Dealing with Data

Reading a Column-Formatted Data file

• One way to solve this problem is to read the data into a temporary variables in the loop.

• See example: “read_column_right.pro”

• The only time this might be a problem is if you are trying to read a whole lot of data through the loop (remember loops slow things down in IDL).

• Remember that loops slow things down in IDL, so it might be better to read all the data in at once into an 11 x 17 array and then pull the vectors out of the larger array using array processing commands. (Again, will not work if there are characters in your data file)

• See example: “read_column_other.pro”

Page 22: Introduction to IDL Dealing with Data

Explicit File Format

• To read/write an explicit file format, use the same readf and printf commands that were used for free format files, except now the file format is explicitly stated with the format keyword.

• Example: “write_format_ascii.pro”

Format Specifier

What do they do

I Specifies integer data

F Specifies floating point data

D Specifies double precision data

E Specifies floating point data using scientific notation

A Specifies character data

nx Skip n character spaces

Page 23: Introduction to IDL Dealing with Data

Example - I

• Specifies integer data. Print the vector out as a two-digit integers, five numbers on each line, each number separated by two spaces

IDL> data = findgen(20)IDL> fmt = '(5(I2,2x),/)'IDL> print, data, format=fmt 0 1 2 3 4 5 6 7 8 910 11 12 13 1415 16 17 18 19

Page 24: Introduction to IDL Dealing with Data

Example - F• Specifies floating point data. Write the data out as

floating values with two digits to the right of the decimal place, one value per line. (Be sure to include enough space in the number for the decimal point itself.)

IDL> fmt = '(F5.2)'IDL> print, data, format = fmt 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.0010.00etc.

Page 25: Introduction to IDL Dealing with Data

Example - D

• Specifies double precision data. Write out five numbers per line, four spaces between each number, in double precision values with 10 digits to the right of the decimal point.

IDL> fmt = '(5(D13.10,4x))'IDL> print, data, format=fmt 0.0000000000 1.0000000000 2.0000000000 3.0000000000 4.0000000000 5.0000000000 6.0000000000 7.0000000000 8.0000000000 9.000000000010.0000000000 11.0000000000 12.0000000000 13.0000000000 14.000000000015.0000000000 16.0000000000 17.0000000000 18.0000000000 19.0000000000

Page 26: Introduction to IDL Dealing with Data

Reading/Writing Unformatted Data

• Unformatted or binary data is much more compact than formatted data and is often used with large data files.

• To read/write binary data use the readu and the writeu commands

• Syntax for writing

• Syntax for reading:

IDL> x = findgen(10000)IDL> openw, lun, 'unformat_data.txt', /get_lunIDL> writeu, lun, xIDL> free_lun, lun

IDL> x = findgen(10000)IDL> openr, lun, 'unformat_data.txt', /get_lunIDL> readu, lun, xIDL> free_lun, lun

Page 27: Introduction to IDL Dealing with Data

Reading netCDF files• netCDF (network Common Data Form) is designed to be

simple and flexible.

• The basic building blocks are:

• Variables are scalars or multidimensional arrays (supports string, byte, int, long, float and double)

• Attributes contain supplementary information about a single variable or an entire file. Variable attributes include: units, valid range, scaling factor. Global attributes contain information about the file i.e. creation date. Attributes are scalars or 1-D arrays.

• Dimensions are long scalars that record the size of one or more variables

Page 28: Introduction to IDL Dealing with Data

What’s in a netCDF File?

• use ncdump unix command or

• testreadnc.pro

• Try on the file “air.mon.mean.nc”

• testreadnc prints info to the file “file_info.txt”

Page 29: Introduction to IDL Dealing with Data

Reading a Variable

• File is opened with the ncdf_open command, returning the file identifier “nc”

• the ncdf_varget function is used to read in the entire contents of the variable

• the file was then closed using ncdf_close

• See example: read_netcdf.pro

; open the netcdf filenc = ncdf_open('/Users/rachel/idl_course_week2/air.mon.mean.nc') ; Extract the variables of interestncdf_varget, nc, 'lat', latncdf_varget, nc, 'lon', lonncdf_varget, nc, 'level', level

; close the netcdf file ncdf_close, nc

Page 30: Introduction to IDL Dealing with Data

Reading an Attribute

• In this example, we extract the attributes from the variable “air” called “add_offset” and “scale_factor” using the ncdf_attget command.

• See example: read_netcdf.pro

; open the netcdf filenc = ncdf_open('/Users/rachel/idl_course_week2/air.mon.mean.nc') ; Extract the variables of interestncdf_varget, nc, 'air', temperature

; Extract the attributes of interestncdf_attget, nc, 'air', 'add_offset', offsetncdf_attget, nc, 'air', 'scale_factor', scale

; close the netcdf file ncdf_close, nc

Page 31: Introduction to IDL Dealing with Data

Some netCDF routinesName Purpose

ncdf_open() Open a netCDF file

ncdf_close() Close a netCDF file

ncdf_varid() Return a variable identifier

ncdf_varget() Read a variable

ncdf_attget() Read an attribute

ncdf_inquire() Return file information

ncdf_varinq() Return variable information

ncdf_attname() Return an attribute name

ncdf_create() Create a netCDF file

ncdf_dimdef() Create a dimension

ncdf_vardef() Create a variable

ncdf_attput Write attribute data

ncdf_control Begin or end define mode

ncdf_varput Write variable data

Page 32: Introduction to IDL Dealing with Data

Simple Graphical Displays

• The “bread and butter” of scientific analysis is the ability to see your data as simple line plots, contour plots, and surface plots.

• The quickest way to visualize data graphically in IDL is to plot to the display window.

• This is not the way you will create “publishable” graphics, but it is a good way to start looking at your data and it is a good way to learn how to display your data using the plot command.

Page 33: Introduction to IDL Dealing with Data

A few things you should know ....

• IDL supports a system known as “Direct Graphics” which is built to a “device” - oriented model.

• What does this mean?? - you select a particular graphics device (screen, printer, PostScript) and then draw graphics on that device.

• This also means that you will have to switch the device to change how you visualize data.

Page 34: Introduction to IDL Dealing with Data

Graphics• The set_plot procedure selects a named graphics

device.

• All subsequent graphics output is sent to the selected graphics device until another set_plot call is made or the IDL session ends

• set_plot, device

• ‘X’, ‘WIN’, ‘PS’

• Default is the screen display (set_plot, ‘x’)

• The following would then display a plot to the screen: IDL> plot, indgen(10)

Page 35: Introduction to IDL Dealing with Data

A few things you might notice ...

• IDL’s default setting is to plot things with a black background using white font. This can be quite annoying at times and I will teach you how to change that in a bit.

• The window is labeled “IDL 0”

• We plotted 1 vector, but got an x vs. y plot.

Page 36: Introduction to IDL Dealing with Data

Window Options• Graphics window can be created directly using the window

command or indirectly by issuing a graphics display command when no window is open. IDL> window

• The title bar of the window has a number in it (0 in this case) - this is the window’s graphics window number index.

• You can open a new window with a graphics window index number of 1 by doing the following: IDL> window, 1

• You are allowed up to 128 different windows (but boy that would be confusing)

• IDL will assign window index numbers for windows 32-127 buy using the /free keyword: IDL> window, /free

• If you don’t explicitly open a new window, IDL will plot over whatever is on the current window.

Page 37: Introduction to IDL Dealing with Data

Window options• There are other options for setting,

deleting, and clearing windows. Try the following:

- Open two windows (indexed 1 and 2)

- Plot something in the current window (window 2)

- Plot something in window 1 (using wset)

- delete window 2

- erase the contents of window 1

Name Purpose

window Create a new window

wset Makes an existing window the current window

wdelete Delete an existing window

wshow Expose, hide or iconify an existing window

erase Erase the contents of an existing window

IDL> window, 1IDL> window, 2IDL> plot, indgen(10)IDL> wset, 1IDL> plot, sin(findgen(200))IDL> wdelete, 2IDL> erase, 1

Page 38: Introduction to IDL Dealing with Data

Window Options• You can also change the position and size of a

graphics window.

• Use xsize and ysize to change the size of the window (these are in pixels)

• Use xpos and ypos to relocate the position of the graphics window. Windows are positioned on the display with respect to the upper left-hand corner of the display in pixel or device coordinates.

IDL> window, 1, xsize=200, ysize = 300

IDL> window, 2, xpos=75, ypos=150

Page 39: Introduction to IDL Dealing with Data

Creating Plots

• The plot procedure draws graphs of vector arguments. It can be used to create line graphs, scatter plots, barplots, and even polar plots.

• The plot command can be modified with over 50 different keywords!

Page 40: Introduction to IDL Dealing with Data

Plotting a vector• IDL will try to plot as nice a plot as it possibly can

with as little information as it has.

• Try plotting: IDL> plot, sin(findgen(100)*.2)

• In this case the x or horizontal axis is labeled from 0-100, corresponding to the number of elements in the vector. The y or vertical axis is labeled with the data coordinates (this is the dependent data axis)

• For the following see: example_line_plot.pro, and example_line_plot2.pro

Page 41: Introduction to IDL Dealing with Data

Plotting x vs. y

• Most of the time a line plot displays one data set (the independent data) plotted against a second data set (the dependent data).

• For example:

• x represents radian values extending from 0 - pi and y is the sine (x)

IDL> x = FINDGEN(21)*.1*!piIDL> y = sin(x)IDL> plot, x, y

Page 42: Introduction to IDL Dealing with Data

Customize Graphics Plots• To label the x-axis, y-axis, and title use: xtitle, ytitle, and title keywords,

respectively.

• By default, the plot title is 1.25 times larger than the x and y axes labels - there are a number of ways to change this:

• To change the size of all the plot annotations use the charsize keyword.

• To change the character size of each individual axis use [XYZ]charsize.

IDL> plot, x, y, xtitle = 'Radians', ytitle = 'Radians', $title = 'Sine Wave'

IDL> plot, x, y, xtitle = 'Radians', ytitle = 'Radians', $title = 'Sine Wave', charsize = 1.5

IDL> plot, x, y, xtitle = 'Radians', ytitle = 'Radians', $title = 'Sine Wave', xcharsize = 2, ycharsize = 3

Page 43: Introduction to IDL Dealing with Data

Modify line styles and thickness

• Use the linestyle keyword to plot the data with a different line style. For example to get a dashed line (instead of a solid line) use:

Index Line Style

0 Solid

1 Dotted

2 Dashed

3 Dash Dot

4 Dash Dot Dot

5 Long Dash

IDL> plot, x, y, linestyle = 2

• Use the thick keyword to change the thickness of the line plots. For example if you want to plot displayed with a dashed line that is three times thicker than normal try:

IDL> plot, x, y, linestyle = 2, thick = 3

Page 44: Introduction to IDL Dealing with Data

Symbols• You can also plot your data using

symbols instead of lines. Like the linestyle keyword, similar index numbers exist to allow you to choose different symbols.

• For example you can draw the plot with asterisks by setting the psym keyword to 2:

Index Symbol

0 No symbol

1 Plus sign

2 Asterisk

3 Period

4 Diamond

5 Triangle

6 Square

7 X

• You can also connect your plot symbols with lines by using negative values for the psym keyword. To plot triangles connected by a solid line try:

IDL> plot, x, y, psym = 2

IDL> plot, x, y, psym = -5

Page 45: Introduction to IDL Dealing with Data

Plot Style and Range

• You can change the way your plot looks by using the [XYZ]style keywords. For example to force an exact axis range use:

Index Axis Property

1 Force exact axis range

2 Extended axis range

4 Suppress entire axis

8 Suppress box style axis

16Inhibit setting the y axis starting at value 0

IDL> plot, x, y, xstyle = 1

• You can also limit the amount of data you plot with keywords. To just plot data that lies between 1 and 3 on the x axis and -0.5 and 0.5 on the y axis try:

IDL> plot, x, y, xrange = [1,3], yrange = [-0.5,0.5]

• Sometimes IDL is lame and won’t like your chosen axis range (because the chosen range is not “aesthetically” pleasing), use the [XYZ]style keyword to make IDL listen to you!

Page 46: Introduction to IDL Dealing with Data

Adding Lines to graphics

• Use plots to add lines to your plots.

• syntax: plots, [x0,x1],[y0,y1],[z0,z1]

• Example: add a line that spans the length of the x-axis and crosses through 0 on the y-axis.

plot, x, y, xrange = [0, 2*!pi], xstyle =1

plots, [0, 2*!pi],[0,0]

Page 47: Introduction to IDL Dealing with Data

Tick marks, intervals, and names

• [XYZ]ticklen - controls the length of the axis tick marks (expressed as a fraction of the window size). Default is 0.02. ticklen =1.0 produces a grid, negative ticklen makes marks that extend outside the window.

• [XYZ]tickinterval - set to a scalar to indicate the interval between major tick marks

• [XYZ]tickname - a string of up to 30 elements that controls the annotation of each tick mark.

Page 48: Introduction to IDL Dealing with Data

Plotting Multiple Data Sets

• Use the oplot command to plot multiple data sets on the same set of axes.

IDL> x = findgen(21)*.1*!piIDL> y = sin(x)IDL> y2 = cos(x)IDL> plot, x, y, IDL> oplot, x, y2, linestyle = 2

• What if you have two data sets that require different axes?

Page 49: Introduction to IDL Dealing with Data

Positioning

• You can also position the plot inside the window using the position keyword.

• position is a 4-element vector giving, in order, the coordinates [(x0,y0),(x1,y1)] of the lower left and upper right corners of the data window. Coordinates are expressed in normalized units ranging from 0.0 to 1.0. Position keyword is never specified in data units.

IDL> x = findgen(21)*.1*!piIDL> y = sin(x)IDL> plot, x, y, position = [0.2, 0.2, 0.8,0.8]

Page 50: Introduction to IDL Dealing with Data

Plotting with Multiple Axes• Sometimes you have two or more data sets on the same

line plots, but you want the data sets to use different y axes. It is easy to establish as many axes as you need with the axis command.

• The key to using the axis command is to use the /save keyword to save the proper plotting parameters.

• Try this: (*note the /ylog keyword is set to make the new axis have a log-scale

IDL> x = findgen(21)*.1*!piIDL> y = sin(x)IDL> y2 = dindgen(21)^10IDL> plot, x, y, position = [0.2,0.2,0.8,0.8],$ xtitle = 'x', ytitle = 'y' IDL> axis, yaxis=1, yrange = [.001,5E9],/save, $ ytitle = 'other axis',/ylog

Page 51: Introduction to IDL Dealing with Data

Multiple plots on a page

• The first way to plot multiple plots on the same page is to use the position command and to make sure to include /noerase keyword in the second and all subsequent plots:

x = findgen(21)*.1*!piy = sin(x)y2 = cos(x)plot, x, y,title = 'Plot #1',position = [0.2,0.2, 0.4, 0.4]plot, x, y2,title = 'Plot #2',linestyle = 2,/noerase, $ position = [0.5,0.5, 0.7, 0.7]

Page 52: Introduction to IDL Dealing with Data

Multiple plots on a page• It is much easier to use the !p.multi system variable

to create multiple plots on a page. !p.multi is defined as a 5 element vector as follows:

• !p.multi[0] - number of graphics plots remaining to plot on the display. It is normally set to 0 meaning there is no more graphics plots remaining to be output on the display, the next graphic command will erase the display and start with the first of the multiple graphics plots

• !p.multi[1] - this element specifies the number of graphics columns on the page

• !p.mulit[2] - this element specifies the number of graphics rows on the page

• !p. multi[3] - this element specifies the number of graphics plots stacked in the z direction ... but you must establish a 3-d coordinate system

• !p.multi[4] - This element specifies whether the graphics plots are going to be displayed by fillin gup the rows first = 0 or the columns first =1

Page 53: Introduction to IDL Dealing with Data

How to use !p.multi

x = findgen(21)*.1*!piy = sin(x)y2 = cos(x)

; set !p.multi to create 4 plots, filling in the columns first!p.multi = [0,2,2,0,1] plot, x, y, linestyle = 0, title = 'Plot #1'plot, x, y2, linestyle = 1, title = 'Plot #2'plot, x, y, linestyle = 2, title = 'Plot #3' plot, x, y2, linestyle = 3, title = 'Plot #4'; set !p.multi to create 4 plots, filling in the rows first!p.multi = [0,2,2,0,0] plot, x, y, linestyle = 0, title = 'Plot #1' plot, x, y2, linestyle = 1, title = 'Plot #2'plot, x, y, linestyle = 2, title = 'Plot #3' plot, x, y2, linestyle = 3, title = 'Plot #4'

Page 54: Introduction to IDL Dealing with Data

Leaving room for Titles with Multiple Graphics plots

• When IDL calculates the position of the graphics plots, it uses the entire window to determine how big each plot should be.

• Sometimes you would like to have extra room on the display for titles and annotation.

• You can leave room with multiple plots by using the “outside margin” files of the !x, !y and !z system variables

• Syntax: !p.multi = [0,2,2,0,1]

!y.omargin = [2,4]

Page 55: Introduction to IDL Dealing with Data

Adding text to Graphics

• The xyouts command is used to place a text string a a specific location on the display

• xyouts can be used to add a larger title to a line plot or to label data points on the graph.

• Syntax: xyouts, 0.5, 0.9, /Normal, ‘Graphics Plots’, Alignment = 0.5, Charsize = 2.5.

• You can specify the x and y location of the string with data coordinates or normalized (positional) coordinates.

Page 56: Introduction to IDL Dealing with Data

Positioning TextEmbedded Command

Command Action

!A Shift text above the division line

!B Shift text below the division line

!C Insert a carriable return and begin a new line of text

!D Create a first-level subscript

!E Create a first-level superscript

!I Create an index-type subscript

!L Create a second-level subscript

!N Shift back to normal level after changing level

!R Restore text position to last saved position

!S Save the current text position

!U Create an index-type superscript

!X Return to entry font

!z Display one ore more characters by their unicode value

!! Display the exclamation mark !

Page 57: Introduction to IDL Dealing with Data

Adding color• Adding color to your plots will seem quite annoying at first,

but once you get the hang of it ... it will seem easy! (even though its really not :( )

• Remember how I said that IDL was a “device” oriented model?

• What this means is that you have to select the device you want to draw your graphics on (for now we are using the display window)

• Next you have to configure the graphics device (“backing store”, decomposed vs indexed color, color display (8 bits vs 24 bits).

• This involves a whole lot of “computer language” that I don’t really understand myself, so I am just going to tell you what to do ... and if you want more info I can help after class.

Page 58: Introduction to IDL Dealing with Data

When plotting to the window

• More than likely your computer will have a 24-bit (or better??) graphics card. This means that when plotting to the window you want to be using a true color visual class (not pseudocolor or direct color). When plotting to the window we need to configure our device to use the 24-bit mode: device, true = 24

• You also need to set up how the contents of graphics windows will be refreshed when windows are moved in front of or behind each other (this is called “backing store”). To do this you need to set the retain keyword, i suggest using: device, retain = 2, which means that IDL is responsible for maintaing backing store.

Page 59: Introduction to IDL Dealing with Data

Colors

• A color in IDL is composed of three specific values. We call these values a color triple and write the color as (red, green, blue or RGB). Where the red, green, and blue values represent the amount of red, green and blue light that should be assigned that color on the display. Each value ranges from 0 to 255, thus a color may be made up of 256 shades or gradients of red, 256 shades of green, and 256 shades of blue.

• This means that there are 16.7 million colors that can be represented on the display by IDL. (color triple that represents yellow is (255, 255, 0).

• Two different color models can be used to specify colors on a 24-bit graphics display, and some of the direct graphics commands work differently depending on the model currently in use.

Page 60: Introduction to IDL Dealing with Data

Color models

• The two different models are called decomposed color and indexed color. (decomposed on and decomposed off)

• When configuring the device decomposed = 0 (index model), decomposed = 1 (decomposed color model).

• By default, when you set the 24-bit Truecolor environment, it will be using the decomposed model, which means that you can set the color using an RGB triple directly or: color = [255,255,0] (yellow)

• I am going to teach you how to use the “index model” - because this is what we will need to use to make PostScripts.

Page 61: Introduction to IDL Dealing with Data

Device Configuration• To configure our device so that we can draw graphics

to the screen and everything works out so that we don’t get crazy flashing and unexpected colors do the following.

device,true=24,decompose=0,retain=2

• Here we are using the truecolor color class, using the indexed color mode, and allowing IDL to manage backing store.

• If you are like me ... you will put this into a procedure that you can run and you won’t think about it again (until you want to make PostScripts)

Page 62: Introduction to IDL Dealing with Data

Back to actually colors

• So how do colors work in IDL?

• Now that we have configured our device, we want to load in a color table.

• We will start with the IDL pre-defined color tables.

• The default color table in IDL is called B- W linear.

• We can look at the colors in this table using the xpalette or cindex procedures.

• To change the color table use: loadct, # of CT : loadct, 39

Page 63: Introduction to IDL Dealing with Data

Setting the background and plot colors in IDL

• IDL’s default setting sets the background of the plot as black and the font as white. To change this use the background and color keywords. Set background and color to the desired color “index” in the selected color table.

• When using the B-W linear color table do the following:

• plot, x, y, background = 255, color = 0

Page 64: Introduction to IDL Dealing with Data

Choosing Colors• Load the color table into IDL

• Use xpalette or cindex to choose the color you want to use.

• Set the color to the chosen index using the color keyword.

• For every new line you want to add to the plot, us the oplot command and change the color keyword.

• Make sure that the first think you plot is set to the color you want your axes to be (black, white, etc)...otherwise you might end up with purple axes.

• Use /nodata in your plot command or oplot the same line again with a different color

Page 65: Introduction to IDL Dealing with Data

Examples• Plot multiple lines on the same graph, each line

should have its own color and its own linestyle.

• Example: global_temp.pro

• Plot a scatter plot!

• Example: temp_vs_sunspot.pro

• Put multiple plots on the same page. Plot the information on each graph with different colors. Title the selection of graphs.

• Example: plotting_examples.pro

Page 66: Introduction to IDL Dealing with Data

Adding a Legend

• use the legend.pro procedure created by to add a legend to your diagram.

• legend accepts, color, psym, linestyle, position, /center, /bottom, /top etc.

Page 67: Introduction to IDL Dealing with Data

Questions???