var fs = require('fs');
var csv = require('csv');
var _ = require('lodash');
var dir = './projection-coefficients/';
var out = 'coeff.json';
var obj = {};
function done() {
var o = fs.createWriteStream(out, {encoding: 'utf8'});
o.once('open', function(fd) {
console.log(JSON.stringify(obj));
o.write(JSON.stringify(obj)+'\n');
o.end();
});
}
fs.readdir(dir, function(err, files) {
var c = 0;
var fnames = files.reduce(function(a,f,i) {
if (/\.csv$/.test(f)) a.push(f);
return a;
}, []);
fnames.forEach(function(f) {
var input = fs.createReadStream(dir.concat(f));
var key = f.replace(/\..*/gi,'');
input.on('data', function(d) {
csv.parse(d.toString(), function(err, output) {
var headers = output[0];
headers.splice(0, 1, 'index');
obj[key] = [];
output.slice(1).forEach(function(l) {
obj[key].push(_.zipObject(headers, l));
});
c += 1;
if (c === fnames.length) done();
});
});
});
});