-
Notifications
You must be signed in to change notification settings - Fork 9
/
select_windows_stalta2.f90
1307 lines (1107 loc) · 42.6 KB
/
select_windows_stalta2.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
137
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
214
215
216
217
218
219
220
221
222
223
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
260
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
329
330
331
332
333
334
335
336
337
338
339
340
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
384
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
506
507
508
509
510
511
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
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
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!
! $Id:$
!
!
!----------------------------------------------------------------------
subroutine select_windows_stalta2()
use seismo_variables
implicit none
integer, dimension(NWINDOWS) :: maxima_lp, minima_lp
integer, dimension(NWINDOWS*NWINDOWS) :: iM, iL, iR
double precision, dimension(NWINDOWS) :: CC_local, Tshift_local, dlnA_local
integer :: nmin, nmax, nwin
integer :: k
double precision :: w_level_local
logical :: data_ok
double precision :: noise_int, signal_int, snr_int, signal_amp, noise_amp, snr_amp
! initialise global arrays
num_win = 0
i_start(:) = 0 ; i_end(:) = 0
win_start(:) = 0 ; win_end(:) = 0
Tshift(:) = 0 ; CC(:) = 0 ; dlnA(:) = 0
Tshift_after(:) = 0 ; CC_after(:) = 0 ; dlnA_after(:) = 0
! initialise local arrays
maxima_lp(:) = 0; minima_lp(:) = 0
iM(:) = 0; iL(:) = 0; iR(:) = 0
CC_local(:) = 0; dlnA_local(:) = 0; Tshift_local(:) = 0
! make all preparatory steps (create envelopes and sta_lta time series)
if(DEBUG) print *, "Preparing windows for stalta"
call prepare_for_window_sel_stalta
! check data quality (signal-to-noise ratios)
if(DATA_QUALITY) then
if(DEBUG) print *, "Checking data quality"
! returns data_ok = .true. if two signal-to-noise criteria are met
call check_data_quality(data_ok,signal_int,noise_int,snr_int,signal_amp,noise_amp,snr_amp)
! if data quality is not ok, then return without selecting windows
if (.not. data_ok) then
num_win = 0
return
endif
endif
!!$ if (1==1) then
!!$ ! keep the manually selected window that has met the signal-to-noise criteria;
!!$ ! this option is used for time-reversing coda surface waves
!!$ nwin = 1
!!$ iL(1) = is1
!!$ iM(1) = int((is1+is2)/2)
!!$ iR(1) = is2
!!$
!!$ ! reject window if it does not satisfy F2, tshift and dlnA criteria;
!!$ ! here we are only interested in using the dlnA criterion
!!$ call reject_on_fit_criteria(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
!!$ if (nwin.eq.0) then
!!$ write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
!!$ num_win = 0
!!$ return
!!$ endif
!!$ if(DEBUG) call display_windows_full(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
!!$
!!$ else
! find list of maxima and minima, ignoring water level
w_level_local = 0.0
if(DEBUG) print *, "Finding max min "
call find_maxima(STA_LTA,npts,maxima_lp,nmax,w_level_local)
call find_minima(STA_LTA,npts,minima_lp,nmin,w_level_local)
! find all possible windows within seismogram, with central maxima above the water level
if(DEBUG) print *, "Making all windows "
call setup_M_L_R(nmax,maxima_lp,nmin,minima_lp,nwin,iM,iL,iR)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
! remove windows with internal minima below the water level
if(DEBUG) print *, "Rejecting on water level "
call reject_on_water_level(nwin,iM,iL,iR,nmin,minima_lp,C_0)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
! sort windows
call sort_on_start_time(nwin,iM,iL,iR)
!if(DEBUG) call display_windows(nwin,iM,iL,iR)
! remove window whose starttime(iL) is in noise region
if(DEBUG) print *, "Rejecting on noise region "
call reject_on_noise_region(nwin, iM, iL, iR)
if(DEBUG) call display_windows(nwin,iM,iL,iR)
! remove small windows
if(DEBUG) print *, "Rejecting on size "
call reject_on_window_width(nwin,iM,iL,iR,C_1)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
!if(DEBUG) call display_windows(nwin,iM,iL,iR)
! reject on prominence of central maximum
if(DEBUG) print *, "Rejecting on prominence "
call reject_on_prominence(nwin,iM,iL,iR,nmin,minima_lp,C_2)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
!if(DEBUG) call display_windows(nwin,iM,iL,iR)
! reject windows containing more than one distinct phase
call reject_on_phase_separation(nwin,iM,iL,iR,nmin,minima_lp,nmax,maxima_lp,C_3a, C_3b)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
!if(DEBUG) call display_windows(nwin,iM,iL,iR)
! curtail window ends by time decay
call curtail_window_length(nwin,iL,iR,nmax,maxima_lp,C_4a,C_4b)
!if(DEBUG) call display_windows(nwin,iM,iL,iR)
! REPEAT: remove small windows, since curtailing may have shrunk them
! NOTE: perhaps this only needs to be done once (here)
if(DEBUG) print *, "Rejecting on size (REPEAT)"
call reject_on_window_width(nwin,iM,iL,iR,C_1)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
if(DEBUG) call display_windows(nwin,iM,iL,iR)
! check window quality
if(DATA_QUALITY) call check_window_s2n(nwin,iM,iL,iR)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
if(DEBUG) call display_windows(nwin,iM,iL,iR)
! reject windows that do not satisfy F2, tshift and dlnA criteria
call reject_on_fit_criteria(nwin,iM,iL,iR,CC_local,Tshift_local, dlnA_local)
if (nwin.eq.0) then
write(*,*) 'NO WINDOWS SELECTED FOR THIS TRACE'
num_win = 0
return
endif
if(DEBUG) call display_windows_full(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
! now that all criteria are satisfied, reject any duplicate windows
call reject_on_duplicate(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
if(DEBUG) call display_windows_full(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
! resolve the overlaps
call resolve_overlaps(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
if(DEBUG) call display_windows_full(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
!!$ endif
! set global number of windows
num_win = nwin
do k = 1,num_win
win_start(k) = b+(iL(k)-1)*dt
win_end(k) = b+(iR(k)-1)*dt
enddo
write(*,*) "Selected windows, start and end time, CC, Tshift, dlnA "
do k = 1,num_win
write(*,'(i4,1x,5f10.3)') k, win_start(k), win_end(k), CC_local(k), Tshift_local(k), dlnA_local(k)
! sanity check for the end of the record
if(win_start(k) .lt. b) win_start(k) = b
if(win_end(k) .gt. b+(npts-1)*dt) win_end(k) = b+(npts-1)*dt
! calculate indexes for start and and of windows
i_start(k) = 1+int((win_start(k)-b)/dt)
i_end(k) = 1+int((win_end(k) -b)/dt)
enddo
! setup quality criteria
CC(1:num_win) = CC_local(1:num_win)
dlnA(1:num_win) = dlnA_local(1:num_win)
Tshift(1:num_win) = Tshift_local(1:num_win)
end subroutine select_windows_stalta2
subroutine reject_on_noise_region(nwin, iM, iL, iR)
! WJL: Reject window whose starttime is in the noise region(before
! noise_end). It is nice to put this function on the top cause
! it could reject a large amount of windows but also acts very
! fast and cheap.
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iR, iL
logical :: accept
integer :: nwin_new, iwin
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
nwin_new = 0
do iwin = 1,nwin
accept = .true.
! check window length against minimum acceptable length
if (iL(iwin) .lt. noise_end ) then
!print *, "iL < noise_end:", iL(iwin), noise_end
accept = .false.
end if
if (accept) then
! checks windows
if (nwin_new .ge. NWINDOWS) then
print *, 'reject_on_noise_region: limit (nwin = NWINDOWS)'
exit
endif
nwin_new = nwin_new + 1
iM_new(nwin_new) = iM(iwin)
iR_new(nwin_new) = iR(iwin)
iL_new(nwin_new) = iL(iwin)
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
nwin = nwin_new
iM(1:nwin) = iM_new(1:nwin_new)
iL(1:nwin) = iL_new(1:nwin_new)
iR(1:nwin) = iR_new(1:nwin_new)
if (DEBUG) write(*,*) 'DEBUG : window noise region retained ', nwin, ' acceptable windows'
end subroutine
subroutine check_window_s2n(nwin,iM,iL,iR)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
integer :: iwin, i
integer :: nwin_new
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
logical :: accept
double precision :: time, noise_amp, signal_amp, amp_ratio
double precision :: noise_pow, signal_pow, pow_ratio
! Determine the max amplitude of the noise, as defined by the
! global variable noise_end.
do i = 1, NDIM
time = b+(i-1)*dt
if (time > noise_end) exit
enddo
! i is the index corresponding to noise_end
noise_amp = maxval( abs(obs_lp(1:i)) )
noise_pow = sum( (obs_lp(1:i))**2 )/(i-1)
! Determine the max amplitude of the windows that have their
! left bound time greater than the noise_end time.
! Window will be rejected based on the amp_ratio.
nwin_new = 0
do iwin = 1, nwin
accept = .true.
signal_amp = maxval( abs(obs_lp(iL(iwin):iR(iwin))) )
signal_pow = sum( (obs_lp(iL(iwin):iR(iwin)))**2 )/(iR(iwin)-iL(iwin))
amp_ratio = signal_amp / noise_amp
pow_ratio = signal_pow / noise_pow
if(DEBUG) then
write (*,*) 'DEBUG : amp_ratio,signal_amp,noise_amp : ',amp_ratio,signal_amp,noise_amp
write (*,*) 'DEBUG : pow_ratio,signal_pow,noise_pow : ',pow_ratio,signal_pow,noise_pow
write (*,*) 'DEBUG : iwin, amp_ratio : ', iwin, amp_ratio, S2N_LIMIT(iM(iwin))
endif
if (amp_ratio < S2N_LIMIT(iM(iwin))) then
!if (pow_ratio < S2N_LIMIT(iM(iwin))) then
accept = .false.
endif
if (accept) then
nwin_new = nwin_new + 1
iM_new(nwin_new) = iM(iwin)
iR_new(nwin_new) = iR(iwin)
iL_new(nwin_new) = iL(iwin)
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
nwin=nwin_new
iM(1:nwin) = iM_new(1:nwin_new)
iL(1:nwin) = iL_new(1:nwin_new)
iR(1:nwin) = iR_new(1:nwin_new)
if (DEBUG) write(*,*) 'DEBUG : window noise quality rejection retained ', nwin, ' acceptable windows'
end subroutine check_window_s2n
subroutine check_data_quality(data_ok,signal_int,noise_int,snr_int,signal_amp,noise_amp,snr_amp)
use seismo_variables
implicit none
! data_ok is a logical value to indicate whether or not
! to continue on with the window selection algorithm.
! If noise to signal ratio is too high, data_ok is false,
! and that particular station will be rejected.
logical, intent(out) :: data_ok
double precision, intent(out) :: signal_int,noise_int,snr_int,signal_amp,noise_amp,snr_amp
double precision :: time_obs_noise, time_obs_signal
integer :: i, j, k, m
! initialize values
data_ok = .true.
noise_int = 0.0
signal_int = 0.0
snr_int = 0.0
signal_amp = 0.0
noise_amp = 0.0
snr_amp = 0.0
if(DEBUG) then
write(*,*) 'DEBUG : noise_start = ', sngl(noise_start)
write(*,*) 'DEBUG : noise_end = ', sngl(noise_end)
write(*,*) 'DEBUG : signal_start = ', sngl(signal_start)
write(*,*) 'DEBUG : signal_end = ', sngl(signal_end)
endif
! compute noise
! see user_functions.f90 (default: noise_start = b)
do m = 1, npts
time_obs_noise = b+(m-1)*dt
if (time_obs_noise > noise_start) exit
enddo
in1 = m-1 ! index corresponding to noise_start
do i = 1, npts
time_obs_noise = b+(i-1)*dt
if (time_obs_noise > noise_end ) exit
enddo
in2 = i-1 ! index corresponding to noise_end
noise_int = sum( (obs_lp(in1:in2))**2 )/(in2-in1)
noise_amp = maxval( abs(obs_lp(in1:in2)) )
! compute signal
! see user_functions.f90 (default: signal_start = noise_end)
do j = 1, npts
time_obs_signal = b+(j-1)*dt
if (time_obs_signal > signal_start) exit
enddo
is1 = j ! index corresponding to signal_start
do k = 1, npts
time_obs_signal = b+(k-1)*dt
if (time_obs_signal > signal_end) exit
enddo
is2 = k-1 ! index corresponding to signal_end
print *, 'is1 = ', is1, ', is2 = ', is2
signal_int = sum( (obs_lp(is1:is2))**2 )/(is2-is1)
signal_amp = maxval( abs(obs_lp(is1:is2)) )
! Calculate signal to noise ratio and amplitude ratio.
snr_int = signal_int / noise_int
snr_amp = signal_amp / noise_amp
! If snr_int or snr_amp is less than a certain value, then data_ok is .false.
if (snr_int < SNR_INTEGRATE_BASE .or. snr_amp < SNR_MAX_BASE) then
write(*,*) 'DATA QUALITY NOT OK'
if(DEBUG) write(*,*) 'snr_int < SNR_INTEGRATE_BASE: ', &
sngl(snr_int), sngl(SNR_INTEGRATE_BASE), snr_int < SNR_INTEGRATE_BASE
if(DEBUG) write(*,*) 'snr_amp < SNR_MAX_BASE: ', &
sngl(snr_amp), sngl(SNR_MAX_BASE), snr_amp < SNR_MAX_BASE
data_ok = .false.
endif
if (DEBUG) then
print *, 'DEBUG : in1,in2,is1,is2,npts :', in1,in2,is1,is2,npts
print *, 'DEBUG : data_ok :', data_ok
print *, 'DEBUG : signal_int : ', sngl(signal_int)
print *, 'DEBUG : noise_int :', sngl(noise_int)
print *, 'DEBUG : snr_int :', sngl(snr_int)
print *, 'DEBUG : signal_amp : ', sngl(signal_amp)
print *, 'DEBUG : noise_amp :', sngl(noise_amp)
print *, 'DEBUG : snr_amp :', sngl(snr_amp)
print *, 'DEBUG : SNR_INTEGRATE_BASE :', sngl(SNR_INTEGRATE_BASE)
print *, 'DEBUG : SNR_MAX_BASE :', sngl(SNR_MAX_BASE)
endif
end subroutine check_data_quality
subroutine setup_M_L_R(nmax,maxima_lp,nmin,minima_lp,nwin,iM,iL,iR)
use seismo_variables
implicit none
integer, intent(in) :: nmax, nmin
integer, dimension(*), intent(in) :: maxima_lp, minima_lp
integer, intent(out) :: nwin
integer, dimension(NWINDOWS*NWINDOWS), intent(out) :: iM,iL,iR
integer :: i, R, L
integer :: ii, i_left, i_right
nwin = 0
do i = 1, nmax
! only continue if there are available minima on either side
if (maxima_lp(i) > minima_lp(1) .and. maxima_lp(i) < minima_lp(nmin) ) then
! only continue if this maximum is above the water level
if (STA_LTA(maxima_lp(i)) .gt. STALTA_W_LEVEL(maxima_lp(i))) then
! find the first minimum right of this maximum
R = 0
do ii = 1,nmin
if (minima_lp(ii) > maxima_lp(i)) then
R = ii
exit
endif
enddo
! find the first minimum left of this maximum
L = 0
do ii = nmin,1,-1
if (minima_lp(ii) < maxima_lp(i)) then
L = ii
exit
endif
enddo
! iterate over minima to get all possible windows around this maximum
do i_left = L,1,-1
do i_right = R,nmin,1
! checks number of windows so far
if (nwin .ge. NWINDOWS*NWINDOWS) then
print *, 'setup_M_L_R: ', nwin, NWINDOWS, NWINDOWS*NWINDOWS
print *, 'setup_M_L_R: limit number (nwin = NWINDOWS*NWINDOWS)'
!print *, 'setup_M_L_R: ignoring trace (nwin > NWINDOWS*NWINDOWS)'
!nwin = 0
!exit do-loop
exit
!return
!stop 'setup_M_L_R: Increase NWINDOWS'
endif
! set the index of the maximum, left and right boundaries
nwin = nwin+1
iM(nwin) = maxima_lp(i)
iL(nwin) = minima_lp(i_left)
iR(nwin) = minima_lp(i_right)
enddo
enddo
endif
endif
if (nwin .ge. NWINDOWS*NWINDOWS) exit
end do
if (DEBUG) write(*,*) 'DEBUG : window generation found ', nwin, ' possible windows'
end subroutine setup_M_L_R
subroutine reject_on_water_level(nwin,iM,iL,iR,nmin,minima_lp,c_value)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
integer, intent(in) :: nmin
integer, dimension(*), intent(in) :: minima_lp
double precision, intent(in) :: c_value
integer :: iwin, imin, min_index
integer :: nwin_new
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
logical :: accept
nwin_new = 0
iM_new(:) = 0
iL_new(:) = 0
iR_new(:) = 0
! for each proto-window, check that no internal minima fall below the
! local water level of the window center
do iwin = 1, nwin
accept = .true.
do imin = 1, nmin
! if the current minimum is within our window AND it falls beneath
! the water level, then reject this proto-window
min_index = minima_lp(imin)
if ( min_index .gt. iL(iwin) &
.and. min_index .lt. iR(iwin) &
.and. STA_LTA(min_index) .le. c_value*STALTA_W_LEVEL(iM(iwin)) ) then
accept = .false.
exit
end if
enddo
! if the proto-window is acceptable, then accept it
if (accept) then
! checks windows
if (nwin_new .ge. NWINDOWS) then
print *, 'reject_on_water_level: limit windows number (nwin = NWINDOWS)',nwin_new
!print *, 'reject_on_water_level: ignoring trace (nwin > NWINDOWS*NWINDOWS)'
!nwin = 0
! exit do-loop
exit
!return
!stop 'reject_on_water_level : Increase NWINDOWS'
endif
nwin_new = nwin_new + 1
iM_new(nwin_new) = iM(iwin)
iR_new(nwin_new) = iR(iwin)
iL_new(nwin_new) = iL(iwin)
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
nwin = nwin_new
iM(1:nwin) = iM_new(1:nwin_new)
iL(1:nwin) = iL_new(1:nwin_new)
iR(1:nwin) = iR_new(1:nwin_new)
if (DEBUG) write(*,*) 'DEBUG : water level rejection retained ', nwin, ' acceptable windows'
end subroutine
subroutine reject_on_phase_separation(nwin,iM,iL,iR,nmin,minima_lp,nmax,maxima_lp,c_value,c_value2)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
integer, intent(in) :: nmin, nmax
integer, dimension(*), intent(in) :: minima_lp, maxima_lp
double precision, intent(in) :: c_value, c_value2
integer :: iwin, imin, min_index, imax, max_index
integer :: nwin_new
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
double precision :: stalta_min, d_stalta, d_stalta_center, d_time, f_time
logical :: accept
nwin_new = 0
! for each proto-window, check that all included maxima are part of the
! same phase group as the central maximum iM
do iwin = 1, nwin
accept = .true.
! find the lowest minimum within the window
stalta_min = STA_LTA(iL(iwin))
do imin = 1,nmin
min_index = minima_lp(imin)
if ( min_index .gt. iL(iwin) &
.and. min_index .le. iR(iwin) &
.and. STA_LTA(min_index) .lt. stalta_min ) then
stalta_min = STA_LTA(min_index)
endif
end do
if(stalta_min .lt. STALTA_W_LEVEL(iM(iwin)) ) stalta_min = STALTA_W_LEVEL(iM(iwin))
! find the height of the central maximum above this minimum value
d_stalta_center = STA_LTA(iM(iwin)) - stalta_min
! run check on maxima on either side of the central maximum
do imax = 1, nmax
max_index = maxima_lp(imax)
if ( max_index .gt. iL(iwin) &
.and. max_index .lt. iR(iwin) &
.and. max_index .ne. iM(iwin) ) then
! find height of current maximum above lowest minimum
d_stalta = (STA_LTA(max_index) - stalta_min)
! find scaled time between current maximum and central maximum
d_time = abs(iM(iwin)-max_index)*dt / WIN_MIN_PERIOD
! find value of time decay function
if (d_time .ge. c_value2) then
f_time = exp(-((d_time-c_value2)/c_value2)**2)
else
f_time = 1.0
endif
! check condition
if (d_stalta .gt. c_value*d_stalta_center*f_time) then
accept = .false.
exit
endif
end if
end do
! if the proto-window is acceptable, then accept it
if (accept) then
nwin_new = nwin_new + 1
iM_new(nwin_new) = iM(iwin)
iR_new(nwin_new) = iR(iwin)
iL_new(nwin_new) = iL(iwin)
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
nwin = nwin_new
iM(1:nwin) = iM_new(1:nwin_new)
iL(1:nwin) = iL_new(1:nwin_new)
iR(1:nwin) = iR_new(1:nwin_new)
if (DEBUG) write(*,*) 'DEBUG : single phase group rejection retained ', nwin, ' acceptable windows'
end subroutine
subroutine reject_on_window_width(nwin,iM,iL,iR,c_value)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
double precision, intent(in) :: c_value
integer :: iwin
integer :: nwin_new
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
logical :: accept
double precision :: window_length
window_length = c_value * WIN_MIN_PERIOD / dt
nwin_new = 0
do iwin = 1,nwin
accept = .true.
! check window length against minimum acceptable length
if (iR(iwin) - iL(iwin) .lt. window_length ) then
accept = .false.
end if
if (accept) then
! checks windows
if (nwin_new .ge. NWINDOWS) then
print *, 'reject_on_window_width: limit (nwin = NWINDOWS)'
exit
!print *, 'reject_on_window_width: ignoring trace (nwin > NWINDOWS*NWINDOWS)'
!nwin = 0
!return
!stop 'Increase NWINDOWS'
endif
nwin_new = nwin_new + 1
iM_new(nwin_new) = iM(iwin)
iR_new(nwin_new) = iR(iwin)
iL_new(nwin_new) = iL(iwin)
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
nwin = nwin_new
iM(1:nwin) = iM_new(1:nwin_new)
iL(1:nwin) = iL_new(1:nwin_new)
iR(1:nwin) = iR_new(1:nwin_new)
if (DEBUG) write(*,*) 'DEBUG : window length rejection retained ', nwin, ' acceptable windows'
end subroutine
!=============================================================
!!$ subroutine reject_on_duplicate(nwin,iM,iL,iR)
!!$ use seismo_variables
!!$ implicit none
!!$ integer, intent(inout) :: nwin
!!$ integer, dimension(*), intent(inout) :: iM, iL, iR
!!$
!!$ integer :: iwin, nwin_new
!!$ integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
!!$ logical :: accept
!!$ logical, dimension(NWINDOWS) :: duplicate
!!$
!!$ duplicate(1:nwin) = .false.
!!$
!!$ nwin_new = 0
!!$ do iwin = 1,nwin
!!$ accept = .true.
!!$ if (duplicate(iwin)) accept = .false.
!!$ do iwin2 = iwin,nwin
!!$ ! check if other windows are duplicates of this one, and if so set them as duplicates
!!$ if (iwin2.ne.iwin &
!!$ .and. iL(iwin).eq.iL(iwin2) &
!!$ .and. iR(iwin).eq.iR(iwin2) ) then
!!$ duplicate(iwin2) = .true.
!!$ continue
!!$ endif
!!$ enddo
!!$
!!$ if (accept) then
!!$ nwin_new = nwin_new + 1
!!$ iM_new(nwin_new) = iM(iwin)
!!$ iR_new(nwin_new) = iR(iwin)
!!$ iL_new(nwin_new) = iL(iwin)
!!$ endif
!!$ enddo
!!$
!!$ ! update the iM, iR, iL arrays to contain only accepted proto-windows
!!$ nwin = nwin_new
!!$ iM(1:nwin) = iM_new(1:nwin_new)
!!$ iL(1:nwin) = iL_new(1:nwin_new)
!!$ iR(1:nwin) = iR_new(1:nwin_new)
!!$
!!$ if (DEBUG) write(*,*) 'DEBUG : duplicate rejection retained ', nwin, ' acceptable windows'
!!$
!!$ end subroutine
!=============================================================
subroutine reject_on_duplicate(nwin,iM,iL,iR,CC_local,Tshift_local,dlnA_local)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
double precision, dimension(*), intent(inout) :: CC_local,Tshift_local,dlnA_local
integer :: iwin, nwin_new, ikeep, iwin2
integer, dimension(NWINDOWS) :: iwin_keep
logical :: accept
logical, dimension(NWINDOWS) :: duplicate
duplicate(1:nwin) = .false.
iwin_keep(1:nwin) = 0
nwin_new = 0
do iwin = 1,nwin
accept = .true.
if (duplicate(iwin)) accept = .false.
do iwin2 = iwin,nwin
! check if other windows are duplicates of this one, and if so set them as duplicates
if (iwin2.ne.iwin &
.and. iL(iwin).eq.iL(iwin2) &
.and. iR(iwin).eq.iR(iwin2) ) then
duplicate(iwin2) = .true.
continue
endif
enddo
if (accept) then
nwin_new = nwin_new + 1
iwin_keep(nwin_new) = iwin
!write(*,*) 'Accept ', nwin_new, ' : ', iwin_keep(nwin_new)
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
! KEY: the ikeep index is ALWAYS greater than or equal to the iwin index
nwin = nwin_new
do iwin = 1,nwin
ikeep = iwin_keep(iwin)
iM(iwin) = iM(ikeep)
iR(iwin) = iR(ikeep)
iL(iwin) = iL(ikeep)
CC_local(iwin) = CC_local(ikeep)
Tshift_local(iwin) = Tshift_local(ikeep)
dlnA_local(iwin) = dlnA_local(ikeep)
enddo
if (DEBUG) write(*,*) 'DEBUG : duplicate rejection retained ', nwin, ' acceptable windows'
end subroutine
!=============================================================
subroutine reject_on_prominence(nwin,iM,iL,iR,nmin,minima_lp,c_value)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
integer, intent(in) :: nmin
integer, dimension(*), intent(in) :: minima_lp
double precision, intent(in) :: c_value
integer :: nwin_new
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
logical :: accept
integer :: iwin, imin, i_left, i_right
double precision :: delta_left, delta_right
nwin_new=0
do iwin = 1,nwin
accept = .true.
i_right = 0
i_left = 0
! find index of minimum on the right of the central maximum
do imin = 1, nmin
if (minima_lp(imin) .gt. iM(iwin)) then
i_right = minima_lp(imin)
exit
endif
enddo
! find index of minimum on the left of the central maximum
do imin = nmin, 1, -1
if (minima_lp(imin) .lt. iM(iwin)) then
i_left = minima_lp(imin)
exit
endif
enddo
delta_left = STA_LTA(iM(iwin)) - STA_LTA(i_left)
delta_right = STA_LTA(iM(iwin)) - STA_LTA(i_right)
! check prominence condition
! if the maximum is not high enough above EITHER of the two adjecent
! minima, then reject the window
if ( delta_left .lt. c_value * STALTA_W_LEVEL(iM(iwin)) &
.or. delta_right .lt. c_value * STALTA_W_LEVEL(iM(iwin)) ) then
accept = .false.
end if
if (accept) then
nwin_new = nwin_new + 1
iM_new(nwin_new) = iM(iwin)
iR_new(nwin_new) = iR(iwin)
iL_new(nwin_new) = iL(iwin)
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
nwin = nwin_new
iM(1:nwin) = iM_new(1:nwin_new)
iL(1:nwin) = iL_new(1:nwin_new)
iR(1:nwin) = iR_new(1:nwin_new)
if (DEBUG) write(*,*) 'DEBUG : prominence of central maximum rejection retained ', &
nwin, ' acceptable windows'
end subroutine
! =================================================
subroutine curtail_window_length(nwin,iL,iR,nmax,maxima_lp,cl_value,cr_value)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iL, iR
integer, intent(in) :: nmax
integer, dimension(*), intent(in) :: maxima_lp
double precision, intent(in) :: cl_value, cr_value
integer :: iwin, i_left, i_right, imax, n_left, n_right
double precision :: time_decay_left, time_decay_right, delta_left, delta_right
time_decay_left = WIN_MIN_PERIOD * cl_value / dt
time_decay_right = WIN_MIN_PERIOD * cr_value / dt
n_left = 0
n_right = 0
do iwin = 1,nwin
i_right = 0
i_left = 0
! find index of maximum on the right of left boundary
do imax = 1, nmax
if (maxima_lp(imax) .gt. iL(iwin)) then
i_left = maxima_lp(imax)
exit
endif
enddo
! find index of maximum on the left of right boundary
do imax = nmax, 1, -1
if (maxima_lp(imax) .lt. iR(iwin)) then
i_right = maxima_lp(imax)
exit
endif
enddo
delta_left = i_left - iL(iwin)
delta_right = iR(iwin) - i_right
! check condition
if (delta_left .gt. time_decay_left) then
n_left = n_left+1
iL(iwin) = int(i_left - time_decay_left)
end if
if (delta_right .gt. time_decay_right) then
n_right = n_right+1
iR(iwin) = int(i_right + time_decay_right)
end if
enddo
if (DEBUG) write(*,*) 'DEBUG : curtailed ', n_left, ' windows on the left'
if (DEBUG) write(*,*) 'DEBUG : curtailed ', n_right, ' windows on the right'
end subroutine
!------------------------------------------------
subroutine reject_on_fit_criteria(nwin,iM,iL,iR,CC_local,Tshift_local, dlnA_local)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
double precision, dimension(*), intent(out) :: CC_local, Tshift_local, dlnA_local
integer :: iwin, nwin_new
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new
double precision :: CC_temp, Tshift_temp, dlnA_temp
double precision :: tshift_min, tshift_max, dlnA_min, dlnA_max
logical :: accept
nwin_new = 0
! for each proto-window, check that all included maxima are part of the
! same phase group as the central maximum iM
do iwin = 1, nwin
accept=.true.
! check the conditions, and set accept to false if rejection
call calc_criteria(obs_lp,synt_lp,npts,iL(iwin),iR(iwin),dt,Tshift_temp,CC_temp,dlnA_temp)
! here we allow for a systematic shift in the time shift and amplitude measurements
tshift_min = TSHIFT_REFERENCE - TSHIFT_LIMIT(iM(iwin))
tshift_max = TSHIFT_REFERENCE + TSHIFT_LIMIT(iM(iwin))
dlnA_min = DLNA_REFERENCE - DLNA_LIMIT(iM(iwin))
dlnA_max = DLNA_REFERENCE + DLNA_LIMIT(iM(iwin))
if ( (Tshift_temp .lt. tshift_min) .or. (Tshift_temp .gt.tshift_max ) ) then
write(*,*) iwin, ' : rejection based on not satisfying TSHIFT_MIN < TSHIFT < TSHIFT_MAX'
write(*,*) 'Tshift : ',tshift_min , Tshift_temp, tshift_max
accept = .false.
endif
if ( accept .and. ( (dlnA_temp .lt. dlnA_min) .or. (dlnA_temp .gt. dlnA_max ) )) then
write(*,*) iwin, ' : rejection based on not satisfying DLNA_MIN < DLNA < DLNA_MAX'
write(*,*) 'dlnA : ', dlnA_min , dlnA_temp, dlnA_max
accept = .false.
endif
if( accept .and. (CC_temp .lt. CC_LIMIT(iM(iwin))) ) then
write(*,*) iwin, ' : rejection based on CC < CC_MIN'
write(*,*) 'CC : ', CC_temp, CC_LIMIT(iM(iwin))
accept = .false.
endif
! if the proto-window is acceptable, then accept it
if (accept) then
nwin_new = nwin_new + 1
iM_new(nwin_new) = iM(iwin)
iR_new(nwin_new) = iR(iwin)
iL_new(nwin_new) = iL(iwin)
CC_local(nwin_new) = CC_temp
Tshift_local(nwin_new) = Tshift_temp
dlnA_local(nwin_new) = dlnA_temp
endif
enddo
! update the iM, iR, iL arrays to contain only accepted proto-windows
nwin = nwin_new
iM(1:nwin) = iM_new(1:nwin_new)
iL(1:nwin) = iL_new(1:nwin_new)
iR(1:nwin) = iR_new(1:nwin_new)
if (DEBUG) write(*,*) 'DEBUG : fit criteria rejection retained ', nwin, ' acceptable windows'
!!$ ! CHT: this is done by display_windows_full
!!$ if(DEBUG) then
!!$ write(*,'(" DEBUG : iwin, iL, iM, iR, CC, Tshift, dlnA")')
!!$ do iwin = 1, nwin
!!$ write(*,'(" DEBUG : ",i2,1x,3(i4,1x),3(f6.2,1x))') iwin, iL(iwin), iM(iwin), iR(iwin), &
!!$ CC_local(iwin), Tshift_local(iwin), dlnA_local(iwin)
!!$ enddo
!!$ endif
end subroutine
subroutine sort_on_start_time(nwin,iM,iL,iR)
use seismo_variables
implicit none
integer, intent(inout) :: nwin
integer, dimension(*), intent(inout) :: iM, iL, iR
integer :: iwin, jwin, iL_early, iL_early_index
integer, dimension(NWINDOWS) :: iM_new, iL_new, iR_new, order_new
logical, dimension(NWINDOWS) :: dealt_with
! initialise dealt_with to false
dealt_with(1:nwin) = .false.
! iterate of windows
do iwin = 1, nwin