Beispiel #1
0
 def lcia(self, quantity, ref_flow=None):
     if not quantity.is_entity:
         # only works for quantity refs-- in other words, always works
         return quantity.do_lcia(self.inventory(ref_flow=ref_flow),
                                 locale=self['SpatialScope'])
     else:
         result = LciaResult(quantity)
         result.add_component(self.get_uuid(), entity=self)
         for ex in self.inventory(ref_flow):
             factor = ex.flow.factor(quantity)
             result.add_score(self.get_uuid(), ex, factor,
                              self['SpatialScope'])
         return result
Beispiel #2
0
    def retrieve_lcia_scores(self, process_uuid, rf_uuid, quantities=None):
        """
        This function retrieves LCIA scores from an Ecospold02 file and stores them as characterizations in
        an LcFlow entity corresponding to the *first* (and presumably, only) reference intermediate flow

        Only stores cfs for quantities that exist locally.
        :param process_uuid:
        :param rf_uuid:
        :param quantities: list of quantity entities to look for (defaults to all local lcia_methods)
        :return: a dict of quantity uuid to score
        """
        if quantities is None:
            quantities = [l for l in self.entities_by_type('quantity') if l.is_lcia_method()]

        import time
        start_time = time.time()
        print('Loading LCIA results for %s_%s' % (process_uuid, rf_uuid))
        o = self.objectify(process_uuid, rf_uuid)

        self._print('%30.30s -- %5f' % ('Objectified', time.time() - start_time))
        p = self._create_process_entity(o)
        rf = self._grab_reference_flow(o, rf_uuid)

        exch = ExchangeValue(p, rf, 'Output', value=1.0)

        tags = dict()
        for q in quantities:
            if 'Method' in q.keys():
                if q['Name'] in tags:
                    raise KeyError('Name collision %s' % q['Name'])
                tags[q['Name']] = q

        results = LciaResults(p)

        for char in find_tag(o, 'flowData').getchildren():
            if 'impactIndicator' in char.tag:
                m = char.impactMethodName.text
                c = char.impactCategoryName.text
                i = char.name.text
                v = float(char.get('amount'))
                my_tag = ', '.join([m, c, i])
                if my_tag in tags:
                    q = tags[my_tag]
                    result = LciaResult(q)
                    cf = Characterization(rf, q, value=v, location=p['SpatialScope'])
                    result.add_score(p.get_uuid(), exch, cf, p['SpatialScope'])
                    results[q.get_uuid()] = result

        self._print('%30.30s -- %5f' % ('Impact scores collected', time.time() - start_time))

        return results
Beispiel #3
0
    def retrieve_lcia_scores(self, filename, quantities=None):
        """
        This function retrieves LCIA scores from an Ecospold02 file and stores them as characterizations in
        an LcFlow entity corresponding to the *first* (and presumably, only) reference intermediate flow

        Only stores cfs for quantities that exist locally.
        :param filename:
        :param quantities: list of quantity entities to look for (defaults to self.quantities())
        :return: a dict of quantity uuid to score
        """
        if quantities is None:
            quantities = self.quantities()

        import time
        start_time = time.time()
        print('Loading LCIA results from %s' % filename)
        o = self.objectify(filename)

        self._print('%30.30s -- %5f' % ('Objectified', time.time() - start_time))
        p = self._create_process_entity(o)
        rf = self._grab_reference_flow(o, spold_reference_flow(filename))

        exch = ExchangeValue(p, rf, 'Output', value=1.0)

        tags = dict()
        for q in quantities:
            if 'Method' in q.keys():
                if q['Name'] in tags:
                    raise KeyError('Name collision %s' % q['Name'])
                tags[q['Name']] = q

        results = LciaResults(p)

        for char in find_tag(o, 'flowData')[0].getchildren():
            if 'impactIndicator' in char.tag:
                m = char.impactMethodName.text
                c = char.impactCategoryName.text
                i = char.name.text
                v = float(char.get('amount'))
                my_tag = ', '.join([m, c, i])
                if my_tag in tags:
                    q = tags[my_tag]
                    result = LciaResult(q)
                    cf = Characterization(rf, q, value=v, location=p['SpatialScope'])
                    result.add_score(p.get_uuid(), exch, cf, p['SpatialScope'])
                    results[q.get_uuid()] = result

        self._print('%30.30s -- %5f' % ('Impact scores collected', time.time() - start_time))

        return results
