*     -------------
*     PROGRAM MERGE
*     -------------
*
*     BDL-GF9612
*
*     This program allows to merge two direct access binary files SLP96
*     into a new direct access file SLP96.
*
*     The two files have to contain consistent ephemerides :
*     the time span of the ephemerides in file 1 must precede the time
*     span of the ephemerides in file 2.
*
*     Uses the subroutine MERGE.
*
* --- Declarations -----------------------------------------------------
*
      implicit double precision (a-h,o-z)
      character*40 a,fname1,fname2,fname3
*
* --- Parameters -------------------------------------------------------
*
*     fname1  = name of the first direct access binary file SLP96.
*     fname2  = name of the second direct access binary file SLP96.
*     fname3  = name of the new direct access binary file SLP96.
*     lu1     = logical unit of the first file SLP96.
*     lu2     = logical unit of the second file SLP96.
*     lu3     = logical unit of the new file SLP96.
*
      lu1     = 11
      lu2     = 12
      lu3     = 13
*
* --- Reading the parameters -------------------------------------------
*
100   continue
*
      write (*,'(//2x,''SLP96 : MERGE (BDL-GF9612)''//)')
*
      write (*,'(2x,''Name of the first file  : '',$)')
      read (*,'(a40)') fname1
      write (*,'(2x,''Name of the second file : '',$)')
      read (*,'(a40)') fname2
      write (*,'(2x,''Name of the new file    : '',$)')
      read (*,'(a40)') fname3
*
200   continue
*
      write (*,'(/2x,''Confirm your choice (y/n) ? '',$)')
      read (*,'(a40)') a
      if (a(1:1).eq.'n'.or.a(1:1).eq.'N') goto 100
      if (a(1:1).ne.'y'.and.a(1:1).ne.'Y') goto 200
      write (*,'(/2x,''Executing ... '')')
*
* --- Merging the two files --------------------------------------------
*
      call MERGE (fname1,lu1,fname2,lu2,fname3,lu3,ierr)
*
      if (ierr.ne.0) then
         write (*,'(/2x,''Error'',i3/)') ierr
         stop
      endif
*
      write (*,'(/2x,''New file created : '',a)') fname3
*
300   continue
*
      write (*,'(/2x,''Another Merger (y/n) ? '',$)')
      read (*,'(a40)') a
      if (a(1:1).eq.'y'.or.a(1:1).eq.'Y') goto 100
      if (a(1:1).ne.'n'.and.a(1:1).ne.'N') goto 300
*
      write (*,'(/2x,''Program ended''/)')
      stop
*
      end
*
*
*
*     ---------------------------------------------------------
      subroutine MERGE (fname1,lu1,fname2,lu2,fname3,lu3,ierr)
*     ---------------------------------------------------------
*
*     BDL-GF9612
*
*     Merging two Ephemerides SLP96.
*
*     This routine merges two direct access binary files SLP96 into
*     a new direct access binary file SLP96.
*
*     The two files have to contain consistent ephemerides :
*     the time span of the ephemerides in file 1 must precede the time
*     span of the ephemerides in file 2.
*
* --- Input ------------------------------------------------------------
*
*               First direct access binary file SLP96 (file 1) :
*     fname1    Name of the file (character*40)
*     lu1       Logical Unit of the file (integer).
*
*               Second direct access binary file SLP96 (file 2) :
*     fname2    Name of the file (character*40)
*     lu2       Logical Unit of the file (integer).
*
*     Output
*     ------
*
*               New direct access binary file SLP96 (file 3) :
*     fname3    Name of the file (character*40)
*     lu2       Logical Unit of file (integer).
*
*     ierr      Error index (integer).
*
*               ierr=0 : no error.
*               ierr=1 : error in file 1.
*               ierr=2 : error in file 2.
*               ierr=3 : error of coherence between file 1 and file 2.
*               ierr=4 : error in file 3.
*
* --- Declarations -----------------------------------------------------
*
      implicit double precision (a-h,o-z)
      character*40 fname1,fname2,fname3
      logical fexist
*
      parameter (lrec=5576,lcoef=lrec/8-1)
*
      dimension coef(lcoef),loc(11,3)
*
* --- Open the first direct access binary file SLP96 (file 1) ----------
*
      inquire (file=fname1,exist=fexist)
*
      if (.not.fexist) then
         ierr=1
         return
      endif
*
      open (lu1,file=fname1,access='direct',recl=lrec,status='old',
     .      iostat=nerr)
*
      if (nerr.ne.0) then
         ierr=1
         return
      endif
*
      read (lu1,rec=1,iostat=nerr)
     .     idf,tla1,tlb1,delta,ncoef,nblock1,nbytes,loc
*
      if (nerr.ne.0) then
         ierr=1
         return
      endif
*
* --- Open the second direct access binary file SLP96 (file 2) ---------
*
      inquire (file=fname2,exist=fexist)
*
      if (.not.fexist) then
         ierr=2
         return
      endif
*
      open (lu2,file=fname2,access='direct',recl=lrec,status='old',
     .      iostat=nerr)
*
      if (nerr.ne.0) then
         ierr=2
         return
      endif
*
      read (lu2,rec=1,iostat=nerr)
     .     idf,tla2,tlb2,delta,ncoef,nblock2,nbytes,loc
*
      if (nerr.ne.0) then
         ierr=2
         return
      endif
*
* --- Comparison between file 1 and file 2 -----------------------------
*
      ierr=3
*
      if (tla1.gt.tla2.or.tlb1.lt.tla2) then
         ierr=3
         return
      endif
*
* --- Open the new direct access binary file SLP96 (file 3) ------------
*
      inquire (file=fname3,exist=fexist)
*
      if (fexist) then
         ierr=4
         return
      endif
*
      open (lu3,file=fname3,access='direct',recl=lrec,status='new',
     .      iostat=nerr)
*
      if (nerr.ne.0) then
         ierr=4
         return
      endif
*
* --- Merge file 1 and file 2 into file 3 ------------------------------
*
      n2=dint(tlb1-tla2+1.d-5)/delta
      nblock3=nblock1+nblock2-n2-1
*
      write (lu3,rec=1)
     .      idf,tla1,tlb2,delta,ncoef,nblock3,nbytes,loc
*
      nr1=1
      nr3=1
      nmax=nblock1-1
*
      do n=1,nmax
         nr1=nr1+1
         nr3=nr3+1
         read (lu1,rec=nr1) tjd0,(coef(i),i=1,ncoef)
         write (lu3,rec=nr3) tjd0,(coef(i),i=1,ncoef)
      enddo
*
      nr2=n2+1
      nmax=nblock2-n2-1
*
      do n=1,nmax
         nr2=nr2+1
         nr3=nr3+1
         read (lu2,rec=nr2) tjd0,(coef(i),i=1,ncoef)
         write (lu3,rec=nr3) tjd0,(coef(i),i=1,ncoef)
      enddo
*
* --- Exit -------------------------------------------------------------
*
      close (lu1)
      close (lu2)
      close (lu3)
*
      ierr=0
      return
*
      end
