# -*- coding: utf-8 -*-

"""Add docstring."""


import pandas as pd
from electricitylci.coal_upstream import read_eia923_fuel_receipts
from os.path import join
from electricitylci.globals import data_dir, output_dir
import electricitylci.PhysicalQuantities as pq
import electricitylci.eia923_generation as eia923
import logging

module_logger=logging.getLogger("petroleum_upstream.py")


def generate_petroleum_upstream(year):
    """
    Generate annual.
    
    Petroleum extraction, transport, and refining emissions (in kg) for each
    plant in EIA923.

    Parameters
    ----------
    year: int
        Year of EIA-923 fuel data to use.

    Returns
    ----------
    dataframe
    """
    eia_fuel_receipts_df=read_eia923_fuel_receipts(year)
    petroleum_criteria = (
        (eia_fuel_receipts_df['fuel_group']=='Petroleum')
        | (eia_fuel_receipts_df['fuel_group']=="Petroleum Coke")
    )
    eia_fuel_receipts_df=eia_fuel_receipts_df.loc[petroleum_criteria,:]

    eia_fuel_receipts_df['heat_input']=(
            eia_fuel_receipts_df['quantity']*
            eia_fuel_receipts_df['average_heat_content']*
            pq.convert(10**6,'Btu','MJ'))

    eia_gen_fuel=eia923.eia923_generation_and_fuel(year)
    petroleum_criteria=eia_gen_fuel["reported_fuel_type_code"].isin(["DFO","RFO","PC"])
    petroleum_fuel=eia_gen_fuel.loc[petroleum_criteria,:]
    #replacing PC with RFO as a proxy for Coke. In the future
    #we should add a profile for coke.
    petroleum_fuel.loc[petroleum_fuel["reported_fuel_type_code"]=="PC","reported_fuel_type_code"]="RFO"
    # Sum all fuel use by plant, plant state, and DFO/RFO
    eia_fuel_receipts_df=eia_fuel_receipts_df.groupby(
            ['plant_id','plant_state','energy_source'],
            as_index=False)['heat_input'].sum()
    petroleum_fuel=petroleum_fuel[["plant_id","state","total_fuel_consumption_mmbtu","reported_fuel_type_code"]]
    petroleum_fuel["heat_input"]=petroleum_fuel["total_fuel_consumption_mmbtu"]*pq.convert(10**6,'Btu','MJ')
    petroleum_fuel["plant_id"]=petroleum_fuel["plant_id"].astype(int)
    # Assume that plants have fuel delivered from the PADD they are in. Note
    # that the crude represented in each PADD is the mix of crude going into
    # that PADD, domestically-produced and imported, and the refining emissions
    # are representative of the mix of refinery types in that PADD.
    state_padd_df=pd.read_csv(data_dir+'/state_padd.csv',low_memory=False)
    state_padd_dict=pd.Series(
            state_padd_df.padd.values,index=state_padd_df.state).to_dict()

    # Assign each power plant to a PADD.
    petroleum_fuel['padd']=(
            petroleum_fuel['state'].map(state_padd_dict))

    # Creating a dictionary to store the inventories of each fuel/PADD
    # combination
    petroleum_lci={}
    fuels=['Diesel','Bunker']
    fuels_map={'Diesel':'DFO',
               'Bunker':'RFO'
            }
    padds=[1,2,3,4,5]
    expected_lci_folder = 'petroleum_inventory'
    for fuel in fuels:
        for padd in padds:
            fn = f'PRELIM_Mixer__{fuel}___PADD_{padd}_.xlsx'
            module_logger.info(f"Reading petroleum inventory: {fn}")
            path = join(data_dir,expected_lci_folder,fn)
            key = f'{fuels_map[fuel]}_{padd}'
            petrol_excel=pd.ExcelFile(path)
            petroleum_lci_emissions=petrol_excel.parse(
                    sheet_name='Inventory',
                    usecols="I:N",
#                    header=0,
#                    mangle_dupe_cols=False,
                    skiprows=2)
