diff --git a/README.md b/README.md index b95cfe9..3b42299 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,21 @@ ![Node.js CI](https://github.com/oyve/barometer-trend/workflows/Node.js%20CI/badge.svg) # barometer-trend -Calculate the tendency and trend of a barometer for a one to three hour period with barometric weather predictions. +Calculate the tendency and trend of a barometer for a *one* to *three* hour period including barometric weather predictions. ## Features -- Tendency and trend of the barometer for the *last hour* or *three hours* (`FALLNG|SLOWLY`)- +- Tendency and trend of the barometer for the *last hour* or *three hours* (`FALLING|SLOWLY`) - Prediction of weather and systems: - - By pressure tendency only (`Expect gale force weather'`) - - By pressure tendency, thresholds and wind direction (`Increasing rain, clearing within 12 hours.`) + - By pressure trend only (`Expect gale force weather'`) + - By pressure trend, thresholds and wind direction (`Increasing rain, clearing within 12 hours.`) - By seasonal pressure thresholds for winter and summer (`Cloudy and humid, thunderstorms`) - Front system tendency for the last three hours (`Falling before a lesser rise` | `Cold front passage` | `Strong and gusty, then veers`) - Force wind expectation in Beaufort scale based on the pressure tendency (`F8-9`) - Detects current pressure system `Low`, `Normal`, `High` Note -- All calculations corrected to sea level pressure by optional `altitude` and `temperature` -- 48 hour history +- Picks the period with the highest severity (one hour or three hours) +- All calculations corrected (internally) to sea level pressure by optional `altitude` and `temperature` +- Up to 48 hour history ## Install & Use ``` @@ -28,7 +29,7 @@ barometer.addPressure(datetime1, 101500); barometer.addPressure(datetime2, 101505); barometer.addPressure(datetime3, 101512, 100, 20, 225); //100 = altitude, 20 = C degrees, 225 = wind direction -//barometer.addPressure(...) is more presice when pressure is corrected by altitude and temperature. +//barometer.addPressure(...) is more precise when pressure is corrected by altitude and temperature. let forecast = barometer.getPredictions(); //returns JSON ``` diff --git a/package.json b/package.json index 2bf4968..2e7d7f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "barometer-trend", - "version": "2.2.0", + "version": "2.2.2", "description": "Calculate the tendency, trend and weather predictions of barometric pressure", "main": "index.js", "directories": { diff --git a/test/utils.test.js b/test/utils.test.js index 6a31647..7a0d620 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -6,7 +6,8 @@ describe("Utils Tests", function () { it("it should equal", function () { //arrange - const expected = new Date().getHours() - 3; + const now = new Date(); + const expected = now.getHours() < 3 ? now.getHours() + 24 - 3 : now.getHours() - 3; //avoid midnight 00 to 02 problem //act var actual = utils.minutesFromNow(-180).getHours(); //assert @@ -102,4 +103,23 @@ describe("Utils Tests", function () { assert.strictEqual(actual.value, expected); }); }); + + describe("getPressureAverageByTime Test", function () { + + it("it should average", function () { + //arrange + const expected = 101950; + const pressures = [ + { datetime: utils.minutesFromNow(-59), value: 101800 }, + { datetime: utils.minutesFromNow(-30), value: 101900 }, + { datetime: utils.minutesFromNow(-10), value: 102000 }, + { datetime: utils.minutesFromNow(-1), value: 102100 } + ]; + let subset = utils.getPressuresByPeriod(pressures, utils.minutesFromNow(-60), new Date()); + //act + var actual = utils.getPressureAverage(subset); + //assert + assert.strictEqual(actual, expected); + }); + }); }); \ No newline at end of file diff --git a/trend.js b/trend.js index a8ac69d..e75323b 100644 --- a/trend.js +++ b/trend.js @@ -1,9 +1,5 @@ const utils = require('./utils'); -const TWENTY_MINUTES = 20; -const ONE_HOUR = 60; -const THREE_HOURS = 180; - const TENDENCY = { RISING: { key: 'RISING' }, FALLING: { key: 'FALLING' } @@ -52,8 +48,8 @@ function calculate(pressures, from) { return { tendency: tendency.key, trend: threshold.trend.key, - from: earlier.value, - to: later.value, + from: earlier, + to: later, difference: difference, ratio: Math.abs(ratio), period: Math.abs(from), diff --git a/utils.js b/utils.js index 2a83ec9..132c8bd 100644 --- a/utils.js +++ b/utils.js @@ -12,7 +12,7 @@ const KELVIN = 273.15; * @returns {Date} Date with given minute difference */ function minutesFromNow(minutes) { - var now = new Date(); + let now = new Date(); now.setMinutes(now.getMinutes() + minutes); return new Date(now); } @@ -63,6 +63,32 @@ function getPressureClosestTo(pressures, datetime) { return (diffNext < diffPrevious) ? next : previous; } +/** + * + * @param {Array} pressures Array of pressures + * @param {Date} startTime Period start + * @param {Date} endTime Period end + * @returns Subset of pressures of the given period + */ +function getPressuresByPeriod(pressures, startTime, endTime) +{ + return pressures.filter((p) => p.datetime.getTime() >= startTime.getTime() && p.datetime.getTime() <= endTime.getTime()); +} + +/** + * + * @param {Array} pressures Array of pressures + * @returns Average pressure value + */ +function getPressureAverage(pressures) { + let sum = 0; + pressures.forEach((p) => { + sum += p.value; + }); + + return sum / pressures.length; +} + function isSummer(isNorthernHemisphere = true) { let month = new Date().getMonth() + 1; let summer = month >= 4 && month <= 9; //April to September @@ -86,6 +112,8 @@ module.exports = { adjustPressureToSeaLevel, toKelvinFromCelcius, getPressureClosestTo, + getPressuresByPeriod, + getPressureAverage, MINUTES, KELVIN } \ No newline at end of file