C
C
      subroutine ts1_moody_write(casco_iter,kd,seckd,iter,delt,nn,zeta,
     $                     ubar,vbar)

C Include file for parameter statements
      INCLUDE 'CASCO.DIM'
C Fixed global variables
      integer casco_iter
      integer kd, iter
      real    seckd
      real    delt
      integer nn 
      real zeta(nndim),ubar(nndim),vbar(nndim)

      character*120 filename, fileroot

      integer samplnod(NNDIM)
      integer lunts1, luninput
      real    sampdt
      integer nts 
      logical do_output

      integer kd0
      real    sec0

      SAVE   kd0, sec0
      SAVE   nts, sampdt
      SAVE   samplnod
      SAVE   fileroot
      SAVE   do_output

      integer i,n  
      real    dt 
      integer istart1, iend1
      integer istart2, iend2
      character*10 citer
      character*120 fname


      lunts1 = 50 
      luninput = 50

      if((casco_iter.eq.1).and.(iter.eq.0)) then 
          write(*,*)'ts1_moody_write: casco_iter ',casco_iter
          write(*,*)'Enter name of file with node numbers to sample'
	  write(*,*)'For no time series sampling enter: none'  
	  read(*,'(a)')filename 
	  do_output = .false.
          if(filename.ne.'none') then
             do_output = .true.
	     write(*,*)filename 
	     write(*,*)'Enter sampling interval (seconds)'
	     read(*,*)sampdt
	     open(luninput,file=filename,status='old')

             nts = 1 
101          continue
                read(luninput,*,end=102)samplnod(nts)
                write(*,*)nts,samplnod(nts)
                nts = nts+1 
	        goto 101
102          continue
             close(luninput)
	     nts = nts-1

             write(*,*)'Enter root name for sampling output files '
	     read(*,'(a)')fileroot 
             write(*,*)'sample n nodes with dt ',nts, sampdt
             write(*,*)'Time series output root file name:'
             write(*,'(a)')fileroot
	  endif
       endif

       if(do_output.eqv..true.) then
          if(iter.eq.0) then 
C create file name and open file 
          write(*,*)'ts1_moody_write: casco_iter, nts ',casco_iter,nts
             write(citer,'(i2.2)')casco_iter
             call cstring(citer,istart1,iend1)
             call cstring(fileroot,istart2,iend2)
             fname =  fileroot(istart2:iend2) // '.' 
     $           // citer(istart1:iend1) // '.ts1'
             write(*,*)'Output file name ',fname 

 	     open(lunts1,file=fname,status='unknown')
             write(lunts1,*)nts

C  save time at start of run 
             kd0 = kd
	     sec0 = seckd 

          elseif(ITER.gt.0) then
	     dt = (kd - kd0)*24*3600. + (seckd - sec0)
c            if(int(dt/sampdt)*sampdt.gt.dt-delt) then
C even though moody goes backwards in time, delt is > 0 
	     if(int(dt/sampdt)*sampdt.lt.dt+delt) then
c               write(*,*)'inside'	     
                write(lunts1,*)kd, seckd,dt,iter
	        do i = 1,nts
	           n = samplnod(i)  
	           write(lunts1,*)n,zeta(n),ubar(n),vbar(n)
	        enddo
             endif
         elseif(ITER.eq.-1) then
	    close(lunts1)
         else 
	    write(*,*)'OUTPUT  Error:  ITER < -1.  = ',ITER
	    stop
         endif
      endif 

      RETURN
      END


C123456789012345678901234567890123456789012345678901234567890123456789012
C*****************************************************************
      subroutine ts1_saco_write(casco_iter,kd,seckd,iter,delt,nn,zeta,
     $                     ubar,vbar)

C Include file for parameter statements
      INCLUDE 'CASCO.DIM'
C Fixed global variables
      integer casco_iter
      integer kd, iter
      real    seckd
      real    delt
      integer nn 
      real zeta(nndim),ubar(nndim),vbar(nndim)

      character*120 filename, fileroot

      integer samplnod(NNDIM)
      integer lunts1, luninput
      real    sampdt
      integer nts 
      logical do_output

      integer kd0
      real    sec0

      SAVE   kd0, sec0
      SAVE   nts, sampdt
      SAVE   samplnod
      SAVE   fileroot
      SAVE   do_output

      integer i,n  
      real    dt 
      integer istart1, iend1
      integer istart2, iend2
      character*10 citer
      character*120 fname


      lunts1 = 50 
      luninput = 50

c     if((casco_iter.eq.1).and.(iter.eq.0)) then 
      if(((casco_iter.eq.1).or.(casco_iter.eq.-1))
     $                                    .and.(iter.eq.0)) then 
          write(*,*)'ts1_saco_write: casco_iter ',casco_iter
          write(*,*)'Enter name of file with node numbers to sample'
	  write(*,*)'For no time series sampling enter: none'  
	  read(*,'(a)')filename 
	  write(*,*)filename 
          do_output = .false.
	  if(filename.ne.'none') then 
	     do_output = .true.  
	     write(*,*)'Enter sampling interval (seconds)'
	     read(*,*)sampdt
	     open(luninput,file=filename,status='old')

             nts = 1 
101          continue
                read(luninput,*,end=102)samplnod(nts)
                write(*,*)nts,samplnod(nts)
                nts = nts+1 
	        goto 101
102          continue
             close(luninput)
	     nts = nts-1

             write(*,*)'Enter root name for sampling output files '
	     read(*,'(a)')fileroot 
             write(*,*)'sample n nodes with dt ',nts, sampdt
             write(*,*)'Time series output root file name:'
             write(*,'(a)')fileroot
          endif
       endif

       if(do_output.eqv..true.) then 
          if(iter.eq.0) then 
C create file name and open file 
          write(*,*)'ts1_saco_write: casco_iter, nts ',casco_iter,nts
             write(citer,'(i2.2)')casco_iter
             call cstring(citer,istart1,iend1)
             call cstring(fileroot,istart2,iend2)
	     if(casco_iter.eq.-1) then 
                fname =  fileroot(istart2:iend2) // '.ts1'
	     else 
                fname =  fileroot(istart2:iend2) // '.' 
     $           // citer(istart1:iend1) // '.ts1'
	     endif
             write(*,*)'Output file name ',fname 

 	     open(lunts1,file=fname,status='unknown')
             write(lunts1,*)nts

C  save time at start of run 
	     kd0 = kd
	     sec0 = seckd 

          elseif(ITER.gt.0) then
	     dt = (kd - kd0)*24*3600. + (seckd - sec0)
C output at the first model time point which occurs after the 
C time required by the regular sampling interval sampdt. 
	     if(int(dt/sampdt)*sampdt.gt.dt-delt) then
c               write(*,*)'inside'	     
	        write(lunts1,*)kd, seckd,dt,iter
	        do i = 1,nts
	           n = samplnod(i)  
	           write(lunts1,*)n,zeta(n),ubar(n),vbar(n)
	        enddo
             endif
          elseif(ITER.eq.-1) then
	     close(lunts1)
          else 
	     write(*,*)'OUTPUT  Error:  ITER < -1.  = ',ITER
	     stop
          endif
       endif

      RETURN
      END

