Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
1/29/2013
1
Intro to Igor Pro: Beyond “Getting Started”
ATOC / CHEM-5151Atmospheric Chemistry
Prof. Jose L. Jimenez
1Last update: Jan 2013Adapted from 2009 lecture by Ingrid Ulbrich
Goals for this lecture• Become familiar with Igor Pro software
– Storing data– Doing calculations– Making graphs– Writing simple functions
• Homeworks to work on these tasks– Start with examples that should already be familiar– “Model” the simplest MS (electric sector)
• Very important to put in the effort, or you will be lost in later homeworks!
2
1/29/2013
2
Did you do the Igor “Getting Started” Tour?
a) Yes, all of it.
b) Yes, some of it.
c) No, or not very much of it.
d) I already know how to program well (Igor or
other language)
e) I need a coffee
3
Why should I learn programming? (1)
• Functions create a record of the steps used in a calculationHelp find errors in multistep calculations
• Functions make it easy to repeat the same calculation with different data Standardized analysis
4
1/29/2013
3
Why should I learn programming? (2)• Functions are easier to share with others than in a
spreadsheetRepeatable calculations for your colleagues
• Slice and dice big datasets– Our Aerosol Mass Spectrometer saves 60,000 mass
spectral values every 2.5 minutes• 106 values per hour 3x107 per day 2x109 per month• And that’s on “slow mode”, now we often save every 1s or
100 ms!
Way too much data to look at everything by hand! Same is true for many instruments, satellites, model
output etc.
5
Why do I have to use Igor instead of software I already know?
• Powerful analysis package, can be very useful later– Superior to spreadsheets! Especially good for
• Complex data analysis: reproducible and traceable calculations• Graphs with more options that are easier to manipulate
– Much cheaper than Matlab, similar capabilities– Excellent email support, good user mailing list
• But always try hard first before emailing them (2 hr rule)
• Used almost exclusively in Tolbert, Jimenez, Volkamer, several other groups for data analysis
• Good tool for learning programming– Menus, command line, and programs to do the same thing
• Consistent tool for the class, makes it possible to discuss code in class, and for Jose and others to help 6
1/29/2013
4
Igor File Vocabulary
• Experiment: an Igor file where you store data and graphs (.pxp) Let’s open a new experiment.
• DataFolder: subdirectory in an experiment– Red Arrow in Data Browser shows current DataFolder
• Notebook: “file” in an experiment where you can write notes, paste graphs– Windows New Notebook (Formatted Text)
• Procedures: functions written by you (or others) – Some exist only in the experiment they are in
– Can be shared with friends/colleagues
– “Local” procedure window (ctrl+m)7
More Igor File Vocabulary• Data Browser
– Can set Preferences to sort by “Name and Type”
• Help Browser (F1)– Look for help on functions
– Can also get function help by right-clicking on a function name, choosing “Help on functionName”
– Most functions have Examples
– Many functions have Related Functions
8
1/29/2013
5
Data is usually stored in Waves
• Wave: a vector (array) containing data– Exists until you “kill” it (even if you’re not looking at
it in a graph or table)• Contrast to e.g. Excel, data only exists if it is on the sheet
– Numbers (single precision by default) or text
• Every wave has some number of “points” (length)
• Waves have inherent “x” values = point number (0, 1, 2…)– When you display a wave, it’s plotted against the
internal x values (unless you tell it to plot vs. something else)
9
Wave Names have Rules• Wave names
– Cannot include spaces, operators (+ - * /), special characters
• Can use underscore ( _ )
– Cannot begin with numbers
– Must be ≤ 32 characters
– Are not case sensitive
– Cannot have the same name as functions, variables, Igor-used terms
10
1/29/2013
6
Making Waves• Let’s make a really simple wave so that we can
practice some simple operationsMake/N=20 y_vals
• Now let’s look at the values in the wave in a table:
edit y_vals.id
(special way to see the “x” and “data” values”)
Igor function that creates a wave
Flag for makethat sets Number of points in wave
Name of wave
11
Giving values to y_vals• Now we can take advantage of the inherent “x”
values and make y_vals = f(x)
Y_vals = xDisplay y_vals
Fix the axes to have range -10, 40 for x and yAdd “zero lines” (Ticks and Grids tab)
Copy and Paste this plot in your Notebook
Change your line by changing the equation in the command line (e.g., y_vals = 2*x y_vals = 2*x + 3)
Copy and Paste a new plot in your Notebook
Note that the y_vals.d changes in the table, too!
12
1/29/2013
7
Kill the table!• Check for the wave in the Data Browser
– Waves exist until you kill them, even if you’re not looking at the them.
– Very different from Excel, of course!
– Important for homeworks, as you try various things, remember to kill old LARGE waves so that your experiment doesn’t become huge
• Make table again if you want to watch the data change
13
Our Sample Function
Function LineValues()
make/O/N=20 y_vals = 0.1 * x + 1
end
And let’s compile!
And let’s run it (from the command line).
Change the values. Does the line change?
Function Name Function Arguments
Gotta end the function
Stuff for the function to do
14
1/29/2013
8
Our Sample Function, with Variables
Function LineValues2()
variable m = 0.1, b = 1
make/O/N=20 y_vals = m * x + b
end
15
Our Sample Function, with Inputs
Function LineValues_input(m,b)
variable m, b
make/O/N=20 y_vals = m * x + b
end
16
1/29/2013
9
Our Sample Function, Print Equation
Function LineValues(m,b)
variable m, b
make/O/N=20 y_vals = m * x + b
print “Equation is y=“, m, “*x + “, b
end
17
Our Sample Function, Print Equation Sometimes
Function LineValues(m, b, printFlag)
variable m, b, printFlag
make/O/N=20 y_vals = m * x + b
if(printFlag == 1)
print “Equation is y=“, m, “*x + “, b
endif
end
18
1/29/2013
10
Our Sample Function, with Outputs
Function LineValues_input(m,b,x)
variable m, b, x
return m * x + b
end
• More versatile function, independent of wave• Calculate the wave on the command line, e.g.
y_vals = LineValues_input(0.1, 1, x)
19
Now Run with Debugger• Procedure Enable Debugger
• In the new column at the left, click in a row in the function.– This will stop execution of the function and start the debugger.
• Above the top center box in the debugger, chose “Local and Global Variables”
• The yellow arrow in the function shows the next line to be executed.– Proceed 1 line at a time by pressing enter or using the yellow
arrow button at the top of the debugger.– Proceed to next stop (or end) by pressing the green arrow
button at the top of the debugger20
1/29/2013
11
Check out Ingrid’s Igor Quick Reference Wiki
• http://cires.colorado.edu/jimenez-group/wiki/index.php/Igor_Quick_Reference
• Still a work in progress! Please make suggestions for improvement to <[email protected]>
21
Other Examples
• When you make a lot of waves with the same x data (e.g., time_hr), clearer to just use a separate x wave– Calculate the distance that
a car goes (50 mph) and that a bike goes (15 mph) over 10 hours
• Plot ln(x), warn user if x_min < 0
800
600
400
200
0
Dis
tanc
e T
rave
lled
(mi)
151050time (hr)
car bike
display dist_car_mi, dist_bike_mi vs time_hr
Plotted vs. same x wave
22
1/29/2013
12
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: When the ball is in the air, what is the force diagram for the ball?
A) B) C) D)
E) None of These
23
Simple Case: Toss a Ball Straight Up in the Air
• What will the trajectory of the ball look like?
x vs. t y vs. t x vs. y
24
1/29/2013
13
Let’s work to plot y vs. t1. Make a wave of time in seconds.
Let’s start a function: Make/O/N=10 time_s // time in seconds
time_s = x // for now 1s increments
25
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: Acceleration is…
Acceleration is equal to…Let’s make a CONSTANT above the function
Now we can make a wave for accel_m_s2
A) constant B) proportional to t C) proportional to t2
D) none of these E) I really need a coffee
26
1/29/2013
14
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: Velocity is…
A) constant B) proportional to t C) proportional to t2
D) none of these E) Please add milk and sugar
27
Integrating Acceleration Velocity
We will use Euler’s Method:
y(i) = y(i-1) + y’(i)*dt
y'
y(0)
y‘(0) = 0y’*dt
t
y‘(1) = -somethingy’*dt
y‘(2) = -something morey’*dt
∫ line = parabola
28
1/29/2013
15
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocity
Acceleration (m/s2) Time (s) Velocity (m/s)
0
1
2
3
29
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocity
Need an initial condition!
Let’s just drop the ball. Then v0 = ?
Acceleration (m/s2) Time (s) Velocity (m/s)
-9.8 0
-9.8 1
-9.8 2
-9.8 3
30
1/29/2013
16
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocity
Acceleration (m/s2) Time (s) Velocity (m/s)
-9.8 0 0.0
-9.8 1
-9.8 2
-9.8 3
31
Since we already “know” how to calculate the answer…
• Let’s make a table to calculate (check) what we expect to get for velocity
Acceleration (m/s2) Time (s) Velocity (m/s)
-9.8 0 0.0
-9.8 1 - 9.8
-9.8 2 -19.6
-9.8 3 -28.4
32
1/29/2013
17
How do we tell the computerhow to do this calculation?
1. Use the initial condition to fill in the first element of the wave.
2. For every row in the rest of the wave, calculate vi = vi-1 + ai*dt
In the function we can do step 1: Create a variable v_0_m_s make vel_m_s set zeroth point of vel_m_s = v_0_m_s
33
Writing the “for each row” part
for(start counter; do it condition; update counter)
stuff to do each time
usually depends on the counter
endfor
Only happens when entering the “for” loop
e.g. index = 0Do stuff until this isn’t true anymoree.g. index < 100
Only happens at the end of the stuff inside the
“for” loop: ready to do the next onee.g. index = index + 1
34
1/29/2013
18
The “index” for our case
• Row 0 was special, and we’ve already handled that• Vel_m_s[0] = v_0_m_s
– So we want to start our integration with row 1• Make counter start at index = 1
• We want to operate on every row• Increase our counter by index = index + 1
• We want to keep going while we have rows• Stop Condition is index < 10
Igor Row Number
Acceleration (m/s2)
Time (s) Velocity (m/s)
0 -9.8 0 0.0
1 -9.8 1 - 9.8
2 -9.8 2 -19.6
3 -9.8 3 -28.4
35
Writing the “for each row” part
for(index = 1; index < 10; index = index + 1)
stuff to do each time
usually depends on the counter
endfor
Only happens when entering the “for” loop Do stuff until this
isn’t true anymoree.g. index < 100
Only happens at the end of the stuff inside the
“for” loop: ready to do the next onee.g. index = index + 1
Now, what’s the stuff to do each time?vi = vi-1 + ai*dt
36
1/29/2013
19
Vel_m_s accel_m_s2
can get dt from time_s
v, a, and t already exist in our function!
So how can we write the equation?
vi = vi-1 + ai*dt
A closer look at our equation
37
Simple Case: Toss a Ball Straight Up in the Air
Clicker Q: Position is…
A) constant B) proportional to t C) proportional to t2
D) none of these E) I need a refill, please
38
1/29/2013
20
How can we implement the position calculation?
Clicker Q:A. Position must be calculated in a different
function.
B. Position can be calculated in a separate for loop before calculating the velocity.
C. Position can be calculated in a separate for loop after the loop for calculating velocity.
D. Velocity and position can be calculated in the same for loop.
39
Let’s make a plot!
• Make your axes like this– Spacing, alignment
– Axis labels
– Zero line for velocity, position
• Markers are nice, but not necessary -300
-200
-100
0
Pos
ition
(m
)
86420
Time (s)
-80
-60
-40
-20
0
Vel
ocity
(m
/s)
-9.80
Acc
eler
atio
n (m
/s2 )
40
1/29/2013
21
Next Steps• Change v0 and s0 and see changes
• Generalize the function with input variables
• Assume that – s0 > 0
– the ground = 0
– After the ball hits the ground it stays there for all remaining time
41
Advanced further steps• Generalize function to allow variable number of
points in waves (hint: look up help for numpntscommand in igor)
• Allow variable time steps
42