module utilmod 
use precis
implicit none
integer i,j,k,l,m,n

interface printmat
module procedure printmat_sp, printmat_dp
end interface

interface printcol
module procedure printcol_sp, printcol_dp
end interface

interface printrow
module procedure printrow_sp, printrow_dp
end interface

contains

subroutine printmat_sp(a,iout,fmt) 
real, intent(in)::  a(:,:)
integer, optional, intent(in) :: iout
character(*), optional, intent(in) :: fmt

character form*20
integer iunit

iunit=6
if (present(iout)) iunit=iout

form='(es10.2)'
if (present(fmt)) form=fmt

m=size(a,1)
n=size(a,2)
do i=1,m
  do j=1,n
    if (j<n) then
      write(iunit,advance='no',fmt=form) a(i,j)
    else
      write(iunit,advance='yes',fmt=form) a(i,j)
    end if
  end do
end do
end subroutine printmat_sp 

subroutine printmat_dp(a,iout,fmt) 
double precision, intent(in)::  a(:,:)
integer, optional, intent(in) :: iout
character(*), optional, intent(in) :: fmt

character form*20
integer iunit

iunit=6
if (present(iout)) iunit=iout

form='(es10.2)'
if (present(fmt)) form=fmt

m=size(a,1)
n=size(a,2)
do i=1,m
  do j=1,n
    if (j<n) then
      write(iunit,advance='no',fmt=form) a(i,j)
    else
      write(iunit,advance='yes',fmt=form) a(i,j)
    end if
  end do
end do
end subroutine printmat_dp 

subroutine printcol_sp(a,iout,fmt) 
double precision, intent(in)::  a(:)
integer, optional, intent(in) :: iout
character(*), optional, intent(in) :: fmt

character form*20
integer iunit

iunit=6
if (present(iout)) iunit=iout

form='(es10.2)'
if (present(fmt)) form=fmt

m=size(a,1)
do i=1,m
  write(iunit,fmt=form) a(i)
end do
end subroutine printcol_sp 

subroutine printcol_dp(a,iout,fmt) 
real, intent(in)::  a(:)
integer, optional, intent(in) :: iout
character(*), optional, intent(in) :: fmt

character form*20
integer iunit

iunit=6
if (present(iout)) iunit=iout

form='(es10.2)'
if (present(fmt)) form=fmt

m=size(a,1)
do i=1,m
  write(iunit,fmt=form) a(i)
end do
end subroutine printcol_dp 

subroutine printrow_sp(a,iout,fmt) 
double precision, intent(in)::  a(:)
integer, optional, intent(in) :: iout
character(*), optional, intent(in) :: fmt

character form*20
integer iunit

iunit=6
if (present(iout)) iunit=iout

form='(es10.2)'
if (present(fmt)) form=fmt

m=size(a,1)
do i=1,m
  if (i<m) then
    write(iunit,advance='no',fmt=form) a(i)
  else
    write(iunit,advance='yes',fmt=form) a(i)
  end if
end do
end subroutine printrow_sp 

subroutine printrow_dp(a,iout,fmt) 
real, intent(in)::  a(:)
integer, optional, intent(in) :: iout
character(*), optional, intent(in) :: fmt

character form*20
integer iunit

iunit=6
if (present(iout)) iunit=iout

form='(es10.2)'
if (present(fmt)) form=fmt

m=size(a,1)
do i=1,m
  if (i<m) then
    write(iunit,advance='no',fmt=form) a(i)
  else
    write(iunit,advance='yes',fmt=form) a(i)
  end if
end do
end subroutine printrow_dp 

subroutine sort_down2(arr,irr)
use precis
real(mrl), dimension(:), intent(inout) :: arr
integer, dimension(:), intent(inout) :: irr
real(mrl) :: a
n=size(arr)
do k=1,n
  irr(k)=k
end do
do j=2,n
  a=arr(j)
  m=irr(j)
  do i=j-1,1,-1
    if (arr(i) >= a) exit
    arr(i+1)=arr(i)
    irr(i+1)=irr(i)
  end do
  arr(i+1)=a
  irr(i+1)=m
end do
end subroutine sort_down2

subroutine read_input_target_file(iunit,p,t,aline,idr)
real(mrl) :: p(:),t(:)
character(*) aline
integer iunit,idr
idr=0
read(iunit,'(a)',end=30) aline
if (scan(aline,'#')>0) goto 20
read(aline,*) p,t
idr=1
return

20 idr=2
return

30 idr=3
return
end subroutine read_input_target_file


subroutine makedeck(ia)
integer ia(:)
integer i,n
n=size(ia)
do i=1,n
  ia(i)=i
end do
end subroutine makedeck

subroutine shuffledeck(ia)
integer ia(:)
integer i,j,k,n
real rnum
n=size(ia)
do i=n,2,-1
  call random_number(rnum)
  j=int((n-1)*rnum)+1
  k=ia(i)
  ia(i)=ia(j)
  ia(j)=k
end do
end subroutine shuffledeck

end module utilmod 
