def new_flow(self, flow=None, name=None, cas=None, quantity=None, comment=None, compartment=None): if flow is None: name = name or input('Enter flow name: ') cas = cas or ifinput('Enter CAS number (or none): ', '') print('Choose reference quantity or none to create new: ') if quantity is None: q = pick_one(self.flow_properties) if q is None: q = self.new_quantity() quantity = q comment = comment or input('Enter flow comment: ') if compartment is None: print('Choose compartment:') compartment = pick_compartment(self.db.compartments).to_list() flow = LcFlow.new(name, quantity, CasNumber=cas, Compartment=compartment, Comment=comment) # flow.add_characterization(q, reference=True) else: quantity = flow.reference_entity self._catalog[0].add(flow) flow.profile() while ifinput('Add characterizations for this flow? y/n', 'n') != 'n': ch = cyoa('[n]ew or [e]xisting quantity? ', 'en', 'e') if ch == 'n': cq = self.new_quantity() else: cq = pick_one(self[0].quantities()) if cq is None: cq = self.new_quantity() val = parse_math(input('Value (1 %s = x %s): ' % (quantity.unit(), cq.unit()))) flow.add_characterization(cq, value=val) return flow
def create_fragment_from_process(self, process_ref, ref_flow=None, background=False, background_children=True): """ The major entry into fragment building. Given only a process ref, construct a fragment from the process, using the process's reference exchange as the reference fragment flow. :param process_ref: :param ref_flow: :param background: [False] add as a background fragment and do not traverse children :param background_children: [True] automatically terminate child flows with background references (ecoinvent). :return: """ process = process_ref.fg() if ref_flow is None: if len(process.reference_entity) == 0: ref = pick_one([x for x in self.db.filter_exch(process_ref, elem=False) if x.direction == 'Output'] ).flow elif len(process.reference_entity) > 1: ref = pick_reference(process) else: ref = list(process.reference_entity)[0].flow else: try: ref = next(x.flow for x in process.reference_entity if x.flow.match(ref_flow)) except StopIteration: print('Reference flow not found in target process.') return None ref_exch = next(x for x in process.exchange(ref)) direction = comp_dir(ref_exch.direction) frag = self[0].create_fragment(ref, direction, Name='%s' % process_ref.entity(), background=background) frag.terminate(process_ref, flow=ref) if not background: self.build_child_flows(frag, background_children=background_children) return frag
def create_fragment(self): print('Create fragment.') name = input('Name: ') k = cyoa('Reference flow: Use (F)oreground flow or (S)earch for flow?', 'FS', 'F') if k.lower() == 'f': print('Select Reference flow:') flow = pick_one(self._catalog[0].flows()) else: self.isearch('flow')() flow = pick_one([f for f in self.selected if f.entity_type == 'flow']) self._m.add_to_foreground(flow) flow = flow.entity() print('Direction w.r.t. upstream:') direction = menu_list('Input', 'Output') print('interface\nname: %s\nflow: %s\ndirn: %s' % (name, flow, direction)) self._catalog[0].create_fragment(flow, direction, name=name)
def edit_flow(self): flow = pick_one(self._catalog[0].flows()) ch = cyoa('Edit (P)roperties or (C)haracterizations? ', 'pc').lower() if ch == 'p': self._edit_entity(flow) elif ch == 'c': self.edit_characterizations(flow)
def edit_flow(self): flow = pick_one(self._catalog[0].flows()) print('Select field to edit:') field = menu_list(*flow.keys()) if field == -1 or field is None: return True new = ifinput('Enter new value for %s: ' % field, flow[field]) flow[field] = new
def find_flow(self, name, index=None, elementary=False): if name is None: name = input('Enter flow name search string: ') res = self.search(index, 'flow', Name=name, show=False) if elementary is not None: res = list(filter(lambda x: self.db.is_elementary(x.entity()) == elementary, res)) pick = pick_one(res) print('Picked: %s' % pick) return pick
def auto_terminate(self, frag, index=None, scenario=None, background_children=True): if scenario is None and not frag.term.is_null: return # nothing to do-- (ecoinvent) already terminated by exchange ex = pick_one(self.find_termination(frag, index=index)) if ex is None: ex = self.terminate_by_search(frag, index=index) if ex is not None: self.add_termination(frag, ex, scenario=scenario, background_children=background_children) else: print('Not terminated.')
def _continue_search(self, result_set): """ :param result_set: set of catalog refs :return: """ while 1: if len(result_set) == 0: return 'No results.' if len(result_set) > 20: group(result_set) i = cyoa('\n(B) Browse results, (A) select all, (N)arrow search, or (X) abandon search / finished?', 'BANX', 'N') else: show_res(result_set) i = cyoa('\n(S) Select one, (A) select all, (N)arrow search, or (X) abandon search / finished?', 'SANX', 'S') if i.lower() == 'x': return True elif i.lower() == 'a': self.selected = self.selected.union(result_set) return True elif i.lower() == 'n': result_set = self._narrow_search(result_set) elif i.lower() == 'b': pick = pick_one(result_set) e = self._prompt_add(pick) if e in result_set: result_set.remove(e) elif i.lower() == 'b' or i.lower() == 's': # 'b' ad 's' are the same- browse and pick pick = pick_one(result_set) if pick is not None: self.selected.add(pick) result_set.remove(pick) else: try: if int(i) < len(result_set): pick = result_set[int(i)] self.selected.add(pick) result_set.remove(pick) except ValueError: pass
def ibrowse(self): self._menu_position = [choices, choices['Catalog']] ar = self._current_archive or self._choose_archive(allow_all=False) if ar == -1: print('No archives loaded!') return [] entities = [e for e in self._catalog[ar].entities()] if len(entities) == 0: return lambda: 'No entities found' g = pick_one(entities) return self._prompt_add(self._catalog.ref(ar, g))
def create_flow(self): name = input('Enter flow name: ') cas = ifinput('Enter CAS number (or none): ', '') print('Choose reference quantity: ') q = pick_one(self._catalog[0].quantities()) print('Choose compartment:') comment = input('Enter comment: ') c = pick_compartment(self._flowdb.compartments) flow = LcFlow.new(name, q, CasNumber=cas, Compartment=c.to_list(), Comment=comment) # flow.add_characterization(q, reference=True) self._catalog[0].add(flow) return flow
def add_child_fragment(self): parent = self._selected_fragment k = cyoa('use (N)ew or (E)xisting flow?', 'NE', 'N') if k.lower() == 'e': print('Select Reference flow:') flow = pick_one(self._catalog[0].flows()) if flow is None: print('Canceling child fragment flow') return None else: flow = self.create_flow() direction = menu_list('Input', 'Output') self._catalog[0].add_child_fragment_flow(parent, flow, direction)
def _reduce_cfs(flow, quantity, cfs, location='GLO'): """ Take a list of CFs and try to find the best one--- failing all else, ask the user :param flow: :param quantity: :param cfs: :param location: :return: """ if len(cfs) == 1: return list(cfs)[0] elif len(cfs) > 1: cf1 = [cf for cf in cfs if cf.flow.match(flow)] if len(cf1) == 1: return cf1[0] elif len(cf1) > 1: cfs = cf1 # this reduces the list (presumably) cf1 = [cf for cf in cfs if location in cf.locations()] if len(cf1) == 1: return cf1[0] elif len(cf1) > 1: cfs = cf1 # this reduces the list (presumably) vals = [cf[location] for cf in cfs] try: if len(set(vals)) > 1: print('Multiple CFs found: %s' % vals) print('Flow: %s' % flow) print('Quantity: %s' % quantity) print('Pick characterization to apply') return pick_one(list(cfs)) except TypeError: print(vals) raise # print('All characterizations have the same value- picking first one') return list(cfs)[0]
def set_term_flow(self, flow): """ flow must have an exchange with process ref :param flow: :return: """ if self.is_null: flow = self._parent.flow elif self._process_ref.entity_type == 'fragment': if flow is None: ## let's try relaxing this # term flow must be sub-fragment's reference flow flow = self.term_node.flow else: if flow is None: flow = self._parent.flow try: next(self._process_ref.fg().exchange(flow)) except StopIteration: r_e = self._process_ref.fg().reference_entity if len(r_e) == 1: r_e = list(r_e)[0] flow = r_e.flow self.direction = r_e.direction elif len(r_e) > 0: r_e = pick_one(list(r_e)) flow = r_e.flow self.direction = r_e.direction else: # instead of throwing exception, just tolerate a no-reference-flow node using _parent.flow pass # raise MissingFlow('%s missing flow %s\nAND no reference exchange' % (self._process_ref, flow)) except TypeError: print('Fragment: %s\nprocess_ref: %s' % (self._parent, self._process_ref)) raise self.term_flow = flow
def q_lcia(self, p_ref): q = pick_one(self._catalog[0].lcia_methods()) self._m.show_detailed_lcia(p_ref, quantity=q)
def edit_characterizations(flow): char = pick_one(cf for cf in flow.characterizations()) val = float(ifinput('enter new characterization value: ', char.value)) char.value = val
def terminate_by_search(self, frag, index=None): print('%s' % frag) print('Search for termination.') string = ifinput('Enter search term: ', frag['Name']) return pick_one(self.search(index, 'p', Name=string, show=False))
def sink(self, flow_ref): z = self._catalog.sink(flow_ref) g = pick_one(z) return self._prompt_add(g)
def new_flow(self, flow=None, name=None, cas=None, quantity=None, comment=None, compartment=None, local_unit=None, **kwargs): if flow is None: name = name or self.input('Enter flow name: ', 'New flow') cas = cas or self.ifinput('Enter CAS number (or none): ', '') if quantity is None: if self._interactive: print('Choose reference quantity or none to create new: ') q = pick_one([fp for fp in self._qdb.flow_properties]) if q is None: q = self.new_quantity() quantity = q else: print('Using mass as reference quantity') quantity = self._qdb.get_canonical('mass') comment = comment or self.input('Enter flow comment: ', '') if compartment is None: if self._interactive: print('Choose compartment:') compartment = pick_compartment( self._qdb.c_mgr.compartments).to_list() else: print('Designating Intermediate flow') compartment = self._qdb.c_mgr.find_matching( 'Intermediate Flows').to_list() else: compartment = self._qdb.c_mgr.find_matching( compartment).to_list() if local_unit is not None: local_conv = quantity.convert(to=local_unit) if local_conv is None: print('Falling back to default unit: %s' % quantity.unit()) local_unit = None flow = LcFlow.new(name, quantity, CasNumber=cas, Compartment=compartment, Comment=comment, local_unit=local_unit, **kwargs) else: quantity = flow.reference_entity if self._interactive: flow.profile() while ifinput('Add characterizations for this flow? y/n', 'n') != 'n': ch = cyoa('[n]ew or [e]xisting quantity? ', 'en', 'e') if ch == 'n': cq = self.new_quantity() else: cq = pick_one(self._qdb.quantities()) if cq is None: cq = self.new_quantity() val = parse_math( input('Value (1 %s = x %s): ' % (quantity.unit(), cq.unit()))) flow.add_characterization(cq, value=val) return flow
def select_exchange(self, p_ref): exch = self._m._filter_exch(p_ref, elem=False) g = pick_one(exch) self.terminate(ExchangeRef(self._catalog, p_ref.index, g))
def terminate(self, exch_ref): z = self._catalog.terminate(exch_ref, show=False) g = pick_one(z) return self._prompt_add(g)