-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
125 lines (107 loc) · 3.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict'
const front = require('./predictions/front');
const byPressureTrend = require('./predictions/byPressureTrend')
const byPressureTendencyAndWind = require('./predictions/byPressureTendencyAndWind')
const byPressureTrendAndSeason = require('./predictions/byPressureTrendAndSeason')
const beaufort = require('./predictions/beaufort')
const trend = require('./trend');
const utils = require('./utils');
const history = require('./predictions/history');
const system = require('./predictions/system');
let pressures = [];
/**
* Clear the pressure readings. (Mainly for testing purposes)
*/
function clear() {
pressures = [];
}
function hasPressures() {
return pressures.length > 0;
}
/**
*
* @param {Date} datetime Timestamp of barometer reading
* @param {number} pressure Pressure in Pascal
* @param {number} altitude Altitude above sea level in meters, default = 0
* @param {number} temperature Temperature in Kelvin, defaults to 15 Celcius degrees
* @param {number} trueWindDirection True wind direction in degrees
*/
function addPressure(datetime, pressure, altitude = null, temperature = null, trueWindDirection = null) {
if (altitude === null) altitude = 0;
if (temperature === null) temperature = utils.toKelvinFromCelcius(15);
if (trueWindDirection !== null && trueWindDirection === 360) trueWindDirection = 0;
let pressureASL = utils.adjustPressureToSeaLevel(pressure, altitude, temperature);
pressures.push({
datetime: datetime,
value: pressureASL,
meta: {
value: pressure,
altitude: altitude,
temperature: temperature,
twd: trueWindDirection
}
});
removeOldPressures();
}
/**
* Get the count of pressure entries. (Mainly for testing purposes)
* @returns {number} Number of pressure entries
*/
function getPressureCount() {
return pressures.length;
}
function removeOldPressures(threshold = null) {
if (threshold === null) {
threshold = utils.minutesFromNow(-utils.MINUTES.FORTYEIGHT_HOURS);
}
pressures = pressures.filter((p) => p.datetime.getTime() >= threshold.getTime());
}
function getLastPressure() {
return pressures[pressures.length - 1];
}
/**
*
* @returns All pressure objects in memory
*/
function getAll() {
return pressures;
}
/**
* Get the trend of the barometer
* @param {boolean} isNorthernHemisphere Located north of equator? Default true.
* @returns {Array.<Object>}
*/
function getPredictions(isNorthernHemisphere = true) {
if (pressures.length < 2) return null;
let lastPressure = getLastPressure();
let pressureTrend = trend.getTrend(pressures);
let pressureSystem = system.getSystemByPressure(lastPressure.value);
let pressureHistory = history.getHistoricPressures(pressures);
let predictionPressureOnly = byPressureTrend.getPrediction(pressureTrend.tendency, pressureTrend.trend);
let predictionFront = front.getFront(pressures);
let predictionBeaufort = beaufort.getByPressureVariationRatio(pressureTrend.ratio);
let predictionSeason = byPressureTrendAndSeason.getPrediction(lastPressure.value, pressureTrend.tendency, pressureTrend.trend, utils.isSummer(isNorthernHemisphere))
let predictionPressureTendencyThresholdAndQuadrant = byPressureTendencyAndWind.getPrediction(lastPressure.value, lastPressure.meta.twd, pressureTrend.tendency, pressureTrend.trend, isNorthernHemisphere);
let forecast = {
lastPressure: lastPressure,
history: pressureHistory,
trend: pressureTrend,
system: pressureSystem,
predictions: {
pressureOnly: predictionPressureOnly,
quadrant: predictionPressureTendencyThresholdAndQuadrant,
season: predictionSeason,
beaufort: predictionBeaufort,
front: predictionFront
}
}
return forecast;
}
module.exports = {
clear,
addPressure,
getPressureCount,
getPredictions,
hasPressures,
getAll
}