Skip to content

Commit

Permalink
Merge pull request #25 from oyve/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
oyve authored Jun 26, 2021
2 parents 7940659 + b3a3eea commit c281165
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
Expand All @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
22 changes: 21 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
});
});
8 changes: 2 additions & 6 deletions trend.js
Original file line number Diff line number Diff line change
@@ -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' }
Expand Down Expand Up @@ -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),
Expand Down
30 changes: 29 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -63,6 +63,32 @@ function getPressureClosestTo(pressures, datetime) {
return (diffNext < diffPrevious) ? next : previous;
}

/**
*
* @param {Array<Object>} 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<Object>} 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
Expand All @@ -86,6 +112,8 @@ module.exports = {
adjustPressureToSeaLevel,
toKelvinFromCelcius,
getPressureClosestTo,
getPressuresByPeriod,
getPressureAverage,
MINUTES,
KELVIN
}

0 comments on commit c281165

Please sign in to comment.