diff --git a/workflow/scripts/osemosys_global/powerplant/activity.py b/workflow/scripts/osemosys_global/powerplant/activity.py index 05be7c75..82289aa4 100644 --- a/workflow/scripts/osemosys_global/powerplant/activity.py +++ b/workflow/scripts/osemosys_global/powerplant/activity.py @@ -58,7 +58,8 @@ def activity_output_pwr(df_ratios, thermal_fuel_list_oar): df_oar = df_ratios.copy() mask = df_oar['TECHNOLOGY'].apply(lambda x: x[3:6] in thermal_fuel_list_oar) df_oar['FUEL'] = 0 - df_oar['FUEL'][mask] = 1 + df_oar.loc[mask, "FUEL"] = 1 + df_oar = df_oar.loc[~((df_oar['MODE_OF_OPERATION'] > 1) & (df_oar['FUEL'] == 0))] df_oar['FUEL'] = ('ELC' + @@ -88,6 +89,7 @@ def activity_input_pwr(df_oar, thermal_fuel_list_iar, renewables_list, df_iar = df_oar.copy() df_iar['FUEL'] = 0 + df_iar['FUEL'] = df_iar['FUEL'].astype(str) # Deal with GAS techs first... OCG and CCG # OCG Mode 1: Domestic GAS diff --git a/workflow/scripts/osemosys_global/powerplant/costs.py b/workflow/scripts/osemosys_global/powerplant/costs.py index cf9426dd..7b22d23c 100644 --- a/workflow/scripts/osemosys_global/powerplant/costs.py +++ b/workflow/scripts/osemosys_global/powerplant/costs.py @@ -63,7 +63,7 @@ def costs_end(df_weo_regions, df_costs, df_oar_final, df_trans_capex, # Create formatted CSVs for each_cost in ['Capital', 'O&M']: - df_costs_temp = df_costs.loc[df_costs['parameter'].str.contains(each_cost)] + df_costs_temp = df_costs.copy().loc[df_costs.copy()['parameter'].str.contains(each_cost)] df_costs_temp.drop(['technology', 'parameter'], axis = 1, @@ -72,7 +72,8 @@ def costs_end(df_weo_regions, df_costs, df_oar_final, df_trans_capex, 'TECHNOLOGY', 'YEAR' ]] - df_costs_final['YEAR'] = df_costs_final['YEAR'].astype(int) + df_costs_final.loc[:,'YEAR'] = df_costs_final['YEAR'].astype(int) + df_costs_final = df_costs_final.drop_duplicates() df_costs_final = (df_costs_final .loc[(df_costs_final['TECHNOLOGY'] @@ -96,17 +97,21 @@ def costs_end(df_weo_regions, df_costs, df_oar_final, df_trans_capex, df_costs_final.drop(['technology_code', 'weo_region'], axis = 1, inplace = True) - df_costs_final = df_costs_final.fillna(-9) + df_costs_final = df_costs_final.infer_objects().fillna(-9) df_costs_final = pd.pivot_table(df_costs_final, index = ['REGION', 'YEAR'], columns = 'TECHNOLOGY', values = 'value').reset_index() df_costs_final = df_costs_final.replace([-9],[np.nan]) - df_costs_final = df_costs_final.interpolate(method = 'linear', - limit_direction='forward').round(2) - df_costs_final = df_costs_final.interpolate(method = 'linear', - limit_direction='backward').round(2) + for col in df_costs_final.columns: + if df_costs_final[col].dtype != 'object': + df_costs_final[col] = df_costs_final[col].interpolate(method = 'linear', + limit_direction='forward').round(2) + + df_costs_final[col] = df_costs_final[col].interpolate(method = 'linear', + limit_direction='backward').round(2) + df_costs_final = pd.melt(df_costs_final, id_vars = ['REGION', 'YEAR'], value_vars = [x for x in df_costs_final.columns diff --git a/workflow/scripts/osemosys_global/powerplant/data.py b/workflow/scripts/osemosys_global/powerplant/data.py index d7eb94c3..18d2fe0b 100644 --- a/workflow/scripts/osemosys_global/powerplant/data.py +++ b/workflow/scripts/osemosys_global/powerplant/data.py @@ -1,7 +1,6 @@ """Functions to extract relevent data""" import pandas as pd -import numpy as np from datetime import datetime import itertools import logging @@ -30,7 +29,7 @@ def set_generator_table(plexos_prop: pd.DataFrame, plexos_memb: pd.DataFrame, index="powerplant", columns="property", values="value", - aggfunc=np.sum, + aggfunc='sum', fill_value=0, ) df_gen["total_capacity"] = (df_gen["Max Capacity"].astype(float)) * ( @@ -59,9 +58,10 @@ def set_generator_table(plexos_prop: pd.DataFrame, plexos_memb: pd.DataFrame, ) ## Extract start year from Commission Date - df_gen_base["Commission Date"] = (pd.TimedeltaIndex(df_gen_base["Commission Date"].astype(int), + df_gen_base["Commission Date"] = (pd.to_timedelta(df_gen_base["Commission Date"].astype(int), unit='d') + datetime(1900, 1, 1)) + df_gen_base["start_year"] = df_gen_base["Commission Date"].dt.year df_gen_base.drop("Commission Date", axis=1, inplace=True) @@ -329,5 +329,6 @@ def newIar(df_in, tech, new_iar_ccg, else: logging.warning(f'Default IAR used for new {tech} power plants') iar = new_iar_default - df_out['VALUE'] = round(1/iar, 3) + df_out.loc[:,'VALUE'] = round(1/iar, 3) + return df_out \ No newline at end of file diff --git a/workflow/scripts/osemosys_global/powerplant/main.py b/workflow/scripts/osemosys_global/powerplant/main.py index 295ecfce..843d7ca3 100644 --- a/workflow/scripts/osemosys_global/powerplant/main.py +++ b/workflow/scripts/osemosys_global/powerplant/main.py @@ -104,7 +104,7 @@ def main( # return generator_table gen_table = set_generator_table(plexos_prop, plexos_memb, op_life_dict, tech_code_dict) - + # Calculate average technology efficiencies. df_eff_node, df_eff_tech = average_efficiency(gen_table, avg_csp_eff, avg_urn_eff) @@ -112,7 +112,7 @@ def main( # Calculate residual capacity. df_res_cap, custom_techs = res_capacity(gen_table, tech_list, tech_code, custom_res_cap, duplicate_techs) - + df_ratios = activity_master_start(gen_table, nodes_extra_list, duplicate_techs, mode_list) @@ -123,7 +123,7 @@ def main( df_oar_upstream, df_oar_int = activity_upstream(df_iar_base, renewables_list, thermal_fuel_list_mining) - + df_iar_trn, df_oar_trn, df_int_trn_oar, df_int_trn_iar = activity_transmission(df_oar_base, plexos_prop, interface_data) @@ -142,7 +142,7 @@ def main( df_capact_final = capact(df_oar_final) df_crossborder_final = activity_transmission_limit(cross_border_trade, df_oar_final) - + df_op_life_trn, df_op_life_out = set_op_life(tech_code_dict, df_iar_final, df_oar_final, op_life_dict) @@ -191,7 +191,7 @@ def main( ) df_af_final = availability_factor(availability, tech_set) - + # OUTPUT CSV's df_res_cap.to_csv(os.path.join(output_data_dir, "ResidualCapacity.csv"), index=None) diff --git a/workflow/scripts/osemosys_global/powerplant/residual_capacity.py b/workflow/scripts/osemosys_global/powerplant/residual_capacity.py index 686df101..32671c1f 100644 --- a/workflow/scripts/osemosys_global/powerplant/residual_capacity.py +++ b/workflow/scripts/osemosys_global/powerplant/residual_capacity.py @@ -27,9 +27,8 @@ def res_capacity(df_gen_base, tech_list, df_tech_code, df_custom_res_cap, duplic ] df_res_cap = df_gen_base[res_cap_cols] - - for each_year in range(start_year, end_year+1): - df_res_cap[str(each_year)] = 0 + + df_res_cap = df_res_cap.reindex(columns=[*df_res_cap.columns.tolist(), *list(range(start_year, end_year+1))], fill_value=0) df_res_cap = pd.melt( df_res_cap, @@ -38,7 +37,10 @@ def res_capacity(df_gen_base, tech_list, df_tech_code, df_custom_res_cap, duplic var_name="model_year", value_name="value", ) + df_res_cap["model_year"] = df_res_cap["model_year"].astype(int) + df_res_cap["value"] = df_res_cap["value"].astype(float) + df_res_cap.loc[ (df_res_cap["model_year"] >= df_res_cap["start_year"]) & (df_res_cap["model_year"] <= df_res_cap["retirement_year_model"]), diff --git a/workflow/scripts/osemosys_global/powerplant/user_defined_capacity.py b/workflow/scripts/osemosys_global/powerplant/user_defined_capacity.py index beefa622..b2eb8a48 100644 --- a/workflow/scripts/osemosys_global/powerplant/user_defined_capacity.py +++ b/workflow/scripts/osemosys_global/powerplant/user_defined_capacity.py @@ -72,7 +72,7 @@ def set_user_defined_capacity(tech_capacity, op_life_dict, df_tech_set, df.loc[(df['YEAR']>=df['FIRST_YEAR']) & (df['YEAR']>df['BUILD_YEAR']), 'VALUE'] = df['MAX_BUILD'] - df.fillna(0, + df.infer_objects().fillna(0, inplace=True) max_cap_techs_df = df[['REGION', 'TECHNOLOGY', @@ -93,7 +93,7 @@ def set_user_defined_capacity(tech_capacity, op_life_dict, df_tech_set, inplace=True) # For technologies with start year before model start year, add to ResidualCapacity - df_res_cap_ud = df_min_cap_inv.loc[df_min_cap_inv['YEAR'] < min(years)] + df_res_cap_ud = df_min_cap_inv.copy().loc[df_min_cap_inv.copy()['YEAR'] < min(years)] df_res_cap_ud.rename(columns={'YEAR':'START_YEAR'}, inplace=True) df_res_cap_ud_final = pd.DataFrame(list(itertools.product(df_res_cap_ud['TECHNOLOGY'].unique(), @@ -122,7 +122,9 @@ def set_user_defined_capacity(tech_capacity, op_life_dict, df_tech_set, 'YEAR', 'VALUE']] - df_res_cap = pd.concat([df_res_cap, df_res_cap_ud_final]) + + df_res_cap = pd.concat([df_res_cap, df_res_cap_ud_final + if not df_res_cap_ud_final.empty else None]) # For technologies with start year at or after model start year, add to # TotalAnnualMinCapacityInvestment