-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSACtoDSN.pl
executable file
·355 lines (286 loc) · 7.83 KB
/
SACtoDSN.pl
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
#!/usr/bin/perl
use strict;
use DateTime;
use File::Temp;
use Math::Complex;
use Getopt::Long;
use TauP::Time;
use Seed::Response;
use lib '/usr/local/share/perl/5.26.2'; # Path to perl libraries
use Seismogram::SAC;
my(@input,$output);
GetOptions('input=s{,}' => \@input, 'output=s' => \$output);
# A perl module to convert from SAC to DSN (a format used by MT5)
open my $out_fp,">$output";
my $taup_path='/usr/local/bin/TauP-2.4.5'; # Path to Taup installation
foreach my $file (<@input>)
{
my $phase = 'S';
if ( $file =~ /BHZ/ ) { $phase = 'P';}
# Decimate the data
my $decData=File::Temp->new();
decimateData($file,$decData);
# Read the decimated file
my($sac) = readSAC($decData);
my($station,$network,$component,$channel) = $sac->Fetch(qw(kstnm knetwk kcmpnm khole));
my($resp) = readResponse($sac);
my($waveform,$initialTime) = extractWaveform($sac,$phase);
my $waveformSize = (scalar @$waveform);
my($header) = createSeismogramHeader($sac,$resp,$waveform,$initialTime);
$header .= printPolesAndZeros($sac,$resp,$waveformSize);
$header .= printWaveform($waveform);
print $out_fp $header;
}
close $out_fp;
0;
sub
readSAC
{
my($file) = @_;
my $sac = Seismogram::SAC->new();
open my $fp,"$file";
my $status = $sac->Read($fp);
close $fp;
return $sac;
}
sub
readResponse
{
my($sac)=@_;
my($station,$network,$component,$channel) = $sac->Fetch(qw(kstnm knetwk kcmpnm khole));
my $respName = "RESP.$network.$station.$channel.$component";
print "Response file: $respName\n\n";
my $resp = Seed::Response->new();
$resp->parseFile($respName);
return $resp;
}
=pod
B<decimateData($file,$decFile)>
Given an input SAC file C<$input>, remove the instrument response, convert from
nanometers to micrometers, and decimate the data by 10 and write the results to
C<$decFile>.
=cut
sub
decimateData
{
my($file,$decFile) = @_;
my $sac2000 = 'sac';
open my $sac_fp, "|$sac2000";
print $sac_fp qq(READ $file
DECIMATE 5
DECIMATE 2
WRITE $decFile
QUIT
);
close $sac_fp;
}
=pod
B<createSeismogramHeader($sac,$waveform,$initialTime)>
Given a SAC object C<$sac>, a reference to an array containing the waveform C<$waveform>,
and a DateTime object C<$initialTime> containing the starting time for the waveform, output
a header in MT5's DSN format.
=cut
sub
createSeismogramHeader
{
my($sac,$resp,$waveform,$initialTime) = @_;
my($min,$max) = calculateMinMax($waveform);
my($sta,$component,$event,$offset,$dt,$lat,$lon) = $sac->Fetch(qw(kstnm kcmpnm kevnm o delta stla stlo));
$sta = lc $sta;
my $hour = $initialTime->hour();
my $minute = $initialTime->minute();
my $second = $initialTime->second() + ($initialTime->nanosecond()/1e9);
if ( $event == -12345 )
{
$event = $initialTime->year().' '.$initialTime->month().' '.$initialTime->day();
}
$component = translateComponent($component,$resp);
my $responseType = 'pza';
#my $gain = calculateGain($resp);
my $gain = $resp->getStage(1)->getGain();
if ( not defined $gain )
{
$gain = 1.0;
}
my $header = sprintf("%-4s%1d%3s%10.4e%12s%2d%2d%5.2f%5.2f%7.2f%7.2f%3s\r\n",$sta,4,$component,$gain,$event,$hour,$minute,$second,$dt,$lat,$lon,$responseType);
return $header;
}
=pod
B<printPolesAndZeros($sac,$numpoints)>
=cut
sub
printPolesAndZeros
{
my($sac,$resp,$numPoints) = @_;
my $numZeros = $resp->getStage(1)->getNumberOfZeros();
my $numPoles = $resp->getStage(1)->getNumberOfPoles();
my $normFactor = $resp->getStage(1)->getNormalizationFactor();
my $normFrequency = $resp->getStage(1)->getNormalizationFrequency();
# Now print the header
my $header = sprintf("%5d%5d%5d %10.4e %6.4f\r\n",$numPoints,$numZeros,$numPoles,$normFactor,$normFrequency);
# Print out the poles and zeros
my $data = '';
for(my $i=0;$i<=$numZeros;$i+=4)
{
$data .= ' ';
for(my $j=0;$j<4;$j++)
{
my $zero = $resp->getStage(1)->getZero($i+$j);
next if ( not defined $zero);
$data .= sprintf("%.4e %.4e ",Re($zero),Im($zero));
}
$data .= "\r\n";
}
for(my $i=0;$i<$numPoles;$i+=4)
{
$data .= ' ';
for(my $j=0;$j<4;$j++)
{
my $pole = $resp->getStage(1)->getPole($i+$j);
next if ( not defined $pole);
$data .= sprintf("%.4e %.4e ",Re($pole),Im($pole));
}
$data .= "\r\n";
}
return $header.$data;
}
sub
doyConvert
{
my($year,$doy) = @_;
my $dt = DateTime->new(month => 1, day => 1, year => $year);
$doy--;
$dt->add( days => $doy );
return $dt->month(),$dt->day();
}
sub
extractWaveform
{
my($sac,$phase) = @_;
# First, calculate the expected arrival time of the phase
# This is the time in seconds from the origin time
my $time = getTravelTime($sac,$phase);
# Get the offset between the event origin time and the beginning of the trace
# and the sampling interval
my($offset,$dt,$npts) = $sac->Fetch(qw(o delta npts));
# The offset is the difference between the origin time and the start of the time series
$offset = abs($offset);
# ten seconds, in data points
my $tenSeconds = int(10/$dt);
# Calculate the time of the first point in the waveform
my($year,$doy,$hour,$min,$second,$msec) = $sac->Fetch(qw(nzyear nzjday nzhour nzmin nzsec nzmsec));
my($month,$day) = doyConvert($year,$doy);
my $initialTime = DateTime->new(year => $year, month => $month, day => $day,
hour => $hour, minute => $min, second => $second,
nanosecond => ($msec*1e6));
# Calculate the data point number from the
# beginning of the time series to the phase
my $arrival = int (($time - $offset)/$dt);
# Calculate the window length for the waveform
my($start,$end);
if ( $phase eq 'P' )
{
# Extract a minute on either side
$start = $arrival - 1*$tenSeconds;
$end = $arrival + 9*$tenSeconds;
$initialTime->add(nanoseconds => ($time - $offset - 10)*1e9);
}
elsif ( $phase eq 'S' )
{
# Extract a minute before and 2 minutes after
$start = $arrival - 2*$tenSeconds;
$end = $arrival + 10*$tenSeconds;
$initialTime->add(nanoseconds => ($time - $offset - 20)*1e9);
}
if ( $start < 0 ) { die "Can't window one minute for waveform\nstart < 0 : $start"; }
if ( $end > $npts ) { die "Can't window one minute for waveform\nend > $npts: $end"; }
my @waveform = ();
# Extract the waveform
for(my $i=$start;$i<$end;$i++)
{
push(@waveform,($sac->data()->[$i]*1e6)); # meters/s -> micrometers/s?
}
return \@waveform,$initialTime;
}
sub
printWaveform
{
my($waveform) = @_;
my $numPoints = (scalar @$waveform);
my $data = '';
for(my $i=0;$i<$numPoints;$i+=8)
{
$data .= ' ';
for(my $j=0;$j<8;$j++)
{
next if (($i+$j) > ($numPoints-1));
$data .= sprintf("%.4e ",$waveform->[$i+$j]/1e6);
}
$data .= "\r\n";
}
return $data;
}
sub
getTravelTime
{
my($sac,$phase) = @_;
my $taup = TauP::Time->new($taup_path);
$taup->setModel('iasp91'); # Set the travel time model to IASPEI91 model
my($deg,$depth) = $sac->Fetch(qw(gcarc evdp));
$depth /= 1000;
my $time = $taup->calculateTravelTimes($deg,$depth,$phase);
return $time->[0];
}
sub
calculateMinMax
{
my($x) = @_;
my @sorted_x = sort {$a <=> $b} @$x;
my $min = $sorted_x[0];
my $max = $sorted_x[-1];
return $min,$max;
}
sub
calculateGain
{
my($resp) = @_;
my $totalGain = 1.0;
for(my $i=0;$i<=6;$i++)
{
my $stage = $resp->getStage($i);
if ( defined $stage )
{
my $stageGain = $stage->getGain();
if ( defined $stageGain )
{
$totalGain *= $stageGain;
}
}
}
return $totalGain;
}
=head2 translateComponent($component,$resp)
Translate IRIS component names like BHZ to MT5 component names
like lpz.
=cut
sub
translateComponent
{
my($component,$resp) = @_;
my $mt5Component = $component;
$mt5Component =~ s/^B|^L/l/;
if ( $resp->getStage(1)->getUnits() =~ /Displacement/ )
{
$mt5Component =~ s/H/p/;
}
elsif ( $resp->getStage(1)->getUnits() =~ /^M\/S/ )
{
$mt5Component =~ s/H/v/;
}
elsif ( $resp->getStage(1)->getUnits() =~ /Acceleration/ )
{
$mt5Component =~ s/H/a/;
}
$mt5Component = lc $mt5Component;
return $mt5Component;
}