greda.f 75.8 KB
Newer Older
1
2
c this is <greda.f>
c------------------------------------------------------------------------------
thomas.forbriger's avatar
thomas.forbriger committed
3
c $Id: greda.f,v 1.23 2010-03-10 17:31:35 tforb Exp $
4
5
6
7
8
c
c 24/06/97 by Thomas Forbriger (IfG Stuttgart)
c
c evaluate greens function for a set of seismograms
c
thomas.forbriger's avatar
thomas.forbriger committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
c ----
c This program is free software; you can redistribute it and/or modify
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
25
26
27
28
c REVISIONS and CHANGES
c    24/06/97   V1.0   Thomas Forbriger
c    03/07/97   V1.1   included hankel functions
c    08/07/97   V1.2   correct 1/2 factors for hankel functions
thomas.forbriger's avatar
thomas.forbriger committed
29
c    25/07/97   V1.3   calculate Fourier Bessel transform
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
c                      first slowness to be calculated is larger than zero
c                      included distance taper
c                      included special handling for u=zero
c    04/08/97   V1.4   now optional matrix inversion method
c                      and correct linear inversion
c    07/08/97   V1.5   build a correct algorithm using wielandt gauss
c    08/08/97   V2.0   reorganized the whole thing
c                      added edge setting
c    14/08/97   V2.1   added direct matrix method
c                      added K_0 damping method
c                      added discrete gram method
c    22/10/97   Vx.x   special version using imsl
c                      change matrix routine and L-rho
c    23/10/97   V2.2   - use double precision (real) matrix methods
c                      - introduced a workspace common block
c    29/10/97   V2.3   - introduced definition for damping factor
c                        relativ to omega, r_max and s_max
c    30/10/97   V2.4   - all methods should be correct now
c    31/10/97   V2.5   - now with gaussian distance taper
c                      - and gaussian time domain taper
c    06/11/97   V2.6   - starting work on Henry, Orcutt Parker method
c                      - fix problem with frequency=zero
c    12/01/98   V2.7   - fix use of minr (to be able to more than
c                        on receiver at the same offset)
c                      - rearrange help and add some more help
c                      - new option -Q
c                      - stacking of seismograms with same offset
c                      - allow different numbers of samples
c    14/01/98   V2.8   - be more verbose when stacking
c    08/06/98   V3.0   - added the plane wave stacking algorithm
c                        new major version as option naming changed
c    12/08/98   V3.1   - ok there was false information in the help text...
c                        it was a misuse of -P option
c    18/08/98   V3.2   - correct stacking algorithm 
c                        did refer to r(i) and not to r(i)/n
c    28/02/99   V3.3   - allow negative slowness values when stacking plane
c                        waves
c                      - changed order of if,elseif,.. section setting
c                        planewaves to the front
c    02/03/99   V3.4   - there was a servere bug in the spectra calculation
c                        routine - the transform array was not initialized
c                        correctly
c    13/05/99   V3.5   - allow scaling of individual spectral coefficients
73
74
75
76
c    20/04/00   V3.6   - there was an error in trace sorting in subroutine
c                        parker which should have lead to wrong results with
c                        non-sorted offset data
c                      - and gram(ntr,ntr) was zero
thomas.forbriger's avatar
thomas.forbriger committed
77
c    27/04/00   V3.7   - report HOP expansion numbers
thomas.forbriger's avatar
thomas.forbriger committed
78
c                      - scale Fourier Bessel transform with HOP factors
79
80
c    29/04/00   V3.8   - hooo - there was still an error in the HOP gram
c                        inverse expansion to alpha
81
82
c    24/05/00   V3.9   - changed cosine distance taper to apply factor greater
c                        than zero to last trace
83
84
c    21/06/00   V3.10  the trapezoid rule in subroutine backcoeff was awfully
c                      misbehaved
85
c    23/06/00   V3.11  trapezoid rule was still wrong by a constant factor 2
86
87
88
89
90
91
92
c    12/06/02   V3.12  two years later :-)
c                      Introduce a "number of wavelength" taper (option -W).
c                      This functionality is implemented into the subroutines
c                      planestack for the Slant Stack algorithm and into
c                      subroutine expmodel. In expmodel it makes only sense
c                      together with the Bessel transform, since only in that
c                      case the inverse of the Gram matrix is diagonal.
93
94
c    13/09/02   V3.13  - add extra offset taper
c                      - output seismogram spectra
thomas.forbriger's avatar
thomas.forbriger committed
95
c    16/09/02   V3.14  - write phasor walkout to file
thomas.forbriger's avatar
thomas.forbriger committed
96
97
98
c    28/03/06   V3.15  - apply special (offset dependent taper) after
c                        rescaling waveforms
c                      - corrected subroutine specialtap
99
c    27/11/09   V3.16  - some corrections to satify gfortran
thomas.forbriger's avatar
thomas.forbriger committed
100
c               V3.17  - pwo_init takes an argument!
thomas.forbriger's avatar
thomas.forbriger committed
101
102
c    10/03/10   V3.18  - correction in taper function: second index of array
c                        spectra is sample index
103
104
105
106
107
108
109
c
c==============================================================================
c
      program greda
c
c first we declare some general variables
c
thomas.forbriger's avatar
thomas.forbriger committed
110
      character*79 version,CVSID
thomas.forbriger's avatar
thomas.forbriger committed
111
      parameter(version='GREDA   V3.18   Greens function from data')
thomas.forbriger's avatar
thomas.forbriger committed
112
      parameter(CVSID=
thomas.forbriger's avatar
thomas.forbriger committed
113
     &  '$Id: greda.f,v 1.23 2010-03-10 17:31:35 tforb Exp $')
114
115
c
c calculations common block
116
      include 'greda_dim.inc'
117
118
119
120
121
122
123
124
125
126
      include 'greda.inc'
c any
      integer i,j
      real pi2
      parameter(pi2=2.*3.141592653589793)
c 
c----------------------------------------------------------------------
c here follows everything we need for the input data hold
c
c datafile
thomas.forbriger's avatar
thomas.forbriger committed
127
      character*80 filename, Fourierfile
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
      real fdata(maxsamp)
      integer idata(maxsamp)
      equivalence(fdata, idata)
      real r(maxtr), dt, tfirst, maxr, minr
c receiver chain
      integer chain(maxtr), firstrec
c 
c----------------------------------------------------------------------
c here is everything we need to perform the calculations
c
c inner product damping exponents
      real rho, rhoq, sigma, expon
c 
c----------------------------------------------------------------------
c here follows what we need to hold and write the output data
c
c magic number for binary file identification
      integer magic
      character*4 cmagic
      parameter(cmagic='1234')
148
149
150
151
c magic number for trace spectra binary file identification
      integer spmagic
      character*4 cspmagic
      parameter(cspmagic='SP34')
152
153
154
155
156
157
158
159
160
c greens function
      character*80 greensfile
      complex green(maxslo, maxom)
      real slo(maxslo), om(maxom), omq
      real smax, fmax
c file
      integer lu
      parameter(lu=20)
c 
thomas.forbriger's avatar
thomas.forbriger committed
161
162
163
164
c selected method
      character*70 method
      character*2 kernel
c 
165
166
167
168
169
c----------------------------------------------------------------------
c here we go with command line parameters
c
c parameters
      real tapfrac, offtapfrac, edgefrac, minoff, stackdelta, rescaleexpo
170
      real hopnumberfrequency, wltaplen, wltapfrac
171
      real tapoffsets(4)
thomas.forbriger's avatar
thomas.forbriger committed
172
173
      real pwofreq, pwoslo
      character*(80) pwofilename, pwffilename, pwafilename
