program sample_avr
! 2008.5.22   by I. Tamagawa

 implicit none

 integer, parameter :: n=24
 integer            :: hour(n)
 real               :: pres(n), temp(n), rh(n), u(n)
 real               :: pres_m, temp_m, rh_m, u_m
 real               :: pres_std, temp_std, rh_std, u_std
 integer            :: i


open(unit=10, file='SampleData.txt')

 do i=1, n
   read(10,*) hour(i),pres(i), temp(i), rh(i), u(i)
  end do
 
close(10)
write(*,*) 'Read finished'

 call average( pres, n, pres_m )
 call average( temp, n, temp_m )
 call average( rh, n, rh_m )
 call average( u, n, u_m )

 call std( pres, n, pres_m, pres_std )
 call std( temp, n, temp_m, temp_std )
 call std( rh, n, rh_m, rh_std )
 call std( u, n, u_m, u_std )
 write(*,*) 'Averages', pres_m, temp_m, rh_m, u_m
 write(*,*) 'Standard Dev.', pres_std, temp_std, rh_std, u_std
end


subroutine std( x, n, xm, xstd )
 real :: x(n)
 integer :: n
 real :: xm, xstd
 double precision :: sum
 integer :: i
!    x, n,xm : Given
!    xsrd:   standard deviation

 sum = 0
 do i=1,n
   sum = sum + (x(i)-xm)*(x(i)-xm)
 end do
 xstd = sqrt( sum/n )
 return
end


subroutine average( x, n, xm )
 real :: x(n)
 integer :: n
 real :: xm
 double precision :: sum
 integer :: i
 sum =0
 do i=1, n
  sum = sum + x(i)
 end do
 xm = sum/n
 return
end
