Skip to content

Commit

Permalink
Merge pull request #121 from incubateur-ademe/bug-stocks-agg
Browse files Browse the repository at this point in the history
Reparer bugs avec les moyens de stock de ref et modifications surface
  • Loading branch information
hfroot authored Aug 31, 2023
2 parents 3fbd8aa + b36ae59 commit 9fc5d26
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions calculations/stocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ function getStocksForLocation (location, options) {
}

function aggregateStocks (stocksForLocations) {
// TODO: area overrides should happen at this level
const aggregatedStocks = {}
const sumKeys = [
'area',
Expand All @@ -323,6 +322,7 @@ function aggregateStocks (stocksForLocations) {
'biStock',
'boStock'
]
const meanFallbacks = {} // if there is no area, want to fall back to mean not weighted avg
const areaWeightedKeys = [
'groundDensity',
'biomassDensity',
Expand All @@ -346,16 +346,18 @@ function aggregateStocks (stocksForLocations) {
stocksForLocations.forEach((stocksForLocation) => {
Object.entries(stocksForLocation).forEach(([groundType, valuesForType]) => {
if (!aggregatedStocks[groundType]) aggregatedStocks[groundType] = {}
if (!meanFallbacks[groundType]) meanFallbacks[groundType] = {}
Object.entries(valuesForType).forEach(([key, value]) => {
if (!aggregatedStocks[groundType][key]) {
aggregatedStocks[groundType][key] = 0
}
if (!meanFallbacks[groundType][key]) {
meanFallbacks[groundType][key] = 0
}
if (sumKeys.includes(key)) aggregatedStocks[groundType][key] += value
else if (areaWeightedKeys.includes(key)) {
// fall back to 1 here and dividing by location count below
// so that if no area for ground type, user still has chance to enter their data
const area = stocksForLocation[groundType].area || 1
aggregatedStocks[groundType][key] += (value * area)
aggregatedStocks[groundType][key] += value * stocksForLocation[groundType].area
meanFallbacks[groundType][key] += value
}
if (constantKeys.includes(key)) {
aggregatedStocks[groundType][key] = value || aggregatedStocks[groundType][key]
Expand All @@ -365,8 +367,13 @@ function aggregateStocks (stocksForLocations) {
})
Object.keys(aggregatedStocks).forEach((groundType) => {
areaWeightedKeys.forEach((key) => {
if (!aggregatedStocks[groundType][key]) return
aggregatedStocks[groundType][key] /= (aggregatedStocks[groundType].area || stocksForLocations.length)
if (!aggregatedStocks[groundType][key] && !meanFallbacks[groundType][key]) return
const area = aggregatedStocks[groundType].area
if (area) {
aggregatedStocks[groundType][key] /= area
} else {
aggregatedStocks[groundType][key] = meanFallbacks[groundType][key] / stocksForLocations.length
}
})
aggregatedStocks[groundType].originalArea = aggregatedStocks[groundType].area
})
Expand Down Expand Up @@ -471,6 +478,7 @@ function calculateStocks (stock) {
stock[`${key}Stock`] = density * area
}
})
stock.totalReservoirStock = stock.totalStock
}

function sumAllBiomassStock (stock) {
Expand Down

0 comments on commit 9fc5d26

Please sign in to comment.