def test_units_are_olcaunits(self):
     import olca.units as olcaunits
     """ Test that units are openlca reference units
     """
     for c_ in flow_list_specs["flow_classes"]:
         flowables = read_in_flowclass_file(c_, "Flowables")
         # Get units and test that they are olca ref units
         ref_units = pd.unique(flowables['Unit'])
         for unt in ref_units:
             olcaref = olcaunits.unit_ref(unt)
             if olcaref is None:
                 log.debug(unt + ' in Flowables for class ' + c_ +
                           ' is not an olca ref unit')
             self.assertIsNotNone(olcaref)
         try:
             altunits_for_class = read_in_flowclass_file(
                 c_, 'FlowableAltUnits')
             alt_units_with_ref = list(
                 altunits_for_class['Alternate Unit']) + list(
                     altunits_for_class['Reference Unit'])
             alt_units_with_ref_unique = set(alt_units_with_ref)
             for unt in alt_units_with_ref_unique:
                 olcaref = olcaunits.unit_ref(unt)
                 if olcaref is None:
                     log.debug(unt + ' in alt units for class ' + c_ +
                               ' is not an olca ref unit')
                 self.assertIsNotNone(olcaref)
         except FileNotFoundError:
             altunits_for_class = None
Beispiel #2
0
def _unit(unit_name: str) -> Optional[olca.Ref]:
    """ Get the openLCA object reference to the unit with the given name. """
    try:
        unit_ref = units.unit_ref(unit_name)
    except:
        log.error('unknown unit %s; no unit reference', unit_name)
    return unit_ref
    def to_json(self) -> dict:
        """
        Creates a dictionary for an olca json file
        :return: dictionary
        """
        flow_ref = olca.FlowRef()
        flow_ref.name = self.name
        if self.category is not None:
            flow_ref.category_path = self.category.split('/')

        # set the UUID or generate it from the attributes
        if self.uid is None:
            flow_ref.id = make_uuid("Flow",
                                    self.category, self.name)
        else:
            flow_ref.id = self.uid

        json = {
            'flow': flow_ref.to_json()
        }
        if self.unit is not None:
            unit_ref = units.unit_ref(self.unit)
            if unit_ref is not None:
                json['unit'] = unit_ref.to_json()
            prop_ref = units.property_ref(self.unit)
            if prop_ref is not None:
                json['flowProperty'] = prop_ref.to_json()

        return json
Beispiel #4
0
    def write(self, df: pandas.DataFrame, write_flows=False):
        for _, row in df.iterrows():
            indicator = self.__indicator(row)
            factor = olca.ImpactFactor()
            flow = self.__flow(row)
            unit = row[8]
            factor.flow = olca.ref(olca.Flow, flow.id)
            factor.flow_property = units.property_ref(unit)
            factor.unit = units.unit_ref(unit)
            factor.value = row[12]
            indicator.impact_factors.append(factor)

        log.info("write entities")
        dicts = [self.__indicators, self.__methods]
        if write_flows:
            dicts.append(self.__categories)
            dicts.append(self.__flows)
        for d in dicts:
            for v in d.values():
                self.__writer.write(v)
Beispiel #5
0
    def to_json(self) -> dict:
        flow_ref = olca.FlowRef()
        flow_ref.name = self.name
        if self.category is not None:
            flow_ref.category_path = self.category.split('/')

        # set the UUID or generate it from the attributes
        if self.uid is None:
            flow_ref.id = _uid(olca.ModelType.FLOW, self.category, self.name)
        else:
            flow_ref.id = self.uid

        json = {'flow': flow_ref.to_json()}
        if self.unit is not None:
            unit_ref = units.unit_ref(self.unit)
            if unit_ref is not None:
                json['unit'] = unit_ref.to_json()
            prop_ref = units.property_ref(self.unit)
            if prop_ref is not None:
                json['flowProperty'] = prop_ref.to_json()

        return json
Beispiel #6
0
def create_unit(unt):
    ar = dict()
    ar['internalId']= units.unit_ref(unt).id
    ar['@type']='Unit'
    ar['name'] = unt
    return ar
Beispiel #7
0
 def test_unit_ref(self):
     ref = units.unit_ref('m2')
     self.assertEqual('3ce61faa-5716-41c1-aef6-b5920054acc9', ref.id),
     self.assertEqual('m2', ref.name)