Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for visibility with a fraction (eg. 2 1/2SM) #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions metar.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,27 @@
}
};

METAR.prototype.parseVisibilityFraction = function() {
var re = /^([0-9]+)\/([0-9]+)([A-Z]{1,2})/g;

if (this.result.cavok) return;
if (this.current === "////") return;

if (this.peek().match(re)) {
this.next();
var matches;
while ((matches = re.exec(this.current)) != null) {
if (matches.index === re.lastIndex) {
re.lastIndex++;
}

var numerator = asInt(matches[1]);
var denominator = asInt(matches[2]);
this.result.visibility += (numerator / denominator);
}
}
};

METAR.prototype.parseRunwayVisibility = function() {
if (this.result.cavok) return;
if (this.peek().match(/^R[0-9]+/)) {
Expand Down Expand Up @@ -397,6 +418,7 @@
this.parseWind();
this.parseCavok();
this.parseVisibility();
this.parseVisibilityFraction();
this.parseRunwayVisibility();
this.parseWeather();
this.parseClouds();
Expand Down
7 changes: 7 additions & 0 deletions test/metar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ describe("METAR parser", function() {
assert.equal("NW", m.visibilityVariationDirection);
});

it("can parse visibility with a fraction (2 1/2SM)", function () {
var m = parseMetar(
"KPWT 141733Z AUTO 24006KT 2 1/2SM -RA BR BKN005 OVC015 08/07 A3020 RMK AO2 CIG 004V008 P0001"
);
assert.equal(2.5, m.visibility);
});

it("can parse clouds with a direction thing?", () => {
var m = parseMetar(
"EFJY 201120Z 30001KT 9999 1500NW -SN SCT002 BKN007 M17/M18 Q1031"
Expand Down