-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainInput.m
More file actions
525 lines (495 loc) · 18.8 KB
/
Copy pathMainInput.m
File metadata and controls
525 lines (495 loc) · 18.8 KB
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
% This version intentionally follows the original processing logic:
% 1. Use EW1 files as station references.
% 2. Read station metadata from EW1 headers.
% 3. Use EW1 sampling rate and scale factor for all three components.
% 4. Apply linear detrending to EW and UD only.
% 5. Align records to the earthquake origin time.
% 6. Allow matrices to expand dynamically to the longest trace.
% 7. Remove zero and abnormal stations using the original criteria.
% =========================================================================
% Stroing-Motion Data could be downloaded from NIED:
% https://www.kyoshin.bosai.go.jp/en/
% You need to register an account and set up an email address:
% e.g.,
% UserName: Yuxiao
% Password: ***
% =========================================================================
% For KiK-net, each station has two sets of three-component instruments:
% e.g.,
% EW1: East-west acceleration record from borehole / underground
% EW2: East-west acceleration record from surface / terrestrial
% Use underground station waveforms to minimize the impact of near-surface
% site effects and urban noise on inversion
% =========================================================================
% The earthquake event time and the recording times at each station should
% be in JST format (not UTC)
clc;
close all;
clear;
%% =========================================================================
% Input parameters
% File: Input
% |-- Datafunc: processing functions
% |-- DataWave: initial waves (e.g., EW1,NS1)
% |-- OutSta: output station info and location
% |-- OutWave: output wave.dat (each has 3 components)
% |-- Info.txt: EQ Information
% =========================================================================
PATH = 'D:\Matlab\MyMechanismFunc\FaultInverseAuto\Input\'; % Root Input
DirWaveIn = [PATH,'DataWave'];
DirWaveOut= [PATH,'OutWave'];
DirStaOut = [PATH,'OutSta'];
DirFunc = [PATH,'Datafunc'];
DirInfo = [PATH,'Info.txt'];
addpath(DirFunc);
srate = 4; % Target sampling rate in Hz
UnitGal = 1e-2; % 1 gal = 0.01 m/s^2
fprintf('[Now] %s\n',datetime('now'));
fprintf('[1] Folders are ready \n');
%% =========================================================================
% Read earthquake origin time and source parameters
% =========================================================================
if ~exist(DirInfo, 'file')
error('Info.txt was not found: %s', DirInfo);
end
fid = fopen(DirInfo, 'r');
if fid < 0
error('Unable to open Info.txt.');
end
% Read the first description line.
fgetl(fid);
% Read the earthquake origin time.
dateori = strtrim(fgetl(fid));
% Read and discard the parameter-header line.
fgetl(fid);
% Read numerical source parameters.
Strings = fscanf(fid, '%f');
fclose(fid);
if mod(numel(Strings), 11) ~= 0
error('The number of source parameters is not a multiple of 11.');
end
Strings = reshape(Strings, 11, [])';
% =========================================================================
% IMPORTANT COORDINATE CONVENTION
% The original code uses:
% epi = [ear_all(:,2), ear_all(:,1)]
%
% The da_zh function expects:
% loca = [latitude, longitude]
% epi = [latitude, longitude]
% =========================================================================
epicenter = [Strings(:, 2), Strings(:, 1)]; % [Latitude, Longitude]
depth = Strings(:, 3); % Earthquake depth in km
macha1 = Strings(:, 4:6); % First focal mechanism
macha2 = Strings(:, 7:9); % Second focal mechanism
fprintf('[2] Earthquake-Info are ready \n');
%% =========================================================================
% Find the downloaded strong-motion event DirWave or just given simply
% =========================================================================
% file_SM = dir(DirInput);
% isSubFolder = [file_SM.isdir];
% folderNames = {file_SM(isSubFolder).name}';
%
% folderNames(ismember(folderNames, {'.', '..'})) = [];
%
% if isempty(folderNames)
% error('No event DirWave was found under: %s', DirInput);
% end
%
% % Keep the original logic: use the first event DirWave.
% Dname = folderNames{1};
% DirWave = fullfile(DirInput, Dname);
% if ~isfolder(DirWave)
% error('Event DirWave does not exist: %s', DirWave);
% end
% fprintf('# Successfully find the Info.txt');
%% =========================================================================
% Find KiK-net borehole waveform files (index=1) or surface (index=2)
% =========================================================================
fileEW = dir(fullfile(DirWaveIn, '*.EW1'));
fileNS = dir(fullfile(DirWaveIn, '*.NS1'));
fileUD = dir(fullfile(DirWaveIn, '*.UD1'));
if isempty(fileEW)
error('No EW1 borehole waveform files were found in: %s', DirWaveIn);
end
if numel(fileEW) ~= numel(fileNS) || numel(fileEW) ~= numel(fileUD)
warning(['The numbers of EW1, NS1, and UD1 files are inconsistent. ', ...
'The code assumes matching file order and station count.']);
end
Nsta = numel(fileEW);
fprintf('[3] Stations are ready \n');
fprintf(' Counts: %d \n',Nsta);
%% =========================================================================
% Initialize variables
% MATLAB dynamically expands them when longer records are assigned.
% Therefore, the final sample count equals the longest processed trace.
% =========================================================================
loca = zeros(Nsta, 2); % [Latitude, Longitude]
mm = repmat(' ', Nsta, 1); % Station names
ts = zeros(1, Nsta); % Record-start time minus origin time
EW = zeros(1, Nsta);
NS = zeros(1, Nsta);
UD = zeros(1, Nsta);
ob = zeros(1, Nsta * 3);
% =========================================================================
% Read three-component strong-motion records
% =========================================================================
for i = 1:Nsta
ewFile = fullfile(DirWaveIn, fileEW(i).name);
% ---------------------------------------------------------------------
% Read station metadata from the EW file header
% ---------------------------------------------------------------------
fid = fopen(ewFile, 'r');
if fid < 0
warning('Unable to open EW file: %s', ewFile);
continue;
end
% Read the station-name line.
for k = 1:6
ch = fgets(fid);
end
dex = find(ch == 'e', 1, 'first');
if isempty(dex)
stationName = strtrim(ch);
else
stationName = ch(dex + 1:end);
stationName(isspace(stationName)) = [];
end
mm(i, 1:numel(stationName)) = stationName;
% Read station latitude.
ch = fgets(fid);
dex = find(ch == 'L', 1, 'first');
if isempty(dex)
error('Unable to parse latitude from: %s', ewFile);
end
locaLat = str2double(ch(dex + 4:end));
% Read station longitude.
ch = fgets(fid);
dex = find(ch == 'L', 1, 'first');
if isempty(dex)
error('Unable to parse longitude from: %s', ewFile);
end
locaLon = str2double(ch(dex + 5:end));
loca(i, :) = [locaLat, locaLon];
% Read the sampling frequency
for k = 1:3
ch = fgets(fid);
end
dex = strfind(ch, ')');
if isempty(dex)
error('Unable to parse sampling frequency from: %s', ewFile);
end
srate0 = str2double(ch(dex + 2:end - 3));
% Read the scale factor
% Notice: Here, it is simplified to assume that the amplitude scaling
% factor for each individual station should be the same; however,
% a more rigorous approach would involve reading the scaling factor
% for each station individually
% Format: Scale Factor 2940(gal)/6170270
for k = 1:3
ch = fgets(fid);
end
dex1 = strfind(ch, 'r');
dex2 = strfind(ch, '(');
if isempty(dex1) || isempty(dex2)
error('Unable to parse scale factor from: %s', ewFile);
end
faca = str2double(ch(dex1 + 7:dex2 - 1));
facb = str2double(ch(dex2 + 6:end));
% Read the record-start time.
for k = 1:2
ch = fgets(fid);
end
dex1 = strfind(ch, 'n');
if isempty(dex1)
error('Unable to parse record time from: %s', ewFile);
end
timestr = ch(dex1 + 4:end);
daterec = datenum(timestr) * 24 * 3600;
ts(i) = daterec - datenum(dateori) * 24 * 3600;
% Skip the final header line before reading waveform samples.
fgets(fid);
% ---------------------------------------------------------------------
% Read EW component
% ---------------------------------------------------------------------
ob0 = fscanf(fid, '%f');
% Convert digital counts using the EW scale factor.
ob0 = ob0 * faca / facb;
% Remove the mean value.
ob0 = ob0 - mean(ob0);
% Remove linear drift from EW
obpoly = legendre_fit(ob0, 1:numel(ob0), 1, 0, 2);
ob0 = ob0 - obpoly;
% Resample to the target sampling rate.
ob0 = resample(ob0, srate, srate0);
% Align the waveform beginning to the earthquake origin time.
if ts(i) > 0
ob0 = [zeros(round(ts(i) * srate), 1); ob0];
else
ob0 = ob0(1 - round(ts(i) * srate):end);
end
obEW = ob0;
% Convert gal to m/s^2.
EW(1:numel(obEW), i) = obEW * UnitGal;
fclose(fid);
% ---------------------------------------------------------------------
% Read NS component
%
% The original code uses the EW sampling rate and EW scale factor.
% That behavior is retained here for compatibility.
% ---------------------------------------------------------------------
nsFile = fullfile(DirWaveIn, fileNS(i).name);
fid1 = fopen(nsFile, 'r');
if fid1 < 0
warning('Unable to open NS file: %s', nsFile);
continue;
end
% Skip the 17-line NIED ASCII header.
for k = 1:17
fgets(fid1);
end
ob0 = fscanf(fid1, '%f');
% Keep the original logic: use EW scale factor.
ob0 = ob0 * faca / facb;
% Remove the mean value.
ob0 = ob0 - mean(ob0);
% Keep the original logic: no linear detrending for NS.
ob0 = resample(ob0, srate, srate0);
% Align the waveform beginning to the earthquake origin time.
if ts(i) > 0
ob0 = [zeros(round(ts(i) * srate), 1); ob0];
else
ob0 = ob0(1 - round(ts(i) * srate):end);
end
obNS = ob0;
% Convert gal to m/s^2.
NS(1:numel(obNS), i) = obNS * UnitGal;
fclose(fid1);
% ---------------------------------------------------------------------
% Read UD component
%
% The original code uses the EW sampling rate and EW scale factor.
% That behavior is retained here for compatibility.
% ---------------------------------------------------------------------
udFile = fullfile(DirWaveIn, fileUD(i).name);
fid2 = fopen(udFile, 'r');
if fid2 < 0
warning('Unable to open UD file: %s', udFile);
continue;
end
% Skip the 17-line NIED ASCII header.
for k = 1:17
fgets(fid2);
end
ob0 = fscanf(fid2, '%f');
% Keep the original logic: use EW scale factor.
ob0 = ob0 * faca / facb;
% Remove the mean value.
ob0 = ob0 - mean(ob0);
% Remove linear drift from UD, following the original code.
obpoly = legendre_fit(ob0, 1:numel(ob0), 1, 0, 2);
ob0 = ob0 - obpoly;
% Resample to the target sampling rate.
ob0 = resample(ob0, srate, srate0);
% Align the waveform beginning to the earthquake origin time.
if ts(i) > 0
ob0 = [zeros(round(ts(i) * srate), 1); ob0];
else
ob0 = ob0(1 - round(ts(i) * srate):end);
end
obUD = ob0;
% Convert gal to m/s^2.
UD(1:numel(obUD), i) = obUD * UnitGal;
fclose(fid2);
% ---------------------------------------------------------------------
% Construct the observed data matrix
%
% Column order:
% EW1 NS1 UD1 EW2 NS2 UD2 ...
% ---------------------------------------------------------------------
if numel(obEW) ~= numel(obNS) || numel(obEW) ~= numel(obUD)
error(['The three components at station %s do not have identical ', ...
'lengths after processing.'], strtrim(mm(i, :)));
end
ob0 = [obEW, obNS, obUD] * UnitGal;
ob(1:numel(obEW), 3 * (i - 1) + 1:3 * i) = ob0;
fprintf(' Processed %d/%d: %s; Samples=%d\n', ...
i, Nsta, strtrim(mm(i, :)), numel(obEW));
end
fprintf('[4] Successfully loaded observations (Three-components) \n');
clear obEW obNS obUD ob0 obpoly;
%% =========================================================================
% Remove stations whose EW component sum is zero
% =========================================================================
ba = sum(EW, 1);
ca = find(ba(:) == 0);
a = ca';
for i = 1:numel(a)
ob(:, (a(i) * 3 - 2):a(i) * 3) = 0;
EW(:, a(i)) = 0;
NS(:, a(i)) = 0;
UD(:, a(i)) = 0;
loca(a(i), :) = 0;
mm(a(i), :) = 0;
end
ob(:, sum(abs(ob), 1) == 0) = [];
EW(:, sum(abs(EW), 1) == 0) = [];
NS(:, sum(abs(NS), 1) == 0) = [];
UD(:, sum(abs(UD), 1) == 0) = [];
loca(sum(abs(loca), 2) == 0, :) = [];
mm(sum(abs(mm), 2) == 0, :) = [];
% =========================================================================
% Remove stations with abnormal differences among components
%
% This section intentionally follows the original calculation:
% max(EW), max(NS), and max(UD) are used without abs().
% =========================================================================
mEW = max(EW);
mNS = max(NS);
mUD = max(UD);
s1 = mEW ./ mNS;
s2 = mNS ./ mEW;
[a1, b1] = max(max(s1, s2));
c1 = mEW ./ mUD;
c2 = mUD ./ mEW;
[a2, b2] = max(max(c1, c2));
v1 = mNS ./ mUD;
v2 = mUD ./ mNS;
[a3, b3] = max(max(v1, v2));
s1 = [a1, a2, a3];
s2 = [b1, b2, b3];
dex = find(s1 > 10);
ndex = s2(dex);
ndex = unique(ndex);
a = ndex;
for i = 1:numel(a)
ob(:, (a(i) * 3 - 2):a(i) * 3) = 0;
EW(:, a(i)) = 0;
NS(:, a(i)) = 0;
UD(:, a(i)) = 0;
loca(a(i), :) = 0;
mm(a(i), :) = 0;
end
ob(:, sum(abs(ob), 1) == 0) = [];
EW(:, sum(abs(EW), 1) == 0) = [];
NS(:, sum(abs(NS), 1) == 0) = [];
UD(:, sum(abs(UD), 1) == 0) = [];
loca(sum(abs(loca), 2) == 0, :) = [];
mm(sum(abs(mm), 2) == 0, :) = [];
clear a a1 a2 a3 b1 b2 b3 c1 c2 v1 v2 s1 s2;
fprintf('[5] Successfully clean observations (Three-components) \n');
%% =========================================================================
% Plot waveforms sorted by epicentral distance
% =========================================================================
% da_zh requires:
% loca = [Latitude, Longitude]
% epi = [Latitude, Longitude]
da = da_zh2(loca, epicenter(1, :), 1);
% Keep only epicentral distance in km.
da = da(:, 1);
% Repeat each station distance for EW, NS, and UD.
da = repmat(da', 3, 1);
da = da(:);
% Sort all waveform component columns by epicentral distance.
[da1, ndex] = sortrows(da);
% sortedObservedData corresponds to the original variable ob1.
Sortob = ob(:, ndex);
% =================================
% Plot
% figure('Color', 'w');
% mplot(Sortob, da1/10);
% ylabel('Epicentral distance /10');
% xlabel('Time Sample');
Fun_SaveVars('ob','Sortob','da','da1','srate','DataObs');
% Fun_PlotWave(Sortob, da1, srate, 60);
% =================================
%% =========================================================================
% S1. Save station coordinates
% [Longitude Latitude]
% =========================================================================
staloc = [loca(:, 2), loca(:, 1)];
stafile = fullfile(DirStaOut, '\Sta.txt');
fid = fopen(stafile, 'wt');
if fid < 0
error('Unable to write Sta.txt.');
end
fprintf(fid, '%.4f %.4f\n', staloc');
fclose(fid);
fprintf('[S1] Successfully save stations location \n');
% =========================================================================
% S2. Save three-component waveform files
% Each station file has three columns:
% [East North Up]
% Unit:
% m/s^2
% =========================================================================
numberOfValidStations = size(loca, 1);
for i = 1:numberOfValidStations
stationName = strtrim(mm(i, :));
outputFile = fullfile(DirWaveOut, [stationName, '.dat']);
data = [EW(:, i), NS(:, i), UD(:, i)];
fid = fopen(outputFile, 'wt');
if fid < 0
warning('Unable to write waveform file: %s', outputFile);
continue;
end
% Write one EW-NS-UD triplet per line.
fprintf(fid, '%.7f %.7f %.7f\n', data');
fclose(fid);
end
fprintf('[S2] Successfully save wave data \n');
% =========================================================================
% S3. Save station information file
% =========================================================================
stationInfoFile=fullfile(DirStaOut, 'StaInfo.txt'); % 'txt' is ok or 'dat'
fid = fopen(stationInfoFile, 'wt');
if fid < 0
error('Unable to create StaInfo');
end
Dname='DataWave';
fprintf(fid, 'Date:%s\n', Dname);
fprintf(fid, ...
'# earthquake hypocenter: latitude[deg] longitude[deg] depth[km]\n');
fprintf(fid, '%.4f %.4f %d\n', epicenter(1, 1), epicenter(1, 2), depth(1));
fprintf(fid, '# Reference time: JST-format\n');
fprintf(fid, '%s\n', dateori);
fprintf(fid, ...
'# Nsta Wave-Type(0/1/2=dis/vel/acc) Time-interval [s]\n');
fprintf(fid, '%d %d %.4f\n', ...
size(loca, 1), 2, 1 / srate);
fprintf(fid, '# column of component\n');
fprintf(fid, '# east north up\n');
fprintf(fid, '1 2 3\n');
fprintf(fid, '# Strike1 Dip1 Rake1 Strike2 Dip2 Rake2\n');
fprintf(fid, '%d %d %d %d %d %d\n', ...
macha1(1, 1), macha1(1, 2), macha1(1, 3), ...
macha2(1, 1), macha2(1, 2), macha2(1, 3));
fprintf(fid, ...
'# station latitude longitude Tstart Nsample\n');
numberOfSamples = size(ob, 1);
for i = 1:size(loca, 1)
stationName = strtrim(mm(i, :));
stationLatitude = loca(i, 1);
stationLongitude = loca(i, 2);
startTime = 0.0;
fprintf(fid, '%s %.4f %.4f %.0f %d\n', ...
stationName, ...
stationLatitude, ...
stationLongitude, ...
startTime, ...
numberOfSamples);
end
fclose(fid);
fclose('all');
fprintf('[S3] Successfully save stations Info \n');
%% =========================================================================
% Display summary
% =========================================================================
fprintf('\nProcessing completed.\n');
fprintf('Number of valid stations: %d\n', size(loca, 1));
fprintf('Number of samples: %d\n', size(ob, 1));
fprintf('Observed data matrix size: %d x %d\n', size(ob, 1), size(ob, 2));
fprintf('Sorted data matrix size: %d x %d\n', ...
size(Sortob, 1), size(Sortob, 2));
fprintf('Sampling rate: %.2f Hz\n', srate);
fprintf('[Final] %s \n',datetime('now'));