%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import os
from os.path import join
import sys
import json
idx = pd.IndexSlice

"""
Explanation: Calculate NERC level results
Use the NERC extra generation and fuel consumption (things not yet reported in EIA-923 final data) from the Calculate national and NERC gen and emissions notebook, along with facility EIA and EPA data to calculate generation and emissions in each NERC region.
Instructions
Make sure the file_date parameter below is set to whatever value you would like appended to file names.
The entire notebook can be run at once using Run All Cells
End of explanation
"""


file_date = '2018-03-06'

%load_ext watermark

%watermark -v -iv

# Load the "autoreload" extension
%load_ext autoreload

# always reload modules marked with "%aimport"
%autoreload 1

# add the 'src' directory as one where we can import modules
src_dir = join(os.getcwd(), os.pardir, 'src')
sys.path.append(src_dir)

%aimport Data.make_data
from Data.make_data import states_in_nerc
%aimport Analysis.index
from Analysis.index import facility_emission_gen, group_facility_data
%aimport Analysis.index
from Analysis.index import facility_co2, adjust_epa_emissions, group_fuel_cats
from Analysis.index import reduce_emission_factors, add_datetime
%aimport util.utils
from util.utils import rename_cols, add_facility_location

"""
Explanation: Date string for filenames
This will be inserted into all filenames (reading and writing)
End of explanation
"""


cwd = os.getcwd()
path = join(cwd, '..', 'Data storage', 'Derived data',
            'NERC extra gen fuels {}.csv'.format(file_date))
extra_nerc = pd.read_csv(path, index_col=[0, 1, 2, 3])

"""
Explanation: Extra NERC emissions and generation
Load extra NERC generation and fuel consumption
End of explanation
"""


path = join(cwd, '..', 'Data storage', 'Final emission factors.csv')
ef = pd.read_csv(path, index_col=0)

ef_type = reduce_emission_factors(ef)

ef_type = pd.Series(ef_type, name='type')

"""
Explanation: Load emission factors
End of explanation
"""


extra_nerc.loc[:, 'total co2 (kg)'] = (extra_nerc
                                       .loc[:, 'total fuel (mmbtu)']
                                       .multiply(ef_type, 'type'))
extra_nerc.loc[:, 'elec co2 (kg)'] = (extra_nerc
                                       .loc[:, 'elec fuel (mmbtu)']
                                       .multiply(ef_type, 'type'))

extra_nerc.sort_index(inplace=True)

"""
Explanation: Calculate CO₂ emissions
End of explanation
"""


%aimport Analysis.state2nerc
from Analysis.state2nerc import fraction_state2nerc, add_region

fuel_cat_folder = join(cwd, '..', 'Data storage', 'Fuel categories')
state_cats_path = join(fuel_cat_folder, 'State_facility.json')

with open(state_cats_path, 'r') as f:
    state_fuel_cat = json.load(f)
    
custom_cats_path = join(fuel_cat_folder, 'Custom_results.json')
with open(custom_cats_path, 'r') as f:
    custom_fuel_cat = json.load(f)

path = join(cwd, '..', 'Data storage', 'Derived data',
            'Monthly EPA emissions {}.csv'.format(file_date))
epa = pd.read_csv(path)

path = join(cwd, '..', 'Data storage', 'Facility labels',
            'Facility locations_RF.csv')
facility_labels = pd.read_csv(path)

path = join(cwd, '..', 'Data storage', 'Derived data',
            'Facility gen fuels and CO2 {}.csv'.format(file_date))
eia_fac = pd.read_csv(path)

co2, gen_fuels_state = facility_emission_gen(eia_facility=eia_fac, epa=epa,
                                              state_fuel_cat=state_fuel_cat,
                                              custom_fuel_cat=custom_fuel_cat,
                                              export_state_cats=True)

"""
Explanation: Facilities by NERC
End of explanation
"""


co2 = add_facility_location(co2, facility_labels,
                            labels=['lat', 'lon', 'state', 'nerc', 'year'])

co2_nerc = co2.groupby(['year', 'nerc', 'month'])['final co2 (kg)'].sum()

"""
Explanation: Monthly CO₂ emissions
End of explanation
"""


gen_fuels_nerc = add_facility_location(gen_fuels_state,
                                       facility_labels, labels=['nerc', 'year'])
gen_fuels_nerc = (gen_fuels_nerc
                  .groupby(['year', 'nerc', 'month', 'type'])
                  ['generation (mwh)'].sum())

"""
Explanation: Monthly generation
End of explanation
"""


total_gen = gen_fuels_nerc.copy()

total_gen.loc[idx[2016:, :, :, :]] = (total_gen.loc[2016:] 
                                     .add(extra_nerc.loc[:, 'generation (mwh)']
                                          , fill_value=0))
total_gen = total_gen.reset_index()
add_datetime(total_gen)

final = group_fuel_cats(total_gen,
                        custom_fuel_cat, 'type',
                        'fuel category', extra_group_cols=['nerc', 'datetime'])
final.set_index(['nerc', 'fuel category', 'datetime'], inplace=True)

total = final.groupby(['nerc', 'datetime']).sum()

nercs = total.index.get_level_values('nerc').unique()

"""
Explanation: Combine generation
End of explanation
"""


df_list = []
for nerc in nercs:
    percent_gen = final.loc[nerc].divide(total.loc[nerc], level='datetime')
    percent_gen['nerc'] = nerc
    percent_gen.set_index('nerc', append=True, inplace=True)
    df_list.append(percent_gen)
    
percent_gen = pd.concat(df_list)
percent_gen.drop(['year', 'month'], axis=1, inplace=True)
percent_gen.columns = ['% generation']

path = join(cwd, '..', 'Data storage', 'Final NERC data',
            'NERC percent gen {}.csv'.format(file_date))
percent_gen.to_csv(path)

total_monthly_gen = final.groupby(['fuel category', 'year', 'nerc', 'month']).sum()
total_monthly_gen.sort_index(inplace=True)

path = join(cwd, '..', 'Data storage', 'Final NERC data',
            'NERC generation {}.csv'.format(file_date))
total_monthly_gen.to_csv(path)

"""
Explanation: Percent generation by fuel
End of explanation
"""


total_monthly_gen = total_monthly_gen.groupby(['year', 'nerc', 'month']).sum()

nerc_index = pd.concat([co2_nerc.sort_index(), total_monthly_gen.sort_index()], axis=1)
nerc_index['index'] = nerc_index['final co2 (kg)'] / nerc_index['generation (mwh)']
nerc_index = nerc_index.reset_index()
add_datetime(nerc_index)

"""
Explanation: CO₂ emissions intensity by NERC region
End of explanation
"""


path = join(cwd, '..', 'Data storage', 'Final NERC data',
            'NERC gen emissions and index {}.csv'.format(file_date))
nerc_index.to_csv(path, index=False)

"""
Explanation: Write data to file
End of explanation
"""


