stufi.f 21.2 KB
Newer Older
1 2 3 4 5
c this is <stufi.f> by Thomas Forbriger 1996
c
c this is a special version of SEIFE by E. Wielandt
c for input and output fo sff format files 
c 
6 7 8
c Copyright 1996, 2010 by Thomas Forbriger
c
c ----
thomas.forbriger's avatar
thomas.forbriger committed
9
c STUFI is free software; you can redistribute it and/or modify
10 11 12 13 14 15 16 17 18 19 20 21 22 23
c it under the terms of the GNU General Public License as published by
c the Free Software Foundation; either version 2 of the License, or
c (at your option) any later version. 
c 
c This program is distributed in the hope that it will be useful,
c but WITHOUT ANY WARRANTY; without even the implied warranty of
c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c GNU General Public License for more details.
c 
c You should have received a copy of the GNU General Public License
c along with this program; if not, write to the Free Software
c Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
c ----
c
24 25 26 27 28 29 30 31 32 33 34 35 36 37
c (sff is the Stuttgart File Format as defined in sff.doc)
c
c REVISIONS and CHANGES
c V1.0   11/11/96   first running version
c V1.1   13/12/96   set correct file status on open action
c V1.2   14/12/96   - added overwrite option
c                   - and fixed some decleration bugs (not serious)
c                   - fixed trace number output decimals
c                   - fixed more traces estimation when not using selection    
c V1.3   21/01/97   correct array-dimension to sff_RData in libstuff V1.07
c V1.4   10/02/97   changed line splitting for seife commands
c V1.5   15/07/98   * give usage information only without arguments
c                   * introduced verbose mode
c V1.6   21/02/99   changed tflib calls to tf_
thomas.forbriger's avatar
thomas.forbriger committed
38
c V1.7   09/02/00   allow longer path names
39
c V1.8   15/04/05   catch sample rates that do not fit on the output format
40
c V1.9   08/10/10   code relied on Fortran to initialize variables
41 42
c
c======================================================================
43
      program stufi
44
      character*77 version, creator
45
      parameter(version=
46
     &  'STUFI V1.9 E. Wielandts filter routines for sff files')
47 48 49 50 51
      parameter(creator='1996 by Thomas Forbriger (IfG Stuttgart)')
c dimensions
      integer maxcontrol, maxsamples, maxfree, maxselect
      parameter(maxcontrol=50, maxsamples=500000, maxfree=400)
      parameter(maxselect=200)
thomas.forbriger's avatar
thomas.forbriger committed
52
      character*200 junkfile
53 54 55
      parameter (junkfile='stufijunkforreplace')
c common definitions
      integer filep,trace,i,j
thomas.forbriger's avatar
thomas.forbriger committed
56
      character*200 line
57 58
      logical debug
c control file
thomas.forbriger's avatar
thomas.forbriger committed
59
      character*200 command(maxcontrol)
60 61 62
      integer ncommand
c sff specific
      character*20 code, outcode
thomas.forbriger's avatar
thomas.forbriger committed
63
      integer sffversion, ierr, nfilefree, ntracefree,flmax
64 65
      character*13 timestamp
      character*80 filefree(maxfree), tracefree(maxfree)
thomas.forbriger's avatar
thomas.forbriger committed
66
      character*200 infile
67 68 69
      character*132 wid2line
      character soutyp*20, soucs*1, soudate*6, soutime*10
      real souc1, souc2, souc3, tanf, ampfac
70
      double precision srat
71 72 73 74 75 76 77 78 79 80 81 82 83 84
      integer idata(maxsamples)
      logical moretraces, expectmoretraces
      integer hour, minute
      real second
      real tracec1, tracec2, tracec3
      character tracecs*1
      integer tracenstack
c using selections
      logical useselect
      logical selection(maxselect)
c commandline
      integer maxopt, lastarg, iargc
      parameter(maxopt=6)
      character*2 optid(maxopt)
thomas.forbriger's avatar
thomas.forbriger committed
85
      character*80 optarg(maxopt)
86 87 88 89 90
      logical optset(maxopt), opthasarg(maxopt)
      character*20 extension
      character*80 outfile, controlfile
      logical setext, replace, newfile, overwrite, verbose
c seife-sepcific variables
thomas.forbriger's avatar
thomas.forbriger committed
91
      double precision x(maxsamples)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      character typ*3, par*80, msg*79
      integer nsamples
      real dt, tmin, tsec