#            petroleum_lci_emissions=pd.read_excel(
#                    path,
#                    sheet_name='Inventory',
#                    usecols="I:N",
#                    header=0,
#                    mangle_dupe_cols=False,
#                    skiprows=2)
            petroleum_lci_emissions.loc[petroleum_lci_emissions["Category.1"]=="NETL database","Category.1"]="NETL database/emissions"
            petroleum_lci_emissions.rename(columns={
                "Flow UUID.1":"Flow UUID",
                "Flow.1":"Flow",
                "Category.1":"Category",
                "Sub-category.1":"Sub-category",
                "Unit.1":"Unit",
                "Result.1":"Result"
                },
                inplace=True)
            petroleum_lci_emissions["input"]=False
            petroleum_lci_input=petrol_excel.parse(
                    sheet_name='Inventory',
                    usecols="B:G",
#                    header=1,
#                    mangle_dupe_cols=False,
                    skiprows=2)
            petroleum_lci_input["input"]=True
            petroleum_lci_input.loc[petroleum_lci_input["Category"]=="NETL database","Category"]="NETL database/resources"
            total_petroleum=pd.concat([petroleum_lci_emissions,petroleum_lci_input],ignore_index=True)
            petroleum_lci[key]=total_petroleum
            petroleum_lci[key]['fuel_code']=f'{fuels_map[fuel]}_{padd}'

    # Merging the dataframes within the dictionary to a single datframe
    combined_lci=pd.concat(petroleum_lci,ignore_index=True)

    petroleum_fuel['fuel_padd']=(petroleum_fuel['reported_fuel_type_code']+
                        '_'+petroleum_fuel['padd'].astype(str))

    # Merge the inventories for each fuel with the fuel use by each power plant
    merged_inventory = combined_lci.merge(
            right=petroleum_fuel[['plant_id','heat_input','fuel_padd']],
            left_on='fuel_code',
            right_on='fuel_padd',
            how='left').sort_values(['plant_id','fuel_padd','Flow'])

    # convert per MJ inventory to annual emissions using plant heat input
    merged_inventory['Result']=(
            merged_inventory['Result']*
            merged_inventory['heat_input'])

    # Cleaning up unneeded columns and renaming
    merged_inventory.drop(
            columns=['fuel_padd',
                     'Sub-category','Flow UUID'],
            inplace=True)
    colnames={
            'Flow':'FlowName',
            'Category':'Compartment',
            'Result':'FlowAmount',
            'fuel_code':'stage_code',
            'heat_input':'quantity'}
    merged_inventory.rename(columns=colnames,inplace=True)
    merged_inventory['fuel_type']='Oil'
    merged_inventory['stage']='well-to-tank'
    merged_inventory.reset_index(inplace=True,drop=True)

    # Change compartment values to be standard'
    compartment_dict={
            'Emission to air':'air',
            "NETL database/emissions":"NETL database/emissions",
            "NETL database/resources":"NETL database/resources",
            'Emission to water':'water',
            'Emission to soil':'soil'}
    merged_inventory['Compartment']=merged_inventory['Compartment'].map(
            compartment_dict)
    merged_inventory.dropna(inplace=True)

    return merged_inventory

if __name__=='__main__':
    year=2016
    df=generate_petroleum_upstream(year)
    df.to_csv(output_dir+'/petroleum_emissions_{}.csv'.format(year))

# -*- coding: utf-8 -*-

"""Add docstring."""


import pandas as pd
from electricitylci.coal_upstream import read_eia923_fuel_receipts
from os.path import join
from electricitylci.globals import data_dir, output_dir
import electricitylci.PhysicalQuantities as pq
import electricitylci.eia923_generation as eia923
import logging

module_logger=logging.getLogger("petroleum_upstream.py")


