Skip to content

Commit

Permalink
Add yieldratio
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrelsford committed Oct 30, 2023
1 parent a056f01 commit 6a9d4a4
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions vacs-map-app/src/stores/cropYields.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,48 @@ export const useCropYieldsStore = defineStore('cropYields', () => {
const load = async () => {
if (loading.value || data.value) return false;
loading.value = true;
data.value = Object.freeze(await d3.csv(getDataUrl('crop-yields-mean-models.csv'), d => {

let transformedData = await d3.csv(getDataUrl('crop-yields-mean-models.csv'), d => {
return Object.fromEntries(
Object.entries(d).map(([k, v]) => ([k, (v && v !== "" ? +v : null)]))
);
}));
});

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

transformedData = transformedData.map((d, i) => {
const rowWithYields = { ...d };

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

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

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

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

let yieldRatioValue = null;
if (d[k] !== null && d[historicalKey] !== null) yieldRatioValue = d[k] / d[historicalKey];

rowWithYields[yieldRatioKey] = yieldRatioValue;
});

return rowWithYields;
});

data.value = Object.freeze(transformedData);
};

return {
Expand Down

0 comments on commit 6a9d4a4

Please sign in to comment.