MAST667-010 Coastal Oceanography: From Physics to Fish

Spring 2001

Instructor Andreas Münchow

Workshop #1 (Mar. 5, 2001)

The purpose of this workshop is to familiarize you with a low-level programming environment like DOS or UNIX where programs and files are written, stored, and run. All tasks could also be performed in more complex environments such as Windows2000 or similar, however, the required software overhead can be excessive and prone to error. This and subsequent exercises will exposure you to both UNIX and C-like programing. You will learn a robust scripting language (AWK or NAWK) that is ideally suited for simple data and text string manipulations.

1. Open a DOS (Disk-Operating-System) window and change disk drives to D:

>d:

2. Make a subdirectory MAST667 where you will perform subsequent tasks:

>mkdir mast667

3. Change directory to mast667:

>cd mast667

4. Make subdirectories called "software" and "data", respectively.

5. Change directory to software.

6. Copy software files from the floppy disk to the hardrive:

>xcopy a:*.* d:\mast667\software

7. Specify the location (path) of the software that you will use subsequently from the data or other directories:

>path=d:\mast667\software

8. Open a Netscape window and find the last 5 days of sealevel data for Lewes (the class link page may prove useful).

9. Save the data as an ASCII text file in the d:\mast667\data directory.

10. Go back to the DOS window, check that the data file is in the directory where you expect it to be:

>dir

11. Use a simple full screen editor called "fsed" to create a control file for subsequent commands:

>fsed getdata.bat

12. Type the following text string in the newly opened file:

---echo I wrote this code

13. Save your file and exit it by hitting the F10 key.

14. Run your program by typing from the DOS command line

>getdata

15. Next, change your program (getdata.bat) by adding a computer program that counts the number of lines in a file with the name "input"; the result is written to a file called "output" (you could also leave the output redirection pipe ">output" out in which case the result will be written to screen):

---awk ‘END{print NR}’ input >output

16. Again, run your just modified program by typing from the DOS command line

>getdata

17. Next you will create an explicity awk script to be stored in the file "getdata.awk":

>fsed getdata.awk

18. Write your program getdata.awk:

---{ #this bracket opens the program

--- print $1,$2,NR #this statement writes column 1, 2, and line number from input to output

---} #this bracket closes the program

---END #this indicates a set of operations conducted after all data are read in

--- { #as before, this opens a program

--- print NR #this writes the number of lines in the file to output

--- } # as before this closes the loop

18a, The above code can also be written more compactly as

--- { print $1,$2,NR}

--- END{ print NR}

19. Modify your control file getdata.bat

>fsed getdata.bat

20. Add a line that tells the command script getdata.bat how to execute your program getdata.awk

---awk -f getdata.awk input >output

21. Run your program:

>getdata

22. Based on the above, can write a small program that computes the mean sea level in Lewes?

23. Based on the above, can you write a small program that removes the computed mean from all values of sealevel? [I can and will help you with this.]

24. Based on the above, can you write a small program using the time information to predict Lewes sealevel given the five tidal amplitude and phases that I gave you for the home work?

Appendix:

The following awk functions will prove useful

--- pi = atan2(1,1) #get the number pi from the arctan(45 degrees)

sin(arg) #arg is dimensionless, but measured in radians (fractions of pi)

cos(arg)

A useful code is the following looping that you could do after all data are read in, that is, after the END statement:

---for (i=1;i<=NR;i++)

{ x[i] = x[i]-xbar

}

There are many more useful and tricky ways to manipulate data that I love to help you with. Rather than listing them all here early on, I will provide plenty of examples around which you can develop your own favorite tools and commands.