def _process_table_creation_gen(process_name, exchanges_list, fuel_type):
    fuel_category_dict = {
        "COAL":
        "21: Mining, Quarrying, and Oil and Gas Extraction/2121: Coal Mining",
        "GAS": "22: Utilities/2212: Natural Gas Distribution",
        "OIL":
        "31-33: Manufacturing/3241: Petroleum and Coal Products Manufacturing",
        "NUCLEAR": "31-33: Manufacturing/3251: Basic Chemical Manufacturing",
        #        "GEOTHERMAL": "22: Utilities/2211: Electric Power Generation Transmission and Distribuion",
        #        "WIND": "22: Utilities/2211: Electric Power Generation Transmission and Distribuion",
        #        "SOLAR": "22: Utilities/2211: Electric Power Generation Transmission and Distribuion",
        "CONSTRUCTION": "23: Construction/2371: Utility System Construction",
    }
    ar = dict()
    ar["@type"] = "Process"
    ar["allocationFactors"] = ""
    ar["defaultAllocationMethod"] = ""
    ar["exchanges"] = exchanges_list
    ar["location"] = ""  # location(region)
    ar["parameters"] = ""
    module_logger.info(
        f"passing {fuel_type.lower()}_upstream to process_doc_creation")
    ar['processDocumentation'] = process_doc_creation(
        process_type=f"{fuel_type.lower()}_upstream")
    ar["processType"] = "LCI_RESULT"
    ar["name"] = process_name
    if fuel_type is "coal_transport":
        ar["category"] = fuel_category_dict["COAL"]
    else:
        ar["category"] = fuel_category_dict[fuel_type]
    ar["description"] = process_description_creation(
        f"{fuel_type.lower()}_upstream")
    ar["version"] = make_valid_version_num(elci_version)
    return ar
def process_table_creation_distribution(region, exchanges_list):
    """Add docstring."""
    ar = dict()
    ar["@type"] = "Process"
    ar["allocationFactors"] = ""
    ar["defaultAllocationMethod"] = ""
    ar["exchanges"] = exchanges_list
    ar["location"] = location(region)
    ar["parameters"] = ""
    ar["processDocumentation"] = process_doc_creation()
    ar["processType"] = "UNIT_PROCESS"
    ar["name"] = distribution_to_end_user_name + " - " + region
    ar[
        "category"
    ] = "22: Utilities/2211: Electric Power Generation, Transmission and Distribution"
    ar["description"] = (
        "Electricity distribution to end user in the "
        + str(region)
        + " region."
    )
    ar["description"]=(ar["description"]
        + " This process was created with ElectricityLCI " 
        + "(https://github.com/USEPA/ElectricityLCI) version " + elci_version
        + " using the " + model_specs.model_name + " configuration."
    )
    ar["version"] = make_valid_version_num(elci_version)
    return ar
def process_table_creation_gen(fuelname, exchanges_list, region):
    """Add docstring."""
    ar = dict()
    ar["@type"] = "Process"
    ar["allocationFactors"] = ""
    ar["defaultAllocationMethod"] = ""
    ar["exchanges"] = exchanges_list
    ar["location"] = location(region)
    ar["parameters"] = ""
    ar["processDocumentation"] = process_doc_creation()
    ar["processType"] = "UNIT_PROCESS"
    ar["name"] = (generation_name_parts["Base name"] + "; from " +
                  str(fuelname) + "; " +
                  generation_name_parts["Location type"])
    ar["category"] = (
        "22: Utilities/2211: Electric Power Generation, Transmission and Distribution/"
        + fuelname)
    ar["description"] = ("Electricity from " + str(fuelname) +
                         " produced at generating facilities in the " +
                         str(region) + " region")
    try:
        # Use the software version number as the process version
        ar["version"] = make_valid_version_num(elci_version)
    except:
        #Set to 1 by default
        ar["version"] = 1
    return ar
