File IO - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

File IO

Description:

Record length (if direct access. OPENing files. To open a file (whether writing to or from) ... the length is 1, can omit for any data type. Note the length of ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 19
Provided by: beth74
Category:
Tags: file | length

less

Transcript and Presenter's Notes

Title: File IO


1
File I/O
  • Every I/O file has
  • A unique identifying number
  • A unique file name
  • ASCII (or text files) also have
  • A newness status (old, unknown, new)
  • Binary files have
  • Access (either direct or sequential)
  • Direct meaning each line can be accessed uniquely
  • Sequential meaning lines have to be read in, in
    order
  • Form (either formatted or unformatted)
  • Formatted - data is represented in external
    character form
  • Unformatted - data is represented in internal
    binary form
  • Record length (if direct access

2
OPENing files
  • To open a file (whether writing to or from)
  • ASCII examples
  • open(1,filegroupstats.txt,statusold)
  • open(14,filefname,statusunknown)
  • Rarely use new -- too restrictive
  • Filename must be a string - no numbers
  • ID number could be a variable
  • Binary examples
  • open(2,fileaugtemp.bin,accessdirect,
    formunformatted,recl42144)
  • Recls 4 at end is machine dependent. Refers to
    size of a byte

3
CLOSE and INQUIRE
  • Just need ID number to close
  • close(2)
  • INQUIRE provides information on a file that was
    opened
  • inquire(2,existfileexist)
  • Where exist is a keyword, and fileexist is an
    arbitrary variable name of type LOGICAL
  • if(fileexist.eq.T) then

4
Free formatting ()
  • An asterisk () denotes free format I/O
  • read , - read input freely from keyboard that
    is delimited either by white space or commas.
    Character/strings must be enclosed by
    apostraphies (e.g. Beth)
  • read(1,) - read in data that is space or comma
    delimited from the file recognized as unit 1.
    Character strings need to be surrounded by
    apostrophes (very rarely does this occur)
  • print , or write(,) - prints output freely to
    screen with only default format
  • print 20, or write(20,) - writes output to the
    file with the unit ID of 20
  • Free formatted files are not lined up in columns

5
Fixed Formatted I/O
  • Fixed format means a specified number of columns
    is associated with each variable or space of
    interest
  • 123456789 lt-- column
  • 1983 Bob6
  • 2002 Tim9
  • Formats for
  • Integers I
  • Reals F
  • Characters A
  • Spaces X

6
More Fixed Formatting
  • Example
  • 04092003 Sand Harbor -114.532 40.628 12.2
  • To read in the following variables from this
    line month, day, year, name, lon, lat, temp
  • Format code i2,i2,i4,1x,a11,1x,f8.3,1x,f6.3,f11.
    1
  • If the length is 1, can omit for any data type
  • Note the length of a real number is lttotal
    columnsgt.ltcolumns after decimalgt
  • You do NOT have to list the format for the entire
    line.
  • Could have left this lines format as
    i2,i2,i4,1x,a11
  • Use X format for skipping anything
  • If wanted to ignore the name, could have use the
    following i2, i2, i4,13x,f8.3
  • Can use multipliers for constant patterns
  • 2i2,i4,13x,f8.3
  • a11,6(i3,f4.0)

7
FORMAT statement and ID
  • 04092003 Sand Harbor -114.532 40.628 12.2
  • integer month, day, year
  • real temp(12,31,19992003)
  • open(1,filetemp.txt,statusold)
  • read(1,1000) month, day, year,
  • temp(month,day,year)
  • 1000 format(i2,i2,i4,28x,f11.2)
  • open(10,filetemp.out,statusunknown)
  • write(1,1010) year, month,
    temp(month,9,year)
  • 1010 format(i4,i2.2, 09,f6.2)

8
Format shorthand
  • If format is short, can include it in the
    read/write statement
  • read(13,(i6,f3.2)) idate, rh
  • write(1,(6i2)) month, day, year, hour, min, sec
  • Read(10,(12f4.2)) (monTemp(I),I1,12)

9
Binary format I/O
  • Since unformatted, need to specify record number
    if direct access
  • open(1,filetemp.bin,accessdirect,formunfor
    matted,recl124)
  • read(1,rec1)(temp(I),I1,12)
  • open(1,filemodelgrid.bin,accessdirect,form
    unformattedrecl128644)
  • read(1,rec32)((temp(ilon,ilat),ilon1,128),ilat1
    ,64)
  • For binary files, need to know
  • How many records are there?
  • How long is each record
  • How is each record written out if more than one
    dimension

10
We Try It!
  • Calculate the average amperage of the lightning
    strike data in
  • /data2/fortran_class/transfer/lightning.sm.txt

11
We Try It!
  • Using /data2/fortran_class/transfer/drd964x.pdsi.t
    xt (monthly pdsi data for a particular climate
    division), find the following
  • Worst drought month on average
  • Worst drought year and month
  • Worst drought year

12
You Try It!
  • Using data from /data2/fortran_class/transfer/wx.2
    60203.fwx, answer the following questions
  • What is the average August temperature?
  • What month has the most precip amount?
  • Which year was driest?

13
Characters to numbersand vice versa
  • To convert a number to a string
  • write(cvar,(i4.4)) ivar
  • To convert a string to a number
  • read(cvar,(i4.4)) ivar
  • Note the structure for both ways is
  • XXXX(ltstring-vargt,ltformatgt) ltnum-vargt
  • Only difference is whether you write or read

14
Random Number Generator
  • Never truly random
  • Use seeds of numbers based upon computer clock
  • Algorithm based upon the seeds
  • UNIX command for date and time
  • date ymdHMS
  • This number is different every time program runs

15
Code for Random Generator
  • We write it together

16
You Try It!
  • The Game of Life
  • Create a 25x25 grid. Fill the grid with random
    binary values (e.g., 1 or 0, X or ). Each
    square has 8 neighbors with exception to border
    (which is infertile). Run generations with
    following rules
  • Birth Organism will be born in each empty
    cell that has exactly 3 neighbors
  • Death Organism with 4 neighbors will die
    from overcrowding. Organism with fewer than 2
    will die of loneliness
  • Survival An organism with 2 or 3 neighbors
    will survive to next generation

17
Creating file lists and reading from them
  • To create a file of a list of files - UNIX
  • ls -1 04.fw9 gt calsites.fw9.lst
  • The -1 is an argument that means just list a
    filename one line at a time
  • The gt means instead of writing the list to the
    default output device (i.e., the monitor/screen),
    write it to the file called calsites.fw9.lst

18
We Try It!
  • Compute the average August temperature, relative
    humidity, and wind speed for 8 different
    California weather stations
Write a Comment
User Comments (0)
About PowerShow.com