Created
March 16, 2017 22:31
-
-
Save cmutel/35e77f3e6902e9d383ca6f24c920e177 to your computer and use it in GitHub Desktop.
Send LCI/A data as CSVs to minimal bw2calc server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import requests | |
""" | |
This script will create a very simple life cycle inventory, and send it to the bw2calc server. | |
Example standalone bw2calc server here: https://gist.github.com/cmutel/a408e3a62f35519065cee125d7821d4d | |
The CSV schema for the inventory exchanges is: | |
producer (int), consumer (int), type (int), amount (float) | |
Note that biosphere exchanges are included here, and the biosphere flow is the producer; the emitting activity is the consumer. | |
Types are: | |
0: production (must be included for each activity) | |
1: technosphere input (positive number for consumption) | |
2: biosphere flow emission or consumption | |
The CSV schema for the impact assessment method is: | |
flow (int), amount (float) | |
""" | |
ELEC = 1 | |
COAL = 2 | |
CO2 = 3 | |
LCI = [ | |
(ELEC, ELEC, 0, 1), # Production | |
(COAL, COAL, 0, 1), # Production | |
(COAL, ELEC, 1, 0.1), | |
(CO2, COAL, 2, 2) | |
] | |
LCIA = [(CO2, 1)] | |
URL = 'http://127.0.0.1:8000/' | |
def export_lci(): | |
with open("lci.csv", "w") as f: | |
writer = csv.writer(f) | |
for row in LCI: | |
writer.writerow(row) | |
with open("lcia.csv", "w") as f: | |
writer = csv.writer(f) | |
for row in LCIA: | |
writer.writerow(row) | |
def make_request(): | |
data = {'activity': ELEC, 'amount': 1} | |
files = { | |
'lci': open('lci.csv', 'rb'), | |
'lcia': open('lcia.csv', 'rb') | |
} | |
return requests.post(URL, data=data, files=files) | |
def main(): | |
export_lci() | |
resp = make_request() | |
print(resp.json()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment