introductory fortran programming part 6 -...

94
Real Programs, Part 2 Introductory Fortran Programming Part 6 Gunnar Wollan 1 Dept. of Geosciences, University of Oslo 1 January 2007 Wollan Introductory Fortran Programming Part 6

Upload: dinhkhanh

Post on 30-Jul-2018

256 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Introductory Fortran Programming Part 6

Gunnar Wollan1

Dept. of Geosciences, University of Oslo1

January 2007

Wollan Introductory Fortran Programming Part 6

Page 2: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Outline

1 From small examples to real programs

Wollan Introductory Fortran Programming Part 6

Page 3: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

List of Topics

1 From small examples to real programs

Wollan Introductory Fortran Programming Part 6

Page 4: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving a real problem

The case of missing data

Very often we have an inconsistent time series where there aremissing data

To be able so use the time series we have to ”fill in theblanks” so to speak

There are several methods for doing this, but here we will lookat the simple linear interpolation

The equation for the linear interpolation is this:

y = y0 + (x − x0)y1 − y0

x1 − x0

(1)

Wollan Introductory Fortran Programming Part 6

Page 5: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving a real problem

The case of missing data

Very often we have an inconsistent time series where there aremissing data

To be able so use the time series we have to ”fill in theblanks” so to speak

There are several methods for doing this, but here we will lookat the simple linear interpolation

The equation for the linear interpolation is this:

y = y0 + (x − x0)y1 − y0

x1 − x0

(1)

Wollan Introductory Fortran Programming Part 6

Page 6: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving a real problem

The case of missing data

Very often we have an inconsistent time series where there aremissing data

To be able so use the time series we have to ”fill in theblanks” so to speak

There are several methods for doing this, but here we will lookat the simple linear interpolation

The equation for the linear interpolation is this:

y = y0 + (x − x0)y1 − y0

x1 − x0

(1)

Wollan Introductory Fortran Programming Part 6

Page 7: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving a real problem

The case of missing data

Very often we have an inconsistent time series where there aremissing data

To be able so use the time series we have to ”fill in theblanks” so to speak

There are several methods for doing this, but here we will lookat the simple linear interpolation

The equation for the linear interpolation is this:

y = y0 + (x − x0)y1 − y0

x1 − x0

(1)

Wollan Introductory Fortran Programming Part 6

Page 8: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving a real problem

The case of missing data

Very often we have an inconsistent time series where there aremissing data

To be able so use the time series we have to ”fill in theblanks” so to speak

There are several methods for doing this, but here we will lookat the simple linear interpolation

The equation for the linear interpolation is this:

y = y0 + (x − x0)y1 − y0

x1 − x0

(1)

Wollan Introductory Fortran Programming Part 6

Page 9: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

How do we start??

First of all we have to read the time series containing missingdata

Let us assume that the file contains an integer with the lengthof the time series as the first entry

A way to begin our program is to declare the necessaryvariables

USE linear_interpolationREAL(KIND=4), POINTER :: array(:)REAL, PARAMETER :: mval = -9999.99INTEGER, PARAMETER :: lun = 10INTEGER :: i, j, kCHARACTER(LEN=80) :: ifname, ofnameINTEGER :: rstatINTEGER :: argcINTEGER, EXTERNAL :: iargc

Wollan Introductory Fortran Programming Part 6

Page 10: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

How do we start??

First of all we have to read the time series containing missingdata

Let us assume that the file contains an integer with the lengthof the time series as the first entry

A way to begin our program is to declare the necessaryvariables

USE linear_interpolationREAL(KIND=4), POINTER :: array(:)REAL, PARAMETER :: mval = -9999.99INTEGER, PARAMETER :: lun = 10INTEGER :: i, j, kCHARACTER(LEN=80) :: ifname, ofnameINTEGER :: rstatINTEGER :: argcINTEGER, EXTERNAL :: iargc

Wollan Introductory Fortran Programming Part 6

Page 11: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

How do we start??

First of all we have to read the time series containing missingdata

Let us assume that the file contains an integer with the lengthof the time series as the first entry

A way to begin our program is to declare the necessaryvariables

USE linear_interpolationREAL(KIND=4), POINTER :: array(:)REAL, PARAMETER :: mval = -9999.99INTEGER, PARAMETER :: lun = 10INTEGER :: i, j, kCHARACTER(LEN=80) :: ifname, ofnameINTEGER :: rstatINTEGER :: argcINTEGER, EXTERNAL :: iargc

Wollan Introductory Fortran Programming Part 6

Page 12: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

How do we start??

First of all we have to read the time series containing missingdata

Let us assume that the file contains an integer with the lengthof the time series as the first entry

A way to begin our program is to declare the necessaryvariables

