c
c  simple program to extract out a section of a seisan file (any seisan
c  primary format). a similar job can be done with wavetool, 
c  however seicut can do the sam ecut on many files
c  
c  jh, april 2004
c
c  changes
c
c  2005 oct 21  jh: Fix so it also works with files with more than 30 channels
c  2007 apr 20  jh: Make it work with filenr.lis, and use relative time from
c                   start of file 
c
c  mode 1 with one file
c
c  syntax is: seicut filename yyyymmdd..  inteval (sec)
c  
c  If no input on prompt, user can enter values manually.
c  the first sample to use is the first sample found before the start time
c  the time interval out will be time from first to last sample, so if e.g.
c  one second of data is asked for at a sample rate of 100 Hz, the time interval
c  in header will be 0.99 sec and the number of samples output will be 100.
c
c  if interval not available in any of the channels, the program will stop.
c
c
c  mode 2
c
c  syntax is : seicut
c
c  When entereing a file name, filenr.lis can be used. The program will
c  then use a list of files. Start time is now relative to start of file, 
c  interval is same as before. So all files are cut the same amount. if
c  file is too short, there is an option to use end of file as end time.
c
c
c  
c  the output file name will use a network code reflecting station code of
c  first channel in input file and '_CUT' is added to the end of the file name

      implicit none
      include 'seidim.inc'                    ! for giving dimensions
      include 'libsei.inc'                    ! for all seisan subroutines
      include 'waveform.inc'                  ! waveform data
      include 'seisan.inc'                    ! seisan general parameters
      character*80 arg(5)    	   	      ! arguments to cfile
      integer nargs                           ! number of arguments
      integer doy                             ! day of year
      character*5 net_code                    ! network code
      character*80 outfile                    ! output file name
      Character*80 start_time                 ! start time
      character*80 mainhead(max_trace)        ! waveform header
      character*1040 chead                    ! waveform header
      integer syear,smonth,sday,shour,smin    ! start time
      real*8 abs_start                        ! abs time of start time
      integer last_samp(max_trace)            ! sample number for last sample
      real ssec                               ! start sec
      real interval                           ! interval to extract (sec)
      real start_t                            ! start time relative to file start, many files
      integer seiclen                         ! function
      integer accept                          ! if 1, accept many files too short
      real del_time                           ! time difference between first sample and
                                              ! sample to use
      integer first_samp(max_trace)           ! first sample to use, first sample in trace
                                              ! is number 1
      integer i,l,k,kk,in                        ! counters

      accept=0
c
c   get arguments, first is start time in yyyymmdd... and second is interval in seconds
c   this argument function is SEISAN general, has the same form on all computer platforms
c
      call get_arguments(nargs,arg)
C
c   check arguments
c
      
      if (nargs.lt.3) then
         write(6,*)' Not enough arguments'
         write(*,*)
     *  'filename start time (yymmdd...) and time',
     *  ' interval (sec) can be given'
         write(6,*)
         write(6,*) 'Will use interactive input'
c
c   just enter one wav name
c
         write(6,*)
     *   'Waveform file name, can be filenr.lis for many files'
         read(5,'(a)') wav_filename(1)
         in=0
         if(wav_filename(1)(1:10).eq.'filenr.lis') then
            open(8,file='filenr.lis',status='old',err=20)
            goto 21
 20         continue
            write(6,*)' No filenr.lis'
             stop
 21          continue
             in=1
          endif          
c
c   one file
c 
         if(in.eq.0) then      
            write(6,*)
     *      'start time (yyyymmddhhmmss.sss)'
            read(5,'(a)') start_t
            read(start_t,'(i4,4i2,f6.0)')
     *      syear,smonth,sday,shour,smin,ssec
         endif
c
c   many files
c
         if(in.eq.1) then
            write(6,*) 'Start time (sec) relative to file start time'
            read(5,*) start_t
         endif

         write(6,*)'Interval in seconds'
         read(5,*) interval
      else
         wav_filename(1)=arg(1)
         read(arg(2),'(i4,4i2,f6.0)')
     *   syear,smonth,sday,shour,smin,ssec
         read(arg(3),'(f9.0)') interval
      endif

      wav_nfiles=1                   ! there is only one file in this case
      wav_nchan=0

c
c  file loop if many files
c

 1000 continue

      if(in.eq.1) then
         read(8,'(7x,a)') wav_filename(1)
         if(wav_filename(1)(1:4).eq.'    ') stop
      endif
      write(6,'(1x,a)') wav_filename(1)
      call wav_init
c
c   read all headers of waveform file 
c
      call read_wav_header(1)
c
c   output possible errors
c
      if(wav_error_message.ne.' ') write(6,'(1x,a)') 
     *wav_error_message
