      subroutine openf
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      parameter(maxsym=8,maxorb=500)
      parameter(maxal=56,ngru3=2048)
      character*4 hab
      common/infdav/ alam(maxal),tau,twvec,twess,sint,sh
     *,hab,Nbtots,nvec,ngao,niter,iprec,lvec(maxal),ipresc,irest
     *,iunit,ihprec,ihvec,ifiess,NESSAI,nf8,BIDIA,maxda2,ywvec,ytana
     *,yhmono,ydens,ywcas
c      character*10000 namel,namel1
      character*5280 namel,namel1
      LOGICAL BIDIA
      common/pr/nprint,mprint(10)
      common/opt/ihab,calcul
      character*3 calcul
      character*80 prefix,cdi,info,deta,detb,daveca,mono,prop,
     *refmr,davecb,davecb1
     *,file
     *,orb,orb1
      character*80 cdi1,info1,deta1,detb1,blan40,daveca1,mono1
     *,refmr1,filorb,prop1
      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(50)
      character*1 c
      dimension c(40)
      namelist /rofil/ prefix,cdi,
     *cdi1,info,info1,deta,deta1,detb,detb1,mono,mono1,prop1,prop
     *daveca,daveca1,davecb,davecb1
      namelist /ij/nsym,norb,noc,ydp,enuc,ycl
c     *,itsym,its
c      write(6,*)'======================'
c      write(6,*)'openf   noms des files'
c      write(6,*)'======================'
      blan40='                                        '
     *//'                                        '
      prefix=blan40
      refmr1=blan40
      cdi1=blan40
      info1=blan40
      mono1=blan40
      prop1=blan40
      deta='deta      '
      deta1=blan40
      detb='detb      '
      detb1=blan40
      daveca1=blan40
      daveca='daveca    '
      davecb1=blan40
      davecb='davecb    '
      refmr='refmr     '
      info='Info      '
      mono='Mono      '
      prop='Prop      '
      cdi='cdi       '
      orb=blan40
      orb1=blan40
      call LECNAM('&ROFIL',namel,93,ierr,1)
      call namela(namel,prefix,'PREFIX')
      call namela(namel,cdi1,'CDI1')
      call namela(namel,cdi,'CDI')
      call namela(namel,PROP1,'PROP1')
      call namela(namel,PROP,'PROP')
      call namela(namel,MONO1,'MONO1')
      call namela(namel,MONO,'MONO')
      call namela(namel,INFO1,'INFO1')
      call namela(namel,INFO,'INFO')
      call namela(namel,deta1,'DETA1')
      call namela(namel,deta,'DETA')
      call namela(namel,daveca1,'DAVECA1')
      call namela(namel,daveca,'DAVECA')
      call namela(namel,detb1,'DETB1')
      call namela(namel,detb,'DETB')
      call namela(namel,davecb1,'DAVECB1')
      call namela(namel,davecb,'DAVECB')
      call namela(namel,ORB1,'ORB1')
      call namela(namel,ORB,'ORB')
      write(6,*)' '
      write(6,*)'==========================='
      write(6,*)'openf   ouverture des files'
      write(6,*)'==========================='
      write(75,rofil)
      call scrinam('NAMELIST ROFIL')
c
c--   fichier orb
c
      filorb=blan40
      if(orb1(1:1).ne.' ') then
         open(1,file=orb1,status='old',err=9)
         print*,'Open file ',orb1
         filorb=orb1
         goto 11
      endif   
  9   continue    
      open(1,file='INPORB',status='old',err=10)
      print*,'0pen file INPORB'
      filorb='INPORB'
      goto 11
 10   continue
      call colle(prefix,'RasOrb',file,long)
      open(1,file=file,status='old',err=12)
      print*,'0pen file',file(1:len_trim(file))
      filorb=file
      goto 11
 12   continue
      call colle(prefix,'ScfOrb',file,long)
      open(1,file=file,status='old',err=13)
      print*,'0pen file ',file(1:len_trim(file))
      filorb=file
      goto 11
 13   continue
      print*, 'no orbital file found'
 11   continue


c
c--   appel lirdav
c
      call lirdav
c
c--   ouverture des files c
c
                  
      call nomfil(45,Info,Info1,prefix,'old    ','FORMATTED  ')
      call nomfil(46,Mono,Mono1,prefix,'old    ','UNFORMATTED')
      call nomfil(47,Prop,Prop1,prefix,'old    ','UNFORMATTED')
      call nomfil(4,cdi,cdi1,prefix,'UNKNOWN','FORMATTED  ')
      rewind 45

      ydp=.true.
      call LECNAM('&COST_AO',namel,45,ierr,0)
      rewind 45
      call LECNAM('&COST_MO',namel1,45,ierr,0)
      if(ierr.eq.1) then
         write(6,*) 'il n''y a pas de file Info'
         stop
      endif
      call nameli(namel,nsym,'NSYM',4)
      call nameli(namel1,norb,'NORB',4)
      call nameli(namel1,noc,'NOC',4)
      call nameli(namel1,itsym(1),'ITSYM',4)
      call nameli(namel,its(1,1),'ITS',4)
      call nameld(namel1,enuc,'ENUC',8)
      call namell(namel1,ycl,'YCL',1)
c     write(6,ij)
c      print*,'noc',noc
      write(75,ij)
      if(ypr)call scrinam('NAMELIST IJ    ')
c      print*,'1coucou'

      rewind 45
      call nomfil(60,deta,deta1,prefix,'old    ','UNFORMATTED')
      call nomfil(61,detb,detb1,prefix,'old    ','UNFORMATTED')
      call nomfil(10,daveca,daveca1,prefix,'old    ','UNFORMATTED')
      call nomfil(11,davecb,davecb1,prefix,'old    ','UNFORMATTED')
      write(6,*)'==========================='
      write(6,*)' '
      return
      end
       subroutine colle(a,b,c,ic)
      character*1 a*(*),b*(*),c*(*)
      la=len(a)
      lb=len(b)
      lc=len(c)
c      print*,'la,lb,lc',la,lb,lc
c      print*,a
c      print*,b
      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 de coller a et b. c pas assez long'
               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 de coller a et b. c pas assez long'
               print*,a
               print*,b
               stop
            endif
            c(ic:ic)=b(ib:ib)
         endif
      enddo   
      return
      end
       subroutine nomfil2(iunit,file1,file,status,form)
      character*1 file*(*),file1*(*),status*(*),form*(*)
      ls=len(status)
      lf=len(form)
      print*,'file1 ',file1
      print*,'file ',file
      if(file1(1:1).eq.' ') file1=file
      write(75,*)'ouverture de la file',iunit,' ',file1,status,'$'
      call scril
      ier=0
      OPEN (UNIT=iunit,FORM=form(1:lf),STATUS=status(1:ls),file=file1)
      return
      end  