Beispiel #4
0
 def lcia(self, quantity, ref_flow=None, scenario=None, flowdb=None):
     result = LciaResult(quantity, scenario)
     result.add_component(self.get_uuid(), entity=self)
     for ex in self.allocated_exchanges(scenario or ref_flow):
         if not ex.flow.has_characterization(quantity):
             if flowdb is not None:
                 if quantity in flowdb.known_quantities():
                     factor = flowdb.lookup_single_cf(ex.flow, quantity, self['SpatialScope'])
                     if factor is None:
                         ex.flow.add_characterization(quantity)
                     else:
                         ex.flow.add_characterization(factor)
         factor = ex.flow.factor(quantity)
         result.add_score(self.get_uuid(), ex, factor, self['SpatialScope'])
     return result
Beispiel #5
0
    def do_lcia(self, quantity, inventory, locale='GLO', refresh=False, debug=False, **kwargs):
        """
        takes a quantity and an exchanges generator; returns an LciaResult for the given quantity.
        For now, does NOT pre-load quantity LCIA methods. that is a catalog action. the Qdb doesn't do catalog stuff.
        I am open to re-thinking it, though.
        :param quantity:
        :param inventory: generates exchanges
        :param locale: ['GLO']
        :param refresh: [False] whether to rewrite characterization factors from the database
        :param debug: [False] print extra information to screen
        :param kwargs: just quell_biogenic_co2 for the moment
        :return: an LciaResult whose components are the flows of the exchanges
        """
        q = self[quantity.link]
        q_ind = self._get_q_ind(q)
        _is_quiet = self._quiet
        if debug:
            self._quiet = False
        self._print('q_ind: %d' % q_ind)
        r = LciaResult(q)
        for x in inventory:
            if refresh or not x.flow.has_characterization(q):
                try:
                    factor = self.convert(flow=x.flow, query_q_ind=q_ind, locale=locale, **kwargs)
                except MissingCompartment:
                    self._print('Missing compartment %s; abandoning this exchange' % x.flow['Compartment'])
                    continue
                except ConversionReferenceMismatch:
                    print('Mismatch %s' % x)
                    factor = None

                if factor is not None:
                    self._print('factor %g %s' % (factor, x))
                    x.flow.add_characterization(q, value=factor, overwrite=refresh)
                else:
                    self._print('factor NONE %s' % x)
                    x.flow.add_characterization(q)
            if x.flow.cf(q) is not None:
                r.add_component(x.flow.external_ref, entity=x.flow)
                fac = x.flow.factor(q)
                fac.set_natural_direction(self.c_mgr)
                r.add_score(x.flow.external_ref, x, fac, locale)
        self._quiet = _is_quiet
        return r
Beispiel #6
0
 def fg_lcia(self, process_ref, quantities=None, dist=3, scenario=None, **kwargs):
     """
     :param process_ref:
     :param quantities: defaults to foreground lcia quantities
     :param dist: [1] how far afield to search for cfs (see CLookup.find() from flowdb)
     :param scenario: (not presently used) - some day the flow-quantity database will be scenario-sensitive
     :return:
     """
     if self._catalog.fg is None:
         print('Missing a foreground!')
         return None
     if not self._catalog.is_loaded(0):
         self._catalog.load(0)
     if not self._catalog.is_loaded(process_ref.index):
         self._catalog.load(process_ref.index)
     exch = self.db.filter_exch(process_ref, elem=True, **kwargs)
     qs = self._prep_quantities(quantities)
     results = LciaResults(process_ref.entity())
     for q in qs:
         q_result = LciaResult(q)
         if q in self.db.known_quantities():
             for x in exch:
                 if not x.flow.has_characterization(q):
                     # look locally
                     local = self[0][x.flow.get_uuid()]
                     if local is not None and local.has_characterization(q):
                         cf = local.factor(q)
                         x.flow.add_characterization(cf)
                     else:
                         cf = self.db.lookup_single_cf(x.flow, q, dist=dist, location=process_ref['SpatialScope'])
                         if cf is None:
                             x.flow.add_characterization(q)
                         else:
                             x.flow.add_characterization(cf)
                             self.add_to_foreground(x.flow)
                 fac = x.flow.factor(q)
                 q_result.add_score(process_ref.id, x, fac, process_ref['SpatialScope'])
         results[q.get_uuid()] = q_result
     return results