1 / 20

MET 50

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. Files. WRITE ( mm , nn ) (things to write). Tells us the FORMAT of what we write. Tells us where we

dean
Download Presentation

MET 50

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. MET 50 WRITING TO AND READING FROM FILES

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. Files Simplified example… OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) MET 50, FALL 2011, CHAPTER 5 PART 2

  11. 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

  12. 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

  13. 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

  14. 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

  15. Files (2) Forms in which data is stored Either: text data Or: binary data MET 50, FALL 2011, CHAPTER 5 PART 2

  16. 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

  17. Files binary data Looks unintelligible Is NOT! Example…online (/121) MET 50, FALL 2011, CHAPTER 5 PART 2

  18. Files (3) Reading & writing binary data This is NOT formatted! Space-saving solution for massive datasets. MET 50, FALL 2011, CHAPTER 5 PART 2

  19. 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

  20. Files Large datasets often arrive in binary form. A new paradigm in atmospheric science is “netCDF” MET 50, FALL 2011, CHAPTER 5 PART 2

More Related