I'm a beginner to Fortran and trying to solve a problem to group some datasets which has the same distance to a point. I'm trying to create a 3D array range_data
with different length (n,5,120) (n varies). I want to group 5 1D-arrays URS
PROB
ix
iy
and radius
(has the same length, same index location) into variable range_data
.
I'm stuck with this error and don't know exactly what leads to this. I ran debugger and it said that Segmentation fault at if (int(radius(m,1))==k) sz=sz+1
The script is below:
function radius_group(radius,dims,ix,iy,URS,PROB) result (range_data)
use typDefMod
real, dimension(:), allocatable, intent(in) :: URS,PROB
real, dimension(:,:), allocatable, intent(in) :: radius
integer,dimension(:), allocatable, intent(in) :: ix,iy
integer :: i,k,m,sz
integer, intent(in) :: dims
type(multi_array), allocatable:: range_data,temp
integer, dimension(:), allocatable :: loc
do k=1,int(maxval(radius(:,1)))
sz=0
do m=1,dims
if (int(radius(m,1))==k) sz=sz+1
end do
allocate(loc(sz))
i=1
do m=1,dims
if (int(radius(m,1))==k) then
loc(i)=m
i=i+1
end if
end do
allocate(temp%range_num(k)%element(sz,5))
temp%range_num(k)%element(:,1)=radius(loc(:),1)
temp%range_num(k)%element(:,2)=ix(loc(:))
temp%range_num(k)%element(:,3)=iy(loc(:))
temp%range_num(k)%element(:,4)=URS(loc(:))
temp%range_num(k)%element(:,5)=PROB(loc(:))
deallocate(loc)
end do
range_data=temp
end function radius_group