const fs = require('fs') const parse = require('csv-parse') const stringify = require('csv-stringify') const parklandInput = fs.createReadStream('./data/media_01_parkland_shooting.csv') const averageInput = fs.createReadStream('./data/media_02_average_of_seven_shootings.csv') const parklandParser = parse() const averageParser = parse() const stringifier = stringify() const output = fs.createWriteStream('./data/media.csv') let finishCount = 0 let parkland = [], average = [] function finish() { finishCount += 1 console.log(finishCount) if (finishCount < 2) { return } console.log('finishing') let a, b, nn for (let n = 0; n < 12 * 8; n += 1) { nn = n / 8 a = find(nn, parkland) b = find(nn, average) stringifier.write([n, a, b]) } stringifier.end() } /* Given the sequence A of pairs (i,j) describing coordinates and an offset n, find i where A[i][0] <= n and n < A[i+1][0] and use these to interpolate the value of A(n) -> j or whatever you call it. */ function find(n, a) { let head, tail, dist for (let i = 1, len = a.length; i < len; i++) { if (a[i][0] > n) { head = a[i-1] tail = a[i] dist = (n - head[0]) / (tail[0] - head[0]) console.log(n, head, tail) return lerp(dist, head[1], tail[1]) } } } function lerp(n, a, b) { return (b-a) * n + a } parklandInput.on('readable', function() { let buf while ((buf = parklandInput.read()) !== null) { parklandParser.write(buf) } }) parklandInput.on('error', function(err){ console.error('parklandInput error', err.message) }) parklandInput.on('finish', function(){ parklandParser.end() }) averageInput.on('readable', function() { let buf while ((buf = averageInput.read()) !== null) { averageParser.write(buf) } }) averageInput.on('error', function(err){ console.error('averageInput error', err.message) }) averageInput.on('finish', function(){ console.log('enddd') averageParser.end() }) parklandParser.on('readable', function(){ let row while (row = parklandParser.read()) { parkland.push(row.map(n => parseFloat(n))) } // console.log('p', parkland.length) if (parkland.length == 108) finish() }) parklandParser.on('error', function(err){ console.error('parklandParser error', err.message) }) parklandParser.on('end', function(){ finish() }) averageParser.on('readable', function(){ let row while (row = averageParser.read()) { average.push(row.map(n => parseFloat(n))) } if (average.length == 89) finish() }) averageParser.on('error', function(err){ console.error('averageParser error', err.message) }) averageParser.on('end', function(){ finish() }) stringifier.on('readable', function(){ let row while (row = stringifier.read()){ output.write(row) } }) stringifier.on('error', function(err){ console.error('stringifier error', err.message) }) stringifier.on('finish', function(){ output.end() })