thomas.forbriger's avatar
thomas.forbriger committed
174
      logical hopnumberthis, backtranscale
175
176
177
      logical debug, overwrite, hankel1, hankel2, verbose, uzerospecial
      logical matrixmethod, lininv, offtaper, edgeset, linkinv
      logical disgram, softcosine, gausstaper, gausstime, parkermethod
thomas.forbriger's avatar
thomas.forbriger committed
178
      logical stackso, planewave, rescale, specrescale, hopnumbers
thomas.forbriger's avatar
thomas.forbriger committed
179
      logical applywltaper,specialtaper,writeFourier
thomas.forbriger's avatar
thomas.forbriger committed
180
      logical pwofile, pwoautofile, pwosel
181
182
c command line
      integer maxopt, lastarg, iargc
thomas.forbriger's avatar
thomas.forbriger committed
183
      parameter(maxopt=35)
184
185
      character*4 optid(maxopt)
      character*80 optarg(maxopt)
186
187
188
189
      logical optset(maxopt), opthasarg(maxopt)
c here are the keys to our commandline options
      data optid/2h-d,2h-t,2h-s,2h-n,2h-f,2h-o,2h-1,2h-2,2h-v,2h-T,2h-S,
     &  2h-M,2h-L,2h-q,2h-E,2h-K,2h-D,2h-a,2h-b,2h-g,2h-H,2h-O,2h-Q,2h-B,
thomas.forbriger's avatar
thomas.forbriger committed
190
     &  2h-P,2h-r,2h-F,2h-N,2h-X,2h-W,4h-tap,4h-spo,3h-pw,4h-pwf,4h-pwa/
191
      data opthasarg/.FALSE.,4*.TRUE.,4*.FALSE.,.TRUE.,3*.FALSE.,2*.TRUE.,
192
     &  6*.FALSE.,2*.TRUE.,.TRUE.,.FALSE.,.TRUE.,.FALSE.,.TRUE.,.FALSE.,
thomas.forbriger's avatar
thomas.forbriger committed
193
     &  6*.TRUE./
194
      data optarg/1h-,3h10.,2h8.,1h5,4h100.,4*1h-,2h0.,3*1h-,5h1.,1.,
195
     &  2h1.,6*1h-,4h0.01,5h1.,1.,4*1h-,3h10.,1h-,6h1.,10.,
thomas.forbriger's avatar
thomas.forbriger committed
196
     &  11h0.,0.,0.,0.,4*3hnil/
197
198
199
200
201
202
c 
c======================================================================
c 
c go on with executable code
c
      print *,version
203
204
      print *,'Usage: greda datafile greensfile'
      print *,'             [-L] [-K] [-P] [-D] [-M] [-H]'
205
      print *,'             [-n nslo] [-s smax] [-f fmax]'
206
207
      print *,'             [-t frac] [-T frac] [-E edge]'
      print *,'             [-W n,f] [-a] [-b] [-g]'
208
      print *,'             [-O minoff] [-B delta] [-r expo] [-F]'
209
210
      print *,'             [-1] [-2] [-q f,e] [-Q f,e] [-S]'
      print *,'             [-v] [-o] [-N f] [-X]'
211
212
      print *,'             [-tap o1,o2,o3,o4]'
      print *,'             [-spo filename]'
thomas.forbriger's avatar
thomas.forbriger committed
213
214
215
      print *,'             [-pw f,p,file]'
      print *,'             [-pwf filename]'
      print *,'             [-pwa filename]'
216
217
218
219
220
221
222
223
      print *,'or     greda -help'
c 
      if (iargc().lt.1) stop 'ERROR: missing parameters'
      call getarg(1, filename)
      if (filename(1:5).eq.'-help') then
        print *,' '
        print *,'Calculate Green''s Function from a set of seismograms'
        print *,'by using linear inversion theory. Actually does'
thomas.forbriger's avatar
thomas.forbriger committed
224
        print *,'a Fourier-Bessel transform by default.'
225
        print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
226
227
228
        print *,CVSID
        print *,'Copyright 1997 by Thomas Forbriger'
        print *,' '
229
230
231
232
233
234
235
        print *,'datafile     Name of file containing seismograms.'
        print *,'             The program will expect a homogeneous dataset.'
        print *,'             This means a dataset where all traces have'
        print *,'             the same time of first sample and the same'
        print *,'             sampling interval.'
        print *,'greensfile   Name of file to contain results.'
        print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
236
        print *,'Available alternatives to the Fourier-Bessel transform:'
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
        print *,'-L           Calculate by linear inversion (exp-damping).'
        print *,'-K           Calculate by linear inversion (K_0-damping).'
        print *,'-H           Use the method following Henry, Orcutt and Parker:'
        print *,'             Calculate by linear inversion (Lorentz-damping).'
        print *,'             This uses a special integral formula that'
        print *,'             leads to a direct matrix inversion.'
        print *,'             The theory is given in:'
        print *,'               Henry, M., Orcutt, J.A., Parker, R.L., 1980,'
        print *,'               A new method for slant stacking refraction data'
        print *,'               Geoph. Res. Lett., vol. 7, 1073-1076'
        print *,'-D           Calculate by linear inversion (gram matrix'
        print *,'             calculated by discrete numerical integration).'
        print *,'-M           Calculate Green''s matrix by simple matrix'
        print *,'             inversion. (This is a DIRTY way - please don''t'
        print *,'             take it!)'
        print *,'-P           Assume that data represents plane waves. Solve'
        print *,'             by ordinary stacking method (well known as'
        print *,'             ''slant stack'').'
        print *,' '
        print *,'How to define the range in the green:'
        print *,'-n nslo      Number of slowness to be scanned from'
        print *,'             greens function. (default is number of'
        print *,'             seismic traces read)'
        print *,'-s smax      Maximum slowness value for scanning the'
        print *,'             greens function in s/km. (default is ',
     &          optarg(3)(1:4),')'
        print *,'-f fmax      Maximum frequency value in Hz. (default'
        print *,'             is to take all theoretical frequencies)'
        print *,' '
        print *,'How to apply tapers to the input data:'
        print *,'-t tfrac     Tapering fraction for each end of the time'
        print *,'             series in percent. (default is ',
     &          optarg(2)(1:4),')'
        print *,'             In the case of a gaussian taper this value gives'
        print *,'             the amplitude fraction of the edge samples'
        print *,'             compared to it''s original values.'
        print *,'-T ofrac     Tapering (cosine taper) fraction for the far end'
        print *,'             of the profile in percent.'
        print *,'             In the case of a gaussian taper this value gives'
        print *,'             the amplitude fraction of the last trace'
        print *,'             compared to it''s original value.'
        print *,'             The default is not to apply any offset domain'
        print *,'             taper.'
280
281
282
283
284
285
286
287
288
        print *,'-tap o1,o2,o3,o4'
        print *,'             special offset taper'
        print *,'             the taper is 0. for offsets smaller than'
        print *,'               o1 and offsets larger than o4'
        print *,'             the taper i 1. for offsets between o2'
        print *,'               and o3'
        print *,'             it has a sine edge between o1 and'
        print *,'               and o2 and a cosine edge between o3'
        print *,'               and o4'
289
290
291
292
293
294
295
296
297
298
        print *,'-E edge      Specify the sample from which on all'
        print *,'             samples should be tapered to zero as a'
        print *,'             fraction of the time series length.'
        print *,'             (2.*tfrac <= edge <= 1.)'
        print *,'-a           Apply smooth cosine distance taper ',
     &            '(default is hard).'
        print *,'-b           Apply gaussian distance taper ',
     &            '(default is hard cosine).'
        print *,'-g           Apply gaussian time domain taper ',
     &            '(default is cosine).'
