From 7fd76217f6008d5791417bbfc3d22f263f75c50b Mon Sep 17 00:00:00 2001 From: romainsacchi Date: Thu, 12 Oct 2023 14:56:15 +0200 Subject: [PATCH] Fix reading CSV files in custom scenarios. --- dev/test_custom_scenarios.ipynb | 599 +++++++------------------------- premise/data_collection.py | 4 +- premise/inventory_imports.py | 27 +- 3 files changed, 143 insertions(+), 487 deletions(-) diff --git a/dev/test_custom_scenarios.ipynb b/dev/test_custom_scenarios.ipynb index 8a012132..fb1559a1 100644 --- a/dev/test_custom_scenarios.ipynb +++ b/dev/test_custom_scenarios.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "80ac6f31-2713-42ee-8929-5525347b2a4b", "metadata": {}, "outputs": [], @@ -13,13 +13,13 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 12, "id": "68f5574a-4334-4d99-b18e-076ae3b4de79", "metadata": {}, "outputs": [], "source": [ - "import bw2data as bw\n", - "bw.projects.set_current(\"ei39\")" + "import bw2io, bw2data, bw2calc\n", + "bw2data.projects.set_current(\"ei39\")" ] }, { @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "943e4ba8-edc7-48ac-8c91-0aeda924b2c2", "metadata": {}, "outputs": [ @@ -48,7 +48,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "premise v.(1, 7, 4)\n", + "premise v.(1, 7, 8)\n", "+------------------------------------------------------------------+\n", "| Warning |\n", "+------------------------------------------------------------------+\n", @@ -97,173 +97,6 @@ "`update_all()` will skip the following steps:\n", "update_two_wheelers(), update_cars(), and update_buses()\n", "If you want to update these steps, please run them separately afterwards.\n", - "Extracted 1 worksheets in 7.93 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 6.03 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 6.83 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 6.55 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "Log file of exchanges saved under /Users/romain/GitHub/premise/premise/data/logs.\n", - "Done!\n", - "\n", - "One or multiple duplicates detected. Removing them...\n", - "One or multiple duplicates detected. Removing them...\n", - "Building scenario factor file...\n", - "Number of new activities: 19598\n", - "Data package saved at /Users/romain/GitHub/premise/dev/export/datapackage/energy-perspective-2050-switzerland.zip\n", - "Generate scenario report.\n", - "Report saved under /Users/romain/GitHub/premise/dev/export/scenario_report.\n", - "Generate change report.\n", - "Report saved under /Users/romain/GitHub/premise/dev.\n", - "premise v.(1, 7, 4)\n", - "+------------------------------------------------------------------+\n", - "| Warning |\n", - "+------------------------------------------------------------------+\n", - "| Because some of the scenarios can yield LCI databases |\n", - "| containing net negative emission technologies (NET), |\n", - "| it is advised to account for biogenic CO2 flows when calculating |\n", - "| Global Warming potential indicators. |\n", - "| `premise_gwp` provides characterization factors for such flows. |\n", - "| It also provides factors for hydrogen emissions to air. |\n", - "| |\n", - "| Within your bw2 project: |\n", - "| from premise_gwp import add_premise_gwp |\n", - "| add_premise_gwp() |\n", - "+------------------------------------------------------------------+\n", - "+--------------------------------+----------------------------------+\n", - "| Utils functions | Description |\n", - "+--------------------------------+----------------------------------+\n", - "| clear_cache() | Clears the cache folder. Useful |\n", - "| | when updating `premise`or |\n", - "| | encountering issues with |\n", - "| | inventories. |\n", - "+--------------------------------+----------------------------------+\n", - "| get_regions_definition(model) | Retrieves the list of countries |\n", - "| | for each region of the model. |\n", - "+--------------------------------+----------------------------------+\n", - "| ndb.NewDatabase(...) | Generates a summary of the most |\n", - "| ndb.generate_scenario_report() | important scenarios' variables. |\n", - "+--------------------------------+----------------------------------+\n", - "Keep uncertainty data?\n", - "NewDatabase(..., keep_uncertainty_data=True)\n", - "\n", - "Disable multiprocessing?\n", - "NewDatabase(..., use_multiprocessing=False)\n", - "\n", - "Hide these messages?\n", - "NewDatabase(..., quiet=True)\n", - "\n", - "//////////////////// EXTRACTING SOURCE DATABASE ////////////////////\n", - "Done!\n", - "\n", - "////////////////// IMPORTING DEFAULT INVENTORIES ///////////////////\n", - "Done!\n", - "\n", - "/////////////////////// EXTRACTING IAM DATA ////////////////////////\n", - "Done!\n", - "`update_all()` will skip the following steps:\n", - "update_two_wheelers(), update_cars(), and update_buses()\n", - "If you want to update these steps, please run them separately afterwards.\n", - "Extracted 1 worksheets in 6.45 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", "Extracted 1 worksheets in 5.37 seconds\n", "Migrating to 3.8 first\n", "Applying strategy: migrate_datasets\n", @@ -274,154 +107,7 @@ "Applying strategy: migrate_exchanges\n", "Done!\n", "Done!\n", - "Extracted 1 worksheets in 6.83 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 6.22 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "Log file of exchanges saved under /Users/romain/GitHub/premise/premise/data/logs.\n", - "Done!\n", - "\n", - "One or multiple duplicates detected. Removing them...\n", - "One or multiple duplicates detected. Removing them...\n", - "Building scenario factor file...\n", - "Number of new activities: 19559\n", - "Data package saved at /Users/romain/GitHub/premise/dev/export/datapackage/cobalt-perspective-2050.zip\n", - "Generate scenario report.\n", - "Report saved under /Users/romain/GitHub/premise/dev/export/scenario_report.\n", - "Generate change report.\n", - "Report saved under /Users/romain/GitHub/premise/dev.\n", - "premise v.(1, 7, 4)\n", - "+------------------------------------------------------------------+\n", - "| Warning |\n", - "+------------------------------------------------------------------+\n", - "| Because some of the scenarios can yield LCI databases |\n", - "| containing net negative emission technologies (NET), |\n", - "| it is advised to account for biogenic CO2 flows when calculating |\n", - "| Global Warming potential indicators. |\n", - "| `premise_gwp` provides characterization factors for such flows. |\n", - "| It also provides factors for hydrogen emissions to air. |\n", - "| |\n", - "| Within your bw2 project: |\n", - "| from premise_gwp import add_premise_gwp |\n", - "| add_premise_gwp() |\n", - "+------------------------------------------------------------------+\n", - "+--------------------------------+----------------------------------+\n", - "| Utils functions | Description |\n", - "+--------------------------------+----------------------------------+\n", - "| clear_cache() | Clears the cache folder. Useful |\n", - "| | when updating `premise`or |\n", - "| | encountering issues with |\n", - "| | inventories. |\n", - "+--------------------------------+----------------------------------+\n", - "| get_regions_definition(model) | Retrieves the list of countries |\n", - "| | for each region of the model. |\n", - "+--------------------------------+----------------------------------+\n", - "| ndb.NewDatabase(...) | Generates a summary of the most |\n", - "| ndb.generate_scenario_report() | important scenarios' variables. |\n", - "+--------------------------------+----------------------------------+\n", - "Keep uncertainty data?\n", - "NewDatabase(..., keep_uncertainty_data=True)\n", - "\n", - "Disable multiprocessing?\n", - "NewDatabase(..., use_multiprocessing=False)\n", - "\n", - "Hide these messages?\n", - "NewDatabase(..., quiet=True)\n", - "\n", - "//////////////////// EXTRACTING SOURCE DATABASE ////////////////////\n", - "Done!\n", - "\n", - "////////////////// IMPORTING DEFAULT INVENTORIES ///////////////////\n", - "Done!\n", - "\n", - "/////////////////////// EXTRACTING IAM DATA ////////////////////////\n", - "Done!\n", - "`update_all()` will skip the following steps:\n", - "update_two_wheelers(), update_cars(), and update_buses()\n", - "If you want to update these steps, please run them separately afterwards.\n", - "Extracted 1 worksheets in 8.82 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 7.70 seconds\n", + "Extracted 1 worksheets in 4.52 seconds\n", "Migrating to 3.8 first\n", "Applying strategy: migrate_datasets\n", "Applying strategy: migrate_exchanges\n", @@ -431,130 +117,54 @@ "Applying strategy: migrate_exchanges\n", "Done!\n", "Done!\n", - "Extracted 1 worksheets in 5.72 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", "Done!\n", - "Extracted 1 worksheets in 6.14 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 5.54 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 10.15 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 8.20 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "Extracted 1 worksheets in 7.70 seconds\n", - "Migrating to 3.8 first\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Applying strategy: migrate_datasets\n", - "Applying strategy: migrate_exchanges\n", - "Done!\n", - "Done!\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "\n", - "//////////////// IMPORTING USER-DEFINED INVENTORIES ////////////////\n", - "Extracted 1 worksheets in 0.00 seconds\n", - "Create custom markets.\n", - "Log file of exchanges saved under /Users/romain/GitHub/premise/premise/data/logs.\n", - "Done!\n", - "\n", - "One or multiple duplicates detected. Removing them...\n", - "One or multiple duplicates detected. Removing them...\n", - "Building scenario factor file...\n", - "Number of new activities: 20269\n", - "Data package saved at /Users/romain/GitHub/premise/dev/export/datapackage/ammonia-prospective-scenarios.zip\n", + "Write new database(s) to Brightway2.\n", + "One or multiple duplicates detected. Removing them...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Writing activities to SQLite3 database:\n", + "0% [##############################] 100% | ETA: 00:00:00\n", + "Total time elapsed: 00:00:31\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Title: Writing activities to SQLite3 database:\n", + " Started: 10/12/2023 13:02:12\n", + " Finished: 10/12/2023 13:02:43\n", + " Total time elapsed: 00:00:31\n", + " CPU %: 95.50\n", + " Memory %: 24.26\n", + "Created database: ecoinvent_cutoff_3.9_image_SSP2-Base_2050\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Writing activities to SQLite3 database:\n", + "0% [##############################] 100% | ETA: 00:00:00\n", + "Total time elapsed: 00:00:28\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Title: Writing activities to SQLite3 database:\n", + " Started: 10/12/2023 13:06:44\n", + " Finished: 10/12/2023 13:07:13\n", + " Total time elapsed: 00:00:28\n", + " CPU %: 94.40\n", + " Memory %: 19.19\n", + "Created database: ecoinvent_cutoff_3.9_remind_SSP2-PkBudg500_2050\n", "Generate scenario report.\n", "Report saved under /Users/romain/GitHub/premise/dev/export/scenario_report.\n", "Generate change report.\n", @@ -563,35 +173,80 @@ } ], "source": [ - "for url in urls:\n", - " scenarios = [\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2020},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2025},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2030},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2035},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2040},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2045},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2050},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2020},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2025},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2030},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2035},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2040},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2045},\n", - " {\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2050},\n", - " ]\n", - " \n", - " ndb = NewDatabase(\n", - " scenarios = scenarios, \n", - " source_db=\"ecoinvent 3.9.1 cutoff\",\n", - " source_version=\"3.9\",\n", - " key='tUePmX_S5B8ieZkkM7WUU2CnO8SmShwmAeWK9x2rTFo=',\n", - " external_scenarios=[Package(url)]\n", - " )\n", - " ndb.update_all()\n", - " #ndb.update_external_scenario()\n", - " ndb.write_datapackage(name=ndb.datapackages[0].descriptor[\"name\"])\n", - " #ndb.write_db_to_brightway(ndb.datapackages[0].descriptor[\"name\"])\n" + "from premise import *\n", + "from datapackage import Package\n", + "bw.projects.set_current(\"ei39\")\n", + "\n", + "#for url in urls:\n", + "scenarios = [\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2020},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2025},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2030},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2035},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2040},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2045},\n", + " {\"model\": \"image\", \"pathway\":\"SSP2-Base\", \"year\": 2050},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2020},\n", + " ##{\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2025},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2030},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2035},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2040},\n", + " #{\"model\": \"image\", \"pathway\":\"SSP2-RCP26\", \"year\": 2045},\n", + " {\"model\": \"remind\", \"pathway\":\"SSP2-PkBudg500\", \"year\": 2050},\n", + "]\n", + "\n", + "ndb = NewDatabase(\n", + " scenarios = scenarios, \n", + " source_db=\"ecoinvent 3.9.1 cutoff\",\n", + " source_version=\"3.9\",\n", + " key='tUePmX_S5B8ieZkkM7WUU2CnO8SmShwmAeWK9x2rTFo=',\n", + " #external_scenarios=[Package(url)]\n", + ")\n", + "ndb.update_all()\n", + "#ndb.update_external_scenario()\n", + "#ndb.write_datapackage(name=ndb.datapackages[0].descriptor[\"name\"])\n", + "ndb.write_db_to_brightway()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "90021696-37d1-481f-9cff-daa69efcdba5", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0.22982712]\n", + " [0.20379977]\n", + " [0.03638609]]\n" + ] + } + ], + "source": [ + "FU = [\n", + " {act: 1} for db in [\n", + " \"ecoinvent 3.9.1 cutoff\",\n", + " \"ecoinvent_cutoff_3.9_image_SSP2-Base_2050\",\n", + " \"ecoinvent_cutoff_3.9_remind_SSP2-PkBudg500_2050\"\n", + " ] for act in bw2data.Database(db) \n", + " if act[\"name\"]==\"transport, passenger car, electric\"\n", + " and act[\"location\"] == \"GLO\"\n", + "]\n", + "\n", + "method = [('IPCC 2021', 'climate change: fossil', 'global warming potential (GWP100)')]\n", + "\n", + "bw2calc.multi_lca.calculation_setups[\"BEVs\"] = {\n", + " \"inv\": FU,\n", + " \"ia\": method\n", + "}\n", + "\n", + "lca = bw2calc.MultiLCA(\"BEVs\")\n", + "\n", + "print(lca.results)" ] }, { diff --git a/premise/data_collection.py b/premise/data_collection.py index e31ea460..99ac98ed 100644 --- a/premise/data_collection.py +++ b/premise/data_collection.py @@ -1105,7 +1105,9 @@ def get_external_data(self, datapackages): resource = dp.get_resource("scenario_data") # getting scenario data in binary format scenario_data = resource.raw_read() - df = pd.read_csv(BytesIO(scenario_data), encoding="latin1") + df = pd.read_csv( + BytesIO(scenario_data), + ) # set headers from first row df.columns = resource.headers diff --git a/premise/inventory_imports.py b/premise/inventory_imports.py index 6b21907e..9f09448b 100644 --- a/premise/inventory_imports.py +++ b/premise/inventory_imports.py @@ -280,7 +280,7 @@ def __init__( ) self.path = Path(path) if isinstance(path, str) else path - self.import_db = self.load_inventory(path) + self.import_db = self.load_inventory() # register migration maps # as imported inventories link @@ -296,7 +296,7 @@ def __init__( description=f"Change technosphere names due to change from {combination[0]} to {combination[1]}", ) - def load_inventory(self, path: Union[str, Path]) -> None: + def load_inventory(self) -> None: """Load an inventory from a specified path. Sets the :attr:`import_db` attribute. :param str path: Path to the inventory file @@ -688,8 +688,8 @@ def __init__( database, version_in, version_out, path, system_model, keep_uncertainty_data ) - def load_inventory(self, path: Union[str, Path]) -> bw2io.ExcelImporter: - return ExcelImporter(path) + def load_inventory(self) -> bw2io.ExcelImporter: + return ExcelImporter(self.path) def prepare_inventory(self) -> None: if self.version_in != self.version_out: @@ -775,8 +775,8 @@ def __init__( self.has_fleet = has_fleet self.geo = Geomap(model=model) - def load_inventory(self, path): - return ExcelImporter(path) + def load_inventory(self): + return ExcelImporter(self.path) def prepare_inventory(self): # if version_out is 3.9, migrate towards 3.8 first, then 3.9 @@ -816,13 +816,12 @@ class AdditionalInventory(BaseInventoryImport): def __init__(self, database, version_in, version_out, path, system_model): super().__init__(database, version_in, version_out, path, system_model) - def load_inventory(self, path): - path = Path(path) + def load_inventory(self): # check if "http" in path - if "http" in str(path): + if "http" in str(self.path): # online file # we need to save it locally first - response = requests.get(path) + response = requests.get(self.path) path = DIR_CACHED_DB / "temp.csv" with open(path, "w", encoding="utf-8") as f: writer = csv.writer( @@ -835,11 +834,11 @@ def load_inventory(self, path): for line in response.iter_lines(): writer.writerow(line.decode("utf-8").split(",")) - if path.suffix == ".xlsx": - return ExcelImporter(path) + if self.path.suffix == ".xlsx": + return ExcelImporter(self.path) - elif path.suffix == ".csv": - return CSVImporter(path) + elif self.path.suffix == ".csv": + return CSVImporter(self.path) else: raise ValueError(