      program limat
!      logical(1),allocatable :: ysgn(:)
      dimension imat(2048),jmat(2048),d(2048)
      call titre('PROGRAM LIMAT#',40)
      open(3,file='NDET',status='old')
      !open(1,file='/work1/daniel/C2H2tot/C2H2.mat',status='old'
      open(1,file='MATCASDI0',status='old'
     *,form='unformatted')
      open(4,file='MATCASDI',status='unknown')
      read(3,*) ndet,ndetp
      print*,'ndet',ndet,ndetp
      kk=0
      do
         read(1) imat,jmat,d
         ifin=2048
         if(imat(ifin)==-1) ifin=jmat(2048)
         do k=1,ifin
            kk=kk+1
            if(imat(k)<=ndetp.and.jmat(k)<=ndetp)
     *      write(4,'(2i6,f16.12)') max(imat(k),jmat(k)),
     *      min(imat(k),jmat(k)),d(k)
         enddo
         if(imat(2048)==-1) exit
      enddo
      close(1)
      close(4)
      !open(1,file='/work1/daniel/C2H2tot/MAT',status='old'
      open(1,file='MATCASDILOC0',status='old'
     *,form='unformatted')
      open(4,file='MATCASDILOC',status='unknown')
      kk=0
      do
         read(1) imat,jmat,d
         ifin=2048
         if(imat(ifin)==-1) ifin=jmat(2048)
         do k=1,ifin
            kk=kk+1
            write(4,'(2i6,f16.12)') max(imat(k),jmat(k)),
     *      min(imat(k),jmat(k)),d(k)
         enddo
         if(imat(2048)==-1) exit
      enddo
      end