def generate_petroleum_upstream(year):
    """
    Generate annual.
    
    Petroleum extraction, transport, and refining emissions (in kg) for each
    plant in EIA923.

    Parameters
    ----------
    year: int
        Year of EIA-923 fuel data to use.

    Returns
    ----------
    dataframe
    """
    eia_fuel_receipts_df=read_eia923_fuel_receipts(year)
    petroleum_criteria = (
        (eia_fuel_receipts_df['fuel_group']=='Petroleum')
        | (eia_fuel_receipts_df['fuel_group']=="Petroleum Coke")
    )
    eia_fuel_receipts_df=eia_fuel_receipts_df.loc[petroleum_criteria,:]

    eia_fuel_receipts_df['heat_input']=(
            eia_fuel_receipts_df['quantity']*
            eia_fuel_receipts_df['average_heat_content']*
            pq.convert(10**6,'Btu','MJ'))

    eia_gen_fuel=eia923.eia923_generation_and_fuel(year)
    petroleum_criteria=eia_gen_fuel["reported_fuel_type_code"].isin(["DFO","RFO","PC"])
    petroleum_fuel=eia_gen_fuel.loc[petroleum_criteria,:]
    #replacing PC with RFO as a proxy for Coke. In the future
    #we should add a profile for coke.
    petroleum_fuel.loc[petroleum_fuel["reported_fuel_type_code"]=="PC","reported_fuel_type_code"]="RFO"
    # Sum all fuel use by plant, plant state, and DFO/RFO
    eia_fuel_receipts_df=eia_fuel_receipts_df.groupby(
            ['plant_id','plant_state','energy_source'],
            as_index=False)['heat_input'].sum()
    petroleum_fuel=petroleum_fuel[["plant_id","state","total_fuel_consumption_mmbtu","reported_fuel_type_code"]]
    petroleum_fuel["heat_input"]=petroleum_fuel["total_fuel_consumption_mmbtu"]*pq.convert(10**6,'Btu','MJ')
    petroleum_fuel["plant_id"]=petroleum_fuel["plant_id"].astype(int)
    # Assume that plants have fuel delivered from the PADD they are in. Note
    # that the crude represented in each PADD is the mix of crude going into
    # that PADD, domestically-produced and imported, and the refining emissions
    # are representative of the mix of refinery types in that PADD.
    state_padd_df=pd.read_csv(data_dir+'/state_padd.csv',low_memory=False)
    state_padd_dict=pd.Series(
            state_padd_df.padd.values,index=state_padd_df.state).to_dict()

    # Assign each power plant to a PADD.
    petroleum_fuel['padd']=(
            petroleum_fuel['state'].map(state_padd_dict))

    # Creating a dictionary to store the inventories of each fuel/PADD
    # combination
    petroleum_lci={}
    fuels=['Diesel','Bunker']
    fuels_map={'Diesel':'DFO',
               'Bunker':'RFO'
            }
    padds=[1,2,3,4,5]
    expected_lci_folder = 'petroleum_inventory'
    for fuel in fuels:
        for padd in padds:
            fn = f'PRELIM_Mixer__{fuel}___PADD_{padd}_.xlsx'
            module_logger.info(f"Reading petroleum inventory: {fn}")
            path = join(data_dir,expected_lci_folder,fn)
            key = f'{fuels_map[fuel]}_{padd}'
            petrol_excel=pd.ExcelFile(path)
            petroleum_lci_emissions=petrol_excel.parse(
                    sheet_name='Inventory',
                    usecols="I:N",
#                    header=0,
#                    mangle_dupe_cols=False,
                    skiprows=2)
