forked from freezer333/nodecpp-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrainfall.js
63 lines (53 loc) · 2 KB
/
rainfall.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
var rainfall = require("./cpp/build/Release/rainfall");
var location = {
latitude : 40.71, longitude : -74.01,
samples : [
{ date : "2015-06-07", rainfall : 2.1 },
{ date : "2015-06-14", rainfall : 0.5},
{ date : "2015-06-21", rainfall : 1.5},
{ date : "2015-06-28", rainfall : 1.3},
{ date : "2015-07-05", rainfall : 0.9}
] };
// utility printing
var print_rain_results = function(results) {
var i = 0;
results.forEach(function(result){
console.log("Result for Location " + i);
console.log("--------------------------");
console.log("\tLatitude: " + locations[i].latitude.toFixed(2));
console.log("\tLongitude: " + locations[i].longitude.toFixed(2));
console.log("\tMean Rainfall: " + result.mean.toFixed(2) + "cm");
console.log("\tMedian Rainfall: " + result.median.toFixed(2) + "cm");
console.log("\tStandard Dev.: " + result.standard_deviation.toFixed(2) + "cm");
console.log("\tNumber Samples: " + result.n);
console.log();
i++;
});
}
// Part 1
console.log("Average rain fall = " + rainfall.avg_rainfall(location) + "cm");
// Part 2
console.log("Rainfall Data = " + JSON.stringify(rainfall.data_rainfall(location)));
// Part 3
var makeup = function(max) {
return Math.round(max * Math.random() * 100)/100;
}
var locations = []
for (var i = 0; i < 10; i++ ) {
var loc = {
latitude: makeup(180),
longitude: makeup(180),
samples : [
{date: "2015-07-20", rainfall: makeup(3)},
{date: "2015-07-21", rainfall: makeup(3)},
{date: "2015-07-22", rainfall: makeup(3)},
{date: "2015-07-23", rainfall: makeup(3)}
]
}
locations.push(loc);
}
var results = rainfall.calculate_results(locations);
print_rain_results(results);
// Part 4 - calling asynchronous c++ addon
rainfall.calculate_results_async(locations, print_rain_results);
console.log("Async results probably still not here yet...")