From 16279c7fddc63be64391ee92315a15f673679a82 Mon Sep 17 00:00:00 2001 From: Helen Root Date: Thu, 31 Aug 2023 08:52:02 +0200 Subject: [PATCH 1/2] Fix bug where the densities of communes with 0 area were included in the weighted average. These are now only included if there is no area at all for the territory group selected. --- calculations/stocks/index.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/calculations/stocks/index.js b/calculations/stocks/index.js index 1c699254..2e772e24 100644 --- a/calculations/stocks/index.js +++ b/calculations/stocks/index.js @@ -323,6 +323,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', @@ -346,16 +347,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] @@ -365,8 +368,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 }) From b36ae59d87863fb0cebf8249d5f81b03102bfc72 Mon Sep 17 00:00:00 2001 From: Helen Root Date: Thu, 31 Aug 2023 09:21:13 +0200 Subject: [PATCH 2/2] Fix stock update from area customisation --- calculations/stocks/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/calculations/stocks/index.js b/calculations/stocks/index.js index 2e772e24..3e585cfa 100644 --- a/calculations/stocks/index.js +++ b/calculations/stocks/index.js @@ -302,7 +302,6 @@ function getStocksForLocation (location, options) { } function aggregateStocks (stocksForLocations) { - // TODO: area overrides should happen at this level const aggregatedStocks = {} const sumKeys = [ 'area', @@ -479,6 +478,7 @@ function calculateStocks (stock) { stock[`${key}Stock`] = density * area } }) + stock.totalReservoirStock = stock.totalStock } function sumAllBiomassStock (stock) {