#            petroleum_lci_emissions=pd.read_excel(
#                    path,
#                    sheet_name='Inventory',
#                    usecols="I:N",
#                    header=0,
#                    mangle_dupe_cols=False,
#                    skiprows=2)
            petroleum_lci_emissions.loc[petroleum_lci_emissions["Category.1"]=="NETL database","Category.1"]="NETL database/emissions"
            petroleum_lci_emissions.rename(columns={
                "Flow UUID.1":"Flow UUID",
                "Flow.1":"Flow",
                "Category.1":"Category",
                "Sub-category.1":"Sub-category",
                "Unit.1":"Unit",
                "Result.1":"Result"
                },
                inplace=True)
            petroleum_lci_emissions["input"]=False
            petroleum_lci_input=petrol_excel.parse(
                    sheet_name='Inventory',
                    usecols="B:G",
#                    header=1,
#                    mangle_dupe_cols=False,
                    skiprows=2)
            petroleum_lci_input["input"]=True
            petroleum_lci_input.loc[petroleum_lci_input["Category"]=="NETL database","Category"]="NETL database/resources"
            total_petroleum=pd.concat([petroleum_lci_emissions,petroleum_lci_input],ignore_index=True)
            petroleum_lci[key]=total_petroleum
            petroleum_lci[key]['fuel_code']=f'{fuels_map[fuel]}_{padd}'

    # Merging the dataframes within the dictionary to a single datframe
    combined_lci=pd.concat(petroleum_lci,ignore_index=True)

    petroleum_fuel['fuel_padd']=(petroleum_fuel['reported_fuel_type_code']+
                        '_'+petroleum_fuel['padd'].astype(str))

    # Merge the inventories for each fuel with the fuel use by each power plant
    merged_inventory = combined_lci.merge(
            right=petroleum_fuel[['plant_id','heat_input','fuel_padd']],
            left_on='fuel_code',
            right_on='fuel_padd',
            how='left').sort_values(['plant_id','fuel_padd','Flow'])

    # convert per MJ inventory to annual emissions using plant heat input
    merged_inventory['Result']=(
            merged_inventory['Result']*
            merged_inventory['heat_input'])

    # Cleaning up unneeded columns and renaming
    merged_inventory.drop(
            columns=['fuel_padd',
                     'Sub-category','Flow UUID'],
            inplace=True)
    colnames={
            'Flow':'FlowName',
            'Category':'Compartment',
            'Result':'FlowAmount',
            'fuel_code':'stage_code',
            'heat_input':'quantity'}
    merged_inventory.rename(columns=colnames,inplace=True)
    merged_inventory['fuel_type']='Oil'
    merged_inventory['stage']='well-to-tank'
    merged_inventory.reset_index(inplace=True,drop=True)

    # Change compartment values to be standard'
    compartment_dict={
            'Emission to air':'air',
            "NETL database/emissions":"NETL database/emissions",
            "NETL database/resources":"NETL database/resources",
            'Emission to water':'water',
            'Emission to soil':'soil'}
    merged_inventory['Compartment']=merged_inventory['Compartment'].map(
            compartment_dict)
    merged_inventory.dropna(inplace=True)

    return merged_inventory

if __name__=='__main__':
    year=2016
    df=generate_petroleum_upstream(year)
    df.to_csv(output_dir+'/petroleum_emissions_{}.csv'.format(year))

# -*- coding: utf-8 -*- Add docstring. Generate annual.          Petroleum extraction, transport, and refining emissions (in kg) for each     plant in EIA923.      Parameters     ----------     year: int         Year of EIA-923 fuel data to use.      Returns     ----------     dataframe #replacing PC with RFO as a proxy for Coke. In the future #we should add a profile for coke. # Sum all fuel use by plant, plant state, and DFO/RFO # Assume that plants have fuel delivered from the PADD they are in. Note # that the crude represented in each PADD is the mix of crude going into # that PADD, domestically-produced and imported, and the refining emissions # are representative of the mix of refinery types in that PADD. # Assign each power plant to a PADD. # Creating a dictionary to store the inventories of each fuel/PADD # combination #                    header=0, #                    mangle_dupe_cols=False, #            petroleum_lci_emissions=pd.read_excel( #                    path, #                    sheet_name='Inventory', #                    usecols="I:N", #                    header=0, #                    mangle_dupe_cols=False, #                    skiprows=2) #                    header=1, #                    mangle_dupe_cols=False, # Merging the dataframes within the dictionary to a single datframe # Merge the inventories for each fuel with the fuel use by each power plant # convert per MJ inventory to annual emissions using plant heat input # Cleaning up unneeded columns and renaming # Change compartment values to be standard'