Skip to content

Commit

Permalink
Fix issues with Simapro export
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Jan 23, 2024
1 parent 8c629ad commit 951c644
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 71 deletions.
2 changes: 1 addition & 1 deletion carculator_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"ExportInventory",
"VehicleInputParameters",
)
__version__ = (1, 2, 0, "dev2")
__version__ = (1, 2, 0, "dev3")

from pathlib import Path

Expand Down
22 changes: 21 additions & 1 deletion carculator_utils/data/export/simapro-biosphere.json
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,11 @@
"Copper, Cu 5.2E-2%, Pt 4.8E-4%, Pd 2.0E-4%, Rh 2.4E-5%, Ni 3.7E-2% in ore",
"Copper, in ground"
],
[
"natural resource",
"Copper, Cu 5.2E-2%, Pt 4.8E-4%, Pd 2.0E-4%, Rh 2.4E-5%, Ni 3.7E-2% in ore",
"Copper"
],
[
"natural resource",
"Diatomite",
Expand Down Expand Up @@ -914,11 +919,16 @@
"Nickel, Ni 3.7E-2%, Pt 4.8E-4%, Pd 2.0E-4%, Rh 2.4E-5%, Cu 5.2E-2% in ore",
"Ni, Ni 3.7E-2%, Pt 4.8E-4%, Pd 2.0E-4%, Rh 2.4E-5%, Cu 5.2E-2% in ore, in ground"
],
[
[
"natural resource",
"Occupation, construction site",
"Occupation, construction site"
],
[
"natural resource",
"Occupation, unspecified",
"Occupation, unspecified"
],
[
"natural resource",
"Occupation, arable",
Expand Down Expand Up @@ -1494,6 +1504,16 @@
"Transformation, to traffic area, rail embankment",
"Transformation, to traffic area, rail/road embankment"
],
[
"natural resource",
"Transformation, to traffic area, rail network",
"Transformation, to traffic area, rail network"
],
[
"natural resource",
"Transformation, to traffic area, road network",
"Transformation, to traffic area, road network"
],
[
"natural resource",
"Transformation, to unknown",
Expand Down
2 changes: 2 additions & 0 deletions carculator_utils/data/export/simapro_fields.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ unit:
vehicle-kilometer: vkm
person-kilometer: personkm
meter-year: my
kilo Becquerel: kBq
hectare: ha

headers:
- "{SimaPro 9.1.1.1}"
Expand Down
121 changes: 53 additions & 68 deletions carculator_utils/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,38 +499,25 @@ def format_data_for_lci_for_simapro(
# We loop through the activities
for a in data:
# We fetch the main and sub categories (sub category is in fact a path)
comment, source = None, None
if a["name"] in self.references:
main_category = self.references[a["name"]]["category 1"]
category = self.references[a["name"]]["category 2"]
source = self.references[a["name"]]["source"]
description = self.references[a["name"]]["description"]
special_remark = self.references[a["name"]]["special remark"]
else:
# if we cannot find it, it's because some keys are more general
try:
key = [
k
for k in self.references.keys()
if k.lower() in a["name"].lower()
][0]
except IndexError:
if self.vm.vehicle_type in a["name"].lower():
pass
else:
print(a["name"])
main_category = self.references.get(key, {"category 1": None}).get(
"category 1"
)
category = self.references.get(key, {"category 2": None}).get(
"category 2"
)
source = self.references.get(key, {"source": None}).get("source")
description = self.references.get(key, {"description": None}).get(
"description"
)
special_remark = self.references.get(key, {"special remark": None}).get(
"special remark"
source = self.references.get(a["name"]).get("source")
comment = self.references.get(a["name"]).get("comment")

main_category = "waste treatment" if any(
i.lower() in a["name"].lower()
for i in (
"waste",
"emissions",
"treatment",
"scrap",
"used powertrain",
"disposal",
"sludge",
"used li-ion",
)
) else "process"
category = "carculator"

# We loop through the fields SimaPro expects to see
for item in fields["fields"]:
Expand All @@ -553,14 +540,10 @@ def format_data_for_lci_for_simapro(

if item == "Comment":
string = ""
if a["comment"]:
if comment is not None:
string = f"{a['comment']}. "

string += f"Originally published in: {source}. "
if description:
string += f"Description: {description}. "
if special_remark:
string += f"Special remark: {special_remark}. "

rows.append([string])

Expand Down Expand Up @@ -733,7 +716,7 @@ def format_data_for_lci_for_simapro(
if e["name"] not in blacklist:
rows.append(
[
dict_bio[e["name"]],
dict_bio.get(e["name"], e["name"]),
"",
fields["unit"][e["unit"]],
"{:.3E}".format(e["amount"]),
Expand Down Expand Up @@ -1001,43 +984,45 @@ def write_simapro_lci(
filename: str = None,
export_format: str = "file",
):
filename = filename or safe_filename(
f"carculator_export_{datetime.date.today()}"
)

filename += "_simapro.csv"
for year in self.vm.array.coords["year"].values:
filename = filename or safe_filename(
f"carculator_export_{datetime.date.today()}"
)

filepath_export = self.get_export_filepath(filename, directory)
filename += f"_{year}_simapro.csv"

list_act = self.write_lci(
ecoinvent_version=ecoinvent_version,
)
filepath_export = self.get_export_filepath(filename, directory)

rows = self.format_data_for_lci_for_simapro(
data=list_act, ei_version=ecoinvent_version
)
list_act = self.write_lci(
ecoinvent_version=ecoinvent_version,
year=year,
)

if export_format == "file":
with open(filepath_export, "w", newline="", encoding="latin1") as csvFile:
writer = csv.writer(csvFile, delimiter=";")
for row in rows:
writer.writerow(row)
csvFile.close()
return filepath_export
rows = self.format_data_for_lci_for_simapro(
data=list_act, ei_version=ecoinvent_version
)

# string format
csvFile = io.StringIO()
writer = csv.writer(
csvFile,
delimiter=";",
quoting=csv.QUOTE_NONE,
quotechar="",
escapechar="\\",
)
for row in rows:
writer.writerow(row)
csvFile.seek(0)
return csvFile.read()
if export_format == "file":
with open(filepath_export, "w", newline="", encoding="utf8") as csvFile:
writer = csv.writer(csvFile, delimiter=";")
for row in rows:
writer.writerow(row)
csvFile.close()
return filepath_export

# string format
csvFile = io.StringIO()
writer = csv.writer(
csvFile,
delimiter=";",
quoting=csv.QUOTE_NONE,
quotechar="",
escapechar="\\",
)
for row in rows:
writer.writerow(row)
csvFile.seek(0)
return csvFile.read()

def write_bw2_lci(
self,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def package_files(directory):

setup(
name="carculator_utils",
version="1.2.0.dev2",
version="1.2.0.dev3",
packages=packages,
author="Romain Sacchi <romain.sacchi@psi.ch>",
python_requires=">=3.9",
Expand Down

0 comments on commit 951c644

Please sign in to comment.