-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
119 lines (101 loc) · 3.02 KB
/
utils.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
const MINUTES = {
ONE_HOUR: 60,
THREE_HOURS: 60*3,
FORTYEIGHT_HOURS: 60*48
}
const KELVIN = 273.15;
/**
*
* @param {number} minutes Minutes from current time
* @returns {Date} Date with given minute difference
*/
function minutesFromNow(minutes) {
let now = new Date();
now.setMinutes(now.getMinutes() + minutes);
return new Date(now);
}
function adjustPressureToSeaLevel(pressure, height, temperature = toKelvinFromCelcius(15)) {
temperature = temperature - KELVIN;
let seaLevelPressure = pressure * Math.pow(1 - ((0.0065 * height) / (temperature + 0.0065 * height + KELVIN)), -5.257);
return Math.round(seaLevelPressure);
}
/**
*
* @param {Array<Object>} pressures The pressures to filter
* @param {number} minutes Since X minutes
* @returns {Array<Object>} Subset of pressures
*/
function getPressuresSince(pressures, minutes) {
let earlier = minutesFromNow(-Math.abs(minutes));
let subPressures = pressures.filter((p) => {
return p.datetime.getTime() >= earlier.getTime();
});
return subPressures;
}
function isNullOrUndefined(value) {
return (value === null || value === undefined);
}
/**
*
* @param {Array<Object>} pressures Array of pressurs
* @param {Date} datetime
* @returns The pressure closest to the given datetime, null if not found
*/
function getPressureClosestTo(pressures, datetime) {
let previous = [...pressures].reverse().find((p) => p.datetime.getTime() <= datetime.getTime());
let next = pressures.find((p) => p.datetime.getTime() >= datetime.getTime());
if(isNullOrUndefined(next) && isNullOrUndefined(previous)) return null;
if(isNullOrUndefined(next)) return previous;
if(isNullOrUndefined(previous)) return next;
let diffNext = Math.abs(next.datetime.getTime() - datetime.getTime());
let diffPrevious = Math.abs(previous.datetime.getTime() - datetime.getTime());
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
return isNorthernHemisphere ? summer : !summer;
}
/**
*
* @param {number} celcius Celcius degrees
* @returns Kelvin degrees
*/
function toKelvinFromCelcius(celcius) {
return celcius + 273.15;
}
module.exports = {
minutesFromNow,
getPressuresSince,
isSummer,
adjustPressureToSeaLevel,
toKelvinFromCelcius,
getPressureClosestTo,
getPressuresByPeriod,
getPressureAverage,
MINUTES,
KELVIN
}