299
300
301
302
303
304
305
306
307
        print *,'-W n,f       Apply an offset domain cosine taper of'
        print *,'             exactly ''n'' wavelengths length, where'
        print *,'             ''n'' may be a floating point value.'
        print *,'             The taper fraction (falling edge) is'
        print *,'             given by ''f'' in percent of the taper'
        print *,'             length. The taper length depends then on'
        print *,'             frequency and slowness.'
        print *,'             This option applies to only to the Slant'
        print *,'             Stack and the Bessel transformation.'
thomas.forbriger's avatar
thomas.forbriger committed
308
309
310
        print *,'             In terms of slowness resolution ''n'' '
        print *,'             defines it to be'
        print *,'             delta p = p / n.'
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
        print *,'-O minoff    Set minimum offset difference that will be'
        print *,'             used to find the minimum receiver distance.'
        print *,'             That is usefull in cases where multiple'
        print *,'             receivers occur at the same offset position.'
        print *,'             (default is ',
     &          optarg(22)(1:4),')'
        print *,'-B delta     Stack all traces lying within the same offset'
        print *,'             interval delta. We will take the mean offset'
        print *,'             value for the stacked result.'
        print *,'-r expo      Rescale seismograms to energy damping of'
        print *,'             r^(-expo) with r being the offset. For'
        print *,'             surface waves without dissipation expo should'
        print *,'             be one.'
        print *,'-F           Do rescaling in the frequency domain to energy'
        print *,'             damping of (omega*r)^(-expo). In this'
thomas.forbriger's avatar
thomas.forbriger committed
326
        print *,'             case every single Fourier coefficient will'
327
328
329
330
331
332
333
334
335
336
337
338
339
        print *,'             be rescaled for itself. This procedure will'
        print *,'             just leave the phase information. No amplitude'
        print *,'             information will be conserved.'
        print *,' '
        print *,'Parameters controlling expansion of Green''s Function:'
        print *,'-1           Use the hankel function H^(1)_0 instead of J_0'
        print *,'-2           Use the hankel function H^(2)_0 instead of J_0'
        print *,'-q f,e       Damping factor for inner product.'
        print *,'             The inner product damping will be calculated'
        print *,'             as rho=delta_r_min*f*omega**e.'
        print *,'             default is: ',optarg(14)(1:8)
        print *,'-Q f,e       Damping factor for inner product.'
        print *,'             The inner product damping will be calculated'
340
341
342
        print *,'             as rho=f*omega**e. Overrides settings of'
        print *,'             -q option.'
        print *,'             proposed: ',optarg(23)(1:8)
343
344
345
346
        print *,'-S           Special handling of zero slowness. This option'
        print *,'             is useful together with the hankel functions'
        print *,'             which become singular at argument zero.'
        print *,'             To calculate the greens coefficients at'
thomas.forbriger's avatar
thomas.forbriger committed
347
348
        print *,'             slowness zero we use the Bessel function.'
        print *,'-X           scale Fourier Bessel transform with HOP factors'
349
        print *,'             (crazy option!)'
350
        print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
351
352
353
354
355
356
357
358
359
360
361
        print *,'Phasor walkout:'
        print *,'-pw f,p,file write phasor walkout for frequency f and'
        print *,'             slowness p to file'
        print *,'-pwf file    read phasor walkout selection from file'
        print *,'             each line has three entries:'
        print *,'             frequency, slowness, filename'
        print *,'-pwa file    read phasor walkout selection from file'
        print *,'             but generate output filenames automatic'
        print *,'             each line has two entries:'
        print *,'             frequency, slowness'
        print *,' '
362
363
364
        print *,'Other parameters:'
        print *,'-o           Overwrite existing output file.'
        print *,'-v           Be somehow verbose.'
thomas.forbriger's avatar
thomas.forbriger committed
365
        print *,'-N f         print HOP numbers for frequency f'
366
367
368
        print *,'-spo filename'
        print *,'             write seismic traces'' Fourier'
        print *,'             coefficients to file ''filename'' '
369
370
371
372
373
374
375
376
        print *,' '
        print *,'How it works:'
        print *,'In cases -L, -K, -H and -D (linear inversion) we use'
        print *,'a scalar product of the type'
        print *,' '
        print *,'  (f,g) = int_0^P f(p) g(p) D(p) p dp.'
        print *,' '
        print *,'J_0(p*omega*r_j) is called the representer of the data'
thomas.forbriger's avatar
thomas.forbriger committed
377
        print *,'value d_j, which here is the Fourier expansion coefficient'
378
379
380
381
382
383
384
385
386
387
388
389
390
        print *,'for the vartical displacement at frequency omega and'
        print *,'offset r_j.'
        print *,' '
        print *,'In case -D P is equal to the maximum slowness and D(p)=1.'
        print *,'In the other cases P is infinity and D(p) is given by'
        print *,' '
        print *,'  D(p) = exp(-rho^2*p^2),         (case -L)'
        print *,'  D(p) = K_0(rho*p), and          (case -K)'
        print *,'  D(p) = 1/(p^2+1/rho^2).         (case -H)'
        print *,' '
        print *,'rho should be chosen somewhere around delta_r_min*omega,'
        print *,'where delta_r_min is the minimal offset difference.'
        print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
391
        print *,'$Id: greda.f,v 1.23 2010-03-10 17:31:35 tforb Exp $'
thomas.forbriger's avatar
thomas.forbriger committed
392
393
        call pwo_cvsid
        print *,' '
394
395
396
397
398
        print *,'compiled array dimensions are:'
        print *,'      samples: ',maxsamp
        print *,'       traces: ',maxtr
        print *,'   slownesses: ',maxslo
        print *,'  frequencies: ',maxom
399
400
401
402
        print *,' '
        print *,'magic numbers:'
        print *,'  Green coefficient files: ',cmagic
        print *,'  Trace coefficient files: ',cspmagic
403
404
405
        stop
      endif
c 
thomas.forbriger's avatar
thomas.forbriger committed
406
407
408
c
c----------------------------------------------------------------------
c 
409
410
c configure from command line
c 
thomas.forbriger's avatar
thomas.forbriger committed
411
c      print *,'DEBUG: call iargc'
412
      if (iargc().lt.2) stop 'ERROR: missing parameters'
thomas.forbriger's avatar
thomas.forbriger committed
413
c      print *,'DEBUG: call tf_cmdline'
414
415
      call tf_cmdline(3, lastarg,
     &     maxopt, optid, optarg, optset, opthasarg)
thomas.forbriger's avatar
thomas.forbriger committed
416
c      print *,'DEBUG: returned from tf_cmdline'
417
418
419
      call getarg(1, filename)
      call getarg(2, greensfile)
      debug=optset(1)
thomas.forbriger's avatar
thomas.forbriger committed
420
421
      if (debug) print *,'DEBUG: in options - place 1'
      read(optarg(2), *, err=99) tapfrac
422
423
      if (tapfrac.gt.50.) stop 'ERROR: silly taper'
      if (tapfrac.lt.0.) stop 'ERROR: negative taper'
thomas.forbriger's avatar
thomas.forbriger committed
424
      read(optarg(3), *, err=99) smax
425
426
427
428
      smax=smax*0.001
      read(optarg(4), '(i10)', err=99) nslo
      if (nslo.lt.1) stop 'ERROR: need positive number of slownesses'
      if (nslo.gt.maxslo) stop 'ERROR: too many slownesses'
thomas.forbriger's avatar
thomas.forbriger committed
429
      read(optarg(5), *, err=99) fmax
430
431
      if (fmax.lt.0.) stop 'ERROR: negative maximum frequency'
      overwrite=optset(6)
