You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
3.4 KiB
165 lines
3.4 KiB
|
|
var IO = require('./io');
|
|
var meteo = require('./meteo');
|
|
|
|
// [
|
|
// {
|
|
// id: str
|
|
// name: str
|
|
// lat: str
|
|
// lon: str
|
|
|
|
// avg1982: {
|
|
// d: int[365] || null,
|
|
// w: int[52] || null,
|
|
// m: int[12] || null,
|
|
// y: int || null
|
|
// },
|
|
|
|
// avg1983: ...
|
|
// },
|
|
//
|
|
// {
|
|
// id: str
|
|
// ...
|
|
// }
|
|
// ]
|
|
|
|
|
|
module.exports = {
|
|
/**
|
|
*
|
|
*/
|
|
read: function(station, year) {
|
|
return IO.read(meteo.dirs.json + station + '-' + year + '.json')
|
|
.then(module.exports.parse);
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
parse: function(str) {
|
|
var json = {};
|
|
|
|
try {
|
|
json = JSON.parse(str);
|
|
} catch(e) {
|
|
IO.error(e);
|
|
}
|
|
|
|
return json;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getStation: function() {
|
|
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
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++;
|
|
});
|
|
|
|
var avg = Math.round((sum / count) * 10) / 10 || 0;
|
|
return avg;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getMonthlyAverages: function(arr, col) {
|
|
var sum, count;
|
|
var months = [];
|
|
var averages = [];
|
|
|
|
console.log('Monthly averages for column ' + col + '.');
|
|
|
|
for (var i = 0; i < 12; i++) {
|
|
months[i] = [];
|
|
}
|
|
|
|
// Assemble all the values for each month.
|
|
arr.forEach(function(row) {
|
|
months[row[1] - 1].push(row[col]);
|
|
});
|
|
|
|
// Get the average for each collection of values in each day of the year.
|
|
months.forEach(function(values, index) {
|
|
sum = 0;
|
|
count = 0;
|
|
|
|
values.map(function(val) {
|
|
sum += parseInt(val);
|
|
count++;
|
|
});
|
|
|
|
averages[index] = Math.round((sum / count) * 10) / 10 || 0;
|
|
});
|
|
|
|
return averages;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getDailyAverages: function(arr, col) {
|
|
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] = [];
|
|
}
|
|
|
|
// Assemble all the values for each day of the year.
|
|
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);
|
|
|
|
days[doy].push(row[col]);
|
|
});
|
|
|
|
// Get the average for each collection of values in each day of the year.
|
|
days.forEach(function(values, index) {
|
|
sum = 0;
|
|
count = 0;
|
|
|
|
values.map(function(val) {
|
|
sum += parseInt(val);
|
|
count++;
|
|
});
|
|
|
|
averages[index] = Math.round((sum / count) * 10) / 10 || 0;
|
|
});
|
|
|
|
return averages;
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
getAverages: function(arr) {
|
|
var columnToAverage = 14;
|
|
|
|
// module.exports.getDailyAverages(arr, columnToAverage);
|
|
// module.exports.getMonthlyAverages(arr, columnToAverage);
|
|
module.exports.getYearlyAverage(arr, columnToAverage);
|
|
|
|
return null;
|
|
}
|
|
};
|
|
|