From 80009c7953c48abfb9c151819e77d8dd761bf640 Mon Sep 17 00:00:00 2001 From: romainsacchi Date: Sun, 28 Jul 2024 20:14:44 +0200 Subject: [PATCH] Allows writing untransformed databases --- premise/new_database.py | 48 +++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/premise/new_database.py b/premise/new_database.py index 4c140e5a..7df08e69 100644 --- a/premise/new_database.py +++ b/premise/new_database.py @@ -980,7 +980,13 @@ def write_superstructure_db_to_brightway( ) for scenario in self.scenarios: - scenario = load_database(scenario) + try: + scenario = load_database(scenario) + except KeyError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + except FileNotFoundError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + _prepare_database( scenario=scenario, db_name=name, @@ -1066,7 +1072,13 @@ def write_db_to_brightway(self, name: [str, List[str]] = None): print("Write new database(s) to Brightway.") for s, scenario in enumerate(self.scenarios): - scenario = load_database(scenario) + try: + scenario = load_database(scenario) + except KeyError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + except FileNotFoundError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + _prepare_database( scenario=scenario, db_name=name[s], @@ -1135,7 +1147,13 @@ def scenario_name(scenario): print("Write new database(s) to matrix.") for s, scenario in enumerate(self.scenarios): - scenario = load_database(scenario) + try: + scenario = load_database(scenario) + except KeyError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + except FileNotFoundError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + _prepare_database( scenario=scenario, db_name="database", @@ -1170,7 +1188,13 @@ def write_db_to_simapro(self, filepath: str = None): print("Write Simapro import file(s).") for scenario in self.scenarios: - scenario = load_database(scenario) + try: + scenario = load_database(scenario) + except KeyError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + except FileNotFoundError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + _prepare_database( scenario=scenario, db_name="database", @@ -1211,7 +1235,13 @@ def write_db_to_olca(self, filepath: str = None): print("Write Simapro import file(s) for OpenLCA.") for scenario in self.scenarios: - scenario = load_database(scenario) + try: + scenario = load_database(scenario) + except KeyError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + except FileNotFoundError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + _prepare_database( scenario=scenario, db_name="database", @@ -1246,7 +1276,13 @@ def write_datapackage( raise ValueError(f"No cached inventories found at {cache_fp}.") for scenario in self.scenarios: - scenario = load_database(scenario) + try: + scenario = load_database(scenario) + except KeyError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + except FileNotFoundError: + scenario["database"] = pickle.loads(pickle.dumps(self.database, -1)) + _prepare_database( scenario=scenario, db_name=name,