def _write_flows(self, pw: pack.Writer):
        altflowlist = fedelemflowlist.get_alt_conversion()
        for _, row in self.flow_list.iterrows():
            description = "From FedElemFlowList_" + flow_list_specs[
                'list_version'] + '.'
            flow_class = row.get("Class")
            if flow_class is not None:
                description += " Flow Class: %s." % flow_class

            preferred = row.get("Preferred", 0)
            if preferred == 1 or preferred == "1":
                description += " Preferred flow."
            else:
                description += " Not a preferred flow."

            flow = olca.Flow()
            flow.description = description
            flow.id = row["Flow UUID"]
            flow.name = row["Flowable"]
            flow.cas = row.get("CAS No", None)
            flow.formula = row.get("Formula", None)
            flow.version = flow_list_specs['list_version']
            flow.last_change = datetime.datetime.now().isoformat()
            flow.flow_type = olca.FlowType.ELEMENTARY_FLOW

            context_uid = self._context_uids.get(row['Context'].lower())
            if context_uid is not None:
                flow.category = olca.ref(olca.Category, context_uid)

            fp = olca.FlowPropertyFactor()
            fp.reference_flow_property = True
            fp.conversion_factor = 1.0
            fp.flow_property = units.property_ref(row["Unit"])
            if fp.flow_property is None:
                log.warning("unknown unit %s in flow %s", row["Unit"],
                            row["Flow UUID"])
            flow.flow_properties = [fp]
            #Add in alternate unit flow propert(ies), if an alternate unit exists
            #in the flows list, uses short list of altflowlist to assign one or more
            #alternate units
            if row["AltUnit"] is not None:
                #create dataframe of all alternate units for this flowable
                altunits = altflowlist[altflowlist['Flowable'] ==
                                       row["Flowable"]]
                for i, alternate in altunits.iterrows():
                    altfp = olca.FlowPropertyFactor()
                    altfp.reference_flow_property = False
                    altfp.conversion_factor = alternate[
                        'AltUnitConversionFactor']
                    altfp.flow_property = units.property_ref(
                        alternate["AltUnit"])
                    if altfp.flow_property is None:
                        log.warning("unknown altunit %s in flow %s",
                                    alternate["AltUnit"], row["Flow UUID"])
                    else:
                        flow.flow_properties.append(altfp)
            pw.write(flow)
def get(subset=None) -> pd.DataFrame:
    """
    Returns a dataframe of inventory based methods.
    :param subset: a list of dictionary keys from available inventories, if 
    none selected all availabile inventories will be generated
    :return: df in standard LCIAmethod format
    """
    method = df.data_frame(list())
    method['Characterization Factor'] = pd.to_numeric(
        method['Characterization Factor'])

    if subset == None:
        list_of_inventories = subsets.get_subsets()
    else:
        list_of_inventories = subset

    alt_units = flowlist.get_alt_conversion()

    for inventory in list_of_inventories:
        flows = flowlist.get_flows(subset=inventory)
        flows.drop([
            'Formula', 'Synonyms', 'Class', 'External Reference', 'Preferred',
            'AltUnit', 'AltUnitConversionFactor'
        ],
                   axis=1,
                   inplace=True)
        flows['Indicator'] = inventory
        flows['Indicator unit'] = subsets.get_inventory_unit(inventory)
        flows['Characterization Factor'] = 1

        # Apply unit conversions where flow unit differs from indicator unit
        flows_w_conversion = pd.merge(
            flows,
            alt_units,
            how='left',
            left_on=['Flowable', 'Indicator unit', 'Unit'],
            right_on=['Flowable', 'AltUnit', 'Unit'])
        flows_w_conversion.loc[(flows_w_conversion['AltUnit'] ==
                                flows_w_conversion['Indicator unit']),
                               'Characterization Factor'] = flows_w_conversion[
                                   'AltUnitConversionFactor']
        flows_w_conversion.drop(
            ['AltUnit', 'AltUnitConversionFactor', 'InverseConversionFactor'],
            axis=1,
            inplace=True)

        method = pd.concat([method, flows_w_conversion], ignore_index=True)

    method['Method'] = 'Inventory'
    return method
예제 #3
0
Requires target unit to be the primary unit. Existing conversion factor must be
set to 1 to avoid replacing manual conversion factors. Mapping file must already
conform to mapping format.
"""
import pandas as pd
import fedelemflowlist
from fedelemflowlist.globals import flowmappingpath, flowmapping_fields, log

#Add source name here. The .csv mapping file with this name must be in the flowmapping directory
#None can be used to add conversions in all mapping files
source = 'ReCiPe2016'

if __name__ == '__main__':
    # Pull in mapping file
    mapping = fedelemflowlist.get_flowmapping(source)
    conversions = fedelemflowlist.get_alt_conversion()
    # merge in conversion factors where source unit = alternate unit
    mapping_w_conversion = pd.merge(
        mapping,
        conversions,
        how='left',
        left_on=['TargetFlowName', 'SourceUnit', 'TargetUnit'],
        right_on=['Flowable', 'AltUnit', 'Unit'])
    # update conversion factor where current conversion is 1 and the updated conversion exists
    converted1 = mapping_w_conversion['InverseConversionFactor'].notnull()
    converted2 = mapping_w_conversion['ConversionFactor'] == 1
    mapping_w_conversion['Convert'] = converted1 & converted2
    mapping_w_conversion.loc[
        (mapping_w_conversion['Convert'] == True),
        'ConversionFactor'] = mapping_w_conversion['InverseConversionFactor']
    converted = mapping_w_conversion['Convert'].sum()