Function to calculate the statistical moments of a 1D array with mask, see calc_moments
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=R8), | intent(in), | dimension(:) | :: | tab |
1D array |
|
logical(kind=I4), | intent(in), | optional, | dimension(:) | :: | mask |
1D mask |
type(moment_stat), | intent(out) | :: | mx |
moment_stat result |
||
integer(kind=I4), | intent(in) | :: | nb_mom |
number of desired moments |
subroutine calc_moments_1D(tab, mask, mx, nb_mom) !================================================================================================ !! Function to calculate the statistical moments of a 1D array with mask, see [[calc_moments]] !------------------------------------------------------------------------------------------------ implicit none integer (kind=I4), intent(in ) :: nb_mom !! *number of desired moments* real (kind=R8), intent(in ), dimension(:) :: tab !! *1D array* logical (kind=I4), intent(in ), dimension(:), optional :: mask !! *1D mask* type(moment_stat), intent(out) :: mx !! [[moment_stat]] *result* integer(kind=I4) :: lg, nz integer(kind=I4) :: i, ii real(kind=R8) :: tmp real(kind=R8), allocatable, dimension(:) :: tab_tmp lg = size( tab ) nz = lg if ( present(mask) ) then nz = count( mask ) allocate( tab_tmp(1:nz) ) ii = 0 do i = 1, lg if ( mask(i) ) then ii = ii + 1 tab_tmp(ii) = tab(i) endif enddo if (ii /= nz) stop 'error calc_moments' else tab_tmp = tab endif mx%mu = 0 mx%si = 0 mx%va = 0 mx%Sk = 0 mx%Ku = 0 mx%Ss = 0 mx%Kk = 0 do i = 1, nz mx%mu = mx%mu +tab_tmp(i)/nz enddo if (nb_mom == 1) return do i = 1, nz mx%va = mx%va +( (tab_tmp(i) -mx%mu)**2 )/nz enddo mx%si = sqrt( mx%va ) if (nb_mom==2) return ! don't go further if (mx%si < 1.e-15_R8) then ! if the standard deviation is too small, quit stop 'calc_moments, std too small' endif do i = 1, nz tmp = ( tab_tmp(i) -mx%mu )/mx%si mx%Sk = mx%Sk +(tmp**3)/nz mx%Ku = mx%Ku +(tmp**4)/nz enddo if (nb_mom == 4) return do i = 1, nz tmp = ( tab_tmp(i) -mx%mu )/mx%si mx%Ss = mx%Ss +(tmp**5)/nz mx%Kk = mx%Kk +(tmp**6)/nz enddo deallocate( tab_tmp ) return endsubroutine calc_moments_1D