8 DATA FILES

Example

Since the early 1600's, astronomers have observed and recorded of the number of solar flares or sunspots that occur on the surface of the sun. Beginning in 1826, Heinrich Schwabe, a German amateur astronomer, daily observed the sun and recorded the number of sunspots. He was the first to suspect a regular variation of 11 years between relative maximum numbers of sunspots (which vary periodically from a minimum of about 10 per year to a maximum of about 110 per year). Between the maximums that occurred in the years 1750 and 1948, there were 18 complete cycles.

Problem Statement

Using the mathematical model given below, calculate the number of sunspots for the years 1850 to 1975. Then calculate the differences in the predicted and the recorded number of sunspots.

Input/Output Description

A DO loop is used to input the years 1850 to 1975 in an equation which predicts the number of sunspots for that year. These values are written to a file called pspots.dat. The predicted number of sunspots and the actual number of sunspots are read from files pspots.dat and ospots.dat, respectively. The difference in predicted and actual numberof sunspots are found for each year from 1850 to 1975 and written to a file called diffs.dat.

Mathematical Equation

Assume that the mathematical model

where s is the number of sunspots in a year and t is the year, is a curve which closely fits the observed number of sunspots.

Algorithm

1. Use the equation s=60+50cos(2pi/11(t-1948)) to find the number of sunspots from 1850 to 1975.
2. Find the differences in the number of sunspots calculated by the equation and the actual number of recorded sunspots for the years 1850-1975.

CODE

c       The following program will 1) find the number of sunspots for the
c       years 1850-1975 2) write those results to a file called
c       pspots.dat with the year followed by the number of sunspots
c	during that year 3) calculate the difference between the recorded	
c	number of sunspots (stored in a data file called ospots.dat) 
c	and the number of sunspots predicted by the given equation and 4)
c	write those differences to a file called diffs.dat.

        PROGRAM sunspot

        REAL pi,sunspots, actual, predicted, diff
        INTEGER year, count

c	The OPEN statements connect the files pspots.dat, ospots.dat,
c	and diffs.dat to the program using the unit numbers that
c	correspond to the files.  
c		ospots.dat - input file consisting of 126 lines of data
c			     with a year and observed number of sunspots
c			     on each line
c		pspots.dat - output file for the year and predicted
c			     number of sunspots
c		diffs.dat -  output file for the year, predicted and
c			     actual number of sunspots, and the 
c			     differences in the two

        OPEN (UNIT=10, FILE='pspots.dat')
        OPEN (UNIT=15, FILE='ospots.dat', STATUS='new', ERR=100)
        OPEN (UNIT=20, FILE='diffs.dat')

        pi=3.141593

c       Use a do loop and the equation s=60+50cos(2pi/11(t-1948)) to find
c       the predicted number of sunspots for the years 1850 to 1975.

        DO 25 year=1850, 1975
                sunspots = 60+50*cos((2.0*pi/11.0)*(year-1948))
                WRITE (10,*) year,sunspots
 25     CONTINUE
        REWIND (UNIT=10)

c       Calculate the difference between the actual number of sunspots
c       observed in a year (stored in a data file called ospots.dat) and
c	the number of predicted sunspots (calculated by the equation
c	above) and write those differences to a file call diffs.dat.

        DO 30 count=1,126
                READ (10,*) year, actual
                Read (15,*) year, predicted
                diff = actual - predicted
                WRITE (20, *) year, actual, predicted, diff
 30     CONTINUE
100     PRINT *, 'ERROR'
        STOP
        END

Opening a Data File

A data file is a file that can be read from or written to. Data files are particularly useful when large amounts of data are involved. Each line of a data file is called a record. The following is the first few records of the file ospots.dat (a file containing the total number of sunspots observed in a year during the years 1850-1975):
1850 66.6
1851 64.5
1852 54.1
1853 39.0
1854 20.6

To read from or write to a data file during the execution of a program, the file must first be opened with the statement

OPEN (UNIT=file_number, FILE='name')

There were three data files opened in the example program

	OPEN (UNIT=10, FILE='pspots.dat')
        OPEN (UNIT=15, FILE='ospots.dat', STATUS='new', ERR=100)
        OPEN (UNIT=20, FILE='diffs.dat')

The OPEN statement