def write_database(self, biosphere=None): new_biosphere = self.add_unlinked_flows_to_new_biosphere_database() main_biosphere = biosphere or config.biosphere print("Created new database for EXIOBASE-specific biosphere flows: {}". format(new_biosphere)) add_biosphere_ids(self.biosphere_correspondence, [new_biosphere, main_biosphere]) self.write_activities_as_database() print("Created database of EXIOBASE activity metadata") add_product_ids(self.products, self.db_name) self.patch_lcia_methods(new_biosphere) print("Patching LCIA methods with EXIOBASE flows") product_mapping = {(o["name"], o["location"]): o["id"] for o in self.products} biosphere_mapping = { o["exiobase name"]: o["id"] for o in self.biosphere_correspondence if "id" in o } biosphere_scales = { o["exiobase name"]: float(o["scale factor"]) for o in self.biosphere_correspondence if "id" in o } technosphere = itertools.chain( ({ "row": product_mapping[x], "col": product_mapping[y], "amount": z, "flip": True, "uncertainty_type": 0, } for x, y, z in self.techosphere_iterator), ({ "row": x, "col": x, "amount": 1, "flip": False, "uncertainty_type": 0 } for x in product_mapping.values()), ) biosphere = ({ "row": biosphere_mapping[x], "col": product_mapping[y], "amount": z * biosphere_scales[x], "flip": False, "uncertainty_type": 0, } for x, y, z in self.biosphere_iterator) dependents = [new_biosphere, main_biosphere] IOTableBackend(self.db_name).write_exchanges(technosphere, biosphere, dependents)
def write_activities_as_database(self): db = IOTableBackend(self.db_name) data = {(self.db_name, "{}|{}".format(o["name"], o["location"])): { "name": o["name"], "reference product": o["name"], "location": o["location"], "unit": o["unit"], "production volume": o["production volume"], "stam": o["stam"], "exchanges": [], } for o in self.products} if self.db_name not in databases: db.register(format="EXIOBASE 3", filepath=str(self.dirpath)) db.write(data)
def write_database(self): mrio = IOTableBackend(self.db_name) mrio.write(self.activities, self.exchanges)