c here are the keys to our commandline options
      data optid/2h-e,2h-r,2h-t,2h-d,2h-o,2h-v/
      data opthasarg/.TRUE.,.FALSE.,.TRUE.,3*.FALSE./
      data optarg/4h.sfi,1h-,4hjunk,3*1h-/
      
c----------------------------------------------------------------------
c 
c go on with some basic information
c
      controlfile=' '
      if (iargc().eq.1) call getarg(1, controlfile)
c give control-file information
      if ((controlfile(1:5).eq.'-help').or.(iargc().lt.1)) then
        print *,version
        print *,' ',creator
        print *,'Usage: stufi controlfile [-e ext | -r | -t file] [-o] [-v]'
        print *,'             file [t:list] ...'
        print *,'or:    stufi -help'
        if (iargc().lt.1) stop 'ERROR: missing arguments\n'
        print *,' '
        print *,'controlfile  Contains a sequence of seife commands'
        print *,'             that will be executed on every'
        print *,'             selected dataset. See below for'
        print *,'             available seife commands. A maximum'
        print *,'             of ',maxcontrol,' lines is allowed.'
        print *,'-e ext       Write results to a file with the same'
        print *,'             name as the original dataset but with'
        print *,'             filename extension ext. The resluting'
        print *,'             file will contain all original data'
        print *,'             traces (the ones not selected will be'
        print *,'             unchanged. the default extension is:'
        print *,'             .sfi'
        print *,'-r           Replace the original datasets with'
        print *,'             the results of the filter operation.'
        print *,'-t file      Write all results to one file with'
        print *,'             the given name. This file will contain'
        print *,'             only the selected traces. Any file'
        print *,'             related FREE blocks or source'
        print *,'             definitions will be omitted.'
        print *,'-o           Overwrite existing file (is default together'
        print *,'             with -r option.'
        print *,'-v           be verbose'
137
        print *,'-d           produce debug output'
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        print *,' '
        print *,'Each datafile name may be followed by a list of'
        print *,'traces. This list selects a range of traces in'
        print *,'the file which will be processed. This list may'
        print *,'contain no blank (which is the separator to the'
        print *,'next filname). The traces will always be processed'
        print *,'in the order they appear in the data file.'
        print *,' '
        print *,'Examples:'
        print *,'  t:2           will select only trace 2'
        print *,'  t:4-6,2,4     will select traces 2, 4, 5 and 6'
        print *,'  t:9,8,10,14   will select traces 8, 9, 10 and 14'
        print *,' '
        print *,'The message returned by each seife command will be'
        print *,'appended to the FREE block of each trace.'
        print *,' '
c----------------------------------------------------------------------
        print *,'Some comments that come with the underlying library:'
        print *,''
        typ='hel'
        par=''
        nsamples=1
        dt=1.
        call seife(typ,par,nsamples,dt,tmin,tsec,x,msg)
        print *,' '
        print *,'This program is compiled for:'
        print *,'                maximum number of samples: ',
     &          maxsamples
        print *,'    maximum number of lines in FREE block: ',
     &          maxfree
        print *,'  maximum number of lines in control file: ',
     &          maxcontrol
        print *,'  maximum number of traces to be selected: ',
     &          maxselect
        stop
      endif
c----------------------------------------------------------------------
c
c extract information from command line
c
      extension='.sfi'
      outfile='junk'
      setext=.true.
      replace=.false.
      newfile=.false.
      overwrite=.false.
c first read the commandline
      call tf_cmdline(2, lastarg,
     &     maxopt, optid, optarg, optset, opthasarg)
      if (optset(1)) then
        setext=.true.
        replace=.false.
        newfile=.false.
        extension=optarg(1)
      endif
      if (optset(2)) then
        setext=.false.
        replace=.true.
        newfile=.false.
      endif
      if (optset(3)) then
        setext=.false.
        replace=.false.
        newfile=.true.
        outfile=optarg(3)
      endif
      debug=optset(4)
      overwrite=optset(5)
      verbose=optset(6)
      if (debug) print *,'DEBUG: debug messages are switched on'
      lastarg=lastarg+1
c get name of commandfile
      call getarg(1, controlfile)
c check for datafiles
      if (iargc().lt.lastarg) stop 'ERROR: missing data file\n'
c----------------------------------------------------------------------
214 215 216 217 218 219
c initialize counters
      ntracefree=0
      nfilefree=0
      ncommand=0
      tracenstack=0
