44       character*77 version, creator
    46      &  
'STUFI V1.9 E. Wielandts filter routines for sff files')
    47       parameter(creator=
'1996 by Thomas Forbriger (IfG Stuttgart)')
    49       integer maxcontrol, maxsamples, maxfree, maxselect
    50       parameter(maxcontrol=50, maxsamples=500000, maxfree=400)
    51       parameter(maxselect=200)
    52       character*200 junkfile
    53       parameter(junkfile=
'stufijunkforreplace')
    55       integer filep,trace,i,j
    59       character*200 command(maxcontrol)
    62       character*20 code, outcode
    63       integer sffversion, ierr, nfilefree, ntracefree,flmax
    64       character*13 timestamp
    65       character*80 filefree(maxfree), tracefree(maxfree)
    67       character*132 wid2line
    68       character soutyp*20, soucs*1, soudate*6, soutime*10
    69       real souc1, souc2, souc3, tanf, ampfac
    71       integer idata(maxsamples)
    72       logical moretraces, expectmoretraces
    75       real tracec1, tracec2, tracec3
    80       logical selection(maxselect)
    82       integer maxopt, lastarg, iargc
    84       character*2 optid(maxopt)
    85       character*80 optarg(maxopt)
    86       logical optset(maxopt), opthasarg(maxopt)
    87       character*20 extension
    88       character*80 outfile, controlfile
    89       logical setext, replace, newfile, overwrite, verbose
    91       double precision x(maxsamples)
    92       character typ*3, par*80, msg*79
    96       data optid/2h-e,2h-r,2h-t,2h-d,2h-o,2h-v/
    97       data opthasarg/.true.,.false.,.true.,3*.false./
    98       data optarg/4h.sfi,1h-,4hjunk,3*1h-/
   105       if (iargc().eq.1) 
call getarg(1, controlfile)
   107       if ((controlfile(1:5).eq.
'-help').or.(iargc().lt.1)) 
then   110         print *,
'Usage: stufi controlfile [-e ext | -r | -t file] [-o] [-v]'   111         print *,
'             file [t:list] ...'   112         print *,
'or:    stufi -help'   113         if (iargc().lt.1) stop 
'ERROR: missing arguments\n'   115         print *,
'controlfile  Contains a sequence of seife commands'   116         print *,
'             that will be executed on every'   117         print *,
'             selected dataset. See below for'   118         print *,
'             available seife commands. A maximum'   119         print *,
'             of ',maxcontrol,
' lines is allowed.'   120         print *,
'-e ext       Write results to a file with the same'   121         print *,
'             name as the original dataset but with'   122         print *,
'             filename extension ext. The resluting'   123         print *,
'             file will contain all original data'   124         print *,
'             traces (the ones not selected will be'   125         print *,
'             unchanged. the default extension is:'   127         print *,
'-r           Replace the original datasets with'   128         print *,
'             the results of the filter operation.'   129         print *,
'-t file      Write all results to one file with'   130         print *,
'             the given name. This file will contain'   131         print *,
'             only the selected traces. Any file'   132         print *,
'             related FREE blocks or source'   133         print *,
'             definitions will be omitted.'   134         print *,
'-o           Overwrite existing file (is default together'   135         print *,
'             with -r option.'   136         print *,
'-v           be verbose'   137         print *,
'-d           produce debug output'   139         print *,
'Each datafile name may be followed by a list of'   140         print *,
'traces. This list selects a range of traces in'   141         print *,
'the file which will be processed. This list may'   142         print *,
'contain no blank (which is the separator to the'   143         print *,
'next filname). The traces will always be processed'   144         print *,
'in the order they appear in the data file.'   147         print *,
'  t:2           will select only trace 2'   148         print *,
'  t:4-6,2,4     will select traces 2, 4, 5 and 6'   149         print *,
'  t:9,8,10,14   will select traces 8, 9, 10 and 14'   151         print *,
'The message returned by each seife command will be'   152         print *,
'appended to the FREE block of each trace.'   155         print *,
'Some comments that come with the underlying library:'   161         call seife(typ,par,nsamples,dt,tmin,tsec,x,msg)
   163         print *,
'This program is compiled for:'   164         print *,
'                maximum number of samples: ',
   166         print *,
'    maximum number of lines in FREE block: ',
   168         print *,
'  maximum number of lines in control file: ',
   170         print *,
'  maximum number of traces to be selected: ',
   185       call tf_cmdline(2, lastarg,
   186      &     maxopt, optid, optarg, optset, opthasarg)
   207       if (debug) print *,
'DEBUG: debug messages are switched on'   210       call getarg(1, controlfile)
   212       if (iargc().lt.lastarg) stop 
'ERROR: missing data file\n'   223       if (debug) print *,
'DEBUG: read control file'   225       open(10, file=controlfile, err=99, status=
'old')
   227         read(10, 
'(a)', err=98, end=96) line
   229         command(ncommand)=line
   230         if (line(1:3).ne.
'end') 
goto 1
   232       if (ncommand.lt.1) stop 
