forked from jleppert/egm96
-
Notifications
You must be signed in to change notification settings - Fork 0
/
egm96.js
66 lines (48 loc) · 1.61 KB
/
egm96.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
var fs = require('fs');
var data = fs.readFileSync('./WW15MGH.DAC');
function toDegree(radians) {
return radians * (180 / Math.PI);
}
function fromDegree(degrees) {
return degrees * (Math.PI / 180);
}
function getPostOffset(row, col) {
var k = row * NUM_COLS + col;
if(k >= data.length * 2) {
throw new RangeError('Offset exceeds height measurements');
}
return data.readInt16BE(k * 2);
}
var INTERVAL = fromDegree(15/60),
INTERVAL_DEGREE = toDegree(INTERVAL),
NUM_ROWS = 721,
NUM_COLS = 1440;
// http://cddis.gsfc.nasa.gov/926/egm96/egm96.html
function getOffset(latitude, longitude) {
var longitude = longitude >= 0 ? longitude : longitude + 360;
var topRow = Math.round((90 - latitude) / INTERVAL_DEGREE);
if(latitude <= -90) topRow = NUM_ROWS - 2;
var bottomRow = topRow + 1;
var leftCol = Math.round(longitude / INTERVAL_DEGREE);
var rightCol = leftCol + 1;
if(longitude >= 360 - INTERVAL_DEGREE) {
leftCol = NUM_COLS - 1;
rightCol = 0;
}
var latTop = 90 - topRow * INTERVAL_DEGREE;
var lonLeft = leftCol * INTERVAL_DEGREE;
var ul = getPostOffset(topRow, leftCol);
var ll = getPostOffset(bottomRow, leftCol);
var lr = getPostOffset(bottomRow, rightCol);
var ur = getPostOffset(topRow, rightCol);
var u = (longitude - lonLeft) / INTERVAL_DEGREE;
var v = (latTop - latitude) / INTERVAL_DEGREE;
var pll = (1.0 - u) * (1.0 - v);
var plr = (1.0 - u) * v;
var pur = u * v;
var pul = u * (1.0 - v);
var offset = pll * ll + plr * lr + pur * ur + pul * ul;
return offset / 100;
};
console.log(getOffset(38.6281550, 269.7791550));
module.exports = getOffset;