Skip to content

Commit

Permalink
Fix flag at beginning of METAR message not being handled (#50) (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Dec 7, 2022
1 parent 509bc85 commit c72813e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import { CommandSupplier as TafCommandSupplier } from "command/taf";
import { Locale } from "commons/i18n";
import { CommandExecutionError } from "commons/errors";

function isStation(stationString: string): boolean {
return stationString.length === 4;
}

/**
* Parses the delivery time of a METAR/TAF
* @param abstractWeatherCode The TAF or METAR object
Expand Down Expand Up @@ -336,9 +340,18 @@ export class MetarParser extends AbstractParser {
parse(input: string): IMetar {
const metarTab = this.tokenize(input);

let index = 0;

// Only parse flag if precedes station identifier
if (isStation(metarTab[index + 1])) {
var flags = findFlags(metarTab[index]);
if (flags) index += 1;
}

const metar: IMetar = {
...parseDeliveryTime(metarTab[1]),
station: metarTab[0],
...parseDeliveryTime(metarTab[index + 1]),
station: metarTab[index],
...flags,
message: input,
remarks: [],
clouds: [],
Expand All @@ -347,7 +360,7 @@ export class MetarParser extends AbstractParser {
runwaysInfo: [],
};

let index = 2;
index += 2;

while (index < metarTab.length) {
if (
Expand Down
24 changes: 24 additions & 0 deletions tests/parser/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,30 @@ describe("MetarParser", () => {
expect(metar.runwaysInfo[0].trend).toBe("N");
});

test("parses 'AUTO' as station if no station identifier", () => {
const input = "AUTO 061950Z 10002KT 9999NDV NCD 01/M00 Q1015 RMK=";

const metar = new MetarParser(en).parse(input);

expect(metar.auto).toBeUndefined();
expect(metar.station).toBe("AUTO");
expect(metar.day).toBe(6);
expect(metar.hour).toBe(19);
expect(metar.minute).toBe(50);
});

test("parses station with flag preceding rest of message", () => {
const input = "AUTO LSZL 061950Z 10002KT 9999NDV NCD 01/M00 Q1015 RMK=";

const metar = new MetarParser(en).parse(input);

expect(metar.auto).toBe(true);
expect(metar.station).toBe("LSZL");
expect(metar.day).toBe(6);
expect(metar.hour).toBe(19);
expect(metar.minute).toBe(50);
});

test("tempo", () => {
const input =
"LFBG 081130Z AUTO 23012KT 9999 SCT022 BKN072 BKN090 22/16 Q1011 TEMPO 26015G25KT 3000 TSRA SCT025CB BKN050";
Expand Down

0 comments on commit c72813e

Please sign in to comment.