Skip to content

Commit

Permalink
Merge pull request #410 from ProjectDrawdown/custom_uploads
Browse files Browse the repository at this point in the history
Allow overwrite of TAM by the scenario
  • Loading branch information
denised authored Aug 24, 2021
2 parents fc3f7af + 46a1658 commit c94f1c2
Show file tree
Hide file tree
Showing 58 changed files with 1,338 additions and 2,030 deletions.
21 changes: 21 additions & 0 deletions model/advanced_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,27 @@ class AdvancedControls:
'tooltip': ("t C storage in Protected Landtype"),
'excelref': '',
})

# NEW ADDITIONS
# These do not represent quantities that were part of the original spreadsheets, but are used in
# for communication from external sources

# Value is a URI that will return a csv for a custom source to use in place of the REF TAM,
# overriding whatever else is defined for this solution/scenario
ref_tam_custom_source: str = None

# Value is a URI that will return a csv for a custom source to use in place of the PDS TAM,
# overriding whatever else is defined for this solution/scenario
pds_tam_custom_source: str = None

# Value is a URI that will return a csv for a custom source to use in place of the REF Adoption (as a Fully Custom Adoption),
# overriding whatever else is defined for this solution/scenario
ref_adoption_custom_source: str = None

# Value is a URI that will return a csv for a custom source to use in place of the PDS Adoption (as a Fully Custom Adoption),
# overriding whatever else is defined for this solution/scenario
pds_adoption_custom_source: str = None


def __post_init__(self):
object.__setattr__(self, 'incorrect_cached_values', {})
Expand Down
112 changes: 110 additions & 2 deletions model/scenario.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,115 @@
"""Base class of all scenario objects"""
"""Base classes of all scenario objects"""
from pathlib import Path

import numpy as np
import pandas as pd

from model import adoptiondata
from model import advanced_controls
from model import ch4calcs
from model import co2calcs
from model import customadoption
from model import dd
from model import emissionsfactors
from model import firstcost
from model import helpertables
from model import operatingcost
from model import s_curve
from model import scenario
from model import unitadoption
from model import vma
from model import tam
from model import conversions
from solution import rrs

# This class is currently a stub, because the code was not oritinally designed with a common Scenario base class.
# We expect to add new functionality, and probably migrate some shared functionality, to this class.

class Scenario:
pass

ac : advanced_controls.AdvancedControls = None

def set_ref_adoption(self):
pass

def set_pds_adoption(self):
pass


class RRSScenario(Scenario):

tm: tam.TAM = None

