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.
 
 
 
 

88 lines
2.4 KiB

/**
* NOAA-specific filtering.
*/
module.exports = {
/**
*
*/
splitLine: function(str) {
var arr = str.split(/\s+/);
arr.filter(function(val) { return (val.length > 0); })
return arr;
},
/**
* Receives a stream from a file read event.
*/
parseTxt: function(str) {
console.log('Parsing NOAA space-delimited columnar data into JSON.');
var arr = [];
var cols = null;
var lines = str.split('\n');
var len = lines.length;
if (len > 8) {
for (var i = 0; i < len; i++) {
cols = module.exports.splitLine(lines[i]);
cols.length > 0 ? arr.push(cols) : null;
}
}
return arr;
},
/**
* After all files have been parsed, Promises.all passes them all as an array.
* This function does filtering on them and finalizes a JSON string.
*/
convert: function(arr) {
console.log('Converting aggregated month files to JSON.');
// Sort.
var sorted = arr.sort(function(a, b) {
var dateA = parseInt([a[0], a[1], a[2], a[3], ('00' + a[4]).substr(-2)].join('')) || 0;
var dateB = parseInt([b[0], b[1], b[2], b[3], ('00' + b[4]).substr(-2)].join('')) || 0;
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);
});
// Convert to JSON that can later be read easily.
var str = null;
if (result.length > 0) {
str = JSON.stringify(result)
str = str.replace(/\],\[/g, '],\n[');
}
return str;
},
/**
* Used to aggregate month files after they have been split into a lines array.
* Each line has been split into individual elements.
* The array passed to this function is therefore an array of two dimensional arrays.
*
* This function adds non-empty lines to a common result set.
*/
aggregate: function(arr) {
console.log('Aggregating month files for the year.');
var tmp = [];
arr.forEach(function(rows) {
if (rows.length === 0) {
return;
}
tmp = tmp.concat(rows);
});
return tmp;
}
};