
       program prepinsol
*--------------------------------------------------------------------------
*
*      this program construct the binary file containning
*      the necessary quantities
*
*      t,k,h,eps,phi
*      which will be use for the computation of the insolation
*      quantities by the program insol
*
*      this binary file is constructed using the ASCII files
*      containing
*                 -  t,k,h,q,p  (stepsize 1000 years)
*                 -  t,eps,phi
*
*  !!!!!!!! the stepsize is always 1000 years !!!!!!!!!!!!
*
*      the parameters are taken in the NAMELIST file  : 'prepinsol.par'
*
*
*       nomascpos   :  ASCII file for elements t,k,h,q,p
*                      positive time
*       nomascneg   :  ASCII file for elements t,h,h,q,p
*                      negative time
*       nomprecpos  :  ASCII file for elements t,eps,phi
*                      positive time
*       nomprecneg  :  ASCII file for elements t,eps,phi
*                      negative time
*       datedebut   :  starting time in millions of years
*       datefin     :  end      time in millions of years
*                        ( -20 <= datedebut < datefin <= +10 )
*       nomfich     :  binary file for the elements t,k,h,eps,phi (OUT)
*       statut      :  'new' or 'unknown'
*
*     (c) ASD/BdL version 0.8 (6/6/93)
*---------------------------------------------------------------------
      implicit double precision (a-h,o-z)
      parameter(nacd=100,nbel=5)
      dimension Taux(nbel,nacd)
      character *20 statut
      character *50 nomascpos,nomascneg,nomprecpos,nomprecneg,
     &nomfich
      NAMELIST/NAMSTD/nomascpos,nomascneg,nomprecpos,nomprecneg,
     &datedebut,datefin,statut,nomfich
      open(8,file='prepinsol.par',form='formatted',status='old')
      read(8,NAMSTD)
      close(8)
      write(*,*) ' Solution La90-La93 for the Earth '
      write(*,*)
      write(*,*) ' La93.prepinsol  version 0.8'
      write(*,*) ' preparation step for insolation computation'
      write(*,*) ' (c) ASD/BdL (1993) '
      write(*,*)
      write(*,*) ' ASCII file for positive time (el. ell.) :',nomascpos
      write(*,*) ' ASCII file for negative time (el. ell.) :',nomascneg
      write(*,*) ' ASCII file for positive time (el. pre.) :',nomprecpos
      write(*,*) ' ASCII file for negative time (el. pre.) :',nomprecneg
      write(*,*) ' Binary file for elements t,k,h,eps,phi  :',nomfich
      write(*,*) ' starting time (Myr)                     :',datedebut
      write(*,*) ' ending   time (Myr)                     :',datefin
      pas=1D3
      nrecl=8*nacd*nbel
      open(10,file=nomfich,status=statut,access='direct',
     *recl=nrecl,err=99)
      idebut=int(datedebut*1.d6/pas)
      ifin=int(datefin*1.d6/pas)
c
      if (idebut.lt.0.and.ifin.lt.0) then
        ndebut=idebut/nacd
        nfin=ifin/nacd
        pdebut=0
        pfin=-1
      endif
      if (idebut.gt.0.and.ifin.gt.0) then
        ndebut=1
        nfin=0
        pdebut=idebut/nacd
        pfin=ifin/nacd
      endif
      if (idebut.le.0.and. ifin.ge.0) then
        ndebut=idebut/nacd
        nfin=-1
        pdebut=0
        pfin=ifin/nacd
      endif
c
      if (idebut.lt.0) then
        open(11,file=nomascneg,status='old')
        open(12,file=nomprecneg,status='old')
        if (ifin.eq.0) then
          read(11,*,err=99) t,rk,rh,rq,rp
          read(12,*,err=99) t,eps,rphi
          Taux(1,1)=t
          Taux(2,1)=rk
          Taux(3,1)=rh
          Taux(4,1)=eps
          Taux(5,1)=rphi
          do i=2,nacd
            do j=1,5
              taux(j,i)=0.D0
            enddo
          enddo
          nrec=-ndebut+1
          write(10,rec=nrec) taux
        else 
          read(11,*)
          read(12,*)
        endif
        do i=-1,ifin,-1
          read(11,*)
          read(12,*)
        enddo
        do n=nfin,ndebut,-1
          do i=nacd,1,-1
            read(11,*,err=99,end=100) t,rk,rh,rq,rp
            read(12,*,err=99,end=100) t,eps,rphi
            Taux(1,i)=t
            Taux(2,i)=rk 
            Taux(3,i)=rh
            Taux(4,i)=eps
            Taux(5,i)=rphi
          enddo
          go to 101
100       do ii=i,1,-1
            do j=1,nbel
              Taux(j,ii)=0
            enddo
          enddo
101       nrec=(n-ndebut)+1
          write(10,rec=nrec)Taux
        enddo
      endif 
c    
      if (ifin.gt.0) then
        open(13,file=nomascpos,status='old')
        open(14,file=nomprecpos,status='old')
        do i=0,idebut-1
          read(13,*)
          read(14,*)
        enddo
        do n=pdebut,pfin
          do i=1,nacd
            read(13,*,err=99,end=200) t,rk,rh,rq,rp
            read(14,*,err=99,end=200) t,eps,rphi
            Taux(1,i)=t
            Taux(2,i)=rk 
            Taux(3,i)=rh
            Taux(4,i)=eps
            Taux(5,i)=rphi
          enddo
          go to 201
200       do ii=i,nacd
            do j=1,nbel
              Taux(j,ii)=0
            enddo
          enddo
201       nrec=(n-ndebut)+1
          if (idebut.gt.0) nrec=n-pdebut+1
          write(10,rec=nrec)Taux
        enddo
      endif 
      go to 300
99    write(*,*)' erreur!'
      stop
300   continue
      end

