      subroutine wrtoud(iun,emin,iconn,ityp,coo,q)
      implicit double precision (a-h,p-z),integer (i-n),logical (o)
      parameter (mxamb=701)
      parameter (mxgff=71)
      parameter (mxcon=10)
      common /athlp/ iatoms, mxnat
      integer*2 ityp
      character*3 chtmp,ambstr
      character*2 gffstr
      common /ffstr/ ambstr(mxamb), gffstr(mxgff)
      dimension icnn(mxcon)
      dimension coo(3,*),q(*),iconn(mxcon+1,*),ityp(*)

c write tinker .xyz file

      write(iun,*) '[AMBFOR]'
      write(iun,'(i5,a,f9.3,a)') iatoms,' emin ',emin,
     &   ' molden generated AMBFOR .xyz (amber/gaff param.)'

      do i=1,iatoms

            ibnds = 0
            do j=1,iconn(1,i)
               if (iconn(j+1,i).gt.0) then
                  ibnds = ibnds + 1
                  icnn(ibnds) = iconn(j+1,i)
               endif
            end do

            iopt = 1
            itypi = ityp(i)

            if (itypi.gt.0) then

c Amber
                if (itypi.le.mxamb) then
                  chtmp = ambstr(itypi)
                  if (itypi.gt.mxamb) itypi = itypi + (2000-mxamb)
                endif

                write(iun,'(i6,2x,a3,1x,3(f9.3),1x,i3,1x,8i5)')
     &            iopt,chtmp,(coo(j,i),j=1,3),
     &            itypi,(icnn(j),j=1,ibnds)

            elseif (ityp(i).lt.0) then

c GAFF
                if (iabs(itypi).le.mxgff) then
                  chtmp = gffstr(iabs(itypi))//' '
                endif

                write(iun,'(i6,2x,a3,1x,3(f9.3),1x,i3,1x,f6.3,1x,8i5)')
     &            iopt,chtmp,(coo(j,i),j=1,3),
     &            itypi,q(i),(icnn(j),j=1,ibnds)
            endif


      end do

      return
      end