c----------------------------------------------------------------------
220 221 222
c 
c read in control file
c
thomas.forbriger's avatar
thomas.forbriger committed
223
      if (debug) print *,'DEBUG: read control file'
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      ncommand=0
      open(10, file=controlfile, err=99, status='old')
    1 continue
        read(10, '(a)', err=98, end=96) line
        ncommand=ncommand+1
        command(ncommand)=line
        if (line(1:3).ne.'end') goto 1
      ncommand=ncommand-1
      if (ncommand.lt.1) stop 'ERROR: missing commands\n'
      close(10, err=97)  
c----------------------------------------------------------------------
c
c open output file if necessary
c
      if (newfile) then
        if (verbose) print 80,'writing to file ',outfile
        if (overwrite) then
          call sff_New(10, outfile, ierr)
          if (ierr.ne.0) stop 'ERROR: deleting file'
        endif
        open(10, file=outfile, err=95, status='new')
        code='F'
        call sff_WStatus(10, code)
        write(filefree(1), '(a)') version
        write(filefree(2), '(a)') 
     &  'collecting all results in one file'
        write(filefree(3), '(a)') 
     &  'file related FREE blocks and source informations are ignored'
        nfilefree=3
        call sff_WFree(10, nfilefree, filefree)
      endif
c----------------------------------------------------------------------
c 
c go through all data files
c
      filep=lastarg
thomas.forbriger's avatar
thomas.forbriger committed
260
      if (debug) print *,'DEBUG: enter main loop'
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    2 continue
c 
c get file names
c
        call getarg(filep, infile) 
        if (verbose) then
          print *,' '
          print *,' '
          print 80,'reading new file ',infile
        endif
        if (setext) then
          outfile=infile
          call tf_nameext(outfile, extension)
          if (verbose) print 80,'writing to file ',outfile
        elseif (replace) then
          outfile=junkfile
          if (verbose) print *,'replacing input file'
        endif
c 
c open files
c
        open(11, file=infile, err=92, status='old')
        if (.not.(newfile)) then
          if (overwrite) then
            call sff_New(10, outfile, ierr)
            if (ierr.ne.0) stop 'ERROR: deleting file'
          endif
          open(10, file=outfile, err=95, status='new')
        endif
c 
c do selections
c
        call getarg(filep+1, line)
        if (line(1:2).eq.'t:') then
          filep=filep+1
          useselect=.true.
          call tf_listselect(maxselect, selection, 3, line, ierr)
          if (ierr.eq.1) then
            print *,'WARNING: selection exceeds possible range',
     &              ' from 1 to',maxselect
            print *,'         selecting only up to no.',maxselect
          elseif (ierr.eq.2) then
            print *,'WARNING: missing selection list - selecting ',
     &              'all traces'
            useselect=.false.
          elseif (ierr.ne.0) then
            print *,'WARNING: unknown error code by tf_listselect'
            print *,'         selecting all traces'
            useselect=.false.
          endif
        else
          useselect=.false.
        endif
c
c go through file
c
c +++++++++++
c read file header
c
        call sff_RStatus(11,sffversion,timestamp,code,ierr)
        if (debug) print *,'DEBUG: read status'
        if (ierr.ne.0) stop 'ERROR: reading status of input file\n'
        if (.not.(newfile)) then
          call sff_WStatus(10, code)
          if (debug) print *,'DEBUG: wrote status'
        endif
        i=1
   10   if (code(i:i).ne.' ') then
thomas.forbriger's avatar
thomas.forbriger committed
329
          if (debug) print *,'DEBUG: code: ',code(i:i)
330 331
          if (code(i:i).eq.'F') then
            if (newfile) then
thomas.forbriger's avatar
thomas.forbriger committed
332 333
              if (verbose) 
     &          print *,'MESSAGE: skipping FREE block of input file'
334 335 336 337
              call sff_SkipFree(11, ierr)
              if (debug) print *,'DEBUG: skipped FREE block of file'
              if (ierr.ne.0) stop 'ERROR: skipping FREE block\n'
            else
thomas.forbriger's avatar
thomas.forbriger committed
338
              if (debug) print *,'DEBUG: read FREE block'