thomas.forbriger's avatar
thomas.forbriger committed
432
      if (debug) print *,'DEBUG: in options - place 2'
433
434
435
436
437
      hankel1=optset(7)
      hankel2=optset(8)
      verbose=optset(9)
      offtaper=optset(10)
      if (offtaper) then
thomas.forbriger's avatar
thomas.forbriger committed
438
        read(optarg(10), *, err=99) offtapfrac
439
440
441
        if (offtapfrac.gt.50.) stop 'ERROR: silly taper'
        if (offtapfrac.lt.0.) stop 'ERROR: negative taper'
      endif
thomas.forbriger's avatar
thomas.forbriger committed
442
      if (debug) print *,'DEBUG: in options - place 3'
443
444
445
      uzerospecial=optset(11)
      matrixmethod=optset(12)
      lininv=optset(13)
thomas.forbriger's avatar
thomas.forbriger committed
446
      read (optarg(14), *) sigma,expon
447
448
      edgeset=optset(15)
      if (edgeset) then
thomas.forbriger's avatar
thomas.forbriger committed
449
        read(optarg(15), *, err=99) edgefrac
450
451
452
453
        if (edgefrac.gt.1.) stop 'ERROR: end of taper behind last sample'
        if (edgefrac.lt.(0.02*tapfrac)) 
     &    stop 'ERROR: edge does not leave enough space for taper'
      endif
thomas.forbriger's avatar
thomas.forbriger committed
454
      if (debug) print *,'DEBUG: in options - place 4'
455
456
457
458
459
460
      linkinv=optset(16)
      disgram=optset(17)
      softcosine=optset(18)
      gausstaper=optset(19)
      gausstime=optset(20)
      parkermethod=optset(21)
thomas.forbriger's avatar
thomas.forbriger committed
461
      read (optarg(22), *) minoff
462
      if ((.not.optset(14)).and.(optset(23)))
thomas.forbriger's avatar
thomas.forbriger committed
463
     &  read (optarg(23), *) sigma,expon
464
      stackso=optset(24)
thomas.forbriger's avatar
thomas.forbriger committed
465
      if (stackso) read (optarg(24), *) stackdelta
466
467
      planewave=optset(25)
      rescale=optset(26)
thomas.forbriger's avatar
thomas.forbriger committed
468
      if (debug) print *,'DEBUG: in options - place 5'
469
470
      specrescale=optset(27)
      if (rescale) then
thomas.forbriger's avatar
thomas.forbriger committed
471
        read(optarg(26), *) rescaleexpo
472
473
474
475
        if (specrescale) rescale=.false.
      else
        specrescale=.false.
      endif
thomas.forbriger's avatar
thomas.forbriger committed
476
477
478
      hopnumbers=optset(28)
      read(optarg(28), *) hopnumberfrequency
      backtranscale=optset(29)
479
480
      applywltaper=optset(30)
      read(optarg(30), *) wltaplen, wltapfrac
thomas.forbriger's avatar
thomas.forbriger committed
481
      if (debug) print *,'DEBUG: in options - place 6'
482
483
      specialtaper=optset(31)
      if (specialtaper) read(optarg(31), *) (tapoffsets(i), i=1,4)
thomas.forbriger's avatar
thomas.forbriger committed
484
485
      writeFourier=optset(32)
      Fourierfile=optarg(32)
thomas.forbriger's avatar
thomas.forbriger committed
486
487
488
489
490
491
      pwosel=optset(33)
      if (pwosel) read(optarg(33), *) pwofreq, pwoslo, pwofilename
      pwofile=optset(34)
      pwffilename=optarg(34)
      pwoautofile=optset(35)
      pwafilename=optarg(35)
thomas.forbriger's avatar
thomas.forbriger committed
492
      if (debug) print *,'DEBUG: read options'
493
c 
494
495
      if ((.not.(planewave)).and.(smax.lt.0.))
     &  stop 'ERROR: negative maximum slowness'
thomas.forbriger's avatar
thomas.forbriger committed
496
497
498
499
500
501
502

c----------------------------------------------------------------------
c initialize common block for phasor walkout
c      print *,'DEBUG: call pwo_init'
      call pwo_init(verbose)
c      print *,'DEBUG: returned from pwo_init'
c----------------------------------------------------------------------
503
504
505
506
c report
      print *,' '
      if (hankel1) then
        print *,'I will use the hankel function H^(1)_0 of order zero'
thomas.forbriger's avatar
thomas.forbriger committed
507
        kernel='H1'
508
509
      elseif (hankel2) then
        print *,'I will use the hankel function H^(2)_0 of order zero'
thomas.forbriger's avatar
thomas.forbriger committed
510
        kernel='H2'
511
      else
thomas.forbriger's avatar
thomas.forbriger committed
512
        print *,'I will use the Bessel function of the first kind J_0',
513
     &    ' of order zero'
thomas.forbriger's avatar
thomas.forbriger committed
514
        kernel='J'
515
516
517
      endif
      print *,' '
c 
518
c initialize wavelength taper factors
thomas.forbriger's avatar
thomas.forbriger committed
519
      if (debug) print *,'DEBUG: call initwltaper(wltaplen,wltapfrac)'
520
      call initwltaper(wltaplen,wltapfrac)
thomas.forbriger's avatar
thomas.forbriger committed
521
522
523
524
525
526
527
528
c
c----------------------------------------------------------------------
c phasor walkout selection
c
      if (pwosel) 
     &  call pwo_selpair(pwofreq*pi2,pwoslo,pwofilename,verbose)
      if (pwofile) call pwo_readsel(pwffilename, .false., verbose)
      if (pwoautofile) call pwo_readsel(pwafilename, .true., verbose)
529
c 
530
531
532
533
534
c----------------------------------------------------------------------
c 
c go for the real calculations
c 
c read seismic data
thomas.forbriger's avatar
thomas.forbriger committed
535
      if (debug) print *,'DEBUG: call readdata'
536
537
      call readdata(filename, fdata, idata, spectra, r, maxr,
     &     maxtr, maxsamp, ntr, nsamp, dt, tfirst)
thomas.forbriger's avatar
thomas.forbriger committed
538
      if (debug) print *,'DEBUG: returned from readdata'
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
c 
      if ((.not.optset(4)).or.(matrixmethod)) nslo=ntr
      if (nslo.gt.maxslo) stop 'ERROR: too many slownesses - check code'
c 
c do stacking
      if (stackso) call stackthem(r, stackdelta, maxr, verbose)
c
c get distances in increasing order
      call tf_rchain(r, chain, ntr, firstrec, 1)
c
c find minimum distance step
      j=firstrec
      i=chain(j)
      minr=maxr
      do while (i.gt.0)
        if (abs(r(i)-r(j)).gt.minoff) minr=min(minr,abs(r(i)-r(j)))
        j=i
        i=chain(i)
      enddo
      print *,' '
      print *,' minimum distance between receivers: ',minr
      print *,'maximum source to receiver distance: ',maxr
c 
      if ((.not.optset(14)).and.(optset(23)))
     &  sigma=sigma/minr
c 
      print *,'rho=',minr*sigma,'*omega**',expon
c 
c apply tapers
      call taper(maxsamp, maxtr, ntr, nsamp, tapfrac, offtapfrac,
     &  maxr, spectra, r, offtaper, verbose, edgeset, edgefrac,
     &  softcosine, gausstaper, gausstime)
thomas.forbriger's avatar
thomas.forbriger committed
571
572
573
c 
c rescale traces if we want them to be rescaled
      if (rescale) call dorescale(rescaleexpo, r)
574
575
576
577
578
c
c apply special tapers
      if (specialtaper) 
     &  call specialtap(maxsamp, maxtr, ntr, nsamp, tapoffsets, maxr,
     &    spectra, r, verbose)
