-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
54 lines (45 loc) · 1.65 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
const Model = require('./lib/model.js');
const geomagnetism = module.exports = {};
const modelData = [
{
file: require('./data/wmm-2025.json'),
startDate: new Date("2024-11-13T03:00:00.000Z"),
endDate: new Date("2029-11-13T03:00:00.000Z")
},
{
file: require('./data/wmm-2020.json'),
startDate: new Date("2019-12-10T08:00:00.000Z"),
endDate: new Date("2024-12-10T08:00:00.000Z")
},
{
file: require('./data/wmm-2015v2.json'),
startDate: new Date("2018-09-18T06:00:00.000Z"),
endDate: new Date("2023-09-18T06:00:00.000Z")
},
{
file: require('./data/wmm-2015.json'),
startDate: new Date("2014-12-15T07:00:00.000Z"),
endDate: new Date("2019-12-15T07:00:00.000Z")
}
];
geomagnetism.model = function (date, options = {}) {
date = date || new Date();
const ts = date.getTime();
const allowOutOfBoundsModel = (options && options.allowOutOfBoundsModel) || false;
// Get the latest matching model
let matchingModelData = modelData.find((model) => {
return ts >= model.startDate.getTime() && ts <= model.endDate.getTime();
});
// Get if the date is before the first model or after the last model
if (!matchingModelData && ts < modelData[modelData.length - 1].startDate.getTime()) {
matchingModelData = modelData[modelData.length - 1];
} else if (!matchingModelData && ts > modelData[0].endDate.getTime()) {
matchingModelData = modelData[0];
}
// If no matching model found, use the latest
if (!matchingModelData) {
matchingModelData = modelData[0]; // latest (will throw error if allowOutOfBoundsModel is true)
}
const matchingModel = new Model(matchingModelData.file);
return matchingModel.getTimedModel(date, allowOutOfBoundsModel);
};