USE linear_interpolationREAL(KIND=4), POINTER :: array(:)REAL, PARAMETER :: mval = -9999.99INTEGER, PARAMETER :: lun = 10INTEGER :: i, j, kCHARACTER(LEN=80) :: ifname, ofnameINTEGER :: rstatINTEGER :: argcINTEGER, EXTERNAL :: iargc

Wollan Introductory Fortran Programming Part 6

Page 13: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The module linear interpolation contains all the necessarymethods for the interpolation

The array to contain the time series is declared as a singleprecision array using the KIND=4 to specify the precision

We use a constant to define the missing value (the number-9999.99 is often used for this)

We also use a constant for the file unit number and twocharacter strings to contain the input and output filenames

To be able to retrieve the number of input arguments wedeclare an external integer function iargc and an integer tohold the number of input arguments (argc)

Wollan Introductory Fortran Programming Part 6

Page 14: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The module linear interpolation contains all the necessarymethods for the interpolation

The array to contain the time series is declared as a singleprecision array using the KIND=4 to specify the precision

We use a constant to define the missing value (the number-9999.99 is often used for this)

We also use a constant for the file unit number and twocharacter strings to contain the input and output filenames

To be able to retrieve the number of input arguments wedeclare an external integer function iargc and an integer tohold the number of input arguments (argc)

Wollan Introductory Fortran Programming Part 6

Page 15: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The module linear interpolation contains all the necessarymethods for the interpolation

The array to contain the time series is declared as a singleprecision array using the KIND=4 to specify the precision

We use a constant to define the missing value (the number-9999.99 is often used for this)

We also use a constant for the file unit number and twocharacter strings to contain the input and output filenames

To be able to retrieve the number of input arguments wedeclare an external integer function iargc and an integer tohold the number of input arguments (argc)

Wollan Introductory Fortran Programming Part 6

Page 16: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The module linear interpolation contains all the necessarymethods for the interpolation

The array to contain the time series is declared as a singleprecision array using the KIND=4 to specify the precision

We use a constant to define the missing value (the number-9999.99 is often used for this)

We also use a constant for the file unit number and twocharacter strings to contain the input and output filenames

To be able to retrieve the number of input arguments wedeclare an external integer function iargc and an integer tohold the number of input arguments (argc)

Wollan Introductory Fortran Programming Part 6

Page 17: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The module linear interpolation contains all the necessarymethods for the interpolation

The array to contain the time series is declared as a singleprecision array using the KIND=4 to specify the precision

We use a constant to define the missing value (the number-9999.99 is often used for this)

We also use a constant for the file unit number and twocharacter strings to contain the input and output filenames

To be able to retrieve the number of input arguments wedeclare an external integer function iargc and an integer tohold the number of input arguments (argc)

Wollan Introductory Fortran Programming Part 6

Page 18: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The module linear interpolation contains all the necessarymethods for the interpolation

The array to contain the time series is declared as a singleprecision array using the KIND=4 to specify the precision

We use a constant to define the missing value (the number-9999.99 is often used for this)

We also use a constant for the file unit number and twocharacter strings to contain the input and output filenames

To be able to retrieve the number of input arguments wedeclare an external integer function iargc and an integer tohold the number of input arguments (argc)

Wollan Introductory Fortran Programming Part 6

Page 19: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Retrieving the number of input arguments and get the inputfilename

argc = iargc()IF(argc == 1) THEN

CALL getarg(1,ifname)ELSE

ifname = "indata.txt"END IF

Wollan Introductory Fortran Programming Part 6

Page 20: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We test to se if we have one command line argument storedin the argc variable

If we have, we retrieve the input filename calling the getargsubroutine with a one to specify we want to retrieve the firstcommand line argument and then the ifname as the secondargument

Note that all command line arguments are text strings even ifwe type in a number

Wollan Introductory Fortran Programming Part 6

Page 21: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We test to se if we have one command line argument storedin the argc variable

If we have, we retrieve the input filename calling the getargsubroutine with a one to specify we want to retrieve the firstcommand line argument and then the ifname as the secondargument

Note that all command line arguments are text strings even ifwe type in a number

Wollan Introductory Fortran Programming Part 6

Page 22: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We test to se if we have one command line argument storedin the argc variable

If we have, we retrieve the input filename calling the getargsubroutine with a one to specify we want to retrieve the firstcommand line argument and then the ifname as the secondargument

Note that all command line arguments are text strings even ifwe type in a number

Wollan Introductory Fortran Programming Part 6

Page 23: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We test to se if we have one command line argument storedin the argc variable

If we have, we retrieve the input filename calling the getargsubroutine with a one to specify we want to retrieve the firstcommand line argument and then the ifname as the secondargument

Note that all command line arguments are text strings even ifwe type in a number

Wollan Introductory Fortran Programming Part 6

Page 24: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

The next step is to open the input file, read the length of thetime series, allocate space for the data and read the valuesfrom the file

