|
| 1 | +from typing import Optional |
| 2 | +from io import StringIO |
| 3 | +from pathlib import Path |
| 4 | +import functools |
| 5 | +from time import time |
| 6 | + |
| 7 | +from bw2data import Database, config |
| 8 | +from bw_simapro_csv import SimaProCSV |
| 9 | + |
| 10 | +from ..strategies import ( |
| 11 | + assign_only_product_as_production, |
| 12 | + change_electricity_unit_mj_to_kwh, |
| 13 | + convert_activity_parameters_to_list, |
| 14 | + drop_unspecified_subcategories, |
| 15 | + fix_localized_water_flows, |
| 16 | + fix_zero_allocation_products, |
| 17 | + link_iterable_by_fields, |
| 18 | + link_technosphere_based_on_name_unit_location, |
| 19 | + migrate_datasets, |
| 20 | + migrate_exchanges, |
| 21 | + normalize_biosphere_categories, |
| 22 | + normalize_biosphere_names, |
| 23 | + normalize_simapro_biosphere_categories, |
| 24 | + normalize_simapro_biosphere_names, |
| 25 | + normalize_units, |
| 26 | + set_code_by_activity_hash, |
| 27 | + sp_allocate_products, |
| 28 | + split_simapro_name_geo, |
| 29 | + strip_biosphere_exc_locations, |
| 30 | + update_ecoinvent_locations, |
| 31 | +) |
| 32 | +from ..strategies.simapro import set_lognormal_loc_value_uncertainty_safe |
| 33 | +from .base_lci import LCIImporter |
| 34 | + |
| 35 | + |
| 36 | +class SimaProBlockCSVImporter(LCIImporter): |
| 37 | + format = u"bw_simapro_csv" |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + path_or_stream: Path | StringIO, |
| 42 | + database_name: Optional[str] = None, |
| 43 | + ): |
| 44 | + start = time() |
| 45 | + data = SimaProCSV(path_or_stream=path_or_stream, database_name=database_name).to_brightway() |
| 46 | + |
| 47 | + # project_parameters |
| 48 | + # database_parameters |
| 49 | + |
| 50 | + self.db_name = data.database_name |
| 51 | + |
| 52 | + self.strategies = [ |
| 53 | + normalize_units, |
| 54 | + update_ecoinvent_locations, |
| 55 | + assign_only_product_as_production, |
| 56 | + drop_unspecified_subcategories, |
| 57 | + sp_allocate_products, |
| 58 | + fix_zero_allocation_products, |
| 59 | + split_simapro_name_geo, |
| 60 | + strip_biosphere_exc_locations, |
| 61 | + functools.partial(migrate_datasets, migration="default-units"), |
| 62 | + functools.partial(migrate_exchanges, migration="default-units"), |
| 63 | + functools.partial(set_code_by_activity_hash, overwrite=True), |
| 64 | + change_electricity_unit_mj_to_kwh, |
| 65 | + link_technosphere_based_on_name_unit_location, |
| 66 | + set_lognormal_loc_value_uncertainty_safe, |
| 67 | + ] |
| 68 | + if normalize_biosphere: |
| 69 | + self.strategies.extend( |
| 70 | + [ |
| 71 | + normalize_biosphere_categories, |
| 72 | + normalize_simapro_biosphere_categories, |
| 73 | + normalize_biosphere_names, |
| 74 | + normalize_simapro_biosphere_names, |
| 75 | + functools.partial(migrate_exchanges, migration="simapro-water"), |
| 76 | + fix_localized_water_flows, |
| 77 | + ] |
| 78 | + ) |
| 79 | + self.strategies.extend( |
| 80 | + [ |
| 81 | + functools.partial( |
| 82 | + link_iterable_by_fields, |
| 83 | + other=Database(biosphere_db or config.biosphere), |
| 84 | + kind="biosphere", |
| 85 | + ), |
| 86 | + convert_activity_parameters_to_list, |
| 87 | + ] |
| 88 | + ) |
| 89 | + |
| 90 | + def get_db_name(self): |
| 91 | + candidates = {obj["database"] for obj in self.data} |
| 92 | + if not len(candidates) == 1: |
| 93 | + raise ValueError("Can't determine database name from {}".format(candidates)) |
| 94 | + return list(candidates)[0] |
| 95 | + |
| 96 | + def write_database(self, data=None, name=None, *args, **kwargs): |
| 97 | + db = super(SimaProCSVImporter, self).write_database(data, name, *args, **kwargs) |
| 98 | + # database_parameters[db.name] = self.global_parameters |
| 99 | + db.metadata["simapro import"] = self.metadata |
| 100 | + db._metadata.flush() |
| 101 | + return db |
| 102 | + |
| 103 | + # def match_ecoinvent3(self, db_name, system_model): |
| 104 | + # """Link SimaPro transformed names to an ecoinvent 3.X database. |
| 105 | + |
| 106 | + # Will only link processes from the given ``system_model``. Available ``system_model``s are: |
| 107 | + |
| 108 | + # * apos |
| 109 | + # * consequential |
| 110 | + # * cutoff |
| 111 | + |
| 112 | + # Matching across system models is possible, but not all processes in one system model exist in other system models. |
| 113 | + |
| 114 | + # """ |
| 115 | + # currently_unmatched = self.statistics(False)[2] |
| 116 | + # func_list = [functools.partial( |
| 117 | + # sp_match_ecoinvent3_database, |
| 118 | + # ei3_name=db_name |
| 119 | + # )] |
| 120 | + # self.apply_strategies(func_list) |
| 121 | + # matched = currently_unmatched - self.statistics(False)[2] |
| 122 | + # print(u"Matched {} exchanges".format(matched)) |
| 123 | + |
| 124 | + def match_ecoinvent2(self, db_name): |
| 125 | + currently_unmatched = self.statistics(False)[2] |
| 126 | + # func_list = [ |
| 127 | + # functools.partial( |
| 128 | + # sp_detoxify_link_technosphere_by_activity_hash, |
| 129 | + # external_db_name=db_name |
| 130 | + # )] |
| 131 | + # TODO |
| 132 | + self.apply_strategies(func_list) |
| 133 | + matched = currently_unmatched - self.statistics(False)[2] |
| 134 | + print(u"Matched {} exchanges".format(matched)) |
0 commit comments