Commits

Chris Mutel committed 32f49d2

SimaPro import: Add ability to overwrite, specify database name, and location

Comments (0)

Files changed (1)

bw2data/io/import_simapro.py

 import csv
 import pprint
 import re
+import warnings
 
 
 detoxify_pattern = '/[A-Z]{2,10} [SU]$'
 
 
 class SimaProImporter(object):
-    def __init__(self, filepath, delimiter="\t", depends=['ecoinvent 2.2']):
+    def __init__(self, filepath, delimiter="\t", depends=['ecoinvent 2.2'], overwrite=False, name=None, geo="GLO"):
         self.filepath = filepath
         self.delimiter = delimiter
         self.depends = depends
+        self.overwrite = overwrite
+        self.name = name
+        self.geo = geo
 
     def importer(self):
         raw_data = self.load_file()
         name, data = self.clean_data(raw_data)
-        assert name not in databases, "Already imported"
-        database = Database(name)
+        if self.name:
+            name = self.name
         data = [self.process_data(obj) for obj in data]
-        database.register(
-            format=raw_data[0][0],
-            depends=self.depends,
-            num_processes=len(data)
-        )
+        if not self.overwrite:
+            assert name not in databases, "Already imported this project"
+            with warnings.catch_warnings():
+                warnings.simplefilter("ignore")
+                database = Database(name)
+                database.register(
+                    format=raw_data[0][0],
+                    depends=self.depends,
+                    num_processes=len(data)
+                )
+        else:
+            database = Database(name)
         database.write(dict([(obj['code'], obj) for obj in data]))
         database.process()
 
     def process_data(self, data):
         data['code'] = (self.db_name, activity_hash(data))
+        data['location'] = self.geo
         data['exchanges'] = self.link_exchanges(data['exchanges']) + [
             self.production_exchange(data)]
         return data
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.