579
580
581
582
c 
c calculate complex spectra
      call calcspec(dt, tfirst, om,
     &  fmax, optset(5))
583
584
c
c write Fourier coefficients
thomas.forbriger's avatar
thomas.forbriger committed
585
586
      if (writeFourier) 
     &  call Fourierwrite(Fourierfile, spmagic, cspmagic, overwrite,
587
     &    r, maxr, om)
588
589
590
591
592
593
594
595
596
597
598
599
600
c 
c rescale traces if we want them to be rescaled
      if (specrescale) call dospecrescale(rescaleexpo, r, om)
c 
c set slowness range
      call setslo(maxslo, nslo, smax, slo, 
     &  hankel1, hankel2, uzerospecial)
c 
c----------------------------------------------------------------------
c 
c now calculate the greens matrix
c handle each frequency individually
c
thomas.forbriger's avatar
thomas.forbriger committed
601
      hopnumberthis=.false.
602
603
      do i=1,nom
c 
thomas.forbriger's avatar
thomas.forbriger committed
604
c init phasor walkout
thomas.forbriger's avatar
thomas.forbriger committed
605
        call pwo_initable(om,slo,nom,nslo,i,verbose)
thomas.forbriger's avatar
thomas.forbriger committed
606
c 
607
608
609
610
611
612
c damping
c
c something usefull
        omq=om(i)*om(i)
        rho=minr*sigma*om(i)**expon
        rhoq=rho*rho
thomas.forbriger's avatar
thomas.forbriger committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
c 
c do we hopnumber? ;-)
        if (hopnumbers) then
          hopnumberthis=.false.
c          print *,abs(hopnumberfrequency-om(i)/pi2)
c          print *,(0.55*(om(2)-om(1))/pi2)
          if (abs(hopnumberfrequency-om(i)/pi2).lt.(0.501*(om(2)-om(1))/pi2)) 
     &    then
            hopnumberthis=.true.
            print *,' '
            print *,'HOP numbers request for ',
     &              hopnumberfrequency,' Hz comes at ',om(i)/pi2,' Hz'
            print *,'rho is ',rho
          endif
        endif
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
c
c algorithm section
c =================
c
c Provided algorithms are:
c
c   1. Slant Stack (assuming plane waves)
c   2. HOP-method (linear inversion with Lorentz-damping)
c   3. linear inversion with exp-damping
c   4. linear inversion with K_0-damping
c   5. linear inversion with boxcar damping
c   6. the dirty direct matrix inversion
c   7. discrete Fourier-Bessel transformation
c
c The following conditionals select one of the provided algorithms.
c The standard sequence of calculations is:
c
c   1. calculate a Gram matrix
c      (subroutines gramex, gramket, gramdis)
c   2. calculate expansion coefficients alpha from Gram matrix
c      by numerical solution of system of linear equations
c      (subroutine modexp) 
c   3. calculate model vector of linear inversion from
c      coefficients alpha and selected representer
c      (subroutine expmodel)
c   4. scale model to Green matrix coefficients
c      (subroutines scalpark, scalex, scalkmet)
c
c Deviations from this scheme:
c
c   - the boxcar damping does not need scaling (step 4)
c   - the Bessel transformation combines steps 1 and two in subroutine
c     backcoeff and does not need scaling (step 4)
c   - the HOP-method knows the inverse of the Gram matrix and omits step 2
c   - the slant stack is too easy to use more than one subroutine ;-)
c     (subroutine planestack)
c   - the dirty direct inversion sets up its one system of linear equations
c     (subroutine forwardmat) and uses subroutine modexp to solve it
c     (steps 3 and 4 are not necessary)
667
668
669
c 
        if (planewave) then
c 
670
c 1. Slant Stack (assuming plane waves)
671
672
673
674
675
676
c calculate stacking algorithm with plane wave assumption
c -------------------------------------------------------
          if (i.eq.1) then
            print *,' '
            print *,'assume plane waves and use stacking algorithm...'
            print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
677
            method='Slant Stack'
678
          endif
679
          call planestack(om(i), slo, r, green, i, applywltaper)
680
681
682
c 
        elseif (parkermethod) then
c 
683
c 2. HOP-method (linear inversion with Lorentz-damping)
684
685
686
687
688
689
c calculate by Henry Orcutt Parker integral
c -----------------------------------------
          if (i.eq.1) then
            print *,' '
            print *,'use gram matrix given by Henry, Orcutt and Parker'
            print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
690
            method='HOP-inversion with '//kernel//' kernel'
691
692
693
694
695
696
697
            call nullmodel(green)
          else
            if (verbose) then
              print *,'f=',om(i)/pi2,'   rho=',rho
            endif
            rho=om(i)/rho
            rhoq=omq/rhoq
thomas.forbriger's avatar
thomas.forbriger committed
698
            call parker(rho, r, spectra(1, i), chain, firstrec, hopnumberthis)
699
            call expmodel(om(i), r, slo, green(1, i),
700
     &        hankel1, hankel2, uzerospecial, .false.)
thomas.forbriger's avatar
thomas.forbriger committed
701
            call scalpark(nslo, rhoq, omq, green(1, i), slo,
thomas.forbriger's avatar
thomas.forbriger committed
702
     &        hopnumberthis,ntr)
703
704
705
706
          endif
c 
        elseif (lininv) then
c 
707
c 3. linear inversion with exp-damping
708
709
710
711
712
713
c calculate by linear inversion exponential damping
c -------------------------------------------------
          if (i.eq.1) then
            print *,' '
            print *,'do it by linear inversion (exponential damping)'
            print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
714
            method='inversion with exp-damping and '//kernel//' kernel'
715
716
717
718
719
720
721
722
            call nullmodel(green)
          else
            if (verbose) then
              print *,'f=',om(i)/pi2,'   rho=',rho
            endif
            call gramex(rhoq, omq, r)
            call modexp(i, spectra(1, i))
            call expmodel(om(i), r, slo, green(1, i),
723
     &        hankel1, hankel2, uzerospecial, .false.)
thomas.forbriger's avatar
thomas.forbriger committed
724
            call scalex(nslo, rhoq, green(1, i), slo, ntr)
725
726
727
728
          endif
c 
        elseif (linkinv) then
c 
729
c 4. linear inversion with K_0-damping
730
731
732
733
734
735
c calculate by linear inversion K_0 damping
c -----------------------------------------
          if (i.eq.1) then
            print *,' '
            print *,'do it by linear inversion (K_0-damping)'
            print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
736
            method='inversion with K0-damping and '//kernel//' kernel'
737
738
739
740
741
742
743
744
            call nullmodel(green)
          else
            if (verbose) then
              print *,'f=',om(i)/pi2,'   rho=',rho
            endif
            call gramkmet(rhoq, omq, r)
            call modexp(i, spectra(1, i))
            call expmodel(om(i), r, slo, green(1, i),
745
     &        hankel1, hankel2, uzerospecial, .false.)
thomas.forbriger's avatar
thomas.forbriger committed
746
            call scalkmet(nslo, rho, green(1, i), slo, ntr)
747
748
749
750
          endif
c 
        elseif (disgram) then
c 
751
c 5. linear inversion with boxcar damping
752
753
754
755
756
757
758
759
760
761
762
763
764
c calculate by linear inversion with discrete gram
c ------------------------------------------------
          if (i.eq.1) then
            print *,' '
            print *,'do it by linear inversion (discrete gram)'
            print *,' '
          endif
          if (verbose) then
            print *,'going for frequency ',i,' of ',nom
          endif
          call gramdis(slo, om(i), r)
          call modexp(i, spectra(1, i))
          call expmodel(om(i), r, slo, green(1, i), 
765
     &      hankel1, hankel2, uzerospecial, .false.)