Beispiel #4
0
def olcaschema_genprocess(database, upstream_dict={}, subregion="BA"):
    """Turns the give database containing generator facility emissions
    into dictionaries that contain the required data for insertion into
    an openLCA-compatible json-ld. Additionally, default providers
    for fuel inputs are mapped, using the information contained in the dictionary
    containing openLCA-formatted data for the fuels.

    Parameters
    ----------
    database : dataframe
        Dataframe containing aggregated emissions to be turned into openLCA
        unit processes
    upstream_dict : dictionary, optional
        Dictionary as created by upstream_dict.py, containing the openLCA
        formatted data for all of the fuel inputs. This function will use the
        names and UUIDs from the entries to assign them as default providers.
    subregion : str, optional
        The subregion level of the aggregated data, by default "BA". See
        aggregation_selector.py for available subregions.

    Returns
    -------
    dictionary: dictionary contaning openLCA-formatted data
    """
    from electricitylci.process_dictionary_writer import (
        unit,
        flow_table_creation,
        ref_exchange_creator,
        uncertainty_table_creation,
        process_doc_creation,
    )

    from electricitylci.aggregation_selector import subregion_col

    region_agg = subregion_col(subregion)
    fuel_agg = ["FuelCategory"]
    if region_agg:
        base_cols = region_agg + fuel_agg
    else:
        base_cols = fuel_agg
    non_agg_cols = [
        "stage_code",
        "FlowName",
        "FlowUUID",
        "Compartment",
        "Unit",
        "Year",
        "source_string",
        "TemporalCorrelation",
        "TechnologicalCorrelation",
        "GeographicalCorrelation",
        "DataCollection",
        "ReliabilityScore",
        "uncertaintyMin",
        "uncertaintyMax",
        "uncertaintyLognormParams",
        "Emission_factor",
        "GeomMean",
        "GeomSD",
    ]

    def turn_data_to_dict(data, upstream_dict):

        module_logger.debug(
            f"Turning flows from {data.name} into dictionaries")
        cols_for_exchange_dict = [
            "internalId",
            "@type",
            "avoidedProduct",
            "flow",
            "flowProperty",
            "input",
            "quantitativeReference",
            "baseUncertainty",
            "provider",
            "amount",
            "amountFormula",
            "unit",
            "pedigreeUncertainty",
            "dqEntry",
            "uncertainty",
            "comment",
        ]
        year = ",".join(data["Year"].astype(str).unique())
        datasources = ",".join(data["source_string"].astype(str).unique())
        data["Maximum"] = data["uncertaintyMax"]
        data["Minimum"] = data["uncertaintyMin"]
        data["uncertainty"] = ""
        data["internalId"] = ""
        data["@type"] = "Exchange"
        data["avoidedProduct"] = False
        data["flowProperty"] = ""
        data["input"] = False
        input_filter = (
            (data["Compartment"].str.lower().str.contains("input"))
            | (data["Compartment"].str.lower().str.contains("resource"))
            | (data["Compartment"].str.lower().str.contains("technosphere")))
        data.loc[input_filter, "input"] = True
        data["baseUncertainty"] = ""
        data["provider"] = ""
        data["unit"] = data["Unit"]
        #        data["ElementaryFlowPrimeContext"] = data["Compartment"]
        #        default_unit = unit("kg")
        #        data["unit"] = [default_unit] * len(data)
        data["FlowType"] = "ELEMENTARY_FLOW"
        product_filter = (
            (data["Compartment"].str.lower().str.contains("technosphere"))
            | (data["Compartment"].str.lower().str.contains("valuable")))
        data.loc[product_filter, "FlowType"] = "PRODUCT_FLOW"
        waste_filter = ((
            data["Compartment"].str.lower().str.contains("technosphere")))
        data.loc[waste_filter, "FlowType"] = "WASTE_FLOW"
        data["flow"] = ""
        provider_filter = data["stage_code"].isin(upstream_dict.keys())
        for index, row in data.loc[provider_filter, :].iterrows():
            provider_dict = {
                "name":
                upstream_dict[getattr(row, "stage_code")]["name"],
                "categoryPath":
                upstream_dict[getattr(row, "stage_code")]["category"],
                "processType":
                "UNIT_PROCESS",
                "@id":
                upstream_dict[getattr(row, "stage_code")]["uuid"],
            }
            data.at[index, "provider"] = provider_dict
            data.at[index, "unit"] = unit(upstream_dict[getattr(
                row, "stage_code")]["q_reference_unit"])
            data.at[index, "FlowType"] = "PRODUCT_FLOW"
        for index, row in data.iterrows():
            data.at[index, "uncertainty"] = uncertainty_table_creation(
                data.loc[index:index, :])
            data.at[index,
                    "flow"] = flow_table_creation(data.loc[index:index, :])
        data["amount"] = data["Emission_factor"]
        data["amountFormula"] = ""
        data["quantitativeReference"] = False
        data["dqEntry"] = (
            "(" + str(round(data["ReliabilityScore"].iloc[0], 1)) + ";" +
            str(round(data["TemporalCorrelation"].iloc[0], 1)) + ";" +
            str(round(data["GeographicalCorrelation"].iloc[0], 1)) + ";" +
            str(round(data["TechnologicalCorrelation"].iloc[0], 1)) + ";" +
            str(round(data["DataCollection"].iloc[0], 1)) + ")")
        data["pedigreeUncertainty"] = ""
        data["comment"] = f"{datasources} - {year}"
        data_for_dict = data[cols_for_exchange_dict]
        data_for_dict = data_for_dict.append(ref_exchange_creator(),
                                             ignore_index=True)
        data_dict = data_for_dict.to_dict("records")
        return data_dict

    database_groupby = database.groupby(by=base_cols)
    process_df = pd.DataFrame(database_groupby[non_agg_cols].apply(
        turn_data_to_dict, (upstream_dict)))
    process_df.columns = ["exchanges"]
    process_df.reset_index(inplace=True)
    process_df["@type"] = "Process"
    process_df["allocationFactors"] = ""
    process_df["defaultAllocationMethod"] = ""
    process_df["location"] = ""
    process_df["parameters"] = ""
    #    process_doc_dict = process_doc_creation(process_type)
    #    process_df["processDocumentation"] = [process_doc_dict]*len(process_df)
    process_df["processType"] = "UNIT_PROCESS"
    process_df["category"] = (
        "22: Utilities/2211: Electric Power Generation, Transmission and Distribution/"
        + process_df[fuel_agg].values)
    if region_agg is None:
        process_df["description"] = (
            "Electricity from " + process_df[fuel_agg].values +
            " produced at generating facilities in the US.")
        process_df["name"] = ("Electricity - " + process_df[fuel_agg].values +
                              " - US")
    else:
        process_df["description"] = (
            "Electricity from " + process_df[fuel_agg].values +
            " produced at generating facilities in the " +
            process_df[region_agg].values + " region.")
        process_df["name"] = ("Electricity - " + process_df[fuel_agg].values +
                              " - " + process_df[region_agg].values)
    process_df["description"] = (
        process_df["description"] +
        " This process was created with ElectricityLCI " +
        "(https://github.com/USEPA/ElectricityLCI) version " + elci_version +
        " using the " + model_specs.model_name + " configuration.")
    process_df["version"] = make_valid_version_num(elci_version)
    process_df["processDocumentation"] = [
        process_doc_creation(x)
        for x in list(process_df["FuelCategory"].str.lower())
    ]
    process_cols = [
        "@type",
        "allocationFactors",
        "defaultAllocationMethod",
        "exchanges",
        "location",
        "parameters",
        "processDocumentation",
        "processType",
        "name",
        "version",
        "category",
        "description",
    ]
    result = process_df[process_cols].to_dict("index")
    return result