!     Last change:  A     3 Jul 2005    3:20 pm
PROGRAM ProjectAssignment4 !New River Discharge Analasys
! by: Alex Beck With all 4 sections completed.
   IMPLICIT NONE
   INTEGER:: A,B,C,D,E,F,G,H,I,J,K,L, DISTANCE0,DISTANCE1,DISTANCE2,DISTANCE3 ! DISTANCE0-3 represents sensor node subsystems.

   !WRITE (*,*) "DISTANCE0: "
   !READ  (*,*) DISTANCE0
   !WRITE (*,*) "DISTANCE1: "
   !READ  (*,*) DISTANCE1
   !WRITE (*,*) "DISTANCE2: "
   !READ  (*,*) DISTANCE2
   !WRITE (*,*) "DISTANCE3: "
   !READ  (*,*) DISTANCE3
   DISTANCE0 = 1466785
   DISTANCE1 = 489634
   DISTANCE2 = 752222
   DISTANCE3 = 1010101





































   INTEGER, PARAMETER :: numDataPoints = 3016 !number of data points -1 coorisponding to coordinates of seq_in
   REAL(KIND=8), DIMENSION(0:numDataPoints) :: seq_in!, seq_out
   REAL(KIND=8) :: time, hour, minute, second
   INTEGER :: day, month, temp, choice

DO
   WRITE (*,*)
   WRITE (*,*) "Enter the time of the data point in hours after zero time: "
   READ  (*,*) time
   hour = time
   month = 10
   day = 4
   DO  !checks to figure out the day, month, hour, minute, and second of the chosen point time
      IF (hour >= 24) THEN
         hour = hour - 24
         day = day + 1
         IF (day > 31) THEN
            day = 1
            month = month + 1
         END IF
      ELSE
            temp = hour
            minute = (hour - temp)*60.0D0
            hour = temp

            temp = minute
            second = (minute - temp)*60.0D0
            minute = temp
            EXIT
      END IF
   END DO

   WRITE (*,*) "The exact date and time is ",month,"/",day,"/2004", " at ",hour,":",minute,":",second
   WRITE (*,*) "What do you want to perform on the data? 1. Newton Interpolation"
   WRITE (*,*) "2. Derivative Finder,    3. Integer Finder::::   "
   READ  (*,*) choice
   IF (choice == 1) THEN
      CALL NewtonInterpolation (seq_in, time)
      ELSE
      IF (choice == 2) THEN
         CALL DerivativeFinder (seq_in, time)
         ELSE
         IF (choice == 3) THEN
            CALL IntegralFinder (seq_in)
         END IF
      END IF
   END IF
END DO
STOP

