def _act_from_variable(self, variable, db, year, region, scale=1): """ Find the activity for a given REMIND transport reporting variable. """ techmap = { "BEV": "battery electric", "FCEV": "fuel cell electric", "Gases": "compressed gas", "Hybrid Electric": { "diesel": "plugin diesel hybrid", "petrol": "plugin gasoline hybrid" }, "Hybrid Liquids": { "diesel": "diesel hybrid", "petrol": "gasoline hybrid" }, "Liquids": { "diesel": "diesel", "petrol": "gasoline" } } tech = variable.split("|")[-1] liq_share = {"diesel": 0.4, "petrol": 0.6} if tech in ["Hybrid Electric", "Hybrid Liquids", "Liquids"]: if region in ["CHA", "REF", "IND"]: demand = { Activity( Act.get((Act.name == "transport, passenger car, fleet average, {}".format(techmap[tech]["petrol"])) & (Act.location == region) & (Act.database == db.name))): scale } else: demand = { Activity( Act.get((Act.name == "transport, passenger car, fleet average, {}". format(techmap[tech][liq])) & (Act.location == region) & (Act.database == db.name))): scale * liq_share[liq] for liq in ["diesel", "petrol"] } else: try: demand = { Activity( Act.get((Act.name == "transport, passenger car, fleet average, {}".format(techmap[tech])) & (Act.location == region) & (Act.database == db.name))): scale } except Exception as e: print("Error: {}".format(e.__class__.__name__)) print("No activity found for {}, {} in {}".format( techmap[tech], year, db.name)) demand = {} return demand
def _get_material_bioflows_for_bev(self): """ Obtain bioflow ids for *interesting* materials. These are the top bioflows in the ILCD materials characterization method for an BEV activity. """ method = ('ILCD 2.0 2018 midpoint', 'resources', 'minerals and metals') year = self.years[0] act_str = "transport, passenger car, fleet average, battery electric, {}".format(year) # upstream material demands are the same for all regions # so we can use GLO here act = Activity( Act.get((Act.name == act_str) & (Act.database == eidb_label( self.model, self.scenario, year)) & (Act.location == "EUR"))) lca = bw.LCA({act: 1}, method=method) lca.lci() lca.lcia() inv_bio = {value: key for key, value in lca.biosphere_dict.items()} ca = ContributionAnalysis() ef_contrib = ca.top_emissions(lca.characterized_inventory) return [inv_bio[int(el[1])] for el in ef_contrib]
def _find_suppliers(self, db, expr, locs): """ Return a list of supplier activites in locations `locs` matching the peewee expression `expr` within `db`. """ assert type(locs) == list sel = self.selector.select(db, expr, locs) if sel.count() == 0: locs = ["RER"] sel = self.selector.select(db, expr, locs) if sel.count() == 0: locs = ["RoW"] sel = self.selector.select(db, expr, locs) if sel.count == 0: raise ValueError("No activity found for expression.") return [Activity(a) for a in sel]
def _copy_activity(target: str, act: Activity) -> tuple: new_code = ActivityController.generate_copy_code((target, act['code'])) new_key = (target, new_code) act.copy(code=new_code, database=target) AB_metadata.update_metadata(new_key) return new_key
def find_evs(): # find EVs (rexexp function in peewee seems to be broken) return [Activity(sel) for sel in Act.select().where( (Act.name.contains("EV,") | Act.name.contains("PHEV-")) # PHEV-d and PHEV-p & (Act.database == eidb.name))]
def relink_electricity_demand(scenario, year): """Create LDV activities for REMIND regions and relink existing electricity exchanges for BEVs, FCEVs and PHEVs to REMIND-compatible (regional) market groups. :param eidb: REMIND scenario. :param year: REMIND year. """ eidb = bw.Database(premise.utils.eidb_label(model, scenario, year)) remind_regions = [ 'LAM', 'OAS', 'SSA', 'EUR', 'NEU', 'MEA', 'REF', 'CAZ', 'CHA', 'IND', 'JPN', 'USA'] def find_evs(): # find EVs (rexexp function in peewee seems to be broken) return [Activity(sel) for sel in Act.select().where( (Act.name.contains("EV,") | Act.name.contains("PHEV-")) # PHEV-d and PHEV-p & (Act.database == eidb.name))] bevs = find_evs() # any non-global activities found? non_glo = [act for act in bevs if act["location"] != "GLO"] if non_glo: print(("Found non-global EV activities: {}" "DB is most likely already updated.").format(non_glo)) ans = input("Delete existing non-GLO activities? (y/n)") if ans == "y": for act in non_glo: act.delete() bevs = find_evs() else: return for region in remind_regions: print("Relinking markets for {}".format(region)) # find markets new_market = Activity(Act.get( Act.name.startswith("market group for electricity, low voltage") & (Act.location == region) & (Act.database == eidb.name))) old_market_name = ("electricity market for fuel preparation, {}" .format(year)) for bev in bevs: new_bev = bev.copy() new_bev["location"] = region new_bev.save() exchanges = [ ex for ex in new_bev.technosphere() if ex["name"] == old_market_name] # should only be one if len(exchanges) > 1: raise ValueError("More than one electricity market for " "fuel production found for {}" .format(new_bev)) elif len(exchanges) == 1: # new exchange new_bev.new_exchange(**{ "name": new_market["name"], "amount": exchanges[0]["amount"], "unit": "kilowatt hour", "type": "technosphere", "location": region, "uncertainty type": 1, "reference product": "electricity, low voltage", "input": new_market.key }).save() exchanges[0].delete()