This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.ts
299 lines (266 loc) · 7.95 KB
/
utils.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
import type {
DistanceToEpicenter,
LngLatObject,
SurfaceDistanceToEpicenter,
} from "../../types";
import { BaseMapZoom, BaseMarkerSize, EarthCircumference } from "./constant";
import region from "../../assets/json/region.json";
import times from "../../assets/json/time.json";
const depthIndexList = Object.keys(times);
export const extractLocationFromString = (str: string) => {
if (str.indexOf("(") < 0) {
return str.substring(0, str.indexOf("方") + 1);
} else {
return str.substring(str.indexOf("(") + 3, str.indexOf(")"));
}
};
export const toFormattedTimeString = (ts: number) => {
const time = new Date(ts);
return [
[
time.getFullYear(),
`${time.getMonth() + 1}`.padStart(2, "0"),
`${time.getDate()}`.padStart(2, "0"),
].join("/"),
" ",
[
`${time.getHours()}`.padStart(2, "0"),
`${time.getMinutes()}`.padStart(2, "0"),
`${time.getSeconds()}`.padStart(2, "0"),
].join(":"),
].join("");
};
export const toReportUrl = (id: string) => {
const arr = id.split("-");
arr.splice(1, 1);
return `https://www.cwa.gov.tw/V8/C/E/EQ/EQ${arr.join("-")}.html` as const;
};
export const getMarkerSizeOnZoom = (zoom: number) =>
BaseMarkerSize - (BaseMapZoom - zoom) * 2;
export const findClosestDepthIndex = (
arr: Array<string>,
target: number
): number => {
return arr
.map(Number)
.reduce((prev, curr) =>
Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev
);
};
export const pgaToFloat = (pga: number) => 2 * Math.log10(pga) + 0.7;
export const roundIntensity = (float: number) =>
float < 0
? 0
: float < 4.5
? Math.round(float)
: float < 5
? 5
: float < 5.5
? 6
: float < 6
? 7
: float < 6.5
? 8
: 9;
export const degreeToRadian = (degree: number) => (degree * Math.PI) / 180;
export const calculateDistance = (p1: LngLatObject) => (p2: LngLatObject) =>
Math.acos(
Math.sin(Math.atan(Math.tan(degreeToRadian(p1.lat)))) *
Math.sin(Math.atan(Math.tan(degreeToRadian(p2.lat)))) +
Math.cos(Math.atan(Math.tan(degreeToRadian(p1.lat)))) *
Math.cos(Math.atan(Math.tan(degreeToRadian(p2.lat)))) *
Math.cos(degreeToRadian(p1.lng) - degreeToRadian(p2.lng))
) * 6371.008;
export const sideDistance = (a: number, b: number) =>
(a ** 2 - b ** 2) ** (1 / 2);
export const speedModel = (depth: number, distance: number) => {
const Za = 1 * depth;
let G0, G;
const Xb = distance;
if (depth <= 40) {
G0 = 5.10298;
G = 0.06659;
} else {
G0 = 7.804799;
G = 0.004573;
}
const Zc = -1 * (G0 / G);
const Xc = (Math.pow(Xb, 2) - 2 * (G0 / G) * Za - Math.pow(Za, 2)) / (2 * Xb);
let Theta_A = Math.atan((Za - Zc) / Xc);
if (Theta_A < 0) {
Theta_A = Theta_A + Math.PI;
}
Theta_A = Math.PI - Theta_A;
const Theta_B = Math.atan((-1 * Zc) / (Xb - Xc));
let Ptime = (1 / G) * Math.log(Math.tan(Theta_A / 2) / Math.tan(Theta_B / 2));
const G0_ = G0 / 1.732;
const G_ = G / 1.732;
const Zc_ = -1 * (G0_ / G_);
const Xc_ =
(Math.pow(Xb, 2) - 2 * (G0_ / G_) * Za - Math.pow(Za, 2)) / (2 * Xb);
let Theta_A_ = Math.atan((Za - Zc_) / Xc_);
if (Theta_A_ < 0) {
Theta_A_ = Theta_A_ + Math.PI;
}
Theta_A_ = Math.PI - Theta_A_;
const Theta_B_ = Math.atan((-1 * Zc_) / (Xb - Xc_));
let Stime =
(1 / G_) * Math.log(Math.tan(Theta_A_ / 2) / Math.tan(Theta_B_ / 2));
if (distance / Ptime > 7) {
Ptime = distance / 7;
}
if (distance / Stime > 4) {
Stime = distance / 4;
}
return { Ptime: Ptime, Stime: Stime };
};
export const calculateWaveRadius = (
accurateTime: number,
depth: number,
eventTime: number
) => {
const newRadius = { p: 0, s: 0 };
const index =
`${findClosestDepthIndex(depthIndexList, depth)}` as keyof typeof times;
const timeTable = times[index];
let prevTable = null;
for (let i = 0, n = timeTable.length; i < n; i++) {
const currTable = timeTable[i];
if (!newRadius.p && currTable.P > (accurateTime - eventTime) / 1000) {
if (prevTable) {
const deltaTime = currTable.P - prevTable.P;
const deltaRadius = currTable.R - prevTable.R;
const timeOffset = (accurateTime - eventTime) / 1000 - prevTable.P;
const radiusOffset = (timeOffset / deltaTime) * deltaRadius;
newRadius.p = prevTable.R + radiusOffset;
} else {
newRadius.p = currTable.R;
}
}
if (!newRadius.s && currTable.S > (accurateTime - eventTime) / 1000) {
if (prevTable) {
const deltaTime = currTable.S - prevTable.S;
const deltaRadius = currTable.R - prevTable.R;
const timeOffset = (accurateTime - eventTime) / 1000 - prevTable.S;
const radiusOffset = (timeOffset / deltaTime) * deltaRadius;
newRadius.s = prevTable.R + radiusOffset;
} else {
newRadius.s = currTable.R;
}
}
if (newRadius.p && newRadius.s) {
break;
}
prevTable = currTable;
}
if (!newRadius.p) {
newRadius.p = sideDistance(((accurateTime - eventTime) / 1000) * 7, depth);
}
if (!newRadius.s) {
newRadius.s = sideDistance(((accurateTime - eventTime) / 1000) * 4, depth);
}
return newRadius;
};
export const calculateEpicenterDistance =
(event: LngLatObject) =>
(local: LngLatObject) =>
(
depth: number
): {
surfaceDistance: SurfaceDistanceToEpicenter;
distance: DistanceToEpicenter;
} => {
const surfaceDistance = calculateDistance(event)(local);
return {
surfaceDistance: surfaceDistance,
distance: (surfaceDistance ** 2 + depth ** 2) ** (1 / 2),
};
};
export const calculateIntensity = (
surfaceDistance: SurfaceDistanceToEpicenter,
distance: DistanceToEpicenter,
magnitude: number,
depth: number,
siteEffect: number = 1.751
) => {
const pga =
1.657 * Math.exp(1.533 * magnitude) * distance ** -1.607 * siteEffect;
let i = pgaToFloat(pga);
if (i > 3) {
const long = 10 ** (0.5 * magnitude - 1.85) / 2;
const hypocenterDistance =
(depth ** 2 + surfaceDistance ** 2) ** 0.5 - long;
const x = Math.max(hypocenterDistance, 3);
const gpv600 =
10 **
(0.58 * magnitude +
0.0038 * depth -
1.29 -
Math.log10(x + 0.0028 * 10 ** (0.5 * magnitude)) -
0.002 * x);
const arv = 1.0;
const pgv400 = gpv600 * 1.31;
const pgv = pgv400 * arv;
i = 2.68 + 1.72 * Math.log10(pgv);
}
return i;
};
export const calculateExpectedIntensity = (
lnglat: LngLatObject,
magnitude: number,
depth: number
) => {
const intensity: Record<number, number> = {};
let maxIntensity = 0;
for (let i = 0, k = Object.keys(region), n = k.length; i < n; i++) {
const cityName = k[i] as keyof typeof region;
const city = region[cityName];
for (let j = 0, l = Object.keys(city), p = l.length; j < p; j++) {
const townName = l[j] as keyof typeof city;
const town: {
code: number;
lat: number;
lon: number;
site: number;
site_s: number;
site_d: number;
id: string;
} = city[townName];
const { surfaceDistance, distance } = calculateEpicenterDistance(lnglat)({
lat: town.lat,
lng: town.lon,
})(depth);
const int = calculateIntensity(
surfaceDistance,
distance,
magnitude,
depth,
town.site
);
if (int > maxIntensity) {
maxIntensity = int;
}
intensity[town.code] = roundIntensity(int);
}
}
return { intensity, maxIntensity };
};
export const calculateLocalExpectedWaveTime = (
distance: DistanceToEpicenter,
depth: number,
time: number
) => {
const tr_time = speedModel(depth, distance);
return {
p: time + tr_time.Ptime * 1000,
s: time + tr_time.Stime * 1000,
};
};
export const kmToPixels = (
kilometers: number,
latitude: number,
zoomLevel: number
) =>
(kilometers * 1000) /
((EarthCircumference * Math.cos((latitude * Math.PI) / 180)) /
Math.pow(2, zoomLevel + 8.983));