CONTAINS
!-----------------------------------------------------------------------------
! NewtonInterpolation (seq_in, time)
!
! Subroutine that integrates over a certain data range
!
! Variables used are:
! time      : value of time of deretive
! cc        : location of nearest data point to time
! answer    : answer to simpson integration
! choice    : choose the interpolation variation
! DD01-DD51 : Divided differences. First number is degree in polynomial. Second is which member of array
!
! Input     : seq_in, data set, time
! Output    : interpolation answer
!-----------------------------------------------------------------------------
SUBROUTINE NewtonInterpolation (seq_in, time)
   REAL(KIND=8), INTENT(IN), DIMENSION(0:numDataPoints) :: seq_in
   REAL(KIND=8), INTENT(IN) :: time
   REAL(KIND=8) :: DD01, DD02, DD03, DD04, DD05, DD06, DD11, DD12, DD13, DD14, DD15, DD21, DD22, DD23, DD24, &
   & DD31, DD32, DD33, DD41, DD42, DD51, answer!divided diffreences degree 0-6, 1-7
   INTEGER :: choice, cc !choice for menue, and cc is the central coordinate from the time
   cc = time*4.0D0
   IF (cc < 3 .OR. cc > numDataPoints - 3) THEN
      WRITE (*,*) "Your time is too close to the edges or beyond them, restart program and enter in a value <3163 and >3."
   END IF
   !AINT
   WRITE (*,*) "Newton's Interpolation - Type in 1. for centered, 2 for forward, 3 backwards::::    "
   READ (*,*) choice

   DD01 = (seq_in(cc-2)-seq_in(cc-3)) / 1.0D0
   DD02 = (seq_in(cc-1)-seq_in(cc-2)) / 1.0D0
   DD03 = (seq_in(cc  )-seq_in(cc-1)) / 1.0D0
   DD04 = (seq_in(cc+1)-seq_in(cc  )) / 1.0D0
   DD05 = (seq_in(cc+2)-seq_in(cc+1)) / 1.0D0
   DD06 = (seq_in(cc+3)-seq_in(cc+2)) / 1.0D0

   DD11 = (DD02-DD01) / 2.0D0
   DD12 = (DD03-DD02) / 2.0D0
   DD13 = (DD04-DD03) / 2.0D0
   DD14 = (DD05-DD04) / 2.0D0
   DD15 = (DD06-DD05) / 2.0D0

   DD21 = (DD12-DD11) / 3.0D0
   DD22 = (DD13-DD12) / 3.0D0
   DD23 = (DD14-DD13) / 3.0D0
   DD24 = (DD15-DD14) / 3.0D0

   DD31 = (DD22-DD21) / 4.0D0
   DD32 = (DD23-DD22) / 4.0D0
   DD33 = (DD24-DD23) / 4.0D0

   DD41 = (DD32-DD31) / 5.0D0
   DD42 = (DD33-DD32) / 5.0D0

   DD51 = (DD42-DD41) / 6.0D0

   IF (choice == 1) THEN   !centered Newton Interpolation
answer=DD04 + DD13*(time-REAL(cc)/4.0D0) + DD23*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/4.0D0+.25D0) + DD32*(time-REAL(cc)/4.0D0)*&
& +(time-REAL(cc)/4.0D0+.25D0)*(time-REAL(cc)/4.0D0+.5D0)& DD41*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/&
&4.0D0+0.25D0)*(time-REAL(cc)/4.0D0+0.5D0)+DD51*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/4.0D0+0.75D0)*(time-REAL(cc)/4.0D0+1.0D0)*&
&(time-REAL(cc)/4.0D0+1.25D0)*(time-REAL(cc)/4.0D0+1.5D0)
   END IF

   IF (choice == 2) THEN  !forward Newton Interpolation
answer=DD01 + DD11*(time-REAL(cc)/4.0D0) + DD21*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/4.0D0+.25D0) + DD31*(time-REAL(cc)/4.0D0)*&
& +(time-REAL(cc)/4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)& DD41*(time-REAL(cc)/4.0D0)*(t&ime-REAL(cc)/4.0D0)*(time-REAL(cc)/&
&4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)+DD51*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)*&
&(time-REAL(cc)/4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)
   END IF

   IF (choice == 3) THEN  !backwards Newton Interpolation
answer=DD06 + DD15*(time-REAL(cc)/4.0D0) + DD24*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/4.0D0+.25D0) + DD33*(time-REAL(cc)/4.0D0)*&
& +(time-REAL(cc)/4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)& DD42*(time-REAL(cc)/4.0D0)*(t&ime-REAL(cc)/4.0D0)*(time-REAL(cc)/&
&4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)+DD51*(time-REAL(cc)/4.0D0)*(time-REAL(cc)/4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)*&
&(time-REAL(cc)/4.0D0+0.0D0)*(time-REAL(cc)/4.0D0+0.0D0)
   END IF

   WRITE (*,*) "The flow at the given time is :  ", answer
   RETURN
