1 / 35

BIL 102 for VIS

BIL 102 for VIS. Week 12. bidirectional data transfer I/O input output. list directed default , unformatted formatted user defined , explicit format control. I / O Statements. I / O Statement Forms An input or output statement has the following form.

lawton
Download Presentation

BIL 102 for VIS

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. BIL 102for VIS Week 12

  2. bidirectional data transferI/O input output • list directed • default , unformatted • formatted • user defined , explicit format control

  3. I / O Statements I / O Statement Forms An input or output statement has the following form. read (unit=2, fmt=”(2i5, a, 2f16.8)”) I, Code, Str, Bugle, Dpr write (unit=*, fmt=”(f10.3, i6, f10.3,a)”, advance =”no”) A, B, C,” # ” write (unit=*, fmt=*) Code, X+Y+Z The positioning specifier advance=”no” in the write statement states that the output file is not to be advanced to a new record after A, B and C have been written to the file. Thus the second write statement will append additional data to the same output record.

  4. I / O Statements Direction of Transfer The direction of transfer is determined by the initial keyword in the statement. The keyword read denotes input, while write denotes output. External Device Each external device is identified with a unique integer value called a unit number. A given unit number represents the same device in the main program and in all of its modules and subprograms. Thus, unit numbers may be said to have global scope.

  5. I/O stmts • input • read (control list) list • read (unit=1,fmt=‘(i4,f3.2)’) Inum,Aver • output • write (control list) list • write (unit=6,fmt=‘(i4,f3.2)’) Inum,Aver • print(format specifier) list • print *, Inum, Aver

  6. control list • unit specifier : External unit/device to/from data is transferred • unit= • format specifier : Explicit specification of conversion between internal and external data • fmt= • positioning, i/o status • other specifiers

  7. Data Transfer RECORDS Information on a file subdivided into records. A record corresponds roughly to a line on a terminal screen or to a printed line. The structure of unformatted records is processor-dependent, since the amount of space required on the external unit depends on the details of processor data representation. FORMATTED RECORDS A formatted record is composed of characters. These characters may include letters, digits, and special symbols from the ASCII character set. NONADVANCING INPUT AND OUTPUT Input and output always positions the input or output file, as a default option, at the end of the last record to or from which information has been transferred. In some cases this style of input and output is not convenient. It would often be preferable to stop data transfer in the middle of an input or output record and to continue later with the remainder of the same record. Instead of default advancing file-positioning style, output statement may request a non advancing style, whereby the file position may remain within a record after the input or output statement has been executed. (See example 9.5, p400, Meissner`s book)

  8. list directed I/O • syntax • read (unit=1,fmt=*) Inum,Rnum • write (unit=2,fmt=*) Inum,Rnum • -------- • 1 2.31 Inum=1 Rnum=2.31

  9. format codes • * list directed • i integer code i4 • f real code f6.2 • l logical code l • e exponential code e11.4 • a character code a8

  10. files • external source • data obtained from (read opr) • external destination • data sent to (write opr)

  11. Files • A file is an external source from which data may be obtained or an external destination to which it may be sent. Data can not be transferred to or from a file until the file is connected to the program. CONNECTION PROPERTIES A connection between a unit and a file has certain connection properties. When a unit becomes connected to a file by execution of an open statement, the following properties may be established: 1) An access method, sequential or direct, is always established. 2) A form, formatted or unformatted, is always established. 3) An initial position, rewind or append, specifies initial positioning of the file during execution of open statement 4) A permanence property, temporary or permanent, specifies disposition of the file when the connection is terminated. 5) An action property, read, write or read-write, specifies which kinds of

  12. Files Data transfer statements are permitted. 6) A record length may be established. The OPEN Statement The statement consists of the keyword open followed by a control list. A control list must include the following specifiers. 1) status = Character expression, where the value of Character expression is old, new, replace, or scratch. 2) action = Character expression, where the value of Character expression is read, write, or readwrite. 3) position = Character expression, where the value of character expression is rewind or append. The followings are optional specifiers 4) access = Character expression, where the value of character expression is sequential or direct (default is sequential).

  13. Files 5) form = Character expression, where the value of Character expression is formatted or unformatted (default formatted). 6) recl = Integer expression, where the value of Integer expression is positive. 7) iostat = Integer variable name. Here some examples for open statement: open (unit = 6, file =“My_Data.bin”, status =“old”, action =“read”,& form = “unformatted”) open (unit = J, file =“My_Input.txt”, status =“old”, action =“read”) open (unit = 31, file =“X23”, status =“old”, action =“read”, & access =“direct”, recl =720)

  14. I / O positioning INPUT POSITIONING Let’s consider an existing file has been connected to unit 7 for sequential access. A rewind initial position property for the connection was specified in the open statement. Suppose that several read statements are then executed. 1) If the file is unformatted, each read will advance the file by one record. 2) If the file formatted, each advancing read statement will advance the file by one record. 3) Each non advancing read from a formatted file will advance the file when a slash is encountered in the format or when rescan occurs, and the position remains within the current record at the end of execution of the statement. Let us suppose that the end-of-file indicator is encountered. The file is then positioned following the end-of-file indicator, and indication of the end-of-file condition is returned to the program.

  15. I / O positioning OUTPUT POSITIONING Now let’s consider a file that has to be used for sequential output. It is connected to unit 13 and contains no records, no indication of the end-of-file condition. A rewind initial position property for the connection was specified in the open statement. Suppose that several write statements are then executed. 1) If the file is unformatted, each write will add one record to the file. 2) If the file formatted, each advancing write will add at least one record. 3) A non advancing write will add a new record when a slash is encountered in the format or when rescan occurs, but will not end the record after data transfer. After the desired data has been transmitted to the file, the statement end file may be executed to append an end-of-file indicator following the last record written.

  16. connection properties • access • sequential / direct (random access) • form • formatted / unformatted • initial position • rewind / append • permanence • temporary / permanent • action • read / write / wr • record length • fixed / variable

  17. opening • syntax : • open (control list specifiers) • unit= • integer expr • file= • character data • status= • old / new / scratch / replace • action= • read / write / readwrite • access= • sequential / direct • form= • formatted / unformatted • recl= • integer expr • iostat= • int vrbl name

  18. closing • syntax • close (control list spesifiers) • close (unit=5) • close (unit=Iunit,iostat=Code,status=“delete”)

  19. explicit positioning • rewind (control list) • rewind (unit=3,iostat=Iocode) • backspace (control list) • backspace (unit=Iunit,iostat=Code)

  20. Direct access input and output The connection of a file to a unit has an access method, which is sequential or direct. Direct access means that records may be accessed in an arbitrary sequence. Each record of the file has a unique record number that is established when the record is written and does not change. A Record number specifier in the Control List of each read or write statement indicates which record is to be read or written; this specifier has the form rec = Record number Record number is an integer expression with a positive value. Example for direct access output statement: write (unit = 3, fmt = *, rec = 76) X, Y, Z, Z5 • write statement sends information to record number 76 of unit 3.

  21. random access • direct accessing • open (unit=1,file=“wr”,access=“direct”, & • recl=100, status=“replace”, action=“readwrite”) • do Irec=1,10 • write (unit=3,fmt=“(3i5)”,rec=Irec) I1,I2,I3 • end do • do Irec=10,1,-1 • read (unit=3,fmt=“(3i5)”,rec=Irec) I1,I2,I3 • end do

  22. ex • Save on a file the informations of students in the class • Randomly ask informations of the students and change them if necessary

  23. part 1 program class1 implicit none real:: Note,Number integer::Recn character(len=10):: Name open (unit=1,file="class1.dat",access="direct",& status="replace",action="readwrite",recl=45,& form="formatted")

  24. part 2 Recn=0 do write (unit=*,fmt="(2x'enter name ')",advance="no") read (unit=*,fmt=*) Name if (Name=="end") then exit else Recn=Recn+1 write (unit=*,fmt="(2x'enter no,note ')",advance="no") read (unit=*,fmt=*) Number,Note end if write (unit=1,fmt="(a5,2f15.3)",rec=Recn) Name,Number,Note end do print *,"Input ended"

  25. Part 3 do write (unit=*,fmt="(2x'enter no ')",advance="no") read (unit=*,fmt=*) Recn if (Recn==0) then exit else read (unit=1,fmt="(a5,2f15.3)",rec=Recn) Name,Number,Note write (unit=*,fmt="(2x,'student ',a5,' with number',f15.3,2x,' has grade ‘& ,f5.0)") Name,Number,Note end if end do close (unit=1) stop end program class1

  26. ex • problem You have a target at a given distance. You are commender of a gun. You give the necessary initial velocity of the bullet and the barrel angle of the gun Try to hit the target with less guess as possible ‘ otherwise you will be hitted :-) ‘

  27. problem • Visualize the trajectory of the bullet up to the hit

  28. part 1 program trajec implicit none real::Dist,Displ,Vel,Init_vel,Angle,Diff,Adiff,Time,& Ang_sin,Ang_cos,Sx,Sy integer::Itime,Utime,Guess character(len=1)::Answ character(len=5)::Miss real,parameter::Grav=9.81,Delt_t=0.5,Rad_cnv=0.01745,Alpha=1

  29. part 2 write (unit=*,fmt="(2x'dist to target ? ')",advance="no") read (unit=*,fmt=*) Dist Guess=0 do Guess=Guess+1 write (unit=*,fmt="(2x'Init veloc ')",advance="no") read (unit=*,fmt=*) Init_vel write (unit=*,fmt="(2x'Init angle ')",advance="no") read (unit=*,fmt=*) Angle Ang_cos=cos(Angle*Rad_cnv) Ang_sin=sin(Angle*Rad_cnv) Time=Init_vel*Ang_sin/(0.5*Grav) ! Displ=2*Init_vel**2*Ang_sin*Ang_cos/Grav Displ=Init_vel*Ang_cos*Time Diff=Displ-Dist

  30. part 3 Adiff=abs(Diff) if (Diff>0) then Miss="Far" else Miss="Short" end if if (Adiff<=Alpha) then exit else write (unit=*,fmt="(2x,' you missed by ',f6.1,' meters ',a5)") Adiff,Miss end if end do print *," !!!!!!!!!!!!" print *," You get it in ",Guess," guesses" print *," !!!!!!!!!!!!" print *,"calculates trajectory and save to file"

  31. part 4 print *,"calculates trajectory and save to file" open (unit=2,file="trajec.dat",action="write",status="replace") Utime=int((Time*100)+0.5) do Itime=0,Utime,1 Time=Itime/100.0 Sx=Init_vel*Ang_cos*Time Sy=-0.5*Grav*Time**2+Init_vel*Ang_sin*Time write (unit=2,fmt="(3f10.2)") Time,Sx,Sy end do stop end program trajec

  32. Unformatted input and output The connection of a file to a unit has a form which is formatted or unformatted. Unformatted files usually correspond to so-called binary files, whereas formatted files correspond to text files. Unformatted records contain information in a processor-dependent form that is obtained by transferring data to and from internal storage without any editing or other transformation. The external representation of the data corresponds exactly to its internal representation. Thus, unformatted data transfer is more efficient because the data is merely copied between the external device and the internal storage of the computer. Formatted records are composed of characters representing data values; a formatted record corresponds to a printed line. Conversion is required between the internal form of the data and its representation on the file. • Do example 9.10 (Meissner’s book)

  33. formatted internal data transfer Formatted internal data transfer changes the representation of data without actually transmitting it to or from an external device. Formatted external data transfer includes two separate processes: a) The change in representation of data between its internal form and its external form. b) The transmission of data to or from the external device. In an internal data transfer statement, the integer expression or asterisk Unit specifier in the control list is replaced by the name of a character variable that serves as a buffer or “internal file”. • Do example 9.11 (Meissner’s book)

  34. visualisation • output saved to a file • file read by an application • visualisation

  35. MS-EXCEL • open an existing file • file type txt • specify delimitaton of columns • get file • choose range to be visualise • ok

More Related