aewx-metar-parser 3.0.2 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -1
- package/README.md +7 -3
- package/dist/convert.js +27 -32
- package/dist/convert.test.js +1 -1
- package/dist/metar-parser-helpers.js +119 -0
- package/dist/metar-parser.js +164 -237
- package/dist/metar-parser.test.js +187 -86
- package/dist/metar-parser.type.js +1 -0
- package/docs/KEYW.json +57 -0
- package/docs/KFSO.json +87 -0
- package/eslint.config.js +11 -8
- package/package.json +10 -9
- package/src/convert.test.ts +1 -1
- package/src/convert.ts +35 -40
- package/src/metar-parser-helpers.ts +155 -0
- package/src/metar-parser.test.ts +205 -86
- package/src/metar-parser.ts +203 -329
- package/src/metar-parser.type.ts +85 -0
- package/types/convert.d.ts +9 -14
- package/types/convert.d.ts.map +1 -1
- package/types/metar-parser-helpers.d.ts +29 -0
- package/types/metar-parser-helpers.d.ts.map +1 -0
- package/types/metar-parser.d.ts +1 -61
- package/types/metar-parser.d.ts.map +1 -1
- package/types/metar-parser.type.d.ts +74 -0
- package/types/metar-parser.type.d.ts.map +1 -0
- package/tsconfig.tsbuildinfo +0 -1
package/src/metar-parser.ts
CHANGED
|
@@ -1,161 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as convert from "./convert.js";
|
|
2
|
+
import * as MetarParserHelpers from "./metar-parser-helpers.js";
|
|
3
|
+
import type { MetarCloudCode, Metar } from "./metar-parser.type.js";
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface MetarVisibility {
|
|
18
|
-
miles: number;
|
|
19
|
-
miles_text: string;
|
|
20
|
-
meters: number;
|
|
21
|
-
meters_text: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type MetarCloudCode = "FEW" | "SCT" | "BKN" | "OVC";
|
|
25
|
-
|
|
26
|
-
export interface MetarCloud {
|
|
27
|
-
code: MetarCloudCode;
|
|
28
|
-
feet: number;
|
|
29
|
-
meters: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface MetarCeiling {
|
|
33
|
-
feet: number | null;
|
|
34
|
-
meters: number | null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface MetarTemperature {
|
|
38
|
-
celsius: number | null;
|
|
39
|
-
fahrenheit: number | null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface MetarHumidity {
|
|
43
|
-
percent: number | null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface MetarPressure {
|
|
47
|
-
hg: number | null;
|
|
48
|
-
kpa: number | null;
|
|
49
|
-
mb: number | null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export type MetarIcaoFlightCategory = "VFR" | "IFR";
|
|
53
|
-
|
|
54
|
-
export type MetarFlightCategory = MetarIcaoFlightCategory | "MVFR" | "LIFR";
|
|
55
|
-
|
|
56
|
-
export interface Metar {
|
|
57
|
-
raw_text: string;
|
|
58
|
-
raw_parts: string[];
|
|
59
|
-
icao: string;
|
|
60
|
-
observed: Date;
|
|
61
|
-
forecastFrom: Date | null;
|
|
62
|
-
forecastTo: Date | null;
|
|
63
|
-
|
|
64
|
-
wind: MetarWind;
|
|
65
|
-
visibility: MetarVisibility;
|
|
66
|
-
conditions: MetarCode[];
|
|
67
|
-
clouds: MetarCloud[];
|
|
68
|
-
ceiling: MetarCeiling;
|
|
69
|
-
temperature: MetarTemperature;
|
|
70
|
-
dewpoint: MetarTemperature;
|
|
71
|
-
humidity: MetarHumidity;
|
|
72
|
-
barometer: MetarPressure;
|
|
73
|
-
flight_category: MetarFlightCategory | null;
|
|
74
|
-
icao_flight_category: MetarIcaoFlightCategory | null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
class MetarParserHelpers {
|
|
78
|
-
/**
|
|
79
|
-
* @param {Number} value dito
|
|
80
|
-
* @param {Number} toNext round to next full xxxx
|
|
81
|
-
* @returns {Number} rounded value
|
|
82
|
-
*/
|
|
83
|
-
static round(value: number, toNext: number = 500): number {
|
|
84
|
-
return Math.round(value / toNext) * toNext;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
static getVisibility(visibility_miles: number, visibility_meters: number): MetarVisibility {
|
|
88
|
-
return {
|
|
89
|
-
miles: visibility_miles,
|
|
90
|
-
miles_text: String(MetarParserHelpers.round(visibility_miles, 0.5)),
|
|
91
|
-
meters: visibility_meters,
|
|
92
|
-
meters_text: String(MetarParserHelpers.round(visibility_meters, 500)),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
static getCloud(code: MetarCloudCode, hundredFeet: string): MetarCloud {
|
|
97
|
-
const feet = Number(hundredFeet) * 100;
|
|
98
|
-
return {
|
|
99
|
-
code: code,
|
|
100
|
-
feet: feet,
|
|
101
|
-
meters: convert.feetToMeters(feet),
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
static getTemperature(temperatureString: string): MetarTemperature {
|
|
106
|
-
const temperature = Number(temperatureString.replace("M", "-"));
|
|
107
|
-
return {
|
|
108
|
-
celsius: temperature,
|
|
109
|
-
fahrenheit: convert.celsiusToFahrenheit(temperature),
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* @see http://andrew.rsmas.miami.edu/bmcnoldy/Humidity.html
|
|
115
|
-
*/
|
|
116
|
-
static getHumidity(temp: MetarTemperature, dew: MetarTemperature): number | null {
|
|
117
|
-
if (dew.celsius === null || temp.celsius === null) {
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
return (
|
|
121
|
-
(Math.exp((17.625 * dew.celsius) / (243.04 + dew.celsius)) /
|
|
122
|
-
Math.exp((17.625 * temp.celsius) / (243.04 + temp.celsius))) *
|
|
123
|
-
100
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
static getFlightCategory(visibility: MetarVisibility, ceiling: MetarCeiling): MetarFlightCategory {
|
|
128
|
-
if (visibility.miles > 5 && (!ceiling.feet || ceiling.feet > 3000)) {
|
|
129
|
-
return "VFR";
|
|
130
|
-
} else if (visibility.miles >= 3 && (!ceiling.feet || ceiling.feet >= 1000)) {
|
|
131
|
-
return "MVFR";
|
|
132
|
-
} else if (visibility.miles >= 1 && (!ceiling.feet || ceiling.feet >= 500)) {
|
|
133
|
-
return "IFR";
|
|
134
|
-
}
|
|
135
|
-
return "LIFR";
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
static getIcaoFLightCategory(visibility: MetarVisibility, ceiling: MetarCeiling): MetarIcaoFlightCategory {
|
|
139
|
-
return visibility.meters >= 5000 && (!ceiling.feet || ceiling.feet >= 1500) ? "VFR" : "IFR";
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
static getCeiling(clouds: MetarCloud[]): MetarCloud | undefined {
|
|
143
|
-
return clouds.find((c) => {
|
|
144
|
-
return c.code === "BKN" || c.code === "OVC";
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
static metarSplit(metarString: string): string[] {
|
|
149
|
-
return metarString
|
|
150
|
-
.trim()
|
|
151
|
-
.replace(/(\s)(\d)\s(\d)\/(\d)(SM)/, function (all, a, b, c, d, e) {
|
|
152
|
-
// convert visbility range like `1 1/2 SM`
|
|
153
|
-
return a + (Number(b) * Number(d) + Number(c)) + "/" + d + e;
|
|
154
|
-
})
|
|
155
|
-
.replace(/\n/, " \\n ")
|
|
156
|
-
.split(/\s+/);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
5
|
+
/**
|
|
6
|
+
* - 0: Initial state, search for ICAO code
|
|
7
|
+
* - 1: Search for observed date
|
|
8
|
+
* - 2: Search for wind
|
|
9
|
+
* - 3: Searchh for visibility
|
|
10
|
+
* - 4: Search for conditions
|
|
11
|
+
* - 5: Search for clouds
|
|
12
|
+
* - 6: Search for temperature
|
|
13
|
+
* - 7: Search for pressure
|
|
14
|
+
* - 8: Done
|
|
15
|
+
*/
|
|
16
|
+
type ParserMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
|
|
159
17
|
|
|
160
18
|
/**
|
|
161
19
|
* Convert METAR string into structured object.
|
|
@@ -166,69 +24,22 @@ class MetarParserHelpers {
|
|
|
166
24
|
* reponse of the data property of https://www.checkwxapi.com/metar
|
|
167
25
|
*/
|
|
168
26
|
export const metarParser = (metarString: string): Metar => {
|
|
169
|
-
const metarObject
|
|
170
|
-
raw_text: metarString,
|
|
171
|
-
raw_parts: MetarParserHelpers.metarSplit(metarString),
|
|
172
|
-
icao: "",
|
|
173
|
-
observed: new Date(),
|
|
174
|
-
forecastFrom: null,
|
|
175
|
-
forecastTo: null,
|
|
176
|
-
wind: {
|
|
177
|
-
degrees: null,
|
|
178
|
-
speed_kts: 0,
|
|
179
|
-
speed_mps: 0,
|
|
180
|
-
gust_kts: null,
|
|
181
|
-
gust_mps: null,
|
|
182
|
-
degrees_from: null,
|
|
183
|
-
degrees_to: null,
|
|
184
|
-
},
|
|
185
|
-
visibility: {
|
|
186
|
-
miles: 10,
|
|
187
|
-
miles_text: "",
|
|
188
|
-
meters: 9999,
|
|
189
|
-
meters_text: "",
|
|
190
|
-
},
|
|
191
|
-
conditions: [],
|
|
192
|
-
clouds: [],
|
|
193
|
-
ceiling: {
|
|
194
|
-
feet: null,
|
|
195
|
-
meters: null,
|
|
196
|
-
},
|
|
197
|
-
temperature: {
|
|
198
|
-
celsius: null,
|
|
199
|
-
fahrenheit: null,
|
|
200
|
-
},
|
|
201
|
-
dewpoint: {
|
|
202
|
-
celsius: null,
|
|
203
|
-
fahrenheit: null,
|
|
204
|
-
},
|
|
205
|
-
humidity: {
|
|
206
|
-
percent: null,
|
|
207
|
-
},
|
|
208
|
-
barometer: {
|
|
209
|
-
hg: null,
|
|
210
|
-
kpa: null,
|
|
211
|
-
mb: null,
|
|
212
|
-
},
|
|
213
|
-
flight_category: null,
|
|
214
|
-
icao_flight_category: null,
|
|
215
|
-
};
|
|
27
|
+
const metarObject = initializeMetarObject(metarString);
|
|
216
28
|
|
|
217
|
-
let mode = 0;
|
|
29
|
+
let mode: ParserMode = 0;
|
|
218
30
|
let isTAF = false;
|
|
219
31
|
metarObject.raw_parts.forEach((metarPart) => {
|
|
220
|
-
let match;
|
|
221
32
|
if (mode === 0 && metarPart.match(/^(METAR|TAF)$/)) {
|
|
222
33
|
isTAF = metarPart === "TAF";
|
|
223
34
|
return;
|
|
224
35
|
}
|
|
225
|
-
if (!isTAF && mode > 1 && mode < 3 && metarPart.match(/^(\d
|
|
36
|
+
if (!isTAF && mode > 1 && mode < 3 && metarPart.match(/^(\d+|\/{2,4})(?:\/(\d+))?(SM)?$/)) {
|
|
226
37
|
mode = 3; // no wind reported
|
|
227
38
|
}
|
|
228
|
-
if (mode > 2 && mode < 5 && metarPart.match(/^(FEW|SCT|BKN|OVC)(\d+)?/)) {
|
|
39
|
+
if (mode > 2 && mode < 5 && metarPart.match(/^(FEW|SCT|BKN|OVC|VV)(\d+)?/)) {
|
|
229
40
|
mode = 5; // no visibility / conditions reported
|
|
230
41
|
}
|
|
231
|
-
if (mode > 3 && mode < 6 && metarPart.match(
|
|
42
|
+
if (mode > 3 && mode < 6 && metarPart.match(/^(M?\d+|\/\/)\/(M?\d+|\/\/)?$/)) {
|
|
232
43
|
mode = 6; // end of clouds
|
|
233
44
|
}
|
|
234
45
|
if (metarPart.match(/^(TEMPO|FM\d+|\\n)$/)) {
|
|
@@ -237,161 +48,224 @@ export const metarParser = (metarString: string): Metar => {
|
|
|
237
48
|
|
|
238
49
|
switch (mode) {
|
|
239
50
|
case 0:
|
|
240
|
-
// ICAO Code
|
|
241
51
|
metarObject.icao = metarPart;
|
|
242
52
|
mode = 1;
|
|
243
53
|
break;
|
|
244
54
|
case 1:
|
|
245
|
-
|
|
246
|
-
match = metarPart.match(/^(\d\d)(\d\d)(\d\d)Z$/);
|
|
247
|
-
if (match) {
|
|
248
|
-
metarObject.observed = new Date();
|
|
249
|
-
metarObject.observed.setUTCDate(Number(match[1]));
|
|
250
|
-
metarObject.observed.setUTCHours(Number(match[2]));
|
|
251
|
-
metarObject.observed.setUTCMinutes(Number(match[3]));
|
|
252
|
-
mode = 2;
|
|
253
|
-
}
|
|
55
|
+
mode = parseObservedDate(metarPart, metarObject, mode);
|
|
254
56
|
break;
|
|
255
57
|
case 2:
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
let gust_kts = match[3] ? Number(match[3]) : null;
|
|
261
|
-
if (match[4] === "KPH") {
|
|
262
|
-
speed_kts = convert.kphToMps(speed_kts);
|
|
263
|
-
gust_kts = gust_kts ? convert.kphToMps(gust_kts) : null;
|
|
264
|
-
match[4] = "MPS";
|
|
265
|
-
}
|
|
266
|
-
const degrees = match[1] === "VRB" ? null : Number(match[1]);
|
|
267
|
-
|
|
268
|
-
metarObject.wind = {
|
|
269
|
-
degrees: degrees,
|
|
270
|
-
speed_kts: match[4] === "MPS" ? convert.mpsToKts(speed_kts) : speed_kts,
|
|
271
|
-
speed_mps: match[4] === "MPS" ? speed_kts : convert.ktsToMps(speed_kts),
|
|
272
|
-
gust_kts: gust_kts ? (match[4] === "MPS" ? convert.mpsToKts(gust_kts) : gust_kts) : null,
|
|
273
|
-
gust_mps: gust_kts ? (match[4] === "MPS" ? gust_kts : convert.ktsToMps(gust_kts)) : null,
|
|
274
|
-
degrees_from: null,
|
|
275
|
-
degrees_to: null,
|
|
276
|
-
};
|
|
277
|
-
if (match[1] === "VRB") {
|
|
278
|
-
metarObject.wind.degrees_from = 0;
|
|
279
|
-
metarObject.wind.degrees_to = 359;
|
|
280
|
-
}
|
|
281
|
-
mode = 3;
|
|
282
|
-
break;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Forecast Date
|
|
286
|
-
match = metarPart.match(/^(\d\d)(\d\d)\/(\d\d)(\d\d)$/);
|
|
287
|
-
if (match) {
|
|
288
|
-
metarObject.forecastFrom = new Date();
|
|
289
|
-
metarObject.forecastFrom.setUTCDate(Number(match[1]));
|
|
290
|
-
metarObject.forecastFrom.setUTCHours(Number(match[2]));
|
|
291
|
-
metarObject.forecastFrom.setUTCMinutes(0);
|
|
292
|
-
|
|
293
|
-
metarObject.forecastTo = new Date();
|
|
294
|
-
metarObject.forecastTo.setUTCDate(Number(match[3]));
|
|
295
|
-
metarObject.forecastTo.setUTCHours(Number(match[4]));
|
|
296
|
-
metarObject.forecastTo.setUTCMinutes(0);
|
|
297
|
-
break;
|
|
58
|
+
mode = parseWind(metarPart, metarObject, mode);
|
|
59
|
+
if (mode === 2) {
|
|
60
|
+
parseForecastDate(metarPart, metarObject);
|
|
61
|
+
// stays in mode
|
|
298
62
|
}
|
|
299
63
|
break;
|
|
300
64
|
case 3:
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
metarObject.visibility = MetarParserHelpers.getVisibility(
|
|
306
|
-
match[3] && match[3] === "SM" ? visibility : convert.metersToMiles(visibility),
|
|
307
|
-
match[3] && match[3] === "SM" ? convert.milesToMeters(visibility) : visibility,
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
mode = 4;
|
|
311
|
-
} else if (metarPart === "CAVOK" || metarPart === "CLR") {
|
|
312
|
-
metarObject.visibility = MetarParserHelpers.getVisibility(10, convert.milesToMeters(10));
|
|
313
|
-
mode = 5; // no clouds & conditions reported
|
|
314
|
-
} else if (metarObject.wind) {
|
|
315
|
-
// Variable wind direction
|
|
316
|
-
match = metarPart.match(/^(\d+)V(\d+)$/);
|
|
317
|
-
if (match) {
|
|
318
|
-
metarObject.wind.degrees_from = Number(match[1]);
|
|
319
|
-
metarObject.wind.degrees_to = Number(match[2]);
|
|
320
|
-
}
|
|
65
|
+
mode = parseVisibility(metarPart, metarObject, mode);
|
|
66
|
+
if (mode === 3) {
|
|
67
|
+
parseVariableWind(metarPart, metarObject);
|
|
68
|
+
// stays in mode
|
|
321
69
|
}
|
|
322
70
|
break;
|
|
323
71
|
case 4:
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (match) {
|
|
327
|
-
if (!metarObject.conditions) {
|
|
328
|
-
metarObject.conditions = [];
|
|
329
|
-
}
|
|
330
|
-
match
|
|
331
|
-
.filter((m, index) => {
|
|
332
|
-
return index !== 0 && m;
|
|
333
|
-
})
|
|
334
|
-
.forEach((m) => {
|
|
335
|
-
metarObject.conditions.push({ code: m });
|
|
336
|
-
});
|
|
337
|
-
// may occur multiple times
|
|
338
|
-
}
|
|
72
|
+
parseCondition(metarPart, metarObject);
|
|
73
|
+
// may occur multiple times
|
|
339
74
|
break;
|
|
340
75
|
case 5:
|
|
341
|
-
// Clouds
|
|
342
76
|
parseClouds(metarPart, metarObject);
|
|
343
77
|
// may occur multiple times
|
|
344
78
|
break;
|
|
345
79
|
case 6:
|
|
346
|
-
// Temperature
|
|
347
80
|
mode = parseTemperature(metarPart, metarObject, mode);
|
|
348
81
|
break;
|
|
349
82
|
case 7:
|
|
350
|
-
|
|
351
|
-
match = metarPart.match(/^(Q|A)(\d+)/);
|
|
352
|
-
if (match) {
|
|
353
|
-
let hg = Number(match[2]);
|
|
354
|
-
hg /= match[1] === "Q" ? 10 : 100;
|
|
355
|
-
metarObject.barometer = {
|
|
356
|
-
hg: match[1] === "Q" ? convert.kpaToInhg(hg) : hg,
|
|
357
|
-
kpa: match[1] === "Q" ? hg : convert.inhgToKpa(hg),
|
|
358
|
-
mb: match[1] === "Q" ? hg * 10 : convert.inhgToKpa(hg * 10),
|
|
359
|
-
};
|
|
360
|
-
mode = 8;
|
|
361
|
-
}
|
|
83
|
+
mode = parsePressure(metarPart, metarObject, mode);
|
|
362
84
|
break;
|
|
363
85
|
}
|
|
364
86
|
});
|
|
365
87
|
|
|
366
|
-
|
|
367
|
-
metarObject.
|
|
368
|
-
metarObject.
|
|
369
|
-
metarObject.flight_category = MetarParserHelpers.getFlightCategory(metarObject.visibility, metarObject.ceiling);
|
|
370
|
-
metarObject.icao_flight_category = MetarParserHelpers.getIcaoFLightCategory(
|
|
371
|
-
metarObject.visibility,
|
|
372
|
-
metarObject.ceiling,
|
|
373
|
-
);
|
|
88
|
+
metarObject.ceiling = MetarParserHelpers.metarCeiling(metarObject.clouds);
|
|
89
|
+
metarObject.flight_category = MetarParserHelpers.flightCategoryUs(metarObject.visibility, metarObject.ceiling);
|
|
90
|
+
metarObject.icao_flight_category = MetarParserHelpers.flightCategoryIcao(metarObject.visibility, metarObject.ceiling);
|
|
374
91
|
|
|
375
92
|
return metarObject;
|
|
376
93
|
};
|
|
377
94
|
|
|
95
|
+
// -----------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
function parseObservedDate(metarPart: string, metarObject: Metar, mode: ParserMode): ParserMode {
|
|
98
|
+
const match = metarPart.match(/^(\d\d)(\d\d)(\d\d)Z$/);
|
|
99
|
+
if (!match) {
|
|
100
|
+
return mode;
|
|
101
|
+
}
|
|
102
|
+
metarObject.observed = MetarParserHelpers.date(Number(match[1]), Number(match[2]), Number(match[3]));
|
|
103
|
+
return 2;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function parseWind(metarPart: string, metarObject: Metar, mode: ParserMode): ParserMode {
|
|
107
|
+
// Case: Unkonw wind
|
|
108
|
+
if (metarPart.match(/^\/+(KT|MPS|KPH)$/)) {
|
|
109
|
+
return 3;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const match = metarPart.match(/^(\d\d\d|VRB)P?(\d+)(?:G(\d+))?(KT|MPS|KPH)/);
|
|
113
|
+
if (!match) {
|
|
114
|
+
return mode;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let speed_kts = Number(match[2]);
|
|
118
|
+
let gust_kts = match[3] ? Number(match[3]) : null;
|
|
119
|
+
if (match[4] === "KPH") {
|
|
120
|
+
speed_kts = convert.kphToMps(speed_kts);
|
|
121
|
+
gust_kts = gust_kts ? convert.kphToMps(gust_kts) : null;
|
|
122
|
+
match[4] = "MPS";
|
|
123
|
+
}
|
|
124
|
+
const degrees = match[1] === "VRB" ? null : Number(match[1]);
|
|
125
|
+
|
|
126
|
+
metarObject.wind = {
|
|
127
|
+
degrees: degrees,
|
|
128
|
+
speed_kts: match[4] === "MPS" ? convert.mpsToKts(speed_kts) : speed_kts,
|
|
129
|
+
speed_mps: match[4] === "MPS" ? speed_kts : convert.ktsToMps(speed_kts),
|
|
130
|
+
gust_kts: gust_kts ? (match[4] === "MPS" ? convert.mpsToKts(gust_kts) : gust_kts) : null,
|
|
131
|
+
gust_mps: gust_kts ? (match[4] === "MPS" ? gust_kts : convert.ktsToMps(gust_kts)) : null,
|
|
132
|
+
degrees_from: null,
|
|
133
|
+
degrees_to: null,
|
|
134
|
+
};
|
|
135
|
+
if (match[1] === "VRB") {
|
|
136
|
+
metarObject.wind.degrees_from = 0;
|
|
137
|
+
metarObject.wind.degrees_to = 359;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return 3;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function parseForecastDate(metarPart: string, metarObject: Metar): void {
|
|
144
|
+
const [, fromDate, fromHours, toDate, toHours] = metarPart.match(/^(\d\d)(\d\d)\/(\d\d)(\d\d)$/) || [];
|
|
145
|
+
if (!fromDate) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
metarObject.forecastFrom = MetarParserHelpers.date(Number(fromDate), Number(fromHours), 0);
|
|
149
|
+
metarObject.forecastTo = MetarParserHelpers.date(Number(toDate), Number(toHours), 0);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function parseVisibility(metarPart: string, metarObject: Metar, mode: ParserMode): ParserMode {
|
|
153
|
+
// Case: No clouds & conditions reported
|
|
154
|
+
if (metarPart === "CAVOK" || metarPart === "CLR") {
|
|
155
|
+
metarObject.visibility = MetarParserHelpers.metarVisibility(10, convert.milesToMeters(10));
|
|
156
|
+
return 5;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const [, digits, fraction, unit] = metarPart.match(/^[MP]?(\d+|\/{2,4})(?:\/(\d+))?(SM)?$/) || [];
|
|
160
|
+
if (digits === undefined) {
|
|
161
|
+
return mode;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Case: Explicitly unknown visibility
|
|
165
|
+
if (digits.match(/^\/+/)) {
|
|
166
|
+
return 4;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Case: Known visibility, possibly as fraction
|
|
170
|
+
const visibility = fraction ? Number(digits) / Number(fraction) : Number(digits);
|
|
171
|
+
metarObject.visibility = MetarParserHelpers.metarVisibility(
|
|
172
|
+
unit === "SM" ? visibility : convert.metersToMiles(visibility),
|
|
173
|
+
unit === "SM" ? convert.milesToMeters(visibility) : visibility,
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
return 4;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function parseVariableWind(metarPart: string, metarObject: Metar): void {
|
|
180
|
+
if (!metarObject.wind) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const [, degreesFrom, degreesTo] = metarPart.match(/^(\d+)V(\d+)$/) || [];
|
|
185
|
+
if (degreesFrom || degreesTo) {
|
|
186
|
+
metarObject.wind.degrees_from = Number(degreesFrom);
|
|
187
|
+
metarObject.wind.degrees_to = Number(degreesTo);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function parseCondition(metarPart: string, metarObject: Metar): void {
|
|
192
|
+
const match = metarPart.match(/^(\+|-|VC|RE)?([A-Z][A-Z])([A-Z][A-Z])?([A-Z][A-Z])?$/);
|
|
193
|
+
if (match) {
|
|
194
|
+
if (!metarObject.conditions) {
|
|
195
|
+
metarObject.conditions = [];
|
|
196
|
+
}
|
|
197
|
+
match
|
|
198
|
+
.filter((m, index) => {
|
|
199
|
+
return index !== 0 && m;
|
|
200
|
+
})
|
|
201
|
+
.forEach((m) => {
|
|
202
|
+
metarObject.conditions.push({ code: m });
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
378
207
|
function parseClouds(metarPart: string, metarObject: Metar) {
|
|
379
|
-
const [, cloudType, cloudAltitude] = metarPart.match(/^(FEW|SCT|BKN|OVC)(\d+)/) || [];
|
|
208
|
+
const [, cloudType, cloudAltitude] = metarPart.match(/^(FEW|SCT|BKN|OVC|VV)(\d+)/) || [];
|
|
380
209
|
if (cloudType && cloudAltitude) {
|
|
381
|
-
metarObject.clouds.push(MetarParserHelpers.
|
|
210
|
+
metarObject.clouds.push(MetarParserHelpers.metarCloud(cloudType as MetarCloudCode, cloudAltitude));
|
|
382
211
|
}
|
|
383
212
|
}
|
|
384
213
|
|
|
385
|
-
function parseTemperature(metarPart: string, metarObject: Metar, mode:
|
|
386
|
-
const [, temp, dewpoint] = metarPart.match(/^(M?\d
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
metarObject.
|
|
392
|
-
metarObject.dewpoint = MetarParserHelpers.getTemperature(dewpoint);
|
|
393
|
-
metarObject.humidity.percent = MetarParserHelpers.getHumidity(metarObject.temperature, metarObject.dewpoint);
|
|
214
|
+
function parseTemperature(metarPart: string, metarObject: Metar, mode: ParserMode): ParserMode {
|
|
215
|
+
const [, temp, dewpoint] = metarPart.match(/^(M?\d+|\/\/)\/(M?\d+|\/\/)?$/) || [];
|
|
216
|
+
|
|
217
|
+
if (temp) {
|
|
218
|
+
metarObject.temperature = MetarParserHelpers.metarTemperature(temp ?? "//");
|
|
219
|
+
metarObject.dewpoint = MetarParserHelpers.metarTemperature(dewpoint ?? "//");
|
|
220
|
+
metarObject.humidity = MetarParserHelpers.relativeHumidity(metarObject.temperature, metarObject.dewpoint);
|
|
394
221
|
return 7;
|
|
395
222
|
}
|
|
396
223
|
return mode;
|
|
397
224
|
}
|
|
225
|
+
|
|
226
|
+
function parsePressure(metarPart: string, metarObject: Metar, mode: ParserMode): ParserMode {
|
|
227
|
+
const [, unit, value] = metarPart.match(/^(Q|A)(\d+)/) || [];
|
|
228
|
+
if (!unit || !value) {
|
|
229
|
+
return mode;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const hg = Number(value) / (unit === "Q" ? 10 : 100);
|
|
233
|
+
metarObject.barometer = {
|
|
234
|
+
hg: unit === "Q" ? convert.kpaToInhg(hg) : hg,
|
|
235
|
+
kpa: unit === "Q" ? hg : convert.inhgToKpa(hg),
|
|
236
|
+
mb: unit === "Q" ? hg * 10 : convert.inhgToKpa(hg * 10),
|
|
237
|
+
};
|
|
238
|
+
return 8;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// -----------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
function initializeMetarObject(metarString: string): Metar {
|
|
244
|
+
return {
|
|
245
|
+
raw_text: metarString,
|
|
246
|
+
raw_parts: MetarParserHelpers.metarSplit(metarString),
|
|
247
|
+
icao: "",
|
|
248
|
+
observed: new Date(0),
|
|
249
|
+
forecastFrom: null,
|
|
250
|
+
forecastTo: null,
|
|
251
|
+
wind: {
|
|
252
|
+
degrees: null,
|
|
253
|
+
speed_kts: null,
|
|
254
|
+
speed_mps: null,
|
|
255
|
+
gust_kts: null,
|
|
256
|
+
gust_mps: null,
|
|
257
|
+
degrees_from: null,
|
|
258
|
+
degrees_to: null,
|
|
259
|
+
},
|
|
260
|
+
visibility: null,
|
|
261
|
+
conditions: [],
|
|
262
|
+
clouds: [],
|
|
263
|
+
ceiling: null,
|
|
264
|
+
temperature: null,
|
|
265
|
+
dewpoint: null,
|
|
266
|
+
humidity: null,
|
|
267
|
+
barometer: null,
|
|
268
|
+
flight_category: null,
|
|
269
|
+
icao_flight_category: null,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export interface MetarCode {
|
|
2
|
+
code: string;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface MetarWind {
|
|
6
|
+
degrees: number | null;
|
|
7
|
+
speed_kts: number | null;
|
|
8
|
+
speed_mps: number | null;
|
|
9
|
+
gust_kts: number | null;
|
|
10
|
+
gust_mps: number | null;
|
|
11
|
+
degrees_from: number | null;
|
|
12
|
+
degrees_to: number | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface MetarVisibility {
|
|
16
|
+
miles: number;
|
|
17
|
+
miles_text: string;
|
|
18
|
+
meters: number;
|
|
19
|
+
meters_text: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type MetarCloudCode = "FEW" | "SCT" | "BKN" | "OVC" | "VV";
|
|
23
|
+
|
|
24
|
+
export interface MetarCloud {
|
|
25
|
+
code: MetarCloudCode;
|
|
26
|
+
feet: number;
|
|
27
|
+
meters: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Ceiling values of lowest cloud layer reporting `BKN`, `OVC` or `VV`.
|
|
32
|
+
* Values will be `null` if no such layer was found.
|
|
33
|
+
*/
|
|
34
|
+
export interface MetarCeiling {
|
|
35
|
+
/**
|
|
36
|
+
* Ceiling in feet of lowest cloud layer reporting `BKN`, `OVC` or `VV`.
|
|
37
|
+
* Ceiling in feet will be `null` if no such layer was found.
|
|
38
|
+
*/
|
|
39
|
+
feet: number;
|
|
40
|
+
/**
|
|
41
|
+
* Ceiling in meters of lowest cloud layer reporting `BKN`, `OVC` or `VV`.
|
|
42
|
+
* Ceiling in meters will be `null` if no such layer was found.
|
|
43
|
+
*/
|
|
44
|
+
meters: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface MetarTemperature {
|
|
48
|
+
celsius: number;
|
|
49
|
+
fahrenheit: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface MetarHumidity {
|
|
53
|
+
percent: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface MetarPressure {
|
|
57
|
+
hg: number;
|
|
58
|
+
kpa: number;
|
|
59
|
+
mb: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type MetarIcaoFlightCategory = "VFR" | "IFR";
|
|
63
|
+
|
|
64
|
+
export type MetarFlightCategory = MetarIcaoFlightCategory | "MVFR" | "LIFR";
|
|
65
|
+
|
|
66
|
+
export interface Metar {
|
|
67
|
+
raw_text: string;
|
|
68
|
+
raw_parts: string[];
|
|
69
|
+
icao: string;
|
|
70
|
+
observed: Date;
|
|
71
|
+
forecastFrom: Date | null;
|
|
72
|
+
forecastTo: Date | null;
|
|
73
|
+
|
|
74
|
+
wind: MetarWind;
|
|
75
|
+
visibility: MetarVisibility | null;
|
|
76
|
+
conditions: MetarCode[];
|
|
77
|
+
clouds: MetarCloud[];
|
|
78
|
+
ceiling: MetarCeiling | null;
|
|
79
|
+
temperature: MetarTemperature | null;
|
|
80
|
+
dewpoint: MetarTemperature | null;
|
|
81
|
+
humidity: MetarHumidity | null;
|
|
82
|
+
barometer: MetarPressure | null;
|
|
83
|
+
flight_category: MetarFlightCategory | null;
|
|
84
|
+
icao_flight_category: MetarIcaoFlightCategory | null;
|
|
85
|
+
}
|