339
              call sff_RFree(11, nfilefree, filefree, 
thomas.forbriger's avatar
thomas.forbriger committed
340
     &                       flmax, maxfree, ierr)
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
              if (debug) print *,'DEBUG: read FREE block of file'
              if (ierr.ne.0) stop 'ERROR: reading FREE block\n'
              call sff_WFree(10, nfilefree, filefree)
              if (debug) print *,'DEBUG: wrote FREE block of file'
            endif
          elseif (code(i:i).eq.'S') then
            call sff_RSource(11, soutyp, soucs, souc1, souc2, souc3,
     &                       soudate, soutime, ierr)
            if (debug) print *,'DEBUG: read SOURCE line of file'
            if (ierr.ne.0) stop 'ERROR: reading SOURCE line\n'
            if (newfile) then
              if (verbose) print *,'MESSAGE skipping SOURCE line'
            else
              call sff_WSource(10, soutyp, soucs, souc1, souc2,
     &                         souc3, soudate, soutime)
              if (debug) print *,'DEBUG: wrote SOURCE line of file'
            endif
          endif
          i=i+1
          goto 10
        endif
c +++++++++++
c go through traces
c 
        trace=0
   20   trace=trace+1
          if (DEBUG) print *,'DEBUG: next trace:',trace
          moretraces=.false.
          if (verbose) then
            print *,' '
            print 81,'trace no.',trace,':'
          endif
          nsamples=maxsamples
          call sff_RData(11, wid2line, nsamples, tanf, dt,
     &                   idata, ampfac, code, ierr)
          if (nsamples.gt.maxsamples) 
     &      stop 'ERROR: too many samples\n'
          if (debug) print *,'DEBUG: read data'
          if (ierr.ne.0) stop 'ERROR: reading trace' 
          i=1
   21     if (code(i:i).ne.' ') then
            if (code(i:i).eq.'F') then
              call sff_RFree(11, ntracefree, tracefree, 
thomas.forbriger's avatar
thomas.forbriger committed
384
     &                       flmax, maxfree, ierr)
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
              if (debug) print *,'DEBUG: read trace FREE block'
              if (ierr.ne.0) stop 'ERROR: reading FREE block\n'
            elseif (code(i:i).eq.'I') then
              call sff_RInfo(11, tracecs, tracec1, tracec2, tracec3,
     &                       tracenstack, ierr)
              if (debug) print *,'DEBUG: read trace INFO line'
              if (ierr.ne.0) stop 'ERROR: reading INFO line\n'
            elseif (code(i:i).eq.'D') then
              moretraces=.true.
            endif
            i=i+1
            goto 21
          endif
c ++++++++++++
c do seife
c
          if (newfile.and.useselect.and.(.not.selection(trace))) then
            if (verbose) print *,'MESSAGE skipping trace'
          else
            if ((.not.useselect).or.selection(trace)) then
c ++++++++++++
c watch out for more traces
c (in case of extracting only selected traces we have to estimate
c  whether there will be one more trace ore not)
c
              expectmoretraces=.false.
              if (newfile) then
                if (debug) print *,'DEBUG: looking for more traces'
                outcode=' '
                if (useselect) then
                  do i=trace+1,maxselect
                    if (selection(i)) expectmoretraces=.true.
                  enddo
                else
                  expectmoretraces=moretraces
                endif
                if ((debug).and.(expectmoretraces))
     &            print *,'DEBUG: more traces selected'
                if (iargc().gt.filep) then
                  expectmoretraces=.true.
                  if (debug) print *,'DEBUG: more files selected'
                endif
                i=1
                j=1
   23           if (code(i:i).ne.' ') then
                  if (code(i:i).eq.'F') then
                    outcode(j:j)='F'
                    j=j+1
                  endif
                  if (code(i:i).eq.'I') then
                    outcode(j:j)='I'
                    j=j+1
                  endif
                  i=i+1
                  goto 23
                endif
                if (expectmoretraces) outcode(j:j)='D'
                if (debug) print *,
     &            'DEBUG: code for this trace was: >',code,'<'
                code=outcode
                if (debug) print *,
     &            'DEBUG: code for this trace is: >',code,'<'
              endif
c
c  BEGIN
c  SEIFE BLOCK
c
c
c prepare data for seife
              if (debug) print *,'DEBUG: entering seife'
455
              if (debug) print *,'DEBUG: convert int to double'
456 457 458 459 460 461 462 463 464 465 466 467 468 469
              call tf_inttodouble(nsamples, maxsamples, 
     &                          idata, x, ampfac)
              tmin=float(int(tanf/60.))
              tsec=tanf-(60.*tmin)