c
c   write out the format
c
      write(6,'(1x,a,a)') 'Format ',wav_file_format(i)
c
      write(6,*)' Total number of channels available:',wav_nchan
c
c   write some input info for each channnel on screen
c
      write(6,'(a,a)')
     *' CHA STAT  COMP  YEAR MO DA HR MI    SEC  NSAMP ',
     *                '    RATE    DELAY DURATION'
      do i=1,wav_nchan
         write(6,'(1x,i3,1x,a,1x,a,1x,i5,4i3,f7.3,1x,i6,3f9.3)')
     *   i, wav_stat(i),wav_comp(i),
     *   wav_year(i),wav_month(i),wav_day(i),wav_hour(i),wav_min(i),
     *   wav_sec(i),wav_nsamp(i),wav_rate(i),wav_delay(i),
     *   wav_duration(i)
      enddo
      write(6,*)
c
c   find first sample to use for each channel
c
      do i=1,wav_nchan
         if(in.eq.0) then
            call timsec(syear,smonth,sday,shour,smin,ssec,abs_start)
            del_time=abs_start-wav_abs_time(i)
         else
            del_time=start_t
         endif
c
c  check if start time ok
c
         if(del_time.lt.0) then
            write(6,*)'channel ',i
c           write(6,*)'start time is before channel start time'
            stop
         endif
         first_samp(i)=del_time*wav_rate(i)+1
c        write(6,*) 'del time, first samp', del_time,first_samp(i)
c
c   check if end time ok
c
         last_samp(i)=first_samp(i)+wav_rate(i)*interval-1 
         if(last_samp(i).gt.wav_nsamp(i)) then
             write(6,*) 'Channel ',i
             write(6,*) 'End time after channel end'
c
c   case of many files
c
             if(accept.eq.1.and.in.eq.1) then
                last_samp(i)=wav_nsamp(i)
             endif
             if(in.eq.1.and.accept.eq.0) then
               write(6,*) 
     *        'Write available samples for this file only (1)'
               write(6,*)
     *        'Write available samples for all files      (2)'
               write(6,*)
     *        'Stop                                       (3)'
               read(5,*) k
               if(k.eq.1.or.k.eq.2) then
                  last_samp(i)=wav_nsamp(i)
               endif
               if(k.eq.2) accept=1                  
               if(k.eq.3) stop
             endif

c
c  always stop if one file only
c
             if(in.eq.0) then
                stop
             endif
          endif
c
c  find exact time of first sample and put in ouput common block
c
          abs_start=wav_abs_time(i)+(first_samp(i)-1)/wav_rate(i)          
          call sectim(abs_start,wav_year(i),doy,wav_month(i),wav_day(i),
     *    wav_hour(i),wav_min(i),wav_sec(i))
c          wav_nsamp(i)=wav_rate(i)*interval
          wav_nsamp(i)=last_samp(i) - first_samp(i) +1
       enddo
c
c   adjust common block variables for trimmed output
c
c     do i=1,wav_nchan
c        wav_year(i)=syear
c        wav_month(i)=smonth
c        wav_day(i)=sday
c        wav_min(i)=smin
c        wav_sec(i)=ssec
c     enddo
c
c   make and write seisan output header
c
      net_code='     '
      call wav_sheads(1,net_code,outfile,mainhead,chead)   ! make headers
c      write(6,*) outfile(1:seiclen(outfile))//'_CUT'
      write(6,*) ' Output filename: '
     &   //outfile(1:seiclen(outfile))//'_CUT'
      open(2,file=outfile(1:seiclen(outfile))//'_CUT',
     *status='unknown',form='unformatted')
c
c   write headers, assume maximum of 30 channels, since only 12 headers are written out
c
      do i=1,12
          write(2) mainhead(i)
      enddo
c
c   write remaining main header lines if more than 30
c
         if(wav_nchan.gt.30) then
            kk=(wav_nchan-31)/3+1
            do i=13,kk+12
               write(2)mainhead(i)
            enddo
         endif

      write(6,*) mainhead(1)(1:79)
c
c  read the waveform data, one trace at a time
c
      do i=1,wav_nchan
          call wav_read_channel(i)                            ! read one trace, read as integer, converted to real
          call wav_sheads(i,net_code,outfile,mainhead,chead)  ! make channel header
          write(2) chead                                      ! write channel header
          write(6,*) chead(1:79)
          write(2)(int(signal1(l)),
     *    l=first_samp(i),last_samp(i)) ! write trace, converted to integer
      enddo
      close(2)
c
c   if many files go back for next
c
      if(in.eq.1) goto 1000
      stop
      end

