# Fortran bug: rank problem gfortran

1. Mar 22, 2013

### billiards

This is my own code, and it won't compile with gfortran. All I want to do is extract the location of the cell with the minimum value in an array. A seemingly simple task but one that does not work with the intrinsic function minloc, for reasons I do not understand.

The error message:

Code (Text):
iclosept=minloc(dist)
1
Error: Incompatible ranks 0 and 1 in assignment at (1)
The code:

Code (Text):

subroutine distance_to_trench(pt,tr,mindist,iclosept)
! find (great circle) distance from point to each point on the trench
! distance = the minimum distance to the trench

type(point) :: pt
type(trench) :: tr
real, parameter :: pi=3.14159265359
real :: x1,y1,z1,x2,y2,z2
real,dimension(:),allocatable :: dist
real :: mindist
integer :: ii, length, iclosept, a

deg=180/pi

length=size(tr%poly%points%x)
allocate(dist(length))

! method:
! convert lat lons to cartesian vectors
! use angle between vectors to find the distance

do ii=1, length

print *,dist(ii)

enddo

mindist=minval(dist)

a=size(dist)
print *,'size of dist', size(dist), a

iclosept=minloc(dist)
print *,'minloc', minloc(dist), iclosept

deallocate(dist)

end subroutine distance_to_trench

2. Mar 22, 2013

### billiards

Fixed it.

Turns out I was confused about the output of the minloc function. It returns an array of integer(s). I was assuming it returned an integer. Because I was only thinking about a 1-d array. But in the case of a 1-d array, it actually returns an array of size 1, where the element contains the integer I was after.

To fix my above code I introduced a temporary array and assigned it by minloc. Then I extracted the single element from it into a pure integer.

Thus:

Code (Text):

subroutine distance_to_trench(pt,tr,mindist,iclosept)
! find (great circle) distance from point to each point on the trench
! distance = the minimum distance to the trench

type(point) :: pt
type(trench) :: tr
real, parameter :: pi=3.14159265359
real :: x1,y1,z1,x2,y2,z2
real,dimension(:),allocatable :: dist
real :: mindist
integer :: ii, length, iclosept, temp(1)

deg=180/pi

length=size(tr%poly%points%x)
allocate(dist(length))

! method:
! convert lat lons to cartesian vectors
! use angle between vectors to find the distance

do ii=1, length

print *,dist(ii)

enddo

mindist=minval(dist)
temp=minloc(dist)

iclosept=temp(1)

deallocate(dist)

end subroutine distance_to_trench

3. Mar 22, 2013

### Staff: Mentor

4. Mar 22, 2013