766
767
768
c 
        elseif (matrixmethod) then
c 
769
c 6. the dirty direct matrix inversion
770
771
772
773
774
775
776
c calculate green matrix by matrix inversion method
c -------------------------------------------------
          if (i.eq.1) then
            print *,' '
            print *,'do it by direct matrix inversion'
            print *,' '
            print *,'THIS IS A DIRTY WAY! You will run into trouble...'
thomas.forbriger's avatar
thomas.forbriger committed
777
            method='dirty direct matrix inversion'
778
779
780
781
782
783
784
785
786
          endif
          call forwardmat(om(i), slo, r, hankel1, hankel2, uzerospecial)
          call modexp(i, spectra(1, i))
          do j=1,nslo
            green(j, i)=alpha(j)
          enddo
c 
        else
c 
787
c 7. discrete Fourier-Bessel transformation
thomas.forbriger's avatar
thomas.forbriger committed
788
789
c perform a Fourier Bessel transform
c ----------------------------------
790
791
          if (i.eq.1) then
            print *,' '
thomas.forbriger's avatar
thomas.forbriger committed
792
            print *,'do it by inverse Fourier Bessel transform'
thomas.forbriger's avatar
thomas.forbriger committed
793
            method='Fourier Bessel transform with '//kernel//' kernel'
794
795
          endif
          call backcoeff((verbose.and.(i.eq.1)), r, om(i), 
thomas.forbriger's avatar
thomas.forbriger committed
796
     &      spectra(1, i), chain, firstrec, hopnumberthis)
797
          call expmodel(om(i), r, slo, green(1, i), 
798
     &      hankel1, hankel2, uzerospecial, applywltaper)
thomas.forbriger's avatar
thomas.forbriger committed
799
800
801
802
803
804
          if (backtranscale) then
            if (i.eq.1) then
              call nullmodel(green)
            else
              rho=om(i)/rho
              rhoq=omq/rhoq
thomas.forbriger's avatar
thomas.forbriger committed
805
              call scalpark(nslo, rhoq, omq, green(1, i), slo,
thomas.forbriger's avatar
thomas.forbriger committed
806
     &          hopnumberthis,ntr)
thomas.forbriger's avatar
thomas.forbriger committed
807
808
            endif
          endif
809
810
811
c 
        endif
c 
thomas.forbriger's avatar
thomas.forbriger committed
812
c write phasor walkout to file
thomas.forbriger's avatar
thomas.forbriger committed
813
        call pwo_write(overwrite, verbose, method, r, ntr)
thomas.forbriger's avatar
thomas.forbriger committed
814
c 
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
c in any case make shure that verbose is switched on only for
c first loop cycle
c        verbose=.false.
      enddo
c 
c----------------------------------------------------------------------
c 
c write green code (easy to use)
c 
      print *,' '
      if (overwrite) then
        print *,'opening green file ',greensfile(1:index(greensfile,' ')),
     &    ' - overwrite mode'
        open(lu, file=greensfile, form='unformatted', err=98)
      else
        print *,'opening green file ',greensfile(1:index(greensfile,' '))
        open(lu, file=greensfile, status='new', form='unformatted', err=98)
      endif
      call tf_magic(cmagic, magic)
      write(lu, err=97) magic
      write(lu, err=97) nom, nslo
      write(lu, err=97) (om(i), i=1,nom), (slo(i), i=1,nslo)
      write(lu, err=97) ((green(j,i), i=1,nom), j=1,nslo)
      close(lu, err=96)
c 
      stop
   99 stop 'ERROR: reading command line argument'
   98 stop 'ERROR: opening green file'
   97 stop 'ERROR: writing green file'
   96 stop 'ERROR: closing green file'
      end