OPEN(UNIT=lun,FILE=ifname,FORM="FORMATTED",IOSTAT=rstat)READ(UNIT=lun,FMT=’(I6)’,IOSTAT=rstat) array_lengthALLOCATE(array(array_length),STAT=rstat)

DO i = 1, array_lengthREAD(UNIT=lun,FMT=’(F10.4)’,IOSTAT=rstat) array(i)

END DOCLOSE(UNIT=lun)

Wollan Introductory Fortran Programming Part 6

Page 25: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

The next step is to open the input file, read the length of thetime series, allocate space for the data and read the valuesfrom the file

OPEN(UNIT=lun,FILE=ifname,FORM="FORMATTED",IOSTAT=rstat)READ(UNIT=lun,FMT=’(I6)’,IOSTAT=rstat) array_lengthALLOCATE(array(array_length),STAT=rstat)

DO i = 1, array_lengthREAD(UNIT=lun,FMT=’(F10.4)’,IOSTAT=rstat) array(i)

END DOCLOSE(UNIT=lun)

Wollan Introductory Fortran Programming Part 6

Page 26: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Using the FORM=”FORMATTED” tells the system that theinput file is an ascii file

Reading the first entry we specify that it is an integer in theFMT=’(I6)’ argument

Then we allocate space for the array. Note that we shouldalways test if our calls to the operating-system were successful

The integer rstat is used here to recieve the return value. Ifthe return value is other than zero an error has occurred

A similiar format statement is used in reading the values fromthe file, but here we use a floating point number with a totalof 10 positions including the decimal separator and with 4decimals

Always remember to close the file when we are finished using it

Wollan Introductory Fortran Programming Part 6

Page 27: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Using the FORM=”FORMATTED” tells the system that theinput file is an ascii file

Reading the first entry we specify that it is an integer in theFMT=’(I6)’ argument

Then we allocate space for the array. Note that we shouldalways test if our calls to the operating-system were successful

The integer rstat is used here to recieve the return value. Ifthe return value is other than zero an error has occurred

A similiar format statement is used in reading the values fromthe file, but here we use a floating point number with a totalof 10 positions including the decimal separator and with 4decimals

Always remember to close the file when we are finished using it

Wollan Introductory Fortran Programming Part 6

Page 28: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Using the FORM=”FORMATTED” tells the system that theinput file is an ascii file

Reading the first entry we specify that it is an integer in theFMT=’(I6)’ argument

Then we allocate space for the array. Note that we shouldalways test if our calls to the operating-system were successful

The integer rstat is used here to recieve the return value. Ifthe return value is other than zero an error has occurred

A similiar format statement is used in reading the values fromthe file, but here we use a floating point number with a totalof 10 positions including the decimal separator and with 4decimals

Always remember to close the file when we are finished using it

Wollan Introductory Fortran Programming Part 6

Page 29: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Using the FORM=”FORMATTED” tells the system that theinput file is an ascii file

Reading the first entry we specify that it is an integer in theFMT=’(I6)’ argument

Then we allocate space for the array. Note that we shouldalways test if our calls to the operating-system were successful

The integer rstat is used here to recieve the return value. Ifthe return value is other than zero an error has occurred

A similiar format statement is used in reading the values fromthe file, but here we use a floating point number with a totalof 10 positions including the decimal separator and with 4decimals

Always remember to close the file when we are finished using it

Wollan Introductory Fortran Programming Part 6

Page 30: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Using the FORM=”FORMATTED” tells the system that theinput file is an ascii file

Reading the first entry we specify that it is an integer in theFMT=’(I6)’ argument

Then we allocate space for the array. Note that we shouldalways test if our calls to the operating-system were successful

The integer rstat is used here to recieve the return value. Ifthe return value is other than zero an error has occurred

A similiar format statement is used in reading the values fromthe file, but here we use a floating point number with a totalof 10 positions including the decimal separator and with 4decimals

Always remember to close the file when we are finished using it

Wollan Introductory Fortran Programming Part 6

Page 31: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Using the FORM=”FORMATTED” tells the system that theinput file is an ascii file

Reading the first entry we specify that it is an integer in theFMT=’(I6)’ argument

Then we allocate space for the array. Note that we shouldalways test if our calls to the operating-system were successful

The integer rstat is used here to recieve the return value. Ifthe return value is other than zero an error has occurred

A similiar format statement is used in reading the values fromthe file, but here we use a floating point number with a totalof 10 positions including the decimal separator and with 4decimals

Always remember to close the file when we are finished using it

Wollan Introductory Fortran Programming Part 6

Page 32: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Using the FORM=”FORMATTED” tells the system that theinput file is an ascii file

Reading the first entry we specify that it is an integer in theFMT=’(I6)’ argument

Then we allocate space for the array. Note that we shouldalways test if our calls to the operating-system were successful

