-
Notifications
You must be signed in to change notification settings - Fork 7
/
seismogramloader.ts
343 lines (304 loc) · 10.2 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
import { Duration, Interval} from 'luxon';
import type {TraveltimeJsonType} from "./traveltime";
import {distaz, DistAzOutput} 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, Station} from "./stationxml";
import {SeismogramDisplayData} from "./seismogram";
import {
createMarkersForTravelTimes,
createMarkerForOriginTime,
} from "./seismograph";
import {isDef, isStringArg, stringify} from "./util";
import RSVP from "rsvp";
export class SeismogramLoader {
stationQuery: StationQuery;
withResponse: boolean;
markOrigin: boolean;
eventQuery: EventQuery;
dataselectQuery: null | DataSelectQuery;
_startPhaseList: Array<string>;
_endPhaseList: Array<string>;
_markedPhaseList: Array<string>;
_startOffset: Duration;
_endOffset: Duration;
_networkList: Promise<Array<Network>> | null;
_quakeList: Promise<Array<Quake>> | null;
traveltimeList: Promise<Array<[Station, Quake, TraveltimeJsonType, DistAzOutput]>> | null;
_sddList: Promise<Array<SeismogramDisplayData>> | null;
constructor(
stationQuery: StationQuery,
eventQuery: EventQuery,
dataselectQuery?: DataSelectQuery,
) {
if (!isDef(stationQuery)) {
throw new Error("stationQuery must not be null");
}
if (!(stationQuery instanceof StationQuery)) {
throw new Error(
"1st arg must be a StationQuery: " +
stringify(stationQuery),
);
}
if (!isDef(eventQuery)) {
throw new Error("eventQuery must not be null");
}
if (!(eventQuery instanceof EventQuery)) {
throw new Error(
"2nd arg must be a EventQuery: " + stringify(eventQuery),
);
}
this.stationQuery = stationQuery;
this.withResponse = false;
this.eventQuery = eventQuery;
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
this._networkList = null;
this._quakeList = null;
this.traveltimeList = null;
this._sddList = null;
}
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.startOffsetOfSeconds(val);
} else {
throw new Error("startOffset must be luxon Duration or number of seconds: " + stringify(val));
}
}
startOffsetOfSeconds(val: number) {
this._startOffset = Duration.fromMillis(val*1000); // seconds
}
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.endOffsetOfSeconds(val);
} else {
throw new Error("startOffset must be luxon Duration or number of seconds: " + stringify(val));
}
}
endOffsetOfSeconds(val: number) {
this._endOffset = Duration.fromMillis(val*1000); //seconds
}
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,
);
}
}
get networkList(): Promise<Array<Network>> {
if (this._networkList === null) {
return this.load().then(([ netList, quakeList, sddList]) => netList);
}
return this._networkList;
}
get quakeList(): Promise<Array<Quake>> {
if (this._quakeList === null) {
return this.load().then(([ netList, quakeList, sddList]) => quakeList);
}
return this._quakeList;
}
get sddList(): Promise<Array<SeismogramDisplayData>> {
if (this._sddList === null) {
return this.load().then(([ netList, quakeList, sddList]) => sddList);
}
return this._sddList;
}
loadSeismograms(): Promise<Array<SeismogramDisplayData>> {
return this.load().then(([netList, quakeList, sddList]) => sddList);
}
load(): Promise<[Array<Network>, Array<Quake>, Array<SeismogramDisplayData>]> {
const fedcat = FedCatalogQuery.fromStationQuery(this.stationQuery);
if (!this.stationQuery.isSomeParameterSet()) {
throw new Error(
"Must set some station parameter to avoid asking for everything.",
);
}
if (!this.eventQuery.isSomeParameterSet()) {
throw new Error(
"Must set some event parameter to avoid asking for everything.",
);
}
if (this.withResponse) {
this._networkList = fedcat.queryResponses();
} else {
this._networkList = fedcat.queryChannels();
}
this._quakeList = this.eventQuery.query();
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(",");
this.traveltimeList = RSVP.all([this._networkList, this._quakeList]).then(
([netList, quakeList]) => {
const ttpromiseList: Array<Promise<[Station, Quake, TraveltimeJsonType, DistAzOutput]>> = [];
for (const q of quakeList) {
for (const s of allStations(netList)) {
if (s.timeRange.contains(q.time)) {
const taupQuery = new TraveltimeQuery();
const daz = distaz(
s.latitude,
s.longitude,
q.latitude,
q.longitude,
);
taupQuery.distdeg(daz.distanceDeg);
taupQuery.phases(allPhasesWithoutOrigin);
// save quake and station along with result from traveltime
ttpromiseList.push(
Promise.all([s, q, taupQuery.queryJson(), daz]),
);
}
}
}
return Promise.all(ttpromiseList);
},
);
this._sddList = this.traveltimeList.then(ttpromiseList => {
const seismogramDataList = [];
for (const ttarr of ttpromiseList) {
const station = ttarr[0];
const quake = ttarr[1];
const ttjson = ttarr[2];
const distaz = ttarr[3];
// 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(distaz.distanceDeg);
} else {
for (const a of ttjson.arrivals) {
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(distaz.distanceDeg);
} else {
for (const a of ttjson.arrivals) {
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, ttjson);
if (this.markOrigin) {
phaseMarkers.push(createMarkerForOriginTime(quake));
}
for (const chan of station.channels) {
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 {
// use IrisFedCat
const fedcatDS = new FedCatalogQuery();
sddListPromise = fedcatDS.postQuerySeismograms(seismogramDataList);
}
return sddListPromise;
});
return Promise.all([this._networkList, this._quakeList, this._sddList]);
}
}