Chris Mutel avatar Chris Mutel committed 3380cbc

Add parameter finder class

Comments (0)

Files changed (1)

bw2analyzer/lookup.py

+# -*- coding: utf-8 -*
+from bw2data import Database, Method
+from bw2calc import LCA
+
+
+class ParameterFinder(object):
+    """Convenience class to find data about particular parameters, i.e. technosphere and biosphere exchanges and characterization factors."""
+    def __init__(self, lca):
+        assert isinstance(lca, LCA), "Must provide ``LCA`` object as input"
+        self.lca = lca
+        self.lca.fix_dictionaries()
+        self.rt, self.rb = self.lca.reverse_dict()
+
+    def find_technosphere(self, row, col):
+        inp = self.rt[row]
+        inp_data = Database(inp[0]).load()[inp]
+        outp = self.rt[col]
+        outp_data = Database(outp[0]).load()[outp]
+        try:
+            exc = [x for x in outp_data['exchanges'] if x['input'] == inp][0]
+        except IndexError:
+            raise ValueError("Can't find this exchange")
+        return {
+            'input': {
+                'key': inp,
+                'data': inp_data
+            },
+            'output': {
+                'key': outp,
+                'data': outp_data
+            },
+            'exchange': exc
+        }
+
+    def find_biosphere(self, row, col):
+        inp = self.rb[row]
+        inp_data = Database(inp[0]).load()[inp]
+        outp = self.rt[col]
+        outp_data = Database(outp[0]).load()[outp]
+        try:
+            exc = [x for x in outp_data['exchanges'] if x['input'] == inp][0]
+        except IndexError:
+            raise ValueError("Can't find this exchange")
+        return {
+            'input': {
+                'key': inp,
+                'data': inp_data
+            },
+            'output': {
+                'key': outp,
+                'data': outp_data
+            },
+            'exchange': exc
+        }
+
+    def find_characterization(self, row):
+        # Doesn't work for regionalized LCIA methods
+        flow = self.rb[row]
+        flow_data = Database(flow[0]).load()[flow]
+        method = Method(self.lca.method)
+        try:
+            cf = [x for x in method.load() if x[0] == flow][0][1]
+        except:
+            raise ValueError("Can't find this CF")
+        return {
+            'flow': {
+                'key': flow,
+                'data': flow_data
+            },
+            'cf': cf
+        }
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.