!////////////////////////////////////////////////////////////
!//
!// stat_test.f90
!//
!// Program for testing the mean and stddev
!// functions
!////////////////////////////////////////////////////////////

PROGRAM stat_test
  IMPLICIT NONE
  !// Declare the array wit 10 elements
  REAL, DIMENSION(10)                          :: array
  !// Declare the result from the call to the mean
  !// function
  REAL                                         :: mval
  !// Declare the result from the call to the stddev
  !// function
  REAL                                         :: stdval
  !// To be able to call the mean function with a variable
  !// length array we have to tell the compiler to
  !// provide additional information to the function about 
  !// the shape and length of the array by using an interface
  !// declaration
  INTERFACE meanval
    !FUNCTION mean(array) RESULT(X_M)
    !  REAL,DIMENSION(:)        :: array
    !  REAL                     :: X_M
    !END FUNCTION mean
    SUBROUTINE my_mean(A,m)
      IMPLICIT NONE
      REAL,DIMENSION(:), INTENT(IN)            :: A
      REAL, INTENT(OUT)                        :: m
    END SUBROUTINE my_mean
  END INTERFACE meanval

  !// To be able to call the stddev function with a variable
  !// length array we have to tell the compiler to
  !// provide additional information to the function about 
  !// the shape and length of the array by using an interface 
  !!/ declaration
  INTERFACE stddev_val
    !FUNCTION stddev(array) RESULT(sigma)
    !  REAL,DIMENSION(:)        :: array
    !  REAL                     :: sigma
    !END FUNCTION stddev
    SUBROUTINE my_stdsub(array,sigma)
      IMPLICIT NONE
      REAL,DIMENSION(:), INTENT(IN)            :: array
      REAL, INTENT(OUT)                        :: sigma
    END SUBROUTINE my_stdsub
  END INTERFACE stddev_val
  
  array = (/1.,2.,3.,4.,5.,6.,7.,8.,9.,10./)
  !mval = mean(array)
  !PRINT *, mval
  !stdval = stddev(array)
  !PRINT *, stdval
  CALL my_mean(array,mval)
  PRINT *, mval
  CALL my_stdsub(array,stdval)
  PRINT *, stdval
END PROGRAM stat_test