subroutine parse_contactindex(string, tiletuple)
character(len=*) :: string
integer, intent(out) :: tiletuple(2,4)
! The contact_index contains a pair of four points that defines the two edges that contact to
! each other from the two neighboring tiles. Assuming the four points are A, B, C, and D.
! A and B defines the edge of tile 1 and C and D defines the edge of tile2. A is the same point
! as C and B is the same as D. (Ai, Aj) is the index for point A.
! Ai:Bi,Aj:Bj::Ci:Di,Cj:Dj
integer :: pos, pos1, pos2
integer :: length, stat
integer :: Ai, Bi, Aj, Bj, Ci, Di, Cj, Dj
character(len=40) :: string1
integer :: i,j
call trim_null(string)
length = len_trim(string)
pos=index(string, "::")
pos1 = index(string(1:pos-1), ",")
pos2 = index(string(1:pos1-1), ":")
read(string(1:pos2-1),*,iostat=stat) Ai
read(string(pos2+1:pos1-1),*,iostat=stat) Bi
pos2 = index(string(pos1+1:pos-1), ":")
read(string(pos1+1:pos1+pos2-1),*,iostat=stat) Aj
read(string(pos1+pos2+1:pos-1),*,iostat=stat) Bj
string1 = string(pos+2:length)
length = length-pos-1
pos1 = index(string1, ",")
pos2 = index(string1(1:pos1-1), ":")
!print *, "Ci,Di:",string1(1:pos2-1), " ",string1(pos2+1:pos1-1)
read(string1(1:pos2-1),*,iostat=stat) Ci
read(string1(pos2+1:pos1-1),*,iostat=stat) Di
pos2 = index(string1(pos1+1:length), ":")
!print *, pos, pos1, pos2, string1(pos1+1:length)
!print *, "Cj,Dj:",string1(pos1+1:pos1+pos2-1), " ", string1(pos1+pos2+1:length)
read(string1(pos1+1:pos1+pos2-1),*,iostat=stat) Cj
read(string1(pos1+pos2+1:length),*,iostat=stat) Dj
!print *, "parse_contactindex:", Ai, Aj, Bi, Bj, Ci, Cj, Di, Dj
tiletuple(1,1)=Ai
tiletuple(2,1)=Aj
tiletuple(1,2)=Bi
tiletuple(2,2)=Bj
tiletuple(1,3)=Ci
tiletuple(2,3)=Cj
tiletuple(1,4)=Di
tiletuple(2,4)=Dj
! devide by 2 if the index /= 1
do j=1,4
do i=1,2
if (tiletuple(i,j)/=1) tiletuple(i,j)=tiletuple(i,j)/2
enddo
enddo
return
end subroutine parse_contactindex