def _unit(unit_name: str) -> Optional[olca.Ref]: """ Get the ID of the openLCA reference unit with the given name. """ ref_id = None if unit_name == 'MWh': ref_id = '92e3bd49-8ed5-4885-9db6-fc88c7afcfcb' elif unit_name == 'MJ': ref_id = '52765a6c-3896-43c2-b2f4-c679acf13efe' elif unit_name == 'kg': ref_id = '20aadc24-a391-41cf-b340-3e4529f44bde' if ref_id is None: log.error('unknown unit %s; no unit reference', unit_name) return None return olca.ref(olca.Unit, ref_id, unit_name)
def _write_top_categories(self, pw: pack.Writer): # elementary flows root = olca.Category() root.id = "f318fa60-bae9-361f-ad5a-5066a0e2a9d1" root.name = "Elementary flows" root.model_type = olca.ModelType.FLOW pw.write(root) # resources res = olca.Category() res.id = "3095c63c-7962-4086-a0d7-df4fd38c2e68" res.name = "resource" res.category = olca.ref(olca.Category, root.id) res.model_type = olca.ModelType.FLOW pw.write(res) # emissions emi = olca.Category() emi.id = "c2433915-9ca3-3933-a64d-68d67e3e3281" emi.name = "emission" emi.category = olca.ref(olca.Category, root.id) emi.model_type = olca.ModelType.FLOW pw.write(emi)
def _write_flows(self, pw: pack.Writer): 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 property if row["AltUnit"] is not None: altfp = olca.FlowPropertyFactor() altfp.reference_flow_property = False altfp.conversion_factor = row["AltUnitConversionFactor"] altfp.flow_property = units.property_ref(row["AltUnit"]) if altfp.flow_property is None: log.warning("unknown altunit %s in flow %s", row["AltUnit"], row["Flow UUID"]) flow.flow_properties.append(altfp) pw.write(flow)
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)
def _write_flow_compartments(self, pw: pack.Writer): handled = set() for _, row in self.flow_list.iterrows(): uid = row["Compartment UUID"] if uid in handled: continue handled.add(uid) parent_uid = None direction = row["Directionality"].strip() if direction == "resource": parent_uid = "3095c63c-7962-4086-a0d7-df4fd38c2e68" elif direction == "emission": parent_uid = "c2433915-9ca3-3933-a64d-68d67e3e3281" else: log.error("Unknown directionality: %s", direction) continue comp = olca.Category() comp.id = uid comp.name = row["Compartment"] comp.model_type = olca.ModelType.FLOW comp.category = olca.ref(olca.Category, parent_uid) pw.write(comp)
def _category(path: str, mtype: olca.ModelType, writer: pack.Writer, created_ids: set) -> Optional[olca.Ref]: if not isinstance(path, str): return None if path.strip() == '': return None parts = path.split('/') parent = None # type: olca.Ref for i in range(0, len(parts)): uid_path = [str(mtype.value)] + parts[0:(i + 1)] uid = _uid(*uid_path) name = parts[i].strip() if uid not in created_ids: category = olca.Category() category.id = uid category.model_type = mtype category.name = name category.category = parent writer.write(category) created_ids.add(uid) parent = olca.ref(olca.Category, uid, name) parent.category_path = uid_path[1:] return parent
def test_ref(self): ref = olca.ref(olca.Flow, 'co2', 'CO2') json = ref.to_json() self.assertEqual('Flow', json['@type']) self.assertEqual('co2', json['@id']) self.assertEqual('CO2', json['name'])
def _flow_property(unit_name: str) -> Optional[olca.Ref]: """Get the ID of the openLCA reference flow property for the unit with the given name.""" if isinstance(unit_name, dict): try: unit_name = unit_name["name"] except KeyError: log.error('dict passed as unit_name but does not contain name key') return None if unit_name == 'MWh': ref_id = 'f6811440-ee37-11de-8a39-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Energy') elif unit_name == 'MJ': ref_id = 'f6811440-ee37-11de-8a39-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Energy') elif unit_name == 'kg': ref_id = '93a60a56-a3c8-11da-a746-0800200b9a66' return olca.ref(olca.FlowProperty, ref_id, 'Mass') elif unit_name == 'sh tn': ref_id = '93a60a56-a3c8-11da-a746-0800200b9a66' return olca.ref(olca.FlowProperty, ref_id, 'Mass') elif unit_name == 'bbl': ref_id = '93a60a56-a3c8-22da-a746-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Volume') elif unit_name == 'cu ft': ref_id = '93a60a56-a3c8-22da-a746-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Volume') elif unit_name == 'btu': ref_id = 'f6811440-ee37-11de-8a39-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Energy') elif unit_name == 'kg*km': ref_id = '838aaa20-0117-11db-92e3-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Goods transport (mass*distance)') elif unit_name == 'kBq': ref_id = '93a60a56-a3c8-17da-a746-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Radioactivity') elif unit_name == 'm2*a': ref_id = '93a60a56-a3c8-21da-a746-0800200c9a66' return olca.ref(olca.FlowProperty, ref_id, 'Area*time') elif unit_name == 'Item(s)': ref_id = '01846770-4cfe-4a25-8ad9-919d8d378345' return olca.ref(olca.FlowProperty, ref_id, 'Number of items') log.error('unknown unit %s; no flow property reference', unit_name) return None