1 / 77

Statistics Function Implementation

Statistics Function Implementation. Traditional Programming Approach. minimum.pro. function minimum, x n = n_elements(x) answer = x(0) for i=1 L , n-1 do begin if( answer gt x(i) ) then $ answer = x(i) endfor return, answer end. sum.pro. function sum, x

EllenMixel
Télécharger la présentation

Statistics Function Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Statistics Function Implementation Traditional Programming Approach

  2. minimum.pro function minimum, x n = n_elements(x) answer = x(0) for i=1L, n-1 do begin if( answergtx(i) ) then $ answer = x(i) endfor return, answer end

  3. sum.pro function sum, x n = n_elements( x ) answer = 0 for i = 0, n-1 do begin answer = answer + x(i) endfor return, answer end

  4. Overflow Problems • What if the array is an integer array? • Sums will have a maximum limit based on the number of bits specified by the integer type.

  5. Corrected sum.pro function sum, x n = n_elements( x ) answer = 0.0D for i =0L, n-1 do begin answer = answer + x(i) endfor return, answer end

  6. mean.pro function mean, x n = n_elements(x) answer = sum(x) / n return, answer end

  7. Integer Problems IDL> b=9/5 IDL> print,b 1

  8. Integer Problems IDL> b=9/double(5) IDL> print,b 1.8000000

  9. Corrected mean.pro function mean, x n = n_elements(x) answer = sum(x) /double(n) return, answer end

  10. sum_squares.pro function sum_squares, x squares = x^2.0 answer= sum( squares ) return, answer end

  11. variance.pro function variance, x n = double(n_elements(x)) answer=(sum_squares(x)-(sum(x)^2.0/n))/ (n-1) return, answer end

  12. sd.pro function sd, x answer = sqrt(variance(x)) return, answer end

  13. Shell Programming

  14. UNIX Tip of the Day • Directory maneuvering commands pushd, popd, and, dirs % cd /usr/tmp % pwd /usr/tmp % pushd ~rvrpci % pwd /cis/staff/rvrpci

  15. UNIX Tip of the Day % dirs /cis/staff/rvrpci /usr/tmp % pushd % pwd /usr/tmp % dirs /usr/tmp /cis/staff/rvrpci % pushd /usr/local/bin

  16. UNIX Tip of the Day % dirs /usr/local/bin /usr/tmp /cis/staff/rvrpci % pwd /usr/local/bin % pushd % dirs /usr/tmp /usr/local/bin /cis/staff/rvrpci

  17. UNIX Tip of the Day % dirs /usr/tmp /usr/local/bin /cis/staff/rvrpci % pwd /usr/tmp % pushd +2 % pwd /cis/staff/rvrpci

  18. UNIX Tip of the Day % dirs /cis/staff/rvrpci /usr/tmp /usr/local/bin % popd % dirs /usr/tmp /usr/local/bin

  19. IMPORTANT UNIX Concepts • Environment and Shell Variables • These allow you to customize your UNIX environment • They are different in terms of their SCOPE • SCOPE determines the visibility of a variable

  20. Other IMPORTANT UNIX Concepts • Environment Variable • Examples are TERM and DISPLAY • Set a particular variable to a value by using the setenv command • You can print the value of a particular variable or all the environment variable using the printenv command

  21. % Environment Variables % • Examples • To set environment variables % setenv TERM vt100 % setenv DOG Goofy • print out the terminal type % printenv TERM vt100 • print out all environment variables % printenv

  22. Shell Variables • Shell variables are similar to Environment variables except they have a limited scope, i.e., they exist only in the shell which they are defined. • Environment variables on the other hand, exist in all its children shells • To illustrate this concept, let us look at the following example

  23. Environment vs. Shell Variables % set prompt = "Parent Shell > " Parent Shell > setenv DOG Goofy Parent Shell > set mouse=Mickey Parent Shell > printenv DOG Goofy Parent Shell > echo $mouse Mickey Parent Shell > xterm & (YOU SHOULD NOW HAVE A NEW xterm WINDOW) THIS IS KNOWN AS “SPAWNING A NEW (OR CHILD) PROCESS”

  24. Environment vs. Shell Variables (IN THE NEW xterm WINDOW, DO THE FOLLOWING) % set prompt = "Child Shell > " Child Shell > printenv DOG Goofy Child Shell > echo $mouse mouse: Undefined variable.

  25. Environment vs. Shell Variables Child Shell > setenv DOG Pluto Child Shell > set mouse=Minnie Child Shell > printenv DOG Pluto Child Shell > echo $mouse Minnie Child Shell > exit (THE xterm WINDOW SHOULD NOW GO AWAY - THIS PROCESS HAS NOW BEEN KILLED)

  26. Environment vs. Shell Variables Parent Shell > Parent Shell > printenv DOG Goofy Parent Shell > echo $mouse Mickey Parent Shell >

  27. Environment & Shell Variables • Why is this important? • UNIX uses Environment and Shell Variables control a number of processes • Customizes your working environment • Variables used for UNIX Scripts • They are typically defined and initialized in your .login and .cshrc files

  28. Useful Shell Variables filec #Allows file completion path #List of command directories cdpath #List of candidate directories to cd into history #Number of commands to remember

  29. What is shell programming? • Shell programming • automate a set of UNIX commands. • Just like any programming language • “wrappers” • black box a customized collection of UNIX commands. • Example of shell programs .login .cshrc

  30. .loginfile set path=($HOME/bin /usr/local/bin \ /usr/ucb /usr/sbin /bin /usr/bin \ /usr/bin/X11 .) stty dec new tset -I -Q set mail=/usr/spool/mail/$USER set editmode = emacs umask 077 biff n date

  31. .cshrcfile if ($?prompt) then set notify set history = 100 set savehist = 100 alias pd pushd alias pop popd alias vt100 "set term = vt100" endif

  32. When these files are executed? .cshrc • is automatically executed when you start a new shell .login • only gets executed once when you first login Can be re-executed by giving the source command % source .cshrc

  33. Other useful .login and .cshrc entries set filec set cdpath=(~ ~rvrpci/pub ~/mythesis) Other common entries set path=( $path /usr/local/bin) set path=(/usr/local/bin $path)

  34. User defined shell program • Determine name of command • Determine input, output, and option arguments • Determine UNIX commands to execute • Establish error trapping • Make shell program executable

  35. A simple shell program • ddcommand to swap bytes % dd if=input.dat of=output.dat bs=2 conv=swab • Very difficult to remember • Very little utility to non-UNIX geeks (normal people)

  36. We would rather see... % swap_bytes input.dat output.dat

  37. Special Shell Variables Set % swap_bytes input.dat output.dat $0 $1 $2 command $argv[1] $argv[2]

  38. Another Special Shell Variables % swap_bytes input.dat output.dat $#argv Indicates how many arguments are present In this case, 2

  39. shell programswap_bytes #!/bin/csh -f dd if=$1 of=$2 bs=2 conv=swab

  40. Making swap_bytesshell script executable % ls -l swap_bytes -rw------- ... swap_bytes % chmod u+x swap_bytes % ls -l swap_bytes -rwx------ ... swap_bytes

  41. To run swap_bytes • swap_bytesbecomes just another unix command! % swap_bytes input.dat output.dat

  42. Limitation of swap_bytes • No error trapping • Should give usage when typing command % swap_bytes usage: swap_bytes input_file output_file

  43. Improvement to swap_bytes #!/bin/csh -f if ( $#argv != 2 ) then echo "usage: $0 input_file output_file" exit 1 endif dd if=$1 of=$2 bs=2 conv=swab

  44. Commad exit status • By convention exit 0 Indicates successful command completion exit 1(or non-zero) Indicates some error condition

  45. Informational message from swap_bytes • UNIX style informational message % swap_bytes usage: swap_bytes input_file output_file

  46. Interactive swap_bytes • If you want a “friendlier” shell program • Have it query the user for the inputs • Another special shell variable can be used $<

  47. Interactive swap_bytes #!/bin/csh -f if ( $#argv != 2 ) then echo -n "Please enter the input file> " set input=$< echo -n "Please enter the output file> " set output=$< endif dd if=$input of=$output bs=2 conv=swab

  48. Interactive swap_bytesexample • User simply types the command % swap_bytes Please enter the input file> input.dat Please enter the output file> output.dat

  49. UNIX Quotes

  50. A note about quotes in UNIX % set a=ls % echo a % echo $a % set b=“$a” % echo $b % set b=‘$a’ % echo $b % set b=`$a` % echo $b

More Related