def calculate_combined_emissions_results(self): export_emissions = self.calc_and_format_export_emissions() embodied_emissions_list = self.calc_and_format_embodied_supply_emissions() direct_emissions_list = self.calc_and_format_direct_demand_emissions() export_emissions = util.add_and_set_index(export_emissions,['EMISSIONS_TYPE'],['EXPORTED']) embodied_emissions_list = [util.add_and_set_index(x, ['EMISSIONS_TYPE'], ['SUPPLY_SIDE']) for x in embodied_emissions_list] direct_emissions_list = [util.add_and_set_index(x,['EMISSIONS_TYPE'],['DEMAND_SIDE']) for x in direct_emissions_list] if export_emissions is not None: for name in [x for x in embodied_emissions_list[0].index.names if x not in export_emissions.index.names]: export_emissions[name] = "N/A" export_emissions.set_index(name,append=True,inplace=True) export_emissions = export_emissions.groupby(level=embodied_emissions_list[0].index.names).sum() if direct_emissions_list is not None: for df in direct_emissions_list: for name in [x for x in embodied_emissions_list[0].index.names if x not in df.index.names]: df[name] = "N/A" df.set_index(name,append=True,inplace=True) self.outputs.c_emissions = [export_emissions] + embodied_emissions_list + direct_emissions_list self.outputs.c_emissions = [util.replace_index_name(x, GeoMapper.combined_outputs_geography.upper() +'-EMITTED', GeoMapper.combined_outputs_geography.upper() +'_SUPPLY',inplace=True) for x in self.outputs.c_emissions] self.outputs.c_emissions = [util.replace_index_name(x, GeoMapper.combined_outputs_geography.upper() +'-CONSUMED', GeoMapper.combined_outputs_geography.upper(),inplace=True) for x in self.outputs.c_emissions] self.outputs.c_emissions = [x[x['VALUE']!=0] for x in self.outputs.c_emissions] emissions_unit = cfg.getParam('mass_unit') for x in self.outputs.c_emissions: x.columns = [emissions_unit.upper()] for x in self.outputs.c_emissions: x.index = x.index.reorder_levels([l for l in embodied_emissions_list[0].index.names if l in x.index.names])
def calculate_d_payback_energy(self): initial_vintage = min(cfg.supply_years) demand_side_df = self.demand.d_all_energy_demand_payback demand_side_df.columns = ['value'] demand_side_df = demand_side_df[demand_side_df.index.get_level_values('vintage')>=initial_vintage] demand_side_df = demand_side_df[demand_side_df.index.get_level_values('year')>=initial_vintage] sales_df = copy.deepcopy(self.demand.outputs.d_sales) util.replace_index_name(sales_df,'vintage','year') sales_df = sales_df[sales_df.index.get_level_values('vintage')>=initial_vintage] sales_df = util.add_and_set_index(sales_df,'year',cfg.supply_years) # sales_df.index = sales_df.index.reorder_levels(demand_side_df.index.names) # sales_df = sales_df.reindex(demand_side_df.index).sort_index() self.demand.outputs.d_payback_energy = util.DfOper.divi([demand_side_df, sales_df]) self.demand.outputs.d_payback_energy = self.demand.outputs.d_payback_energy[np.isfinite(self.demand.outputs.d_payback_energy.values)] self.demand.outputs.d_payback_energy = self.demand.outputs.d_payback_energy.replace([np.inf,np.nan],0) for sector in self.demand.sectors.values(): for subsector in sector.subsectors.values(): if hasattr(subsector,'stock') and subsector.sub_type!='link': indexer = util.level_specific_indexer(self.demand.outputs.d_payback_energy,'subsector',subsector.id) self.demand.outputs.d_payback_energy.loc[indexer,'unit'] = subsector.stock.unit.upper() self.demand.outputs.d_payback_energy = self.demand.outputs.d_payback_energy.set_index('unit', append=True) self.demand.outputs.d_payback_energy.columns = [cfg.calculation_energy_unit.upper()] self.demand.outputs.d_payback_energy['lifetime_year'] = self.demand.outputs.d_payback_energy.index.get_level_values('year')-self.demand.outputs.d_payback_energy.index.get_level_values('vintage')+1 self.demand.outputs.d_payback_energy = self.demand.outputs.d_payback_energy.set_index('lifetime_year',append=True) self.demand.outputs.d_payback_energy = util.remove_df_levels(self.demand.outputs.d_payback_energy,'year') self.demand.outputs.d_payback_energy = self.demand.outputs.d_payback_energy.groupby(level = [x for x in self.demand.outputs.d_payback_energy.index.names if x !='lifetime_year']).transform(lambda x: x.cumsum()) self.demand.outputs.d_payback_energy = self.demand.outputs.d_payback_energy[self.demand.outputs.d_payback_energy[cfg.calculation_energy_unit.upper()]!=0] self.demand.outputs.d_payback_energy = self.demand.outputs.return_cleaned_output('d_payback_energy')
def calc_and_format_export_energy(self): if self.supply.export_energy is None: return None export_energy = GeoMapper.geo_map(self.supply.export_energy.copy(), GeoMapper.supply_primary_geography, GeoMapper.combined_outputs_geography, 'total') export_energy = Output.clean_df(export_energy) util.replace_index_name(export_energy, 'FINAL_ENERGY','SUPPLY_NODE_EXPORT') export_energy = util.add_to_df_index(export_energy, names=['EXPORT/DOMESTIC', "ENERGY ACCOUNTING"], keys=["EXPORT", "EMBODIED"]) for x in cfg.output_combined_levels: if x not in export_energy.index.names: export_energy = util.add_and_set_index(export_energy,[x],["N/A"]) return export_energy
def calculate_combined_cost_results(self): cost_unit = cfg.getParam('currency_year') + " " + cfg.getParam('currency_name') export_costs = self.calc_and_format_export_costs() embodied_costs_list = self.calc_and_format_embodied_costs() direct_costs = self.calc_and_format_direct_demand_costs() export_costs = util.add_and_set_index(export_costs,['COST_TYPE'],['EXPORTED']) embodied_costs_list = [util.add_and_set_index(x,['COST_TYPE'],['SUPPLY-SIDE']) for x in embodied_costs_list] direct_costs = util.add_and_set_index(direct_costs,['COST_TYPE'],['DEMAND-SIDE']) if export_costs is not None: for name in [x for x in embodied_costs_list[0].index.names if x not in export_costs.index.names]: export_costs[name] = "N/A" export_costs.set_index(name,append=True,inplace=True) export_costs = export_costs.groupby(level=embodied_costs_list[0].index.names).sum() if direct_costs is not None: for name in [x for x in embodied_costs_list[0].index.names if x not in direct_costs.index.names]: direct_costs[name] = "N/A" direct_costs.set_index(name, append=True, inplace=True) direct_costs = direct_costs.groupby(level=embodied_costs_list[0].index.names).sum() self.outputs.c_costs = embodied_costs_list + [direct_costs] + [export_costs] self.outputs.c_costs= [x[x.values!=0] for x in self.outputs.c_costs] for x in self.outputs.c_costs: x.index = x.index.reorder_levels(embodied_costs_list[0].index.names)
def calculate(self, vintages, years): self.vintages = vintages self.years = years if self._has_data and self.raw_values is not None and (cfg.rio_supply_run is not True or 'vintage' in self.raw_values.index.names): self.remap(time_index_name='vintage', converted_geography=GeoMapper.supply_primary_geography,fill_value=np.nan) self.values.replace(0,1,inplace=True) util.convert_age(self, vintages=self.vintages, years=self.years, attr_from='values', attr_to='values', reverse=True) elif self._has_data and self.raw_values is not None and cfg.rio_supply_run==True: self.remap(time_index_name='year', converted_geography=GeoMapper.supply_primary_geography, fill_value=np.nan) self.values.replace(0, 1, inplace=True) self.values = util.add_and_set_index(self.values,'vintage',self.vintages,index_location=-1) self.values = self.values.squeeze().unstack(level='year')
def calculate_payback(self): # self.embodied_emissions_df = self.demand.outputs.return_cleaned_output('demand_embodied_emissions_tco') # del self.demand.outputs.demand_embodied_emissions #calculte and format direct demand emissions # self.direct_emissions_df = self.demand.outputs.return_cleaned_output('demand_direct_emissions') ## del self.demand.outputs.demand_direct_emissions # emissions = util.DfOper.add([self.embodied_emissions_df,self.direct_emissions_df]) # #calculate and format export costs cost_unit = cfg.cfgfile.get('case','currency_year_id') + " " + cfg.cfgfile.get('case','currency_name') initial_vintage = min(cfg.supply_years) supply_side_df = self.demand.outputs.demand_embodied_energy_costs_payback supply_side_df = supply_side_df[supply_side_df.index.get_level_values('vintage')>=initial_vintage] supply_side_df = supply_side_df[supply_side_df.index.get_level_values('year')>=initial_vintage] supply_side_df = supply_side_df.sort_index() demand_side_df = self.demand.d_annual_costs_payback demand_side_df.columns = ['value'] demand_side_df = demand_side_df[demand_side_df.index.get_level_values('vintage')>=initial_vintage] demand_side_df = demand_side_df[demand_side_df.index.get_level_values('year')>=initial_vintage] demand_side_df = demand_side_df.reindex(supply_side_df.index).sort_index() sales_df = copy.deepcopy(self.demand.outputs.d_sales) util.replace_index_name(sales_df,'vintage','year') sales_df = sales_df[sales_df.index.get_level_values('vintage')>=initial_vintage] sales_df = util.add_and_set_index(sales_df,'year',cfg.supply_years) sales_df.index = sales_df.index.reorder_levels(supply_side_df.index.names) sales_df = sales_df.reindex(supply_side_df.index).sort_index() keys = ['SUPPLY-SIDE', 'DEMAND-SIDE'] names = ['COST TYPE'] self.outputs.c_payback = pd.concat([util.DfOper.divi([supply_side_df, sales_df]), util.DfOper.divi([demand_side_df, sales_df])],keys=keys,names=names) self.outputs.c_payback = self.outputs.c_payback[np.isfinite(self.outputs.c_payback.values)] self.outputs.c_payback = self.outputs.c_payback.replace([np.inf,np.nan],0) for sector in self.demand.sectors.values(): for subsector in sector.subsectors.values(): if hasattr(subsector,'stock') and subsector.sub_type!='link': indexer = util.level_specific_indexer(self.outputs.c_payback,'subsector',subsector.id) self.outputs.c_payback.loc[indexer,'unit'] = subsector.stock.unit.upper() self.outputs.c_payback = self.outputs.c_payback.set_index('unit', append=True) self.outputs.c_payback.columns = [cost_unit.upper()] self.outputs.c_payback['lifetime_year'] = self.outputs.c_payback.index.get_level_values('year')-self.outputs.c_payback.index.get_level_values('vintage')+1 self.outputs.c_payback = self.outputs.c_payback.set_index('lifetime_year',append=True) self.outputs.c_payback = util.remove_df_levels(self.outputs.c_payback,'year') self.outputs.c_payback = self.outputs.c_payback.groupby(level = [x for x in self.outputs.c_payback.index.names if x !='lifetime_year']).transform(lambda x: x.cumsum()) self.outputs.c_payback = self.outputs.c_payback[self.outputs.c_payback[cost_unit.upper()]!=0] self.outputs.c_payback = self.outputs.return_cleaned_output('c_payback')