-
Notifications
You must be signed in to change notification settings - Fork 7
/
seismogramloader.ts
409 lines (371 loc) · 12.8 KB
/
seismogramloader.ts
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
import {Dataset} from "./dataset";
import { Duration, Interval} from 'luxon';
import type {TraveltimeJsonType} from "./traveltime";
import {distaz} from "./distaz";
import {TraveltimeQuery, createOriginArrival} from "./traveltime";
import {DataSelectQuery} from "./fdsndataselect";
import {EventQuery} from "./fdsnevent";
import {StationQuery} from "./fdsnstation";
import {FedCatalogQuery} from "./irisfedcatalog";
import {Quake} from "./quakeml";
import {allStations, Network} from "./stationxml";
import {SeismogramDisplayData} from "./seismogram";
import {
createMarkersForTravelTimes,
createMarkerForOriginTime,
} from "./seismograph";
import {isDef, isStringArg, stringify} from "./util";
/**
* Loads seismograms based on queries to Station and Event web services.
* Uses the traveltime web service to create times for data query. Default
* is P-30 sec to S+60 sec.
*
* @param stationQuery query to find channels
* @param eventQuery query to find events
* @param dataselectQuery optional additional parameters for seismogram query
*/
export class SeismogramLoader {
stationQuery: StationQuery | Promise<Array<Network>>;
withFedCatalog: boolean;
withResponse: boolean;
markOrigin: boolean;
eventQuery: EventQuery | Promise<Array<Quake>>;
dataselectQuery: null | DataSelectQuery;
_startPhaseList: Array<string>;
_endPhaseList: Array<string>;
_markedPhaseList: Array<string>;
_startOffset: Duration;
_endOffset: Duration;
constructor(
stationQuery: StationQuery | Array<Network>,
eventQuery: EventQuery | Array<Quake>,
dataselectQuery?: DataSelectQuery,
) {
if (stationQuery instanceof StationQuery) {
this.stationQuery = stationQuery;
} else if ( Array.isArray(stationQuery)) {
this.stationQuery = Promise.resolve(stationQuery);
} else {
throw new Error(
"1st arg must be a StationQuery or array of Networks: " +
stringify(stationQuery),
);
}
if (eventQuery instanceof EventQuery) {
this.eventQuery = eventQuery;
} else if ( Array.isArray(eventQuery)) {
this.eventQuery = Promise.resolve(eventQuery);
} else {
throw new Error("2nd arg must be EventQuery or array of Quake: "+stringify(eventQuery));
}
this.withFedCatalog = true;
this.withResponse = false;
this.markOrigin = true;
this.dataselectQuery = null;
if (isDef(dataselectQuery)) {
this.dataselectQuery = dataselectQuery;
}
this._startPhaseList = ["p", "P", "Pdiff", "PKP"];
this._endPhaseList = ["s", "S", "Sdiff", "SKS"];
this._markedPhaseList = [];
this._startOffset = Duration.fromMillis(-30*1000);// seconds;
this._endOffset = Duration.fromMillis(60*1000); //seconds
}
get startPhaseList(): Array<string> {
return this._startPhaseList;
}
set startPhaseList(val: Array<string> | string) {
if (Array.isArray(val)) {
this._startPhaseList = val;
} else if (isStringArg(val)) {
this._startPhaseList = val.split(",");
} else {
throw new Error(
"value argument is string or array of string, but was " + typeof val,
);
}
}
get startOffset(): Duration {
return this._startOffset;
}
set startOffset(val: Duration) {
if (Duration.isDuration(val)) {
this._startOffset = val;
} else if (typeof val === "number") {
this.startOffsetSeconds(val);
} else {
throw new Error("startOffset must be luxon Duration or number of seconds: " + stringify(val));
}
}
/**
* Sets the startOffset Duration to be val seconds.
*
* @param val number of seconds, negative for before, positive for after
* @returns this
*/
startOffsetSeconds(val: number): SeismogramLoader {
this._startOffset = Duration.fromMillis(val*1000); // seconds
return this;
}
get endPhaseList(): Array<string> {
return this._endPhaseList;
}
set endPhaseList(val: Array<string> | string) {
if (Array.isArray(val)) {
this._endPhaseList = val;
} else if (isStringArg(val)) {
this._endPhaseList = val.split(",");
} else {
throw new Error(
"value argument is string or array of string, but was " + typeof val,
);
}
}
get endOffset(): Duration {
return this._endOffset;
}
set endOffset(val: Duration) {
if (Duration.isDuration(val)) {
this._endOffset = val;
} else if (typeof val === "number") {
this.endOffsetSeconds(val);
} else {
throw new Error("startOffset must be luxon Duration or number of seconds: " + stringify(val));
}
}
/**
* Sets the endOffset Duration to be val seconds.
*
* @param val number of seconds, negative for before, positive for after
* @returns this
*/
endOffsetSeconds(val: number): SeismogramLoader {
this._endOffset = Duration.fromMillis(val*1000); //seconds
return this;
}
/**
* Additional phase arrival travel times to be marked, but do not effect
* the request time window.
*
* @returns array of phase names.
*/
get markedPhaseList(): Array<string> {
return this._markedPhaseList;
}
set markedPhaseList(val: Array<string> | string) {
if (Array.isArray(val)) {
this._markedPhaseList = val;
} else if (isStringArg(val)) {
this._markedPhaseList = val.split(",");
} else {
throw new Error(
"value argument is string or array of string, but was " + typeof val,
);
}
}
loadSeismograms(): Promise<Array<SeismogramDisplayData>> {
return this.load().then(res => res.waveforms);
}
/**
* Loads a Dataset based on the input station and event queries.
*
* The raw traveltimes are included in the extras of the dataset with
* key "traveltimes", which is a Map with the quake as the key.
*
* @returns a Dataset
*/
load(): Promise<Dataset> {
let networkListPromise;
if (this.stationQuery instanceof StationQuery) {
if ( !this.stationQuery.isSomeParameterSet()) {
throw new Error(
"Must set some station parameter to avoid asking for everything.",
);
}
let fedcat;
if (this.withFedCatalog) {
fedcat = FedCatalogQuery.fromStationQuery(this.stationQuery);
} else {
fedcat = this.stationQuery;
}
if (this.withResponse) {
networkListPromise = fedcat.queryResponses();
} else {
networkListPromise = fedcat.queryChannels();
}
} else {
networkListPromise = this.stationQuery;
}
let quakeListPromise;
if (this.eventQuery instanceof EventQuery) {
if (!this.eventQuery.isSomeParameterSet()) {
throw new Error(
"Must set some event parameter to avoid asking for everything.",
);
}
quakeListPromise = this.eventQuery.query();
} else {
quakeListPromise = this.eventQuery;
}
let allPhaseList: Array<string> = [];
allPhaseList = allPhaseList.concat(
this.startPhaseList,
this.endPhaseList,
this.markedPhaseList,
);
if (allPhaseList.includes("origin")) {
this.markOrigin = true;
}
const allPhasesWithoutOrigin = allPhaseList
.filter(p => p !== "origin")
.join(",");
return Promise.all([networkListPromise, quakeListPromise]).then(
([netList, quakeList]) => {
const ttpromiseList: Array<Promise<[Quake, TraveltimeJsonType]>> = [];
for (const q of quakeList) {
// separate call per quake as depth might be different
const allDistDeg = [];
for (const s of allStations(netList)) {
if (s.timeRange.contains(q.time)) {
const daz = distaz(
s.latitude,
s.longitude,
q.latitude,
q.longitude,
);
allDistDeg.push(daz.distanceDeg);
}
}
if (allDistDeg.length > 0) {
const taupQuery = new TraveltimeQuery();
taupQuery.distdeg(allDistDeg);
taupQuery.phases(allPhasesWithoutOrigin);
// save quake along with result from traveltime
ttpromiseList.push(
Promise.all([q, taupQuery.queryJson()]),
);
}
}
return Promise.all([Promise.all(ttpromiseList), netList, quakeList]);
}).then(([ttList, netList, quakeList]) => {
const ttMap = new Map<Quake, TraveltimeJsonType>();
for (const [q, tt] of ttList) {
ttMap.set(q, tt);
}
return Promise.all([ttMap, netList, quakeList]);
}).then(([ttMap, netList, quakeList]) => {
const seismogramDataList = [];
for (const [quake, ttjson] of ttMap) {
for (const station of allStations(netList)) {
if ( ! station.timeRange.contains(quake.time)) {
// skip stations not active during quake
continue;
}
const daz = distaz(
station.latitude,
station.longitude,
quake.latitude,
quake.longitude,
);
// find arrivals for station, match distance
const stationArrivals = [];
for (const a of ttjson.arrivals) {
// look for station with same distance
if ((Math.abs((a.distdeg % 360)-(daz.distanceDeg%360)) < 1e-6 ||
Math.abs(360-(a.distdeg % 360)-(daz.distanceDeg%360)) < 1e-6 )) {
stationArrivals.push(a);
}
}
const station_ttjson: TraveltimeJsonType = {
model: ttjson.model,
sourcedepth: ttjson.sourcedepth,
receiverdepth: ttjson.receiverdepth,
phases: ttjson.phases,
arrivals: stationArrivals
};
// find earliest start and end arrival
let startArrival = null;
let endArrival = null;
for (const pname of this.startPhaseList) {
if (pname === "origin" &&
(startArrival === null || startArrival.time > 0)) {
startArrival = createOriginArrival(daz.distanceDeg);
} else {
for (const a of stationArrivals) {
// look for station with same distance
if (a.phase === pname &&
(startArrival===null || startArrival.time > a.time) ) {
startArrival = a;
}
}
}
}
for (const pname of this.endPhaseList) {
// weird, but might as well allow origin to be the end phase
if (pname === "origin" &&
(endArrival===null || endArrival.time < 0)) {
endArrival = createOriginArrival(daz.distanceDeg);
} else {
for (const a of stationArrivals) {
if (a.phase === pname &&
(endArrival===null || endArrival.time < a.time)) {
endArrival = a;
}
}
}
}
if (isDef(startArrival) && isDef(endArrival)) {
const startTime = quake.time
.plus(Duration.fromMillis(1000*startArrival.time)) // seconds
.plus(this.startOffset);
const endTime = quake.time
.plus(Duration.fromMillis(1000*endArrival.time)) // seconds
.plus(this.endOffset);
const timeRange = Interval.fromDateTimes(startTime, endTime);
const phaseMarkers = createMarkersForTravelTimes(quake, station_ttjson);
if (this.markOrigin) {
phaseMarkers.push(createMarkerForOriginTime(quake));
}
for (const chan of station.channels) {
if ( ! chan.timeRange.contains(quake.time)) {
// skip channels not active during quake
continue;
}
const sdd = SeismogramDisplayData.fromChannelAndTimeWindow(
chan,
timeRange,
);
sdd.addQuake(quake);
sdd.addTravelTimes(ttjson);
sdd.addMarkers(phaseMarkers);
seismogramDataList.push(sdd);
}
}
}
}
let sddListPromise;
if (this.dataselectQuery !== null) {
sddListPromise = this.dataselectQuery.postQuerySeismograms(
seismogramDataList,
);
} else if (this.withFedCatalog) {
// use IrisFedCat
const fedcatDS = new FedCatalogQuery();
sddListPromise = fedcatDS.postQuerySeismograms(seismogramDataList);
} else {
// use default dataselect
sddListPromise = new DataSelectQuery().postQuerySeismograms(
seismogramDataList,
);
}
return Promise.all([sddListPromise, ttMap, netList, quakeList]);
}).then(([sddList, ttMap, networkList, quakeList]) => {
const dataset = new Dataset();
dataset.waveforms = sddList;
dataset.catalog = quakeList;
dataset.inventory = networkList;
dataset.extra.set("traveltimes", ttMap);
return dataset;
});
}
}