Skip to content

Commit

Permalink
Update script to work with processed data
Browse files Browse the repository at this point in the history
  • Loading branch information
mwbernard committed Jan 18, 2024
1 parent 90eb917 commit 3b88554
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions vacs-map-app/scripts/generate-minimaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ const getGeoData = () => {
const getData = () => {
const file = fs.readFileSync(data_filename, 'utf-8');
let data = d3.csvParse(file, (d) => {
return Object.fromEntries(Object.entries(d).map(([k, v]) => [k, v && v !== '' ? +v : null]));
return Object.fromEntries(
Object.entries(d).map(([k, v]) => {
if (k.includes('maxCrop') || k.includes('minCrop')) return [k, v && v !== '' ? v : null]
return [k, v && v !== '' ? +v : null]
})
)
})

const grid_file = fs.readFileSync(grid_filename, 'utf-8');
Expand All @@ -47,32 +52,10 @@ const getData = () => {
);
})

const yieldKeys = Object.keys(data[0]).filter((k) => k.startsWith('yield'));

data = data.map((d, i) => {
const rowWithYields = {
id: d.id,
coords: [grid[i].X, grid[i].Y]
};

yieldKeys.forEach((k) => {
const [_, crop, timeframe, scenario] = k.split('_');
if (timeframe === 'historical') return;

const historicalKey = ['yield', crop, 'historical'].join('_');

if (!yieldKeys.includes(historicalKey)) return;

const yieldRatioKey = ['yieldratio', crop, timeframe, scenario].join('_');

let yieldRatioValue = null;
if (d[k] !== null && d[historicalKey] !== null && d[historicalKey]) {
yieldRatioValue = (d[k] - d[historicalKey]) / d[historicalKey];
}
rowWithYields[yieldRatioKey] = yieldRatioValue;
})
return rowWithYields;
data.forEach((d, i) => {
d.coords = [grid[i].X, grid[i].Y]
})

return data;
}

Expand Down

0 comments on commit 3b88554

Please sign in to comment.