      subroutine openf
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      parameter(maxsym=8)
      include 'maxorb.prm'
      parameter(maxda3=300,ngru3=2048)
      dimension b(ngru3)
      character*4 hab
      common/infdav/ twcas,alam(maxda3),tau,twvec,twess,sint,sint4,sh
     *,hab,Nbtots,nvec,ngao,niter,iprec,lvec(maxda3),nfrag,ipresc,irest
     *,jrest
     *,iunit,ihprec,ihvec,ifiess,NESSAI,nf8,maxda2,bidia,ywvec,ytana
     *,yhmono,ydens,ywcas
      character*100000 namel2
      character*25000 namel,namel1,namel3
      logical*1 bidia
      common/pr/nprint,mprint(10)
      common/opt/ihab,calcul
      character*3 calcul
      character*80 prefix,mat,cdi,info,detcl,det,ess,ijcl,davec,sc2ex
     *,fok,refmr,mono,mono1,ijcl2,ijcl3,ijcl4
      common/files/mono1
      character*7 stat
      character*80 mat1,cdi1,info1,detcl1,det1,blan40,ess1,ijcl1,davec1
     *,fok1,sc2ex1,refmr1,dens,dens1,ijcl21,ijcl31,ijcl41
      common/detfile/ det1
      common/info2/nmat
      common/sym/ itsym(maxorb*2),its(maxsym,maxsym),nsym
      common/ref2/refmr1,refmr,prefix
      common/stp/stop
      character*8 stop
      common/test/itest(100)
      common/n_fil/ ijcl1,nb_ijcl,l_ijcl,nrec_ijcl
      common/only/ ymatonly
      common/fil63/ ijcl21,ijcl31,ijcl41
      integer(8) ndet ; common/det/ndet,mdet(50),nbdet_cas
      namelist /cdifil/ prefix,mat,cdi,ess,ess1,
     *mat1,cdi1,info,info1,detcl1,detcl,det,det1,ijcl,ijcl1,
     *davec,davec1,fok,fok1,sc2ex,sc2ex1,dens,dens1,mono,mono1
      namelist /ij/nsym,norb,noc,ydp,enuc,ycl
c     *,itsym,its
c      write(6,*)'======================'
c      write(6,*)'   noms des files'
c      write(6,*)'======================'
      do k=1,80
         blan40(k:k)=' '
      enddo
      prefix=blan40
      refmr1=blan40
      mat1=blan40
      ijcl1=blan40
      ijcl21=blan40
      ijcl31=blan40
      ijcl41=blan40
      ijcl2='ijcl2'
      ijcl3='ijcl3'
      ijcl4='ijcl4'
      mono1=blan40
      cdi1=blan40
      info1=blan40
      ess1=blan40
      detcl1=blan40
      det1=blan40
      davec1=blan40
      sc2ex1=blan40
      dens1=blan40
      refmr='refmr     '
      mat='mat       '
      ess='ess       '
      info='Info      '
      mono='Mono      '
      cdi='cdi       '
      det='det       '
      detcl='detcl     '
      davec='davec     '
      sc2ex='sc2ex     '
      dens='dens      '
      fok='fok       '
      fok1=blan40
      ymatonly=.false.
!&&
!&& ymatonly: the program diagonalizes a matrix (calcul='D'). No file
!&& prefix... is required
!&&
      call LECNAM('&CDIFIL',namel,93,ierr,1)
      call namell(namel,ymatonly,'YMATONLY',1)
      if(itest(77)==1) print'(a,l2)','ymatonly',ymatonly
      if(ymatonly) ycl=.false.
      if(ymatonly) then
         call namela(namel,mat1,'MAT1')
         call nomfil(4,cdi,cdi1,prefix,'UNKNOWN','FORMATTED  ')
         calcul='D'
         stat='UNKNOWN'
         imat=1
