mean_mod.f90 8.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
module mean_mod
  public

! Interface to select single or double precision version of the 'mean'
! function from type of input arguments ("function overloading")
!********************************************************************
  interface mean
    module procedure mean_sp
    module procedure mean_dp
10
11
12
    module procedure mean_mixed_dss
    module procedure mean_mixed_dsd

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  end interface mean

contains

  subroutine mean_sp(x_sp,xm,xs,number)

!*****************************************************************************
!                                                                            *
!  This subroutine calculates mean and standard deviation of a given element.*
!                                                                            *
!      AUTHOR: Andreas Stohl, 25 January 1994                                *
!                                                                            *
!      Single precision version ESO 2016                                     *
!*****************************************************************************
!                                                                            *
! Variables:                                                                 *
! x_sp(number)        field of input data                                    *
! xm                  mean                                                   *
! xs                  standard deviation                                     *
! number              number of elements of field x_sp                       *
!                                                                            *
! Constants:                                                                 *
! eps                 tiny number                                            *
!                                                                            *
!*****************************************************************************

    use par_mod, only: sp

    implicit none

    ! integer :: number,i
    ! real(sp) :: x_sp(number),xm,xs,xl,xq,xaux
    ! real(sp),parameter :: eps=1.0e-30

47
    integer,intent(in) :: number
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    real(sp), intent(in) :: x_sp(number)
    real(sp), intent(out) ::xm,xs
    real(sp) :: xl,xq,xaux
    real(sp),parameter :: eps=1.0e-30
    integer :: i

    xl=0.
    xq=0.
    do i=1,number
      xl=xl+x_sp(i)
      xq=xq+x_sp(i)*x_sp(i)
    end do

    xm=xl/real(number,kind=sp)

    xaux=xq-xl*xl/real(number,kind=sp)

    if (xaux.lt.eps) then
      xs=0.
    else
      xs=sqrt(xaux/real(number-1,kind=sp))
    endif

  end subroutine mean_sp

  subroutine mean_dp(x_dp,xm,xs,number)

!*****************************************************************************
!                                                                            *
!  This subroutine calculates mean and standard deviation of a given element.*
!                                                                            *
!      AUTHOR: Andreas Stohl, 25 January 1994                                *
!                                                                            *
!      Double precision version ESO 2016                                     *
!*****************************************************************************
!                                                                            *
! Variables:                                                                 *
! x_dp(number)        field of input data                                    *
! xm                  mean                                                   *
! xs                  standard deviation                                     *
! number              number of elements of field x_dp                       *
!                                                                            *
! Constants:                                                                 *
! eps                 tiny number                                            *
!                                                                            *
!*****************************************************************************

    use par_mod, only: dp

    implicit none

99
    integer,intent(in) :: number
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    real(dp), intent(in) :: x_dp(number)
    real(dp), intent(out) ::xm,xs
    real(dp) :: xl,xq,xaux
    real(dp),parameter :: eps=1.0e-30
    integer :: i

    xl=0._dp
    xq=0._dp
    do i=1,number
      xl=xl+x_dp(i)
      xq=xq+x_dp(i)*x_dp(i)
    end do

    xm=xl/real(number,kind=dp)

    xaux=xq-xl*xl/real(number,kind=dp)

    if (xaux.lt.eps) then
      xs=0._dp
    else
      xs=sqrt(xaux/real(number-1,kind=dp))
    endif

  end subroutine mean_dp

125
  subroutine mean_mixed_dss(x_dp,xm,xs,number)
126
127
128
129
130
131
132

!*****************************************************************************
!                                                                            *
!  This subroutine calculates mean and standard deviation of a given element.*
!                                                                            *
!      AUTHOR: Andreas Stohl, 25 January 1994                                *
!                                                                            *
133
!      Mixed precision version ESO 2016 (dp in, sp out, sp out)              *
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
!*****************************************************************************
!                                                                            *
! Variables:                                                                 *
! x_dp(number)        field of input data                                    *
! xm                  mean                                                   *
! xs                  standard deviation                                     *
! number              number of elements of field x_dp                       *
!                                                                            *
! Constants:                                                                 *
! eps                 tiny number                                            *
!                                                                            *
!*****************************************************************************

    use par_mod, only: sp,dp

    implicit none

151
    integer,intent(in) :: number
152
153
154
155
156
157
158
159
160
    real(dp), intent(in) :: x_dp(number)
    real(sp), intent(out) ::xm,xs
    real(sp) :: xl,xq,xaux
    real(sp),parameter :: eps=1.0e-30
    integer :: i

    xl=0._sp
    xq=0._sp
    do i=1,number
161
162
      xl=xl+real(x_dp(i),kind=sp)
      xq=xq+real(x_dp(i),kind=sp)*real(x_dp(i),kind=sp)
163
164
165
166
167
168
169
170
171
172
173
174
    end do

    xm=xl/real(number,kind=sp)

    xaux=xq-xl*xl/real(number,kind=sp)

    if (xaux.lt.eps) then
      xs=0._sp
    else
      xs=sqrt(xaux/real(number-1,kind=sp))
    endif

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  end subroutine mean_mixed_dss

  subroutine mean_mixed_dsd(x_dp,xm,xs_dp,number)

!*****************************************************************************
!                                                                            *
!  This subroutine calculates mean and standard deviation of a given element.*
!                                                                            *
!      AUTHOR: Andreas Stohl, 25 January 1994                                *
!                                                                            *
!      Mixed precision version ESO 2016 (dp in, sp out, dp out)              *
!*****************************************************************************
!                                                                            *
! Variables:                                                                 *
! x_dp(number)        field of input data                                    *
! xm                  mean                                                   *
! xs_dp               standard deviation                                     *
! number              number of elements of field x_dp                       *
!                                                                            *
! Constants:                                                                 *
! eps                 tiny number                                            *
!                                                                            *
!*****************************************************************************

    use par_mod, only: sp,dp

    implicit none

    integer,intent(in) :: number
    real(dp), intent(in) :: x_dp(number)
    real(sp), intent(out) ::xm
    real(dp), intent(out) ::xs_dp
    real(dp) :: xl,xq,xaux
    real(dp),parameter :: eps=1.0e-30_dp
    integer :: i

    xl=0._dp
    xq=0._dp
    do i=1,number
      xl=xl+x_dp(i)
      xq=xq+x_dp(i)*x_dp(i)
    end do

218
    xm=real(xl,kind=sp)/real(number,kind=sp)
219
220
221
222
223
224
225
226
227
228
229

    xaux=xq-xl*xl/real(number,kind=dp)

    if (xaux.lt.eps) then
      xs_dp=0._dp
    else
      xs_dp=sqrt(xaux/real(number-1,kind=dp))
    endif

  end subroutine mean_mixed_dsd

230
end module mean_mod