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/src/convert.ts CHANGED
@@ -1,40 +1,35 @@
1
- /**
2
- * Convert units
3
- */
4
- export const convert = {
5
- celsiusToFahrenheit: function (celsius: number) {
6
- return celsius * 1.8 + 32;
7
- },
8
-
9
- feetToMeters: function (feet: number) {
10
- return feet * 0.3048;
11
- },
12
-
13
- milesToMeters: function (miles: number) {
14
- return miles * 1609.344;
15
- },
16
-
17
- metersToMiles: function (meters: number) {
18
- return meters / 1609.344;
19
- },
20
-
21
- inhgToKpa: function (inHg: number) {
22
- return inHg / 0.29529988;
23
- },
24
-
25
- kpaToInhg: function (kpa: number) {
26
- return kpa * 0.29529988;
27
- },
28
-
29
- kphToMps: function (kph: number) {
30
- return (kph / 3600) * 1000;
31
- },
32
-
33
- mpsToKts: function (mps: number) {
34
- return mps * 1.9438445;
35
- },
36
-
37
- ktsToMps: function (kts: number) {
38
- return kts / 1.9438445;
39
- },
40
- };
1
+ export function celsiusToFahrenheit(celsius: number) {
2
+ return celsius * 1.8 + 32;
3
+ }
4
+
5
+ export function feetToMeters(feet: number) {
6
+ return feet * 0.3048;
7
+ }
8
+
9
+ export function milesToMeters(miles: number) {
10
+ return miles * 1609.344;
11
+ }
12
+
13
+ export function metersToMiles(meters: number) {
14
+ return meters / 1609.344;
15
+ }
16
+
17
+ export function inhgToKpa(inHg: number) {
18
+ return inHg / 0.29529988;
19
+ }
20
+
21
+ export function kpaToInhg(kpa: number) {
22
+ return kpa * 0.29529988;
23
+ }
24
+
25
+ export function kphToMps(kph: number) {
26
+ return (kph / 3600) * 1000;
27
+ }
28
+
29
+ export function mpsToKts(mps: number) {
30
+ return mps * 1.9438445;
31
+ }
32
+
33
+ export function ktsToMps(kts: number) {
34
+ return kts / 1.9438445;
35
+ }
@@ -0,0 +1,155 @@
1
+ import * as convert from "./convert.js";
2
+ import type {
3
+ MetarVisibility,
4
+ MetarCloudCode,
5
+ MetarCloud,
6
+ MetarTemperature,
7
+ MetarCeiling,
8
+ MetarFlightCategory as MetarUsFlightCategory,
9
+ MetarIcaoFlightCategory,
10
+ MetarHumidity,
11
+ } from "./metar-parser.type.js";
12
+
13
+ /**
14
+ * @param {Number} value dito
15
+ * @param {Number} toNext round to next full xxxx
16
+ * @returns {Number} rounded value
17
+ */
18
+ export function round(value: number, toNext: number = 500): number {
19
+ return Math.round(value / toNext) * toNext;
20
+ }
21
+
22
+ export function metarVisibility(visibility_miles: number, visibility_meters: number): MetarVisibility {
23
+ return {
24
+ miles: visibility_miles,
25
+ miles_text: String(round(visibility_miles, 0.5)),
26
+ meters: visibility_meters,
27
+ meters_text: String(round(visibility_meters, 500)),
28
+ };
29
+ }
30
+
31
+ export function metarCloud(code: MetarCloudCode, hundredFeet: string): MetarCloud {
32
+ const feet = Number(hundredFeet) * 100;
33
+ return {
34
+ code: code,
35
+ feet: feet,
36
+ meters: convert.feetToMeters(feet),
37
+ };
38
+ }
39
+
40
+ export function metarTemperature(temperatureString: string): MetarTemperature | null {
41
+ if (temperatureString === "//") {
42
+ return null;
43
+ }
44
+ const temperature = Number(temperatureString.replace("M", "-"));
45
+ return {
46
+ celsius: temperature,
47
+ fahrenheit: convert.celsiusToFahrenheit(temperature),
48
+ };
49
+ }
50
+
51
+ /**
52
+ * @see http://andrew.rsmas.miami.edu/bmcnoldy/Humidity.html
53
+ */
54
+ export function relativeHumidity(temp: MetarTemperature | null, dew: MetarTemperature | null): MetarHumidity | null {
55
+ if (dew === null || temp === null) {
56
+ return null;
57
+ }
58
+
59
+ const percent =
60
+ (Math.exp((17.625 * dew.celsius) / (243.04 + dew.celsius)) /
61
+ Math.exp((17.625 * temp.celsius) / (243.04 + temp.celsius))) *
62
+ 100;
63
+
64
+ return { percent };
65
+ }
66
+
67
+ export function flightCategoryUs(
68
+ visibility: MetarVisibility | null,
69
+ ceiling: MetarCeiling | null,
70
+ ): MetarUsFlightCategory | null {
71
+ if (visibility === null) {
72
+ return null;
73
+ }
74
+
75
+ if (visibility.miles > 5 && (ceiling === null || ceiling.feet > 3000)) {
76
+ return "VFR";
77
+ } else if (visibility.miles >= 3 && (ceiling === null || ceiling.feet >= 1000)) {
78
+ return "MVFR";
79
+ } else if (visibility.miles >= 1 && (ceiling === null || ceiling.feet >= 500)) {
80
+ return "IFR";
81
+ }
82
+ return "LIFR";
83
+ }
84
+
85
+ export function flightCategoryIcao(
86
+ visibility: MetarVisibility | null,
87
+ ceiling: MetarCeiling | null,
88
+ ): MetarIcaoFlightCategory | null {
89
+ if (visibility === null) {
90
+ return null;
91
+ }
92
+
93
+ return visibility.meters >= 5000 && (ceiling === null || ceiling.feet >= 1500) ? "VFR" : "IFR";
94
+ }
95
+
96
+ /**
97
+ * @returns the lowest cloud layer reporting `BKN`, `OVC` or `VV`,
98
+ * or undefined if not such cloud layer was found.
99
+ */
100
+ function findCeiling(clouds: MetarCloud[]): MetarCloud | undefined {
101
+ return clouds.find((c) => {
102
+ return c.code === "BKN" || c.code === "OVC" || c.code === "VV";
103
+ });
104
+ }
105
+
106
+ /**
107
+ * @see findCeiling
108
+ * @returns the ceiling by looking for the lowest cloud layer reporting `BKN`
109
+ * or `OVC`.
110
+ */
111
+ export function metarCeiling(clouds: MetarCloud[]): MetarCeiling | null {
112
+ const ceilingCloud = findCeiling(clouds);
113
+
114
+ if (!ceilingCloud) {
115
+ return null;
116
+ }
117
+
118
+ return {
119
+ feet: ceilingCloud.feet,
120
+ meters: ceilingCloud.meters,
121
+ };
122
+ }
123
+
124
+ /**
125
+ * @return METAR string split along its white spaces. Will convert fractional
126
+ * visibility values into one concatenated value.
127
+ */
128
+ export function metarSplit(metarString: string): string[] {
129
+ return metarString
130
+ .trim()
131
+ .replace(/(\s)(\d)\s(\d)\/(\d)(SM)/, function (all, a, b, c, d, e) {
132
+ // convert visbility range like `1 1/2 SM`
133
+ return a + (Number(b) * Number(d) + Number(c)) + "/" + d + e;
134
+ })
135
+ .replace(/\n/, " \\n ")
136
+ .split(/\s+/);
137
+ }
138
+
139
+ export function date(date: number, hours: number, minutes: number): Date {
140
+ const now = new Date();
141
+
142
+ const d = new Date(0);
143
+ d.setUTCDate(date);
144
+ d.setUTCMonth(now.getUTCMonth());
145
+ d.setUTCFullYear(now.getUTCFullYear());
146
+
147
+ d.setUTCHours(hours);
148
+ d.setUTCMinutes(minutes);
149
+
150
+ if (d.getTime() > now.getTime()) {
151
+ d.setUTCMonth(d.getUTCMonth() - 1);
152
+ }
153
+
154
+ return d;
155
+ }