c
c======================================================================
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
c
c summary of general subroutines
c ==============================
c
c read sff seismogram traces
c --------------------------
c
c     subroutine readdata(filename, fdata, idata, spectra, r, maxr,
c    &     maxtr, maxsamp, ntr, nsamp, dt, tfirst)
c       
c stack all seismograms within stackdelta (option -B stackdelta)
c ---------------------------------------
c reads common blocks from greda.inc
c
c     subroutine stackthem(r, stackdelta, maxr, verbose)
c
c rescale Fourier coefficients (option -F)
c ----------------------------
c reads common blocks from greda.inc
c
c     subroutine dospecrescale(expon, r, om)
c
c rescale seismogram traces (option -r expon)
c -------------------------
c reads common blocks from greda.inc
c
c     subroutine dorescale(expon, r)
c
c apply time and offset domain tapering (many options)
c -------------------------------------
c
c     subroutine taper(maxsamp, maxtr, ntr, nsamp, tapfrac, offtapfrac,
c    &  maxr, spectra, r, offtaper, verbose, edgeset, edgefrac,
c    &  softcosine, gausstaper, gausstime)
c
883
884
885
886
887
888
c apply special offset domain taper (option -tap tapoffsets)
c ---------------------------------
c
c     subroutine specialtap(maxsamp, maxtr, ntr, nsamp, tapoffsets, maxr
c    &    spectra, r, verbose)
c
889
890
891
892
893
c calculate complex Fourier coefficients from waveform data
c ---------------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine calcspec(dt, tfirst, om, fmax, fmaxset)
894
895
896
897
898
c 
c write trace Fourier spectra (easy to use) (option -spo filename)
c ---------------------------------------­-
c reads common blocks from greda.inc
c 
thomas.forbriger's avatar
thomas.forbriger committed
899
c     subroutine Fourierwrite(filename, magic, cmagic, overwrite,
900
c    &  r, maxr, om)
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
c
c set slowness values for different expansion cases
c -------------------------------------------------
c fills the array slo with appropriate values
c
c     subroutine setslo(maxslo, nslo, smax, slo, 
c    &  hankel1, hankel2, uzerospecial)
c
c set coefficients for all slowness values to zero
c ------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine nullmodel(ogreen)
c
c expands the model, using alpha as expnsion coefficients and
c J_0, H^1_0, H^2_0 as a representer
c -----------------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine expmodel(omega, r, slo, ogreen, 
921
c    &  hankel1, hankel2, uzerospecial, applywltaper)
922
923
924
925
926
927
928
929
c
c numerically solve the system of linear equations for the gram matrix
c --------------------------------------------------------------------
c claculates the expansion coefficients alpha
c reads common blocks from greda.inc
c
c     subroutine modexp(iom, s)
c
930
931
932
933
934
935
936
937
938
939
940
941
c initialize wavelength specific taper parameters
c -----------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine initwltaper(length,fraction)
c
c set wavelength specific taper to given wavelength
c -------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine setwltaper(omega,slo,r)
c
942
c----------------------------------------------------------------------
943
c 
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
c summary of subroutines specific to different approaches
c =======================================================
c
c fills expansion coefficients alpha with values appropriate to
c modified Fourier-Bessel transform
c -------------------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine backcoeff(verbose, r, omega, spect, chain, firstrec,
c    &                     numbers)
c
c calculate Gram matrix for linear inversion with exp-damping 
c -----------------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine gramex(rhoq, omq, r)
c
c scale greens matrix appropriate to exp-damping
c ----------------------------------------------
c
thomas.forbriger's avatar
thomas.forbriger committed
964
c     subroutine scalex(nslo, rhoq, ogreen, slo, ntr)
965
966
967
968
969
c
c calculate Greens matrix directly by Slant Stack
c -----------------------------------------------
c reads common blocks from greda.inc
c
970
c     subroutine planestack(omega, slo, r, green, iomega, applywltaper)
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
c
c fill Gram matrix for a direct inversion (that's a dirty method)
c ---------------------------------------
c reads common blocks from greda.inc
c
c     subroutine forwardmat(omega, slo, r, hankel1, hankel2, uzerospecial)
c
c calculate Gram matrix for linear inversion with K_0-damping
c -----------------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine gramkmet(rhoq, omq, r)
c
c scale Green matrix appropriate to K_0-damping
c ---------------------------------------------
c
thomas.forbriger's avatar
thomas.forbriger committed
987
c     subroutine scalkmet(nslo, rho, ogreen, slo, ntr)
988
989
990
991
992
993
994
995
996
997
998
999
1000
c
c calculate Gram matrix for linear inversion with boxcar-damping
c --------------------------------------------------------------
c reads common blocks from greda.inc
c
c     subroutine gramdis(slo, om, r)
c
c function used by gramdis for numerical integration
c --------------------------------------------------
c
c     double precision function gramdisint(maxslo, nslo,
c    &  slo, rj, rk, om)
c
1001
1002
1003
c calculate expansion coefficients alpha for linear inversion
c with Lorentz-damping
c -----------------------------------------------------------
1004
1005
1006
1007
1008
1009
1010
c reads common blocks from greda.inc
c
c     subroutine parker(rho, r, s, chain, firstrec, numbers)
c
c scale Green matrix appropriate to Lorentz-damping
c -------------------------------------------------
c
thomas.forbriger's avatar
thomas.forbriger committed
1011
c     subroutine scalpark(nslo, rhoq, omq, ogreen, slo, numbers, ntr)
1012
1013
1014
c
c======================================================================
c
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
c some general subroutines
c
c----------------------------------------------------------------------
c
      subroutine readdata(filename, fdata, idata, spectra, r, maxr,
     &     maxtr, maxsamp, ntr, nsamp, dt, tfirst)
c 
c read sff seismic traces
c
      character filename*(*)
      integer maxtr, maxsamp, ntr, nsamp
      real fdata(maxsamp)
      integer idata(maxsamp)
      complex*16 spectra(maxtr, maxsamp)
      real r(maxtr), dt, tfirst, maxr
c 
      integer lu, ierr, i
      real version
      parameter(lu=20)
      character timestamp*20, code*10, type*20, cs*1, date*6, time*10
      character wid2line*132
      integer srctime(7), datatime(7), tdif(7), itdif(7)
      real sc1, sc2, sc3, c1, c2, c3, tanf, idt, sffu_seconds
      integer nstack, insamp, time_compare
      logical last
c 
c open sff-file
      print *,'open sff data ',filename(1:index(filename,' '))
      call sff_ROpenS(lu, filename, version, timestamp, code,
     &  type, cs, sc1, sc2, sc3, date, time, ierr)
      if (ierr.ne.0) stop 'ERROR: opening seismogram file'
      if (index(code, 'S').eq.0) stop 'ERROR: no SRCE line'
      if (cs.ne.'C') stop 'ERROR: source coordinates are not cartesian'
      call sffu_timesrce(date, time, srctime) 
      ntr=0
      maxr=0.
   1  continue
        ntr=ntr+1
        insamp=maxsamp
        call sff_RTraceI(lu, tanf, idt, wid2line, insamp, fdata, idata,
     &    code, last, cs, c1, c2, c3, nstack, ierr)
        if (ierr.ne.0) stop 'ERROR: reading trace'
        if (index(code, 'I').eq.0) stop 'ERROR: no INFO line'
        if (cs.ne.'C') stop 'ERROR: trace coordinates are not cartesian'
        call sffu_timewid2(wid2line, datatime)
        if (ntr.eq.1) then
          dt=idt
          nsamp=insamp
          call time_sub(datatime, srctime, tdif)
          tfirst=sffu_seconds(tdif)
        else
          call time_sub(datatime, srctime, itdif)
          if (nsamp.ne.insamp)
     &      print *,'NOTICE: your time series have inconsitent numbers ',
     &        'of samples'
          nsamp=max(nsamp,insamp)
          if ((dt.ne.idt).or.
     &        (time_compare(tdif, itdif).ne.0)) then
            print *,'ERROR: sampling interval or'
            stop 'ERROR: time of first sample differs from previous trace'
          endif
        endif
        r(ntr)=sqrt((sc1-c1)**2+(sc2-c2)**2+(sc3-c3)**2)
        maxr=max(maxr,r(ntr))
        do i=1,maxsamp
          spectra(ntr, i)=(0.,0.)
        enddo
thomas.forbriger's avatar
thomas.forbriger committed
1082
        do i=1,insamp
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
          spectra(ntr, i)=cmplx(fdata(i))
        enddo
        if ((ntr.lt.maxtr).and.(.not.last)) goto 1
      if (.not.(last)) then
        print *,'NOTICE: will ignore traces after ',ntr
        close(lu)
      endif
      print *,'file read and closed'
      return
      end
c 
c----------------------------------------------------------------------
c
      subroutine stackthem(r, stackdelta, maxr, verbose)
c 
c stack all seismograms within stackdelta
c 
c get common block
1101
      include 'greda_dim.inc'
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
      include 'greda.inc'
c 
      real r(maxtr), stackdelta, maxr
      logical verbose
c 
c something to work here
      logical obsolete(maxtr)
      integer i,j,k,n
      real refoff
c 
      print *,' '
      print *,'  stack traces within ',stackdelta,'m offset range'
      print *,'  (entered with ',ntr,' traces)'
c 
      do i=1,ntr
        obsolete(i)=.false.
      enddo
c 
      do i=1,ntr-1
        if (.not.(obsolete(i))) then
          n=1
          do j=i+1,ntr
            if (.not.(obsolete(j))) then
              refoff=r(i)/float(n)
              if (abs(r(j)-refoff).le.stackdelta) then
                n=n+1
                if (verbose) print *,'    stack ',j,' at ',r(j),' --> ',i,
     &            ' at ',r(i)
                obsolete(j)=.true.
                do k=1,nsamp
                  spectra(i,k)=spectra(i,k)+spectra(j,k)
                enddo
                r(i)=r(i)+r(j)
              endif
            endif
          enddo
          if (n.gt.1) then
            do k=1,nsamp
              spectra(i,k)=spectra(i,k)/float(n)
            enddo
            r(i)=r(i)/float(n)
            if (verbose) print *,'    ',n,' traces stacked to trace ',
     &        i,' at ',r(i)
          endif
        endif
      enddo
c 
      n=0
      do i=1,ntr
        if (.not.(obsolete(i))) then
          n=n+1
          if (n.ne.i) then
            do k=1,nsamp
              spectra(n,k)=spectra(i,k)
            enddo
            r(n)=r(i)
          endif
        endif
      enddo
c 
      ntr=n
      print *,'  ',ntr,' traces left after stacking'
c 
      maxr=r(1)
      do i=1,ntr
        maxr=max(maxr, r(i))
      enddo
c 
      return
      end
c 
c----------------------------------------------------------------------
c
      subroutine dospecrescale(expon, r, om)
c
thomas.forbriger's avatar
thomas.forbriger committed
1177
c rescale traces Fourier coefficients
1178
c
1179
      include 'greda_dim.inc'
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
      include 'greda.inc'
c
      real expon, r(maxtr)
      real om(maxom)
c
      double precision fact
      integer i,j
c
      print *,' '
      print *,'rescale spectral coefficients to energy content of',
     &        ' (omega*r)^(-',expon,')'
c 
      do i=1,ntr
        do j=1,nsamp
          fact=sqrt((om(j)*r(i))**(-expon))
          spectra(i,j)=spectra(i,j)*fact/abs(spectra(i,j))
        enddo
      enddo
c 
      return
      end
c
c----------------------------------------------------------------------
c
      subroutine dorescale(expon, r)
c
c rescale traces
c
1208
      include 'greda_dim.inc'
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
      include 'greda.inc'
c
      real expon, r(maxtr)
c
      double precision sum, fact
      integer i,j
c
      print *,' '
      print *,'rescale seismic traces to energy content of r^(-',expon,')'
c 
      do i=1,ntr
        sum=0.d0
        do j=1,nsamp
          sum=sum+real(spectra(i,j))*real(spectra(i,j))
        enddo
        fact=sqrt(r(i)**(-expon)/sum)
        do j=1,nsamp
          spectra(i,j)=spectra(i,j)*fact
        enddo
      enddo
c 
      return
      end
c
c----------------------------------------------------------------------
c 
      subroutine taper(maxsamp, maxtr, ntr, nsamp, tapfrac, offtapfrac,
     &  maxr, spectra, r, offtaper, verbose, edgeset, edgefrac,
     &  softcosine, gausstaper, gausstime)
c
c apply time and offset domain tapering
c
      integer maxsamp, maxtr, ntr, nsamp
      real tapfrac, offtapfrac, maxr, edgefrac
      double complex spectra(maxtr, maxsamp)
      real r(maxtr)
      logical offtaper, verbose, edgeset, softcosine, gausstaper
      logical gausstime
c 
      integer i, l, wil, wirb, wire
1249
      real fac, tf_sincostap, rtap, pi, ltap
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
      double precision sigmaq
      parameter(pi=3.141592653589793)
c 
      print *,' '
      print *,'apply taper to input data'
c 
c first apply time domain taper
c 
      wil=int(nsamp*tapfrac/100.)+1
      wire=nsamp
      if (edgeset) wire=int(nsamp*edgefrac)
      wirb=wire-wil 
      if (gausstime) then
        tapfrac=max(tapfrac,1.e-10)
        sigmaq=-1.*(wire/2)**2/log(tapfrac*0.01)
        print *,'  using ',tapfrac,'% time domain gaussian taper'
        print *,'     (zero after sample ',wire,' with ',
     &    nsamp,' samples total)'
      else
        print *,'  using ',tapfrac,'% time domain cosine taper'
        print *,'    (full scale from sample ',
     &    wil,' to ',wirb
        print *,'     zero after sample ',wire,' with ',
     &    nsamp,' samples total)'
      endif
      do i=1,nsamp
        if (gausstime) then
          fac=exp(-1.*(i-wire/2.)**2/sigmaq)
          if (i.gt.wire) fac=0.
          if (verbose) print *,'  sample ',i,'   fac: ',fac
        else
          fac=tf_sincostap(i, 1, wil, wirb, wire)
        endif
        do l=1,ntr
          spectra(l,i)=fac*spectra(l,i)
        enddo
      enddo
c 
c apply offset domain taper
      if (offtaper) then
        if (gausstaper) then
          offtapfrac=max(offtapfrac,1.e-10)
          sigmaq=-1.*maxr*maxr/log(offtapfrac*0.01)
          print *,'  apply ',offtapfrac,
     &      '% offset domain gaussian taper'
          print *,'    from 0m to ',maxr,'m'
        else
1297
1298
          ltap=maxr*(1.-0.01*offtapfrac)
          rtap=maxr*(1.+(0.01*offtapfrac)**2)
1299
1300
1301
1302
1303
1304
1305
          if (softcosine) then
            print *,'  apply ',offtapfrac,
     &        '% offset domain soft cosine taper'
          else
            print *,'  apply ',offtapfrac,
     &        '% offset domain hard cosine taper'
          endif
1306
          print *,'    from ', ltap,'m to ',rtap,'m'
1307
1308
1309
1310
1311
1312
        endif
c 
        do i=1,ntr
          fac=1.
          if (gausstaper) then
            fac=exp(-1.*r(i)*r(i)/sigmaq)
1313
          elseif (r(i).gt.ltap) then
1314
1315
            if (softcosine) then
c smooth taper
1316
              fac=0.5*(1.+cos(pi*(r(i)-ltap)/(rtap-ltap)))
1317
1318
            else
c hard taper
1319
              fac=cos(0.5*pi*(r(i)-ltap)/(rtap-ltap))
1320
1321
1322
1323
1324
1325
            endif
          endif
          if (verbose) then
            print *,'    at ',r(i),'m   fac: ',fac
          endif
          do l=1,nsamp
thomas.forbriger's avatar
thomas.forbriger committed
1326
            spectra(i,l)=fac*spectra(i,l)
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
          enddo
        enddo
      else
        print *,'  no offset domain taper will be applied'
      endif
c 
      return
      end
c
c----------------------------------------------------------------------
1337
1338
1339
1340
1341
c 
      subroutine specialtap(maxsamp, maxtr, ntr, nsamp, tapoffsets, 
     &    maxr, spectra, r, verbose)
c
c apply special offset domain taper
thomas.forbriger's avatar
thomas.forbriger committed
1342
1343
c
c 28/03/2006: order of array indices in spectra was wrong
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
c
      integer maxsamp, maxtr, ntr, nsamp
      real tapoffsets(4), maxr
      double complex spectra(maxtr, maxsamp)
      real r(maxtr)
      logical verbose
c 
      integer i, l
      real fac, pi
      parameter(pi=3.141592653589793)
c 
      print *,' '
      print *,'apply special offset taper to input data'
      print *,'  min=',tapoffsets(1),', left=',tapoffsets(2),
     & ' right=',tapoffsets(3),', max=',tapoffsets(4)
c 
c apply offset domain taper
c 
      do i=1,ntr
        fac=1.
        if (r(i).lt.tapoffsets(1)) then
          fac=0.
        elseif (r(i).gt.tapoffsets(4)) then
          fac=0.
        elseif (r(i).lt.tapoffsets(2)) then
          fac=sin(0.5*pi*(r(i)-tapoffsets(1))/
     &      (tapoffsets(2)-tapoffsets(1)))
        elseif (r(i).gt.tapoffsets(3)) then
          fac=cos(0.5*pi*(r(i)-tapoffsets(3))/
     &      (tapoffsets(4)-tapoffsets(3)))
        else
          fac=1.
        endif
        if (verbose) then
          print *,'    at ',r(i),'m   fac: ',fac
        endif
        do l=1,nsamp
thomas.forbriger's avatar
thomas.forbriger committed
1381
          spectra(i,l)=fac*spectra(i,l)
1382
1383
1384
1385
1386
1387
1388
        enddo
      enddo
c 
      return
      end
c
c----------------------------------------------------------------------
1389
1390
1391
1392
1393
1394
c
      subroutine calcspec(dt, tfirst, om, fmax, fmaxset)
c 
c calculate complex spectra of time series data
c
c get common block
1395
      include 'greda_dim.inc'
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
      include 'greda.inc'
c
      real dt, tfirst, om(maxom), fmax
      logical fmaxset
c 
      real scal, df
      integer i, j, pow, newnsamp
      real pi2
      parameter(pi2=2.*3.141592653589793)
c 
      print *,' '
      print *,'transform dataset to frequency domain'
c now get next integer power of two
      pow=0
    1 continue
        pow=pow+1
        newnsamp=2**pow
        if (newnsamp.lt.nsamp) goto 1
      if (newnsamp.gt.maxsamp)
     &  stop 'ERROR: can not fit integer power of two into samples dimension'
c 
c get correct scaling factor to manipulate FORK
      scal=dt*sqrt(float(newnsamp))
c calculate other values
      nom=newnsamp/2+1
1421
      df=1/(newnsamp*dt)
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440