Data retrieval finished. Final result file assembly completed.

master
ben-burlingham 10 years ago
parent 177ff46ca2
commit 796fe7746c
  1. 16
      server.js
  2. 162
      server/assemble.js
  3. 4
      server/io.js
  4. 4
      server/noaa.js
  5. 17482
      test.json

@ -7,6 +7,9 @@
var IO = require('./server/io'); var IO = require('./server/io');
var chalk = require('chalk'); var chalk = require('chalk');
/**
* Iteration based flow.
*/
function next(index) { function next(index) {
if (index >= stations.ids.length) { if (index >= stations.ids.length) {
console.log(chalk.green("Finished.")); console.log(chalk.green("Finished."));
@ -29,18 +32,17 @@
// .then(meteo.parseAllMonths.bind(null, station, 2015)) // .then(meteo.parseAllMonths.bind(null, station, 2015))
// .then(meteo.parseAllYears.bind(null, station, 1982, 2014)) // .then(meteo.parseAllYears.bind(null, station, 1982, 2014))
//===== Assemble
// .then(assemble.read.bind(null, station, 2015))
// .then(assemble.getAverages)
// .then(function() { console.log('something') })
//===== Flow control //===== Flow control
// .then(function() { console.log('=== Chain complete.\n'); })
// .then(next.bind(null, index + 1)) // .then(next.bind(null, index + 1))
.catch(IO.error); .catch(IO.error);
}; };
next(0); next(0);
/**
* Final assembly of file for client.
*/
assemble.buildFinalJson();
})(); })();

@ -1,9 +1,10 @@
var IO = require('./io'); var IO = require('./io');
var meteo = require('./meteo'); var meteo = require('./meteo');
var stations = require('./stations');
// [ // {
// { // station-46011: {
// id: str // id: str
// name: str // name: str
// lat: str // lat: str
@ -23,18 +24,10 @@ var meteo = require('./meteo');
// id: str // id: str
// ... // ...
// } // }
// ] // }
module.exports = { 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 sum = 0;
var count = 0; var count = 0;
console.log('Yearly average for column ' + col + '.');
arr.forEach(function(row) { arr.forEach(function(row) {
sum += parseInt(row[col]); sum += parseInt(row[col]);
count++; count++;
@ -79,12 +69,16 @@ module.exports = {
* *
*/ */
getMonthlyAverages: function(arr, col) { getMonthlyAverages: function(arr, col) {
console.log('Monthly averages for column ' + col + '.');
if (arr.length === undefined) {
return [];
}
var sum, count; var sum, count;
var months = []; var months = [];
var averages = []; var averages = [];
console.log('Monthly averages for column ' + col + '.');
for (var i = 0; i < 12; i++) { for (var i = 0; i < 12; i++) {
months[i] = []; months[i] = [];
} }
@ -114,13 +108,17 @@ module.exports = {
* *
*/ */
getDailyAverages: function(arr, col) { getDailyAverages: function(arr, col) {
console.log('Daily averages for column ' + col + '.');
if (arr.length === undefined) {
return [];
}
var sum, count, a, b, doy; var sum, count, a, b, doy;
var days = []; var days = [];
var averages = []; var averages = [];
var dayms = 1000 * 60 * 60 * 24; var dayms = 1000 * 60 * 60 * 24;
console.log('Daily averages for column ' + col + '.');
for (var i = 0; i <= 365; i++) { for (var i = 0; i <= 365; i++) {
days[i] = []; days[i] = [];
} }
@ -129,8 +127,14 @@ module.exports = {
arr.forEach(function(row) { arr.forEach(function(row) {
a = new Date(row[0], row[1] - 1, row[2]); a = new Date(row[0], row[1] - 1, row[2]);
b = new Date(row[0], 0, 1); b = new Date(row[0], 0, 1);
doy = Math.ceil((a - b) / dayms); 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]); 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; var columnToAverage = 14;
// module.exports.getDailyAverages(arr, columnToAverage); var result = [];
// module.exports.getMonthlyAverages(arr, columnToAverage);
module.exports.getYearlyAverage(arr, columnToAverage); 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; 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);
} }
}; };

@ -8,7 +8,7 @@ module.exports = {
/** /**
* *
*/ */
read: function(file, aaa, bbb) { read: function(file) {
return new Promise(function(resolve) { return new Promise(function(resolve) {
fs.readFile(file, 'utf8', function(err, str) { fs.readFile(file, 'utf8', function(err, str) {
console.log('Read ' + file); console.log('Read ' + file);
@ -38,6 +38,8 @@ module.exports = {
*/ */
error: function(e) { error: function(e) {
if (e !== null) { if (e !== null) {
// Gotcha: Promise.all() will not execute unless all promises are resolved.
// http://stackoverflow.com/questions/28250680/
console.log(chalk.yellow(e)); console.log(chalk.yellow(e));
} }
} }

@ -51,11 +51,13 @@ module.exports = {
// Prepare average to trim incidental head/tail data points from different years. // Prepare average to trim incidental head/tail data points from different years.
var sum = 0; var sum = 0;
var count = 0; var count = 0;
sorted.forEach(function(row) { sorted.forEach(function(row) {
if (row[0] === '#YY' || row[0] === '#yr' || row.length === 1) { if (row[0] === '#YY' || row[0] === 'YYYY' || row[0] === 'YY' || row[0] === '#yr' || row.length === 1) {
row[0] = null; row[0] = null;
} }
else { else {
row[0] = ('19' + row[0]).substr(-4);
sum += parseInt(row[0]); sum += parseInt(row[0]);
count++; count++;
} }

17482
test.json

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save