subroutine analyze_data(this, mean, error, eie)
! Finds the mean, error and error in error for Flyvbjerg and Petersen
! blocking analysis
! General routine, does not require global data
real(dp), intent(in) :: this(:)
real(dp), intent(out) :: mean, error, eie
real(dp) :: mean2 ! squared value mean
integer :: length
integer :: i
real(dp) :: s, s2 ! sum x_i and sum x_i^2
s = 0.0_dp
s2 = 0.0_dp
mean = 0.0_dp
mean2 = 0.0_dp
error = 0.0_dp
eie = 0.0_dp
length = size(this, 1)
do i = 1, length
s = s + this(i)
s2 = s2 + this(i)**2
end do
mean = s / length
mean2 = s2 / length
error = sqrt(merge(mean2 - mean**2, 0._dp, mean2 - mean**2 > 0) / real(length - 1._dp, dp))
eie = error / sqrt(2.0_dp * (length - 1))
end subroutine analyze_data