From 1d7bf5b2bc42c6e55556bdcfd8aa00f84586ef7a Mon Sep 17 00:00:00 2001 From: Abby Wheelis Date: Thu, 9 Nov 2023 09:13:35 -0700 Subject: [PATCH] remove country-specific since we've changed the way fallback labels are handled, everything should be custom now. This means that we're not using the carbon values from the set country, but rather from the labels --- www/__tests__/footprintHelper.test.ts | 4 +- www/js/metrics/CarbonDatasets.ts | 123 ------------------------ www/js/metrics/CustomMetricsHelper.ts | 74 +++----------- www/js/metrics/carbonDatasetFallback.ts | 14 +++ www/js/metrics/footprintHelper.ts | 12 +-- 5 files changed, 36 insertions(+), 191 deletions(-) delete mode 100644 www/js/metrics/CarbonDatasets.ts create mode 100644 www/js/metrics/carbonDatasetFallback.ts diff --git a/www/__tests__/footprintHelper.test.ts b/www/__tests__/footprintHelper.test.ts index 07c0a1264..5267bc858 100644 --- a/www/__tests__/footprintHelper.test.ts +++ b/www/__tests__/footprintHelper.test.ts @@ -29,11 +29,11 @@ const metrics = [ { key: 'Unicycle', values: 5000 }, ]; -it('gets footprint for metrics (not custom, fallback 0', () => { +it('gets footprint for metrics (not custom, fallback 0)', () => { expect(getFootprintForMetrics(metrics, 0)).toBe(10.534493474207583); }); -it('gets footprint for metrics (not custom, fallback 0.1', () => { +it('gets footprint for metrics (not custom, fallback 0.1)', () => { expect(getFootprintForMetrics(metrics, 0.1)).toBe(10.534493474207583 + 0.5); }); diff --git a/www/js/metrics/CarbonDatasets.ts b/www/js/metrics/CarbonDatasets.ts deleted file mode 100644 index b4815eead..000000000 --- a/www/js/metrics/CarbonDatasets.ts +++ /dev/null @@ -1,123 +0,0 @@ -// Values are in Kg/PKm (kilograms per passenger-kilometer) -// Sources for EU values: -// - Tremod: 2017, CO2, CH4 and N2O in CO2-equivalent -// - HBEFA: 2020, CO2 (per country) -// German data uses Tremod. Other EU countries (and Switzerland) use HBEFA for car and bus, -// and Tremod for train and air (because HBEFA doesn't provide these). -// EU data is an average of the Tremod/HBEFA data for the countries listed; -// for this average the HBEFA data was used also in the German set (for car and bus). -export const carbonDatasets = { - US: { - regionName: 'United States', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 267 / 1609, - BUS: 278 / 1609, - LIGHT_RAIL: 120 / 1609, - SUBWAY: 74 / 1609, - TRAM: 90 / 1609, - TRAIN: 92 / 1609, - AIR_OR_HSR: 217 / 1609, - }, - }, - EU: { - // Plain average of values for the countries below (using HBEFA for car and bus, Tremod for others) - regionName: 'European Union', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 0.14515, - BUS: 0.04751, - LIGHT_RAIL: 0.064, - SUBWAY: 0.064, - TRAM: 0.064, - TRAIN: 0.048, - AIR_OR_HSR: 0.201, - }, - }, - DE: { - regionName: 'Germany', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 0.139, // Tremod (passenger car) - BUS: 0.0535, // Tremod (average city/coach) - LIGHT_RAIL: 0.064, // Tremod (DE tram, urban rail and subway) - SUBWAY: 0.064, // Tremod (DE tram, urban rail and subway) - TRAM: 0.064, // Tremod (DE tram, urban rail and subway) - TRAIN: 0.048, // Tremod (DE average short/long distance) - AIR_OR_HSR: 0.201, // Tremod (DE airplane) - }, - }, - FR: { - regionName: 'France', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 0.13125, // HBEFA (passenger car, considering 1 passenger) - BUS: 0.04838, // HBEFA (average short/long distance, considering 16/25 passengers) - LIGHT_RAIL: 0.064, // Tremod (DE tram, urban rail and subway) - SUBWAY: 0.064, // Tremod (DE tram, urban rail and subway) - TRAM: 0.064, // Tremod (DE tram, urban rail and subway) - TRAIN: 0.048, // Tremod (DE average short/long distance) - AIR_OR_HSR: 0.201, // Tremod (DE airplane) - }, - }, - AT: { - regionName: 'Austria', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 0.14351, // HBEFA (passenger car, considering 1 passenger) - BUS: 0.04625, // HBEFA (average short/long distance, considering 16/25 passengers) - LIGHT_RAIL: 0.064, // Tremod (DE tram, urban rail and subway) - SUBWAY: 0.064, // Tremod (DE tram, urban rail and subway) - TRAM: 0.064, // Tremod (DE tram, urban rail and subway) - TRAIN: 0.048, // Tremod (DE average short/long distance) - AIR_OR_HSR: 0.201, // Tremod (DE airplane) - }, - }, - SE: { - regionName: 'Sweden', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 0.13458, // HBEFA (passenger car, considering 1 passenger) - BUS: 0.04557, // HBEFA (average short/long distance, considering 16/25 passengers) - LIGHT_RAIL: 0.064, // Tremod (DE tram, urban rail and subway) - SUBWAY: 0.064, // Tremod (DE tram, urban rail and subway) - TRAM: 0.064, // Tremod (DE tram, urban rail and subway) - TRAIN: 0.048, // Tremod (DE average short/long distance) - AIR_OR_HSR: 0.201, // Tremod (DE airplane) - }, - }, - NO: { - regionName: 'Norway', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 0.13265, // HBEFA (passenger car, considering 1 passenger) - BUS: 0.04185, // HBEFA (average short/long distance, considering 16/25 passengers) - LIGHT_RAIL: 0.064, // Tremod (DE tram, urban rail and subway) - SUBWAY: 0.064, // Tremod (DE tram, urban rail and subway) - TRAM: 0.064, // Tremod (DE tram, urban rail and subway) - TRAIN: 0.048, // Tremod (DE average short/long distance) - AIR_OR_HSR: 0.201, // Tremod (DE airplane) - }, - }, - CH: { - regionName: 'Switzerland', - footprintData: { - WALKING: 0, - BICYCLING: 0, - CAR: 0.17638, // HBEFA (passenger car, considering 1 passenger) - BUS: 0.04866, // HBEFA (average short/long distance, considering 16/25 passengers) - LIGHT_RAIL: 0.064, // Tremod (DE tram, urban rail and subway) - SUBWAY: 0.064, // Tremod (DE tram, urban rail and subway) - TRAM: 0.064, // Tremod (DE tram, urban rail and subway) - TRAIN: 0.048, // Tremod (DE average short/long distance) - AIR_OR_HSR: 0.201, // Tremod (DE airplane) - }, - }, -}; diff --git a/www/js/metrics/CustomMetricsHelper.ts b/www/js/metrics/CustomMetricsHelper.ts index 3d7eaf507..11493cb95 100644 --- a/www/js/metrics/CustomMetricsHelper.ts +++ b/www/js/metrics/CustomMetricsHelper.ts @@ -1,79 +1,35 @@ import angular from 'angular'; import { getLabelOptions } from '../survey/multilabel/confirmHelper'; import { getConfig } from '../config/dynamicConfig'; -import { storageGet, storageSet } from '../plugin/storage'; import { displayError, displayErrorMsg, logDebug } from '../plugin/logger'; import { standardMETs } from './metDataset'; -import { carbonDatasets } from './carbonDatasets'; - -const CARBON_DATASET_KEY = 'carbon_dataset_locale'; -const defaultCarbonDatasetCode = 'US'; +import { fallbackCarbon } from './carbonDatasetFallback'; let _customMETs; let _customPerKmFootprint; let _range_limited_motorized; let _inputParams; -let _currentCarbonDatasetCode = defaultCarbonDatasetCode; - -// we need to call the method from within a promise in initialize() -// and using this.setCurrentCarbonDatasetLocale doesn't seem to work -const setCurrentCarbonDatasetLocale = function (localeCode) { - for (var code in carbonDatasets) { - if (code == localeCode) { - _currentCarbonDatasetCode = localeCode; - break; - } - } -}; - -const loadCarbonDatasetLocale = function () { - return storageGet(CARBON_DATASET_KEY).then(function (localeCode) { - logDebug('loadCarbonDatasetLocale() obtained value from storage [' + localeCode + ']'); - if (!localeCode) { - localeCode = defaultCarbonDatasetCode; - logDebug('loadCarbonDatasetLocale() no value in storage, using [' + localeCode + '] instead'); - } - setCurrentCarbonDatasetLocale(localeCode); - }); -}; - -export const saveCurrentCarbonDatasetLocale = function (localeCode) { - setCurrentCarbonDatasetLocale(localeCode); - storageSet(CARBON_DATASET_KEY, _currentCarbonDatasetCode); - logDebug( - 'saveCurrentCarbonDatasetLocale() saved value [' + _currentCarbonDatasetCode + '] to storage', - ); -}; - -export const getCarbonDatasetOptions = function () { - var options = []; - for (var code in carbonDatasets) { - options.push({ - text: code, //carbonDatasets[code].regionName, - value: code, - }); - } - return options; -}; - -export const getCurrentCarbonDatasetCode = function () { - return _currentCarbonDatasetCode; -}; - -export const getCurrentCarbonDatasetFootprint = function () { - return carbonDatasets[_currentCarbonDatasetCode].footprintData; -}; export const getCustomMETs = function () { - console.log('Getting custom METs', _customMETs); + logDebug('Getting custom METs' + JSON.stringify(_customMETs)); return _customMETs; }; export const getCustomFootprint = function () { - console.log('Getting custom footprint', _customPerKmFootprint); + logDebug('Getting custom footprint' + JSON.stringify(_customPerKmFootprint)); return _customPerKmFootprint; }; +export const getRangeLimitedMotorixe = function () { + logDebug('Getting range limited motorized' + JSON.stringify(_range_limited_motorized)); + return _range_limited_motorized; +}; + +export const getFallbackFootprint = function () { + console.log('getting fallback carbon'); + return fallbackCarbon.footprintData; +}; + const populateCustomMETs = function () { let modeOptions = _inputParams['MODE']; let modeMETEntries = modeOptions.map((opt) => { @@ -103,7 +59,7 @@ const populateCustomMETs = function () { } }); _customMETs = Object.fromEntries(modeMETEntries.filter((e) => angular.isDefined(e))); - console.log('After populating, custom METs = ', _customMETs); + logDebug('After populating, custom METs = ' + JSON.stringify(_customMETs)); }; const populateCustomFootprints = function () { @@ -128,7 +84,7 @@ const populateCustomFootprints = function () { }) .filter((modeCO2) => angular.isDefined(modeCO2)); _customPerKmFootprint = Object.fromEntries(modeCO2PerKm); - console.log('After populating, custom perKmFootprint', _customPerKmFootprint); + logDebug('After populating, custom perKmFootprint' + JSON.stringify(_customPerKmFootprint)); }; export const initCustomDatasetHelper = async function (newConfig) { diff --git a/www/js/metrics/carbonDatasetFallback.ts b/www/js/metrics/carbonDatasetFallback.ts new file mode 100644 index 000000000..4561d078a --- /dev/null +++ b/www/js/metrics/carbonDatasetFallback.ts @@ -0,0 +1,14 @@ +export const fallbackCarbon = { + regionName: 'United States', + footprintData: { + WALKING: 0, + BICYCLING: 0, + CAR: 267 / 1609, + BUS: 278 / 1609, + LIGHT_RAIL: 120 / 1609, + SUBWAY: 74 / 1609, + TRAM: 90 / 1609, + TRAIN: 92 / 1609, + AIR_OR_HSR: 217 / 1609, + }, +}; diff --git a/www/js/metrics/footprintHelper.ts b/www/js/metrics/footprintHelper.ts index 4725aa1c7..2162e87bf 100644 --- a/www/js/metrics/footprintHelper.ts +++ b/www/js/metrics/footprintHelper.ts @@ -1,5 +1,5 @@ -import { getCustomFootprint } from './CustomMetricsHelper'; -import { getCurrentCarbonDatasetFootprint } from './CustomMetricsHelper'; +import { displayErrorMsg } from '../plugin/logger'; +import { getCustomFootprint, getFallbackFootprint } from './CustomMetricsHelper'; var highestFootprint = 0; let useCustom = false; @@ -16,14 +16,12 @@ const getFootprint = function () { if (useCustom == true) { return getCustomFootprint(); } else { - return getCurrentCarbonDatasetFootprint(); + //TODO: check through configs and ensure they all have custom lables + displayErrorMsg('Error in Footprint Calculatons', 'issue with data or default labels'); + return getFallbackFootprint(); } }; -const readableFormat = function (v) { - return v > 999 ? Math.round(v / 1000) + 'k kg CO₂' : Math.round(v) + ' kg CO₂'; -}; - export const getFootprintForMetrics = function (userMetrics, defaultIfMissing = 0) { var footprint = getFootprint(); var result = 0;