There are many ways to do IO in Fortran. We begin from the simplest possible.
The easiest way to read an array from standard input in Fortran is simply to say:
xis an array. If the array is of type
realand is 10 entries long, Fortran will attempt to read 10 floating point numbers. The
readcommand reads a single line of standard input at a time. This means that all array entries have to be typed on a single line.
Figure 2.5 shows a content of an input file,
let's call it
chi.dat, to be read by our program,
in which we must, at this stage, replace
integer, parameter :: n = 100with
integer, parameter :: n = 10You will learn soon how to allocate an array of desired length in Fortran.
In the program itself we will place the following three lines in front of the computational part:
read(*,*) x read(*,*) y read(*,*) sigmaand place the following single line, which writes the result of our computation on standard output, just before the word
write(*,*) a, sigma_a, b, sigma_b, chi_2
The program, after recompilation, can now be run as follows:
gustav@blanc:../src 12:57:44 !522 $ ./chi < chi.dat 1.8082818294190344, 8.7731094559548306E-2, \ 0.51538936959208925, 1.43532455281008345E-2, \ 15.310414091470935 gustav@blanc:../src 12:57:52 !523 $The output will be actually written on a single line, because, as
readreads a line at a time,
writewrites a line at a time.
The results returned by the program are as follows:
Before we go any further, let us restructure our program so
that we can view the results of our
fit on a graph.
We do that by printing on standard output our results in multiple
columns. The first column contains xi, the second column contains
yi, and the third column contains .
In the fourth
column we will print the values of
real(kind=long), dimension(n) :: y_prime integer :: i ... y_prime = a + b * x do i=1,n write(*, '(4f8.3)') x(i), y(i), sigma(i), y_prime(i) end doObserve the new Fortran element, the
DOloop, which is Fortran's main iterative construct. We have used this construct to produce pretty output, but at no stage did we have to use
DOduring computations. The string
writestatement, means ``print 4 floating point numbers, reserving 8 spaces for each number and with 3 digits after decimal point''. The printed numbers are right justified within the space allocated to them.
Running this program as before returns a table of numbers that is shown in Figure 2.6.
chi.out. We can now use
gnuplotto view the results.
Looking at the plot you can appreciate that points with smaller error bars have markedly greater weights than points with larger error bars. Consequently the fit tries to come closer to the former.
The syntax of the gnuplot command plot is quite complicated, but you don't have to use it in its most complex form. Suffice to remember that the keyword using tells plot, which columns of the input file to use for the abscissa, the ordinate, and for error bars.
Let us sum up our accomplishments, because even at this early stage they are not inconsiderable. We have a 28 lines long Fortran program, shown in Figure 2.9, and a 6 lines long Gnuplot script shown in Figure 2.7, and with these two very simple tools we have done in just a few seconds what used to take me good few hours when I was a student myself and had to do all these computations by hand, because all that we had at that time were logarithmic rulers, whereas computers were very scarce, expensive and reserved for top science and military work only.