The integer rstat is used here to recieve the return value. Ifthe return value is other than zero an error has occurred

A similiar format statement is used in reading the values fromthe file, but here we use a floating point number with a totalof 10 positions including the decimal separator and with 4decimals

Always remember to close the file when we are finished using it

Wollan Introductory Fortran Programming Part 6

Page 33: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we shall check the data for missing values

We loop through the array and test each element for missingvalue

i = 1DO

IF(array(i) == mval) THENCALL find_index(array,i,j,mval)CALL interpolate(array,i-1,j)i = j

ELSEi = i + 1

END IFIF(i == array_length) THENEXIT

END IFEND DO

Wollan Introductory Fortran Programming Part 6

Page 34: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we shall check the data for missing values

We loop through the array and test each element for missingvalue

i = 1DO

IF(array(i) == mval) THENCALL find_index(array,i,j,mval)CALL interpolate(array,i-1,j)i = j

ELSEi = i + 1

END IFIF(i == array_length) THENEXIT

END IFEND DO

Wollan Introductory Fortran Programming Part 6

Page 35: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We use a potensial endless loop by using the standalone DOso we use a manually updated index

If this array element contains a missing value we call thefind index subroutine to find the index of the next nonmissingvalue element

Then we call the interpolate subroutine with the array and theindex of the elements on both sides of the sequence ofelements with the missing value

We then set the index to the element after the last missingvalue in the sequence

Then we test to see if we have reached the end of the arrayand exit the loop if we have

Wollan Introductory Fortran Programming Part 6

Page 36: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We use a potensial endless loop by using the standalone DOso we use a manually updated index

If this array element contains a missing value we call thefind index subroutine to find the index of the next nonmissingvalue element

Then we call the interpolate subroutine with the array and theindex of the elements on both sides of the sequence ofelements with the missing value

We then set the index to the element after the last missingvalue in the sequence

Then we test to see if we have reached the end of the arrayand exit the loop if we have

Wollan Introductory Fortran Programming Part 6

Page 37: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We use a potensial endless loop by using the standalone DOso we use a manually updated index

If this array element contains a missing value we call thefind index subroutine to find the index of the next nonmissingvalue element

Then we call the interpolate subroutine with the array and theindex of the elements on both sides of the sequence ofelements with the missing value

We then set the index to the element after the last missingvalue in the sequence

Then we test to see if we have reached the end of the arrayand exit the loop if we have

Wollan Introductory Fortran Programming Part 6

Page 38: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We use a potensial endless loop by using the standalone DOso we use a manually updated index

If this array element contains a missing value we call thefind index subroutine to find the index of the next nonmissingvalue element

Then we call the interpolate subroutine with the array and theindex of the elements on both sides of the sequence ofelements with the missing value

We then set the index to the element after the last missingvalue in the sequence

Then we test to see if we have reached the end of the arrayand exit the loop if we have

Wollan Introductory Fortran Programming Part 6

Page 39: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We use a potensial endless loop by using the standalone DOso we use a manually updated index

If this array element contains a missing value we call thefind index subroutine to find the index of the next nonmissingvalue element

Then we call the interpolate subroutine with the array and theindex of the elements on both sides of the sequence ofelements with the missing value

We then set the index to the element after the last missingvalue in the sequence

Then we test to see if we have reached the end of the arrayand exit the loop if we have

Wollan Introductory Fortran Programming Part 6

Page 40: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We use a potensial endless loop by using the standalone DOso we use a manually updated index

If this array element contains a missing value we call thefind index subroutine to find the index of the next nonmissingvalue element

Then we call the interpolate subroutine with the array and theindex of the elements on both sides of the sequence ofelements with the missing value

We then set the index to the element after the last missingvalue in the sequence

Then we test to see if we have reached the end of the arrayand exit the loop if we have

Wollan Introductory Fortran Programming Part 6

Page 41: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

The last ting we have to do is to write the array to a newoutput file

This is how we can do it

ofname = TRIM(ifname)//".data"OPEN(UNIT=lun,FILE=ofname,FORM=’FORMATTED’,IOSTAT=rstat)WRITE(UNIT=lun,FMT=’(I6)’,IOSTAT=rstat) array_lengthDO i = 1, array_length

WRITE(UNIT=lun,FMT=’(F10.4)’,IOSTAT=rstat) array(i)END DOCLOSE(UNIT=lun)

Wollan Introductory Fortran Programming Part 6

Page 42: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

The last ting we have to do is to write the array to a newoutput file

This is how we can do it

ofname = TRIM(ifname)//".data"OPEN(UNIT=lun,FILE=ofname,FORM=’FORMATTED’,IOSTAT=rstat)WRITE(UNIT=lun,FMT=’(I6)’,IOSTAT=rstat) array_lengthDO i = 1, array_length

