Title: Introduction to IDL Dealing with Data
1Introduction to IDLDealing with Data
- Week 2
- Rachel McCrary
- 3-31-2009
2Goals 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
3Opening 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
4Opening 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
5Reading/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.
6Writing 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
7Reading 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
8Rule 1
- 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
IDLgt OPENR, lun, 'test_free_format.txt', /GET_LUN
IDLgt header ''IDLgt READF, lun, headerIDLgt
print, headerTest data file.
- You can then read the first two lines (or any
number of lines) in a similar way using the
following
IDLgt header strarr(2)IDLgt READF, lun,
headerIDLgt print, headerTest data file. Created
Sun Mar 29 133552 2009
9Rule 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)!
10Rules 3, 4 and 5
- 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. - If the current input line is empty, and there are
still variables left requiring input, read
another line. - If the current input line is not empty, but there
are no variables left requiring input, ignore the
remainder of the line.
11Rule 4 5
IDLgt data fltarr(8)IDLgt READF, lun, dataIDLgt
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.
IDLgt vector3 fltarr(3)IDLgt READF, lun,
vector3IDLgt print, vector312.0000 13.0000
14.0000
12Rule 6
- 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
IDLgt dummy strarr(2)IDLgt READF, lun, dummy
- 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.
IDLgt ints intarr(2)IDLgt READF, lun, intsIDLgt
print, ints 33 77
13Rule 7
- 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.
IDLgt value complexarr(2)IDLgt read, value
(3,4) (4.4,25.5)IDLgt print, value( 3.00000,
4.00000)( 4.40000, 25.5000)
14Always make sure...
- When you are done with a file, make sure that you
close it! To do this type
IDLgt FREE_LUN, lun
15Reading a Free Format File
- 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.
16Writing 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.
17Reading ASCII
- 72469 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
filename - sounding.txt
18sounding.txt - info
- File info
- 5 lines of header
- missing data filled with -9999
- 11 columns (floating pt. and integers)
- 117 rows of data
19Read 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
20Reading 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 doesnt 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.
21Reading 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
22Explicit 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
23Example - I
- Specifies integer data. Print the vector out as
a two-digit integers, five numbers on each line,
each number separated by two spaces
IDLgt data findgen(20)IDLgt fmt
'(5(I2,2x),/)'IDLgt print, data, formatfmt 0 1
2 3 4 5 6 7 8 910 11 12 13 1415
16 17 18 19
24Example - 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.)
IDLgt fmt '(F5.2)'IDLgt 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.
25Example - 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.
IDLgt fmt '(5(D13.10,4x))'IDLgt print, data,
formatfmt 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
26Reading/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
IDLgt x findgen(10000)IDLgt openw, lun,
'unformat_data.txt', /get_lunIDLgt writeu, lun,
xIDLgt free_lun, lun
IDLgt x findgen(10000)IDLgt openr, lun,
'unformat_data.txt', /get_lunIDLgt readu, lun,
xIDLgt free_lun, lun
27Reading 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
28Whats 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
29Reading a Variable
open the netcdf file nc ncdf_open('/Users/rac
hel/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
- 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
30Reading an Attribute
open the netcdf file nc ncdf_open('/Users/rac
hel/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
- 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
31Some netCDF routines
Name 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
32Simple 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.
33A 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.
34Graphics
- 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 IDLgt plot, indgen(10)
35A few things you might notice ...
- IDLs 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.
36Window Options
- Graphics window can be created directly using the
window command or indirectly by issuing a
graphics display command when no window is open.
IDLgt window - The title bar of the window has a number in it (0
in this case) - this is the windows graphics
window number index. - You can open a new window with a graphics window
index number of 1 by doing the following IDLgt
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 IDLgt window,
/free - If you dont explicitly open a new window, IDL
will plot over whatever is on the current window.
37Window 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
IDLgt window, 1IDLgt window, 2IDLgt plot,
indgen(10)IDLgt wset, 1IDLgt plot,
sin(findgen(200))IDLgt wdelete, 2IDLgt erase, 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
38Window 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.
IDLgt window, 1, xsize200, ysize 300
IDLgt window, 2, xpos75, ypos150
39Creating 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!
40Plotting a vector
- IDL will try to plot as nice a plot as it
possibly can with as little information as it
has. - Try plotting IDLgt 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
41Plotting 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)
IDLgt x FINDGEN(21).1!piIDLgt y sin(x)IDLgt
plot, x, y
42Customize 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 XYZcharsize.
IDLgt plot, x, y, xtitle 'Radians', ytitle
'Radians', title 'Sine Wave'
IDLgt plot, x, y, xtitle 'Radians', ytitle
'Radians', title 'Sine Wave', charsize 1.5
IDLgt plot, x, y, xtitle 'Radians', ytitle
'Radians', title 'Sine Wave', xcharsize 2,
ycharsize 3
43Modify 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
IDLgt 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
IDLgt plot, x, y, linestyle 2, thick 3
44Symbols
- 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
IDLgt plot, x, y, psym 2
IDLgt plot, x, y, psym -5
45Plot Style and Range
- 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
IDLgt plot, x, y, xrange 1,3, yrange
-0.5,0.5
- Sometimes IDL is lame and wont like your chosen
axis range (because the chosen range is not
aesthetically pleasing), use the XYZstyle
keyword to make IDL listen to you!
Index Axis Property
1 Force exact axis range
2 Extended axis range
4 Suppress entire axis
8 Suppress box style axis
16 Inhibit setting the y axis starting at value 0
- You can change the way your plot looks by using
the XYZstyle keywords. For example to force an
exact axis range use
IDLgt plot, x, y, xstyle 1
46Adding 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
47Tick marks, intervals, and names
- XYZticklen - 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. - XYZtickinterval - set to a scalar to indicate
the interval between major tick marks - XYZtickname - a string of up to 30 elements
that controls the annotation of each tick mark.
48Plotting Multiple Data Sets
- Use the oplot command to plot multiple data sets
on the same set of axes.
IDLgt x findgen(21).1!piIDLgt y sin(x)IDLgt y2
cos(x) IDLgt plot, x, y, IDLgt oplot, x, y2,
linestyle 2
- What if you have two data sets that require
different axes?
49Positioning
- 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.
IDLgt x findgen(21).1!piIDLgt y sin(x) IDLgt
plot, x, y, position 0.2, 0.2, 0.8,0.8
50Plotting 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
IDLgt x findgen(21).1!piIDLgt y sin(x)IDLgt y2
dindgen(21)10 IDLgt plot, x, y, position
0.2,0.2,0.8,0.8, xtitle 'x', ytitle 'y'
IDLgt axis, yaxis1, yrange
.001,5E9,/save, ytitle 'other axis',/ylog
51Multiple 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.4plot, x, y2,title 'Plot 2',linestyle
2,/noerase, position 0.5,0.5, 0.7, 0.7
52Multiple 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.multi0 - 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.multi1 - this element specifies the number
of graphics columns on the page - !p.mulit2 - this element specifies the number
of graphics rows on the page - !p. multi3 - this element specifies the number
of graphics plots stacked in the z direction ...
but you must establish a 3-d coordinate system - !p.multi4 - This element specifies whether the
graphics plots are going to be displayed by
fillin gup the rows first 0 or the columns
first 1
53How 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'
54Leaving 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
55Adding 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.
56Positioning Text
Embedded 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 !
57Adding 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 dont 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.
58When 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.
59Colors
- 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.
60Color 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.
61Device Configuration
- To configure our device so that we can draw
graphics to the screen and everything works out
so that we dont get crazy flashing and
unexpected colors do the following. - device,true24,decompose0,retain2
- 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 wont think
about it again (until you want to make
PostScripts)
62Back 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
63Setting the background and plot colors in IDL
- IDLs 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
64Choosing 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
65Examples
- 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
66Adding 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.
67Questions???