forked from bakerb845/fem25
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seismio.f
1141 lines (1135 loc) · 38.4 KB
/
seismio.f
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
SUBROUTINE WTEEST25(PROJNM, MFREQ,MREC,NFREQ, NREC,NSRC,
; FREQ8, UEST,VEST,WEST, IERR)
!
! Writes the estimates at receivers
!
! INPUT MEANING
! ----- -------
! FREQ8 frequency list
! MFREQ leading dimension for ?est
! MREC leading dimension for ?est
! NFREQ number of frequencies
! NREC number of recievers
! PROJNM project name
! UEST estimate wavefield in u
! VEST estimate wavefield in v
! WEST estimate waveifeld in w
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
!
!.... variable declarations
CHARACTER(*), INTENT(IN) :: PROJNM
COMPLEX*8, INTENT(IN) :: UEST(MFREQ,MREC,*), VEST(MFREQ,MREC,*),
; WEST(MFREQ,MREC,*)
REAL*8, INTENT(IN) :: FREQ8(NFREQ)
INTEGER*4, INTENT(IN) :: MFREQ,MREC,NFREQ, NREC,NSRC
INTEGER*4, INTENT(OUT) :: IERR
!.... local variables
CHARACTER(80) FLNAME
CHARACTER(4), ALLOCATABLE :: CDAT(:)
LOGICAL*4 LEX,LSWAP,LISDIR
CHARACTER(4) PACKI4, PACKR4
INTEGER*4 ENDIAN
PARAMETER(NDIM = 3, IUNIT = 45)
!
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... allocate space and pack header
NBYTES = 16 + 4*NFREQ + 8*NDIM*NFREQ*NSRC*NREC
LWORK = NBYTES/4
ALLOCATE(CDAT(LWORK))
CDAT(1) = PACKI4(LSWAP,NFREQ)
CDAT(2) = PACKI4(LSWAP,NSRC)
CDAT(3) = PACKI4(LSWAP,NREC)
CDAT(4) = PACKI4(LSWAP,NDIM)
!
!.... pack data
INDX = 5
DO 1 IFREQ=1,NFREQ
CDAT(INDX) = PACKR4(LSWAP,SNGL(FREQ8(IFREQ)))
INDX = INDX + 1
DO 2 ISRC=1,NSRC
DO 3 IREC=1,NREC
CDAT(INDX) = PACKR4(LSWAP,REAL(UEST(IFREQ,IREC,ISRC)))
INDX = INDX + 1
CDAT(INDX) = PACKR4(LSWAP,IMAG(UEST(IFREQ,IREC,ISRC)))
INDX = INDX + 1
CDAT(INDX) = PACKR4(LSWAP,REAL(VEST(IFREQ,IREC,ISRC)))
INDX = INDX + 1
CDAT(INDX) = PACKR4(LSWAP,IMAG(VEST(IFREQ,IREC,ISRC)))
INDX = INDX + 1
CDAT(INDX) = PACKR4(LSWAP,REAL(WEST(IFREQ,IREC,ISRC)))
INDX = INDX + 1
CDAT(INDX) = PACKR4(LSWAP,IMAG(WEST(IFREQ,IREC,ISRC)))
INDX = INDX + 1
3 CONTINUE !loop on receivers
2 CONTINUE !loop on sources
1 CONTINUE !loop on frequencies
INDX = INDX - 1
IF (INDX*4.NE.NBYTES) THEN
WRITE(*,*) 'wteest25: Error this file is the wrong size'
IERR = 1
ENDIF
!
!.... file handling
LEX = LISDIR('./dest')
!#ifdef INTEL
! INQUIRE(DIRECTORY='./dest',EXIST=LEX)
!#else
! INQUIRE(FILE='./dest',EXIST=LEX)
!#endif
IF (.NOT.LEX) CALL SYSTEM('mkdir ./dest')
FLNAME(1:80) = ' '
FLNAME = './dest/'//TRIM(PROJNM)//'.edest'
FLNAME = ADJUSTL(FLNAME)
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; STATUS='REPLACE',ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
WRITE(IUNIT,REC=1,IOSTAT=IERR) (CDAT(J),J=1,LWORK)
CLOSE(IUNIT)
DEALLOCATE(CDAT)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE RDTOBS_HD(PROJNM, NDIM,NFREQ,NREC,NSRC, IERR)
!
! Reads the frequency domain observation headers
!
! INPUT MEANING
! ----- -------
! PROJNM project name
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! NDIM number of componenets in file
! NFREQ number of frequencies in file
! NREC number of receivers in file
! NSRC number of sources in file
!
!.... variable declarations
CHARACTER(*), INTENT(IN) :: PROJNM
INTEGER*4, INTENT(OUT) :: NDIM,NFREQ,NREC,NSRC, IERR
!.... local variables
CHARACTER(1), ALLOCATABLE :: CDAT(:)
CHARACTER(80) FLNAME
LOGICAL*4 LEX, LSWAP
PARAMETER(IUNIT = 44)
INTEGER*4 UNPACKI4, ENDIAN
PARAMETER(TOL = 1.11E-5)
!
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... file detection
FLNAME(1:80) = ' '
FLNAME = './tobs/'//TRIM(PROJNM)//'.etobs'
FLNAME = ADJUSTL(FLNAME)
INQUIRE(FILE=TRIM(FLNAME),EXIST=LEX)
IF (.NOT.LEX) THEN
WRITE(*,*) 'rdtobs25_hd: Error cannot detect observation file'
IERR = 1
RETURN
ENDIF
!
!.... open and read header
NBYTES = 16
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
ALLOCATE(CDAT(NBYTES))
READ(IUNIT,REC=1,IOSTAT=IERR) (CDAT(J),J=1,NBYTES)
CLOSE(IUNIT)
IF (IERR.NE.0) THEN
NFREQ = 0
NSRC = 0
NREC = 0
NDIM = 0
GOTO 500
ENDIF
!
!.... unpack the header
NFREQ = UNPACKI4(LSWAP,CDAT( 1:4))
NSRC = UNPACKI4(LSWAP,CDAT( 5:8))
NREC = UNPACKI4(LSWAP,CDAT( 9:12))
NDIM = UNPACKI4(LSWAP,CDAT(13:16))
500 CONTINUE
DEALLOCATE(CDAT)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE RDTOBS_SB_HD(PROJNM, NDIM,NFREQ_SRF,NFREQ_BDY,
; NREC,NSRC_SRF,NSRC_BDY, IERR)
!
! Reads the frequency domain observation headers
!
! INPUT MEANING
! ----- -------
! PROJNM project name
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! NDIM number of componenets in file
! NFREQ_BDY number of body wave frequencies in file
! NFREQ_SRF number of surface wave frequencies in file
! NREC number of receivers in file
! NSRC_BDY number of body wave sources
! NSRC_SRF number of surface wave sources
!
!.... variable declarations
CHARACTER(*), INTENT(IN) :: PROJNM
INTEGER*4, INTENT(OUT) :: NDIM,NFREQ_SRF,NFREQ_BDY,
; NREC,NSRC_SRF,NSRC_BDY, IERR
!.... local variables
CHARACTER(1), ALLOCATABLE :: CDAT(:)
CHARACTER(80) FLNAME
LOGICAL*4 LEX, LSWAP
PARAMETER(IUNIT = 44)
INTEGER*4 UNPACKI4, ENDIAN
PARAMETER(TOL = 1.11E-5)
!
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... file detection
FLNAME(1:80) = ' '
FLNAME = './tobs/'//TRIM(PROJNM)//'.sb_etobs'
FLNAME = ADJUSTL(FLNAME)
INQUIRE(FILE=FLNAME,EXIST=LEX)
IF (.NOT.LEX) THEN
WRITE(*,*) 'rdtobs25_hd: Error cannot detect observation file'
IERR = 1
RETURN
ENDIF
!
!.... open and read header
NBYTES = 24
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
ALLOCATE(CDAT(NBYTES))
READ(IUNIT,REC=1,IOSTAT=IERR) (CDAT(J),J=1,NBYTES)
CLOSE(IUNIT)
IF (IERR.NE.0) THEN
NFREQ_SRF = 0
NFREQ_BDY = 0
NSRC_SRF = 0
NSRC_BDY = 0
NREC = 0
NDIM = 0
GOTO 500
ENDIF
!
!.... unpack the header
NFREQ_SRF = UNPACKI4(LSWAP,CDAT( 1:4))
NFREQ_BDY = UNPACKI4(LSWAP,CDAT( 5:8))
NSRC_SRF = UNPACKI4(LSWAP,CDAT( 9:12))
NSRC_BDY = UNPACKI4(LSWAP,CDAT(13:16))
NREC = UNPACKI4(LSWAP,CDAT(17:20))
NDIM = UNPACKI4(LSWAP,CDAT(21:24))
500 CONTINUE
DEALLOCATE(CDAT)
RETURN
END
! !
!======================================================================!
! !
!
SUBROUTINE RDTOBS25(PROJNM, MDIM,MFREQL,MREC,NFREQL, NDIM,NREC,
; NSRC, LUNWRAP, FREQL, WGHT,TOBS, IERR)
!
! Reads the frequency domain observations frequencies FREQL
! and the corresponding data weights
!
! INPUT MEANING
! ----- -------
! FREQL frequency list
! LUNWRAP True -> unwrapping phase so TOBS will be stored
! (magnitude,phase)
! FALSE -> TOBS stored as a complex number
! MFREQL leading dimension for ?est
! MREC leading dimension for ?est
! NDIM number of spatial dimensions expected, should be 3
! NFREQL number of frequencies in list
! NREC number of recievers we are expecting
! NSRC number of sources we are expecting
! PROJNM project name
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! TOBS (N,E,Vertical) observation at receivers
! WGHT (N,E,Vertical) weights on observations
!
!.... variable declarations
CHARACTER(*), INTENT(IN) :: PROJNM
REAL*8, INTENT(IN) :: FREQL(NFREQL)
INTEGER*4, INTENT(IN) :: MFREQL,MREC,NFREQL, NDIM,NREC,NSRC
LOGICAL*4, INTENT(IN) :: LUNWRAP
COMPLEX*8, INTENT(OUT) :: TOBS(MDIM,MFREQL,MREC,*)
REAL*4, INTENT(OUT) :: WGHT(MDIM,MFREQL,MREC,*)
INTEGER*4, INTENT(OUT) :: IERR
!.... local variables
CHARACTER(1), ALLOCATABLE :: CDAT(:)
CHARACTER(80) FLNAME
COMPLEX*8 CARG
REAL*4 R1, Q1, XMAG, PHASE, WRAPPH
LOGICAL*4 LSWAP, LEX
PARAMETER(IUNIT = 44)
REAL*4 UNPACKR4
INTEGER*4 UNPACKI4, ENDIAN
PARAMETER(TOL = 1.11E-5)
!
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... file detection
FLNAME(1:80) = ' '
FLNAME = './tobs/'//TRIM(PROJNM)//'.etobs'
FLNAME = ADJUSTL(FLNAME)
!
!.... open and read
INQUIRE(FILE=TRIM(FLNAME),EXIST=LEX,SIZE=NBYTES)
IF (.NOT.LEX) THEN
IERR = 1
WRITE(*,*) 'rdtobs: Error file does not exist',TRIM(FLNAME)
RETURN
ENDIF
! CALL STAT(TRIM(FLNAME),INFO)
! NBYTES = INFO(8)
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
ALLOCATE(CDAT(NBYTES))
READ(IUNIT,REC=1,IOSTAT=IERR) (CDAT(I),I=1,NBYTES)
CLOSE(IUNIT)
IF (IERR.NE.0) RETURN
!
!.... unpack the header
NFREQIN = UNPACKI4(LSWAP,CDAT( 1:4))
NSRCIN = UNPACKI4(LSWAP,CDAT( 5:8))
NRECIN = UNPACKI4(LSWAP,CDAT( 9:12))
NDIMIN = UNPACKI4(LSWAP,CDAT(13:16))
ITYPE = UNPACKI4(LSWAP,CDAT(17:20))
IF (NRECIN.NE.NREC)
;WRITE(*,*) 'rdtobs25: Warning sources do not match'
IF (NSRCIN.NE.NSRC)
;WRITE(*,*) 'rdtobs25: Warning sources do not match'
IF (NDIMIN.NE.NDIM) THEN
WRITE(*,*) 'rdtobs25: Error dimensions do not match'
IERR = 1
RETURN
ENDIF
NADV = 36*NSRCIN*NRECIN
!
!.... search for frequency in frequency list
DO 1 IFREQL=1,NFREQL
INDX = 20 !length of header
DO 2 JFREQL=1,NFREQIN
FREQIN = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
IF (ABS(FREQIN - SNGL(FREQL(IFREQL))).LT.TOL) THEN
GOTO 20
ELSE
INDX = INDX + NADV
ENDIF
2 CONTINUE
WRITE(*,*) 'rdtobs25: Error could not locate frequency'
IERR = 1
RETURN
20 CONTINUE !got it
DO 3 ISRC=1,NSRCIN
DO 4 IREC=1,NRECIN
DO 5 I=1,NDIMIN
W1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
R1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
Q1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
IF (R1.EQ.0.0 .AND. Q1.EQ.0.0 .AND. W1.NE.0.0) THEN
WRITE(*,*) 'rdtobs25: Warning null obs has weight'
W1 = 0.0
ENDIF
IF (.NOT.LUNWRAP) THEN !no phase unwrapping
IF (ITYPE.EQ.1) THEN !input stored mag,phase
CARG = CMPLX(R1,0.0)*CEXP(CMPLX(0.0,Q1))
R1 = REAL(CARG)
Q1 = IMAG(CARG)
ENDIF
ELSE !i am unwrapping the phase
IF (ITYPE.EQ.0) THEN !input stored complex
XMAG = SQRT(R1**2 + Q1**2)
PHASE = ATAN2(Q1,R1)
PHASE = WRAPPH(0,PHASE)
R1 = XMAG
Q1 = PHASE
ENDIF
ENDIF
TOBS(I,IFREQL,IREC,ISRC) = CMPLX(R1,Q1)
WGHT(I,IFREQL,IREC,ISRC) = W1
5 CONTINUE
4 CONTINUE !loop on recievers
3 CONTINUE !looop on sources
1 CONTINUE !loop on frequency list
DEALLOCATE(CDAT)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE RDTOBS_SB25(PROJNM, RCV,SRC,FRQ,INV, IERR)
!
! Reads the observations for surface and body waves. If we are
! windowing surface waves or body waves we need all the observations
! for STF and RRF updates
!
! INPUT MEANING
! ----- -------
! LINVF True -> frequency is an inversion frequency
! LUNWRAP True -> observations are (mag,phase)
! MFREQ_INV leading dimension
! NFREQ number of frequencies to model (surface+body)
! NFREQ_BDY number of body wave frequencies to model
! NFREQ_SRF number of surface wave frequencies to model
! NFREQ_BDY_INV number of body wave inversion frequencies
! NFREQ_SRF_INV number of surface wave inversion frequencies
! NREC number of receivers
! NSRC_BDY number of body wave sources
! NSRC_SRF number of surface wave sources
! PROJNM project name
! SRCTYP source type surface or body wave
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! TOBS observations
! WGHT weights on observations
!
!.... variable declarations
IMPLICIT NONE
INCLUDE 'fwd_struc.h'
TYPE (RECV_INFO) RCV
TYPE (SRC_INFO) SRC
TYPE (FRQ_INFO) FRQ
TYPE (INV_INFO) INV
CHARACTER(*), INTENT(IN) :: PROJNM
! INTEGER*4, INTENT(IN) :: NFREQ_OBS
INTEGER*4, INTENT(OUT) :: IERR
!.... local variables
CHARACTER(1), ALLOCATABLE :: CDAT(:)
CHARACTER(80) FLNAME
COMPLEX*8 CARG
REAL*4 UNPACKR4, XMAG, PHASE, W1, R1, Q1, TOL, FREQIN, WRAPPH
INTEGER*4 UNPACKI4, ENDIAN, NFREQ_SRF_IN,
; NFREQ_BDY_IN, NSRC_SRF_IN, NSRC_BDY_IN, NRECIN, NDIMIN,
; ITYPE, JFREQL, IFREQ, JFREQ, IOFF, ISRC, IREC,
; I, IUNIT, INDX, NADV, NBYTES, MYEND
LOGICAL*4 LSWAP, LEX
PARAMETER(IUNIT = 65)
PARAMETER(TOL = 2.22E-5)
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... file detection
FLNAME(1:80) = ' '
FLNAME = './tobs/'//TRIM(PROJNM)//'.sb_etobs'
FLNAME = ADJUSTL(FLNAME)
INQUIRE(FILE=TRIM(FLNAME),EXIST=LEX,SIZE=NBYTES)
IF (.NOT.LEX) THEN
WRITE(*,*) 'rdtobs_sb25: Observation file does not exist!'
IERR = 1
RETURN
ENDIF
!
!.... open and read
! CALL STAT(TRIM(FLNAME),INFO)
! NBYTES = INFO(8)
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
ALLOCATE(CDAT(NBYTES))
READ(IUNIT,REC=1,IOSTAT=IERR) (CDAT(I),I=1,NBYTES)
CLOSE(IUNIT)
IF (IERR.NE.0) RETURN
!
!.... unpack the header
NFREQ_SRF_IN = UNPACKI4(LSWAP,CDAT( 1:4))
NFREQ_BDY_IN = UNPACKI4(LSWAP,CDAT( 5:8))
NSRC_SRF_IN = UNPACKI4(LSWAP,CDAT( 9:12))
NSRC_BDY_IN = UNPACKI4(LSWAP,CDAT(13:16))
NRECIN = UNPACKI4(LSWAP,CDAT(17:20))
NDIMIN = UNPACKI4(LSWAP,CDAT(21:24))
ITYPE = UNPACKI4(LSWAP,CDAT(25:28))
IF (NRECIN.NE.rcv%NREC) THEN
WRITE(*,*) 'rdtobs_sb25: Error receiver mismatch'
IERR = 1
ENDIF
IF (NSRC_SRF_IN.NE.src%NSRC_SRF) THEN
WRITE(*,*) 'rdtobs_sb25: Error surface source mismatch'
IERR = 1
ENDIF
IF (NSRC_BDY_IN.NE.src%NSRC_BDY) THEN
WRITE(*,*) 'rdtobs_sb25: Error body source mismatch!'
IERR = 1
ENDIF
IF (NDIMIN.NE.NDIM) THEN
WRITE(*,*) 'rdtobs_sb25: Error dimensions do not match'
IERR = 1
ENDIF
IF (IERR.NE.0) RETURN
IOFF = 28
NADV = 4*3*NDIM*NSRC_SRF_IN*NRECIN + 4*1
!
!.... read the surface waves
DO 1 IFREQ=1,frq%NFREQ_SRF
INDX = IOFF
DO 2 JFREQ=1,NFREQ_SRF_IN
JFREQL = JFREQ
FREQIN = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
IF (ABS(FREQIN - SNGL(frq%FREQ(IFREQ))).LT.TOL) THEN
GOTO 20
ELSE
INDX = INDX + NADV
ENDIF
2 CONTINUE
WRITE(*,*) 'rdtobs_sb25: Error couldnt find surface wave freq'
IERR = 1
RETURN
20 CONTINUE
INDX = INDX + 4
DO 3 ISRC=1,src%NSRC
IF (src%SRCTYP(ISRC)(1:1).EQ.'S') THEN
DO 4 IREC=1,rcv%NREC
DO 5 I=1,NDIM
W1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
R1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
Q1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
IF (.NOT.inv%LUNWRAP) THEN !no phase unwrapping
IF (ITYPE.EQ.1) THEN !input stored mag,phase
CARG = CMPLX(R1,0.0)*CEXP(CMPLX(0.0,Q1))
R1 = REAL(CARG)
Q1 = IMAG(CARG)
ENDIF
ELSE !i am unwrapping the phase
IF (ITYPE.EQ.0) THEN !input stored complex
XMAG = SQRT(R1**2 + Q1**2)
PHASE = ATAN2(Q1,R1)
PHASE = WRAPPH(0,PHASE)
R1 = XMAG
Q1 = PHASE
ENDIF
ENDIF
JFREQ = IFREQ
inv%OBS(I,JFREQ,IREC,ISRC) = CMPLX(R1,Q1)
inv%WGHTS(I,JFREQ,IREC,ISRC) = W1
5 CONTINUE !Loop on components
4 CONTINUE !Loop on receivers
ENDIF !end check on source type
3 CONTINUE
1 CONTINUE !loop on types
!
!.... repeat for body waves
IOFF = 28
; + 4*3*NDIM*NSRC_SRF_IN*NRECIN*NFREQ_SRF_IN
; + 4*1*NFREQ_SRF_IN
NADV = 4*3*NDIM*NSRC_BDY_IN*NRECIN + 4*1
DO 11 IFREQ=1,frq%NFREQ_BDY
INDX = IOFF
DO 12 JFREQ=1,NFREQ_BDY_IN
JFREQL = frq%NFREQ_SRF + JFREQ
FREQIN = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
IF (ABS(FREQIN
; -SNGL(frq%FREQ(frq%NFREQ_SRF+IFREQ))).LT.TOL) THEN
GOTO 25
ELSE
INDX = INDX + NADV
ENDIF
12 CONTINUE
WRITE(*,*) 'rdtobs_sb25: Couldnt find body wave frequency!'
IERR = 1
RETURN
25 CONTINUE
INDX = INDX + 4
DO 13 ISRC=1,src%NSRC
IF (src%SRCTYP(ISRC)(1:1).EQ.'P') THEN
DO 14 IREC=1,rcv%NREC
DO 15 I=1,NDIM
W1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
R1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
Q1 = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4
IF (.NOT.inv%LUNWRAP) THEN !no phase unwrapping
IF (ITYPE.EQ.1) THEN !input stored mag,phase
CARG = CMPLX(R1,0.0)*CEXP(CMPLX(0.0,Q1))
R1 = REAL(CARG)
Q1 = IMAG(CARG)
ENDIF
ELSE !i am unwrapping the phase
IF (ITYPE.EQ.0) THEN !input stored complex
XMAG = SQRT(R1**2 + Q1**2)
PHASE = ATAN2(Q1,R1)
PHASE = WRAPPH(0,PHASE)
R1 = XMAG
Q1 = PHASE
ENDIF
ENDIF
JFREQ = IFREQ + frq%NFREQ_SRF
inv%OBS(I,JFREQ,IREC,ISRC) = CMPLX(R1,Q1)
inv%WGHTS(I,JFREQ,IREC,ISRC) = W1
15 CONTINUE
14 CONTINUE
ENDIF !end check on source type
13 CONTINUE
11 CONTINUE
DEALLOCATE(CDAT)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE RDWIN(PROJNM, NREC,NSRC, SRCTYP, WIN, IERR)
!
! Reads the windows chopping down the synthetics
!
! INPUT MEANING
! ----- -------
! NREC number of receivers
! NSRC number of sources
! SRCTYP source type
! PROJNM project name
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! IWNDO window type
! DT_BDY sampling period (s) for body waves
! DT_SRF sampling period (s) for surfae waves
! LWNDO_BDY True -> windowing body wave synthetics
! LWNDO_SRF True -> windowing surface wave synthetics
! NSAMP_BDY number of samples in body wave synthetics
! NSAMP_SRF number of samples in surface wave synthetics
! START_BDY start time for body waves (s)
! START_SRF start time for surface waves (s)
! TWIN start stop windows (s) for windows
!
!.... variable declarations
IMPLICIT NONE
INCLUDE 'fwd_struc.h'
TYPE (WIN_INFO) WIN
CHARACTER(*), INTENT(IN) :: PROJNM
CHARACTER(2), INTENT(IN) :: SRCTYP(NSRC)
INTEGER*4, INTENT(IN) :: NREC,NSRC
INTEGER*4, INTENT(OUT) :: IERR
!.... variable declarations
CHARACTER(80) FILENM
REAL*4 TE, TB, TOL, STARTT, DT
INTEGER*4 NREC_IN, NSRC_IN, IREC, ISRC, JREC, JSRC, IUNIT, NSAMP
LOGICAL*4 LEX
PARAMETER(IUNIT = 65)
PARAMETER(TOL = 1.11E-5)
!
!----------------------------------------------------------------------!
!
!.... initialize
win%DT_SRF = 0.0
win%DT_BDY = 0.0
win%START_SRF = 0.0
win%START_BDY = 0.0
win%NSAMP_SRF = 0
win%NSAMP_BDY = 0
win%NPCT_SRF = 0
win%NPCT_BDY = 0
win%LWNDO_SRF = .FALSE.
win%LWNDO_BDY = .FALSE.
!.... check if the file exists
IERR = 0
FILENM(1:80) = ' '
FILENM = './tobs/'//TRIM(PROJNM)//'.window'
FILENM = ADJUSTL(FILENM)
INQUIRE(FILE=TRIM(FILENM),EXIST=LEX)
IF (.NOT.LEX) THEN
WRITE(*,*) 'rdwin: Window file does not exist!'
IERR =-1
RETURN
ENDIF
OPEN(UNIT=IUNIT,FILE=TRIM(FILENM))
READ(IUNIT,*,END=60) NREC_IN,NSRC_IN,win%IWNDO,
; win%NPCT_SRF,win%NPCT_BDY
IF (NSRC_IN.NE.NSRC) THEN
WRITE(*,*) 'rdwin: Error source mismatch!'
IERR = 1
GOTO 60
ENDIF
IF (NREC_IN.NE.NREC) THEN
WRITE(*,*) 'rdwin: Error receiver mismatch!'
IERR = 1
GOTO 60
ENDIF
!
!.... loop on sources
DO 1 ISRC=1,NSRC
READ(IUNIT,*,END=60) NSAMP,DT,STARTT
!
!....... get sampling rates
IF (SRCTYP(ISRC)(1:1).EQ.'S') THEN
IF (win%DT_SRF.EQ.0.0) THEN
win%DT_SRF = DT
win%START_SRF = STARTT
win%NSAMP_SRF = NSAMP
ELSE
IF (ABS(win%DT_SRF - DT).GT.TOL) THEN
WRITE(*,*) 'rdwin: Warning dt variation'
win%DT_SRF = MAX(DT,win%DT_SRF) !get away from nyq
ENDIF
ENDIF
ELSE !body waves
IF (win%DT_BDY.EQ.0.0) THEN
win%DT_BDY = DT
win%START_BDY = STARTT
win%NSAMP_BDY = NSAMP
ELSE
IF (ABS(win%DT_BDY - DT).GT.TOL) THEN
WRITE(*,*) 'rdwin: Warning dt variation'
win%DT_BDY = MAX(DT,win%DT_BDY) !get away from nyq
ENDIF
ENDIF
ENDIF
!
!....... read windows for receivers
DO 2 IREC=1,NREC
win%TWIN(IREC,ISRC,1) = 0.0
win%TWIN(IREC,ISRC,2) = 0.0
READ(IUNIT,*,END=60) JREC,JSRC,TB,TE
IF (JSRC.NE.ISRC) THEN
WRITE(*,*) 'rdwin: Warning jsrc /= isrc'
IERR = 1
GOTO 60
ENDIF
IF (JREC.NE.IREC) THEN
WRITE(*,*) 'rdwin: Warning jrec /= irec'
IERR = 1
GOTO 60
ENDIF
win%TWIN(IREC,ISRC,1) = TB
win%TWIN(IREC,ISRC,2) = TE
IF (ABS(TE - TB).LT.TOL) THEN
win%TWIN(IREC,ISRC,1) = 0.0
win%TWIN(IREC,ISRC,2) = 0.0
ENDIF
IF (win%TWIN(IREC,ISRC,1).GT.0.0 .AND.
; win%TWIN(IREC,ISRC,2).GT.0.0) THEN
IF (SRCTYP(ISRC)(1:1).EQ.'S') THEN
win%LWNDO_SRF = .TRUE.
ELSE
win%LWNDO_BDY = .TRUE.
ENDIF
ENDIF
2 CONTINUE
READ(IUNIT,*,END=60) !blank line
1 CONTINUE !loop on frequencies
CLOSE(IUNIT)
!
!.... review
IF (win%LWNDO_SRF)
;WRITE(*,905) win%NSAMP_SRF,win%DT_SRF,win%START_SRF
IF (win%LWNDO_BDY)
;WRITE(*,906) win%NSAMP_BDY,win%DT_BDY,win%START_BDY
905 FORMAT(' rdwin: Samples in surface wave synthetics:',I6,/,
; ' At sampling rate:',F12.4,/,
; ' With start time:',F8.3,/)
906 FORMAT(' rdwin: Samples in body wave synthetics:',I6,/,
; ' At sampling rate:',F12.4,/,
; ' With start time:',F8.3,/)
RETURN
60 CONTINUE
CLOSE(IUNIT)
WRITE(*,*) 'rdwin: Premature end of window file!'
IERR = 1
CLOSE(60)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE RDEEST_HD(PROJNM, NDIM,NFREQ,NREC,NSRC, IERR)
!
! Reads the frequency response estimate seismogram headers
!
! INPUT MEANING
! ----- -------
! PROJNM project name
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! NDIM number of componenets in file
! NFREQ number of frequencies in file
! NREC number of receivers in file
! NSRC number of sources in file
!
!.... variable declarations
CHARACTER(*), INTENT(IN) :: PROJNM
INTEGER*4, INTENT(OUT) :: NDIM,NFREQ,NREC,NSRC, IERR
!.... local variables
CHARACTER(1), ALLOCATABLE :: CDAT(:)
CHARACTER(80) FLNAME
LOGICAL*4 LEX, LSWAP
PARAMETER(IUNIT = 44)
INTEGER*4 UNPACKI4, ENDIAN
PARAMETER(TOL = 1.11E-5)
!
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... file detection
FLNAME(1:80) = ' '
FLNAME = './dest/'//TRIM(PROJNM)//'.edest'
FLNAME = ADJUSTL(FLNAME)
INQUIRE(FILE=FLNAME,EXIST=LEX)
IF (.NOT.LEX) THEN
WRITE(*,*) 'rdeest25_hd: Error cannot detect estimate file'
IERR = 1
RETURN
ENDIF
!
!.... open and read header
NBYTES = 16
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
ALLOCATE(CDAT(NBYTES))
READ(IUNIT,REC=1,IOSTAT=IERR) (CDAT(J),J=1,NBYTES)
CLOSE(IUNIT)
IF (IERR.NE.0) THEN
NFREQ = 0
NSRC = 0
NREC = 0
NDIM = 0
GOTO 500
ENDIF
!
!.... unpack the header
NFREQ = UNPACKI4(LSWAP,CDAT( 1:4))
NSRC = UNPACKI4(LSWAP,CDAT( 5:8))
NREC = UNPACKI4(LSWAP,CDAT( 9:12))
NDIM = UNPACKI4(LSWAP,CDAT(13:16))
500 CONTINUE
DEALLOCATE(CDAT)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE RDEEST_FR(PROJNM, NFREQ,FREQ4,IERR)
!
! Reads frequency list from frequency response estimate file
!
! INPUT MEANING
! ----- -------
! NFREQ number of frequencies
! PROJNM project name
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! FREQ4 frequency list from estimate file
!
!.... variable declarations
CHARACTER(*), INTENT(IN) :: PROJNM
INTEGER*4, INTENT(IN) :: NFREQ
REAL*4, INTENT(OUT) :: FREQ4(NFREQ)
INTEGER*4, INTENT(OUT) :: IERR
!.... local variables
CHARACTER(1), ALLOCATABLE :: CDAT(:)
CHARACTER(80) FLNAME
LOGICAL*4 LSWAP, LEX
PARAMETER(IUNIT = 44)
REAL*4 UNPACKR4
INTEGER*4 UNPACKI4, ENDIAN
PARAMETER(TOL = 1.11E-5)
!
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... file detection
FLNAME(1:80) = ' '
FLNAME = './dest/'//TRIM(PROJNM)//'.edest'
FLNAME = ADJUSTL(FLNAME)
!
!.... open and read
INQUIRE(FILE=TRIM(FLNAME),EXIST=LEX,SIZE=NBYTES)
IF (.NOT.LEX) THEN
IERR = 1
WRITE(*,*) 'rdeest_fr: Error file does not exist',TRIM(FLNAME)
RETURN
ENDIF
! CALL STAT(TRIM(FLNAME),INFO)
! NBYTES = INFO(8)
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
ALLOCATE(CDAT(NBYTES))
READ(IUNIT,REC=1,IOSTAT=IERR) (CDAT(I),I=1,NBYTES)
CLOSE(IUNIT)
IF (IERR.NE.0) RETURN
!
!.... unpack the header
NFREQIN = UNPACKI4(LSWAP,CDAT( 1:4))
NSRCIN = UNPACKI4(LSWAP,CDAT( 5:8))
NRECIN = UNPACKI4(LSWAP,CDAT( 9:12))
NDIMIN = UNPACKI4(LSWAP,CDAT(13:16))
NADV = 2*4*NDIMIN*NSRCIN*NRECIN
!
!.... search for frequency in frequency list
INDX = 16
DO 1 IFREQL=1,NFREQIN
FREQ4(IFREQL) = UNPACKR4(LSWAP,CDAT(INDX+1:INDX+4))
INDX = INDX + 4 + NADV
1 CONTINUE
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE RDEEST25(PROJNM, MFREQL,MREC,NFREQL, NDIM,NREC,NSRC,
; FREQL, UEST,VEST,WEST, IERR)
!
! Reads the frequency domain estimates at seismogram stations
! for plotting by a xwiggle25
!
! INPUT MEANING
! ----- -------
! FREQL frequency list
! MFREQL leading dimension for ?est
! MREC leading dimension for ?est
! NDIM number of spatial dimensions expected, should be 3
! NFREQL number of frequencies in list
! NREC number of recievers we are expecting
! NSRC number of sources we are expecting
! PROJNM project name
!
! OUTPUT MEANING
! ------ -------
! IERR error flag
! UEST u estimate at receivers
! VEST v estimate at receivers
! WEST w estimate at receivers
!
!.... variable declarations
CHARACTER(*), INTENT(IN) :: PROJNM
REAL*4, INTENT(IN) :: FREQL(NFREQL)
INTEGER*4, INTENT(IN) :: MFREQL,MREC,NFREQL, NDIM,NREC,NSRC
COMPLEX*8, INTENT(OUT) :: UEST(MFREQL,MREC,*),
; VEST(MFREQL,MREC,*), WEST(MFREQL,MREC,*)
INTEGER*4, INTENT(OUT) :: IERR
!.... local variables
CHARACTER(1), ALLOCATABLE :: CDAT(:)
CHARACTER(80) FLNAME
! INTEGER*4 INFO(13)
LOGICAL*4 LSWAP, LEX
PARAMETER(IUNIT = 44)
REAL*4 UNPACKR4
INTEGER*4 UNPACKI4, ENDIAN
PARAMETER(TOL = 1.11E-5)
!
!----------------------------------------------------------------------!
!
!.... detect endianness
IERR = 0
LSWAP = .FALSE.
MYEND = ENDIAN()
IF (MYEND.NE.0) LSWAP = .TRUE.
!.... file detection
FLNAME(1:80) = ' '
FLNAME = './dest/'//TRIM(PROJNM)//'.edest'
FLNAME = ADJUSTL(FLNAME)
!
!.... open and read
INQUIRE(FILE=TRIM(FLNAME),EXIST=LEX,SIZE=NBYTES)
IF (.NOT.LEX) THEN
IERR = 1
WRITE(*,*) 'rdeest25: Error file does not exist',TRIM(FLNAME)
RETURN
ENDIF
! CALL STAT(TRIM(FLNAME),INFO)
! NBYTES = INFO(8)
OPEN(UNIT=IUNIT,FILE=TRIM(FLNAME),FORM='UNFORMATTED',
; ACCESS='DIRECT',RECL=NBYTES,IOSTAT=IERR)
ALLOCATE(CDAT(NBYTES))
READ(IUNIT,REC=1,IOSTAT=IERR) (CDAT(I),I=1,NBYTES)
CLOSE(IUNIT)