WRITE(UNIT=lun,FMT=’(F10.4)’,IOSTAT=rstat) array(i)END DOCLOSE(UNIT=lun)

Wollan Introductory Fortran Programming Part 6

Page 43: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We create the output filename from the input filename byconcatenating the input filename with an extra extension”.data”

Note the concatenation is performed using a double slash.The TRIM function returns the character string withouttrailing spaces

The code for writing is just the same as the one for reading,but we use of course WRITE instead of READ

Wollan Introductory Fortran Programming Part 6

Page 44: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We create the output filename from the input filename byconcatenating the input filename with an extra extension”.data”

Note the concatenation is performed using a double slash.The TRIM function returns the character string withouttrailing spaces

The code for writing is just the same as the one for reading,but we use of course WRITE instead of READ

Wollan Introductory Fortran Programming Part 6

Page 45: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We create the output filename from the input filename byconcatenating the input filename with an extra extension”.data”

Note the concatenation is performed using a double slash.The TRIM function returns the character string withouttrailing spaces

The code for writing is just the same as the one for reading,but we use of course WRITE instead of READ

Wollan Introductory Fortran Programming Part 6

Page 46: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

We create the output filename from the input filename byconcatenating the input filename with an extra extension”.data”

Note the concatenation is performed using a double slash.The TRIM function returns the character string withouttrailing spaces

The code for writing is just the same as the one for reading,but we use of course WRITE instead of READ

Wollan Introductory Fortran Programming Part 6

Page 47: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we have finished the main program, but we still have towrite the two subroutines find index and interpolate

So how do we go about doing this??

We have to take into consideration that we can have dataeither as single or double precision

To solve this we will make a module containing all theneccessary routines

Since we do not want to have a lot of different subroutinenames to call we will use an interface which allows us to haveone subroutine name for both the single and double precisiondata

Also it is convenient to have a global variable containing thearray length

Wollan Introductory Fortran Programming Part 6

Page 48: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we have finished the main program, but we still have towrite the two subroutines find index and interpolate

So how do we go about doing this??

We have to take into consideration that we can have dataeither as single or double precision

To solve this we will make a module containing all theneccessary routines

Since we do not want to have a lot of different subroutinenames to call we will use an interface which allows us to haveone subroutine name for both the single and double precisiondata

Also it is convenient to have a global variable containing thearray length

Wollan Introductory Fortran Programming Part 6

Page 49: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we have finished the main program, but we still have towrite the two subroutines find index and interpolate

So how do we go about doing this??

We have to take into consideration that we can have dataeither as single or double precision

To solve this we will make a module containing all theneccessary routines

Since we do not want to have a lot of different subroutinenames to call we will use an interface which allows us to haveone subroutine name for both the single and double precisiondata

Also it is convenient to have a global variable containing thearray length

Wollan Introductory Fortran Programming Part 6

Page 50: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we have finished the main program, but we still have towrite the two subroutines find index and interpolate

So how do we go about doing this??

We have to take into consideration that we can have dataeither as single or double precision

To solve this we will make a module containing all theneccessary routines

Since we do not want to have a lot of different subroutinenames to call we will use an interface which allows us to haveone subroutine name for both the single and double precisiondata

Also it is convenient to have a global variable containing thearray length

Wollan Introductory Fortran Programming Part 6

Page 51: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we have finished the main program, but we still have towrite the two subroutines find index and interpolate

So how do we go about doing this??

We have to take into consideration that we can have dataeither as single or double precision

To solve this we will make a module containing all theneccessary routines

Since we do not want to have a lot of different subroutinenames to call we will use an interface which allows us to haveone subroutine name for both the single and double precisiondata

Also it is convenient to have a global variable containing thearray length

Wollan Introductory Fortran Programming Part 6

Page 52: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now we have finished the main program, but we still have towrite the two subroutines find index and interpolate

So how do we go about doing this??

We have to take into consideration that we can have dataeither as single or double precision

To solve this we will make a module containing all theneccessary routines

Since we do not want to have a lot of different subroutinenames to call we will use an interface which allows us to haveone subroutine name for both the single and double precisiondata

Also it is convenient to have a global variable containing thearray length

Wollan Introductory Fortran Programming Part 6

Page 53: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Let us code some more

INTEGER :: array_lengthINTERFACE interpolate

MODULE PROCEDURE sinterpMODULE PROCEDURE dinterp

END INTERFACE interpolateINTERFACE find_index

MODULE PROCEDURE find_indexsMODULE PROCEDURE find_indexd

END INTERFACE find_index

Wollan Introductory Fortran Programming Part 6

Page 54: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

A global variable array length will contain the length of thetime series we read form the input file

The construct INTERFACE interpolate gives us theopportunity to use the interpolate for both the single anddouble precision data

Note that the subroutines sinterp and dinterp will be selectedand linked in at compile time depending on the inputarguments when we call the intepolate subroutine

