43 real(
double),
intent(in) :: data(:)
44 logical,
intent(in),
optional :: mask(:)
46 integer :: indexx(size(data)), ni
48 logical :: locmask(size(data))
52 if(
present(mask))
then
53 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR median(): data and mask must have the same size', 0)
66 if(mod(ni,2).eq.0)
then
67 median = 0.5d0*(
data(indexx(ni/2)) +
data(indexx(ni/2+1)))
69 median =
data(indexx((ni+1)/2))
90 real,
intent(in) :: data(:)
91 logical,
intent(in),
optional :: mask(:)
94 real(
double) :: data_d(size(data)), median_d
95 logical :: locmask(size(data))
98 if(
present(mask))
then
99 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR median_sp(): data and mask must have the same size', 0)
104 median_d =
median(data_d, mask=locmask)
124 real(
double),
intent(in) :: data(:)
125 logical,
intent(in),
optional :: mask(:)
129 logical :: locmask(size(data))
132 if(
present(mask))
then
133 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR mean(): data and mask must have the same size', 0)
143 mean = sum(
data, mask=locmask)/dble(ni)
163 real,
intent(in) :: data(:)
164 logical,
intent(in),
optional :: mask(:)
167 real(
double) :: data_d(size(data)), mean_d
168 logical :: locmask(size(data))
171 if(
present(mask))
then
172 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR mean_sp(): data and mask must have the same size', 0)
177 mean_d =
mean(data_d, mask=locmask)
201 real(
double),
intent(in) :: data(:), wgts(:)
202 logical,
intent(in),
optional :: mask(:)
206 logical :: locmask(size(data))
209 if(
present(mask))
then
210 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR mean_weight(): data and mask must have the same size', 0)
219 if(wgts(i).lt.0.d0)
call quit_program_error(
'libSUFR mean_weight(): weights cannot be negative', 0)
220 totdat = totdat + wgts(i)*
data(i)
221 totwgt = totwgt + wgts(i)
222 if(wgts(i).gt.0.d0) ni = ni + 1
227 call error(
'libSUFR mean_weight(): data() has fewer than two weighted elements', 0)
250 function stdev(data, dMean, mask, var)
255 real(
double),
intent(in) :: data(:)
256 real(
double),
intent(in),
optional :: dmean
257 logical,
intent(in),
optional :: mask(:)
258 real(
double),
intent(out),
optional :: var
262 logical :: locmask(size(data))
265 if(
present(mask))
then
266 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR stDev(): data and mask must have the same size', 0)
270 if(
present(dmean))
then
273 lmean =
mean(
data, locmask)
280 lvar = lvar + (
data(i)-lmean)**2
286 call error(
'libSUFR stDev(): data() has fewer than two elements', 0)
289 lvar = lvar/dble(ni-1)
293 if(
present(var)) var = lvar
313 real,
intent(in) :: data(:),
mean
314 logical,
intent(in),
optional :: mask(:)
318 logical :: locmask(size(data))
321 if(
present(mask))
then
322 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR stDev_sp(): data and mask must have the same size', 0)
326 stdevd =
stdev(dble(data), dble(
mean), mask=locmask)
348 real(
double),
intent(in) :: data(:), wgts(:)
349 real(
double),
intent(in),
optional :: wmean
350 logical,
intent(in),
optional :: mask(:)
354 logical :: locmask(size(data))
357 if(
present(mask))
then
358 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR stDev_weight(): data and mask must have the same size', 0)
362 if(
present(wmean))
then
375 if(wgts(i).lt.0.d0)
call quit_program_error(
'libSUFR stDev_weight(): weights cannot be negative', 0)
377 totwgt = totwgt + wgts(i)
378 totwgt2 = totwgt2 + wgts(i)**2
379 if(wgts(i).gt.0.d0) nw = nw + 1
384 call error(
'libSUFR stDev_weight(): data() has fewer than 2 weighted elements', 0)
412 real(
double),
intent(in) :: data
413 integer,
intent(in) :: num
415 real(
double) :: oldmean, var1,oldvar
426 var1 = oldvar + (
data - oldmean) * (
data -
mean)
427 var = var1/dble(num-1)
429 if(
present(
stdev))
stdev = sqrt(var1/dble(num))
454 real(double),
intent(in) :: data(:), range
455 real(double),
intent(out) :: llim, ulim
456 logical,
intent(in),
optional :: mask(:)
458 real(double) :: diff, mindiff
459 integer :: indexx(size(data)), ni, i0,i1,i1max,i2,di
460 logical :: locmask(size(data))
464 call error(
'libSUFR prob_range(): data() has size 0', 0)
471 if(
present(mask))
then
472 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR prob_range(): data and mask must have the same size', 0)
479 di = nint(dble(ni)*range)
480 i1max = nint(dble(ni)*(1.d0-range))
482 mindiff = huge(mindiff)
486 diff =
data(indexx(i2))-
data(indexx(i1))
489 if(diff.lt.mindiff .or. (
deq(diff,mindiff).and.i1-ni/2.lt.i0-ni/2))
then
497 call warn(
'prob_range(): results are shaky...')
501 llim =
data(indexx(i0))
502 ulim =
data(indexx(i1))
527 real,
intent(in) :: data(:), range
528 real,
intent(out) :: llim, ulim
529 logical,
intent(in),
optional :: mask(:)
531 real(double) :: data_d(size(data)), range_d, llim_d,ulim_d
532 logical :: locmask(size(data))
535 if(
present(mask))
then
536 if(
size(data).ne.
size(mask))
call quit_program_error(
'libSUFR prob_range_sp(): data and mask must have the same size', 0)
542 range_d = dble(range)
544 call prob_range(data_d, range_d, llim_d, ulim_d, locmask)
566 integer,
intent(in) :: npoints
569 if(npoints.le.100)
then
596 subroutine bin_data_1d(xDat, Nbin, norm,mode,cumul, xMin,xMax, xBin,yBin)
600 real(double),
intent(in) :: xDat(:)
601 integer,
intent(in) :: Nbin, mode
602 logical,
intent(in) :: norm, cumul
603 real(double),
intent(inout) :: xMin,xMax
604 real(double),
intent(inout) :: xBin(:),yBin(:)
607 real(double) :: dx, dk
611 if(
size(xbin).le.nbin)
call quit_program_error(
'libSUFR bin_data_1d(): xBin must have size >= Nbin+1',1)
612 if(
size(ybin).le.nbin)
call quit_program_error(
'libSUFR bin_data_1d(): yBin must have size >= Nbin+1',1)
614 if(abs((xmin-xmax)/(xmax+tiny(xmax))).lt.sqrt(tiny(xmax)))
then
620 xmin = xmin - epsilon(xmin)*xmin
621 xmax = xmax + epsilon(xmax)*xmax
622 dx = abs(xmax - xmin)/dble(nbin)
624 dk = dble(min(max(mode,-1),1))/2.d0
626 xbin(k) = xmin + (dble(k)-0.5d0+dk)*dx
632 if(xdat(i).ge.xbin(k))
then
633 if(xdat(i).lt.xbin(k+1))
then
634 ybin(k) = ybin(k) + 1.d0
641 if(norm) ybin = ybin / (sum(ybin) + sqrt(tiny(ybin)))
646 ybin(k) = ybin(k-1) + ybin(k)
596 subroutine bin_data_1d(xDat, Nbin, norm,mode,cumul, xMin,xMax, xBin,yBin)
…
674 real,
intent(in) :: xDat(:)
675 integer,
intent(in) :: Nbin, mode
676 logical,
intent(in) :: norm, cumul
677 real,
intent(inout) :: xMin,xMax
678 real,
intent(inout) :: xBin(:),yBin(:)
680 real(double) :: dxDat(size(xDat)), dxMin,dxMax, dxBin(size(xBin)),dyBin(size(yBin))
687 call bin_data_1d(dxdat, nbin, norm,mode,cumul, dxmin,dxmax, dxbin,dybin)
723 real(
double),
intent(in) :: xdat, xmin,xmax
724 integer,
intent(in) :: nbin, mode
725 logical,
intent(in) :: cumul
726 real(
double),
intent(inout) :: xbin(:),ybin(:)
727 real(
double),
intent(in),
optional :: weight
728 logical,
intent(in),
optional :: init
736 if(
present(init)) initl = init
739 if(
present(weight)) weightl = weight
745 xbin(ibin) = xmin + dble(ibin-1)/dble(nbin) * (xmax-xmin)
746 else if(mode.eq.0)
then
747 xbin(ibin) = xmin + (dble(ibin-1)+0.5d0)/dble(nbin) * (xmax-xmin)
749 xbin(ibin) = xmin + dble(ibin)/dble(nbin) * (xmax-xmin)
763 ibin = ceiling( (xdat-xmin) / (xmax-xmin) * nbin )
764 else if(mode.eq.0)
then
765 ibin = nint( (xdat-xmin) / (xmax-xmin) * nbin )
767 ibin = floor( (xdat-xmin) / (xmax-xmin) * nbin )
771 if(ibin.ge.1 .and. ibin.le.nbin)
then
773 ybin(1:ibin) = ybin(1:ibin) + weightl
775 ybin(ibin) = ybin(ibin) + weightl
804 subroutine bin_data_2d(xDat,yDat, norm, nxBin,nyBin, xMin,xMax,yMin,yMax, z, tr, weights)
809 integer,
intent(in) :: nxBin,nyBin, norm
810 real(double),
intent(in) :: xDat(:),yDat(:)
811 real(double),
intent(in),
optional :: weights(size(xDat))
812 real(double),
intent(inout) :: xMin,xMax,yMin,yMax
813 real(double),
intent(out) :: z(nxBin+1,nyBin+1),tr(6)
815 integer :: i,bx,by, ndat
816 real(double) :: dx,dy, weightsl(size(xDat))
820 if(
size(ydat).ne.ndat)
call quit_program_error(
'libSUFR bin_data_2d(): data arrays xDat and yDat must have the same size',1)
823 if(
present(weights)) weightsl = weights
825 if(abs((xmin-xmax)/(xmax + sqrt(huge(xmax)))) .lt. sqrt(huge(xmax)))
then
826 xmin = minval(xdat(1:ndat))
827 xmax = maxval(xdat(1:ndat))
829 xmin = xmin - epsilon(xmin)*xmin
830 xmax = xmax + epsilon(xmax)*xmax
831 dx = abs(xmax - xmin)/dble(nxbin)
833 if(abs((ymin-ymax)/(ymax + sqrt(huge(ymax)))) .lt. sqrt(huge(ymax)))
then
834 ymin = minval(ydat(1:ndat))
835 ymax = maxval(ydat(1:ndat))
837 ymin = ymin - epsilon(ymin)*ymin
838 ymax = ymax + epsilon(ymax)*ymax
839 dy = abs(ymax - ymin)/dble(nybin)
844 tr(1) = xmin - dx/2.d0
847 tr(4) = ymin - dy/2.d0
853 bx = floor((xdat(i) - xmin)/dx) + 1
854 by = floor((ydat(i) - ymin)/dy) + 1
876 if(bx.ge.1.and.bx.le.nxbin.and.by.ge.1.and.by.le.nybin) z(bx,by) = z(bx,by) + weightsl(i)
880 if(norm.eq.1) z = z/maxval(z+sqrt(huge(z)))
804 subroutine bin_data_2d(xDat,yDat, norm, nxBin,nyBin, xMin,xMax,yMin,yMax, z, tr, weights)
…
906 subroutine bin_data_2d_sp(xDat,yDat, norm, nxBin,nyBin, xMin,xMax,yMin,yMax, z, tr, weights)
911 integer,
intent(in) :: nxBin,nyBin, norm
912 real,
intent(in) :: xDat(:),yDat(:)
913 real,
intent(in),
optional :: weights(size(xDat))
914 real,
intent(inout) :: xMin,xMax,yMin,yMax
915 real,
intent(out) :: z(nxBin+1,nyBin+1),tr(6)
917 real(double) :: dxDat(size(xDat)),dyDat(size(yDat)), dweights(size(xDat)), dxMin,dxMax,dyMin,dyMax, dz(nxBin+1,nyBin+1),dtr(6)
928 if(
present(weights)) dweights = dble(weights)
930 call bin_data_2d(dxdat,dydat, norm, nxbin,nybin, dxmin,dxmax,dymin,dymax, dz, dtr, dweights)
906 subroutine bin_data_2d_sp(xDat,yDat, norm, nxBin,nyBin, xMin,xMax,yMin,yMax, z, tr, weights)
…
967 subroutine histogram_2d_onthefly(xDat,yDat, nxBin,nyBin, xMin,xMax,yMin,yMax, z, init, weight, tr)
972 integer,
intent(in) :: nxBin,nyBin
973 real(double),
intent(in) :: xDat,yDat, xMin,xMax,yMin,yMax
974 real(double),
intent(out) :: z(nxBin+1,nyBin+1)
975 real(double),
intent(out),
optional :: tr(6)
976 logical,
intent(in),
optional :: init
977 real(double),
intent(in),
optional :: weight
980 real(double),
save :: dx,dy
981 real(double) :: weightl
985 if(
present(init)) initl = init
990 dx = abs(xmax - xmin)/dble(nxbin)
991 dy = abs(ymax - ymin)/dble(nybin)
995 tr(1) = xmin - dx/2.d0
998 tr(4) = ymin - dy/2.d0
1007 if(
size(z,1).lt.nxbin+1)
call quit_program_error(
'histogram_2d_onthefly: the first dimension of z is too small; the 2D '// &
1008 'array should have a size of at least nxBin+1 x nyBin+1', 1)
1009 if(
size(z,2).lt.nybin+1)
call quit_program_error(
'histogram_2d_onthefly: the second dimension of z is too small; the 2D '// &
1010 'array should have a size of at least nxBin+1 x nyBin+1', 1)
1015 if(
present(weight)) weightl = weight
1017 bx = floor((xdat - xmin)/dx) + 1
1018 by = floor((ydat - ymin)/dy) + 1
1020 if(bx.ge.1 .and. bx.le.nxbin .and. by.ge.1 .and. by.le.nybin) z(bx,by) = z(bx,by) + weightl
967 subroutine histogram_2d_onthefly(xDat,yDat, nxBin,nyBin, xMin,xMax,yMin,yMax, z, init, weight, tr)
…
1036 integer,
intent(in) :: n
1058 integer,
intent(in) :: n
1083 integer,
intent(in) :: n, k
1089 if(k.lt.0 .or. k.gt.n)
then
1091 else if(k.eq.0 .or. k.eq.n)
then
1120 integer,
intent(in) :: n, k
1121 real(
double),
intent(in) :: p
1143 integer,
intent(in) :: n, k
1144 real(
double),
intent(in) :: p
1170 integer,
intent(in) :: k
1171 real(
double),
intent(in) :: lambda
1194 integer,
intent(in) :: k
1195 real(
double),
intent(in) :: lambda
1223 real(
double),
intent(in) :: data1(:), data2(:)
1226 if(
size(data1).ne.
size(data2)) &
1227 call warn(
'correlation(): data arrays are of unequal size; using the size of the smalles array',0)
1228 nn = min(
size(data1),
size(data2))
1238 sum12 = sum12 + (data1(ii)-mean1) * (data2(ii)-mean2)
1239 sum21 = sum21 + (data1(ii)-mean1)**2
1240 sum22 = sum22 + (data2(ii)-mean2)**2
Provides kinds and related constants/routines.
integer, parameter double
Double-precision float. Precision = 15, range = 307.
integer, parameter long
Long integer.
Procedures for numerical operations.
elemental logical function deq(x1, x2, eps)
Test whether two double-precision variables are equal to better than a given value (default: 2x machi...
subroutine sorted_index_list(array, index_list, mask, index_n)
Return a list of indices index_list that sorts the members of array to ascending value,...
Procedures to do statistics.
subroutine bin_data_2d(xdat, ydat, norm, nxbin, nybin, xmin, xmax, ymin, ymax, z, tr, weights)
Bin data in 2 dimensions - computing the bin number rather than searching for it is ~10x faster.
pure real(double) function faculty(n)
Compute the faculty of an integer, returning a double-precision real.
real function median_sp(data, mask)
Compute the median of a data array - single-precision wrapper for median()
subroutine histogram_2d_onthefly(xdat, ydat, nxbin, nybin, xmin, xmax, ymin, ymax, z, init, weight, tr)
Bin data in 2 dimensions - computing the bin number rather than searching for it is ~10x faster.
pure real(double) function poisson_prob_cumul(k, lambda)
Compute the cumulative Poisson probability of k OR FEWER events occurring in a fixed interval for a k...
pure real(double) function binom_coeff(n, k)
Compute the binomial coefficient of n and k.
real(double) function correlation(data1, data2)
Compute the normalised correlation between two data series.
subroutine bin_data_1d(xdat, nbin, norm, mode, cumul, xmin, xmax, xbin, ybin)
Bin data in 1D bins by counting the number of data points in each bin.
pure real(double) function binom_cumul_prob(n, k, p)
Compute the cumulative binomial probability of n and k OR FEWER, and probability p.
real(double) function stdev_weight(data, wgts, wmean, mask)
Compute the weighted standard deviation of a data array with weighted mean 'wMean'.
subroutine bin_data_1d_sp(xdat, nbin, norm, mode, cumul, xmin, xmax, xbin, ybin)
Bin data in 1D bins by counting the number of data points in each bin - single-precision wrapper for ...
real(double) function mean(data, mask)
Compute the mean of a data array.
pure subroutine mean_var_running(mean, var, data, num, stdev)
Compute a running mean and variance by adding a data point and the data point number to existing valu...
pure integer function determine_nbin_1d(npoints)
Roughly estimate the number of 1D bins needed, from the number of data points.
real(double) function mean_weight(data, wgts, mask)
Compute the weighted mean of a data array.
pure integer(long) function faculty_i(n)
Compute the faculty of an integer, returning a long integer.
real function mean_sp(data, mask)
Compute the mean of a data array - single-precision wrapper for mean()
subroutine prob_range(data, range, llim, ulim, mask)
Find a given probability range for a data array - the narrowest range that contains a given fraction ...
subroutine prob_range_sp(data, range, llim, ulim, mask)
Find a given probability range for a data array - the narrowest range that contains a given fraction ...
real(double) function stdev(data, dmean, mask, var)
Compute the standard deviation of a data array with mean 'mean'.
pure real(double) function poisson_prob(k, lambda)
Compute the Poisson probability of EXACTLY k events occurring in a fixed interval for a known average...
real(double) function median(data, mask)
Compute the median of a data array.
subroutine bin_data_2d_sp(xdat, ydat, norm, nxbin, nybin, xmin, xmax, ymin, ymax, z, tr, weights)
Bin data in 2 dimensions - single-precision wrapper for bin_data_2d()
pure real(double) function binom_prob(n, k, p)
Compute the binomial probability of n and k, and probability p.
real function stdev_sp(data, mean, mask)
Compute the standard deviation of a data array with mean 'mean' - single-precision wrapper for stDev(...
pure subroutine histogram_1d_onthefly(xdat, nbin, mode, cumul, xmin, xmax, xbin, ybin, init, weight)
Create a 1D histogram on the fly (point by point). Bin data points by computing the bin they should b...
System-related procedures.
subroutine error(message, unit)
Print an error to StdOut or StErr.
subroutine quit_program_error(message, status)
Print an error message to StdErr and stop the execution of the current program.
subroutine warn(message, unit)
Print a warning to StdOut or StErr.