program sample_avr
! 2008.5.22   by K. Miwa, based on
!         2008.5.15   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,pres_s, temp_s, rh_s, u_s

 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 stde( pres, n, pres_m,pres_s )
 call stde( temp, n, temp_m ,temp_s)
 call stde( rh, n, rh_m ,rh_s)
 call stde( u, n, u_m ,u_s)
 write(*,*) 'Averages', pres_m, temp_m, rh_m, u_m
 write(*,*) 'Standard Deviation', pres_s, temp_s, rh_s, u_s

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


subroutine stde( x,n,xm,xs)
 real :: x(n)
 integer :: n
 real :: xm,xs
 double precision :: var
 integer :: i
 var =0
 do i=1, n
  var = var + (x(i)-xm)*(x(i)-xm)
 end do
 xs = sqrt(var/n)
 return
end
