Count data in each bin
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=wp), | intent(in), | dimension(:) | :: | d | Data for binning |
|
integer, | intent(in), | optional | :: | N | Number of bins |
|
real(kind=wp), | intent(in), | optional | dimension(2) | :: | db | Boundaries of bin range |
integer, | intent(in), | optional | :: | normalize | Normalization type (1=sum, 2=bin size, 3=maxval) |
function binData(d,N,db,normalize) result(o)
!! Count data in each bin
real(wp),dimension(:),intent(in)::d
!! Data for binning
integer,intent(in),optional::N
!! Number of bins
real(wp),dimension(2),intent(in),optional::db
!! Boundaries of bin range
integer,intent(in),optional::normalize
!! Normalization type (1=sum, 2=bin size, 3=maxval)
real(wp),dimension(:,:),allocatable::o
real(wp),dimension(:),allocatable::b
integer::Nl,k
Nl = 10
if(present(N)) Nl = N
if(present(db)) then
b = linspace(db(1),db(2),Nl+1)
else
b = linspace(minval(d)-epsilon(1.0_wp),maxval(d)+epsilon(1.0_wp),Nl+1)
end if
allocate(o(Nl,2))
o(:,1) = (b(1:Nl)+b(2:Nl+1))/2.0_wp
do k=1,Nl
o(k,2) = real(count(d>=b(k) .and. d<=b(k+1)),wp)
end do
if(present(normalize)) then
select case(normalize)
case(1)
o(:,2) = o(:,2)/sum(o(:,2))
case(2)
do k=1,Nl
o(k,2) = o(k,2)/(b(k+1)-b(k))
end do
case(3)
o(:,2) = o(:,2)/maxval(o(:,2))
end select
end if
end function binData