!
! --- PROGRAM VSOP2013_binfile -----------------------------------------
!
!     BUILD A DIRECT ACCES FILE OF A CHEBYSHEV EPHEMERIS (BINARY)
!     FROM A SEQUENTIAL FILE OF A CHEBYSHEV EPHEMERIS (ASCII))
!
!     THE USER HAS TO CHOICE THE VALUES OF THE PARAMETERS
!     NAMEF1 : NAME OF THE SEQUENTIAL FILE   (character*40)
!     NAMEF2 : NAME OF THE DIREC ACCESS FILE (character*40)
!     LREC   : LENGH OF RECORDS IN THE DIRECT ACCESS FILE (integer).
!
!     *** CAUTION ***
!     The block size of the direct acces file is expressed in unit which
!     depends on the Fortran compiler: 
!     7840 in bytes or 
!     3920 in words of 4 bytes or
!     1960 in words of 2 bytes.
!     
!     ERROR IN THE SEQUENTIAL FILE
!     Stop11 : Error in open statement
!     Stop12 : Error in read statement (Header)
!     Stop13 : Error in read statement (Data)
!
!     ERROR IN THE DIRECT ACCES FILE
!     Stop21 : Error in open statement
!     Stop22 : Error in write statement (Header)
!     Stop23 : Error in write statement (Data)
!      
! --- Declarations -----------------------------------------------------
!
      implicit none

      character*40                  :: namef1,namef2
      integer                       :: i,j,k,l,n,m
      integer                       :: nul1,nul2,nerr
      integer                       :: idf,nintv,ncoef,lrec
      real*8                        :: t1,t2,delta,dj1,dj2
!
      integer,      dimension(3,9)  :: loc
      real*8,       dimension(978)  :: coef
!
      integer,      dimension(6)    :: icoef
      real*8,       dimension(6)    :: xcoef
!
! --- Parameters -------------------------------------------------------
!
      NAMEF1='VSOP2013.m4000'      ! [-4500 -3500]
      NAMEF1='VSOP2013.m2000'      ! [-3500 -1500]
      NAMEF1='VSOP2013.m1000'      ! [-1500  0]
      NAMEF1='VSOP2013.p1000'      ! [ 0  1500]
      NAMEF1='VSOP2013.p2000'      ! [+1500 +3000]
      NAMEF1='VSOP2013.p4000'      ! [+3000 +4500]
!
      NAMEF2='VSOP2013.m4000.bin'  ! [-4500 -3500]
      NAMEF2='VSOP2013.m2000.bin'  ! [-3500 -1500]
      NAMEF2='VSOP2013.m1000.bin'  ! [-1500  0]
      NAMEF2='VSOP2013.p1000.bin'  ! [ 0  1500]
      NAMEF2='VSOP2013.p2000.bin'  ! [+1500 +3000]
      NAMEF2='VSOP2013.p4000.bin'  ! [+3000 +4500]
!
      LREC = 1960   !!! *** size in words of 2 bytes
      LREC = 3920   !!! *** size in words of 4 bytes
      LREC = 7840   !!! *** size in bytes
!
! --- Opening the files ------------------------------------------------
!
      nul1=10
      nul2=20
!
      write (*,'(/2x,a/)') 'Copy Processing ...'
!
      open (nul1,file=NAMEF1,iostat=nerr)
      if (nerr.ne.0) stop11
      open (nul2,file=NAMEF2,access='direct',recl=LREC,iostat=nerr)
      if (nerr.ne.0) stop21
!
! --- Copy the Header record -------------------------------------------
!
      read (nul1,1001,iostat=nerr) idf,t1,t2,delta,nintv,ncoef
      if (nerr.ne.0) stop12
      read (nul1,1002,iostat=nerr) ((loc(i,j),j=1,9),i=1,3)
      if (nerr.ne.0) stop12
      write (nul2,rec=1,iostat=nerr) idf,t1,t2,delta,nintv,ncoef,loc
      if (nerr.ne.0) stop22
!
! --- Copy the Chebyshev coefficients ----------------------------------
!
      do k=2,nintv+1
         read (nul1,1003,iostat=nerr) dj1,dj2
         if (nerr.ne.0) stop13
         do n=1,163
            read (nul1,1004,iostat=nerr) (xcoef(l),icoef(l),l=1,6)
            if (nerr.ne.0) stop13
            do l=1,6
               m=(n-1)*6+l
               coef(m)=xcoef(l)*10.d0**icoef(l)
            enddo
         enddo
         write (nul2,rec=k,iostat=nerr) dj1,dj2,coef
         if (nerr.ne.0) stop23
      enddo
!
      write (*,'(2x,a/)') 'Done'
      stop
!
! --- Format -----------------------------------------------------------
!
1001  format (2x,i4/2x,f9.1/2x,f9.1/2x,f4.1/2x,i6/2x,i4)
1002  format (1x,9(1x,i4.4)/1x,9(1x,i4.4)/1x,9(1x,i4.4))
1003  format (2x,f9.1,2x,f9.1)
1004  format (2x,6(f20.16,i4))
!
      end
