-
Notifications
You must be signed in to change notification settings - Fork 6
/
f90sac.F90
3213 lines (2883 loc) · 114 KB
/
f90sac.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
!===============================================================================
!-------------------------------------------------------------------------------
!
! Fortran 90/95 Source Code File
!
!-------------------------------------------------------------------------------
!===============================================================================
!
! PROGRAM : f90sac
! VERSION : 4.41
! CVS: $Revision: 1.26 $ $Date: 2008/10/17 10:52:56 $
!
! (C) James Wookey
! Department of Earth Sciences, University of Bristol
! Wills Memorial Building, Queen's Road, Bristol, BR8 1RJ, UK
! j.wookey@bristol.ac.uk
!
! (C) Andy Nowacki
! School of Earth and Environment, University of Leeds, Leeds, LS2 9JT, UK
! a.nowacki@leeds.ac.uk
!
!-------------------------------------------------------------------------------
!
! The module provides data structures and functions for reading,
! writing and handling SAC files in Fortran 90/95.
!
! Please report bugs/problems to email address above
!
! NOTE: This version of the code assumes IO filestream 99 is available
! for reading and writing.
!
!-------------------------------------------------------------------------------
!
! This software is distributed under the term of the BSD free software license.
!
! Copyright:
! (c) 2003-2008, James Wookey
! (c) 2015-, Andy Nowacki
!
! All rights reserved.
!
! * Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are
! met:
!
! * Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! * Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in the
! documentation and/or other materials provided with the distribution.
!
! * Neither the name of the copyright holder nor the names of its
! contributors may be used to endorse or promote products derived from
! this software without specific prior written permission.
!
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
! A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
! OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
! LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
! DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
! THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
! OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
!-------------------------------------------------------------------------------
! Changes log
!-------------------------------------------------------------------------------
!
! 2003-12-04 v0.9 * Incept date
! 2003-12-05 v1.0 * Added basic error checking to f90sac_readtrace
! and changed all subroutine names
! 2004-01-26 v1.0 * Added a white-noise function
! 2004-02-05 v1.0 * Added a windowing function
! 2004-02-19 v1.0 * Added an SAC x/y file data structure
! 2004-03-08 v1.0 * added subroutines to set event and station info
! 2004-03-09 v1.0 * organised error/warning reporting
! 2004-05-03 v2.0 * changed to f90 file
! 2004-05-26 v2.1 * updated the file definitions to standard f90/95
! 2004-07-02 v2.11 * minor comment fixes
! 2004-07-21 v2.2 * added a subroutine f90sac_rotate2d_rz
! 2004-09-08 v2.3 * added functions f90sac_resampleup,
! f90sac_cattraces
! 2004-10-22 v2.31 * included NR routines in the file for ease
! 2004-11-02 v2.32 * included option to suppress warnings
! 2005-01-11 v2.4 * included option for setting record length
! 2005-02-16 v2.5 * added function f90sac_jd2ymd
! 2005-02-16 v2.6 * change dateseed routine to use date_and_time
! for compatibility with more f90 compilers
! 2005-05-03 v2.61 * disabled free filestream search in read/write
! for compatibility with Mac OSX Darwin. Also
! disable deallocation of trace memory prior to
! read because of error message using XLF
! 2005-07-05 v2.62 * fixed spelling mistake (unused18)
! 2005-08-17 v2.63 * disable memory deallocation in newtrace
! 2005-08-18 v2.7 * added an attribute to the SAC trace type:
! iTraceActive. This is not input/output, but is set
! by the _newtrace, _destroytrace, and _readtrace. This
! is to deal with the allocation/deallocation problem
! 2006-01-16 v2.8 * added functions for getting and setting the SAC
! header by using header name strings. Also added
! routines for reading/writing the header of a file.
! 2006-06-05 v3.0 * redesign of I/O routines to incorporate byte-swapping
! see parameter f90sac_force_byteswap
! 2006-06-20 v3.1 * improved handling of random noise seed, added
! f90sac_init_random subroutine
! 2006-10-18 v3.11 * changes to f90sac_enumhdr
! 2007-02-21 v3.2 * added function f90sac_compare_origin_time
! 2007-02-22 v3.3 * changed from GPL to BSD license
! 2007-03-07 v3.31 * added f90sac_isBigEndian (for checking CPU type)
! 2007-03-12 v3.4 * changed memory allocation procedure.
! 2007-08-02 v3.41 * changed writetrace to destroy pre-existing file
! 2007-09-24 v3.42 * minor fixes to byteswapping routines
! 2007-10-03 v4.00 * Moved trace reading/writing to C for speed
! Only f90sac_writeheader remains unchanged
! 2007-12-05 v4.1 * Changed to a .F90 file to allow pre-processing
! Used this to add ppd to set endian behaviour.
! Also added an init routine, to set up IO.
! 2008-02-06 v4.2 * Moved non-C subroutines to preprocessor directives,
! added some tagging to allow easy removal of
! non-distribution routines
! 2008-02-15 v4.21 * Added a C based writeheader, completing the set.
! 2008-03-31 v4.22 * Added an optional force parameter to rotation
! routines
! 2008-03-31 v4.3 * Added two routines to generate covariance matrices
! 2008-08-26 v4.4 * Added a routine to suggest filenames for SAC data
! structures, based on header values. Also added a
! parameter to standardise filename lengths.
! 2008-10-17 v4.41 * Changed to allow variable length strings as filenames
! (with a maximum length, set by f90sac_fnlength)
! AJN:2011-03-09 * Cross-introduced f90sac_deletetrace from v4.43 of the
! f90sac_distrib.F90 version.
! AJN:2011-03-09 * Future changes in the source will be found in the git
! history in this repo. No new updates to this list
! will be made.
!===============================================================================
module f90sac ! Utility module for F90/95 for SAC files
!===============================================================================
implicit none
private
! ** DECLARE CONTAINED FUNCTIONS
public :: f90sac_cattraces
public :: f90sac_clonetrace
public :: f90sac_compare_origin_time
public :: f90sac_copytraceheader
public :: f90sac_covar2
public :: f90sac_covar3
public :: f90sac_dateseed
public :: f90sac_deletetrace
public :: f90sac_enumhdr
public :: f90sac_filename
public :: f90sac_get_file_endianness
public :: f90sac_getfhdr
public :: f90sac_getihdr
public :: f90sac_getkhdr
public :: f90sac_getlhdr
public :: f90sac_newtrace
public :: f90sac_orient2d
public :: f90sac_orth2d
public :: f90sac_readheader
public :: f90sac_readtrace
public :: f90sac_rotate2d
public :: f90sac_rotate2d_rz
public :: f90sac_setdate
public :: f90sac_setevent
public :: f90sac_setfhdr
public :: f90sac_setihdr
public :: f90sac_setkhdr
public :: f90sac_setlhdr
public :: f90sac_setstation
public :: f90sac_tshift
public :: f90sac_unwind
public :: f90sac_window
public :: f90sac_writeheader
public :: f90sac_writetrace
public :: f90sac_ymd2jd
! Filtering reoutines relying on libxapiir
#ifdef USE_XAPIIR
public :: f90sac_bandpass_bu, &
f90sac_highpass_bu, &
f90sac_lowpass_bu
#endif
!!!BEG_NONDIST
! ** The f90sac_addwnoise subroutine requires the Numerical Recipes function
! ** ran1. If this is not available, either comment the function
! ** out, or substitute an appropriate alternative.
! ** Similarly, f90sac_resampleup uses NR routines splint and spline.
public :: f90sac_init_random
public :: f90sac_addwnoise
public :: f90sac_resampleup
!!!END_NONDIST
! ** define a long (32 bit) integer and 32 bit real
integer, parameter, private :: int4 = selected_int_kind(9) ;
integer, parameter, private :: real4 = selected_real_kind(6,37) ;
integer, parameter, private :: real8 = selected_real_kind(15,307) ;
! ** define the record length in a sequential access file for a 32 bit number
! ** this is compiler dependent:
! IFORT/IFC Version >= 8.0 = 1 (or set flag -assume byterecl)
! IFC Version < v8.0 = 4
! Solaris F90 = 4
! g95/gfortran = 4
integer, parameter, private :: f90sac_32bit_record_length = 4 ;
! ** define the unit number to use for reading and writing (opened and closed
! ** within each call)
integer, parameter, private :: f90sac_iounit = 99 ;
! ** endian configuration
#ifdef FORCE_BIGENDIAN_SACFILES
character, parameter :: f90sac_endian_mode = 'b'
#else
character, parameter :: f90sac_endian_mode = 'n'
#endif
! ** Define the current SAC file structure version. This is used to determine
! endianness of files.
integer, parameter, private :: f90sac_current_nvhdr = 6
integer :: f90sac_init_flag ; ! This is set to a value of 51423
! when the initialisation is done
logical :: f90sac_force_byteswap ; ! This is now set by f90sac_init_io
! ** noise generator seed value
integer, private :: f90sac_random_seed ;
! ** standard filename length
integer, parameter :: f90sac_fnlength = 256 ;
!===============================================================================
! ** Define a specialised data structure for containing SAC files
!===============================================================================
type, public :: SACtrace
! ** Header floating point part
real(real4) :: delta,depmin,depmax,scale,odelta,b,e,o,a,internal0
real(real4) :: t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,f
real(real4) :: resp0,resp1,resp2,resp3,resp4,resp5
real(real4) :: resp6,resp7,resp8,resp9
real(real4) :: stla,stlo,stel,stdp,evla,evlo,evel,evdp,mag
real(real4) :: user0,user1,user2,user3,user4
real(real4) :: user5,user6,user7,user8,user9
real(real4) :: dist,az,baz,gcarc,internal1,internal2,depmen
real(real4) :: cmpaz,cmpinc
real(real4) :: xminimum,xmaximum,yminimum,ymaximum
real(real4) :: unused1,unused2,unused3,unused4
real(real4) :: unused5,unused6,unused7
! ** Header integer part
integer(int4) :: nzyear,nzjday,nzhour,nzmin,nzsec,nzmsec
integer(int4) :: nvhdr,norid,nevid,npts
integer(int4) :: internal3,nwfid,nxsize,nysize,unused8
integer(int4) :: iftype,idep,iztype,unused9
integer(int4) :: iinst,istreg,ievreg,ievtyp
integer(int4) :: iqual,isynth,imagtyp,imagsrc
integer(int4) :: unused10,unused11,unused12,unused13,unused14
integer(int4) :: unused15,unused16,unused17
! ** Header logical part (stored as integers)
integer(int4) :: leven,lpspol,lovrok,lcalda,unused18
! ** Header character part
character (len = 16) :: kevnm
character (len = 8) :: kstnm,khole,ko,ka
character (len = 8) :: kt0,kt1,kt2,kt3,kt4
character (len = 8) :: kt5,kt6,kt7,kt8,kt9
character (len = 8) :: kf,kuser0,kuser1,kuser2
character (len = 8) :: kcmpnm,knetwk,kdatrd,kinst
! ** the trace
real(real4), allocatable :: trace(:)
end type SACtrace
!===============================================================================
! ** Define a data structure for containing SAC xy files
!===============================================================================
type, public :: SACxy
! ** Header floating point part
real(real4) :: delta,depmin,depmax,scale,odelta,b,e,o,a,internal0
real(real4) :: t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,f
real(real4) :: resp0,resp1,resp2,resp3,resp4,resp5
real(real4) :: resp6,resp7,resp8,resp9
real(real4) :: stla,stlo,stel,stdp,evla,evlo,evel,evdp,mag
real(real4) :: user0,user1,user2,user3,user4
real(real4) :: user5,user6,user7,user8,user9
real(real4) :: dist,az,baz,gcarc,internal1,internal2,depmen
real(real4) :: cmpaz,cmpinc
real(real4) :: xminimum,xmaximum,yminimum,ymaximum
real(real4) :: unused1,unused2,unused3,unused4
real(real4) :: unused5,unused6,unused7
! ** Header integer part
integer(int4) :: nzyear,nzjday,nzhour,nzmin,nzsec,nzmsec
integer(int4) :: nvhdr,norid,nevid,npts
integer(int4) :: internal3,nwfid,nxsize,nysize,unused8
integer(int4) :: iftype,idep,iztype,unused9
integer(int4) :: iinst,istreg,ievreg,ievtyp
integer(int4) :: iqual,isynth,imagtyp,imagsrc
integer(int4) :: unused10,unused11,unused12,unused13,unused14
integer(int4) :: unused15,unused16,unused17
! ** Header logical part (stored as integers)
integer(int4) :: leven,lpspol,lovrok,lcalda,unused18
! ** Header character part
character (len = 16) :: kevnm
character (len = 8) :: kstnm,khole,ko,ka
character (len = 8) :: kt0,kt1,kt2,kt3,kt4
character (len = 8) :: kt5,kt6,kt7,kt8,kt9
character (len = 8) :: kf,kuser0,kuser1,kuser2
character (len = 8) :: kcmpnm,knetwk,kdatrd,kinst
! ** The trace
real(real4), allocatable :: x(:)
real(real4), allocatable :: y(:)
end type SACxy
! ** tolerance for the comparison of angles
real, parameter, public :: f90sac_angle_tolerance = 0.001
! ** NULL values set in SAC objects
real, parameter, public :: SAC_rnull = -12345.0
integer, parameter, public :: SAC_inull = -12345
integer, parameter, public :: SAC_lnull = -12345
character (len = 8), public :: SAC_cnull = '-12345'
! ** OPTIONAL suppression of warnings, set to 1 to supress
integer, public :: f90sac_suppress_warnings
!===============================================================================
!
! ** MODULE SUBROUTINES
!
!===============================================================================
CONTAINS
!===============================================================================
subroutine f90sac_io_init()
!===============================================================================
!
! Initialise SAC file IO configuration:
!
implicit none
if (f90sac_init_flag == 6514236) return ! configuration is already done
! ** configure endian behaviour. If forced big-endian, then swapping is
! required on little-endian machines.
if (f90sac_endian_mode == 'b') then
if(f90sac_isBigEndian()) then
f90sac_force_byteswap = .false.
else
f90sac_force_byteswap = .true.
endif
else
f90sac_force_byteswap = .false.
endif
f90sac_init_flag = 6514236 ! need only do this once
return
end subroutine f90sac_io_init
!===============================================================================
!===============================================================================
subroutine f90sac_filename(tr,iformat,fn)
!===============================================================================
!
! Suggest a filename for a SAC file based on specified format of the
! header values.
!
! Available formats are:
!
! iformat=1 : STNM.NW.CMP
! =2 : YYYYDDD.STNM.NW.CMP (*reference* time)
! =3 : YYYYDDD.HHMMSS.STNM.NW.CMP (*reference* time)
!
! Names (such as station or network name) which are longer than the fields
! above are truncated. Only the time information is checked.
! Note that the date/time used is the reference time which might be the
! zero, event or neither, depending on the values of the b and o headers.
!
implicit none
type (SACtrace) :: tr
character (len=f90sac_fnlength) :: fn
character (len=7) :: yyyyddd
character (len=6) :: hhmmss
character (len=4) :: stnm
character (len=3) :: cmp
character (len=2) :: nw
integer :: iformat
! ** blank out the string
fn(1:256) = ''
! ** construct all of the possible string parts first
stnm(1:4) = tr%kstnm(1:4)
nw(1:2) = tr%knetwk(1:2)
cmp(1:3) = tr%kcmpnm(1:3)
if (tr % nzyear == SAC_inull .or. tr % nzjday== SAC_inull) then
yyyyddd = '_______'
else
write(yyyyddd,'(i4.4,i3.3)') tr % nzyear, tr % nzjday
endif
if (tr % nzhour == SAC_inull .or. &
tr % nzmin == SAC_inull .or. &
tr % nzsec == SAC_inull) then
yyyyddd = '______'
else
write(hhmmss,'(3i2.2)') tr % nzhour, tr % nzmin, tr % nzsec
endif
if (iformat==0) then
fn = trim(stnm) // '.' // trim(cmp)
elseif (iformat==1) then
fn = trim(stnm) // '.' // trim(nw) // '.' // trim(cmp)
elseif (iformat==2) then
fn = trim(yyyyddd) // '.'
fn = trim(fn) // trim(stnm) // '.' // trim(nw) // '.' // trim(cmp)
elseif (iformat==3) then
fn = trim(yyyyddd) // '.' // trim(hhmmss) // '.'
fn = trim(fn) // trim(stnm) // '.' // trim(nw) // '.' // trim(cmp)
else
write(0,'(a)') &
'F90SAC_FILENAME: Error: Unsupported format code'
STOP
endif
return
end subroutine f90sac_filename
!===============================================================================
!===============================================================================
subroutine f90sac_covar2(t1,t2,cov)
!===============================================================================
!
! Generate the covariance matrix for 2 traces
!
implicit none
type (SACtrace) :: t1,t2
real :: cov(2,2)
integer :: i
! ** check that traces are the same length
if (t1 % npts /= t2 % npts) then
write(0,'(a)') &
'F90SAC_COVAR2: Error: Input traces are different lengths'
STOP
endif
! ** calculate covariance matrix
cov(:,:) = 0.0
do i=1,t1 % npts
cov(1,1) = cov(1,1) + t1%trace(i)**2.
cov(2,2) = cov(2,2) + t2%trace(i)**2
cov(1,2) = cov(1,2) + t1%trace(i)*t2%trace(i)
enddo
cov(2,1) = cov(1,2)
return
end subroutine f90sac_covar2
!===============================================================================
!===============================================================================
subroutine f90sac_covar3(t1,t2,t3,cov)
!===============================================================================
!
! Generate the covariance matrix for 3 traces
!
implicit none
type (SACtrace) :: t1,t2,t3
real :: cov(3,3)
real, allocatable :: m1(:,:),m2(:,:)
! ** check that traces are the same length
if (t1 % npts /= t2 % npts .or. &
t1 % npts /= t3 % npts .or. &
t2 % npts /= t3 % npts ) then
write(0,'(a)') &
'F90SAC_COVAR2: Error: Input traces are different lengths'
STOP
endif
allocate(m1(t1%npts,3))
allocate(m2(3,t1%npts))
m1(:,1) = t1%trace(:)
m1(:,2) = t2%trace(:)
m1(:,3) = t3%trace(:)
m2 = transpose(m1)
cov = matmul(m2,m1)
deallocate(m1)
deallocate(m2)
return
end subroutine f90sac_covar3
!===============================================================================
!===============================================================================
function f90sac_compare_origin_time(t1,t2)
!===============================================================================
!
! Compare origin times of two SAC traces (using nzyear,nzjday etc)
! returns:
! -1 = t1 is earlier
! 0 = same time
! 1 = t2 is earlier
!
implicit none
type (SACtrace) :: t1,t2
integer :: f90sac_compare_origin_time
!
f90sac_compare_origin_time = 0
if (t1%nzyear<t2%nzyear) then
f90sac_compare_origin_time=-1
return
elseif (t1%nzyear>t2%nzyear) then
f90sac_compare_origin_time=1
return
else
if (t1%nzjday<t2%nzjday) then
f90sac_compare_origin_time=-1
return
elseif (t1%nzjday>t2%nzjday) then
f90sac_compare_origin_time=1
return
else
if (t1%nzhour<t2%nzhour) then
f90sac_compare_origin_time=-1
return
elseif (t1%nzhour>t2%nzhour) then
f90sac_compare_origin_time=1
return
else
if (t1%nzmin<t2%nzmin) then
f90sac_compare_origin_time=-1
return
elseif (t1%nzmin>t2%nzmin) then
f90sac_compare_origin_time=1
return
else
if (t1%nzsec<t2%nzsec) then
f90sac_compare_origin_time=-1
return
elseif (t1%nzsec>t2%nzsec) then
f90sac_compare_origin_time=1
return
else
if (t1%nzmsec<t2%nzmsec) then
f90sac_compare_origin_time=-1
return
elseif (t1%nzmsec>t2%nzmsec) then
f90sac_compare_origin_time=1
return
else
f90sac_compare_origin_time=0
return
endif
endif
endif
endif
endif
endif
return
end function f90sac_compare_origin_time
!===============================================================================
!===============================================================================
subroutine f90sac_cattraces(t1,t2,tc)
!===============================================================================
!
! Concatenate two traces. t1 // t2 = tc. Header information is taken from
! t1, start time is from t1
!
implicit none
type (SACtrace) :: t1,t2,tc
integer :: npts_new
! ** check delta is the same for both traces.
if (t1 % delta /= t2 % delta) then
write(0,'(a)') &
'F90SAC_CATTRACES: Error: Traces have different deltas'
STOP
endif
npts_new = t1 % npts + t2 % npts
! ** make a new trace
call f90sac_newtrace(npts_new, t1 % delta, tc)
! ** copy the trace header to the new trace
call f90sac_copytraceheader(t1,tc)
tc % npts = npts_new
! ** set the new end time (if begin time is not null)
if (tc%b /= SAC_rnull) then
tc % e = tc % b + real(tc % npts) * tc % delta
endif
! ** copy the traces
tc % trace(1:t1 % npts) = t1 % trace(1:t1 % npts)
tc % trace(t1%npts + 1 : t1%npts + t2%npts) = t2%trace(1:t2 % npts)
return
end subroutine f90sac_cattraces
!===============================================================================
!===============================================================================
subroutine f90sac_setevent(trace,lat,lon,depth)
!===============================================================================
!
! Set event information in SAC object trace
!
implicit none
type (SACtrace) :: trace
real :: lat,lon,depth
trace % evla = lat
trace % evlo = lon
trace % evdp = depth
return
end subroutine f90sac_setevent
!===============================================================================
!===============================================================================
subroutine f90sac_setstation(trace,lat,lon,elevation)
!===============================================================================
!
! Set station information in SAC object trace
!
implicit none
type (SACtrace) :: trace
real :: lat,lon,elevation
trace % stla = lat
trace % stlo = lon
trace % stel = elevation
return
end subroutine f90sac_setstation
!===============================================================================
!===============================================================================
subroutine f90sac_window(tr_in,tr_out,t1,t2)
!===============================================================================
!
! Window the data in SAC object .between t1 and t2 and produce a new
! trace containing this data
!
implicit none
type (SACtrace) :: tr_in,tr_out
integer iwbeg,iwend,new_npts,istatus,i
real t1,t2
real new_b,new_e
! ** calculate indices of window
iwbeg = nint((t1-tr_in % b)/tr_in % delta)+1
iwend = nint((t2-tr_in % b)/tr_in % delta)+1
! ** Check these values
if (iwbeg <=0 .or. iwend > tr_in % npts) then
write(0,'(a)') &
'F90SAC_WINDOW: Error: Window limits out of range of data.'
STOP
endif
! ** Check these values
if (iwend <= iwbeg) then
write(0,'(a)') &
'F90SAC_WINDOW: Error: Resulting trace has no data points.'
STOP
endif
! ** calculate new header values
new_npts = iwend - iwbeg + 1
new_b = tr_in % b + real(iwbeg-1)*tr_in % delta
new_e = new_b + real(new_npts-1)*tr_in % delta
! ** create a new trace
call f90sac_clonetrace(tr_in,tr_out)
! ** deallocate it's trace memory, and reallocate the correct amount
deallocate (tr_out % trace, stat = istatus)
allocate (tr_out % trace(new_npts))
! ** set header values
tr_out % npts = new_npts
tr_out % b = new_b
tr_out % e = new_e
! ** copy the trace
do i = 1,new_npts
tr_out % trace(i) = tr_in % trace( iwbeg + (i-1) )
enddo
return
end subroutine f90sac_window
!===============================================================================
!===============================================================================
subroutine f90sac_setdate(trace,iyr,ijd,ihr,imi,ise,ims)
!===============================================================================
!
! Set the date and time in a SAC object
!
implicit none
type (SACtrace) :: trace
integer iyr,ijd,ihr,imi,ise,ims
trace % nzyear = iyr
trace % nzjday = ijd
trace % nzhour = ihr
trace % nzmin = imi
trace % nzsec = ise
trace % nzmsec = ims
return
end subroutine f90sac_setdate
!===============================================================================
!===============================================================================
subroutine f90sac_orient2d(t1,t2)
!===============================================================================
!
! take 2 traces in arbitrary (orthogonal) orientation and rotate to a
! north-east reference frame
!
! t1 : (I/O) SAC trace, on output this holds the north component
! t2 : (I/O) SAC trace, on output this holds the east component
!
implicit none
type (SACtrace) :: t1,t2
real cmpazdiff
! ** check that cmpaz is set
if (t1 % cmpaz == SAC_rnull .or. t2 % cmpaz == SAC_rnull) then
write(0,'(a)') &
'F90SAC_ORIENT2D: Error: CMPAZ Header is not set'
STOP
endif
! ** check for orthogonality
cmpazdiff = abs(t1 % cmpaz - t2 % cmpaz)
if ( .not.((abs(cmpazdiff-90.0) <= f90sac_angle_tolerance) &
.or. (abs(cmpazdiff-270.0) <= f90sac_angle_tolerance))) then
write(0,'(a)') &
'F90SAC_ORIENT2D: Error: Input components are not orthogonal'
STOP
endif
! ** first rotate t1 to the north
call f90sac_rotate2d(t1,t2,-t1 % cmpaz)
! ** now check whether t2 needs to be reversed
if ( (abs(t2 % cmpaz-270.0) <= f90sac_angle_tolerance) ) then
t2 % trace(1:t2 % npts) = -t2 % trace(1:t2 % npts)
endif
! ** update component azimuths
t1 % cmpaz = 0.0 ! north
t2 % cmpaz = 90.0 ! east
! ** update component names
write(t1 % kcmpnm,'(f5.1)') t1 % cmpaz
write(t2 % kcmpnm,'(f5.1)') t2 % cmpaz
return
end subroutine f90sac_orient2d
!===============================================================================
!===============================================================================
function f90sac_orth2d(t1,t2)
!===============================================================================
!
! Check for the (azimuthal) orthogonality between two traces
!
implicit none
type (SACtrace) :: t1,t2
integer f90sac_orth2d
real cmpazdiff
! ** check for orthogonality
cmpazdiff = abs(t1 % cmpaz - t2 % cmpaz)
if ( .not.((abs(cmpazdiff-90.0) <= f90sac_angle_tolerance) &
.or. (abs(cmpazdiff-270.0) <= f90sac_angle_tolerance))) then
f90sac_orth2d = 0
else
f90sac_orth2d = 1
endif
return
end function f90sac_orth2d
!===============================================================================
!===============================================================================
subroutine f90sac_rotate2d_rz(t1,t2,theta)
!===============================================================================
!
! rotate 2 SAC traces in the radial vertical plane
!
! t1,t2 : (I/O) SAC traces
! theta : (I) angle to rotate by (clockwise)
!
implicit none
integer :: isamp
type (SACtrace) :: t1,t2
real theta, rotmat(2,2), sample(2,1), rsample(2,1)
real,parameter :: pi = 3.1415927410125732421875
real :: cmpazdiff
! ** check CMPINC header is present
if (t1 % cmpinc == SAC_rnull .or. t2 % cmpinc == SAC_rnull) then
if (f90sac_suppress_warnings /= 1) then
write(0,'(a)') &
'F90SAC_ROTATE2D_RZ: Warning: CMPINC Header is not set,'
write(0,'(a)') &
'F90SAC_ROTATE2D_RZ: Warning: setting CMPINC to 0,90'
endif
t1 % cmpinc = 0.0
t2 % cmpinc = 90.0
endif
! ** check for orthogonality
cmpazdiff = abs(t1 % cmpinc - t2 % cmpinc)
if ( .not.((abs(cmpazdiff-90.0) <= f90sac_angle_tolerance) &
.or. (abs(cmpazdiff-270.0) <= f90sac_angle_tolerance))) then
write(0,'(a)') &
'F90SAC_ROTATE2D_RZ: Error: Input components are not orthogonal'
STOP
endif
! ** check that traces are the same length
if (t1 % npts /= t2 % npts) then
write(0,'(a)') &
'F90SAC_ROTATE2D_RZ: Error: Input components are different lengths'
STOP
endif
! ** make theta clockwise
theta = theta * (-1.0)
! ** build rotation matrix
rotmat(1,1) = cos(theta*pi/180.)
rotmat(1,2) = sin(theta*pi/180.)
rotmat(2,1) = -sin(theta*pi/180.)
rotmat(2,2) = cos(theta*pi/180.)
! ** rotate traces
do isamp = 1 , t1 % npts
sample(1,1) = t1 % trace(isamp) ;
sample(2,1) = t2 % trace(isamp) ;
rsample = matmul(rotmat,sample) ;
t1 % trace(isamp) = rsample(1,1) ;
t2 % trace(isamp) = rsample(2,1) ;
enddo
! ** update component azimuths
t1 % cmpinc = t1 % cmpinc + theta
t2 % cmpinc = t2 % cmpinc + theta
! ** force to be in the range 0-360 degrees
do ! forever
if (t1 % cmpinc >= 0.0 .and. t1 % cmpinc < 360.0) exit
if (t1 % cmpinc >= 360.0) t1 % cmpinc = t1 % cmpinc - 360.0
if (t1 % cmpinc < 0.0) t1 % cmpinc = t1 % cmpinc + 360.0
enddo
do ! forever
if (t2 % cmpinc >= 0.0 .and. t2 % cmpinc < 360.0) exit
if (t2 % cmpinc >= 360.0) t2 % cmpinc = t2 % cmpinc - 360.0
if (t2 % cmpinc < 0.0) t2 % cmpinc = t2 % cmpinc + 360.0
enddo
return
end subroutine f90sac_rotate2d_rz
!===============================================================================
!===============================================================================
subroutine f90sac_rotate2d(t1,t2,theta,iforce)
!===============================================================================
!
! rotate 2 SAC traces in azimuth
!
! t1,t2 : (I/O) SAC traces
! theta : (I) angle to rotate by (clockwise)
! iforce : set to 1 to circumvent checking (except trace length)
!
implicit none
integer :: isamp
type (SACtrace) :: t1,t2
real :: theta
integer, optional :: iforce
! ** locals
integer :: iforceV
real rotmat(2,2), sample(2,1), rsample(2,1)
real,parameter :: pi = 3.1415927410125732421875
real :: cmpazdiff
if (present(iforce)) then
iforceV = iforce
else
iforceV = 0
endif
if (iforceV==0) then
! ** check that cmpaz is set
if (t1 % cmpaz == SAC_rnull .or. t2 % cmpaz == SAC_rnull) then
if (f90sac_suppress_warnings /= 1) then
write(0,'(a)') &
'F90SAC_ROTATE2D: Warning: CMPAZ Header is not set'
write(0,'(a)') &
'F90SAC_ROTATE2D: Warning: Setting CMPAZ to 0,90'
endif
t1 % cmpaz = 0.
t2 % cmpaz = 90.0
endif
! ** check for orthogonality
cmpazdiff = abs(t1 % cmpaz - t2 % cmpaz)
if ( .not.((abs(cmpazdiff-90.0) <= f90sac_angle_tolerance) &
.or. (abs(cmpazdiff-270.0) <= f90sac_angle_tolerance))) then
write(0,'(a)') &
'F90SAC_ROTATE2D: Error: Input components are not orthogonal'
STOP
endif
endif
! ** check that traces are the same length
if (t1 % npts /= t2 % npts) then
write(0,'(a)') &
'F90SAC_ROTATE2D: Error: Input traces are different lengths'
STOP
endif
! ** build rotation matrix
rotmat(1,1) = cos(theta*pi/180.)
rotmat(1,2) = sin(theta*pi/180.)
rotmat(2,1) = -sin(theta*pi/180.)
rotmat(2,2) = cos(theta*pi/180.)
! ** rotate traces
do isamp = 1 , t1 % npts
sample(1,1) = t1 % trace(isamp) ;
sample(2,1) = t2 % trace(isamp) ;
rsample = matmul(rotmat,sample) ;
t1 % trace(isamp) = rsample(1,1) ;
t2 % trace(isamp) = rsample(2,1) ;
enddo
! ** update component azimuths
t1 % cmpaz = t1 % cmpaz + theta
t2 % cmpaz = t2 % cmpaz + theta
! ** force to be in the range 0-360 degrees
do ! forever
if (t1 % cmpaz >= 0.0 .and. t1 % cmpaz < 360.0) exit
if (t1 % cmpaz >= 360.0) t1 % cmpaz = t1 % cmpaz - 360.0
if (t1 % cmpaz < 0.0) t1 % cmpaz = t1 % cmpaz + 360.0
enddo
do ! forever
if (t2 % cmpaz >= 0.0 .and. t2 % cmpaz < 360.0) exit
if (t2 % cmpaz >= 360.0) t2 % cmpaz = t2 % cmpaz - 360.0
if (t2 % cmpaz < 0.0) t2 % cmpaz = t2 % cmpaz + 360.0
enddo
! ** update component names
write(t1 % kcmpnm,'(f5.1)') t1 % cmpaz
write(t2 % kcmpnm,'(f5.1)') t2 % cmpaz
return
end subroutine f90sac_rotate2d