c         call nomfil(imat,mat,mat1,prefix,'OLD    ','UNFORMATTED')
         goto 20
         return
      endif
      call namela(namel,prefix,'PREFIX')
      call namela(namel,info1,'INFO1')
      call namela(namel,INFO,'INFO')
      call nomfil(45,info,info1,prefix,'old    ','FORMATTED  ')
      call LECNAM('&COST_AO',namel2,45,ierr,0)
      if(ierr.eq.1) goto 1
      rewind 45
      call LECNAM('&COST_MO',namel1,45,ierr,itest(77))
      if(ierr.eq.1) goto 1
      call namell(namel1,ycl,'YCL',1)
      if(ycl) print'(a,l1)','ycl dans openf',ycl
      if(ycl) then
         ijcl='IJCL      '
      else
         ijcl='ijcl      '
      endif
      call namela(namel,ess1,'ESS1')
      call namela(namel,ess,'ESS')
      call namela(namel,mat1,'MAT1')
      call namela(namel,mat,'MAT')
      call namela(namel,cdi1,'CDI1')
      call namela(namel,cdi,'CDI')
      call namela(namel,detcl1,'DETCL1')
      call namela(namel,detcl,'DETCL')
      call namela(namel,det1,'DET1')
      call namela(namel,det,'DET')
      call namela(namel,MONO1,'MONO1')
      call namela(namel,MONO,'MONO')
      call namela(namel,ijcl21,'IJCL21')
      call namela(namel,ijcl31,'IJCL31')
      call namela(namel,ijcl41,'IJCL41')
      call namela(namel,ijcl2,'IJCL2')
      call namela(namel,ijcl3,'IJCL3')
      call namela(namel,ijcl4,'IJCL4')
      call namela(namel,ijcl1,'IJCL1')
      call namela(namel,ijcl,'IJCL')
      call namela(namel,davec1,'DAVEC1')
      call namela(namel,davec,'DAVEC')
      call namela(namel,fok1,'FOK1')
      call namela(namel,fok,'FOK')
      call namela(namel,sc2ex1,'SC2EX1')
      call namela(namel,sc2ex,'SC2EX')
      call namela(namel,dens1,'DENS1')
      call namela(namel,dens,'DENS')
      call nameli(namel1,nb_ijcl,'N_IJCL',4)
      if(itest(77)==1)print*,'nb_ijcl',nb_ijcl
      if(itest(77)==1)write(75,cdifil)
      if(itest(77)==1)call scrinam('NAMELIST CDIFIL')
 20   continue
      call lirdav
      write(6,*)' '
      write(6,*)'==========================='
      write(6,*)'   ouverture des files'
      write(6,*)'==========================='
      rewind 75
      if(calcul.eq.'D') then
         stat='UNKNOWN'
         imat=1
         call nomfil(imat,mat,mat1,prefix,'OLD    ','UNFORMATTED')
      else if(calcul.eq.'M') then
         stat='OLD    '
         if(irest.ne.0) stat='UNKNOWN'
         imat=83
         call nomfil(imat,mat,mat1,prefix,'UNKNOWN','UNFORMATTED')
      else if(calcul.eq.'MD') then
         stat='OLD    '
         if(irest.ne.0) stat='UNKNOWN'
         imat=83
         if(itest(21).eq.1)
     *   call nomfil(imat,mat,mat1,prefix,'OLD    ','UNFORMATTED')
      endif
      if(irest.eq.1.or.irest.eq.2) then
         open(74,form='unformatted',file='CD_RESTi',status='OLD')
      else
         open(74,form='unformatted',file='CD_RESTi',status='UNKNOWN')
      endif
      if(irest.eq.1.or.irest.eq.3) then
         open(64,form='unformatted',file='CD_RESTd',status='OLD')
      else
         open(64,form='unformatted',file='CD_RESTd',status='UNKNOWN')
      endif
      open(25,file='DIAGON',status='unknown',form='UNFORMATTED')
      call nomfil(2,fok,fok1,prefix,'UNKNOWN','UNFORMATTED')
      call nomfil(4,cdi,cdi1,prefix,'UNKNOWN','FORMATTED  ')
      if(ydens)call nomfil(63,dens,dens1,prefix,'UNKNOWN'
     *,'UNFORMATTED')
      call LECNAM('&CDI',namel3,4,ierr,0)
      call nameli(namel3,NDET,'NDET',4)
      rewind 4
      open(3,file='CD_SYSPIN',form='unformatted',status='unknown')
      rewind 45
 
 
 
 
