Title: Mars Data Analysis Using IDL GG7101 Lecture 6
1Mars Data Analysis Using IDLGG710-1Lecture 6
- F. Scott Anderson
- Office POST 526B
- Phone 956-6887
- Email anderson_at_higp.hawaii.edu
2Review of Monday
- Procedures and Functions
- Main program follows subroutines
- Data passed to a
- Pro in a comma delimited list pro_name, var1,
var2 - Fun in a comma delimited list surrounded by
parens - xfun_name(var1, var2)
- Data can be passed back from a
- Pro or Fun Changing parameters passed to it
- Fun Using return statement
- Control Statements
- Most commonly used if, for, while
- Also can use case, switch
- ASH Example of Function/Procedure
3Today
- Questions from last time
- Quiz
- HW returned
- Finish Control Statements
- Global Variables efficient programming
- I/O
- Standard I/O
- ASCII I/O
- Binary I/O
- HW Predict the pressure for the surface of Mars
- Review of today's class
4Questions from 9/28
- 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
- Show the command to resize an array containing
THEMIS floating point data called them_data that
is 800 samples by 1000 lines to a new array
called new_data that is 700 samples by 800 lines - Assume there is a function called
seismic_scale_depth that is handed density and
returns a distance. Show how to call this
function with a density of 2.7 and store the
returned data in a variable zdepth - Show a for loop that prints the numbers 5.5 to
10.5 in steps of 0.5 - Show an if statement that prints the words 'Got
ya!' if a variable named status1 equals 4.3
6Homework1 Scores
- Overall, good!
- Answers last time
- Scores
- 80 85 93 100 100 100 100
7Repeat Form
- Similar to while, but executes statement before
checking condition - repeat statement until condition
- repeat begin
- statement
- statement
- endrep until condition
8Repeat
- Example
- x0.4
- repeat begin
- print, x
- xx0.3
- endrep until (x le 10)
0.400000
9Goto
- Goto is used to jump from the current position in
the code to a new position located by a variable
label - Considered poor programming technique, as it
difficult to follow logical jumps - x0.4
- goto new_code1
- print, x
- xx0.3
- new_code1
- print, Jumped!
Goto Form goto, label skipped statement skipped
statement label
10Break
- Break is used to immediately exit from a for,
while, repeat, case, or switch statement - Execution jumps to the next statement after the
loop - x0.4
- repeat begin
- print, x
- xx0.3
- if (x eq 1.0) then break
- endrep until (x ge 10)
- print, x
0.400000 0.700000 1.000000
11Continue
- Continue is used to jump to the next iteration of
a for loop without executing intervening code - for ctr0,10 do begin
- if (ctr eq 5) then continue
- print, ctr
- endfor
0 1 2 3 4
6 7 8 9 10
No 5!
12Global Variables
- Variables are available only to the
routine/subroutine they are created in, unless
specifically passed to another routine - Variables that can be seen by all
routines/subroutines without being explicitly
passed are called Global Variables - Considered poor programming, because it is hard
to tell where they are being changed when
debugging or reading a program - There are two types of global variables in IDL
- System defined global variables (some not
changeable, start with !) - User defined global variables (changeable)
13Unchangeable Global Variables
14Changeable Global Variables
- Two ways to generate a user defined global
variable - defsysv create a changeable system variable
- defsysv, '!my_var1', 33.3
- print, !my_var1
- common block creates a user global variable
accessible to any routine with a common block
definition
15Common Blocks
- Form
- common block_name, var1, var2, var_n
- Any routine with
- common block_name, var1, var2, var_n
- inside of it can access var1-var_n
16Example of Common Blocks
- pro subroutine1
- common rockin, var1, var2, var_n
- var1'Yippee!'
- end
- pro main_routine1
- common rockin, var1, var2, var_n
- subroutine1
- print, var1
- end
Note var1 not handed to subroutine1, but still
changed, because it is global as a result of
common block!
17Efficient Code
- IDL is optimized for array processing
- If you can avoid control statements that create
loops, and instead use arrays, IDL can run
significantly faster - pro test
- adist(500,1000)
- tvscl, dist(50,100)
- sum0.0
- t0 systime(1)
- for i0L, (n_elements(a)-1L) do
- if (ai gt 100.0) then sum sum ai
- t1 systime(1)
- print, t1 - t0
- end
Slow! 1.0 Sec
18Efficient Code
- IDL is optimized for array processing
- No loops, using IDL array functions
- pro test
- adist(500,1000)
- sum0.0
- t0 systime(1)
- idx where(a gt 100.0, cnt)
- if (cnt gt 0) then sumtotal(aidx)
- t1 systime(1)
- print, t1 - t0
- end
Faster! 0.088 Sec
19Efficient Code
- IDL is optimized for array processing
- Using only arrays and IDL functions
- pro test
- adist(500,1000)
- sum0.0
- t0 systime(1)
- sum total(a (a gt 100.0))
- t1 systime(1)
- print, t1 - t0
- end
Fastest! 0.083 Sec
20Input and Output
- Three primary types of I/O
- Reading and writing from the keyboard and screen
- Called standard I/O or
- stdio
- Reading and writing text characters to a file
- ASCII I/O
- Reading and writing binary data to a file
- Binary I/O
21STDIO Writing
- Print used to write numbers and text to screen
- Can control the precision and style of numbers
presented on screen using format commands - print, !pi, format(f7.2)
- print, findgen(5), format(5f7.2)
- print, 2, !pi, format(i4, 2x, f7.2)
- Details of the format codes can be found in Table
4.2, and Chapter 4.1
22STDIO Writing
23STDIO Reading
- Can read data from the user at the command line
using the read command - read, var1, var2, var_n, prompt'your text here'
- read, sin_var, prompt'Enter value to take sine
of ' - If not previously defined, the default type of
variables in read is float. - To use another type, such as int or string,
define the variable first with a bogus value - ivar0
- svar' '
- read, ivar, svar, prompt'Enter integer and
string '
24Working with files (ASCII or binary)
- Difference between ASCII and binary
- Can write data into a file as human readable
(ASCII) or the actual bits/bytes required for
variable type (binary) - Example In the file looks like
- An ASCII Integer 10 gt _ _ _ _ _ _10 (eof)
- 8 bytes1 for eof, each 8 bits long
- A binary integer 10 gt _at_
- 2 bytes long (16 bits matches type)
- Quiz Which is longer, an ASCII or binary file
with 10 floating point numbers in it?
25Working with files (ASCII or binary)
- To write or read data from a file, several steps
must be taken - The file needs to be opened for access
- Data needs to be written or read
- The file needs to be closed
26Working with files (ASCII or binary)
- IDL can open a file for reading, writing, or both
- openr read
- openw write
- openu both
- Opening a file LUN's
- IDL uses a string containing the name of a file
to create a variable associated with the file - Variable associated with file called a "Look Up
Number" or LUN - There are a limited number of files that can be
open simultaneously (128) - IDL can automatically provide us with an
available LUN when opening a file
27Examples of opening a file
- Form of command
- openr, lun_var, filename_str, /get_lun
- openw, lun_var, filename_str, /get_lun
- openu, lun_var, filename_str, /get_lun
- Example
- openr, iln, 'scott_data.txt', /get_lun
28Closing a file
- When done reading or writing a file, close it
with free_lun - Gives the LUN back into the limited pool of files
that can be open simultaneously - Form of command
- free_lun, lun_var
- Example
- openr, iln, 'scott_data.txt', /get_lun
- statements to read the file go here
- free_lun, iln
29Writing ASCII files
- To write data to an ASCII file, use printf
- Form
- printf, lun_var, var_1, var_2, var_n,
formatfmt_string - Example
- openw, olun, 'test.txt', /get_lun
- printf, olun, 5, 6, 7, 9.0
- datafindgen(5,5)
- printf, olun, data
- datafindgen(100)
- printf, olun, data, format'(f7.2)'
- free_lun, olun
30Reading ASCII files
- To read data from an ASCII file, use readf
- Form
- readf, lun_var, var_1, var_2, var_n,
formatfmt_string - Example
- openr, ilun, 'test.txt', /get_lun
- readf, ilun, var1, var2, var3, var4
- datafltarr(5,5)
- readf, ilun, data
- datafltarr(100)
- readf, ilun, data, format'(f7.2)'
- free_lun, ilun
31Writing Binary files
- To write data to an binary file, use writeu
- Form
- writeu, lun_var, var_1, var_2, var_n (note no
format!) - Example
- openw, olun, 'test.txt', /get_lun
- writeu, olun, 5, 6, 7, 9.0
- datafindgen(5,5)
- writeu, olun, data
- free_lun, olun
32Reading Binary files
- To read data from an binary file, use readu
- Form
- readu, lun_var, var_1, var_2, var_n
- Example
- openr, ilun, 'test.txt', /get_lun
- readu, ilun, var1, var2, var3, var4
- datafltarr(5,5)
- readu, ilun, data
- free_lun, ilun
33Mixed ASCII/Binary in a file
- Can mix ASCII and binary in a file
- Typically used to create a human readable header,
containing information on file size, lines,
samples, data range etc. - This is followed by the data in a binary form
- Example
- nx 5
- ny 7
- data findgen(nx,ny)
- openw, olun, 'test.dat', /get_lun
- printf, olun, nx, ny write a header
- writeu, olun, data
- free_lun, olun
- The header also sometimes comes in a separate
file, called a detached header
34Endian Ewoks?
- The bytes of binary data are stored differently
on different machines - If a computer stores the most significant byte of
a binary number first, they are called Big
Endian, else Little Endian - Suns, Mac OS, etc are Big Endian
- PC's are Little Endian
- Most binary PDS data, like the MOLA data, were
saved on machines that are Big Endian. - If you open and read this data on a Little Endian
machine (like the PC's in the lab), most reverse
the byte order - openr, ilun, myfilename, /get_lun,
/swap_if_little_endian - or
- xswap_endian(y)
35Homework Mars Global Pressure as a function of Ls
- Plot the variation of pressure over a season at
VL1 in units of 1 Ls. - Create a program to calculate the theoretical
atmospheric pressure for the entire surface of
Mars using MOLA data, the known altitude of the
VL-1 landing site, and the known variation in
atmospheric pressure measured at VL-1 as a
function of season. - Do a screen capture of a contour map of pressure
for Ls210 - What is the highest and lowest surface pressure
at this season? - Where are these places located in lat, lon?
- Create an image that shows MOLA topography
everyplace with a pressure gt 4 mbar for Ls210
36Homework Mars Global Pressure as a function of Ls
- Assume
- I have provided a file of MOLA data and a
detached header in - z//bambam/Users/GG710-1
- T205 K everywhere all the time
- That there is no weather on Mars creating clouds,
fronts, high or low pressure zones - all pressure
change is due to changes in altitude - That the MOLA elevation of VL-1 is -3.6 km
37Homework Mars Global Pressure as a function of Ls
- Pressure at VL-1 for given Ls
38Homework Mars Global Pressure as a function of Ls
- Plots
- Use the command plot to make x,y graphs on the
screen - plot, LS_array, P_array, xtitleLs,
ytitlePressure - see help for more details
- Use contour to make a contour plot
- this example assumes pres_array is 360, 181 in
size - lat_arrfindgen(181)-90
- lon_arrfindgen(360)
- contour, pres_array, lon_arr, lat_arr,
xtitleLon, ytitleLat - see help for more details
39Homework Mars Global Pressure as a function of Ls
- Plots
- Use the command tvscl to visualize your data
- TVSCL will make an image on screen that has the
same number of pixels as the image has size, but - The default window that IDL opens is 640 by 480
- To resize window, use the window command
- Can only make a window as big as the screen may
need to reduce the size of the image to see it
properly - The image is displayed with the first index at
the bottom left the last index at the top
right this can lead to an upside down image that
you may need to flip to see properly - window, xsize1440, ysize720
- tvscl, reverse(an_image,2)
40Homework Mars Global Pressure as a function of Ls
- Think structured programming
- Want a program that we provide Ls, and it returns
an array (or image) of pressure values - Steps
- Use the equation provided to calculate pressure
at VL-1 for given Ls - Read the MOLA data
- Determine the difference in altitude between VL-1
and rest of MOLA image - Calculate the pressures using your ASH routine
- Create a contour plot of the results
- Use functions we learned about earlier to answer
questions
41Today
- Finished Control Statements repeat, break,
continue, goto - Error Handling Next time
- Global variables
- Efficient Code
- I/O
- Homework 2
42Next Time
- More on Input and Output
- Direct Graphics
43Error Handling
- Default IDL behavior is to stop when it detects
an error, at the location of the error - Errors can be intercepted and dealt with by your
code using catch - Anytime an error occurs following a catch
command, control is handed to the statement
following catch - Catch returns an error code if there is an error
- catch, var1
- To obtain the error message associated with the
error, use the help procedure - help, /last_message, output my_var
- To end error interception
- catch, /cancel
44Error Handling
- pro test
- error_stat0
- catch, error_stat
- if (error_stat ne 0) then begin
- help, /last_message, outputerrtext
- print, errtext
- print,'On Error, the ctr was ', ctr
- stop
- endif
- xfindgen(4)3.7
- for ctr0,4 do print, xctr
- for ctr5,-1,-1 do print, xctr
- end
Establish error handler that will tell us the
value of ctr if an error occurs, then stop
execution
Loop over a short array printing values. The
2nd loop is out of range, and will cause an error
45Handling Math Errors
- Default behavior on math error is continued
operation - Math errors x1.0/0.0 xsqrt(-1.0)
- IDL tells you the type of error at the end of
routine/subroutine - Can change IDL behavior by setting system
variable !except - !except2
46Handling Math Errors
- Can check for math errors in a variable using
finite command which returns 0 if there has been
an error, and 1 if OK - x10.0, 1.0/0.0, sqrt(-1.0)
- print, finite(x)
- 1 0 0