Docsity

Formatting Output in Fortran: Controlling Spaces, Descriptors and Statement Numbers, Study notes of Aerospace Engineering

Toggle sidebar
bg1
D. Haugli, Lecturer Aer E 160 Aerospace Engineering
9/5/2003 Formatted Output, Page 1 Iowa State University
Formatted Output
Consider the variables step, velocity, units and direction. Assume step is an integer,
velocity is real, units is a character string and direction is logical and that the values of
each are,
units = "ft/s"
step = 14
direction = .true.
velocity = 14.422
List-directed write statements of the form,
WRITE (*,*) "At step ", step, ", the velocity is ", velocity, units,"."
WRITE (*,*) "Upward? ", direction, "."
would create output (to the screen) that might look something like this:
At step 14 , the velocity is 14.42200 ft/s
Upward? T .
The output, while in the correct order, contains extra spaces and extra digits (on velocity). How
could the format be improved (i.e., controlled)? Through the use of FORMAT statements:
WRITE (*,10) step, velocity, units
10 FORMAT (1X,"At step ",I2,", the velocity is ",F6.3,1X,A4,".")
WRITE (*,12) direction
12 FORMAT (1X,"Upward? ",L1,".")
These statements would produce the output,
At step 14, the velocity is 14.422 ft/s.
Upward? T.
This output is free from extra spaces and has the correct number of digits for velocity.
Statement Numbers. FORMAT statements are preceded by a number (sometimes called a label),
and that number is used in WRITE statements to refer to the FORMAT statement. A single FORMAT
statement may be referenced multiple times (i.e., with more than one WRITE statement). The
number can be anywhere from 1 to 99999 and is at the discretion of the programmer.
Suggestions:
• When possible, put FORMAT statements next to the WRITE statements that reference them
(for clarity and readability).
• Keep statement numbers in order (from lowest to highest, for example) to make finding the
statement number a matter of looking downward through the file.
pf3
pf4
pf5

Partial preview of the text

Download Formatting Output in Fortran: Controlling Spaces, Descriptors and Statement Numbers and more Study notes Aerospace Engineering in PDF only on Docsity!

D. Haugli, Lecturer Aer E 160 Aerospace Engineering

Formatted Output

Consider the variables step, velocity, units and direction. Assume step is an integer, velocity is real, units is a character string and direction is logical and that the values of each are,

units = "ft/s" step = 14 direction = .true. velocity = 14.

List-directed write statements of the form,

WRITE (,) "At step ", step, ", the velocity is ", velocity, units,"." WRITE (,) "Upward? ", direction, "."

would create output (to the screen) that might look something like this:

At step 14 , the velocity is 14.42200 ft/s Upward? T.

The output, while in the correct order, contains extra spaces and extra digits (on velocity). How could the format be improved (i.e., controlled)? Through the use of FORMAT statements:

WRITE (,10) step, velocity, units 10 FORMAT (1X,"At step ",I2,", the velocity is ",F6.3,1X,A4,".") WRITE (,12) direction 12 FORMAT (1X,"Upward? ",L1,".")

These statements would produce the output,

At step 14, the velocity is 14.422 ft/s. Upward? T.

This output is free from extra spaces and has the correct number of digits for velocity.

Statement Numbers. FORMAT statements are preceded by a number (sometimes called a label ), and that number is used in WRITE statements to refer to the FORMAT statement. A single FORMAT statement may be referenced multiple times (i.e., with more than one WRITE statement). The number can be anywhere from 1 to 99999 and is at the discretion of the programmer.

Suggestions:

  • When possible, put FORMAT statements next to the WRITE statements that reference them (for clarity and readability).
  • Keep statement numbers in order (from lowest to highest, for example) to make finding the statement number a matter of looking downward through the file.

D. Haugli, Lecturer Aer E 160 Aerospace Engineering

  • Skip a few numbers between each statement number, to leave room for future additions (i.e., adding a new FORMAT statement in the middle of an existing code, while still keeping the statement numbers in order.

D. Haugli, Lecturer Aer E 160 Aerospace Engineering

The I Descriptor. This descriptor controls the format of INTEGER strings and has the form I#, where # is the number of digits to print out. Examples:

Code Segment: INTEGER :: i, j, k i = 1000 j = 12 k = 519 WRITE (*,7) i, j, k 7 FORMAT (3(1X,I3))

Output: *** 12 519

Notes:

  • The first variable (i) did not print correctly because I3 allows for too few digits; *** indicates the integer is too large to fit in the allocated number of digits.
  • The second integer (j) is preceded by one extra space since it has only two digits, where as I3 specifies three. Making the descriptor larger than the actual integer is akin to adding spaces to the left of the integer.
  • Coding 3(1X,I3) is the same as coding (1X,I3,1X,I3,1X,I3).

The F Descriptor. This descriptor controls the format of REAL strings and has the form F#.#, where left # is the number of digits to print out (including the decimal point) and the right # is the number of decimal places. Examples:

Code Segment: REAL :: a, b, c, d, e a = 97. b = 0. c = 51900. d = 713133. e = 15. WRITE (*,9) a, b, c, d, e 9 FORMAT (1X,F4.1,1X,F6.4,2(1X,F7.1),1X,F4.1)

Output: 97.5 0.0001 51900.0 ******* 15.

Notes:

  • The variable d did not print correctly because F7.1 allows for too few digits; ******* indicates the real number is too large to fit in the allocated number of digits.
  • The variable e was truncated since F4.1 has one fewer decimal places than would be required.
  • Coding 2(1X,F7.1) is the same as coding (1X,F7.1,1X,F7.1).

D. Haugli, Lecturer Aer E 160 Aerospace Engineering

The E Descriptor. This descriptor causes REAL strings to be printed in exponential form, and has the form E#.#, where left # is the number of digits to print out (including the decimal point and power of ten) and the right # is the number of decimal places. Examples:

Code Segment: REAL :: a, b, c, d, e a = 97. b = 0. c = 51900. d = 713133. e = 15. WRITE (*,11) a, b, c, d, e 11 FORMAT (1X,E9.3,1X,E7.1,2(1X,E11.5),1X,E4.1)

Output: 0.975E+02 0.1E-03 0.51900E+05 0.71313E+06 ****

Notes:

  • The variable e did not print correctly because E4.1 allows for too few digits; **** indicates the real number is too large to fit in the allocated number of digits and include the power of ten.
  • The variable d was truncated since E11.5 has one fewer decimal places than would be required (so E11.6 would work).
  • Coding 2(1X,E11.5) is the same as coding (1X,E11.5,1X,E11.5).

The L and / Descriptors. This descriptor controls the format of LOGICAL strings and has the form L#, where # is the number of spaces to output with the values "T" or "F". The / descriptor causes output to be written to a new line. Examples:

Code Segment: LOGICAL :: answer, switch answer = .true. switch = .false. WRITE (*,13) answer, switch 13 FORMAT (1X,"The answer is ",L1,"."/1X,"The switch is on ",L5,".")

Output: The answer is T. The switch is on F.

Notes:

  • The variable switch is output with several spaces before the value "F" since the field width is five.
  • The sentences containing answer and switch appear on different lines because of /.
  • In this example, no comma was necessary before or after /, but could have been included with no change in output:

13 FORMAT (1X,"The answer is ",L1,".",/,1X,"The switch is on ",L5,".")