def __init__(self, source=REF_QTYS, quantities=Q_SYNS, flowables=F_SYNS, compartments=None, quell_biogenic_CO2=False, quell_biogenic_co2=False, ref=None, **kwargs): """ :param source: for LcArchive and core quantities (ELCD) :param quantities: [Q_SYNS] synonym list for quantities :param flowables: [F_SYNS] synonym list for flowables :param compartments: either a compartment manager, or a compartment definition file :param quell_biogenic_CO2: [False] if True, return '0' for all inputs of CO2 from air and all CO2 emissions matching a 'biogenic' search criterion. The CO2 flowable is distinguished by its CAS number, 124-38-9. No other flowables are affected. The criterion is not quantity-based, but depends on the flowable and compartment only. The regex test is currently case-insensitive '(biogenic|biotic|non-fossil)'. It can be tested with Qdb.is_biogenic(term) The quell_biogenic_CO2 parameter can be overridden at query-time as a keyword param. Note that the Qdb's architecture does not permit it to distinguish between two different flows of the same substance into the same compartment, without making a special exception. Frankly, to make it do so is somewhat distasteful. :param ref: :param kwargs: """ if ref is None: ref = 'local.qdb' if not os.path.exists(source): print('Using default reference quantities') source = REF_QTYS self._f = Flowables.from_json(from_json(flowables)) self._q = SynList.from_json(from_json(quantities)) super(Qdb, self).__init__(source, ref=ref, **kwargs) self.load_from_dict(from_json(source)) if isinstance(compartments, CompartmentManager): self.c_mgr = compartments else: self.c_mgr = CompartmentManager(compartments) self._index_quantities() self._q_dict = defaultdict(set) # dict of quantity index to set of characterized flowables (by index) self._fq_dict = defaultdict(CLookup) # dict of (flowable index, quantity index) to c_lookup self._f_dict = defaultdict(set) # dict of flowable index to set of characterized quantities (by index) # following are to implement special treatment for biogenic CO2 self._quell_biogenic_co2 = quell_biogenic_CO2 or quell_biogenic_co2 self._co2_index = self._f.index('124-38-9') self._comp_from_air = self.c_mgr.find_matching('Resources from air') # load CFs from reference flows for f in self.entities_by_type('flow'): for cf in f.characterizations(): if cf.quantity is not f.reference_entity: self.add_cf(cf)
def __init__(self, ref, version='Unspecified', internal=False, data_dir=None, model=None, **kwargs): """ :param ref: :param version: :param internal: :param kwargs: quiet, upstream """ super(EcoinventSpreadsheet, self).__init__(ref, **kwargs) self.version = version self.internal = internal self._serialize_dict['version'] = version self._serialize_dict['internal'] = internal # these things are query-only, for foreground use self._data_dir = data_dir self._model = model self.fg = None self.bg = None self.lcia = None if self._data_dir is not None: if model == 'undefined': self.fg = EcospoldV2Archive(self._fg_filename, prefix='datasets - public') else: self.fg = EcospoldV2Archive(self._fg_filename, prefix='datasets') if os.path.exists(self._bg_filename): print('BG: Accessing LCI from %s' % self._bg_filename) self.bg = LcArchive(self._lci_cache) if os.path.exists(self._lci_cache): self.bg.load_json(from_json(self._lci_cache)) if os.path.exists(self._lcia_validate_filename): self.lcia = EcospoldV2Archive(self._lcia_validate_filename, prefix='datasets')
def archive_from_json(fname, **archive_kwargs): """ :param fname: JSON filename :return: an ArchiveInterface """ j = from_json(fname) archive_kwargs["quiet"] = True if "prefix" in j.keys(): archive_kwargs["prefix"] = j["prefix"] if "nsUuid" in j.keys(): archive_kwargs["ns_uuid"] = j["nsUuid"] if j["dataSourceType"] == "EcoinventSpreadsheet": archive_kwargs["internal"] = bool(j["internal"]) archive_kwargs["version"] = j["version"] try: a = archive_factory(j["dataSourceReference"], j["dataSourceType"], **archive_kwargs) except KeyError: raise ValueError("Unknown dataSourceType %s" % j["dataSourceType"]) if "catalogNames" in j: a.catalog_names = j["catalogNames"] if "upstreamReference" in j: print("**Upstream reference encountered: %s\n" % j["upstreamReference"]) a._serialize_dict["upstreamReference"] = j["upstreamReference"] a.load_json(j) return a
def load_lci_cache(self): if self._bg_cache_loaded is False: print('Accessing LCI from %s' % self._bg_filename) self.bg.load_from_dict(from_json(self._lci_cache)) self._bg_cache_loaded = True