aewx-metar-parser 3.0.2 → 4.0.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 +11 -1
- package/README.md +5 -1
- package/dist/convert.js +27 -32
- package/dist/convert.test.js +1 -1
- package/dist/metar-parser-helpers.js +100 -0
- package/dist/metar-parser.js +149 -212
- package/dist/metar-parser.test.js +159 -68
- package/dist/metar-parser.type.js +1 -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 +129 -0
- package/src/metar-parser.test.ts +174 -68
- package/src/metar-parser.ts +175 -303
- package/src/metar-parser.type.ts +85 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/types/convert.d.ts +9 -14
- package/types/convert.d.ts.map +1 -1
- package/types/metar-parser-helpers.d.ts +23 -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/src/metar-parser.ts
CHANGED
|
@@ -1,161 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
code: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface MetarWind {
|
|
8
|
-
degrees: number | null;
|
|
9
|
-
speed_kts: number;
|
|
10
|
-
speed_mps: number;
|
|
11
|
-
gust_kts: number | null;
|
|
12
|
-
gust_mps: number | null;
|
|
13
|
-
degrees_from: number | null;
|
|
14
|
-
degrees_to: number | null;
|
|
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
|
-
}
|
|
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";
|
|
159
4
|
|
|
160
5
|
/**
|
|
161
6
|
* Convert METAR string into structured object.
|
|
@@ -166,53 +11,7 @@ class MetarParserHelpers {
|
|
|
166
11
|
* reponse of the data property of https://www.checkwxapi.com/metar
|
|
167
12
|
*/
|
|
168
13
|
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
|
-
};
|
|
14
|
+
const metarObject = initializeMetarObject(metarString);
|
|
216
15
|
|
|
217
16
|
let mode = 0;
|
|
218
17
|
let isTAF = false;
|
|
@@ -222,13 +21,13 @@ export const metarParser = (metarString: string): Metar => {
|
|
|
222
21
|
isTAF = metarPart === "TAF";
|
|
223
22
|
return;
|
|
224
23
|
}
|
|
225
|
-
if (!isTAF && mode > 1 && mode < 3 && metarPart.match(/^(\d
|
|
24
|
+
if (!isTAF && mode > 1 && mode < 3 && metarPart.match(/^(\d+|\/{2,4})(?:\/(\d+))?(SM)?$/)) {
|
|
226
25
|
mode = 3; // no wind reported
|
|
227
26
|
}
|
|
228
|
-
if (mode > 2 && mode < 5 && metarPart.match(/^(FEW|SCT|BKN|OVC)(\d+)?/)) {
|
|
27
|
+
if (mode > 2 && mode < 5 && metarPart.match(/^(FEW|SCT|BKN|OVC|VV)(\d+)?/)) {
|
|
229
28
|
mode = 5; // no visibility / conditions reported
|
|
230
29
|
}
|
|
231
|
-
if (mode > 3 && mode < 6 && metarPart.match(
|
|
30
|
+
if (mode > 3 && mode < 6 && metarPart.match(/^(M?\d+|\/\/)\/(M?\d+|\/\/)?$/)) {
|
|
232
31
|
mode = 6; // end of clouds
|
|
233
32
|
}
|
|
234
33
|
if (metarPart.match(/^(TEMPO|FM\d+|\\n)$/)) {
|
|
@@ -237,7 +36,6 @@ export const metarParser = (metarString: string): Metar => {
|
|
|
237
36
|
|
|
238
37
|
switch (mode) {
|
|
239
38
|
case 0:
|
|
240
|
-
// ICAO Code
|
|
241
39
|
metarObject.icao = metarPart;
|
|
242
40
|
mode = 1;
|
|
243
41
|
break;
|
|
@@ -253,71 +51,15 @@ export const metarParser = (metarString: string): Metar => {
|
|
|
253
51
|
}
|
|
254
52
|
break;
|
|
255
53
|
case 2:
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
let speed_kts = Number(match[2]);
|
|
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;
|
|
54
|
+
mode = parseWind(metarPart, metarObject, mode);
|
|
55
|
+
if (mode === 2) {
|
|
56
|
+
parseForecastDate(metarPart, metarObject);
|
|
298
57
|
}
|
|
299
58
|
break;
|
|
300
59
|
case 3:
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
const visibility = match[2] ? Number(match[1]) / Number(match[2]) : Number(match[1]);
|
|
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
|
-
}
|
|
60
|
+
mode = parseVisibility(metarPart, metarObject, mode);
|
|
61
|
+
if (mode === 3) {
|
|
62
|
+
parseVariableWind(metarPart, metarObject);
|
|
321
63
|
}
|
|
322
64
|
break;
|
|
323
65
|
case 4:
|
|
@@ -338,60 +80,190 @@ export const metarParser = (metarString: string): Metar => {
|
|
|
338
80
|
}
|
|
339
81
|
break;
|
|
340
82
|
case 5:
|
|
341
|
-
// Clouds
|
|
342
83
|
parseClouds(metarPart, metarObject);
|
|
343
84
|
// may occur multiple times
|
|
344
85
|
break;
|
|
345
86
|
case 6:
|
|
346
|
-
// Temperature
|
|
347
87
|
mode = parseTemperature(metarPart, metarObject, mode);
|
|
348
88
|
break;
|
|
349
89
|
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
|
-
}
|
|
90
|
+
mode = parsePressure(metarPart, metarObject, mode);
|
|
362
91
|
break;
|
|
363
92
|
}
|
|
364
93
|
});
|
|
365
94
|
|
|
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
|
-
);
|
|
95
|
+
metarObject.ceiling = MetarParserHelpers.metarCeiling(metarObject.clouds);
|
|
96
|
+
metarObject.flight_category = MetarParserHelpers.flightCategoryUs(metarObject.visibility, metarObject.ceiling);
|
|
97
|
+
metarObject.icao_flight_category = MetarParserHelpers.flightCategoryIcao(metarObject.visibility, metarObject.ceiling);
|
|
374
98
|
|
|
375
99
|
return metarObject;
|
|
376
100
|
};
|
|
377
101
|
|
|
102
|
+
// -----------------------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
function parseWind(metarPart: string, metarObject: Metar, mode: number): number {
|
|
105
|
+
// Case: Unkonw wind
|
|
106
|
+
if (metarPart.match(/^\/+(KT|MPS|KPH)$/)) {
|
|
107
|
+
return 3;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const match = metarPart.match(/^(\d\d\d|VRB)P?(\d+)(?:G(\d+))?(KT|MPS|KPH)/);
|
|
111
|
+
if (!match) {
|
|
112
|
+
return mode;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let speed_kts = Number(match[2]);
|
|
116
|
+
let gust_kts = match[3] ? Number(match[3]) : null;
|
|
117
|
+
if (match[4] === "KPH") {
|
|
118
|
+
speed_kts = convert.kphToMps(speed_kts);
|
|
119
|
+
gust_kts = gust_kts ? convert.kphToMps(gust_kts) : null;
|
|
120
|
+
match[4] = "MPS";
|
|
121
|
+
}
|
|
122
|
+
const degrees = match[1] === "VRB" ? null : Number(match[1]);
|
|
123
|
+
|
|
124
|
+
metarObject.wind = {
|
|
125
|
+
degrees: degrees,
|
|
126
|
+
speed_kts: match[4] === "MPS" ? convert.mpsToKts(speed_kts) : speed_kts,
|
|
127
|
+
speed_mps: match[4] === "MPS" ? speed_kts : convert.ktsToMps(speed_kts),
|
|
128
|
+
gust_kts: gust_kts ? (match[4] === "MPS" ? convert.mpsToKts(gust_kts) : gust_kts) : null,
|
|
129
|
+
gust_mps: gust_kts ? (match[4] === "MPS" ? gust_kts : convert.ktsToMps(gust_kts)) : null,
|
|
130
|
+
degrees_from: null,
|
|
131
|
+
degrees_to: null,
|
|
132
|
+
};
|
|
133
|
+
if (match[1] === "VRB") {
|
|
134
|
+
metarObject.wind.degrees_from = 0;
|
|
135
|
+
metarObject.wind.degrees_to = 359;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return 3;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function parseForecastDate(metarPart: string, metarObject: Metar): void {
|
|
142
|
+
const match = metarPart.match(/^(\d\d)(\d\d)\/(\d\d)(\d\d)$/);
|
|
143
|
+
if (!match) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
metarObject.forecastFrom = new Date();
|
|
147
|
+
metarObject.forecastFrom.setUTCDate(Number(match[1]));
|
|
148
|
+
metarObject.forecastFrom.setUTCHours(Number(match[2]));
|
|
149
|
+
metarObject.forecastFrom.setUTCMinutes(0);
|
|
150
|
+
|
|
151
|
+
metarObject.forecastTo = new Date();
|
|
152
|
+
metarObject.forecastTo.setUTCDate(Number(match[3]));
|
|
153
|
+
metarObject.forecastTo.setUTCHours(Number(match[4]));
|
|
154
|
+
metarObject.forecastTo.setUTCMinutes(0);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function parseVisibility(metarPart: string, metarObject: Metar, mode: number): number {
|
|
158
|
+
// Case: No clouds & conditions reported
|
|
159
|
+
if (metarPart === "CAVOK" || metarPart === "CLR") {
|
|
160
|
+
metarObject.visibility = MetarParserHelpers.metarVisibility(10, convert.milesToMeters(10));
|
|
161
|
+
return 5;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const [, digits, fraction, unit] = metarPart.match(/^[MP]?(\d+|\/{2,4})(?:\/(\d+))?(SM)?$/) || [];
|
|
165
|
+
if (digits === undefined) {
|
|
166
|
+
return mode;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Case: Explicitly unknown visibility
|
|
170
|
+
if (digits.match(/^\/+/)) {
|
|
171
|
+
return 4;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Case: Known visibility, possibly as fraction
|
|
175
|
+
const visibility = fraction ? Number(digits) / Number(fraction) : Number(digits);
|
|
176
|
+
metarObject.visibility = MetarParserHelpers.metarVisibility(
|
|
177
|
+
unit === "SM" ? visibility : convert.metersToMiles(visibility),
|
|
178
|
+
unit === "SM" ? convert.milesToMeters(visibility) : visibility,
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
return 4;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function parseVariableWind(metarPart: string, metarObject: Metar): void {
|
|
185
|
+
if (!metarObject.wind) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const match = metarPart.match(/^(\d+)V(\d+)$/);
|
|
190
|
+
if (match) {
|
|
191
|
+
metarObject.wind.degrees_from = Number(match[1]);
|
|
192
|
+
metarObject.wind.degrees_to = Number(match[2]);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
378
196
|
function parseClouds(metarPart: string, metarObject: Metar) {
|
|
379
|
-
const [, cloudType, cloudAltitude] = metarPart.match(/^(FEW|SCT|BKN|OVC)(\d+)/) || [];
|
|
197
|
+
const [, cloudType, cloudAltitude] = metarPart.match(/^(FEW|SCT|BKN|OVC|VV)(\d+)/) || [];
|
|
380
198
|
if (cloudType && cloudAltitude) {
|
|
381
|
-
metarObject.clouds.push(MetarParserHelpers.
|
|
199
|
+
metarObject.clouds.push(MetarParserHelpers.metarCloud(cloudType as MetarCloudCode, cloudAltitude));
|
|
382
200
|
}
|
|
383
201
|
}
|
|
384
202
|
|
|
385
203
|
function parseTemperature(metarPart: string, metarObject: Metar, mode: number): number {
|
|
386
|
-
const [, temp, dewpoint] = metarPart.match(/^(M?\d
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
metarObject.
|
|
392
|
-
|
|
393
|
-
|
|
204
|
+
const [, temp, dewpoint] = metarPart.match(/^(M?\d+|\/\/)\/(M?\d+|\/\/)?$/) || [];
|
|
205
|
+
|
|
206
|
+
if (temp) {
|
|
207
|
+
metarObject.temperature = MetarParserHelpers.metarTemperature(temp ?? "//");
|
|
208
|
+
metarObject.dewpoint = MetarParserHelpers.metarTemperature(dewpoint ?? "//");
|
|
209
|
+
metarObject.humidity.percent = MetarParserHelpers.relativeHumidityPercent(
|
|
210
|
+
metarObject.temperature,
|
|
211
|
+
metarObject.dewpoint,
|
|
212
|
+
);
|
|
394
213
|
return 7;
|
|
395
214
|
}
|
|
396
215
|
return mode;
|
|
397
216
|
}
|
|
217
|
+
|
|
218
|
+
function parsePressure(metarPart: string, metarObject: Metar, mode: number): number {
|
|
219
|
+
const match = metarPart.match(/^(Q|A)(\d+)/);
|
|
220
|
+
if (!match) {
|
|
221
|
+
return mode;
|
|
222
|
+
}
|
|
223
|
+
let hg = Number(match[2]);
|
|
224
|
+
hg /= match[1] === "Q" ? 10 : 100;
|
|
225
|
+
metarObject.barometer = {
|
|
226
|
+
hg: match[1] === "Q" ? convert.kpaToInhg(hg) : hg,
|
|
227
|
+
kpa: match[1] === "Q" ? hg : convert.inhgToKpa(hg),
|
|
228
|
+
mb: match[1] === "Q" ? hg * 10 : convert.inhgToKpa(hg * 10),
|
|
229
|
+
};
|
|
230
|
+
return 8;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// -----------------------------------------------------------------------------
|
|
234
|
+
|
|
235
|
+
function initializeMetarObject(metarString: string): Metar {
|
|
236
|
+
return {
|
|
237
|
+
raw_text: metarString,
|
|
238
|
+
raw_parts: MetarParserHelpers.metarSplit(metarString),
|
|
239
|
+
icao: "",
|
|
240
|
+
observed: new Date(),
|
|
241
|
+
forecastFrom: null,
|
|
242
|
+
forecastTo: null,
|
|
243
|
+
wind: {
|
|
244
|
+
degrees: null,
|
|
245
|
+
speed_kts: null,
|
|
246
|
+
speed_mps: null,
|
|
247
|
+
gust_kts: null,
|
|
248
|
+
gust_mps: null,
|
|
249
|
+
degrees_from: null,
|
|
250
|
+
degrees_to: null,
|
|
251
|
+
},
|
|
252
|
+
visibility: null,
|
|
253
|
+
conditions: [],
|
|
254
|
+
clouds: [],
|
|
255
|
+
ceiling: null,
|
|
256
|
+
temperature: null,
|
|
257
|
+
dewpoint: null,
|
|
258
|
+
humidity: {
|
|
259
|
+
percent: null,
|
|
260
|
+
},
|
|
261
|
+
barometer: {
|
|
262
|
+
hg: null,
|
|
263
|
+
kpa: null,
|
|
264
|
+
mb: null,
|
|
265
|
+
},
|
|
266
|
+
flight_category: null,
|
|
267
|
+
icao_flight_category: null,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
@@ -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` or `OVC`.
|
|
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` or `OVC`.
|
|
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` or `OVC`.
|
|
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 | null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface MetarPressure {
|
|
57
|
+
hg: number | null;
|
|
58
|
+
kpa: number | null;
|
|
59
|
+
mb: number | null;
|
|
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;
|
|
82
|
+
barometer: MetarPressure;
|
|
83
|
+
flight_category: MetarFlightCategory | null;
|
|
84
|
+
icao_flight_category: MetarIcaoFlightCategory | null;
|
|
85
|
+
}
|
package/tsconfig.tsbuildinfo
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["./src/convert.test.ts","./src/convert.ts","./src/metar-parser.test.ts","./src/metar-parser.ts"],"version":"
|
|
1
|
+
{"root":["./src/convert.test.ts","./src/convert.ts","./src/metar-parser-helpers.ts","./src/metar-parser.test.ts","./src/metar-parser.ts","./src/metar-parser.type.ts"],"version":"6.0.3"}
|
package/types/convert.d.ts
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export declare
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
kpaToInhg: (kpa: number) => number;
|
|
11
|
-
kphToMps: (kph: number) => number;
|
|
12
|
-
mpsToKts: (mps: number) => number;
|
|
13
|
-
ktsToMps: (kts: number) => number;
|
|
14
|
-
};
|
|
1
|
+
export declare function celsiusToFahrenheit(celsius: number): number;
|
|
2
|
+
export declare function feetToMeters(feet: number): number;
|
|
3
|
+
export declare function milesToMeters(miles: number): number;
|
|
4
|
+
export declare function metersToMiles(meters: number): number;
|
|
5
|
+
export declare function inhgToKpa(inHg: number): number;
|
|
6
|
+
export declare function kpaToInhg(kpa: number): number;
|
|
7
|
+
export declare function kphToMps(kph: number): number;
|
|
8
|
+
export declare function mpsToKts(mps: number): number;
|
|
9
|
+
export declare function ktsToMps(kts: number): number;
|
|
15
10
|
//# sourceMappingURL=convert.d.ts.map
|
package/types/convert.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAAA,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,UAElD;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,UAExC;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,UAE1C;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,UAE3C;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,UAErC;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,UAEpC;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,UAEnC;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,UAEnC;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,UAEnC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { MetarVisibility, MetarCloudCode, MetarCloud, MetarTemperature, MetarCeiling, MetarFlightCategory as MetarUsFlightCategory, MetarIcaoFlightCategory } from "./metar-parser.type.js";
|
|
2
|
+
/**
|
|
3
|
+
* @param {Number} value dito
|
|
4
|
+
* @param {Number} toNext round to next full xxxx
|
|
5
|
+
* @returns {Number} rounded value
|
|
6
|
+
*/
|
|
7
|
+
export declare function round(value: number, toNext?: number): number;
|
|
8
|
+
export declare function metarVisibility(visibility_miles: number, visibility_meters: number): MetarVisibility;
|
|
9
|
+
export declare function metarCloud(code: MetarCloudCode, hundredFeet: string): MetarCloud;
|
|
10
|
+
export declare function metarTemperature(temperatureString: string): MetarTemperature | null;
|
|
11
|
+
/**
|
|
12
|
+
* @see http://andrew.rsmas.miami.edu/bmcnoldy/Humidity.html
|
|
13
|
+
*/
|
|
14
|
+
export declare function relativeHumidityPercent(temp: MetarTemperature | null, dew: MetarTemperature | null): number | null;
|
|
15
|
+
export declare function flightCategoryUs(visibility: MetarVisibility | null, ceiling: MetarCeiling | null): MetarUsFlightCategory | null;
|
|
16
|
+
export declare function flightCategoryIcao(visibility: MetarVisibility | null, ceiling: MetarCeiling | null): MetarIcaoFlightCategory | null;
|
|
17
|
+
export declare function metarCeiling(clouds: MetarCloud[]): MetarCeiling | null;
|
|
18
|
+
/**
|
|
19
|
+
* @return METAR string split along its white spaces. Will convert fractional
|
|
20
|
+
* visibility values into one concatenated value.
|
|
21
|
+
*/
|
|
22
|
+
export declare function metarSplit(metarString: string): string[];
|
|
23
|
+
//# sourceMappingURL=metar-parser-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metar-parser-helpers.d.ts","sourceRoot":"","sources":["../src/metar-parser-helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,UAAU,EACV,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,IAAI,qBAAqB,EAC5C,uBAAuB,EACxB,MAAM,wBAAwB,CAAC;AAEhC;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,MAAY,GAAG,MAAM,CAEjE;AAED,wBAAgB,eAAe,CAAC,gBAAgB,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,GAAG,eAAe,CAOpG;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,GAAG,UAAU,CAOhF;AAED,wBAAgB,gBAAgB,CAAC,iBAAiB,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CASnF;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,gBAAgB,GAAG,IAAI,EAAE,GAAG,EAAE,gBAAgB,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CASlH;AAED,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,eAAe,GAAG,IAAI,EAClC,OAAO,EAAE,YAAY,GAAG,IAAI,GAC3B,qBAAqB,GAAG,IAAI,CAa9B;AAED,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,eAAe,GAAG,IAAI,EAClC,OAAO,EAAE,YAAY,GAAG,IAAI,GAC3B,uBAAuB,GAAG,IAAI,CAMhC;AAYD,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,YAAY,GAAG,IAAI,CAWtE;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,CASxD"}
|