The same goes for the find index

Wollan Introductory Fortran Programming Part 6

Page 55: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

A global variable array length will contain the length of thetime series we read form the input file

The construct INTERFACE interpolate gives us theopportunity to use the interpolate for both the single anddouble precision data

Note that the subroutines sinterp and dinterp will be selectedand linked in at compile time depending on the inputarguments when we call the intepolate subroutine

The same goes for the find index

Wollan Introductory Fortran Programming Part 6

Page 56: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

A global variable array length will contain the length of thetime series we read form the input file

The construct INTERFACE interpolate gives us theopportunity to use the interpolate for both the single anddouble precision data

Note that the subroutines sinterp and dinterp will be selectedand linked in at compile time depending on the inputarguments when we call the intepolate subroutine

The same goes for the find index

Wollan Introductory Fortran Programming Part 6

Page 57: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

A global variable array length will contain the length of thetime series we read form the input file

The construct INTERFACE interpolate gives us theopportunity to use the interpolate for both the single anddouble precision data

Note that the subroutines sinterp and dinterp will be selectedand linked in at compile time depending on the inputarguments when we call the intepolate subroutine

The same goes for the find index

Wollan Introductory Fortran Programming Part 6

Page 58: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

A global variable array length will contain the length of thetime series we read form the input file

The construct INTERFACE interpolate gives us theopportunity to use the interpolate for both the single anddouble precision data

Note that the subroutines sinterp and dinterp will be selectedand linked in at compile time depending on the inputarguments when we call the intepolate subroutine

The same goes for the find index

Wollan Introductory Fortran Programming Part 6

Page 59: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now let us code the find index subroutine

SUBROUTINE find_indexs(array,s,e,mv)IMPLICIT NONEREAL(KIND=4), POINTER :: array(:)INTEGER, INTENT(IN) :: sINTEGER, INTENT(OUT) :: eREAL(KIND=4), INTENT(IN) :: mvINTEGER :: iDO i = s, array_length

IF(array(i) /= mv) THENEXIT

END IFEND DOe = i

END SUBROUTINE find_indexs

Wollan Introductory Fortran Programming Part 6

Page 60: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This is a fairly straighe forward subroutine which takes 4arguments, the array, the starting index which both are inputonly

Then the return value containing the index of the nextnonmissing value element and the missing value itself

If we are at the end of the array the returning index will bethat of the last element

Note that the variable array length is the global variable

Wollan Introductory Fortran Programming Part 6

Page 61: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This is a fairly straighe forward subroutine which takes 4arguments, the array, the starting index which both are inputonly

Then the return value containing the index of the nextnonmissing value element and the missing value itself

If we are at the end of the array the returning index will bethat of the last element

Note that the variable array length is the global variable

Wollan Introductory Fortran Programming Part 6

Page 62: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This is a fairly straighe forward subroutine which takes 4arguments, the array, the starting index which both are inputonly

Then the return value containing the index of the nextnonmissing value element and the missing value itself

If we are at the end of the array the returning index will bethat of the last element

Note that the variable array length is the global variable

Wollan Introductory Fortran Programming Part 6

Page 63: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This is a fairly straighe forward subroutine which takes 4arguments, the array, the starting index which both are inputonly

Then the return value containing the index of the nextnonmissing value element and the missing value itself

If we are at the end of the array the returning index will bethat of the last element

Note that the variable array length is the global variable

Wollan Introductory Fortran Programming Part 6

Page 64: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This is a fairly straighe forward subroutine which takes 4arguments, the array, the starting index which both are inputonly

Then the return value containing the index of the nextnonmissing value element and the missing value itself

If we are at the end of the array the returning index will bethat of the last element

Note that the variable array length is the global variable

Wollan Introductory Fortran Programming Part 6

Page 65: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now for the really tricky part

Let us program the intepolation subroutine for the singleprecision data and begin with some declarations

RECURSIVE SUBROUTINE sinterp(array,s,e)IMPLICIT NONEREAL(KIND=4), POINTER :: array(:)INTEGER, INTENT(IN) :: s, eINTEGER :: i, j, kREAL(KIND=4) :: tmp_val

Wollan Introductory Fortran Programming Part 6

Page 66: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Now for the really tricky part

Let us program the intepolation subroutine for the singleprecision data and begin with some declarations

RECURSIVE SUBROUTINE sinterp(array,s,e)IMPLICIT NONEREAL(KIND=4), POINTER :: array(:)INTEGER, INTENT(IN) :: s, eINTEGER :: i, j, kREAL(KIND=4) :: tmp_val

Wollan Introductory Fortran Programming Part 6

Page 67: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The first thing to note is that we use a recursive subroutine

This means that the subroutine can call itself with newarguments

It is a nice way to be able to split a problem into smaller moremanageable parts

The rest of the declaratons are well known from earlier code

