230 likes | 403 Views
MET 50. More on… MAKING YOUR OUTPUT LOOK NICE!. Pretty output. Recall the more sophisticated PRINT statement: PRINT nn , GRAV nn FORMAT ( stuff ) We use the PRINT command to print output to the screen (today).
E N D
MET 50 More on… MAKING YOUR OUTPUT LOOK NICE!
Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the PRINT command to print output to the screen (today). We use the WRITE command to print output to the printer or a data file (next topic). MET 50, FALL 2011, CHAPTER 5 PART 2
Pretty output PRINT nn, GRAV nn FORMAT (stuff) • “nn” is called a label number, and must be an integer. • The FORMAT line tells the compiler which format to use for output • and you get to define the format MET 50, FALL 2011, CHAPTER 5 PART 2
MET 50 WRITING TO AND READING FROM FILES
Files To read from a file, we still use the READ statement. To write output to a file, we use the WRITE statement. MET 50, FALL 2011, CHAPTER 5 PART 2
Files WRITE (mm , nn) (things to write) Tells us the FORMAT of what we write Tells us where we write the output to MET 50, FALL 2011, CHAPTER 5 PART 2
Files READ (mm , nn) (things to write) Tells us the FORMAT of what we read Tells us where we read the input from MET 50, FALL 2011, CHAPTER 5 PART 2
Files Example… WRITE (25 , 16) X1, X2, X3 16 FORMAT (1X, 3F10.2) Also… WRITE (mm , nn) X1, X2, X3 “mm” and “nn” must be integer WRITE (* , nn) directs output to screen MET 50, FALL 2011, CHAPTER 5 PART 2
Files Guidance for values of “mm” … In the old days, WRITE (6 , nn) - directed output to the printer READ (5 , nn) - read input from the screen Caution: the values ‘5’ and ‘6’ might be hard-wired into old code. MET 50, FALL 2011, CHAPTER 5 PART 2
Files Files… A file is like a book. It needs to be opened, closed, flipped thru etc. We have commands for this! MET 50, FALL 2011, CHAPTER 5 PART 2
Files Opening a file… OPEN (UNIT=mm, FILE=“name of file”, STATUS=“OLD or NEW or replace”, ACTION=“READ or WRITE or READWRITE”, POSITION=“REWIND or APPEND or ASIS”, IOSTAT=integer number) MET 50, FALL 2011, CHAPTER 5 PART 2
Files Example… OPEN (UNIT=12, don’t need to write “UNIT” FILE=“temperature_data_1977.dat”, name STATUS=“OLD”, file already exists ACTION=“READWRITE”, usually omit POSITION=“ASIS”, often omit IOSTAT=integer number) see later… MET 50, FALL 2011, CHAPTER 5 PART 2
Files Simplified example… OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) MET 50, FALL 2011, CHAPTER 5 PART 2
Files Code example…running this… REAL :: DATE, TEMP add code here to generate some data to write! OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“NEW”, IOSTAT=IOS) WRITE (12,22) DATE, TEMP 22 FORMAT (1X, 2F12.4) After you compile and run this code, you would find that the file “temperature_data_1977.dat” has appeared in your directory! MET 50, FALL 2011, CHAPTER 5 PART 2
Files And reading from a file? REAL :: DATE, TEMP OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) READ (12,22) DATE, TEMP 22 FORMAT (1X, 2F12.4) IF (IOS > 0) THEN PRINT*,’Bad data or missing data with IOS=‘, IOS ENDIF add some code to do something with this data!!! MET 50, FALL 2011, CHAPTER 5 PART 2
Files Two three more things… (1) REWIND command Sometimes we may need to : OPEN a file READ the data READ it all again from the start MET 50, FALL 2011, CHAPTER 5 PART 2
Files Code could include these lines… OPEN (12, file=‘name.dat’, status=‘OLD’) READ (12,100) X,Y,Z 12 FORMAT (blablabla) do some calculations REWIND (12) READ (12,100) X,Y,Z do some more calculations CLOSE (12) MET 50, FALL 2011, CHAPTER 5 PART 2
Files (2) Forms in which data is stored Either: text data Or: binary data MET 50, FALL 2011, CHAPTER 5 PART 2
Files text data Looks just like regular characters on the screen IS readable! Read: http://en.wikipedia.org/wiki/Data_file Example…online (/121) MET 50, FALL 2011, CHAPTER 5 PART 2
Files binary data Looks unintelligible Is NOT! Example…online (/121) MET 50, FALL 2011, CHAPTER 5 PART 2
Files (3) Reading & writing binary data This is NOT formatted! Space-saving solution for massive datasets. MET 50, FALL 2011, CHAPTER 5 PART 2
Files Code would include lines like this… OPEN (12, file=‘name.dat’, status=‘OLD’, form=‘unformatted’) READ (12) X,Y,Z note no FORMAT statement! and WRITE (12) X,Y,Z ditto! MET 50, FALL 2011, CHAPTER 5 PART 2
Files Large datasets often arrive in binary form. A new paradigm in atmospheric science is “netCDF” MET 50, FALL 2011, CHAPTER 5 PART 2