c call seife
              do i=1,ncommand
                line=command(i)
                typ=line(1:3)
                par=line(4:)
c                read(line(1:3), '(a3)') typ
c                read(line(6:), '(a75)') par
                if (debug) print *,'DEBUG: +',typ,'++',par,'+'
                call seife(typ, par, nsamples, dt, tmin, tsec,
     &                     x, msg)
470
                if (debug) print *,'DEBUG: returned from seife: ',msg
471
                if (ntracefree.lt.maxfree) then
472 473 474
                  if (debug) print *,'DEBUG: write to FREE block'
                  if (debug) print *,'DEBUG: ',ntracefree
                  if (debug) print *,'DEBUG: ',maxfree
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
                  ntracefree=ntracefree+1
                  write(tracefree(ntracefree), '(a)') msg
                else
                  print *,
     &             'WARNING: reached maximum length of FREE block'
                endif
                if (verbose) print *,msg
              enddo
c prepare seife data to be written in sff format
c to WID2: nsamples, dt, tmin, tsec
              hour=int(tmin/60.)
              if ((hour.gt.23).or.(hour.lt.0)) then
                print *,'WARNING: time of first sample out of range'
                print *,'WARNING: time of first sample set to zero'
                hour=0
                minute=0
                second=0.
              else
                minute=int(tmin)-hour*60
                if (minute.lt.0) then
                  print *,'WARNING: time of first sample out of range'
                  print *,'WARNING: time of first sample set to zero'
                  hour=0
                  minute=0
                  second=0.
                else
                  second=tsec
                endif
              endif
              write(wid2line(17:28), '(i2,a1,i2,a1,f6.3)')
     &          hour,':',minute,':',second
506 507 508 509 510 511
              srat=1./dt
              if ((srat.lt.1.e-4).or.(srat.gt.100.)) then
                write(wid2line(58:68), '(e11.6)') 1./dt
              else
                write(wid2line(58:68), '(f11.6)') 1./dt
              endif
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
              write(wid2line(49:56), '(i8)') nsamples
              call tf_doubletoint(nsamples, maxsamples, 
     &                          idata, x, ampfac)
              if (debug) print *,'DEBUG: exiting seife'
c
c
c  SEIFE BLOCK
c  END
c

            endif
c ++++++++++++
c write data
c
            if (debug) print *,'DEBUG: ',wid2line
            if (debug) print *,'DEBUG: nsamples ',nsamples
            if (debug) print *,'DEBUG: ampfac ',ampfac
            if (debug) print *,'DEBUG: code ',code
            call sff_WData(10, wid2line, nsamples, idata,
     &                     ampfac, code)
            if (debug) print *,'DEBUG: wrote data'
            i=1
   22       if (code(i:i).ne.' ') then
              if (code(i:i).eq.'F') then
                call sff_WFree(10, ntracefree, tracefree)
                if (debug) print *,'DEBUG: wrote FREE block of trace'
              elseif (code(i:i).eq.'I') then
                call sff_WInfo(10, tracecs, tracec1, tracec2, tracec3,
     &                         tracenstack)
                if (debug) print *,'DEBUG: wrote INFO line of trace'
              endif
              i=i+1
              goto 22
            endif
          endif
        if (moretraces) goto 20
c close files
c
        if (.not.(newfile)) then
          close(10, err=93)
        endif
        close(11, err=91)
c
c do replace
c
        if (replace) then
          call system('/bin/mv '//junkfile//' '//infile)
          if (debug) print *,'DEBUG: /bin/mv '//junkfile//' '//infile
        endif
        filep=filep+1
        if (filep.le.iargc()) goto 2
c----------------------------------------------------------------------
c
c close output file if necessary
c
      if (newfile) close(10, err=93)
      if (expectmoretraces) then
        print *,'WARNING: there were still some traces expected'
        print *,'WARNING: last trace of output file may be not'
        print *,'         marked correctly'
      endif
      stop
   80 format(a,a)
   81 format(a,i3,a)
   99 stop 'ERROR: opening control file\n'
   98 stop 'ERROR: reading control file\n'
   97 stop 'ERROR: closing control file\n'
   96 stop 'ERROR: unexpected end of control file\n'
   95 stop 'ERROR: opening output file\n'
   94 stop 'ERROR: writing output file\n'
   93 stop 'ERROR: closing output file\n'
   92 stop 'ERROR: opening input file\n'
   91 stop 'ERROR: closing input file\n'
      end