# This may be overridden by a class if they don't use the standard configuration
_tamconfig_list = [
['param', 'World', 'PDS World', 'OECD90', 'Eastern Europe', 'Asia (Sans Japan)',
'Middle East and Africa', 'Latin America', 'China', 'India', 'EU', 'USA'],
['source_until_2014', None, None, # These will be substituted from the scenario
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['source_after_2014', None, None, # These will be substituted from the scenario
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['trend', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly'],
['growth', 'Medium', 'Medium', 'Medium', 'Medium',
'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium'],
['low_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
['high_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]

# These must be set by each class
tam_ref_data_sources = None
tam_pds_data_sources = None


def set_tam(self, config_values=None, **args):
"""Create the self.tm object based on the information in self._tamconfig_list, self.tam_ref_data_sources
and self.tam_pds_data_sources.
Overrides to individual values in the tamconfig can also be specified
in the config_values argument, which should be a list of tuples (rowname, columname, value)
Other configuration values may be passed directly to tam.TAM via **args.
"""
tamconfig = pd.DataFrame(self._tamconfig_list[1:], columns=self._tamconfig_list[0]).set_index('param')
tamconfig.loc['source_until_2014','World'] = self.ac.source_until_2014
tamconfig.loc['source_until_2014','PDS World'] = self.ac.source_until_2014
tamconfig.loc['source_after_2014','World'] = self.ac.ref_source_post_2014
tamconfig.loc['source_after_2014','PDS World'] = self.ac.pds_source_post_2014
if config_values is not None:
for (row,col,val) in config_values:
tamconfig.loc[row,col] = val

if self.ac.ref_tam_custom_source:
# completely override the tam_ref_data_sources field
# #HACK
# TAM doesn't auto-interpolate single sources. But it does auto-interpolate groups.
# So we use the sneaky hack of duplicating the source get TAM to do this work for us.
# It keeps the code changes less invasive for now, but should be refactored later.
self.tam_ref_data_sources = { 'Custom Cases' : {
'Custom Ref Tam' : self.ac.ref_tam_custom_source,
'Custom Ref Dup' : self.ac.ref_tam_custom_source
}}
# set 'source_after_2014' for 'World' and all regions (everything except 'PDS World')
pdsworld = tamconfig.loc['source_after_2014', 'PDS World']
tamconfig.loc['source_after_2014',:] = 'Custom Cases'
tamconfig.loc['source_after_2014','PDS World'] = pdsworld
if self.ac.pds_tam_custom_source:
# completely override the pds_ref_data_sources field
self.tam_pds_data_sources = { 'Custom Cases' : {
'Custom PDS Tam' : self.ac.ref_tam_custom_source,
'Custom PDS Dup' : self.ac.ref_tam_custom_source
}}
tamconfig.loc['source_after_2014','PDS World'] = 'Custom Cases'

self.tm = tam.TAM(
tamconfig=tamconfig,
tam_ref_data_sources=self.tam_ref_data_sources,
tam_pds_data_sources=self.tam_pds_data_sources,
**args)



class LandScenario(Scenario):
pass
1 change: 1 addition & 0 deletions model/tam.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def forecast_trend(self, region, trend=None):
Degree3: SolarPVUtil 'TAM Data'!CM677:CQ723 Exponential: SolarPVUtil 'TAM Data'!CV677:CX723
"""

# #BAD EXCEL Remove this condition when we aren't trying to match Excel.
if region in self.interpolation_overrides:
result = pd.read_csv(self.interpolation_overrides[region], index_col='Year' )
else:
Expand Down
44 changes: 14 additions & 30 deletions solution/airplanes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,39 +131,13 @@
PDS2 = "PDS2-85p2050-18%Efficiency"
PDS3 = "PDS3-100p2050-20%Efficiency"

class Scenario(scenario.Scenario):
class Scenario(scenario.RRSScenario):
name = name
units = units
vmas = VMAs
solution_category = solution_category

def __init__(self, scenario=None):
if isinstance(scenario, ac.AdvancedControls):
self.scenario = scenario.name
self.ac = scenario
else:
self.scenario = scenario or PDS2
self.ac = scenarios[self.scenario]

# TAM
tamconfig_list = [
['param', 'World', 'PDS World', 'OECD90', 'Eastern Europe', 'Asia (Sans Japan)',
'Middle East and Africa', 'Latin America', 'China', 'India', 'EU', 'USA'],
['source_until_2014', self.ac.source_until_2014, self.ac.source_until_2014,
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['source_after_2014', self.ac.ref_source_post_2014, self.ac.pds_source_post_2014,
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['trend', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly'],
['growth', 'Medium', 'Medium', 'Medium', 'Medium',
'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium'],
['low_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
['high_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]
tamconfig = pd.DataFrame(tamconfig_list[1:], columns=tamconfig_list[0]).set_index('param')
tam_ref_data_sources = {
tam_ref_data_sources = {
'Baseline Cases': {
'Based on: IEA ETP 2016 6DS': THISDIR.joinpath('tam', 'tam_based_on_IEA_ETP_2016_6DS.csv'),
'ICCT (2012) Global Roadmap Model': THISDIR.joinpath('tam', 'tam_ICCT_2012_Global_Roadmap_Model.csv'),
Expand All @@ -175,8 +149,18 @@ def __init__(self, scenario=None):
'Based on: IEA ETP 2016 2DS': THISDIR.joinpath('tam', 'tam_based_on_IEA_ETP_2016_2DS.csv'),
},
}
self.tm = tam.TAM(tamconfig=tamconfig, tam_ref_data_sources=tam_ref_data_sources,
tam_pds_data_sources=tam_ref_data_sources)
tam_pds_data_sources = tam_ref_data_sources

def __init__(self, scenario=None):
if isinstance(scenario, ac.AdvancedControls):
self.scenario = scenario.name
self.ac = scenario
else:
self.scenario = scenario or PDS2
self.ac = scenarios[self.scenario]

# TAM
self.set_tam()
ref_tam_per_region=self.tm.ref_tam_per_region()
pds_tam_per_region=self.tm.pds_tam_per_region()

Expand Down
56 changes: 20 additions & 36 deletions solution/altcement/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,30 @@
PDS2 = "PDS2-100p2050-0.46clinker/cement-postintjune2020"
PDS3 = "PDS-100p2050-0.27clinker/cement-postintjune2020"

class Scenario(scenario.Scenario):
class Scenario(scenario.RRSScenario):
name = name
units = units
vmas = VMAs
solution_category = solution_category

tam_ref_data_sources = {
'Baseline Cases': {
'Project Drawdown - Based on Data from Several Sources. (See HVFAC Links Sheet and HVFAC Material Availability Models)': THISDIR.joinpath('tam', 'tam_Project_Drawdown_based_on_Data_from_Several_Sources__See_HVFAC_Links_Sheet_and_HVFAC_Mat_2961774c.csv'),
},
'Conservative Cases': {
'IEA 2018 Low-Variability': THISDIR.joinpath('tam', 'tam_IEA_2018_LowVariability.csv'),
'Farfan et al. 2019': THISDIR.joinpath('tam', 'tam_Farfan_et_al__2019.csv'),
'van Ruijven et al. 2016': THISDIR.joinpath('tam', 'tam_van_Ruijven_et_al__2016.csv'),
},
'Ambitious Cases': {
'IEA 2018 High-Variability': THISDIR.joinpath('tam', 'tam_IEA_2018_HighVariability.csv'),
},
'Maximum Cases': {
'WBCSD Cement 2002': THISDIR.joinpath('tam', 'tam_WBCSD_Cement_2002.csv'),
},
}
tam_pds_data_sources = tam_ref_data_sources

def __init__(self, scenario=None):
if isinstance(scenario, ac.AdvancedControls):
self.scenario = scenario.name
Expand All @@ -144,41 +162,7 @@ def __init__(self, scenario=None):
self.ac = scenarios[self.scenario]

# TAM
tamconfig_list = [
['param', 'World', 'PDS World', 'OECD90', 'Eastern Europe', 'Asia (Sans Japan)',
'Middle East and Africa', 'Latin America', 'China', 'India', 'EU', 'USA'],
['source_until_2014', self.ac.source_until_2014, self.ac.source_until_2014,
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['source_after_2014', self.ac.ref_source_post_2014, self.ac.pds_source_post_2014,
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['trend', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly'],
['growth', 'Medium', 'Medium', 'Medium', 'Medium',
'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium'],
['low_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
['high_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]
tamconfig = pd.DataFrame(tamconfig_list[1:], columns=tamconfig_list[0]).set_index('param')
tam_ref_data_sources = {
'Baseline Cases': {
'Project Drawdown - Based on Data from Several Sources. (See HVFAC Links Sheet and HVFAC Material Availability Models)': THISDIR.joinpath('tam', 'tam_Project_Drawdown_based_on_Data_from_Several_Sources__See_HVFAC_Links_Sheet_and_HVFAC_Mat_2961774c.csv'),
},
'Conservative Cases': {
'IEA 2018 Low-Variability': THISDIR.joinpath('tam', 'tam_IEA_2018_LowVariability.csv'),
'Farfan et al. 2019': THISDIR.joinpath('tam', 'tam_Farfan_et_al__2019.csv'),
'van Ruijven et al. 2016': THISDIR.joinpath('tam', 'tam_van_Ruijven_et_al__2016.csv'),
},
'Ambitious Cases': {
'IEA 2018 High-Variability': THISDIR.joinpath('tam', 'tam_IEA_2018_HighVariability.csv'),
},
'Maximum Cases': {
'WBCSD Cement 2002': THISDIR.joinpath('tam', 'tam_WBCSD_Cement_2002.csv'),
},
}
self.tm = tam.TAM(tamconfig=tamconfig, tam_ref_data_sources=tam_ref_data_sources,
tam_pds_data_sources=tam_ref_data_sources)
self.set_tam()
ref_tam_per_region=self.tm.ref_tam_per_region()
pds_tam_per_region=self.tm.pds_tam_per_region()

Expand Down
52 changes: 18 additions & 34 deletions solution/bikeinfrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,28 @@
PDS2 = "PDS2-8p2050-Based on Several Sources (Book Ed.1)"
PDS3 = "PDS3-10p2050-ICE Car Replacement (Book Ed.1)"

class Scenario(scenario.Scenario):
class Scenario(scenario.RRSScenario):
name = name
units = units
vmas = VMAs
solution_category = solution_category

tam_ref_data_sources = {
'Baseline Cases': {
'ETP 2016, URBAN 6 DS + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ETP_2016_URBAN_6_DS_Nonmotorized_Travel_Adjustment.csv'),
'ICCT, 2012, "Global Transportation Roadmap Model" + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ICCT_2012_Global_Transportation_Roadmap_Model_Nonmotorized_Travel_Adjustment.csv'),
},
'Conservative Cases': {
'ETP 2016, URBAN 4 DS + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ETP_2016_URBAN_4_DS_Nonmotorized_Travel_Adjustment.csv'),
'ITDP/UC Davis 2014 Global High Shift Baseline': THISDIR.joinpath('tam', 'tam_ITDPUC_Davis_2014_Global_High_Shift_Baseline.csv'),
},
'Ambitious Cases': {
'ETP 2016, URBAN 2 DS + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ETP_2016_URBAN_2_DS_Nonmotorized_Travel_Adjustment.csv'),
'ITDP/UC Davis 2014 Global High Shift HighShift': THISDIR.joinpath('tam', 'tam_ITDPUC_Davis_2014_Global_High_Shift_HighShift.csv'),
},
}
tam_pds_data_sources=tam_ref_data_sources

def __init__(self, scenario=None):
if isinstance(scenario, ac.AdvancedControls):
self.scenario = scenario.name
Expand All @@ -128,39 +144,7 @@ def __init__(self, scenario=None):
self.ac = scenarios[self.scenario]

# TAM
tamconfig_list = [
['param', 'World', 'PDS World', 'OECD90', 'Eastern Europe', 'Asia (Sans Japan)',
'Middle East and Africa', 'Latin America', 'China', 'India', 'EU', 'USA'],
['source_until_2014', self.ac.source_until_2014, self.ac.source_until_2014,
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['source_after_2014', self.ac.ref_source_post_2014, self.ac.pds_source_post_2014,
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES',
'ALL SOURCES', 'ALL SOURCES', 'ALL SOURCES'],
['trend', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly', '3rd Poly',
'3rd Poly', '3rd Poly', '3rd Poly'],
['growth', 'Medium', 'Medium', 'Medium', 'Medium',
'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium'],
['low_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
['high_sd_mult', 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]
tamconfig = pd.DataFrame(tamconfig_list[1:], columns=tamconfig_list[0]).set_index('param')
tam_ref_data_sources = {
'Baseline Cases': {
'ETP 2016, URBAN 6 DS + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ETP_2016_URBAN_6_DS_Nonmotorized_Travel_Adjustment.csv'),
'ICCT, 2012, "Global Transportation Roadmap Model" + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ICCT_2012_Global_Transportation_Roadmap_Model_Nonmotorized_Travel_Adjustment.csv'),
},
'Conservative Cases': {
'ETP 2016, URBAN 4 DS + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ETP_2016_URBAN_4_DS_Nonmotorized_Travel_Adjustment.csv'),
'ITDP/UC Davis 2014 Global High Shift Baseline': THISDIR.joinpath('tam', 'tam_ITDPUC_Davis_2014_Global_High_Shift_Baseline.csv'),
},
'Ambitious Cases': {
'ETP 2016, URBAN 2 DS + Non-motorized Travel Adjustment': THISDIR.joinpath('tam', 'tam_ETP_2016_URBAN_2_DS_Nonmotorized_Travel_Adjustment.csv'),
'ITDP/UC Davis 2014 Global High Shift HighShift': THISDIR.joinpath('tam', 'tam_ITDPUC_Davis_2014_Global_High_Shift_HighShift.csv'),
},
}
self.tm = tam.TAM(tamconfig=tamconfig, tam_ref_data_sources=tam_ref_data_sources,
tam_pds_data_sources=tam_ref_data_sources)
self.set_tam()
ref_tam_per_region=self.tm.ref_tam_per_region()
pds_tam_per_region=self.tm.pds_tam_per_region()

Expand Down
Loading

0 comments on commit c94f1c2

Please sign in to comment.