Skip to content

Commit

Permalink
Fix emissions reporting bug in DB_to_Excel.py.
Browse files Browse the repository at this point in the history
Temoa throws an error when trying to report emissions data
in the excel file when there are no emissions in the model
results. This commit patches the bug by first making sure
the there are emissions to report.
  • Loading branch information
SutubraResearch committed Aug 18, 2023
1 parent bbb11e1 commit 1b9442f
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions data_processing/DB_to_Excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from pyam import IamDataFrame

def make_excel(ifile, ofile, scenario):

if ifile is None :
raise "You did not specify the input file, remember to use '-i' option"
print("Use as :\n python DB_to_Excel.py -i <input_file> (Optional -o <output_excel_file_name_only>)\n Use -h for help.")
print("Use as :\n python DB_to_Excel.py -i <input_file> (Optional -o <output_excel_file_name_only>)\n Use -h for help.")
sys.exit(2)
else :
file_type = re.search(r"(\w+)\.(\w+)\b", ifile) # Extract the input filename and extension
Expand Down Expand Up @@ -75,18 +75,19 @@ def make_excel(ifile, ofile, scenario):
query = "SELECT regions, tech, sector, t_periods, emissions_comm, sum(emissions) as emissions FROM Output_Emissions WHERE scenario='" + scenario + "' GROUP BY \
regions, tech, sector, t_periods, emissions_comm"
df_emissions_raw = pd.read_sql_query(query, con)
df_emissions = df_emissions_raw.pivot_table(values='emissions', index=['regions', 'tech', 'sector','emissions_comm'], columns='t_periods')
df_emissions.reset_index(inplace=True)
df_emissions = pd.merge(all_emis_techs, df_emissions, on=['regions','tech', 'sector', 'emissions_comm'], how='left')
df_emissions.rename(columns={'regions':'Region', 'tech':'Technology', 'emissions_comm':'Emission Commodity', 'sector':'Sector'}, inplace=True)
df_emissions.to_excel(writer, sheet_name='Emissions', index=False, encoding='utf-8', startrow=1, header=False)
worksheet = writer.sheets['Emissions']
worksheet.set_column('A:A', 10)
worksheet.set_column('B:B', 10)
worksheet.set_column('C:C', 10)
worksheet.set_column('D:D', 20)
for col, val in enumerate(df_emissions.columns.values):
worksheet.write(0, col, val, header_format)
if not df_emissions_raw.empty:
df_emissions = df_emissions_raw.pivot_table(values='emissions', index=['regions', 'tech', 'sector','emissions_comm'], columns='t_periods')
df_emissions.reset_index(inplace=True)
df_emissions = pd.merge(all_emis_techs, df_emissions, on=['regions','tech', 'sector', 'emissions_comm'], how='left')
df_emissions.rename(columns={'regions':'Region', 'tech':'Technology', 'emissions_comm':'Emission Commodity', 'sector':'Sector'}, inplace=True)
df_emissions.to_excel(writer, sheet_name='Emissions', index=False, encoding='utf-8', startrow=1, header=False)
worksheet = writer.sheets['Emissions']
worksheet.set_column('A:A', 10)
worksheet.set_column('B:B', 10)
worksheet.set_column('C:C', 10)
worksheet.set_column('D:D', 20)
for col, val in enumerate(df_emissions.columns.values):
worksheet.write(0, col, val, header_format)

query = "SELECT regions, tech, sector, output_name, vintage, output_cost FROM Output_Costs WHERE output_name LIKE '%V_Discounted%' AND scenario='" + scenario + "'"
df_costs = pd.read_sql_query(query, con)
Expand Down Expand Up @@ -118,7 +119,7 @@ def make_excel(ifile, ofile, scenario):
df_activity['variable']='Activity|' + df_activity['sector'] + '|' + df_activity['tech']
df_activity.rename(columns={'t_periods':'year', 'vflow_out':'value', 'regions':'region'}, inplace=True)


# cast results to IamDataFrame and write to xlsx
columns = ['scenario', 'region', 'variable', 'year', 'value', 'unit']
_results = pd.concat([df_emissions_raw[columns], df_activity[columns], df_capacity[columns]])
Expand Down Expand Up @@ -147,10 +148,10 @@ def get_data(inputs):
ifile = None
ofile = None
scenario = set()

if inputs is None:
raise "no arguments found"

for opt, arg in inputs.items():
if opt in ("-i", "--input"):
ifile = arg
Expand All @@ -159,20 +160,20 @@ def get_data(inputs):
elif opt in ("-s", "--scenario"):
scenario.add(arg)
elif opt in ("-h", "--help") :
print("Use as :\n python DB_to_Excel.py -i <input_file> (Optional -o <output_excel_file_name_only>)\n Use -h for help.")
print("Use as :\n python DB_to_Excel.py -i <input_file> (Optional -o <output_excel_file_name_only>)\n Use -h for help.")
sys.exit()

make_excel(ifile, ofile, scenario)

if __name__ == "__main__":
if __name__ == "__main__":

try:
argv = sys.argv[1:]
opts, args = getopt.getopt(argv, "hi:o:s:", ["help", "input=", "output=", "scenario="])
except getopt.GetoptError:
print("Something's Wrong. Use as :\n python DB_to_Excel.py -i <input_file> (Optional -o <output_excel_file_name_only>)\n Use -h for help.")
sys.exit(2)
except getopt.GetoptError:
print("Something's Wrong. Use as :\n python DB_to_Excel.py -i <input_file> (Optional -o <output_excel_file_name_only>)\n Use -h for help.")
sys.exit(2)

print(opts)
get_data( dict(opts) )

get_data( dict(opts) )

0 comments on commit 1b9442f

Please sign in to comment.