Download presentation
Presentation is loading. Please wait.
1
WRITING TO AND READING FROM FILES MET 50
2
Files To read from a file, we still use the READ statement. To write output to a file, we use the WRITE statement. 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 2
3
Files WRITE (mm, nn) (things to write) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 3 Tells us the FORMAT of what we write Tells us where we write the output to
4
Files READ (mm, nn) (things to write) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 4 Tells us the FORMAT of what we read Tells us where we read the input from
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 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 5
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. 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 6
7
Files Files… A file is like a book. It needs to be opened, closed, flipped thru etc. We have commands for this! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 7
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) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 8
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… 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 9
10
Files Simplified example… OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 10
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! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 11
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!!! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 12
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 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 13
14
Files Code could include these lines… OPEN (12, file=‘name.dat’, status=‘OLD’) READ (12,100) X,Y,Z 12 FORMAT (bla bla bla) do some calculations REWIND (12) READ (12,100) X,Y,Z do some more calculations CLOSE (12) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 14
15
Files (2) Forms in which data is stored Either:text data Or:binary data 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 15
16
Files text data Looks just like regular characters on the screen IS readable! Read: http://en.wikipedia.org/wiki/Data_filehttp://en.wikipedia.org/wiki/Data_file Example…online (/121) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 16
17
Files binary data Looks unintelligible Is NOT! Example…online (/121) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 17
18
Files (3) Reading & writing binary data This is NOT formatted! Space-saving solution for massive datasets. 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 18
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,Zditto! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 19
20
Files Large datasets often arrive in binary form. A new paradigm in atmospheric science is “netCDF” 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.