From 177ff46ca209860b7b310ce7ed1646744dbcd5da Mon Sep 17 00:00:00 2001 From: ben-burlingham Date: Sun, 17 Jan 2016 19:08:52 -0800 Subject: [PATCH] Removing non-matching years from data files. --- server.js | 12 +++++------- server/assemble.js | 1 - server/meteo.js | 6 +++--- server/noaa.js | 25 ++++++++++++++++++++++--- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/server.js b/server.js index 5d83f4b..edc41ff 100644 --- a/server.js +++ b/server.js @@ -15,7 +15,7 @@ console.log('=== Starting next promise chain.'); - var station = 46026 //stations.ids[index]; + var station = stations.ids[index]; Promise.resolve() //===== Download @@ -26,23 +26,21 @@ //===== Parse // .then(stations.parseStation.bind(null, station)) - // .then(meteo.parseAllMonths.bind(null, 46026, 2015)) + // .then(meteo.parseAllMonths.bind(null, station, 2015)) // .then(meteo.parseAllYears.bind(null, station, 1982, 2014)) //===== Assemble - .then(assemble.read.bind(null, station, 2015)) - .then(assemble.getAverages) + // .then(assemble.read.bind(null, station, 2015)) + // .then(assemble.getAverages) // .then(function() { console.log('something') }) //===== Flow control - .then(function() { console.log('=== Chain complete.\n'); }) + // .then(function() { console.log('=== Chain complete.\n'); }) // .then(next.bind(null, index + 1)) .catch(IO.error); }; - // TODO remove previous years from file, such as 46026-2015 shouldn't have data from 2014. - next(0); })(); diff --git a/server/assemble.js b/server/assemble.js index bd4f479..cf59779 100644 --- a/server/assemble.js +++ b/server/assemble.js @@ -1,4 +1,3 @@ -'use strict'; var IO = require('./io'); var meteo = require('./meteo'); diff --git a/server/meteo.js b/server/meteo.js index 6e90c54..c00b0a7 100644 --- a/server/meteo.js +++ b/server/meteo.js @@ -91,7 +91,7 @@ module.exports = { .then(NOAA.aggregate) .then(NOAA.convert) .then(function(str) { - IO.write(jsonPath + station + '-' + year + '.json', str); + return IO.write(jsonPath + station + '-' + year + '.json', str); }); }, @@ -106,7 +106,7 @@ module.exports = { .then(NOAA.parseTxt) .then(NOAA.convert) .then(function(str) { - IO.write(jsonPath + station + '-' + year + '.json', str); + return IO.write(jsonPath + station + '-' + year + '.json', str); }); }, @@ -161,5 +161,5 @@ module.exports = { } return Promise.all(arr); - }, + } }; diff --git a/server/noaa.js b/server/noaa.js index f713a3a..1373682 100644 --- a/server/noaa.js +++ b/server/noaa.js @@ -47,9 +47,28 @@ module.exports = { return dateA - dateB; }); - // Filter for multiple headings/units rows. - var result = sorted.filter(function(row) { - return !(row[0] === '#YY' || row[0] === '#yr' || row.length === 1); + // Flag violations: headings, units, empty. + // Prepare average to trim incidental head/tail data points from different years. + var sum = 0; + var count = 0; + sorted.forEach(function(row) { + if (row[0] === '#YY' || row[0] === '#yr' || row.length === 1) { + row[0] = null; + } + else { + sum += parseInt(row[0]); + count++; + } + }); + + // Filter rows that have been flagged or that are the wrong year. + var year = Math.round(sum / count); + var result = sorted.filter(function(row, i) { + if (row[0] === null || row[0] != year) { + return false; + } + + return true; }); // Convert to JSON that can later be read easily.