c
c     ################################################################
c     ##                                                            ##
c     ##        subroutine readfile --  read an input file          ##
c     ##                                                            ##
c     ################################################################
c
c
c     "initial" sets up original values for some parameters
c     and variables that might not otherwise get initialized
c
c
      subroutine readfile(ixyz,mcmatm,mcmcar,xtemp,
     *                     insv,itagt,itypt,icont,atnamt,
     *                     mcmkey,inkey,inklin)
      implicit none
CAMBP#ifdef MPI
CAMBP      include 'extra.h'
CAMBP#endif
      include 'sizes.i'
      include 'align.i'
      include 'argue.i'
      include 'atoms.i'
      include 'atmtyp.i'
      include 'bath.i'
      include 'bound.i'
      include 'cell.i'
      include 'couple.i'
      include 'files.i'
      include 'inform.i'
      include 'iounit.i'
      include 'keys.i'
      include 'linmin.i'
      include 'minima.i'
      include 'molcul.i'
      include 'mutant.i'
      include 'output.i'
      include 'pdb.i'
      include 'precis.i'
      include 'scales.i'
      include 'sequen.i'
      include 'warp.i'
      include 'zclose.i'
      integer ixyz,ikey,itp,mcmatm,mcmcar,in,j,mcmkey,inkey
      real*8 precise
      real*8 xtemp(mcmcar)
      integer itagt(mcmatm),itypt(mcmatm),
     *        icont(mcmatm,mcmatm),insv(mcmatm)
      character*3 atnamt(mcmatm)
      character*60 xyzfile
      character*60 keyfile
      character*80 inklin(mcmkey)
      logical exist
c
      call initial
      if (ixyz .eq. 81 .or. ixyz .eq. 82) then
      itp=ixyz-80
      else if (ixyz .eq. 72) then
      itp=12
      else if (ixyz .eq. 73) then
      itp=23
      else if (ixyz .eq. 74) then
      itp=13
      else if (ixyz .eq. 75) then
      itp=123
      else if (ixyz .eq. 76) then
      itp=132
      else if (ixyz .eq. 62) then
      itp=212
      else if (ixyz .eq. 63) then
      itp=223
      else if (ixyz .eq. 64) then
      itp=213
      else if (ixyz .eq. 65) then
      itp=2123
      else if (ixyz .eq. 66) then
      itp=2132
      endif
      ikey=90+itp
      do in=1,mcmatm
        do j=1,mcmatm
          icont(in,j)=0
        enddo
      enddo
c
c     first open and then read the Cartesian coordinates file
c
      if (itp .eq. 1 .or. itp .eq. 2) then
       xyzfile = 'esp.fu8 '
       xyzfile(8:8)=char(48+itp)
       keyfile = 'esp.fu9 '
       keyfile(8:8)=char(48+itp)
       else if (itp .eq. 12) then
       xyzfile = 'esp.fu72'
       else if (itp .eq. 23) then
       xyzfile = 'esp.fu73'
       else if (itp .eq. 13) then
       xyzfile = 'esp.fu74'
       else if (itp .eq. 123) then
       xyzfile = 'esp.fu75'
       else if (itp .eq. 132) then
       xyzfile = 'esp.fu76'
       else if (itp .eq. 212) then
       xyzfile = 'esp.fu62'
       else if (itp .eq. 223) then
       xyzfile = 'esp.fu63'
       else if (itp .eq. 213) then
       xyzfile = 'esp.fu64'
       else if (itp .eq. 2123) then
       xyzfile = 'esp.fu65'
       else if (itp .eq. 2132) then
       xyzfile = 'esp.fu66'
      endif
c      open (unit=ixyz,file=xyzfile,status='old')                       0305MH08
CAMBP#ifdef MPI
CAMBP      if(master) rewind (unit=ixyz)
CAMBP#else
      rewind (unit=ixyz)
CAMBP#endif
      call readxyz (ixyz)
c      close (unit=ixyz)                                                0305MH08
      do in=1,mcmatm
         itagt(in)=tag(in)
         atnamt(in)=name(in)
         itypt(in)=type(in)
         do j=1,mcmatm
c           icont(in,j)=i12(in,j)
            icont(j,in)=i12(j,in)
         enddo
         insv(in)=n12(in)
      enddo
      do in=1,mcmatm
         xtemp((in-1)*3+1)=x(in)
         xtemp((in-1)*3+2)=y(in)
         xtemp((in-1)*3+3)=z(in)
      enddo
      exist=.false.
      inquire (file=keyfile, exist=exist)
      if (exist) then
        open (unit=ikey,file=keyfile,status='old')
        call getkey(ikey)
        inkey=nkey
        if (inkey.gt.0) then
           do in=1,inkey
              inklin(in)=keyline(in)
           enddo
        endif
      else
        inkey = 0
      endif
      return
      end
c
