Title: Mars Data Analysis Using IDL GG7101 Lecture 5
1Mars Data Analysis Using IDLGG710-1Lecture 5
- F. Scott Anderson
- Office POST 526B
- Phone 956-6887
- Email anderson_at_higp.hawaii.edu
2Today
- Questions from last time
- Quizzes returned/Answers
- Review of yesterday
- Finish Procedures and Functions
- Control Statements
- In class exercise Atmospheric Scale Height
- Review of today's class
3Questions from 9/22
- To reset an IDL session
- .reset_session
- Local/user defined system/global variables
destroyed - Pointers, structures destroyed
- Compiled programs subroutines destroyed
- Graphics windows widgets destroyed
4Questions from 9/22
- Pointers Allocating the heap
- ptr_new() creates a pointer that always points to
nothing - Can be made to point at something with a 2nd
ptr_new command - Generally used as a marker of the end of an array
- ptr_new(/allocate_heap) creates a pointer to an
undefined variable - ptr_new(x) creates a copy of x, then makes a
pointer to it - ptr_new(x, /no_copy) makes a copy of x, creates a
pointer to the copy, then deletes x
5Quiz Scores from 9/22
- Much improvement!
- Average went from 69 to 85
- Standard deviation from 20 to 9
- Quiz Scores
- 70 80 83 83 90 90 100
69/22 Quiz Answers
- Create an anonymous structure containing a float,
an int, and a double - datax 0.0, y 0, z 0.0D
- Illustrate how to print the int from the
anonymous structure - print, data.y
- What does the function n_elements do?
- Counts the number of positions in an array
79/22 Quiz Answers
- Create a pointer to a image of floating point
numbers that is 1000 lines by 100 samples - img_ptrptr_new(fltarr(100,1000))
- In the array x0, 7, 23, 9, 400 show the
command that searches for and returns the index
of the value 9 - print, where(x eq 9)
- partial credit for variations on
- for ctr0,4 do if (xctr eq 9) then print, ctr
- but as we havent covered this yet, it wasnt the
main answer - for ctr0,4 do if ((xctr mod 3) eq 0) then
print, ctr - wont work, as 0 mod 3 also is 0
8Review of 9/22
- Array resizing (congrid)
- Procedures and functions
- Definitions (functions always return a value)
- Order
- Naming
- Editing compiling
- Passing parameters
- Procedures use comma separated variables
- Functions use comma separated variables in
parentheses - Keywords Passing optional parameters
- Returning parameters
9Returning Data from Functions or PRO
- Three methods for returning values from functions
or procedures
- By changing the parameters that are handed to
them (applies to procedures and functions) - By returning a specific value or structure from a
function using return - Both at the same time
IDLgt hello
10Functions Returning Parameters
filename hello.pro
Functions always return a value, even if there
is no return statement (in which case 0 is
returned)
IDLgt hello
11Both Return and Parameter Passing
filename hello.pro
FUNCTION manky, str1 print, str1 str1'Hot
Doggity!' return, 'Done!' END PRO hello var
"hello world" xmanky(var) print, var print, x end
IDLgt hello
12Notes on Return
- In a procedure subroutine, return can be used to
send control back to the calling program - However, a value cannot be returned in a
procedure - pro subroutine1, var
- print, Great subroutine
- if (var eq 1) then return
- print, Var ne 1
- end
pro main_routine subroutine1, 1 subroutine1,
2 end
13Notes on Return
- In a function subroutine, return is used to
return control and a value to the calling routine - For Functions and PRO, return can be called more
than once from within conditional statements - function subroutine2, var
- print, Really Great Subroutine!
- if (var eq 42) then return, 42
- print, var ne 42
- return, var10
- end
14Exception Parameter Passing
- Parameters may be passed to a PRO or FUNCTION by
reference or by value - Reference passing the location in memory
- Value passing the actual value
- Parameters passed by value to a subroutine will
not be changed unless explicitly passed back to
the calling routine with return
Changeable
Not Changed! Use return
15Exception Parameter Passing
not changeable by subroutine manky, 100 var9,
10, 11 manky, var01 dataarr2.3, 10.5,
'Bomber!' manky, data.arr0 use return to get
changed values back
16Control Statements
- Used to control the flow of your program
- Can be used to repeat a given step many times
- Can be used to check values and decide what to do
next
17If
- If is used to check the value of a variable and
select an action - var10
- if (var eq 10) then print, var else print, 'Doh!'
- Operators in Table 2.6
18If
- Note if (var10) wrong
- Assigns 10 to var, and value in () is set to 10
- if (var 10) then print, var else print, 'Doh!'
- the same as
- if (10) then print, var else print, 'Doh!'
- If evaluates the value in () for true or false
- 0 or Null always evaluates to false!
19If Form
- Form
- if condition then statement else statement
- or
- if condition then begin
- statement
- statement
- endif else if condition then begin
- statement
- statement
- endif else begin
- statement
- statement
- endelse
20If Example
- pro test, var
- if ((var eq 7) or (var eq 9)) then begin
- print, 'Yowzer'
- var0
- endif else if (var eq 3) then begin
- print, 'Yahoo'
- var1
- endif else begin
- print, 'Oh Blah!'
- var2
- endelse
- print, var
- end
21Case
- Similar to if but easier to write for extensive
lists of conditions - pro test, var
- case var of
- 0 print, Var equals 0
- 1 print, 0ltvarlt1
- 2 begin
- print, This is getting old
- print, This is the last message
- end
- else print, Var not 0,1,2
- endcase
- end
22Case Form
- Must use scalar index variable for expression
- Else is optional, but will get error if no case
selected - case expression of
- exp1 statement
- exp2 statement
- exp_n begin
- statement
- statement
- end
- else statement
- endcase
- Once an expression is selected no more are
executed
23Switch
- Similar to case
- Once a true expression is found, switch assumes
all following expressions are true - switch expression of
- exp1 statement
- exp2 statement
- exp_n begin
- statement
- statement
- end
- else statement
- endswitch
IDLgt test,1 Test 1 Test 2 Test 3 IDLgt test,2 Test
2 Test 3 IDLgt test,3 Test 3 IDLgt
test.pro
pro test, var switch var of 1 print,
'Test 1' 2 print, 'Test 2' 3
print, 'Test 3' endswitch end
24For
- Loop over a set of statements, incrementing an
variable from a start value to an end value by a
given increment - xfindgen(10)3.7
- for ctr0,4 do print, xctr
- for ctr5,-1,-1 do print, xctr
- Default increment is 1
0.00000 3.70000 7.40000
11.1000 14.8000
18.5000 14.8000 11.1000
7.40000 3.70000 0.00000
25For Form
- for variablestart, finish, increment do
statement - for variablestart, finish, increment do begin
- statement
- statement
- endfor
- Note the loop variable is defined by the value
contained in start - I.e., if need to loop over floating point range,
ensure that start is a float
26While
- Loop over a set of statements while the provided
condition is true - x0.4
- while (x le 10) do begin
- print, x
- xx0.3
- endwhile
0.40000 0.70000 1.00000
1.30000 1.60000
9.40000 9.70000
27While Form
- while condition do statement
- while condition do begin
- statement
- statement
- endwhile
28Atmospheric Scale Height
- Scale height
- Distance over which a parameter is reduced by a
factor of 1/e - Atmospheric Scale Height
- Distance over which the atmospheric pressure drop
by 1/e - Defined as
- On Earth, H7.5 km
- For mean T250K
29Mars Atmospheric Scale Height
- For Mars,
- Thus, atmospheric scale height is
-
- Write a program to return
- ASH as a function of Temp
- What is the scale height for 210 K?
- If the surface pressure is 7.4 mb, what is the
pressure 21.6 km higher?
30ASH Example
As a function
filename mash.pro
IDLgt xmash(210) IDLgt print, x 10791.9 IDLgt
print, mash(210) 10791.9 IDLgt print,
7.4exp(-21600.0/x) 0.999970
function mash, tempr return, 191.17
float(tempr) / 3.72 end
Or as a procedure
filename mash.pro
IDLgt mash,210,x IDLgt print, x 10791.9 IDLgt
print, 7.4exp(-21600.0/x) 0.999970
pro mash, tempr, msh msh 191.17
float(tempr) / 3.72 end
Pressure at 21.6 km 1.0 mbar
31Today
- Procedures and functions
- Control Statements if, case, switch, for, while
- Atmospheric Scale Height Example
32Next Time
- Finish Control Statements
- Global variables, error handling, efficient
coding - Input and Output
- Standard I/O (to the screen/terminal)
- Formatting
- Files
- Formatted/ASCII
- Unformatted/Binary
- open, read, write, file pointers