Skip to content

Commit

Permalink
feat(carbon-stock): fix calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
wri7tno committed Jan 28, 2025
1 parent 2d64344 commit 671ab73
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
1 change: 1 addition & 0 deletions components/widgets/climate/carbon-stock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default {
if (data && data.length === 1) {
parsedData = {
...data[0],
...soilCarbonData[0],
soilCarbon: soilCarbonData.soil_carbon__t || 0,
soilCarbonDensity: soilCarbonData.soil_carbon_density__t_ha || 0,
};
Expand Down
14 changes: 7 additions & 7 deletions components/widgets/climate/carbon-stock/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export const calculateData = createSelector(
const { variable } = settings;
const extent = (data && data.extent) || 0;
const soil = (data && data.gfw_soil_carbon_stocks_2000__mg_c) || 0;
const soilDensity = (data && data.soilCarbonDensity) || 0;
const soilDensity = (data && data.biomassdensity) || 0;

const aboveGroundCarbon =
const aboveGroundCarbonDensity =
(data && data.gfw_aboveground_carbon_stocks_2000__mg_c) || 0;
const belowGroundCarbon =
const belowGroundCarbonDensity =
(data && data.gfw_belowground_carbon_stocks_2000__mg_c) || 0;

const aboveGroundCarbonDensity =
extent > 0 ? aboveGroundCarbon / extent : 0;
const belowGroundCarbonDensity =
extent > 0 ? belowGroundCarbon / extent : 0;
const aboveGroundCarbon =
extent > 0 ? aboveGroundCarbonDensity * extent : 0;
const belowGroundCarbon =
extent > 0 ? belowGroundCarbonDensity * extent : 0;
const total = soil + aboveGroundCarbon + belowGroundCarbon;

return {
Expand Down
56 changes: 33 additions & 23 deletions components/widgets/climate/soil-organic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export default {
label: 'unit',
type: 'switch',
whitelist: ['totalBiomass', 'biomassDensity'],
border: true,
},
{
key: 'threshold',
label: 'canopy density',
type: 'mini-select',
metaKey: 'widget_canopy_density',
},
],
datasets: [
Expand All @@ -40,7 +47,7 @@ export default {
layers: [SOIL_CARBON_DENSITY],
},
],
refetchKeys: ['unit'],
refetchKeys: ['unit', 'threshold'],
chartType: 'rankedList',
visible: ['dashboard', 'analysis'],
colors: 'climate',
Expand All @@ -55,10 +62,11 @@ export default {
pageSize: 5,
page: 0,
unit: 'totalBiomass',
threshold: 30,
},
sentences: {
region:
'In 2000, {location} had a soil organic carbon density of {biomassDensity}, and a total carbon storage of {totalBiomass}.',
'In 2000, {location} had a soil organic carbon density of {biomassDensity}, and a total soil carbon storage of {totalBiomass}.',
globalBiomass:
'Around {value} of the world’s {label} is contained in the top 5 countries.',
globalDensity:
Expand All @@ -68,36 +76,38 @@ export default {
const location =
type === 'country'
? {
type,
adm0: adm0 && !adm1 ? null : adm0,
adm1: adm1 && !adm2 ? null : adm1,
adm2: null,
}
type,
adm0: adm0 && !adm1 ? null : adm0,
adm1: adm1 && !adm2 ? null : adm1,
adm2: null,
}
: {
type,
adm0,
adm1,
adm2,
};
type,
adm0,
adm1,
adm2,
};

return getOrganicSoilCarbonGrouped({ ...rest, ...location });
},
getDataURL: ({ type, adm0, adm1, adm2, ...rest } = {}) => {
const location =
type === 'country'
? {
type,
adm0: adm0 && !adm1 ? null : adm0,
adm1: adm1 && !adm2 ? null : adm1,
adm2: null,
}
type,
adm0: adm0 && !adm1 ? null : adm0,
adm1: adm1 && !adm2 ? null : adm1,
adm2: null,
}
: {
type,
adm0,
adm1,
adm2,
};
return [getOrganicSoilCarbonGrouped({ ...rest, ...location, download: true })];
type,
adm0,
adm1,
adm2,
};
return [
getOrganicSoilCarbonGrouped({ ...rest, ...location, download: true }),
];
},
getWidgetProps,
};
17 changes: 9 additions & 8 deletions components/widgets/climate/soil-organic/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getSortedData = createSelector(
[getData, getSettings, getAdm1, getAdm2],
(data, settings, adm1, adm2) => {
if (isEmpty(data)) return null;
// console.log('soil-organic', { data, settings })

let regionKey = 'iso';
if (adm1) regionKey = 'adm1';
if (adm2) regionKey = 'adm2';
Expand Down Expand Up @@ -121,10 +121,10 @@ export const parseSentence = createSelector(
settings.unit === 'totalBiomass'
? formatNumber({ num: percent, unit: '%' })
: formatNumber({
num: avgBiomDensity,
unit: 'tC/ha',
spaceUnit: true,
});
num: avgBiomDensity,
unit: 'tC/ha',
spaceUnit: true,
});

const labels = {
globalDensity: 'soil organic carbon density',
Expand All @@ -134,9 +134,10 @@ export const parseSentence = createSelector(
// Properties defined for labels and sentences are named in a way that relates with the display
// but the units are somewhat fixed, due to their dependency on the whitelists for settings dropdowns.
// We map them here.
const sentenceLabelProperty = settings.unit === 'totalBiomass' ? 'globalBiomass' : 'globalDensity';
const sentenceLabelProperty =
settings.unit === 'totalBiomass' ? 'globalBiomass' : 'globalDensity';
const sentence = sentences[sentenceLabelProperty];
const label = labels[sentenceLabelProperty]
const label = labels[sentenceLabelProperty];

return {
sentence,
Expand All @@ -145,7 +146,7 @@ export const parseSentence = createSelector(
value,
},
};
};
}

// Standard processing for adm1 and adm2 areas, same sentence, same formatting.
const location_id = location && location.value;
Expand Down

0 comments on commit 671ab73

Please sign in to comment.