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 CHANGED
@@ -1,6 +1,16 @@
1
1
  # Change log
2
2
 
3
- ## [Unreleased]
3
+ ## [Unreleaed]
4
+
5
+ ## [4.0.0]
6
+
7
+ - Updated ESLint
8
+ - Visbility may not be provided, returning `null`
9
+ - Temperature / dewpoints may not be provided, returning `null`
10
+ - Wind may not be provided, returning `null`
11
+ - Required values for flight category may not be provided, returning `null`
12
+
13
+ ## [3.0.2] - 2026-08-25
4
14
 
5
15
  - Added array safe guarding
6
16
 
package/README.md CHANGED
@@ -17,10 +17,14 @@ This METAR parser returns the following parts of a METAR string:
17
17
  - Temperature (in °C & °F) & humidity
18
18
  - Barometer pressure (in InHg, kpa & mb)
19
19
 
20
+ It also provides TypeScript type definitions.
21
+
22
+ Please be aware that some values may come up bein `null`. This may be caused by missing information in the original METAR information.
23
+
20
24
  ## Code example
21
25
 
22
26
  ```javascript
23
- import { metarParser } from "./metar-parser.js";
27
+ import { metarParser } from "aewx-metar-parser";
24
28
 
25
29
  const metarObject = metarParser(
26
30
  "KSFO 070121Z 19023KT 1 1/2SM R28R/6000VP6000FT -RA BKN004 BKN013 OVC035 15/12 A2970 RMK AO2 T01500122 PNO $",
package/dist/convert.js CHANGED
@@ -1,32 +1,27 @@
1
- /**
2
- * Convert units
3
- */
4
- export const convert = {
5
- celsiusToFahrenheit: function (celsius) {
6
- return celsius * 1.8 + 32;
7
- },
8
- feetToMeters: function (feet) {
9
- return feet * 0.3048;
10
- },
11
- milesToMeters: function (miles) {
12
- return miles * 1609.344;
13
- },
14
- metersToMiles: function (meters) {
15
- return meters / 1609.344;
16
- },
17
- inhgToKpa: function (inHg) {
18
- return inHg / 0.29529988;
19
- },
20
- kpaToInhg: function (kpa) {
21
- return kpa * 0.29529988;
22
- },
23
- kphToMps: function (kph) {
24
- return (kph / 3600) * 1000;
25
- },
26
- mpsToKts: function (mps) {
27
- return mps * 1.9438445;
28
- },
29
- ktsToMps: function (kts) {
30
- return kts / 1.9438445;
31
- },
32
- };
1
+ export function celsiusToFahrenheit(celsius) {
2
+ return celsius * 1.8 + 32;
3
+ }
4
+ export function feetToMeters(feet) {
5
+ return feet * 0.3048;
6
+ }
7
+ export function milesToMeters(miles) {
8
+ return miles * 1609.344;
9
+ }
10
+ export function metersToMiles(meters) {
11
+ return meters / 1609.344;
12
+ }
13
+ export function inhgToKpa(inHg) {
14
+ return inHg / 0.29529988;
15
+ }
16
+ export function kpaToInhg(kpa) {
17
+ return kpa * 0.29529988;
18
+ }
19
+ export function kphToMps(kph) {
20
+ return (kph / 3600) * 1000;
21
+ }
22
+ export function mpsToKts(mps) {
23
+ return mps * 1.9438445;
24
+ }
25
+ export function ktsToMps(kts) {
26
+ return kts / 1.9438445;
27
+ }
@@ -1,6 +1,6 @@
1
1
  import { strict as assert } from "node:assert";
2
2
  import { describe, it } from "node:test";
3
- import { convert } from "./convert.js";
3
+ import * as convert from "./convert.js";
4
4
  describe("Conversion Tests", () => {
5
5
  it("should convert temperature units correctly", () => {
6
6
  const fahrenheit = convert.celsiusToFahrenheit(1);
@@ -0,0 +1,100 @@
1
+ import * as convert from "./convert.js";
2
+ /**
3
+ * @param {Number} value dito
4
+ * @param {Number} toNext round to next full xxxx
5
+ * @returns {Number} rounded value
6
+ */
7
+ export function round(value, toNext = 500) {
8
+ return Math.round(value / toNext) * toNext;
9
+ }
10
+ export function metarVisibility(visibility_miles, visibility_meters) {
11
+ return {
12
+ miles: visibility_miles,
13
+ miles_text: String(round(visibility_miles, 0.5)),
14
+ meters: visibility_meters,
15
+ meters_text: String(round(visibility_meters, 500)),
16
+ };
17
+ }
18
+ export function metarCloud(code, hundredFeet) {
19
+ const feet = Number(hundredFeet) * 100;
20
+ return {
21
+ code: code,
22
+ feet: feet,
23
+ meters: convert.feetToMeters(feet),
24
+ };
25
+ }
26
+ export function metarTemperature(temperatureString) {
27
+ if (temperatureString === "//") {
28
+ return null;
29
+ }
30
+ const temperature = Number(temperatureString.replace("M", "-"));
31
+ return {
32
+ celsius: temperature,
33
+ fahrenheit: convert.celsiusToFahrenheit(temperature),
34
+ };
35
+ }
36
+ /**
37
+ * @see http://andrew.rsmas.miami.edu/bmcnoldy/Humidity.html
38
+ */
39
+ export function relativeHumidityPercent(temp, dew) {
40
+ if (dew === null || temp === null) {
41
+ return null;
42
+ }
43
+ return ((Math.exp((17.625 * dew.celsius) / (243.04 + dew.celsius)) /
44
+ Math.exp((17.625 * temp.celsius) / (243.04 + temp.celsius))) *
45
+ 100);
46
+ }
47
+ export function flightCategoryUs(visibility, ceiling) {
48
+ if (visibility === null) {
49
+ return null;
50
+ }
51
+ if (visibility.miles > 5 && (ceiling === null || ceiling.feet > 3000)) {
52
+ return "VFR";
53
+ }
54
+ else if (visibility.miles >= 3 && (ceiling === null || ceiling.feet >= 1000)) {
55
+ return "MVFR";
56
+ }
57
+ else if (visibility.miles >= 1 && (ceiling === null || ceiling.feet >= 500)) {
58
+ return "IFR";
59
+ }
60
+ return "LIFR";
61
+ }
62
+ export function flightCategoryIcao(visibility, ceiling) {
63
+ if (visibility === null) {
64
+ return null;
65
+ }
66
+ return visibility.meters >= 5000 && (ceiling === null || ceiling.feet >= 1500) ? "VFR" : "IFR";
67
+ }
68
+ /**
69
+ * @returns the lowest cloud layer reporting `BKN` or `OVC`, or undefined if
70
+ * not such cloud layer was found.
71
+ */
72
+ function findCeiling(clouds) {
73
+ return clouds.find((c) => {
74
+ return c.code === "BKN" || c.code === "OVC";
75
+ });
76
+ }
77
+ export function metarCeiling(clouds) {
78
+ const ceilingCloud = findCeiling(clouds);
79
+ if (!ceilingCloud) {
80
+ return null;
81
+ }
82
+ return {
83
+ feet: ceilingCloud.feet,
84
+ meters: ceilingCloud.meters,
85
+ };
86
+ }
87
+ /**
88
+ * @return METAR string split along its white spaces. Will convert fractional
89
+ * visibility values into one concatenated value.
90
+ */
91
+ export function metarSplit(metarString) {
92
+ return metarString
93
+ .trim()
94
+ .replace(/(\s)(\d)\s(\d)\/(\d)(SM)/, function (all, a, b, c, d, e) {
95
+ // convert visbility range like `1 1/2 SM`
96
+ return a + (Number(b) * Number(d) + Number(c)) + "/" + d + e;
97
+ })
98
+ .replace(/\n/, " \\n ")
99
+ .split(/\s+/);
100
+ }
@@ -1,78 +1,5 @@
1
- import { convert } from "./convert.js";
2
- class MetarParserHelpers {
3
- /**
4
- * @param {Number} value dito
5
- * @param {Number} toNext round to next full xxxx
6
- * @returns {Number} rounded value
7
- */
8
- static round(value, toNext = 500) {
9
- return Math.round(value / toNext) * toNext;
10
- }
11
- static getVisibility(visibility_miles, visibility_meters) {
12
- return {
13
- miles: visibility_miles,
14
- miles_text: String(MetarParserHelpers.round(visibility_miles, 0.5)),
15
- meters: visibility_meters,
16
- meters_text: String(MetarParserHelpers.round(visibility_meters, 500)),
17
- };
18
- }
19
- static getCloud(code, hundredFeet) {
20
- const feet = Number(hundredFeet) * 100;
21
- return {
22
- code: code,
23
- feet: feet,
24
- meters: convert.feetToMeters(feet),
25
- };
26
- }
27
- static getTemperature(temperatureString) {
28
- const temperature = Number(temperatureString.replace("M", "-"));
29
- return {
30
- celsius: temperature,
31
- fahrenheit: convert.celsiusToFahrenheit(temperature),
32
- };
33
- }
34
- /**
35
- * @see http://andrew.rsmas.miami.edu/bmcnoldy/Humidity.html
36
- */
37
- static getHumidity(temp, dew) {
38
- if (dew.celsius === null || temp.celsius === null) {
39
- return null;
40
- }
41
- return ((Math.exp((17.625 * dew.celsius) / (243.04 + dew.celsius)) /
42
- Math.exp((17.625 * temp.celsius) / (243.04 + temp.celsius))) *
43
- 100);
44
- }
45
- static getFlightCategory(visibility, ceiling) {
46
- if (visibility.miles > 5 && (!ceiling.feet || ceiling.feet > 3000)) {
47
- return "VFR";
48
- }
49
- else if (visibility.miles >= 3 && (!ceiling.feet || ceiling.feet >= 1000)) {
50
- return "MVFR";
51
- }
52
- else if (visibility.miles >= 1 && (!ceiling.feet || ceiling.feet >= 500)) {
53
- return "IFR";
54
- }
55
- return "LIFR";
56
- }
57
- static getIcaoFLightCategory(visibility, ceiling) {
58
- return visibility.meters >= 5000 && (!ceiling.feet || ceiling.feet >= 1500) ? "VFR" : "IFR";
59
- }
60
- static getCeiling(clouds) {
61
- return clouds.find((c) => {
62
- return c.code === "BKN" || c.code === "OVC";
63
- });
64
- }
65
- static metarSplit(metarString) {
66
- return metarString
67
- .trim()
68
- .replace(/(\s)(\d)\s(\d)\/(\d)(SM)/, function (all, a, b, c, d, e) {
69
- // convert visbility range like `1 1/2 SM`
70
- return a + (Number(b) * Number(d) + Number(c)) + "/" + d + e;
71
- })
72
- .replace(/\n/, " \\n ")
73
- .split(/\s+/);
74
- }
75
- }
1
+ import * as convert from "./convert.js";
2
+ import * as MetarParserHelpers from "./metar-parser-helpers.js";
76
3
  /**
77
4
  * Convert METAR string into structured object.
78
5
  * @see https://api.checkwx.com/#31-single
@@ -82,53 +9,7 @@ class MetarParserHelpers {
82
9
  * reponse of the data property of https://www.checkwxapi.com/metar
83
10
  */
84
11
  export const metarParser = (metarString) => {
85
- const metarObject = {
86
- raw_text: metarString,
87
- raw_parts: MetarParserHelpers.metarSplit(metarString),
88
- icao: "",
89
- observed: new Date(),
90
- forecastFrom: null,
91
- forecastTo: null,
92
- wind: {
93
- degrees: null,
94
- speed_kts: 0,
95
- speed_mps: 0,
96
- gust_kts: null,
97
- gust_mps: null,
98
- degrees_from: null,
99
- degrees_to: null,
100
- },
101
- visibility: {
102
- miles: 10,
103
- miles_text: "",
104
- meters: 9999,
105
- meters_text: "",
106
- },
107
- conditions: [],
108
- clouds: [],
109
- ceiling: {
110
- feet: null,
111
- meters: null,
112
- },
113
- temperature: {
114
- celsius: null,
115
- fahrenheit: null,
116
- },
117
- dewpoint: {
118
- celsius: null,
119
- fahrenheit: null,
120
- },
121
- humidity: {
122
- percent: null,
123
- },
124
- barometer: {
125
- hg: null,
126
- kpa: null,
127
- mb: null,
128
- },
129
- flight_category: null,
130
- icao_flight_category: null,
131
- };
12
+ const metarObject = initializeMetarObject(metarString);
132
13
  let mode = 0;
133
14
  let isTAF = false;
134
15
  metarObject.raw_parts.forEach((metarPart) => {
@@ -137,13 +18,13 @@ export const metarParser = (metarString) => {
137
18
  isTAF = metarPart === "TAF";
138
19
  return;
139
20
  }
140
- if (!isTAF && mode > 1 && mode < 3 && metarPart.match(/^(\d+)(?:\/(\d+))?(SM)?$/)) {
21
+ if (!isTAF && mode > 1 && mode < 3 && metarPart.match(/^(\d+|\/{2,4})(?:\/(\d+))?(SM)?$/)) {
141
22
  mode = 3; // no wind reported
142
23
  }
143
- if (mode > 2 && mode < 5 && metarPart.match(/^(FEW|SCT|BKN|OVC)(\d+)?/)) {
24
+ if (mode > 2 && mode < 5 && metarPart.match(/^(FEW|SCT|BKN|OVC|VV)(\d+)?/)) {
144
25
  mode = 5; // no visibility / conditions reported
145
26
  }
146
- if (mode > 3 && mode < 6 && metarPart.match(/(^M?\d+\/M?\d+$)|(^\/\/\/\/\/)/)) {
27
+ if (mode > 3 && mode < 6 && metarPart.match(/^(M?\d+|\/\/)\/(M?\d+|\/\/)?$/)) {
147
28
  mode = 6; // end of clouds
148
29
  }
149
30
  if (metarPart.match(/^(TEMPO|FM\d+|\\n)$/)) {
@@ -151,7 +32,6 @@ export const metarParser = (metarString) => {
151
32
  }
152
33
  switch (mode) {
153
34
  case 0:
154
- // ICAO Code
155
35
  metarObject.icao = metarPart;
156
36
  mode = 1;
157
37
  break;
@@ -167,66 +47,15 @@ export const metarParser = (metarString) => {
167
47
  }
168
48
  break;
169
49
  case 2:
170
- // Wind
171
- match = metarPart.match(/^(\d\d\d|VRB)P?(\d+)(?:G(\d+))?(KT|MPS|KPH)/);
172
- if (match) {
173
- let speed_kts = Number(match[2]);
174
- let gust_kts = match[3] ? Number(match[3]) : null;
175
- if (match[4] === "KPH") {
176
- speed_kts = convert.kphToMps(speed_kts);
177
- gust_kts = gust_kts ? convert.kphToMps(gust_kts) : null;
178
- match[4] = "MPS";
179
- }
180
- const degrees = match[1] === "VRB" ? null : Number(match[1]);
181
- metarObject.wind = {
182
- degrees: degrees,
183
- speed_kts: match[4] === "MPS" ? convert.mpsToKts(speed_kts) : speed_kts,
184
- speed_mps: match[4] === "MPS" ? speed_kts : convert.ktsToMps(speed_kts),
185
- gust_kts: gust_kts ? (match[4] === "MPS" ? convert.mpsToKts(gust_kts) : gust_kts) : null,
186
- gust_mps: gust_kts ? (match[4] === "MPS" ? gust_kts : convert.ktsToMps(gust_kts)) : null,
187
- degrees_from: null,
188
- degrees_to: null,
189
- };
190
- if (match[1] === "VRB") {
191
- metarObject.wind.degrees_from = 0;
192
- metarObject.wind.degrees_to = 359;
193
- }
194
- mode = 3;
195
- break;
196
- }
197
- // Forecast Date
198
- match = metarPart.match(/^(\d\d)(\d\d)\/(\d\d)(\d\d)$/);
199
- if (match) {
200
- metarObject.forecastFrom = new Date();
201
- metarObject.forecastFrom.setUTCDate(Number(match[1]));
202
- metarObject.forecastFrom.setUTCHours(Number(match[2]));
203
- metarObject.forecastFrom.setUTCMinutes(0);
204
- metarObject.forecastTo = new Date();
205
- metarObject.forecastTo.setUTCDate(Number(match[3]));
206
- metarObject.forecastTo.setUTCHours(Number(match[4]));
207
- metarObject.forecastTo.setUTCMinutes(0);
208
- break;
50
+ mode = parseWind(metarPart, metarObject, mode);
51
+ if (mode === 2) {
52
+ parseForecastDate(metarPart, metarObject);
209
53
  }
210
54
  break;
211
55
  case 3:
212
- // Visibility
213
- match = metarPart.match(/^P?(\d+)(?:\/(\d+))?(SM)?$/);
214
- if (match) {
215
- const visibility = match[2] ? Number(match[1]) / Number(match[2]) : Number(match[1]);
216
- metarObject.visibility = MetarParserHelpers.getVisibility(match[3] && match[3] === "SM" ? visibility : convert.metersToMiles(visibility), match[3] && match[3] === "SM" ? convert.milesToMeters(visibility) : visibility);
217
- mode = 4;
218
- }
219
- else if (metarPart === "CAVOK" || metarPart === "CLR") {
220
- metarObject.visibility = MetarParserHelpers.getVisibility(10, convert.milesToMeters(10));
221
- mode = 5; // no clouds & conditions reported
222
- }
223
- else if (metarObject.wind) {
224
- // Variable wind direction
225
- match = metarPart.match(/^(\d+)V(\d+)$/);
226
- if (match) {
227
- metarObject.wind.degrees_from = Number(match[1]);
228
- metarObject.wind.degrees_to = Number(match[2]);
229
- }
56
+ mode = parseVisibility(metarPart, metarObject, mode);
57
+ if (mode === 3) {
58
+ parseVariableWind(metarPart, metarObject);
230
59
  }
231
60
  break;
232
61
  case 4:
@@ -247,53 +76,161 @@ export const metarParser = (metarString) => {
247
76
  }
248
77
  break;
249
78
  case 5:
250
- // Clouds
251
79
  parseClouds(metarPart, metarObject);
252
80
  // may occur multiple times
253
81
  break;
254
82
  case 6:
255
- // Temperature
256
83
  mode = parseTemperature(metarPart, metarObject, mode);
257
84
  break;
258
85
  case 7:
259
- // Pressure
260
- match = metarPart.match(/^(Q|A)(\d+)/);
261
- if (match) {
262
- let hg = Number(match[2]);
263
- hg /= match[1] === "Q" ? 10 : 100;
264
- metarObject.barometer = {
265
- hg: match[1] === "Q" ? convert.kpaToInhg(hg) : hg,
266
- kpa: match[1] === "Q" ? hg : convert.inhgToKpa(hg),
267
- mb: match[1] === "Q" ? hg * 10 : convert.inhgToKpa(hg * 10),
268
- };
269
- mode = 8;
270
- }
86
+ mode = parsePressure(metarPart, metarObject, mode);
271
87
  break;
272
88
  }
273
89
  });
274
- const ceilingCloud = MetarParserHelpers.getCeiling(metarObject.clouds);
275
- metarObject.ceiling.feet = ceilingCloud?.feet ?? null;
276
- metarObject.ceiling.meters = ceilingCloud?.meters ?? null;
277
- metarObject.flight_category = MetarParserHelpers.getFlightCategory(metarObject.visibility, metarObject.ceiling);
278
- metarObject.icao_flight_category = MetarParserHelpers.getIcaoFLightCategory(metarObject.visibility, metarObject.ceiling);
90
+ metarObject.ceiling = MetarParserHelpers.metarCeiling(metarObject.clouds);
91
+ metarObject.flight_category = MetarParserHelpers.flightCategoryUs(metarObject.visibility, metarObject.ceiling);
92
+ metarObject.icao_flight_category = MetarParserHelpers.flightCategoryIcao(metarObject.visibility, metarObject.ceiling);
279
93
  return metarObject;
280
94
  };
95
+ // -----------------------------------------------------------------------------
96
+ function parseWind(metarPart, metarObject, mode) {
97
+ // Case: Unkonw wind
98
+ if (metarPart.match(/^\/+(KT|MPS|KPH)$/)) {
99
+ return 3;
100
+ }
101
+ const match = metarPart.match(/^(\d\d\d|VRB)P?(\d+)(?:G(\d+))?(KT|MPS|KPH)/);
102
+ if (!match) {
103
+ return mode;
104
+ }
105
+ let speed_kts = Number(match[2]);
106
+ let gust_kts = match[3] ? Number(match[3]) : null;
107
+ if (match[4] === "KPH") {
108
+ speed_kts = convert.kphToMps(speed_kts);
109
+ gust_kts = gust_kts ? convert.kphToMps(gust_kts) : null;
110
+ match[4] = "MPS";
111
+ }
112
+ const degrees = match[1] === "VRB" ? null : Number(match[1]);
113
+ metarObject.wind = {
114
+ degrees: degrees,
115
+ speed_kts: match[4] === "MPS" ? convert.mpsToKts(speed_kts) : speed_kts,
116
+ speed_mps: match[4] === "MPS" ? speed_kts : convert.ktsToMps(speed_kts),
117
+ gust_kts: gust_kts ? (match[4] === "MPS" ? convert.mpsToKts(gust_kts) : gust_kts) : null,
118
+ gust_mps: gust_kts ? (match[4] === "MPS" ? gust_kts : convert.ktsToMps(gust_kts)) : null,
119
+ degrees_from: null,
120
+ degrees_to: null,
121
+ };
122
+ if (match[1] === "VRB") {
123
+ metarObject.wind.degrees_from = 0;
124
+ metarObject.wind.degrees_to = 359;
125
+ }
126
+ return 3;
127
+ }
128
+ function parseForecastDate(metarPart, metarObject) {
129
+ const match = metarPart.match(/^(\d\d)(\d\d)\/(\d\d)(\d\d)$/);
130
+ if (!match) {
131
+ return;
132
+ }
133
+ metarObject.forecastFrom = new Date();
134
+ metarObject.forecastFrom.setUTCDate(Number(match[1]));
135
+ metarObject.forecastFrom.setUTCHours(Number(match[2]));
136
+ metarObject.forecastFrom.setUTCMinutes(0);
137
+ metarObject.forecastTo = new Date();
138
+ metarObject.forecastTo.setUTCDate(Number(match[3]));
139
+ metarObject.forecastTo.setUTCHours(Number(match[4]));
140
+ metarObject.forecastTo.setUTCMinutes(0);
141
+ }
142
+ function parseVisibility(metarPart, metarObject, mode) {
143
+ // Case: No clouds & conditions reported
144
+ if (metarPart === "CAVOK" || metarPart === "CLR") {
145
+ metarObject.visibility = MetarParserHelpers.metarVisibility(10, convert.milesToMeters(10));
146
+ return 5;
147
+ }
148
+ const [, digits, fraction, unit] = metarPart.match(/^[MP]?(\d+|\/{2,4})(?:\/(\d+))?(SM)?$/) || [];
149
+ if (digits === undefined) {
150
+ return mode;
151
+ }
152
+ // Case: Explicitly unknown visibility
153
+ if (digits.match(/^\/+/)) {
154
+ return 4;
155
+ }
156
+ // Case: Known visibility, possibly as fraction
157
+ const visibility = fraction ? Number(digits) / Number(fraction) : Number(digits);
158
+ metarObject.visibility = MetarParserHelpers.metarVisibility(unit === "SM" ? visibility : convert.metersToMiles(visibility), unit === "SM" ? convert.milesToMeters(visibility) : visibility);
159
+ return 4;
160
+ }
161
+ function parseVariableWind(metarPart, metarObject) {
162
+ if (!metarObject.wind) {
163
+ return;
164
+ }
165
+ const match = metarPart.match(/^(\d+)V(\d+)$/);
166
+ if (match) {
167
+ metarObject.wind.degrees_from = Number(match[1]);
168
+ metarObject.wind.degrees_to = Number(match[2]);
169
+ }
170
+ }
281
171
  function parseClouds(metarPart, metarObject) {
282
- const [, cloudType, cloudAltitude] = metarPart.match(/^(FEW|SCT|BKN|OVC)(\d+)/) || [];
172
+ const [, cloudType, cloudAltitude] = metarPart.match(/^(FEW|SCT|BKN|OVC|VV)(\d+)/) || [];
283
173
  if (cloudType && cloudAltitude) {
284
- metarObject.clouds.push(MetarParserHelpers.getCloud(cloudType, cloudAltitude));
174
+ metarObject.clouds.push(MetarParserHelpers.metarCloud(cloudType, cloudAltitude));
285
175
  }
286
176
  }
287
177
  function parseTemperature(metarPart, metarObject, mode) {
288
- const [, temp, dewpoint] = metarPart.match(/^(M?\d+)\/(M?\d+)$/) || [];
289
- if (temp === undefined && metarPart.match(/^\/\/\/\/\/$/)) {
290
- return 7;
291
- }
292
- if (temp && dewpoint) {
293
- metarObject.temperature = MetarParserHelpers.getTemperature(temp);
294
- metarObject.dewpoint = MetarParserHelpers.getTemperature(dewpoint);
295
- metarObject.humidity.percent = MetarParserHelpers.getHumidity(metarObject.temperature, metarObject.dewpoint);
178
+ const [, temp, dewpoint] = metarPart.match(/^(M?\d+|\/\/)\/(M?\d+|\/\/)?$/) || [];
179
+ if (temp) {
180
+ metarObject.temperature = MetarParserHelpers.metarTemperature(temp ?? "//");
181
+ metarObject.dewpoint = MetarParserHelpers.metarTemperature(dewpoint ?? "//");
182
+ metarObject.humidity.percent = MetarParserHelpers.relativeHumidityPercent(metarObject.temperature, metarObject.dewpoint);
296
183
  return 7;
297
184
  }
298
185
  return mode;
299
186
  }
187
+ function parsePressure(metarPart, metarObject, mode) {
188
+ const match = metarPart.match(/^(Q|A)(\d+)/);
189
+ if (!match) {
190
+ return mode;
191
+ }
192
+ let hg = Number(match[2]);
193
+ hg /= match[1] === "Q" ? 10 : 100;
194
+ metarObject.barometer = {
195
+ hg: match[1] === "Q" ? convert.kpaToInhg(hg) : hg,
196
+ kpa: match[1] === "Q" ? hg : convert.inhgToKpa(hg),
197
+ mb: match[1] === "Q" ? hg * 10 : convert.inhgToKpa(hg * 10),
198
+ };
199
+ return 8;
200
+ }
201
+ // -----------------------------------------------------------------------------
202
+ function initializeMetarObject(metarString) {
203
+ return {
204
+ raw_text: metarString,
205
+ raw_parts: MetarParserHelpers.metarSplit(metarString),
206
+ icao: "",
207
+ observed: new Date(),
208
+ forecastFrom: null,
209
+ forecastTo: null,
210
+ wind: {
211
+ degrees: null,
212
+ speed_kts: null,
213
+ speed_mps: null,
214
+ gust_kts: null,
215
+ gust_mps: null,
216
+ degrees_from: null,
217
+ degrees_to: null,
218
+ },
219
+ visibility: null,
220
+ conditions: [],
221
+ clouds: [],
222
+ ceiling: null,
223
+ temperature: null,
224
+ dewpoint: null,
225
+ humidity: {
226
+ percent: null,
227
+ },
228
+ barometer: {
229
+ hg: null,
230
+ kpa: null,
231
+ mb: null,
232
+ },
233
+ flight_category: null,
234
+ icao_flight_category: null,
235
+ };
236
+ }