Wollan Introductory Fortran Programming Part 6

Page 68: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The first thing to note is that we use a recursive subroutine

This means that the subroutine can call itself with newarguments

It is a nice way to be able to split a problem into smaller moremanageable parts

The rest of the declaratons are well known from earlier code

Wollan Introductory Fortran Programming Part 6

Page 69: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The first thing to note is that we use a recursive subroutine

This means that the subroutine can call itself with newarguments

It is a nice way to be able to split a problem into smaller moremanageable parts

The rest of the declaratons are well known from earlier code

Wollan Introductory Fortran Programming Part 6

Page 70: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The first thing to note is that we use a recursive subroutine

This means that the subroutine can call itself with newarguments

It is a nice way to be able to split a problem into smaller moremanageable parts

The rest of the declaratons are well known from earlier code

Wollan Introductory Fortran Programming Part 6

Page 71: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

The first thing to note is that we use a recursive subroutine

This means that the subroutine can call itself with newarguments

It is a nice way to be able to split a problem into smaller moremanageable parts

The rest of the declaratons are well known from earlier code

Wollan Introductory Fortran Programming Part 6

Page 72: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

One element with missing value

k = e - s - 1SELECT CASE(k)

CASE(1)j = s + 1tmp_val = array(s) + FLOAT((j-s))* &

((array(e)-array(s))/FLOAT((e-s)))array(j) = tmp_val

Wollan Introductory Fortran Programming Part 6

Page 73: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This code needs to be explained a little thorough

The variable k recieves the number of elements containingmissing values

Then we use a case construct to check if we have 1, 2, ormore than 2 elements containing missing values

If we have only one we calculate the intepolated valueaccording to the formula and replaces the missing value withthe interpolated one

Wollan Introductory Fortran Programming Part 6

Page 74: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This code needs to be explained a little thorough

The variable k recieves the number of elements containingmissing values

Then we use a case construct to check if we have 1, 2, ormore than 2 elements containing missing values

If we have only one we calculate the intepolated valueaccording to the formula and replaces the missing value withthe interpolated one

Wollan Introductory Fortran Programming Part 6

Page 75: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This code needs to be explained a little thorough

The variable k recieves the number of elements containingmissing values

Then we use a case construct to check if we have 1, 2, ormore than 2 elements containing missing values

If we have only one we calculate the intepolated valueaccording to the formula and replaces the missing value withthe interpolated one

Wollan Introductory Fortran Programming Part 6

Page 76: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This code needs to be explained a little thorough

The variable k recieves the number of elements containingmissing values

Then we use a case construct to check if we have 1, 2, ormore than 2 elements containing missing values

If we have only one we calculate the intepolated valueaccording to the formula and replaces the missing value withthe interpolated one

Wollan Introductory Fortran Programming Part 6

Page 77: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

This code needs to be explained a little thorough

The variable k recieves the number of elements containingmissing values

Then we use a case construct to check if we have 1, 2, ormore than 2 elements containing missing values

If we have only one we calculate the intepolated valueaccording to the formula and replaces the missing value withthe interpolated one

Wollan Introductory Fortran Programming Part 6

Page 78: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Two consecutive elements with missing values

With two consecutive elements containing a missing value wewill need a little modified code

CASE(2)j = s + 1tmp_val = array(s) + FLOAT((j-s))* &

((array(e)-array(s))/FLOAT((e-s)))array(j) = tmp_valCALL sinterp(array,j,e)

Wollan Introductory Fortran Programming Part 6

Page 79: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Two consecutive elements with missing values

With two consecutive elements containing a missing value wewill need a little modified code

CASE(2)j = s + 1tmp_val = array(s) + FLOAT((j-s))* &

((array(e)-array(s))/FLOAT((e-s)))array(j) = tmp_valCALL sinterp(array,j,e)

Wollan Introductory Fortran Programming Part 6

Page 80: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Here we just calculate the first missing value and then use arecursive call to the subroutine to calculate the next value

The value calculated in the recursive call is not really linaer,but it is as close we can come with this algorithm

Wollan Introductory Fortran Programming Part 6

Page 81: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Here we just calculate the first missing value and then use arecursive call to the subroutine to calculate the next value

The value calculated in the recursive call is not really linaer,but it is as close we can come with this algorithm

Wollan Introductory Fortran Programming Part 6

Page 82: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Here we just calculate the first missing value and then use arecursive call to the subroutine to calculate the next value

The value calculated in the recursive call is not really linaer,but it is as close we can come with this algorithm

Wollan Introductory Fortran Programming Part 6

Page 83: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

If we have more than two consecutive elements with missingvalues we will have to extend our code

Here is what can be done

CASE DEFAULTj = s + (e - s)/2tmp_val = array(s) + FLOAT((j-s))* &

