1
1
import warnings
2
2
from pathlib import Path
3
- from typing import List , Optional
3
+ from typing import List , Optional , Union
4
4
5
5
import pandas as pd
6
6
from ecoinvent_interface import EcoinventRelease , Settings
7
7
from loguru import logger
8
+ from randonneur import Datapackage , MappingConstants
8
9
9
10
from .data_io import get_brightway_databases , get_change_report_filepath , setup_project
10
- from .datapackage import write_datapackage
11
11
from .errors import VersionJump
12
- from .utils import configure_logs
12
+ from .utils import configure_logs , setup_output_directory
13
13
from .wrangling import (
14
14
resolve_glo_row_rer_roe ,
15
15
source_target_biosphere_pair ,
16
16
source_target_pair_as_bw_dict ,
17
17
split_replace_disaggregate ,
18
18
)
19
+ from . import __version__
19
20
20
21
21
22
def get_change_report_context (
@@ -88,11 +89,12 @@ def generate_technosphere_mapping(
88
89
ecoinvent_username : Optional [str ] = None ,
89
90
ecoinvent_password : Optional [str ] = None ,
90
91
write_logs : bool = True ,
92
+ write_file : bool = True ,
91
93
licenses : Optional [List [dict ]] = None ,
92
94
output_directory : Optional [Path ] = None ,
93
- output_version : str = "1 .0.0" ,
95
+ output_version : str = "2 .0.0" ,
94
96
description : Optional [str ] = None ,
95
- ) -> Path :
97
+ ) -> Union [ Path , Datapackage ] :
96
98
"""Generate a Randonneur mapping file for technosphere edge attributes from source to target."""
97
99
configure_logs (write_logs = write_logs )
98
100
@@ -130,6 +132,10 @@ def generate_technosphere_mapping(
130
132
source_db_name , target_db_name , source_lookup , target_lookup = get_brightway_databases (
131
133
source_version = source_version , target_version = target_version , system_model = system_model
132
134
)
135
+
136
+ if not description :
137
+ description = f"Data migration file from { source_db_name } to { target_db_name } generated with `ecoinvent_migrate` version { __version__ } "
138
+
133
139
data = resolve_glo_row_rer_roe (
134
140
data = data ,
135
141
source_db_name = source_db_name ,
@@ -149,15 +155,32 @@ def generate_technosphere_mapping(
149
155
if not data ["disaggregate" ]:
150
156
del data ["disaggregate" ]
151
157
152
- return write_datapackage (
153
- data = data ,
154
- source_db_name = source_db_name ,
155
- target_db_name = target_db_name ,
156
- licenses = licenses ,
157
- output_directory = output_directory ,
158
- version = output_version ,
158
+ dp = Datapackage (
159
+ name = f"{ source_db_name } -{ target_db_name } " ,
159
160
description = description ,
161
+ contributors = [
162
+ {"title" : "ecoinvent association" , "path" : "https://ecoinvent.org/" , "role" : "author" },
163
+ {"title" : "Chris Mutel" , "path" : "https://chris.mutel.org/" , "role" : "wrangler" },
164
+ ],
165
+ mapping_source = MappingConstants .ECOSPOLD2 ,
166
+ mapping_target = MappingConstants .ECOSPOLD2 ,
167
+ homepage = "https://github.com/brightway-lca/ecoinvent_migrate" ,
168
+ version = "2.0.0" ,
169
+ source_id = source_db_name ,
170
+ target_id = target_db_name ,
171
+ licenses = licenses ,
160
172
)
173
+ for key , value in data .items ():
174
+ dp .add_data (key , value )
175
+
176
+ if write_file :
177
+ filename = f"{ source_db_name } -{ target_db_name } .json"
178
+ output_directory = setup_output_directory (output_directory )
179
+ fp = output_directory / filename
180
+ logger .info ("Writing output file {fp}" , fp = str (fp ))
181
+ return dp .to_json (fp )
182
+ else :
183
+ return dp
161
184
162
185
163
186
def generate_biosphere_mapping (
@@ -168,14 +191,18 @@ def generate_biosphere_mapping(
168
191
ecoinvent_username : Optional [str ] = None ,
169
192
ecoinvent_password : Optional [str ] = None ,
170
193
write_logs : bool = True ,
194
+ write_file : bool = True ,
171
195
licenses : Optional [List [dict ]] = None ,
172
196
output_directory : Optional [Path ] = None ,
173
- output_version : str = "1 .0.0" ,
197
+ output_version : str = "2 .0.0" ,
174
198
description : Optional [str ] = None ,
175
199
) -> Path :
176
200
"""Generate a Randonneur mapping file for biosphere edge attributes from source to target."""
177
201
configure_logs (write_logs = write_logs )
178
202
203
+ source_db_name = f"ecoinvent-{ source_version } -biosphere"
204
+ target_db_name = f"ecoinvent-{ target_version } -biosphere"
205
+
179
206
logger .info (
180
207
"""The `EE Deletions` format is not consistent across versions.
181
208
Please check the outputs carefully before applying them."""
@@ -212,12 +239,29 @@ def generate_biosphere_mapping(
212
239
keep_deletions = keep_deletions ,
213
240
)
214
241
215
- return write_datapackage (
216
- data = data ,
217
- source_db_name = f"ecoinvent-{ source_version } -biosphere" ,
218
- target_db_name = f"ecoinvent-{ target_version } -biosphere" ,
219
- licenses = licenses ,
220
- output_directory = output_directory ,
221
- version = output_version ,
242
+ dp = Datapackage (
243
+ name = f"{ source_db_name } -{ target_db_name } " ,
222
244
description = description ,
245
+ contributors = [
246
+ {"title" : "ecoinvent association" , "path" : "https://ecoinvent.org/" , "role" : "author" },
247
+ {"title" : "Chris Mutel" , "path" : "https://chris.mutel.org/" , "role" : "wrangler" },
248
+ ],
249
+ mapping_source = MappingConstants .ECOSPOLD2_BIO ,
250
+ mapping_target = MappingConstants .ECOSPOLD2_BIO ,
251
+ homepage = "https://github.com/brightway-lca/ecoinvent_migrate" ,
252
+ version = "2.0.0" ,
253
+ source_id = source_db_name ,
254
+ target_id = target_db_name ,
255
+ licenses = licenses ,
223
256
)
257
+ for key , value in data .items ():
258
+ dp .add_data (key , value )
259
+
260
+ if write_file :
261
+ filename = f"{ source_db_name } -{ target_db_name } .json"
262
+ output_directory = setup_output_directory (output_directory )
263
+ fp = output_directory / filename
264
+ logger .info ("Writing output file {fp}" , fp = str (fp ))
265
+ return dp .to_json (fp )
266
+ else :
267
+ return dp
0 commit comments