c----------------------------------------

      subroutine timestamp(ival)

c----------------------------------------

c     TIMESTAMP after every step

      INCLUDE 'ssbox.inc'

      CALL CPU(CPURR)
      CPUTEMP=CPURR-TALKU
      
      do 1001 i=1,80
         if(runid(i:i).eq.'') goto 1002
 1001 continue
 1002 nlen=i-1
      
      do 10 iround=1,2

         if(iround.eq.1) then
            open(unit=17 ,file='TIMESTAMP',status='unknown')
         endif
         if(iround.eq.2) then
            open(unit=17 ,file=runid(1:nlen)//'.timestamp',
     +           status='unknown')
         endif
         
         write(17,*)     '---------------------------------------'
         write(17,*) runid,'   ',codeid
         
         if(ival.eq.0) then 
            write(17,*) 'CREATING INITIAL VALUES'
            goto 999
         endif
         
         if(ival.eq.1) then 
            write(17,*) 'FIRST STEP STARTED'
            goto 999
         endif
         
         if(ival.eq.2) then 
            write(17,*) 'RUN IN PROGRESS'
         endif

         if(ival.eq.3) then 
            write(17,*) 'RUN FINISHED NORMALLY'
         endif

         write(17,*)     '---------------------------------------'
         write(17,*) 'GMET,DMET ',GMET,DMET
         if(gmet.eq.2.and.isearch_order.eq.1)
     +        write(17,*) 'x-ordering'
         if(gmet.eq.2.and.isearch_order.eq.2)
     +        write(17,*) 'y-ordering'
         
         write(17,*) 'omega,rho,smin,smax',omega,roo,s(1),s(lkm)

         
         write(17,*)     '---------------------------------------'
 1703    format(a18,i6,2f12.3,f12.4)
         write(17,1703) 'LKM,XL,YL,TAU    ',LKM,XL,YL,TAUMIN
         

         WRITE(17,7701) 'DIST,OME,KAP/OME,OMEZ/OME  ',
     +        eta,omega,kappa/omega,wz/omega
 7701    format(a28,g12.2,g12.5,f10.4,f10.4)
         
         WRITE(17,7702) 'SMIN,SMAX,NSIG, QQ         ',
     +        s(1),s(lkm),nsig,qq
 7702    format(a28,2f12.5,I10,f10.3)
         

         apu=pii/alfome/(dt/alfstep)
 1705    format(a28,f12.5,f12.0,I12,f12.2)
         write(17,1705) ' DT, DOME, DSTEP, TDUR/DT_S  ',
     +        DT/period,alfome/omega,alfstep,apu
         
 1706    format(a21,i12,f10.2,g12.4)
         write(17,1706) ' IMODEL, LAMDA, V0   ',IMODEL,LAMDA,V0
         
 1716    format(a21,f12.3,2f12.3)
         write(17,1716) 'BETA  LFRIC QZ_INIT ',BETA,LFRIC,QZINIT/w0
         
 1708    format(a22,e14.3,e14.3)
         write(17,1708) ' ALF_FIRST: MEAN,MAX  ',
     +        alfmean_first,alfmax_first
 1709    format(a30,2f12.3)
         write(17,1709) ' DERO=A*dv*dt+B*raja1 with A,B= ',
     +        erovak1,erovak2
         if(dmet.lt.0) then
            write(17,*) 'PAIR, NOPAIR ',n_pair,N_nopair
         ENDIF
 1707    format(a29,i12,i8,g14.4,2f10.4)
         write(17,1707) ' IX, NSAV, ALFMAX, R1, EROmax ',
     +        ix,nsav,alfmax,raja1,ero1max
         
         if(gmet.gt.2) then
            write(17,*)     '---------------------------------------'
            write(17,*) 'NXG,NYG,NZG ',
     +           nxg,nyg,nzg
 1720       format(a21,4f12.3)
            write(17,1720) ' ENCMIN,ENCLIM,FZR,ZMAX ',encmin,enclim,
     +           fzrange,zmax_stamp
            write(17,*)    'MX_G,MY_G ',mxgravi,mygravi
            write(17,1104) 'f_check3: ',orb,fx_check2,fy_check2,fz_check3
 1104       format(a10,f12.3,3e20.6)
         endif
         
         cpusub=cpu_ret+cpu_lim+
     +        cpu_pota+cpu_int+cpu_sg+cpu_potb

      write(17,*)
      write(17,*)     'ROUTINE         CPU          % of TOTAL'
      write(17,*)     '---------------------------------------'
      write(17,4848)  'RET    ',cpu_ret,cpu_ret/cputemp*100.
      write(17,4848)  'LIM1   ',cpu_lim,cpu_lim/cputemp*100.
      write(17,4848)  'POTA   ',cpu_pota,cpu_pota/cputemp*100.
      write(17,4848)  'POTB   ',cpu_potb,cpu_potb/cputemp*100.
      write(17,4848)  'INTE   ',cpu_int,cpu_int/cputemp*100.
      write(17,4848)  'SG:    ',cpu_sg,cpu_sg/cputemp*100.
      write(17,4848)  ' FAR   ',cpugrav2,cpugrav2/cputemp*100.
      write(17,4848)  ' NEAR  ',cpugrav3,cpugrav3/cputemp*100.
      write(17,4848)  'SUBTOT ',cpusub,cpusub/cputemp*100.
      write(17,*)     '---------------------------------------'
      testimate=
     +     (cputemp-cpustamp)/(orb-orbstamp)*(orbmax-orbori)
      
 1900 format(a10,f14.2,a13,f14.2)
      write(17,1900) 'CPUTOT',cputemp,'EST-CPU:',testimate
      
 1901 format(a22,2f8.0)
      write(17,1901) '-FOR ORB TO ORBMAX ',orbori,orbmax
      write(17,*) runid
 1704 format(a18,f12.5,i14,f12.4)
      write(17,1704) 'ORB,COLL,FREK',orb,coll,FREK

      write(17,1772) 'cx, cy, cz, cxy',
     +     cx_stamp,cy_stamp,cz_stamp,cxy_stamp
      write(17,1774) 'const:u,w,z,zp',
     +     uvak_all,wvak_all,zvak_all,zpvak_all

 1810 format(a16,4g12.4)
 1813 format(a16,2g12.4)

      close(17)
      
      
 1772 format(a16,3f14.4,f12.2)
 1773 format(a16,2f14.4)
 1774 format(a16,4g16.6)
      
 1771 format(' x,y,z,wz/OMEGA ',3g12.2,f12.4)
 4848 format(A8,2x,f12.2,f12.2)
          
      cpustamp=cputemp
      orbstamp=orb 

 999  continue
      close(17)

 10   continue
      end

