{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Importing a SimaPro CSV that includes ecoinvent\n",
    "\n",
    "This notebook shows how to import a SimaPro CSV export that includes ecoinvent 2.2. We will use a custom strategy to remove the ecoinvent processes, and then link to the ecoinvent already present on our machine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from brightway2 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic setup\n",
    "\n",
    "Start a new project, and install base data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "projects.set_current(\"simapro-ecoinvent-import\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating default biosphere\n",
      "\n",
      "Applying strategy: drop_unspecified_subcategories"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Writing activities to SQLite3 database:\n",
      "0%                          100%\n",
      "[##############################] | ETA[sec]: 0.000 \n",
      "Total time elapsed: 0.666 sec\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Title: Writing activities to SQLite3 database:\n",
      "  Started: 05/19/2015 08:46:17\n",
      "  Finished: 05/19/2015 08:46:17\n",
      "  Total time elapsed: 0.666 sec\n",
      "  CPU %: 91.200000\n",
      "  Memory %: 1.037788\n",
      "Created database: biosphere3\n",
      "Creating default LCIA methods\n",
      "\n",
      "Applying strategy: set_biosphere_type\n",
      "Applying strategy: drop_unspecified_subcategories\n",
      "Applying strategy: link_iterable_by_fields\n",
      "Wrote 692 LCIA methods with 170915 characterization factors\n",
      "Creating core data migrations\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bw2setup()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import ecoinvent 2.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Extracting ecospold1 files:\n",
      "0%                          100%\n",
      "[##############################] | ETA[sec]: 0.000 | Item ID: /Users/cmutel/D\n",
      "Total time elapsed: 12.012 sec\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Extracting ecospold1 files:\n",
      "  Started: 05/19/2015 08:47:38\n",
      "  Finished: 05/19/2015 08:47:50\n",
      "  Total time elapsed: 12.012 sec\n",
      "  CPU %: 87.100000\n",
      "  Memory %: 2.893019\n",
      "Extracted 4087 datasets in 12.18 seconds\n",
      "Applying strategy: assign_only_product_as_production\n",
      "Applying strategy: clean_integer_codes\n",
      "Applying strategy: drop_unspecified_subcategories\n",
      "Applying strategy: normalize_biosphere_categories\n",
      "Applying strategy: normalize_biosphere_names\n",
      "Applying strategy: strip_biosphere_exc_locations\n",
      "Applying strategy: set_code_by_activity_hash\n",
      "Applying strategy: link_iterable_by_fields\n",
      "Applying strategy: link_technosphere_by_activity_hash"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Writing activities to SQLite3 database:\n",
      "0%                          100%\n",
      "[##############################] | ETA[sec]: 0.000 \n",
      "Total time elapsed: 20.534 sec\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Title: Writing activities to SQLite3 database:\n",
      "  Started: 05/19/2015 08:47:55\n",
      "  Finished: 05/19/2015 08:48:15\n",
      "  Total time elapsed: 20.534 sec\n",
      "  CPU %: 86.500000\n",
      "  Memory %: 3.013086\n",
      "Created database: ecoinvent 2.2\n"
     ]
    }
   ],
   "source": [
    "ei22 = SingleOutputEcospold1Importer(\n",
    "    \"/Users/cmutel/Documents/LCA Documents/Ecoinvent/2.2/processes\",\n",
    "    \"ecoinvent 2.2\"\n",
    ")\n",
    "ei22.apply_strategies()\n",
    "ei22.write_database()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load SimaPro CSV export"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NameError\n",
      "   1000/_NCV/(1+_Percent_humidity_u)*_C_ratio*_CO2_conversion\n",
      "                                      ^^^\n",
      "name '_C_ratio' is not defined\n",
      "NameError\n",
      "   _Furniture_lifetime_equivalence*_furniture_elasticity\n",
      "name '_Furniture_lifetime_equivalence' is not defined\n",
      "NameError\n",
      "   _Furniture_weight_ppbox\n",
      "name '_Furniture_weight_ppbox' is not defined\n",
      "NameError\n",
      "   _Furniture_weight_metalbox\n",
      "name '_Furniture_weight_metalbox' is not defined\n",
      "NameError\n",
      "   (_Furniture_PPbox_PP)/0.9\n",
      "     ^^^\n",
      "name '_Furniture_PPbox_PP' is not defined\n",
      "NameError\n",
      "   (_Furniture_PPbox_Isorel)/0.9\n",
      "     ^^^\n",
      "name '_Furniture_PPbox_Isorel' is not defined\n",
      "NameError\n",
      "   _Furniture_PPbox_Melamine\n",
      "name '_Furniture_PPbox_Melamine' is not defined\n",
      "NameError\n",
      "   _Furniture_PPbox_ABS\n",
      "name '_Furniture_PPbox_ABS' is not defined\n",
      "NameError\n",
      "   _Furniture_PPbox_steel\n",
      "name '_Furniture_PPbox_steel' is not defined\n",
      "NameError\n",
      "   _Furniture_PPbox_steel\n",
      "name '_Furniture_PPbox_steel' is not defined\n",
      "NameError\n",
      "   (_Furniture_PPbox_PP*_G_PP_Density+_Furniture_PPbox_Isorel*_G_Hardboard_density)/0.9-(_Furniture_PPbox_PP*_G_PP_Density+_Furniture_PPbox_Isorel*_G_Hardboard_density)\n",
      "     ^^^\n",
      "name '_Furniture_PPbox_PP' is not defined\n",
      "NameError\n",
      "   _EU_Steel_REcycling\n",
      "name '_EU_Steel_REcycling' is not defined\n",
      "NameError\n",
      "   1/(1+0.2)*(0.6-0.2)*1.4*3.6/0.9*_Eu_Per_Gas\n",
      "                                    ^^^\n",
      "name '_Eu_Per_Gas' is not defined\n"
     ]
    },
    {
     "ename": "CapitalizationError",
     "evalue": "Possible errors in upper/lower case letters for some parameters.\n'_per_load'' not found; could be '_per_load'\n'_EU_conso_semi_min_l_km'' not found; could be '_EU_Conso_semi_min_l_km'\n'_EU_conso_semi_max_l_km'' not found; could be '_EU_Conso_semi_max_l_km'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mCapitalizationError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-75468c1fb3f4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSimaProCSVImporter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"/Users/cmutel/Downloads/Furniture particle board and hardboard.CSV\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"particle-board\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0msp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatistics\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/cmutel/local34/bw3dev/lib/python3.4/site-packages/bw2io/importers/simapro_csv.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, filepath, name, delimiter, encoding, normalize_biosphere, biosphere_db)\u001b[0m\n\u001b[1;32m     37\u001b[0m                 \u001b[0mdelimiter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdelimiter\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     38\u001b[0m                 \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 39\u001b[0;31m                 \u001b[0mencoding\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mencoding\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     40\u001b[0m             )\n\u001b[1;32m     41\u001b[0m         print(u\"Extracted {} unallocated datasets in {:.2f} seconds\".format(\n",
      "\u001b[0;32m/Users/cmutel/local34/bw3dev/lib/python3.4/site-packages/bw2io/extractors/simapro_csv.py\u001b[0m in \u001b[0;36mextract\u001b[0;34m(cls, filepath, delimiter, name, encoding)\u001b[0m\n\u001b[1;32m    102\u001b[0m                     \u001b[0mfilepath\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    103\u001b[0m                     \u001b[0mglobal_parameters\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 104\u001b[0;31m                     \u001b[0mproject_metadata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    105\u001b[0m                 )\n\u001b[1;32m    106\u001b[0m                 \u001b[0mdatasets\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/cmutel/local34/bw3dev/lib/python3.4/site-packages/bw2io/extractors/simapro_csv.py\u001b[0m in \u001b[0;36mread_data_set\u001b[0;34m(cls, data, index, db_name, filepath, gp, pm)\u001b[0m\n\u001b[1;32m    512\u001b[0m         ps = ParameterSet(\n\u001b[1;32m    513\u001b[0m             \u001b[0mds\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'parameters'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 514\u001b[0;31m             \u001b[0;34m{\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'amount'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    515\u001b[0m         )\n\u001b[1;32m    516\u001b[0m         \u001b[0;31m# Changes in-place\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/cmutel/local34/bw3dev/lib/python3.4/site-packages/bw2parameters/parameter_set.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, params, global_params)\u001b[0m\n\u001b[1;32m     21\u001b[0m                 )\n\u001b[1;32m     22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0morder\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_order\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mget_order\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/cmutel/local34/bw3dev/lib/python3.4/site-packages/bw2parameters/parameter_set.py\u001b[0m in \u001b[0;36mget_order\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     48\u001b[0m                         u\"\\n\".join([\n\u001b[1;32m     49\u001b[0m                             \u001b[0;34mu\"'{}'' not found; could be '{}'\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 50\u001b[0;31m                             \u001b[0;32mfor\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mwrong_case\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     51\u001b[0m                         ])\n\u001b[1;32m     52\u001b[0m                     )\n",
      "\u001b[0;31mCapitalizationError\u001b[0m: Possible errors in upper/lower case letters for some parameters.\n'_per_load'' not found; could be '_per_load'\n'_EU_conso_semi_min_l_km'' not found; could be '_EU_Conso_semi_min_l_km'\n'_EU_conso_semi_max_l_km'' not found; could be '_EU_Conso_semi_max_l_km'"
     ]
    }
   ],
   "source": [
    "sp = SimaProCSVImporter(\"/Users/cmutel/Downloads/Furniture particle board and hardboard.CSV\", \"particle-board\")\n",
    "sp.statistics()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "OK, the SimaPro export file has several cases where the parameter names were incorrect - SimaPro appears to ignore the difference between upper and lower case for parameter names, so for SimaPro \"Foo\" and \"foo\" are the same thing, which raises errors for our parameter parser. These have to be fixed manually in the CSV file. Basically, fix the errors that come up until the code runs without any errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracted 2045 unallocated datasets in 13.11 seconds\n",
      "2045 datasets\n",
      "85048 exchanges\n",
      "85048 unlinked exchanges\n",
      "  Type biosphere: 1798 unique unlinked exchanges\n",
      "  Type production: 2045 unique unlinked exchanges\n",
      "  Type substitution: 8 unique unlinked exchanges\n",
      "  Type technosphere: 2075 unique unlinked exchanges\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(2045, 85048, 85048)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp = SimaProCSVImporter(\"/Users/cmutel/Downloads/Furniture particle board and hardboard-fixed.CSV\", \"particle-board\")\n",
    "sp.statistics()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit conversions\n",
    "\n",
    "To match against ecoinvent using the ``unit`` field, we need to have everything in the right units. We accomplish this by applying a migration that converts units to ecoinvent defaults."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying strategy: migrate_datasets\n",
      "Applying strategy: migrate_exchanges\n"
     ]
    }
   ],
   "source": [
    "sp.migrate('default-units')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Strip ecoinvent processes\n",
    "\n",
    "We are lucky in this case, as we know that the version of ecoinvent was not modified. If there were modifications, we would have to include the whole modified database. No software that I know of, including Brightway2, will keep a log of the modified processes after a database import, though this is certainly on our radar. But at least in this simple case, we can just delete the ecoinvent processes.\n",
    "\n",
    "To do this, we use the most common technique in the IO library - matching based on a certain number of attributes. In this case, because we are importing from SimaPro, the categories will not match ecoinvent categories. However, at least for version 2.2, the names, units, and locations will (after we normalize things, and ignoring SimaPro changes to capitalization).\n",
    "\n",
    "First, however, we need to apply some basic strategies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<function bw2io.strategies.generic.assign_only_product_as_production>,\n",
       " <function bw2io.strategies.biosphere.drop_unspecified_subcategories>,\n",
       " <function bw2io.strategies.simapro.sp_allocate_products>,\n",
       " <function bw2io.strategies.simapro.split_simapro_name_geo>,\n",
       " <function bw2io.strategies.biosphere.strip_biosphere_exc_locations>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.strategies[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying strategy: assign_only_product_as_production\n",
      "Applying strategy: drop_unspecified_subcategories\n",
      "Applying strategy: sp_allocate_products\n",
      "Applying strategy: split_simapro_name_geo\n",
      "Applying strategy: strip_biosphere_exc_locations\n"
     ]
    }
   ],
   "source": [
    "sp.apply_strategies(sp.strategies[:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can try to find Ecoinvent datasets, and remove them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from bw2io.utils import activity_hash\n",
    "\n",
    "def match_by_activity_hash(data):\n",
    "    ei22 = Database(\"ecoinvent 2.2\")\n",
    "    fields = [\"name\", \"unit\", \"location\"]\n",
    "    ei22_hashes = {activity_hash(ds, fields=fields) for ds in ei22}\n",
    "    assert len(ei22) == len(ei22_hashes)\n",
    "    return [obj for obj in data if activity_hash(obj, fields=fields) not in ei22_hashes]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying strategy: match_by_activity_hash\n",
      "110 datasets\n",
      "14992 exchanges\n",
      "14992 unlinked exchanges\n",
      "  Type biosphere: 1500 unique unlinked exchanges\n",
      "  Type production: 110 unique unlinked exchanges\n",
      "  Type substitution: 8 unique unlinked exchanges\n",
      "  Type technosphere: 232 unique unlinked exchanges\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(110, 14992, 14992)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.apply_strategy(match_by_activity_hash)\n",
    "sp.statistics()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Internal and external linking\n",
    "\n",
    "First, construct internal links within the database:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying strategy: link_technosphere_based_on_name_unit_location\n"
     ]
    }
   ],
   "source": [
    "sp.apply_strategy(sp.strategies[5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, link biosphere flows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying strategy: normalize_biosphere_categories\n",
      "Applying strategy: normalize_simapro_biosphere_categories\n",
      "Applying strategy: normalize_biosphere_names\n",
      "Applying strategy: normalize_simapro_biosphere_names\n",
      "Applying strategy: link_iterable_by_fields\n"
     ]
    }
   ],
   "source": [
    "sp.apply_strategies(sp.strategies[6:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, link to ecoinvent 2.2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Applying strategy: link_technosphere_based_on_name_unit_location\n",
      "110 datasets\n",
      "14992 exchanges\n",
      "238 unlinked exchanges\n",
      "  Type biosphere: 139 unique unlinked exchanges\n",
      "  Type technosphere: 28 unique unlinked exchanges\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(110, 14992, 238)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.match_database(\"ecoinvent 2.2\", ignore_categories=True)\n",
    "sp.statistics()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is fairly typical - there are some flows specific to SimaPro that we can't match perfectly yet. Let's look at what we still have to do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Clay kilogram ('natural resource', 'in ground')\n",
      "Water, unspecified natural origin/m3 cubic meter ('natural resource', 'in water')\n",
      "Mineral oil kilogram ('soil',)\n",
      "Mineral oil kilogram ('soil', 'forestry')\n",
      "Particulates kilogram ('air',)\n",
      "_Exported electricity kilowatt hour ('Final waste flows',)\n",
      "_Exported heat megajoule ('Final waste flows',)\n",
      "Barite, 15% in crude ore, in ground kilogram ('natural resource',)\n",
      "Bauxite kilogram ('natural resource',)\n",
      "Clay, bentonite, in ground kilogram ('natural resource',)\n",
      "Anhydrite, in ground kilogram ('natural resource',)\n",
      "Chromium, 25.5% in chromite, 11.6% in crude ore, in ground kilogram ('natural resource',)\n",
      "Clay kilogram ('natural resource',)\n",
      "Coal, hard, unspecified, in ground kilogram ('natural resource',)\n",
      "Copper, 0.99% in sulfide, Cu 0.36% and Mo 8.2E-3% in crude ore, in ground kilogram ('natural resource',)\n",
      "Gravel, in ground kilogram ('natural resource',)\n",
      "Iron, 46% in ore, 25% in crude ore, in ground kilogram ('natural resource',)\n",
      "TEAM_(r) Iron Sulphate (FeSO4, ore) kilogram ('natural resource',)\n",
      "Lead, 5.0% in sulfide, Pb 3.0%, Zn, Ag, Cd, In, in ground kilogram ('natural resource',)\n",
      "Coal, brown, in ground kilogram ('natural resource',)\n",
      "Calcite, in ground kilogram ('natural resource',)\n",
      "Manganese, 35.7% in sedimentary deposit, 14.2% in crude ore, in ground kilogram ('natural resource',)\n"
     ]
    }
   ],
   "source": [
    "for i, e in enumerate(sp.unlinked):\n",
    "    print(e['name'], e['unit'], e['categories'])\n",
    "    if i > 20:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Write excel sheet of what is left\n",
    "\n",
    "Let's write an excel sheet and get a broader picture of what is left to do."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wrote matching file to:\n",
      "/Users/cmutel/Library/Application Support/Brightway3/simapro-ecoinvent-import.9cd6edad52b89dcde4f02f36f9e9e106/export/db-matching-particle-board.xlsx\n"
     ]
    }
   ],
   "source": [
    "sp.write_excel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
