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 altimeter units and leave unit conversion up to the user #65

Merged
merged 1 commit into from
Feb 3, 2023
Merged
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
6 changes: 5 additions & 1 deletion src/command/metar/AltimeterCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UnexpectedParseError } from "commons/errors";
import { IMetar } from "model/model";
import { AltimeterUnit } from "model/enum";
import { ICommand } from "../metar";

export class AltimeterCommand implements ICommand {
Expand All @@ -14,6 +15,9 @@ export class AltimeterCommand implements ICommand {

if (!matches) throw new UnexpectedParseError("Match not found");

metar.altimeter = Math.trunc(+matches[1]);
metar.altimeter = {
value: +matches[1],
unit: AltimeterUnit.HPa,
};
}
}
8 changes: 5 additions & 3 deletions src/command/metar/AltimeterMercuryCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as converter from "commons/converter";
import { UnexpectedParseError } from "commons/errors";
import { IMetar } from "model/model";
import { AltimeterUnit } from "model/enum";
import { ICommand } from "../metar";

export class AltimeterMercuryCommand implements ICommand {
Expand All @@ -17,8 +18,9 @@ export class AltimeterMercuryCommand implements ICommand {

const mercury = +matches[1] / 100;

metar.altimeter = Math.trunc(
converter.convertInchesMercuryToPascal(mercury)
);
metar.altimeter = {
value: mercury,
unit: AltimeterUnit.InHg,
};
}
}
4 changes: 0 additions & 4 deletions src/commons/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ export function convertTemperature(input: string): number {
return +input;
}

export function convertInchesMercuryToPascal(input: number): number {
return 33.8639 * input;
}

/**
* Converts number `.toFixed(1)` before outputting to match python implementation
*/
Expand Down
16 changes: 16 additions & 0 deletions src/model/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,19 @@ export enum DepositCoverage {
From26To50 = "5",
From51To100 = "9",
}

export enum AltimeterUnit {
/**
* Inches of mercury (inHg)
*
* e.g. A2994 parses as 29.94 inHg
*/
InHg = "inHg",

/**
* Hectopascals (hPa), also known as millibars
*
* e.g. Q1018 parses as 1018 millibars
*/
HPa = "hPa",
}
8 changes: 7 additions & 1 deletion src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
MetarType,
DepositType,
DepositCoverage,
AltimeterUnit,
} from "model/enum";
import { Remark } from "command/remark";

Expand Down Expand Up @@ -73,6 +74,11 @@ export function isWeatherConditionValid(weather: IWeatherCondition): boolean {
);
}

export interface IAltimeter {
value: number;
unit: AltimeterUnit;
}

export interface ITemperature {
temperature: number;
day: number;
Expand Down Expand Up @@ -239,7 +245,7 @@ export interface IMetar extends IAbstractWeatherCode {
type?: MetarType;
temperature?: number;
dewPoint?: number;
altimeter?: number;
altimeter?: IAltimeter;
nosig?: true;
runwaysInfo: RunwayInfo[];

Expand Down
4 changes: 0 additions & 4 deletions tests/commons/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ describe("convertTemperature", () => {
});
});

test("convertInchesMercuryToPascal", () => {
expect(converter.convertInchesMercuryToPascal(29.92)).toBeCloseTo(1013.2, 1);
});

describe("convertTemperatureRemarks", () => {
test("positive", () => {
expect(converter.convertTemperatureRemarks("0", "142")).toBe(14.2);
Expand Down
11 changes: 9 additions & 2 deletions tests/parser/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
TurbulenceIntensity,
IcingIntensity,
MetarType,
AltimeterUnit,
} from "model/enum";
import { IAbstractWeatherContainer, IRunwayInfoRange } from "model/model";
import { Direction } from "model/enum";
Expand Down Expand Up @@ -436,7 +437,10 @@ describe("MetarParser", () => {
});
expect(metar.temperature).toBe(9);
expect(metar.dewPoint).toBe(6);
expect(metar.altimeter).toBe(1031);
expect(metar.altimeter).toStrictEqual({
value: 1031,
unit: AltimeterUnit.HPa,
});
expect(metar.nosig).toBe(true);
});

Expand All @@ -445,7 +449,10 @@ describe("MetarParser", () => {
"KTTN 051853Z 04011KT 9999 VCTS SN FZFG BKN003 OVC010 M02/M02 A3006"
);

expect(metar.altimeter).toBe(1017);
expect(metar.altimeter).toStrictEqual({
value: 30.06,
unit: AltimeterUnit.InHg,
});
expect(metar.weatherConditions).toHaveLength(3);
});

Expand Down