/
traveltime.js
251 lines (236 loc) · 7.71 KB
/
traveltime.js
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
// @flow
/*
* Philip Crotwell
* University of South Carolina, 2019
* http://www.seis.sc.edu
*/
import {checkProtocol, hasArgs, stringify, isDef, isNonEmptyStringArg } from './util';
import {Station} from './stationxml.js';
import {Quake} from './quakeml.js';
export let IRIS_HOST = "service.iris.edu";
export const TEXT_FORMAT = "text";
export const JSON_FORMAT = "json";
export const SVG_FORMAT = "svg";
/**
* Query to the IRIS traveltime webservice, based on the TauP Toolkit. See
* http://service.iris.edu/irisws/traveltime/1/ and
* https://www.seis.sc.edu/TauP/
*
* @param host optional host to connect to, defaults to IRIS
*/
export class TraveltimeQuery {
/** @private */
_specVersion: string;
/** @private */
_protocol: string;
/** @private */
_host: string;
/** @private */
_nodata: number;
/** @private */
_evdepth: number;
/** @private */
_distdeg: number;
/** @private */
_model: string;
/** @private */
_phases: string;
/** @private */
_stalat: number;
/** @private */
_stalon: number;
/** @private */
_evlat: number;
/** @private */
_evlon: number;
/** @private */
_format: string;
/** @private */
_noheader: boolean;
constructor(host: ?string) {
this._specVersion = "1";
this._protocol = checkProtocol();
if (! isNonEmptyStringArg(host)) {
this._host = IRIS_HOST;
} else {
this._host = host;
}
this._format = JSON_FORMAT;
this._noheader = false; // only for text format
}
protocol(value?: string): string | TraveltimeQuery {
return hasArgs(value) ? (this._protocol = value, this) : this._protocol;
}
host(value?: string): string | TraveltimeQuery {
return hasArgs(value) ? (this._host = value, this) : this._host;
}
specVersion(value?: string): string | TraveltimeQuery {
return hasArgs(value) ? (this._specVersion = value, this) : this._specVersion;
}
evdepth(value?: number): number | TraveltimeQuery {
return hasArgs(value) ? (this._evdepth = value, this) : this._evdepth;
}
distdeg(value?: number): number | TraveltimeQuery {
return hasArgs(value) ? (this._distdeg = value, this) : this._distdeg;
}
model(value?: string): string | TraveltimeQuery {
return hasArgs(value) ? (this._model = value, this) : this._model;
}
phases(value?: string): string | TraveltimeQuery {
return hasArgs(value) ? (this._phases = value, this) : this._phases;
}
stalat(value?: number): number | TraveltimeQuery {
return hasArgs(value) ? (this._stalat = value, this) : this._stalat;
}
stalon(value?: number): number | TraveltimeQuery {
return hasArgs(value) ? (this._stalon = value, this) : this._stalon;
}
latLonFromStation(station: Station): TraveltimeQuery {
this.stalat(station.latitude);
this.stalon(station.longitude);
return this;
}
evlat(value?: number): number | TraveltimeQuery {
return hasArgs(value) ? (this._evlat = value, this) : this._evlat;
}
evlon(value?: number): number | TraveltimeQuery {
return hasArgs(value) ? (this._evlon = value, this) : this._evlon;
}
latLonFromQuake(quake: Quake): TraveltimeQuery {
this.evlat(quake.latitude);
this.evlon(quake.longitude);
this.evdepth(quake.depth/1000);
return this;
}
format(value?: string): string | TraveltimeQuery {
return hasArgs(value) ? (this._format = value, this) : this._format;
}
noheader(value?: boolean): boolean | TraveltimeQuery {
return hasArgs(value) ? (this._noheader = value, this) : this._noheader;
}
convertToArrival(ttimeline: string) {
let items = ttimeline.trim().split(/\s+/);
return {
distdeg: parseFloat(items[0]),
phase: items[2],
time: parseFloat(items[3]),
rayparam: parseFloat(items[4]),
takeoff: parseFloat(items[5]),
incident: parseFloat(items[6]),
puristdist: parseFloat(items[7]),
puristname: items[9]
};
}
queryText(): Promise<string> {
this.format(TEXT_FORMAT);
return fetch(this.formURL())
.then(response => {
if (response.ok) {
return response.text();
} else {
throw new Error("Fetching over network was not ok: "+response.status+" "+response.statusText);
}
});
}
queryJson(): Promise<any> {
this.format(JSON_FORMAT);
return fetch(this.formURL())
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Fetching over network was not ok: "+response.status+" "+response.statusText);
}
});
}
querySvg(): Promise<Element> {
this.format(SVG_FORMAT);
return fetch(this.formURL())
.then(response => {
if (response.ok) {
return response.text()
.then(textResponse => (new window.DOMParser()).parseFromString(textResponse, "text/xml"))
.then(xml => {
let elArray = xml.getElementsByTagName("svg");
if (elArray.length > 0) {
return elArray[0];
} else {
throw new Error("Can't find svg element in response");
}
});
} else {
throw new Error("Fetching over network was not ok: "+response.status+" "+response.statusText);
}
});
}
queryWadl(): Promise<Element> {
return fetch(this.formWadlURL())
.then(response => {
if (response.ok) {
return response.text()
.then(textResponse => (new window.DOMParser()).parseFromString(textResponse, "text/xml"));
} else {
throw new Error("Fetching over network was not ok: "+response.status+" "+response.statusText);
}
});
}
query(): Promise<any> {
if (this._format === JSON_FORMAT) {
return this.queryJson();
} else if (this._format === SVG_FORMAT) {
return this.querySvg();
} else if (this._format === TEXT_FORMAT) {
return this.queryText();
} else {
throw new Error("Unknown format: "+this._format);
}
}
makeParam(name: string, val: mixed): string {
return name+"="+encodeURIComponent(stringify(val))+"&";
}
formBaseURL(): string {
let colon = ":";
if (this._protocol.endsWith(colon)) {
colon = "";
}
let url = this._protocol+colon+"//"+this._host+"/irisws/traveltime/"+this._specVersion+"/";
return url;
}
formURL(): string {
let url = this.formBaseURL()+'query?';
if (isDef(this._noheader) && this._noheader) {
url = url +"noheader=true&";
}
if (isDef(this._evdepth)) { url = url+this.makeParam("evdepth", this.evdepth()); }
if (isDef(this._stalat) && isDef(this._stalon)) {
url = url+this.makeParam("staloc", "["+stringify(this.stalat())+","+stringify(this.stalon())+"]");
}
if (isDef(this._evlat) && isDef(this._evlon)) {
url = url+this.makeParam("evloc", "["+stringify(this.evlat())+","+stringify(this.evlon())+"]");
}
if (isDef(this._distdeg)) { url = url+this.makeParam("distdeg", this.distdeg());}
if (isDef(this._model)) { url = url+this.makeParam("model", this.model());}
if (isDef(this._phases)) { url = url+this.makeParam("phases", this.phases());}
if (isDef(this._format)) { url = url+this.makeParam("format", this.format());}
if (url.endsWith('&') || url.endsWith('?')) {
url = url.substr(0, url.length-1); // zap last & or ?
}
return url;
}
queryTauPVersion(): Promise<string> {
return fetch(this.formTauPVersionURL())
.then(response => {
if (response.ok) {
return response.text();
} else {
throw new Error("Fetching over network was not ok: "+response.status+" "+response.statusText);
}
});
}
formTauPVersionURL(): string {
return this.formBaseURL()+'taupversion';
}
formWadlURL(): string {
return this.formBaseURL()+'application.wadl';
}
}