((array(e)-array(s))/FLOAT((e-s)))array(j) = tmp_valCALL sinterp(array,s,j)CALL sinterp(array,j,e)

END SELECTEND SUBROUTINE sinterp

Wollan Introductory Fortran Programming Part 6

Page 84: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

If we have more than two consecutive elements with missingvalues we will have to extend our code

Here is what can be done

CASE DEFAULTj = s + (e - s)/2tmp_val = array(s) + FLOAT((j-s))* &

((array(e)-array(s))/FLOAT((e-s)))array(j) = tmp_valCALL sinterp(array,s,j)CALL sinterp(array,j,e)

END SELECTEND SUBROUTINE sinterp

Wollan Introductory Fortran Programming Part 6

Page 85: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Note the use of the statment CASE DEFAULT which will bereached in the case we hve more than two consecutiveelements with missing values

First of all we here calculates the midpoint in the sequence

Then we calculate the intepolated value for the midpoint anduse this for further calculations

Then we use the recursive call to the subroutine first for theleft part of the sequence with the midpoint as the upperelement

Then we do the same for the right part again using themidpoint as the lower element

Wollan Introductory Fortran Programming Part 6

Page 86: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Note the use of the statment CASE DEFAULT which will bereached in the case we hve more than two consecutiveelements with missing values

First of all we here calculates the midpoint in the sequence

Then we calculate the intepolated value for the midpoint anduse this for further calculations

Then we use the recursive call to the subroutine first for theleft part of the sequence with the midpoint as the upperelement

Then we do the same for the right part again using themidpoint as the lower element

Wollan Introductory Fortran Programming Part 6

Page 87: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Note the use of the statment CASE DEFAULT which will bereached in the case we hve more than two consecutiveelements with missing values

First of all we here calculates the midpoint in the sequence

Then we calculate the intepolated value for the midpoint anduse this for further calculations

Then we use the recursive call to the subroutine first for theleft part of the sequence with the midpoint as the upperelement

Then we do the same for the right part again using themidpoint as the lower element

Wollan Introductory Fortran Programming Part 6

Page 88: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Note the use of the statment CASE DEFAULT which will bereached in the case we hve more than two consecutiveelements with missing values

First of all we here calculates the midpoint in the sequence

Then we calculate the intepolated value for the midpoint anduse this for further calculations

Then we use the recursive call to the subroutine first for theleft part of the sequence with the midpoint as the upperelement

Then we do the same for the right part again using themidpoint as the lower element

Wollan Introductory Fortran Programming Part 6

Page 89: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Note the use of the statment CASE DEFAULT which will bereached in the case we hve more than two consecutiveelements with missing values

First of all we here calculates the midpoint in the sequence

Then we calculate the intepolated value for the midpoint anduse this for further calculations

Then we use the recursive call to the subroutine first for theleft part of the sequence with the midpoint as the upperelement

Then we do the same for the right part again using themidpoint as the lower element

Wollan Introductory Fortran Programming Part 6

Page 90: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

Some explanation

Note the use of the statment CASE DEFAULT which will bereached in the case we hve more than two consecutiveelements with missing values

First of all we here calculates the midpoint in the sequence

Then we calculate the intepolated value for the midpoint anduse this for further calculations

Then we use the recursive call to the subroutine first for theleft part of the sequence with the midpoint as the upperelement

Then we do the same for the right part again using themidpoint as the lower element

Wollan Introductory Fortran Programming Part 6

Page 91: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

We now have a tool for performing a liner interpolation forfloating point numbers

Note that the subroutines for the double precision numbersare exactly the same with the exception that we use a doubleprecision array and missing value variable

The code for the main program is found here

The code for the module program is found here

Wollan Introductory Fortran Programming Part 6

Page 92: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

We now have a tool for performing a liner interpolation forfloating point numbers

Note that the subroutines for the double precision numbersare exactly the same with the exception that we use a doubleprecision array and missing value variable

The code for the main program is found here

The code for the module program is found here

Wollan Introductory Fortran Programming Part 6

Page 93: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

We now have a tool for performing a liner interpolation forfloating point numbers

Note that the subroutines for the double precision numbersare exactly the same with the exception that we use a doubleprecision array and missing value variable

The code for the main program is found here

The code for the module program is found here

Wollan Introductory Fortran Programming Part 6

Page 94: Introductory Fortran Programming Part 6 - folk.uio.nofolk.uio.no/gunnarw/CSE-FL/Fortran/PART6/cse-fl.pdf · Wollan Introductory Fortran Programming Part 6. Real Programs, Part 2 Solving

Real Programs, Part 2

Solving the linear interpolation

We now have a tool for performing a liner interpolation forfloating point numbers

Note that the subroutines for the double precision numbersare exactly the same with the exception that we use a doubleprecision array and missing value variable

The code for the main program is found here

The code for the module program is found here

Wollan Introductory Fortran Programming Part 6