c     read(45,ij,end=1)
c      namelist /ij/nsym,norb,noc,ydp,itsym,its,enuc,ycl
      call nameli(namel2,nsym,'NSYM',4)
      call nameli(namel1,norb,'NORB',4)
      call verdim('norb superieur a maxorb : norb, maxorb=',norb,maxorb)
      call nameli(namel1,noc,'NOC',4)
      call nameli(namel1,itsym(1),'ITSYM',4)
      call nameli(namel2,its(1,1),'ITS',4)
      call nameld(namel2,enuc,'ENUC',8)
c     write(6,ij)
 
      if(ycl.and.itest(63)==1) then
         print*,'ycl=T  -->  itest(63) est remis a 0'
         itest(63)=0
      endif
 
 
 
      rewind 45
      goto 3
 1    write(6,*)'il n''y a pas de file info'
      if(calcul.eq.'D') then
         write(6,*)'casdi continue en ne prenant en compte que le '
     *   ,'fichier de la matrice'
         open(61,file='BID61',status='unknown',form='unformatted')
      else
         stop
      endif
      goto 2
 3    continue
      if(itest(63).eq.0) then
         if(ycl)
     *   call nomfil(26,ijcl,ijcl1,prefix,stat,'FORMATTED  ')
         if(.not.ycl) then
            if(ijcl1.eq.blan40) then
               call colle(prefix,ijcl,ijcl1,ibid)
            endif
         endif
      else
         if(ijcl21.eq.blan40) call colle(prefix,ijcl2,ijcl21,ibid)
         print*,'ijcl21= ',trim(ijcl21)
         if(ijcl31.eq.blan40) call colle(prefix,ijcl3,ijcl31,ibid)
         print*,'ijcl31= ',trim(ijcl31)
         if(ijcl41.eq.blan40) call colle(prefix,ijcl4,ijcl41,ibid)
         print*,'ijcl41= ',trim(ijcl41)
      endif
c     *call nomfil(26,ijcl,ijcl1,prefix,stat,'UNFORMATTED')
      if(.not.ycl)
     *call nomfil(8,mono,mono1,prefix,stat,'UNFORMATTED')
      if(itest(25).eq.1) then
         call nomfil(61,det,det1,prefix,'UNKNOWN','UNFORMATTED')
      else
         call nomfil(61,detcl,detcl1,prefix,'UNKNOWN','UNFORMATTED')
      endif
 2    continue
      call nomfil(11,davec,davec1,prefix,'UNKNOWN','UNFORMATTED')
      if(ihab.eq.2)
     *call nomfil(12,sc2ex,sc2ex1,prefix,'OLD    ','UNFORMATTED')
      if(ihab.eq.1)
     *call nomfil(12,sc2ex,sc2ex1,prefix,'unknown','UNFORMATTED')
      call nomfil(15,ess,ess1,prefix,'UNKNOWN','UNFORMATTED')
      write(6,*)'==========================='
      write(6,*)''
      inquire(iolength=long) b
      open(21,status='UNKNOWN',file='DIRB',access='DIRECT',
     *recl=long,form='UNFORMATTED')
      open(22,status='UNKNOWN',file='DIRAB',access='DIRECT',
     *recl=long,form='UNFORMATTED')
      open(23,status='UNKNOWN',file='DIRP',access='DIRECT',
     *recl=long,form='UNFORMATTED')
      open(24,status='UNKNOWN',file='DIRR',access='DIRECT',
     *recl=long,form='UNFORMATTED')
      return
      end
       subroutine colle(a,b,c,ic)
      character*1 a*(*),b*(*),c*(*)
      la=len(a)
      lb=len(b)
      lc=len(c)
      do i=1,lc
         c(i:i)=' '
      enddo
      ic=0
      do ia=1,la
         if(a(ia:ia).ne.' ') then
            ic=ic+1
            if(ic.gt.lc) then
               print*,'impossible to concatenate a and b. '
               print*,'c is not long enough '
               print*,a
               print*,b
               stop
            endif
            c(ic:ic)=a(ia:ia)
         endif
      enddo
      do ib=1,lb
         if(b(ib:ib).ne.' ') then
            ic=ic+1
            if(ic.gt.lc) then
               print*,'impossible to concatenate a and b. '
               print*,'c is not long enough '
               print*,a
               print*,b
               stop
            endif
            c(ic:ic)=b(ib:ib)
         endif
      enddo
      return
      end