END SUBROUTINE NewtonInterpolation
!-----------------------------------------------------------------------------
! DerivativeFinder (seq_in, time)
!
! Subroutine that integrates over a certain data range
!
! Variables used are:
! time      : value of time of deretive
! cc        : location of nearest data point to time
! answer    : answer to simpson integration
!
! Input     : seq_in, data set, time
! Output    : dirivitive answer
!-----------------------------------------------------------------------------
SUBROUTINE DerivativeFinder (seq_in, time)
   REAL(KIND=8), INTENT(IN), DIMENSION(0:numDataPoints) :: seq_in
   REAL(KIND=8), INTENT(IN) :: time
   REAL(KIND=8) :: answer
   INTEGER :: cc
   cc = time*4.0D0


   IF (cc<0 .OR. cc>numDataPoints) THEN
      WRITE (*,*) "That time is outside of the data range"
   ELSE
      IF (cc < 2) THEN  !below is 5 point forward derivitising
       answer = (-25.0D0*seq_in(cc) + 48.0D0*seq_in(cc+1) - 36.0D0*seq_in(cc+2) +  16.0D0*seq_in(cc+3) - 3.0D0*seq_in(cc+4)) / 3.0D0
         ELSE    !below is 5 point bacjwards dereivitising
         IF (cc > numDataPoints - 2) THEN
        answer = (25.0D0*seq_in(cc) - 48.0D0*seq_in(cc-1) + 36.0D0*seq_in(cc-2) -  16.0D0*seq_in(cc-3) + 3.0D0*seq_in(cc-4)) / 3.0D0
         ELSE    !below is 5 point centered derivitising
            answer = ( (seq_in(cc-2) - 8.0D0*seq_in(cc-1) + 8.0D0*seq_in(cc+1) - seq_in(cc+2)) ) / 3.0D0
         END IF
      END IF
   END IF
   WRITE (*,*) "The deritive of water flow at the given time is aproximatly: ", answer," cubic feet per second per second."
   RETURN
END SUBROUTINE DerivativeFinder
!-----------------------------------------------------------------------------
! IntegralFinder (seq_in)
!
! Subroutine that integrates over a certain data range
!
! Variables used are:
! a, b      : start and stop value
! n         : number of intervals
! answer    : answer to simpson integration
!
! Input     : seq_in, data set
! Output    : integrated answer
!-----------------------------------------------------------------------------
SUBROUTINE IntegralFinder (seq_in)
   REAL(KIND=8), INTENT(IN), DIMENSION(0:numDataPoints) :: seq_in
   REAL(KIND=8) :: answer, input1, input2, h
   INTEGER :: a, b, i, x, n

  DO
     WRITE (*,*) "Input a beggining and end point in hours for an interval for integration:  "
     READ  (*,*) input1, input2
     a = input1*4.0D0
     b = input2*4.0D0
     n = b-a                              !FFFFFFFFFFFFFFFIIIIIIIIIIIIIIIIIIIXXXXXXXXXXXXXXXXXXXXXX h!=n???????????
     h = (b-a)/0.25D0
     IF (a<0 .OR. a>numDataPoints*4 .OR. b<0 .OR. b>numDataPoints*4) THEN
        WRITE (*,*) "That time is outside of the data range"
     ELSE
        IF (MOD(REAL(n),2.0) < 0.01 .AND. MOD(REAL(n),2.0) > -0.01) THEN  ! even n is reqired for formula to work
           WRITE (*,*) "Enter a starting or ending value that is exactly 0.25 hours different."
        ELSE
           EXIT
        END IF
     END IF
  END DO
  answer = 0
  DO i = 1, n/2  !simpsons rule integrater
     x = a-h+2*h*i
     answer = answer + 4.0D0 * seq_in(x)
     IF (i/=n/2) THEN
        answer = answer + 2.0D0 * seq_in(x+n)
     END IF
   END DO
   answer = answer + seq_in(a) + seq_in(b)
   answer = answer*REAL(n)/3.0D0 * 60.0D0 * 60.0D0 * 8.0D0  !accounts for how many seconds in an hour
   WRITE (*,*) "The volume of water that flowed through the given time is aproximatly: ", answer," cubic feet."
   RETURN
END SUBROUTINE IntegralFinder




END PROGRAM ProjectAssignment4