'ERROR: missing commands\n'   239         if (verbose) print 80,
'writing to file ',outfile
   241           call sff_new(10, outfile, ierr)
   242           if (ierr.ne.0) stop 
'ERROR: deleting file'   244         open(10, file=outfile, err=95, status=
'new')
   246         call sff_wstatus(10, code)
   247         write(filefree(1), 
'(a)') version
   248         write(filefree(2), 
'(a)') 
   249      &  
'collecting all results in one file'   250         write(filefree(3), 
'(a)') 
   251      &  
'file related FREE blocks and source informations are ignored'   253         call sff_wfree(10, nfilefree, filefree)
   260       if (debug) print *,
'DEBUG: enter main loop'   265         call getarg(filep, infile) 
   269           print 80,
'reading new file ',infile
   273           call tf_nameext(outfile, extension)
   274           if (verbose) print 80,
'writing to file ',outfile
   275         elseif (replace) 
then   277           if (verbose) print *,
'replacing input file'   282         open(11, file=infile, err=92, status=
'old')
   283         if (.not.(newfile)) 
then   285             call sff_new(10, outfile, ierr)
   286             if (ierr.ne.0) stop 
'ERROR: deleting file'   288           open(10, file=outfile, err=95, status=
'new')
   293         call getarg(filep+1, line)
   294         if (line(1:2).eq.
't:') 
then   297           call tf_listselect(maxselect, selection, 3, line, ierr)
   299             print *,
'WARNING: selection exceeds possible range',
   300      &              
' from 1 to',maxselect
   301             print *,
'         selecting only up to no.',maxselect
   302           elseif (ierr.eq.2) 
then   303             print *,
'WARNING: missing selection list - selecting ',
   306           elseif (ierr.ne.0) 
then   307             print *,
'WARNING: unknown error code by tf_listselect'   308             print *,
'         selecting all traces'   320         call sff_rstatus(11,sffversion,timestamp,code,ierr)
   321         if (debug) print *,
'DEBUG: read status'   322         if (ierr.ne.0) stop 
'ERROR: reading status of input file\n'   323         if (.not.(newfile)) 
then   324           call sff_wstatus(10, code)
   325           if (debug) print *,
'DEBUG: wrote status'   328    10   
if (code(i:i).ne.
' ') 
then   329           if (debug) print *,
'DEBUG: code: ',code(i:i)
   330           if (code(i:i).eq.
'F') 
then   333      &          print *,
'MESSAGE: skipping FREE block of input file'   334               call sff_skipfree(11, ierr)
   335               if (debug) print *,
'DEBUG: skipped FREE block of file'   336               if (ierr.ne.0) stop 
'ERROR: skipping FREE block\n'   338               if (debug) print *,
'DEBUG: read FREE block'   339               call sff_rfree(11, nfilefree, filefree, 
   340      &                       flmax, maxfree, ierr)
   341               if (debug) print *,
'DEBUG: read FREE block of file'   342               if (ierr.ne.0) stop 
'ERROR: reading FREE block\n'   343               call sff_wfree(10, nfilefree, filefree)
   344               if (debug) print *,
'DEBUG: wrote FREE block of file'   346           elseif (code(i:i).eq.
'S') 
then   347             call sff_rsource(11, soutyp, soucs, souc1, souc2, souc3,
   348      &                       soudate, soutime, ierr)
   349             if (debug) print *,
'DEBUG: read SOURCE line of file'   350             if (ierr.ne.0) stop 
'ERROR: reading SOURCE line\n'   352               if (verbose) print *,
'MESSAGE skipping SOURCE line'   354               call sff_wsource(10, soutyp, soucs, souc1, souc2,
   355      &                         souc3, soudate, soutime)
   356               if (debug) print *,
'DEBUG: wrote SOURCE line of file'   367           if (debug) print *,
'DEBUG: next trace:',trace
   371             print 81,
'trace no.',trace,
':'   374           call sff_rdata(11, wid2line, nsamples, tanf, dt,
   375      &                   idata, ampfac, code, ierr)
   376           if (nsamples.gt.maxsamples) 
   377      &      stop 
'ERROR: too many samples\n'   378           if (debug) print *,
'DEBUG: read data'   379           if (ierr.ne.0) stop 
'ERROR: reading trace'    381    21     
if (code(i:i).ne.
' ') 
then   382             if (code(i:i).eq.
'F') 
then   383               call sff_rfree(11, ntracefree, tracefree, 
   384      &                       flmax, maxfree, ierr)
   385               if (debug) print *,
