metar-plot
Version:
METAR parsing and ploting library
146 lines (145 loc) • 6.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.metarToImgSrc = exports.metarToSVG = exports.rawMetarToMetarPlot = exports.rawMetarToSVG = exports.MetarPlot = void 0;
var Metar_1 = require("./Metar");
var Cloud_1 = require("./parts/Cloud");
var Weather_1 = require("./parts/Weather");
var Wind_1 = require("./parts/Wind");
/**
* Extracted Metar message
*/
var MetarPlot = /** @class */ (function () {
function MetarPlot() {
}
return MetarPlot;
}());
exports.MetarPlot = MetarPlot;
/**
* Turns a raw METAR to an SVG image
* @param rawMetar RAW metar
* @param width css width of svg
* @param height css height of svg
* @param metric true for metric units(m, hPa, mps), false for north american units (miles, inHg, Kts)
* @returns
*/
function rawMetarToSVG(rawMetar, width, height, metric) {
var plot = rawMetarToMetarPlot(rawMetar, metric);
return metarToSVG(plot, width, height);
}
exports.rawMetarToSVG = rawMetarToSVG;
/**
*
* @param rawMetar raw metar string
* @param metric true for metric units(m, hPa, mps), false for north american units (miles, inHg, Kts)
* @returns
*/
function rawMetarToMetarPlot(rawMetar, metric) {
var _a, _b;
var metar = new Metar_1.METAR(rawMetar);
var wx = (_a = metar.weather[0]) === null || _a === void 0 ? void 0 : _a.abbreviation;
//Metric converion
var pressure;
var vis = undefined;
var temp = metar.temperature;
var dp = metar.dewpoint;
if (metric) {
pressure = (metar.altimeter != null) ? Math.round(metar.altimeter * 33.86) : undefined;
if (metar.visibility != null) {
vis = metar.visibility > 9999 ? 9999 : Math.round(metar.visibility);
}
}
else {
temp = cToF(temp);
dp = cToF(dp);
pressure = metar.altimeter;
vis = milePrettyPrint((_b = metar.visibility) !== null && _b !== void 0 ? _b : -1);
}
return {
metric: metric !== null && metric !== void 0 ? metric : false,
visablity: vis,
temp: temp,
dew_point: dp,
station: metar.station,
wind_direction: (typeof metar.wind.direction === "number") ? metar.wind.direction : undefined,
wind_speed: metar.wind.speed,
gust_speed: metar.wind.gust,
wx: wx,
pressure: pressure,
coverage: determinCoverage(metar)
};
}
exports.rawMetarToMetarPlot = rawMetarToMetarPlot;
/**
* Pretty print Miles in fractions if under 1 mile
*/
function milePrettyPrint(meters) {
var print = "";
if (meters === -1) {
return print;
}
var miles = meters * 0.0006213712;
//round to nearest quarter
var text = (Math.round(miles * 4) / 4).toFixed(2).toString();
return text.replace(".00", "");
}
/**
* Determines the coverage symbol
* @param metar
* @returns
*/
function determinCoverage(metar) {
var _a;
var prevailingCoverage;
metar.clouds.forEach(function (cloud) {
if (prevailingCoverage != null) {
var curr = prevailingCoverage.abbreviation != null ? Cloud_1.CLOUDS[prevailingCoverage.abbreviation].rank : undefined;
var rank = cloud.abbreviation != null ? Cloud_1.CLOUDS[cloud.abbreviation].rank : undefined;
console.log("cur: " + curr + ", rank: " + rank);
if (rank != null) {
if (rank > curr) {
prevailingCoverage = cloud;
}
}
}
else {
prevailingCoverage = cloud;
}
});
return (_a = prevailingCoverage === null || prevailingCoverage === void 0 ? void 0 : prevailingCoverage.abbreviation) !== null && _a !== void 0 ? _a : "";
}
/**
* Turns a Metar plot object to a SVG image
* @param metar MetarPlot Object
* @param width css width for svg
* @param height css height for svg
* @returns
*/
function metarToSVG(metar, width, height) {
var _a, _b, _c, _d, _e, _f;
var VIS = (_a = metar.visablity) !== null && _a !== void 0 ? _a : "";
var TMP = (_b = metar.temp) !== null && _b !== void 0 ? _b : "";
var DEW = (_c = metar.dew_point) !== null && _c !== void 0 ? _c : "";
var STA = (_d = metar.station) !== null && _d !== void 0 ? _d : "";
var ALT = (_e = metar.pressure) !== null && _e !== void 0 ? _e : "";
return "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + width + "\" height=\"" + height + "\" viewBox=\"0 0 500 500\">\n <style>\n .txt{ font-size: 47.5px; font-family: sans-serif; }\n .tmp{ fill: red }\n .sta{ fill: grey }\n .dew{ fill: blue }\n .vis{ fill: violet }\n </style>\n " + (0, Wind_1.genWind)(metar) + "\n " + (0, Weather_1.getWeatherSVG)((_f = metar.wx) !== null && _f !== void 0 ? _f : "") + "\n " + (0, Cloud_1.genCoverage)(metar.coverage, metar.condition) + "\n <g id=\"text\">\n <text class=\"vis txt\" fill=\"#000000\" stroke=\"#000\" stroke-width=\"0\" x=\"80\" y=\"260\" text-anchor=\"middle\" xml:space=\"preserve\">" + VIS + "</text>\n <text class=\"tmp txt\" fill=\"#000000\" stroke=\"#000\" stroke-width=\"0\" x=\"160\" y=\"220\" text-anchor=\"middle\" xml:space=\"preserve\" >" + TMP + "</text>\n <text class=\"dew txt\" fill=\"#000000\" stroke=\"#000\" stroke-width=\"0\" x=\"160\" y=\"315\" text-anchor=\"middle\" xml:space=\"preserve\">" + DEW + "</text>\n <text class=\"sta txt\" fill=\"#000000\" stroke=\"#000\" stroke-width=\"0\" x=\"275\" y=\"315\" text-anchor=\"start\" xml:space=\"preserve\">" + STA + "</text>\n <text class=\"sta txt\" fill=\"#000000\" stroke=\"#000\" stroke-width=\"0\" x=\"275\" y=\"220\" text-anchor=\"start\" xml:space=\"preserve\">" + ALT + "</text>\n </g>\n </svg>";
}
exports.metarToSVG = metarToSVG;
/**
* Turns a Metar plot object to a SVG image
* @param metar MetarPlot Object
* @returns A Base64 encoded string to be added directly as img src
*/
function metarToImgSrc(metar) {
var data = btoa(unescape(encodeURIComponent(metarToSVG(metar, "100px", "100px"))));
return "data:image/svg+xml;base64," + data;
}
exports.metarToImgSrc = metarToImgSrc;
/**
* Convert ºF to ºF
* @param celsius
*/
function cToF(celsius) {
if (celsius != null) {
return Math.round(celsius * 9 / 5 + 32);
}
}