|
|
|
@ -1,9 +1,10 @@ |
|
|
|
|
|
|
|
|
|
var IO = require('./io'); |
|
|
|
|
var meteo = require('./meteo'); |
|
|
|
|
var stations = require('./stations'); |
|
|
|
|
|
|
|
|
|
// [
|
|
|
|
|
// {
|
|
|
|
|
// {
|
|
|
|
|
// station-46011: {
|
|
|
|
|
// id: str
|
|
|
|
|
// name: str
|
|
|
|
|
// lat: str
|
|
|
|
@ -23,18 +24,10 @@ var meteo = require('./meteo'); |
|
|
|
|
// id: str
|
|
|
|
|
// ...
|
|
|
|
|
// }
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
read: function(station, year) { |
|
|
|
|
return IO.read(meteo.dirs.json + station + '-' + year + '.json') |
|
|
|
|
.then(module.exports.parse); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
@ -53,19 +46,16 @@ module.exports = { |
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
getStation: function() { |
|
|
|
|
getYearlyAverage: function(arr, col) { |
|
|
|
|
console.log('Yearly average for column ' + col + '.'); |
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
if (arr.length === undefined) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
getYearlyAverage: function(arr, col) { |
|
|
|
|
var sum = 0; |
|
|
|
|
var count = 0; |
|
|
|
|
|
|
|
|
|
console.log('Yearly average for column ' + col + '.'); |
|
|
|
|
|
|
|
|
|
arr.forEach(function(row) { |
|
|
|
|
sum += parseInt(row[col]); |
|
|
|
|
count++; |
|
|
|
@ -79,12 +69,16 @@ module.exports = { |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
getMonthlyAverages: function(arr, col) { |
|
|
|
|
console.log('Monthly averages for column ' + col + '.'); |
|
|
|
|
|
|
|
|
|
if (arr.length === undefined) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var sum, count; |
|
|
|
|
var months = []; |
|
|
|
|
var averages = []; |
|
|
|
|
|
|
|
|
|
console.log('Monthly averages for column ' + col + '.'); |
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 12; i++) { |
|
|
|
|
months[i] = []; |
|
|
|
|
} |
|
|
|
@ -114,13 +108,17 @@ module.exports = { |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
getDailyAverages: function(arr, col) { |
|
|
|
|
console.log('Daily averages for column ' + col + '.'); |
|
|
|
|
|
|
|
|
|
if (arr.length === undefined) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var sum, count, a, b, doy; |
|
|
|
|
var days = []; |
|
|
|
|
var averages = []; |
|
|
|
|
var dayms = 1000 * 60 * 60 * 24; |
|
|
|
|
|
|
|
|
|
console.log('Daily averages for column ' + col + '.'); |
|
|
|
|
|
|
|
|
|
for (var i = 0; i <= 365; i++) { |
|
|
|
|
days[i] = []; |
|
|
|
|
} |
|
|
|
@ -129,8 +127,14 @@ module.exports = { |
|
|
|
|
arr.forEach(function(row) { |
|
|
|
|
a = new Date(row[0], row[1] - 1, row[2]); |
|
|
|
|
b = new Date(row[0], 0, 1); |
|
|
|
|
|
|
|
|
|
doy = Math.ceil((a - b) / dayms); |
|
|
|
|
|
|
|
|
|
// if (days[doy] === undefined) {
|
|
|
|
|
// // problem here - there are still YYMMDD rows in data
|
|
|
|
|
// console.log(row);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
days[doy].push(row[col]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -153,13 +157,117 @@ module.exports = { |
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
getAverages: function(arr) { |
|
|
|
|
getAllDailyAverages: function(station, startYear, endYear) { |
|
|
|
|
var columnToAverage = 14; |
|
|
|
|
|
|
|
|
|
var result = []; |
|
|
|
|
|
|
|
|
|
for (var year = startYear; year <= endYear; year++) { |
|
|
|
|
result.push( |
|
|
|
|
IO.read(meteo.dirs.json + station + '-' + year + '.json') |
|
|
|
|
.then(module.exports.parse) |
|
|
|
|
.then(function(arr) { |
|
|
|
|
return module.exports.getDailyAverages(arr, columnToAverage); |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Promise.all(result); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
getAllMonthlyAverages: function(station, startYear, endYear) { |
|
|
|
|
var columnToAverage = 14; |
|
|
|
|
|
|
|
|
|
// module.exports.getDailyAverages(arr, columnToAverage);
|
|
|
|
|
// module.exports.getMonthlyAverages(arr, columnToAverage);
|
|
|
|
|
module.exports.getYearlyAverage(arr, columnToAverage); |
|
|
|
|
var result = []; |
|
|
|
|
|
|
|
|
|
for (var year = startYear; year <= endYear; year++) { |
|
|
|
|
result.push( |
|
|
|
|
IO.read(meteo.dirs.json + station + '-' + year + '.json') |
|
|
|
|
.then(module.exports.parse) |
|
|
|
|
.then(function(arr) { |
|
|
|
|
return module.exports.getMonthlyAverages(arr, columnToAverage); |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Promise.all(result); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
getAllStations: function(arr) { |
|
|
|
|
console.log('asdf') |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
buildFinalJson: function() { |
|
|
|
|
var finalResult = {}; |
|
|
|
|
var startYear = 1982; |
|
|
|
|
var endYear = 2015; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function next(index) { |
|
|
|
|
if (index === stations.ids.length) { |
|
|
|
|
IO.write('test.json', JSON.stringify(finalResult, null, 4)); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var station = stations.ids[index]; |
|
|
|
|
|
|
|
|
|
// Init objects.
|
|
|
|
|
finalResult['s' + station] = {}; |
|
|
|
|
for (var year = startYear; year <= endYear; year++) { |
|
|
|
|
finalResult['s' + station]['avgs' + year] = {}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Promise.resolve() |
|
|
|
|
// Stations
|
|
|
|
|
.then(IO.read.bind(null, stations.dirs.json + station + '.json')) |
|
|
|
|
.then(module.exports.parse) |
|
|
|
|
.then(function(json) { |
|
|
|
|
finalResult['s' + station].id = json.id; |
|
|
|
|
finalResult['s' + station].name = json.name; |
|
|
|
|
finalResult['s' + station].lat = json.lat; |
|
|
|
|
finalResult['s' + station].lon = json.lon; |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Daily
|
|
|
|
|
// .then(module.exports.getAllDailyAverages.bind(null, station, startYear, endYear))
|
|
|
|
|
// .then(function(arr) {
|
|
|
|
|
// arr.forEach(function(averages, index) {
|
|
|
|
|
// finalResult['s' + station]['avgs' + (startYear + index)].d = averages;
|
|
|
|
|
// });
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Monthly
|
|
|
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, startYear, endYear)) |
|
|
|
|
.then(function(arr) { |
|
|
|
|
arr.forEach(function(averages, index) { |
|
|
|
|
finalResult['s' + station]['avgs' + (startYear + index)].m = averages; |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Yearly
|
|
|
|
|
.then(module.exports.getAllMonthlyAverages.bind(null, station, startYear, endYear)) |
|
|
|
|
.then(function(arr) { |
|
|
|
|
arr.forEach(function(averages, index) { |
|
|
|
|
finalResult['s' + station]['avgs' + (startYear + index)].y = averages; |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
.then(next.bind(null, index + 1)) |
|
|
|
|
.catch(IO.error); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
next(0); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|