def _flow(d: dict, flowprop: olca.Ref, writer: pack.Writer, created_ids: set) -> Optional[olca.Ref]: if not isinstance(d, dict): return None uid = _val(d, 'id') name = _val(d, 'name') if isinstance(uid, str) and uid != '': return olca.ref(olca.Flow, uid, name) category_path = _val(d, 'category', default='') uid = _uid(olca.ModelType.FLOW, category_path, name) if uid not in created_ids: flow = olca.Flow() flow.id = uid flow.name = name flow.flow_type = olca.FlowType[_val(d, 'flowType', default='ELEMENTARY_FLOW')] #Do not assign flows a location #flow.location = _location(_val(d, 'location'), # writer, created_ids) flow.category = _category(category_path, olca.ModelType.FLOW, writer, created_ids) propfac = olca.FlowPropertyFactor() propfac.conversion_factor = 1.0 propfac.flow_property = flowprop propfac.reference_flow_property = True flow.flow_properties = [propfac] writer.write(flow) created_ids.add(uid) return olca.ref(olca.Flow, uid, name)
def _write_flows(self, pw: pack.Writer): for _, row in self.flow_list.iterrows(): description = "From FedElemFlowList_%s." % self.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 = self.version flow.last_change = datetime.datetime.now().isoformat() flow.category = olca.ref(olca.Category, row["Compartment UUID"]) flow.flow_type = olca.FlowType.ELEMENTARY_FLOW fp = olca.FlowPropertyFactor() fp.reference_flow_property = True fp.conversion_factor = 1.0 fp.flow_property = olca.ref(olca.FlowProperty, row["Quality UUID"]) flow.flow_properties = [fp] pw.write(flow)
def _write_categories(self, pw: pack.Writer): root = olca.Category() root.id = "f318fa60-bae9-361f-ad5a-5066a0e2a9d1" root.name = "Elementary flows" root.model_type = olca.ModelType.FLOW self._context_uids[root.name.lower()] = root.id pw.write(root) for _, row in self.flow_list.iterrows(): path = row['Context'] if not isinstance(path, str): continue path = path.strip() if path == '' or path.lower() in self._context_uids: continue parts = path.split("/") parent_id = root.id for i in range(0, len(parts)): lpath = "/".join(parts[0:i+1]).lower() uid = self._context_uids.get(lpath) if uid is not None: parent_id = uid continue uid = make_uuid("Flow", lpath) log.info("create category %s", lpath) c = olca.Category() c.id = uid c.name = parts[i] c.category = olca.ref(olca.Category, parent_id) c.model_type = olca.ModelType.FLOW pw.write(c) self._context_uids[lpath] = uid parent_id = uid
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 _source(name: str, writer: pack.Writer, created_ids: set) -> Optional[olca.Ref]: if not isinstance(name, str) or name == '': return None uid = _uid(olca.ModelType.SOURCE, name) if uid in created_ids: return olca.ref(olca.Source, uid, name) source = olca.Source() source.id = uid source.name = name writer.write(source) created_ids.add(uid) return olca.ref(olca.Source, uid, name)
def _actor(name: str, writer: pack.Writer, created_ids: set) -> Optional[olca.Ref]: if not isinstance(name, str) or name == '': return None uid = _uid(olca.ModelType.ACTOR, name) if uid in created_ids: return olca.ref(olca.Actor, uid, name) actor = olca.Actor() actor.id = uid actor.name = name writer.write(actor) created_ids.add(uid) return olca.ref(olca.Actor, uid, name)
def _flow(dict_d: dict, flowprop: olca.Ref, writer: pack.Writer, created_ids: set) -> Optional[olca.Ref]: if not isinstance(dict_d, dict): return None uid = _val(dict_d, 'id') name = _val(dict_d, 'name') orig_uid = None # Checking for technosphere or third party flows that were mapped in # an openLCA model, but these flows must be created in the json-ld here. if (isinstance(uid, str) and uid != '' and ("technosphere" in _val(dict_d, 'category').lower() or "third party" in _val(dict_d, 'category').lower() or "waste" in _val(dict_d, 'category').lower())): orig_uid = uid uid = '' if isinstance(uid, str) and uid != '': return olca.ref(olca.Flow, uid, name) category_path = _val(dict_d, 'category', default='') if orig_uid is None: uid = _uid(olca.ModelType.FLOW, category_path, name) else: uid = orig_uid if uid not in created_ids: flow = olca.Flow() flow.id = uid flow.name = name flow.flow_type = olca.FlowType[_val(dict_d, 'flowType', default='ELEMENTARY_FLOW')] if "waste" in _val(dict_d, 'category').lower(): dict_d['flowType'] = "WASTE_FLOW" flow.flow_type = olca.FlowType[_val(dict_d, 'flowType', default='WASTE_FLOW')] # Do not assign flows a location # flow.location = _location(_val(dict_d, 'location'), # writer, created_ids) flow.category = _category(category_path, olca.ModelType.FLOW, writer, created_ids) propfac = olca.FlowPropertyFactor() propfac.conversion_factor = 1.0 propfac.flow_property = flowprop propfac.reference_flow_property = True flow.flow_properties = [propfac] writer.write(flow) created_ids.add(uid) return olca.ref(olca.Flow, uid, name)
def _location(d: dict, writer: pack.Writer, created_ids: set) -> Optional[olca.Ref]: code = _val(d, 'name') if not isinstance(code, str): return None if code == '': return None uid = _val(d, 'id') if isinstance(uid, str) and uid != '': return olca.ref(olca.Location, uid, code) uid = _uid(olca.ModelType.LOCATION, code) if uid in created_ids: return olca.ref(olca.Location, uid, code) location = olca.Location() location.id = uid location.name = code writer.write(location) created_ids.add(uid) return olca.ref(olca.Location, uid, code)
def _source(src_data: dict, writer: pack.Writer, created_ids: set) -> Optional[olca.Ref]: if not isinstance(src_data, dict) or src_data["Name"] == '': return None try: if isinstance(src_data["Category"], list): category = src_data["Category"] elif isinstance(src_data["Category"], str): category = src_data["Category"] else: category = '' except KeyError: category = '' uid = _uid(olca.ModelType.SOURCE, category, src_data["Name"]) if uid in created_ids: return olca.ref(olca.Source, uid, src_data["Name"]) source = olca.Source() source.id = uid source.name = src_data["Name"] try: source.url = src_data["Url"] except KeyError: source.url = '' try: source.version = src_data["Version"] except KeyError: source.version = '1.0.1' try: source.text_reference = src_data["TextReference"] except KeyError: source.text_reference = src_data["Name"] try: source.year = src_data["Year"] except TypeError: source.year = int(src_data["Year"]) except KeyError: source.year = datetime.datetime.now(pytz.utc).year source.category = _category(category, olca.ModelType.SOURCE, writer, created_ids) writer.write(source) created_ids.add(uid) return olca.ref(olca.Source, uid, src_data["Name"])
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 _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)