'DEBUG: read trace FREE block'   386               if (ierr.ne.0) stop 
'ERROR: reading FREE block\n'   387             elseif (code(i:i).eq.
'I') 
then   388               call sff_rinfo(11, tracecs, tracec1, tracec2, tracec3,
   390               if (debug) print *,
'DEBUG: read trace INFO line'   391               if (ierr.ne.0) stop 
'ERROR: reading INFO line\n'   392             elseif (code(i:i).eq.
'D') 
then   401           if (newfile.and.useselect.and.(.not.selection(trace))) 
then   402             if (verbose) print *,
'MESSAGE skipping trace'   404             if ((.not.useselect).or.selection(trace)) 
then   410               expectmoretraces=.false.
   412                 if (debug) print *,
'DEBUG: looking for more traces'   415                   do i=trace+1,maxselect
   416                     if (selection(i)) expectmoretraces=.true.
   419                   expectmoretraces=moretraces
   421                 if ((debug).and.(expectmoretraces))
   422      &            print *,
'DEBUG: more traces selected'   423                 if (iargc().gt.filep) 
then   424                   expectmoretraces=.true.
   425                   if (debug) print *,
'DEBUG: more files selected'   429    23           
if (code(i:i).ne.
' ') 
then   430                   if (code(i:i).eq.
'F') 
then   434                   if (code(i:i).eq.
'I') 
then   441                 if (expectmoretraces) outcode(j:j)=
'D'   443      &            
'DEBUG: code for this trace was: >',code,
'<'   446      &            
'DEBUG: code for this trace is: >',code,
'<'   454               if (debug) print *,
'DEBUG: entering seife'   455               if (debug) print *,
'DEBUG: convert int to double'   456               call tf_inttodouble(nsamples, maxsamples, 
   458               tmin=float(int(tanf/60.))
   467                 if (debug) print *,
'DEBUG: +',typ,
'++',par,
'+'   468                 call seife(typ, par, nsamples, dt, tmin, tsec,
   470                 if (debug) print *,
'DEBUG: returned from seife: ',msg
   471                 if (ntracefree.lt.maxfree) 
then   472                   if (debug) print *,
'DEBUG: write to FREE block'   473                   if (debug) print *,
'DEBUG: ',ntracefree
   474                   if (debug) print *,
'DEBUG: ',maxfree
   475                   ntracefree=ntracefree+1
   476                   write(tracefree(ntracefree), 
'(a)') msg
   479      &             
'WARNING: reached maximum length of FREE block'   481                 if (verbose) print *,msg
   486               if ((hour.gt.23).or.(hour.lt.0)) 
then   487                 print *,
'WARNING: time of first sample out of range'   488                 print *,
'WARNING: time of first sample set to zero'   493                 minute=int(tmin)-hour*60
   494                 if (minute.lt.0) 
then   495                   print *,
'WARNING: time of first sample out of range'   496                   print *,
'WARNING: time of first sample set to zero'   504               write(wid2line(17:28), 
'(i2,a1,i2,a1,f6.3)')
   505      &          hour,
':',minute,
':',second
   507               if ((srat.lt.1.e-4).or.(srat.gt.100.)) 
then   508                 write(wid2line(58:68), 
'(e11.6)') 1./dt
   510                 write(wid2line(58:68), 
'(f11.6)') 1./dt
   512               write(wid2line(49:56), 
'(i8)') nsamples
   513               call tf_doubletoint(nsamples, maxsamples, 
   515               if (debug) print *,
'DEBUG: exiting seife'   526             if (debug) print *,
'DEBUG: ',wid2line
   527             if (debug) print *,
'DEBUG: nsamples ',nsamples
   528             if (debug) print *,
'DEBUG: ampfac ',ampfac
   529             if (debug) print *,
'DEBUG: code ',code
   530             call sff_wdata(10, wid2line, nsamples, idata,
   532             if (debug) print *,
'DEBUG: wrote data'   534    22       
if (code(i:i).ne.
' ') 
then   535               if (code(i:i).eq.
'F') 
then   536                 call sff_wfree(10, ntracefree, tracefree)
   537                 if (debug) print *,
'DEBUG: wrote FREE block of trace'   538               elseif (code(i:i).eq.
'I') 
then   539                 call sff_winfo(10, tracecs, tracec1, tracec2, tracec3,
   541                 if (debug) print *,
'DEBUG: wrote INFO line of trace'   547         if (moretraces) 
goto 20
   550         if (.not.(newfile)) 
then   558           call system(
'/bin/mv '//junkfile//
' '//infile)
   559           if (debug) print *,
'DEBUG: /bin/mv '//junkfile//
' '//infile
   562         if (filep.le.iargc()) 
goto 2
   567       if (newfile) 
close(10, err=93)
   568       if (expectmoretraces) 
then   569         print *,
'WARNING: there were still some traces expected'   570         print *,
'WARNING: last trace of output file may be not'   571         print *,
'         marked correctly'   576    99 stop 
'ERROR: opening control file\n'   577    98 stop 
'ERROR: reading control file\n'   578    97 stop 
'ERROR: closing control file\n'   579    96 stop 
'ERROR: unexpected end of control file\n'   580    95 stop 
'ERROR: opening output file\n'   581    94 stop 
'ERROR: writing output file\n'   582    93 stop 
'ERROR: closing output file\n'   583    92 stop 
'ERROR: opening input file\n'   584    91 stop 
'ERROR: closing input file\n'