This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.js
187 lines (159 loc) · 6.68 KB
/
updater.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var path = require('path');
var config = require('./config');
var sqlite3 = require("sqlite3").verbose();
var mongoose = require('mongoose');
var async = require('async');
// importing the events
// var ew = require('./events/event_worker.js');
// require('./events/events.js');
// preparing the mongoose connection
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
// preparing the sqlite DB and prepare the query
var db = new sqlite3.Database('data/weather_data.db');
var query = db.prepare("select case when longitude > 180 then longitude-360 else longitude end as longitude, latitude, speed, direction, time ,forecast from wind where time > datetime('now') and longitude in (?, ?) and latitude in (?, ?) order by time asc, forecast asc, longitude asc, latitude asc limit ?;");
mongoose.connect('mongodb://localhost/vb', function(err){
if (err) {
end();
throw err;
}
});
var Balloon = require('./models/balloon_model');
var BalloonHistory = require('./models/balloon_history_model');
var earth_radius = 6371*1000;
var ll = require("latlon");
function lerp(a, b, t){
return a + t * (b-a);
}
function toRad(alpha){
return alpha * Math.PI / 180;
}
function toDeg(x){
return x * 180 / Math.PI;
}
function rhumb(balloon, wind, cb){
var lat1 = toRad(balloon.location.latitude);
var lon1 = toRad(balloon.location.longitude);
var now = new Date();
var td = now - balloon.location.timestamp;
td = td / 1000;
// correct direction, wind direction is given as the direction from which the wind blows
// we need where the wind blows to
brng = toRad((wind.direction + 180) % 360);
d = wind.speed * td / earth_radius;
console.log("td: " + td + " seconds");
console.log("wind: " + wind.speed + " m/s " + wind.direction + " degree");
console.log("distance 1: " + wind.speed * td + " m");
var dLat = d * Math.cos(brng);
var lat2 = lat1 + dLat;
var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
var q = (isFinite(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1); // E-W line gives dPhi=0
var dLon = d*Math.sin(brng)/q;
// check for some daft bugger going past the pole, normalise latitude if so
if (Math.abs(lat2) > Math.PI/2) lat2 = lat2>0 ? Math.PI-lat2 : -Math.PI-lat2;
lon2 = (lon1+dLon+Math.PI)%(2*Math.PI) - Math.PI;
newPos = {'longitude': toDeg(lon2),
'latitude': toDeg(lat2),
timestamp: now,
windspeed: wind.speed,
winddirection: toDeg(brng)
};
var p1 = new ll(balloon.location.latitude, balloon.location.longitude, earth_radius);
var p2 = new ll(newPos.latitude, newPos.longitude, earth_radius);
console.log("distance 2: " + p1.rhumbDistanceTo(p2) + " m");
l = balloon.location;
balloon.history.push(l.toObject());
balloon.history = balloon.history.slice(balloon.history.length - config.history_size, balloon.history.length);
balloon.location = newPos;
// balloon.history.push(l.toObject());
balloon.reduceHistory();
balloon.save(function(err, b, n){
if (err) throw err;
bh = new BalloonHistory();
bh.balloonID = b._id;
bh.location = b.location.toObject();
bh.save(function(err, h, n){
if (err) throw err;
// ew.check(b, cb);
cb();
});
return console.log(balloon.name + ' saved.');
});
}
function interpolate(point, row){
console.log(point);
console.log(row);
values = row;
if (values.length==1){
console.log("1 result");
sp = values[0].speed;
dir = values[0].direction;
} else if (values.length==2){
console.log("2 results");
if (values[0].latitude == values[1].latitude){
sp = lerp(values[0].speed, values[1].speed, (point.longitude-values[0].longitude)/(values[1].longitude - values[0].longitude));
dir = lerp(values[0].direction, values[1].direction, (point.longitude-values[0].longitude)/(values[1].longitude - values[0].longitude));
} else {
sp = lerp(values[0].speed, values[1].speed, (point.latitude-values[0].latitude)/(values[1].latitude - values[0].latitude));
dir = lerp(values[0].direction, values[1].direction, (point.longitude-values[0].longitude)/(values[1].longitude - values[0].longitude));
}
} else if (values.length==4){
console.log("4 results");
sp1 = lerp(values[0].speed, values[1].speed, (point.latitude-values[0].latitude)/(values[1].latitude - values[0].latitude));
console.log(sp1);
sp2 = lerp(values[2].speed, values[3].speed, (point.latitude-values[2].latitude)/(values[3].latitude - values[2].latitude));
console.log(sp2);
dir1 = lerp(values[0].direction, values[1].direction, (point.latitude-values[0].latitude)/(values[1].latitude - values[0].latitude));
dir2 = lerp(values[2].direction, values[3].direction, (point.latitude-values[2].latitude)/(values[3].latitude - values[2].latitude));
sp = lerp(sp1, sp2, (point.longitude-values[0].longitude)/(values[2].longitude - values[0].longitude));
console.log('hans');
console.log(sp);
dir = lerp(dir1, dir2, (point.longitude-values[0].longitude)/(values[2].longitude - values[0].longitude));
}
// console.log({direction: dir, speed: sp});
return {direction: dir, speed: sp};
}
function test(b, row){
console.log(b.name);
console.log(row);
}
function getWind(balloon, cb){
console.log("processing " + balloon.name);
location = balloon.location;
lat1 = Math.floor(location.latitude/0.5)*0.5;
lat2 = Math.ceil(location.latitude/0.5)*0.5;
var longitude = (location.longitude < 0 ? location.longitude + 360 : location.longitude);
lng1 = Math.floor(longitude/0.5)*0.5;
lng2 = Math.ceil(longitude/0.5)*0.5;
limit = ((lat1==lat2)?0:1) + ((lng1==lng2)?0:1);
limit = Math.pow(2, limit);
lng1 = lng1%360;
lng2 = lng2%360;
console.log([lng1,lng2,lat1,lat2, limit]);
// console.log(balloon.history.length);
console.log([lng1,lng2,lat1,lat2, limit]);
query.all([lng1,lng2,lat1,lat2, limit], function(err, row){
if (err) throw err;
console.log(row);
console.log('hans');
if (row.length==0){
console.log('empty wind result');
cb();
return;
}
rhumb(balloon, interpolate(balloon.location, row), cb);
});
}
Balloon.find(function(err, balloons) {
async.each(balloons, getWind, end);
});
function end(err){
if (err) throw err;
query.finalize();
db.close();
//mongoose.connection.close();
mongoose.disconnect();
// ew.close();
console.log("end function");
}