Denes Csala deleted figures/S1.ipynb  almost 10 years ago

Commit id: 8328da8b870baf9cbd1e3f0ee60716a38241cbd3

deletions | additions      

         

{  "metadata": {  "name": "",  "signature": "sha256:d4f8032be1cd08e428b774a156f2a3417b7eb4a3732330522b9802d6b2940935"  },  "nbformat": 3,  "nbformat_minor": 0,  "worksheets": [  {  "cells": [  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

The Role of Food in the Global Sustainable Energy Transition


\n",
  "Sgouris Sgouridis, Denes Csala\n",  "

Supplimentary Material S1


\n",
  "

\n",  "This workbook will guide you through the creation, visulization and intepretation of the Food Energy Flows (FEF).\n",  "

\n",
  "

Data

\n",
  "The major data sources for this work are the Food Balance Sheets of the Statistical Division (FAOSTAT) of the Food and Agriculture Organization of the United Nations (FAO). Other minor data sources will be referenced further in the document, where relevant.\n",  "

\n",
  "

Processing

\n",
  "The data processing has been done using the Anaconda distribution of Python 2.7 using the IPython Notebook editor. The *FAOSTAT* database is accessed using the open-scource requests *Python* library and the data is loaded into a pandas dataframe, which represents the backbone of the data analysis. Numerical processing is done with NumPy. The Python code outputs standardized JSON files for every years and country, which is used as the input for the web-based visualization. Hereby the authors would like to extend their gratitude to the creators of these open-source tools, if it wasn't for their existence this work would not have been possible. \n",  "

\n",
  "

Visualization

\n",
  "Using the *JSON* files generated by the *Python* script, the data is represented in a native browser visualization format using the D3.js data-oriented javascript framework. A Sankey diagram format is used, inspired by Mike Bostock's implementation, which is based on the initial version by Thomas Counsell. In addition to the *Sankey* diagram, pie charts are used for increased detail, using the NVD3 extension for *D3.js*. The data can be viewed dynamically and animated over time, using the Dragdealer.js slider.\n",  "


\n",  "All source files are freely availabe on GitHub at this repository and at the project website at http://food.csaladen.es.\n",  "

"
  ]  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Section 1

\n",
  "We start with importing the libraries that we are going to use into the *Python* workspace. The code is partitioned into section, called *cells*. If you are using this workbook in the active format, Ctrl + Enter runs the code, Shift + Enter runs the code and steps into the next cell, while Alt + Enter runs the code and adds a new, empty cell."  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "import numpy as np, requests, zipfile, StringIO, pandas as pd, json, copy"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 5  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "Afer importing the necessary libraries, we connect to the *FAOSTAT* database and define indicator paths. Alternatively, the *FAOSTAT* database is mirrored at the projects website data repository. *FAOSTAT* offers the data in the format of zipped *CSV (comma-separated values)* files, and it has a separate file for each of their 17 domains. Should you choose to download the entire database to your local folder, please set the variable localpath to True and indicate this path in the masterpath variable. If you choose to use the online resources directly, please set localpath to False and use the appropiate masterpath (*FAOSTAT* or project website), as indicated below. "  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "masterpath = 'http://faostat.fao.org/Portals/_Faostat/Downloads/zip_files/'\n",  "#masterpath = 'http://food.csaladen.es/db/' #alternative mirror for entire FAO database\n",  "localpath=True\n",  "if localpath:\n",  " #local path for downloaded FAO database, only for developer purposes\n",  " masterpath = 'E:/Skydrive/Documents/GitHub/Food-Energy/db/' \n",  " \n",  "indicators=[] #we store indicator name strings in this string array\n",  "indicators.append('CommodityBalances_Crops_E_All_Data') #0\n",  "indicators.append('CommodityBalances_LivestockFish_E_All_Data') #1\n",  "indicators.append('Emissions_Agriculture_Burning_crop_residues_E_All_Data') #2\n",  "indicators.append('Emissions_Agriculture_Energy_E_All_Data') #3\n",  "indicators.append('Emissions_Agriculture_Synthetic_Fertilizers_E_All_Data') #4\n",  "indicators.append('Environment_Energy_E_All_Data') #5\n",  "indicators.append('FoodBalanceSheets_E_All_Data') #6\n",  "indicators.append('FoodSupply_Crops_E_All_Data') #7\n",  "indicators.append('FoodSupply_LivestockFish_E_All_Data') #8\n",  "indicators.append('Population_E_All_Data') #9\n",  "indicators.append('Production_Crops_E_All_Data') #10\n",  "indicators.append('Production_CropsProcessed_E_All_Data') #11\n",  "indicators.append('Production_Livestock_E_All_Data') #12\n",  "indicators.append('Production_LivestockPrimary_E_All_Data') #13\n",  "indicators.append('Production_LivestockProcessed_E_All_Data') #14\n",  "indicators.append('Resources_Fertilizers_E_All_Data') #15\n",  "indicators.append('Resources_FertilizersArchive_E_All_Data') #16"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 9  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "After defining the indicator names, we define the function get_data([indicator index]) that loads the data from the respective *CSV* from inside the corresponding *ZIP* file into a *pandas* dataframe. This can take anywhere from a few minutes to several hours, depending on your computer's performance. If you are running this notebook live and have access to paralell computing capabilites please turn them on at the *Clusters* tab of the main Ipython window. Make sure you run the cells above first, as the get_data() function uses their output."  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "def get_data(ind):\n",  " if localpath:\n",  " r = masterpath+indicators[ind]+'.zip'\n",  " z = zipfile.ZipFile(r)\n",  " else:\n",  " r = requests.get(masterpath+indicators[ind]+'.zip')\n",  " z = zipfile.ZipFile(StringIO.StringIO(r.content))\n",  " return pd.read_csv(z.open(indicators[ind]+'.csv'))"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 3  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "For this work, we will use the *Food Balance Sheets*, the *Emissions Agriculture Energy*, the *Fertilizers* and *fertilizers Archive* and *Population* modules of *FAOSTAT*. First, let's load the food balance sheets dataset. This is the largest of the datasets, and it can take anywhere from several minutes in case of locally mirrored data, to hours (especially for the balance dataset) in case of direct online access. We display the first items of the *pandas* dataframe returned by the get_data() function."  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "balance = get_data(6)\n",  "print balance.count()\n",  "balance.head()"  ],  "language": "python",  "metadata": {},  "outputs": [  {  "output_type": "stream",  "stream": "stdout",  "text": [  "CountryCode 18539748\n",  "Country 18539748\n",  "ItemCode 18539748\n",  "Item 18539748\n",  "ElementGroup 18539748\n",  "ElementCode 18539748\n",  "Element 18539748\n",  "Year 18539748\n",  "Unit 18539748\n",  "Value 11081711\n",  "Flag 11078192\n",  "dtype: int64\n"  ]  },  {  "html": [  "
\n",  "\n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  "
CountryCodeCountryItemCodeItemElementGroupElementCodeElementYearUnitValueFlag
0 2 Afghanistan 2659 Alcohol, Non-Food 100 5301 Domestic supply quantity 1961 1000 tonnesNaN NaN
1 2 Afghanistan 2659 Alcohol, Non-Food 100 5301 Domestic supply quantity 1962 1000 tonnesNaN NaN
2 2 Afghanistan 2659 Alcohol, Non-Food 100 5301 Domestic supply quantity 1963 1000 tonnesNaN NaN
3 2 Afghanistan 2659 Alcohol, Non-Food 100 5301 Domestic supply quantity 1964 1000 tonnesNaN NaN
4 2 Afghanistan 2659 Alcohol, Non-Food 100 5301 Domestic supply quantity 1965 1000 tonnesNaN NaN
\n",
  "
"
  ],  "metadata": {},  "output_type": "pyout",  "prompt_number": 34,  "text": [  " CountryCode Country ItemCode Item ElementGroup \\\n",  "0 2 Afghanistan 2659 Alcohol, Non-Food 100 \n",  "1 2 Afghanistan 2659 Alcohol, Non-Food 100 \n",  "2 2 Afghanistan 2659 Alcohol, Non-Food 100 \n",  "3 2 Afghanistan 2659 Alcohol, Non-Food 100 \n",  "4 2 Afghanistan 2659 Alcohol, Non-Food 100 \n",  "\n",  " ElementCode Element Year Unit Value Flag \n",  "0 5301 Domestic supply quantity 1961 1000 tonnes NaN NaN \n",  "1 5301 Domestic supply quantity 1962 1000 tonnes NaN NaN \n",  "2 5301 Domestic supply quantity 1963 1000 tonnes NaN NaN \n",  "3 5301 Domestic supply quantity 1964 1000 tonnes NaN NaN \n",  "4 5301 Domestic supply quantity 1965 1000 tonnes NaN NaN "  ]  }  ],  "prompt_number": 34  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "As we can see, there are *18539748* entries in the dataset. We are interested in categorizing the food energy flows based on their destiantions, which is depicted under the *Element* column. In order to get a sense of how data is stored in the database, we filter the data to a certain country and a certain year. Then we list the number of unique entries in this column."  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "balance[((balance.Country=='Brazil')&(balance.Year==1961)&(balance.Item=='Apples and products'))]"  ],  "language": "python",  "metadata": {},  "outputs": [  {  "html": [  "
\n",  "\n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  "
CountryCodeCountryItemCodeItemElementGroupElementCodeElementYearUnitValueFlag
7378794 21 Brazil 2617 Apples and products 100 5301 Domestic supply quantity 1961 1000 tonnes 63.0 S
7378845 21 Brazil 2617 Apples and products 101 5521 Feed 1961 1000 tonnes NaN NaN
7378896 21 Brazil 2617 Apples and products 111 5527 Seed 1961 1000 tonnes NaN NaN
7378947 21 Brazil 2617 Apples and products 121 5123 Waste 1961 1000 tonnes 6.0 S
7378998 21 Brazil 2617 Apples and products 131 5131 Processing 1961 1000 tonnes NaN NaN
7379049 21 Brazil 2617 Apples and products 141 5142 Food 1961 1000 tonnes 57.0 S
7379100 21 Brazil 2617 Apples and products 151 5154 Other Util 1961 1000 tonnes NaN NaN
7379151 21 Brazil 2617 Apples and products 51 5511 Production 1961 1000 tonnes 20.0 S
7379202 21 Brazil 2617 Apples and products 61 5611 Import Quantity 1961 1000 tonnes 43.0 S
7379253 21 Brazil 2617 Apples and products 645 645 Food supply quantity (kg/capita 1961 Kg 0.8 Fc
7379304 21 Brazil 2617 Apples and products 664 664 Food supply 1961 kcal/capita/day 1.0 Fc
7379355 21 Brazil 2617 Apples and products 674 674 Protein supply quantity 1961 g/capita/day 0.0 Fc
7379406 21 Brazil 2617 Apples and products 684 684 Fat supply quantity 1961 g/capita/day 0.0 Fc
7379457 21 Brazil 2617 Apples and products 71 5072 Stock Variation 1961 1000 tonnes NaN NaN
7379508 21 Brazil 2617 Apples and products 91 5911 Export Quantity 1961 1000 tonnes 0.0 S
\n",
  "

15 rows \u00d7 11 columns

\n",
  "
"
  ],  "metadata": {},  "output_type": "pyout",  "prompt_number": 18,  "text": [  " CountryCode Country ItemCode Item ElementGroup \\\n",  "7378794 21 Brazil 2617 Apples and products 100 \n",  "7378845 21 Brazil 2617 Apples and products 101 \n",  "7378896 21 Brazil 2617 Apples and products 111 \n",  "7378947 21 Brazil 2617 Apples and products 121 \n",  "7378998 21 Brazil 2617 Apples and products 131 \n",  "7379049 21 Brazil 2617 Apples and products 141 \n",  "7379100 21 Brazil 2617 Apples and products 151 \n",  "7379151 21 Brazil 2617 Apples and products 51 \n",  "7379202 21 Brazil 2617 Apples and products 61 \n",  "7379253 21 Brazil 2617 Apples and products 645 \n",  "7379304 21 Brazil 2617 Apples and products 664 \n",  "7379355 21 Brazil 2617 Apples and products 674 \n",  "7379406 21 Brazil 2617 Apples and products 684 \n",  "7379457 21 Brazil 2617 Apples and products 71 \n",  "7379508 21 Brazil 2617 Apples and products 91 \n",  "\n",  " ElementCode Element Year Unit \\\n",  "7378794 5301 Domestic supply quantity 1961 1000 tonnes \n",  "7378845 5521 Feed 1961 1000 tonnes \n",  "7378896 5527 Seed 1961 1000 tonnes \n",  "7378947 5123 Waste 1961 1000 tonnes \n",  "7378998 5131 Processing 1961 1000 tonnes \n",  "7379049 5142 Food 1961 1000 tonnes \n",  "7379100 5154 Other Util 1961 1000 tonnes \n",  "7379151 5511 Production 1961 1000 tonnes \n",  "7379202 5611 Import Quantity 1961 1000 tonnes \n",  "7379253 645 Food supply quantity (kg/capita 1961 Kg \n",  "7379304 664 Food supply 1961 kcal/capita/day \n",  "7379355 674 Protein supply quantity 1961 g/capita/day \n",  "7379406 684 Fat supply quantity 1961 g/capita/day \n",  "7379457 5072 Stock Variation 1961 1000 tonnes \n",  "7379508 5911 Export Quantity 1961 1000 tonnes \n",  "\n",  " Value Flag \n",  "7378794 63.0 S \n",  "7378845 NaN NaN \n",  "7378896 NaN NaN \n",  "7378947 6.0 S \n",  "7378998 NaN NaN \n",  "7379049 57.0 S \n",  "7379100 NaN NaN \n",  "7379151 20.0 S \n",  "7379202 43.0 S \n",  "7379253 0.8 Fc \n",  "7379304 1.0 Fc \n",  "7379355 0.0 Fc \n",  "7379406 0.0 Fc \n",  "7379457 NaN NaN \n",  "7379508 0.0 S \n",  "\n",  "[15 rows x 11 columns]"  ]  }  ],  "prompt_number": 18  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "print set(balance.Element)"  ],  "language": "python",  "metadata": {},  "outputs": [  {  "output_type": "stream",  "stream": "stdout",  "text": [  "set(['Feed', 'Stock Variation', 'Protein supply quantity', 'Food', 'Processing', 'Other Util', 'Food supply', 'Domestic supply quantity', 'Fat supply quantity', 'Seed', 'Food supply quantity (kg/capita', 'Total Population - Both sexes', 'Waste', 'Production', 'Import Quantity', 'Export Quantity'])\n"  ]  }  ],  "prompt_number": 21  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "We repeat the operations for the *Emissions Agriculture Energy*, the *Fertilizers* and *fertilizers Archive* and *Population* modules. \n",  "(To conserve display space, we only dispaly the first 5 elements - i.e. *head* - of the dataframes.)"  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "energy = get_data(3)\n",  "print set(energy.Item) #note that for energy, the categroies that are of interest to us are stored in the Item column\n",  "energy[((energy.Country=='Brazil')&(energy.Year==1970)&(energy.Element=='Consumption in Agriculture'))].head() \n",  "#data availability is 1970-2010"  ],  "language": "python",  "metadata": {},  "outputs": [  {  "output_type": "stream",  "stream": "stdout",  "text": [  "set(['Residual fuel oil in fisheries', 'Gas-diesel oils in fisheries', 'Residual fuel oil', 'Natural gas (including LNG)', 'Energy consumed in fishery + (Total)', 'Electricity', 'Gasoline', 'Energy for power irrigation', 'Transport fuel consumed in agriculture (excl. fishery) + (Total)', 'Total Energy + (Total)', 'Gas-diesel oils', 'Liquefied petroleum gas (LPG)', 'Hard coal'])\n"  ]  },  {  "html": [  "
\n",  "\n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  "
CountryCodeCountryItemCodeItemElementGroupElementCodeElementYearUnitValueFlag
42163 21 Brazil 6807 Electricity 7218 72184 Consumption in Agriculture 1970 Terajoule 1101.60 Fc
42165 21 Brazil 6807 Electricity 7218 72182 Consumption in Agriculture 1970 million kWh 306.00 NaN
42572 21 Brazil 6811 Energy consumed in fishery + (Total) 7218 72184 Consumption in Agriculture 1970 Terajoule 7327.25 A
42902 21 Brazil 6801 Gas-diesel oils 7218 72184 Consumption in Agriculture 1970 Terajoule 1952.10 Fc
43782 21 Brazil 6804 Residual fuel oil 7218 72184 Consumption in Agriculture 1970 Terajoule 7327.25 Fc
\n",
  "
"
  ],  "metadata": {},  "output_type": "pyout",  "prompt_number": 11,  "text": [  " CountryCode Country ItemCode Item \\\n",  "42163 21 Brazil 6807 Electricity \n",  "42165 21 Brazil 6807 Electricity \n",  "42572 21 Brazil 6811 Energy consumed in fishery + (Total) \n",  "42902 21 Brazil 6801 Gas-diesel oils \n",  "43782 21 Brazil 6804 Residual fuel oil \n",  "\n",  " ElementGroup ElementCode Element Year \\\n",  "42163 7218 72184 Consumption in Agriculture 1970 \n",  "42165 7218 72182 Consumption in Agriculture 1970 \n",  "42572 7218 72184 Consumption in Agriculture 1970 \n",  "42902 7218 72184 Consumption in Agriculture 1970 \n",  "43782 7218 72184 Consumption in Agriculture 1970 \n",  "\n",  " Unit Value Flag \n",  "42163 Terajoule 1101.60 Fc \n",  "42165 million kWh 306.00 NaN \n",  "42572 Terajoule 7327.25 A \n",  "42902 Terajoule 1952.10 Fc \n",  "43782 Terajoule 7327.25 Fc "  ]  }  ],  "prompt_number": 11  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "fertilizers1 = get_data(15)\n",  "print set(fertilizers1.Item) #note that for fertilizers, the categroies that are of interest to us are stored in the Item column\n",  "fertilizers1[((fertilizers1.Country=='Brazil')&(fertilizers1.Year==2002)&(fertilizers1.Element=='Consumption'))].head() \n",  "#data availability is 2002-2011"  ],  "language": "python",  "metadata": {},  "outputs": [  {  "output_type": "stream",  "stream": "stdout",  "text": [  "set(['Superphosphate other', 'Superphosphate', 'Potassium chloride (Muriate of potash)', 'Urea', 'Ammonium sulphate', 'NPK blends', 'Phosphate rock', 'Potassium sulphate', 'Potash Fertilizers (K20 total nutrients)', 'Nitrogen Fertilizers (N total nutrients)', 'PK compounds', 'Potassium nitrate', 'Superphosphate above 35%', 'Monoammonium phosphate (MAP)', 'Other nitrogen & phosphorus compounds', 'Phosphate Fertilizers (P205 total nutrients)', 'Other nitrogen & phosphates compounds', 'Urea and ammonium nitrate solutions', 'NPK complex >10kg', 'Other NP compounds', 'Diammonium phosphate (DAP)', 'Ammonia, anhydrous', 'Ammonium nitrate', 'Calcium ammonium nitrate', 'NPK complex <=10kg', 'NPK complex'])\n"  ]  },  {  "html": [  "
\n",  "\n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  "
CountryCodeCountryItemCodeItemElementGroupElementCodeElementYearUnitValueFlag
24370 21 Brazil 310230 Ammonium nitrate 5157 5157 Consumption 2002 tonnes 939031 Fb
24420 21 Brazil 310221 Ammonium sulphate 5157 5157 Consumption 2002 tonnes 1430570 Fb
24470 21 Brazil 310240 Calcium ammonium nitrate 5157 5157 Consumption 2002 tonnes 4096 Fb
24520 21 Brazil 310530 Diammonium phosphate (DAP) 5157 5157 Consumption 2002 tonnes 140103 Fb
24570 21 Brazil 310540 Monoammonium phosphate (MAP) 5157 5157 Consumption 2002 tonnes 2245023 Fb
\n",
  "
"
  ],  "metadata": {},  "output_type": "pyout",  "prompt_number": 10,  "text": [  " CountryCode Country ItemCode Item \\\n",  "24370 21 Brazil 310230 Ammonium nitrate \n",  "24420 21 Brazil 310221 Ammonium sulphate \n",  "24470 21 Brazil 310240 Calcium ammonium nitrate \n",  "24520 21 Brazil 310530 Diammonium phosphate (DAP) \n",  "24570 21 Brazil 310540 Monoammonium phosphate (MAP) \n",  "\n",  " ElementGroup ElementCode Element Year Unit Value Flag \n",  "24370 5157 5157 Consumption 2002 tonnes 939031 Fb \n",  "24420 5157 5157 Consumption 2002 tonnes 1430570 Fb \n",  "24470 5157 5157 Consumption 2002 tonnes 4096 Fb \n",  "24520 5157 5157 Consumption 2002 tonnes 140103 Fb \n",  "24570 5157 5157 Consumption 2002 tonnes 2245023 Fb "  ]  }  ],  "prompt_number": 10  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "fertilizers2 = get_data(16)\n",  "print set(fertilizers2.Item) #note that for fertilizers, the categroies that are of interest to us are stored in the Item column\n",  "fertilizers2[((fertilizers2.Country=='Brazil')&(fertilizers2.Year==1961)&(fertilizers2.Element=='Consumption'))].head()\n",  "#data availability is 1961-2002"  ],  "language": "python",  "metadata": {},  "outputs": [  {  "output_type": "stream",  "stream": "stdout",  "text": [  "set(['Other Potash Fertilizers', 'Ammonium Phosphat (P2o5)', 'Phosphoric Acid', 'Phosphate fertilizers', 'Ammonium Nitrate', 'Potash fertilizers', 'Basic Slag', 'Urea', 'Oth Complex Fert (P2o5)', 'Other Complex Fert (N)', 'Ground Rock Phosphate', 'Crude Salts To 20% K2o', 'Other Phosphate Fertil', 'Calcium Ammonium Nitrate', 'Muriate 20-45% K2o', 'Single Superphosphate', 'Total Fertilizers + (Total)', 'Ammonium Phosphate (N)', 'Ammonium SulphateNitrate', 'Other Nitrogenous Fert', 'Muriate Over 45% K2o', 'Ammonium Sulphate', 'Calcium Cyanamide', 'Concent Superphosphate', 'Calcium Nitrate', 'Nitrogenous fertilizers', 'Complex Fertilizer (K2o)', 'Sodium Nitrate', 'Potassium Sulphate', 'Ammonia'])\n"  ]  },  {  "html": [  "
\n",  "\n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  "
CountryCodeCountryItemCodeItemElementGroupElementCodeElementYearUnitValueFlag
155022 21 Brazil 1402 Ammonia 5157 5157 Consumption 1961 tonnes 0 NR
155232 21 Brazil 1362 Ammonium Nitrate 5157 5157 Consumption 1961 tonnes 12206 NaN
155442 21 Brazil 1379 Ammonium Phosphat (P2o5) 5157 5157 Consumption 1961 tonnes 0 NR
155652 21 Brazil 1368 Ammonium Phosphate (N) 5157 5157 Consumption 1961 tonnes 0 NR
155862 21 Brazil 1361 Ammonium Sulphate 5157 5157 Consumption 1961 tonnes 27314 NaN
\n",
  "
"
  ],  "metadata": {},  "output_type": "pyout",  "prompt_number": 15,  "text": [  " CountryCode Country ItemCode Item ElementGroup \\\n",  "155022 21 Brazil 1402 Ammonia 5157 \n",  "155232 21 Brazil 1362 Ammonium Nitrate 5157 \n",  "155442 21 Brazil 1379 Ammonium Phosphat (P2o5) 5157 \n",  "155652 21 Brazil 1368 Ammonium Phosphate (N) 5157 \n",  "155862 21 Brazil 1361 Ammonium Sulphate 5157 \n",  "\n",  " ElementCode Element Year Unit Value Flag \n",  "155022 5157 Consumption 1961 tonnes 0 NR \n",  "155232 5157 Consumption 1961 tonnes 12206 NaN \n",  "155442 5157 Consumption 1961 tonnes 0 NR \n",  "155652 5157 Consumption 1961 tonnes 0 NR \n",  "155862 5157 Consumption 1961 tonnes 27314 NaN "  ]  }  ],  "prompt_number": 15  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "population = get_data(9)\n",  "print set(population.Item) #note that for population, the categroies that are of interest to us are stored in the Item column\n",  "population[((population.Country=='Brazil')&(population.Year==1980)\\\n",  " &(population.Element=='Total economically active population in Agr'))].head() \n",  "#data availability is 1980-2020"  ],  "language": "python",  "metadata": {},  "outputs": [  {  "output_type": "stream",  "stream": "stdout",  "text": [  "set(['Population - Est. & Proj.'])\n"  ]  },  {  "html": [  "
\n",  "\n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  " \n",  "
CountryCodeCountryItemCodeItemElementGroupElementCodeElementYearUnitValueFlag
42017 21 Brazil 3010 Population - Est. & Proj. 601 601 Total economically active population in Agr 1980 1000 16345 NaN
\n",
  "
"
  ],  "metadata": {},  "output_type": "pyout",  "prompt_number": 25,  "text": [  " CountryCode Country ItemCode Item ElementGroup \\\n",  "42017 21 Brazil 3010 Population - Est. & Proj. 601 \n",  "\n",  " ElementCode Element Year Unit \\\n",  "42017 601 Total economically active population in Agr 1980 1000 \n",  "\n",  " Value Flag \n",  "42017 16345 NaN "  ]  }  ],  "prompt_number": 25  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Section 2

\n",
  "In the following section we convert the dataframes defined above into *JSON* format that can be read by the *D3.js* javascript framework for web visualization. Aside form the data manipulation, we also compute and add additional information such as caloric values and food category sums into the *JSON* files. *D3.js* requires the input *JSON*s to be in a very specific format. All of them have to inclue at least the following key-value pairs:
    \n",  "
  • nodes
      \n",  "
    • name: name of the node (the order or these entries automatically assigns an index to the names)
    • \n",
        "
  • links
    • source: the index of the source node
    • target: the index of the target node
    • value: value of the flow
\n",
  "In addition to the above, we will include additional information such as brakedown (sotred under the *key* supply) or secondary flow values for a different type of category (stored under the *key* prod). As a first step, we define the function add_to_json([data],[i],[source],[target],[value],[prod],[supply]) which will take numeric entries and create the *JSON*-like structure presented above. The conversion is made easy using *Python dictionaries*. The argument data is the *Python* dictionary to which the data will be written to. We will generate the following datasets:
  • Gross energy [TWh] - grossdata variable
  • Food energy [TWh] - energydata variable
  • Food weight [ktonnes] - supplydata variable
\n",
  "In addition to the elements above, the argument i defines the detail level at which we are passing the data and currently it is one of the following:
  1. Food supergroups
  2. *FAO* food groups
  3. *FAO* crop groups
  4. Test
"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "def add_to_json(data,i,source,target,value,prod,supply):\n",  " global groupdict\n",  " global count\n",  " if (~np.isnan(value)&(value!=0)):\n",  " if source not in groupdict[i]: \n",  " groupdict[i][source]=count[i]\n",  " data[i][\"nodes\"].append({\"name\":source})\n",  " count[i] += 1 \n",  " if target not in groupdict[i]: \n",  " groupdict[i][target]=count[i]\n",  " data[i][\"nodes\"].append({\"name\":target})\n",  " count[i] += 1 \n",  " links_innerdict={} \n",  " links_innerdict[\"source\"]=groupdict[i][source]\n",  " links_innerdict[\"target\"]=groupdict[i][target]\n",  " links_innerdict[\"value\"]=value\n",  " links_innerdict[\"prod\"]=prod\n",  " links_innerdict[\"supply\"]=supply\n",  " data[i][\"links\"].append(links_innerdict)"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 37  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "We define the main nodes, based on the flows of the *Food Balance Sheets*."  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "elements1=['Feed','Food','Processing','Other Util','Seed','Waste']\n",  "elements2=['Food supply','Food supply quantity (kg/capita']\n",  "fuels=['Gas-diesel oils', 'Natural gas (including LNG)', 'Residual fuel oil', 'Liquefied petroleum gas (LPG)']\n",  "fert1=['Phosphate fertilizers', 'Potash fertilizers', 'Nitrogenous fertilizers']\n",  "fert2=['Potash Fertilizers (K20 total nutrients)', 'Phosphate Fertilizers (P205 total nutrients)',\\\n",  " 'Nitrogen Fertilizers (N total nutrients)']"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 26  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "The main code is structured into 5 *parts* over 2 *modules*:
  • *OUTPUT* module
    • Food part
      • Define food groups
      • Calculate food flows
  • *INPUT* module
    • Fuels part
      • Electricity
      • Fossil fuels
    • Fertilizers part
    • Labor part
  • Save data
\n",
  "Then this code is executed within two for cycles that loop over all countries and years. In the following we present the code parts one by one. Throughout code development it has been assumed that the order in which the code parts are run is preserved, as defined by the above list."  ]  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Food part

"
  ]  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Define food groups

\n",
  "First we need to define the food groups, for all abstraction levels."  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "def food_groups(country, year):\n",  " \n",  " ###############################################################################################\n",  " ###### FOOD PART - define food groups ######\n",  " ###############################################################################################\n",  " \n",  " if True: # placeholder dummy\n",  " \n",  " # associate food items to their respective groups\n",  " \n",  " global supergroup\n",  " global parent\n",  " \n",  " for i in balance[(balance.Element==\"Food\")&(balance.Year==year)&(balance.Country==country)\\\n",  " &(((2511<=balance.ItemCode)&(balance.ItemCode<=2782))\\\n",  " |(balance.ItemCode==2805)\\\n",  " |(balance.ItemCode==2899)\\\n",  " |(balance.ItemCode==2912)\\\n",  " |(balance.ItemCode==2945)\\\n",  " |(balance.ItemCode==2948)\\\n",  " |(balance.ItemCode==2949)\\\n",  " |(balance.ItemCode==2961))].values:\n",  " fooditem=i[3] #choose column in which food item names are stored, might need to change it\n",  " if \"+\" in fooditem: #simplify food names for the sake of visualization\n",  " fooditem=fooditem[0:fooditem.find(\"+\")-1]\n",  " if \"(\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"(\")-1]\n",  " if \"and products\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"and products\")-1]\n",  " index = i[2] #choose column in which food group codes are stored, might need to change it\n",  " parent[fooditem]='None'\n",  " for i in range (n): #assign default supergroup 'Others' and no parent to all food items\n",  " supergroup[i][fooditem]='Others'\n",  " if ((2511<=index<=2659) | (index==2745) | (index==2805) | (index==2899) | (index==2912)):\n",  " if ((2511<=index<=2520) | (index==2805)):\n",  " supergroup[1][fooditem] = 'Cereals - Excluding Beer'\n",  " supergroup[0][fooditem] = 'Cereals'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2531<=index<=2535):\n",  " supergroup[1][fooditem] = 'Starchy Roots'\n",  " supergroup[0][fooditem] = 'Starchy Roots'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2536<=index<=2537):\n",  " supergroup[1][fooditem] = 'Sugar Crops'\n",  " supergroup[0][fooditem] = 'Sugar Crops'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif ((2541<=index<=2543)|(index==2745)):\n",  " supergroup[1][fooditem] = 'Sugar & Sweeteners'\n",  " supergroup[0][fooditem] = 'Sugar'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Processing'\n",  " elif (2546<=index<=2549):\n",  " supergroup[1][fooditem] = 'Pulses'\n",  " supergroup[0][fooditem] = 'Fruits & Vegetables'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2555<=index<=2570):\n",  " supergroup[1][fooditem] = 'Oilcrops'\n",  " supergroup[0][fooditem] = 'Oilcrops'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2571<=index<=2586):\n",  " supergroup[1][fooditem] = 'Vegetable Oils'\n",  " supergroup[0][fooditem] = 'Vegetable Oils'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Processing'\n",  " elif (2601<=index<=2605):\n",  " supergroup[1][fooditem] = 'Vegetables'\n",  " supergroup[0][fooditem] = 'Fruits & Vegetables'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2611<=index<=2625):\n",  " supergroup[1][fooditem] = 'Fruits - Excluding Wine'\n",  " supergroup[0][fooditem] = 'Fruits & Vegetables'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2630<=index<=2635):\n",  " supergroup[1][fooditem] = 'Stimulants'\n",  " supergroup[0][fooditem] = 'Others'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2640<=index<=2645):\n",  " supergroup[1][fooditem] = 'Spices'\n",  " supergroup[0][fooditem] = 'Others'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (2655<=index<=2659):\n",  " supergroup[1][fooditem] = 'Alcoholic Beverages'\n",  " supergroup[0][fooditem] = 'Other Processed'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Processing'\n",  " elif (index==2899):\n",  " supergroup[1][fooditem] = 'Miscellaneous'\n",  " supergroup[0][fooditem] = 'Others'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " elif (index==2912):\n",  " supergroup[1][fooditem] = 'Treenuts'\n",  " supergroup[0][fooditem] = 'Fruits & Vegetables'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'None'\n",  " \n",  " elif ((2731<=index<=2782) | (index==2945)| (index==2948) | (index==2949) | (index==2961)):\n",  " if (2731<=index<=2736):\n",  " supergroup[1][fooditem] = 'Meat'\n",  " supergroup[0][fooditem] = 'Animal Products'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Feed'\n",  " elif (2737<=index<=2743):\n",  " supergroup[1][fooditem] = 'Animal Fats' \n",  " supergroup[0][fooditem] = 'Other Processed' \n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Processing'\n",  " elif (2761<=index<=2768):\n",  " supergroup[1][fooditem] = 'Fish, Seafood'\n",  " supergroup[0][fooditem] = 'Aquatic Products'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Feed'\n",  " elif ((2769<=index<=2775)|(index==2961)):\n",  " supergroup[1][fooditem] = 'Aquatic Products, Other'\n",  " supergroup[0][fooditem] = 'Aquatic Products'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Feed'\n",  " elif (2781<=index<=2782):\n",  " supergroup[1][fooditem] = 'Animal Fats'\n",  " supergroup[0][fooditem] = 'Other Processed'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Processing'\n",  " elif (index==2945):\n",  " supergroup[1][fooditem] = 'Offals'\n",  " supergroup[0][fooditem] = 'Animal Products'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Feed'\n",  " elif (index==2948):\n",  " supergroup[1][fooditem] = 'Milk - Excluding Butter'\n",  " supergroup[0][fooditem] = 'Animal Products'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Feed'\n",  " elif (index==2949):\n",  " supergroup[1][fooditem] = 'Eggs' \n",  " supergroup[0][fooditem] = 'Animal Products'\n",  " parent[fooditem]=parent[supergroup[0][fooditem]]=parent[supergroup[1][fooditem]]= 'Feed'"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 46  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Calculate food flows

"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "def food_part(country, year):\n",  " \n",  " ###############################################################################################\n",  " ###### FOOD PART - Calculate food flows ######\n",  " ###############################################################################################\n",  " \n",  " if True: # placeholder dummy\n",  " \n",  " global foodinput\n",  " global foodoutput\n",  " \n",  " #extract food item caloric values\n",  " percapitasupply={}\n",  " percapitaenergy={}\n",  " for i in balance[(balance.Element=='Food supply')\\\n",  " &(balance.Year==year)\\\n",  " &(balance.Country==country)].values:\n",  " foodvalue=i[9] #choose column in which food item values are stored, might need to change it\n",  " fooditem=i[3] #choose column in which food item names are stored, might need to change it\n",  " if \"+\" in fooditem: #simplify food names for the sake of visualization\n",  " fooditem=fooditem[0:fooditem.find(\"+\")-1]\n",  " if \"(\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"(\")-1]\n",  " if \"and products\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"and products\")-1]\n",  " percapitaenergy[fooditem]=foodvalue\n",  " \n",  " for i in balance[(balance.Element=='Food supply quantity (kg/capita')\\\n",  " &(balance.Year==year)\\\n",  " &(balance.Country==country)].values:\n",  " foodvalue=i[9] #choose column in which food item values are stored, might need to change it\n",  " fooditem=i[3] #choose column in which food item names are stored, might need to change it\n",  " if \"+\" in fooditem: #simplify food names for the sake of visualization\n",  " fooditem=fooditem[0:fooditem.find(\"+\")-1]\n",  " if \"(\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"(\")-1]\n",  " if \"and products\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"and products\")-1]\n",  " percapitasupply[fooditem]=foodvalue*1000/365.25 #kg/year/capita->g/day/capita\n",  " \n",  " foodcalorie={}\n",  " for i in percapitasupply:\n",  " foodcalorie[i]=100 #set default caloric value for all food items, necessary to handle exceptions\n",  " if i in percapitaenergy:\n",  " if ((percapitasupply[i]==0)|(percapitaenergy[i]==0)): \n",  " #if either term is null, try to assing the supergroup values, if they exist\n",  " if ((i in supergroup[1])&(supergroup[1][i] in percapitasupply)&(supergroup[1][i] in percapitaenergy)):\n",  " if ((percapitasupply[supergroup[1][i]]!=0)&(percapitaenergy[supergroup[1][i]]!=0)):\n",  " foodcalorie[i]=100*percapitaenergy[supergroup[1][i]]/percapitasupply[supergroup[1][i]]\n",  " else:\n",  " foodcalorie[i]=100*percapitaenergy[i]/percapitasupply[i]\n",  " \n",  " #extract supply data and calculate energy data\n",  " global energydata\n",  " global supplydata\n",  " global parentsupplysum\n",  " global parentenergysum\n",  " \n",  " for element in elements1:\n",  " supplysum=[] #dictionary to keep track of item sums in case of aggregations, based on the supergroup\n",  " energysum=[] #dictionary to keep track of item sums in case of aggregations, based on the supergroup\n",  " for i in range (n):\n",  " supplysum.append({})\n",  " energysum.append({})\n",  " \n",  " for i in balance[(balance.Element==element)&(balance.Year==year)&(balance.Country==country)\\\n",  " &(((2511<=balance.ItemCode)&(balance.ItemCode<=2782))\\\n",  " |(balance.ItemCode==2805)\\\n",  " |(balance.ItemCode==2899)\\\n",  " |(balance.ItemCode==2912)\\\n",  " |(balance.ItemCode==2945)\\\n",  " |(balance.ItemCode==2948)\\\n",  " |(balance.ItemCode==2949)\\\n",  " |(balance.ItemCode==2961))].values:\n",  " foodvalue=i[9] #choose column in which food item values are stored, might need to change it\n",  " if (~np.isnan(foodvalue)&(foodvalue!=0)):\n",  " fooditem=i[3] #choose column in which food item names are stored, might need to change it\n",  " if \"+\" in fooditem: #simplify food names for the sake of visualization\n",  " fooditem=fooditem[0:fooditem.find(\"+\")-1]\n",  " if \"(\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"(\")-1]\n",  " if \"and products\" in fooditem:\n",  " fooditem=fooditem[0:fooditem.find(\"and products\")-1]\n",  " \n",  " energyvalue=foodvalue*foodcalorie[fooditem]*0.00116222222/100\n",  " #TWh = 1000tons * kcal/100g # 10^9) /100g * 0.00116222222 kwh/kcal *10^-9 TWh/kWh\n",  " \n",  " #create links for detail level 3 \n",  " if (element==\"Seed\"): #replace seed group with self-loops \n",  " add_to_json(supplydata,2,fooditem,fooditem,foodvalue,energyvalue,\\\n",  " '[{\"l\":'+repr(fooditem)+',\"v\":'+repr(foodvalue)+'}]') \n",  " add_to_json(energydata,2,fooditem,fooditem,energyvalue,foodvalue,\\\n",  " '[{\"l\":'+repr(fooditem)+',\"v\":'+repr(energyvalue)+'}]') \n",  " else:\n",  " add_to_json(supplydata,2,fooditem,element,foodvalue,energyvalue,\\\n",  " '[{\"l\":'+repr(fooditem)+',\"v\":'+repr(foodvalue)+'}]')\n",  " add_to_json(energydata,2,fooditem,element,energyvalue,foodvalue,\\\n",  " '[{\"l\":'+repr(fooditem)+',\"v\":'+repr(energyvalue)+'}]')\n",  " \n",  " #sum all links going into food for sepparate representation\n",  " if (element==\"Food\"):\n",  " foodoutput+=energyvalue\n",  " \n",  " #create aggregate values for parent links detail level 3\n",  " if fooditem in parentsupplysum[2]:\n",  " parentsupplysum[2][fooditem]+=foodvalue\n",  " parentenergysum[2][fooditem]+=energyvalue\n",  " else:\n",  " parentsupplysum[2][fooditem]=foodvalue\n",  " parentenergysum[2][fooditem]=energyvalue\n",  " \n",  " #create aggregate values for detail levels 1-2\n",  " for i in range(2): #sum food item values over different groups, defined above, currently 1-2\n",  " #save sum, but also components for the piechart breakdown\n",  " if supergroup[i][fooditem] in supplysum[i]:\n",  " supplysum[i][supergroup[i][fooditem]][\"data\"].append({\"l\":fooditem, \"v\":foodvalue})\n",  " supplysum[i][supergroup[i][fooditem]][\"sum\"]+=foodvalue\n",  " energysum[i][supergroup[i][fooditem]][\"data\"].append({\"l\":fooditem, \"v\":energyvalue})\n",  " energysum[i][supergroup[i][fooditem]][\"sum\"]+=energyvalue\n",  " else:\n",  " supplysum[i][supergroup[i][fooditem]]={\"data\":[{\"l\":fooditem, \"v\":foodvalue}],\"sum\":foodvalue}\n",  " energysum[i][supergroup[i][fooditem]]={\"data\":[{\"l\":fooditem, \"v\":energyvalue}],\"sum\":energyvalue}\n",  " \n",  " #create links for detail levels 1-2 \n",  " for i in range(2):\n",  " for j in supplysum[i]:\n",  " if (element==\"Seed\"):\n",  " add_to_json(supplydata,i,j,j,supplysum[i][j][\"sum\"],energysum[i][j][\"sum\"],supplysum[i][j][\"data\"])\n",  " add_to_json(energydata,i,j,j,energysum[i][j][\"sum\"],supplysum[i][j][\"sum\"],energysum[i][j][\"data\"])\n",  " else:\n",  " add_to_json(supplydata,i,j,element,supplysum[i][j][\"sum\"],energysum[i][j][\"sum\"],supplysum[i][j][\"data\"])\n",  " add_to_json(energydata,i,j,element,energysum[i][j][\"sum\"],supplysum[i][j][\"sum\"],energysum[i][j][\"data\"])\n",  " \n",  " #create aggregate values for parent links detail levels 1-2\n",  " for i in range(2):\n",  " for j in set(supplysum[i]):\n",  " if j in parentsupplysum[i]:\n",  " parentsupplysum[i][j]+=supplysum[i][j][\"sum\"]\n",  " parentenergysum[i][j]+=energysum[i][j][\"sum\"]\n",  " else:\n",  " parentsupplysum[i][j]=supplysum[i][j][\"sum\"]\n",  " parentenergysum[i][j]=energysum[i][j][\"sum\"]\n",  " \n",  " #create parent links for detail levels 1-3\n",  " for i in range(3): \n",  " for j in parentsupplysum[i]:\n",  " if (parent[j]!=\"None\"):\n",  " add_to_json(supplydata,i, parent[j],j,parentsupplysum[i][j],parentenergysum[i][j],\\\n",  " '[{\"l\":'+repr( parent[j])+',\"v\":'+repr(parentsupplysum[i][j])+'}]')\n",  " add_to_json(energydata,i, parent[j],j,parentenergysum[i][j],parentsupplysum[i][j],\\\n",  " '[{\"l\":'+repr( parent[j])+',\"v\":'+repr(parentenergysum[i][j])+'}]')\n",  " \n",  " #calculate crops share for energy input distribution\n",  " global supplyshare\n",  " global energyshare\n",  " \n",  " cropsum=[]\n",  " cropenergy=[]\n",  " for i in range(n):\n",  " cropsum.append(0)\n",  " cropenergy.append(0)\n",  " for j in parentsupplysum[i]:\n",  " if (parent[j]=='None'):\n",  " cropsum[i] += parentsupplysum[i][j]\n",  " cropenergy[i] += parentenergysum[i][j]\n",  " \n",  " for i in range(n):\n",  " for j in parentsupplysum[i]:\n",  " if (parent[j]=='None'):\n",  " supplyshare[i][j] = parentsupplysum[i][j]/cropsum[i]\n",  " energyshare[i][j] = parentenergysum[i][j]/cropenergy[i]\n",  " \n",  " #update nodes name map\n",  " for i in range(n):\n",  " energydata[i]['nodes']=supplydata[i]['nodes']"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 83  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Fuels part

"
  ]  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Electricity

"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  " def fuels_part1(country, year):\n",  " \n",  " ###############################################################################################\n",  " ###### FUELS PART - Electricity ######\n",  " ###############################################################################################\n",  " \n",  " if True: # placeholder dummy\n",  " \n",  " global foodinput\n",  " global foodoutput\n",  " \n",  " global supplyshare\n",  " global energyshare\n",  " \n",  " #distribute electricity input\n",  " for fuel in ['Electricity']:\n",  " for i in energy[(energy.Element=='Consumption in Agriculture')&(energy.Year==year)&(energy.Country==country)\\\n",  " &(energy.Unit=='Terajoule')&(energy.Item==fuel)].values:\n",  " fuelvalue=i[9] #choose column in which fuel item values are stored, might need to change it\n",  " if (~np.isnan(fuelvalue)&(fuelvalue!=0)):\n",  " fuelitem=i[3] #choose column in which fuel item names are stored, might need to change it\n",  " if \"+\" in fuelitem: #simplify fuel names for the sake of visualization\n",  " fuelitem=fuelitem[0:fuelitem.find(\"+\")-1]\n",  " if \"(\" in fuelitem:\n",  " fuelitem=fuelitem[0:fuelitem.find(\"(\")-1]\n",  " if \"and products\" in fuelitem:\n",  " fuelitem=fuelitem[0:fuelitem.find(\"and products\")-1]\n",  " electricityvalue=fuelvalue*0.000277777778 #1 joule = 0.000277777778 Wh\n",  " for i in range(n):\n",  " for j in parentsupplysum[i]:\n",  " if (parent[j]=='None'):\n",  " add_to_json(energydata,i, fuel,j,electricityvalue*energyshare[i][j],0,\\\n",  " '[{\"l\":'+repr(fuel)+',\"v\":'+repr(electricityvalue*energyshare[i][j])+'}]')\n",  " \n",  " foodinput+=electricityvalue "  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 84  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Fossil fuels

"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  " def fuels_part2(country, year):\n",  " \n",  " ###############################################################################################\n",  " ###### FUELS PART - Fossil fuels ######\n",  " ###############################################################################################\n",  " \n",  " if True: # placeholder dummy\n",  " \n",  " global foodinput\n",  " global foodoutput\n",  " \n",  " global supplyshare\n",  " global energyshare\n",  " fuelenergysum=[]\n",  " for i in range (n):\n",  " fuelenergysum.append({})\n",  " \n",  " #distribute fossil fuel input\n",  " for fuel in fuels:\n",  " for i in energy[(energy.Element=='Consumption in Agriculture')&(energy.Year==year)&(energy.Country==country)\\\n",  " &(energy.Unit=='Terajoule')&(energy.Item==fuel)].values:\n",  " fuelvalue=i[9] #choose column in which fuel item values are stored, might need to change it\n",  " if (~np.isnan(fuelvalue)&(fuelvalue!=0)):\n",  " fuelitem=i[3] #choose column in which fuel item names are stored, might need to change it\n",  " if \"+\" in fuelitem: #simplify fuel names for the sake of visualization\n",  " fuelitem=fuelitem[0:fuelitem.find(\"+\")-1]\n",  " if \"(\" in fuelitem:\n",  " fuelitem=fuelitem[0:fuelitem.find(\"(\")-1]\n",  " if \"and products\" in fuelitem:\n",  " fuelitem=fuelitem[0:fuelitem.find(\"and products\")-1]\n",  " fuelvalue*=0.000277777778 #1 joule = 0.000277777778 Wh\n",  " for i in range(n): #sum fuel item values over all groups\n",  " supergroup[i][fuelitem]=\"Fossil Fuels\"\n",  " #save sum, but also components for the piechart breakdown\n",  " if supergroup[i][fuelitem] in fuelenergysum[i]:\n",  " fuelenergysum[i][supergroup[i][fuelitem]][\"data\"].append({\"l\":fuelitem, \"v\":fuelvalue})\n",  " fuelenergysum[i][supergroup[i][fuelitem]][\"sum\"]+=fuelvalue\n",  " else:\n",  " fuelenergysum[i][supergroup[i][fuelitem]]={\"data\":[{\"l\":fuelitem, \"v\":fuelvalue}],\"sum\":fuelvalue}\n",  " f_save=copy.deepcopy(fuelenergysum)\n",  " for i in range(n):\n",  " for j in parentsupplysum[i]:\n",  " fuelenergysum=copy.deepcopy(f_save)\n",  " if (parent[j]=='None'):\n",  " for k in fuelenergysum[i]:\n",  " for f in fuelenergysum[i][k][\"data\"]:\n",  " f[\"v\"]*=energyshare[i][j]\n",  " add_to_json(energydata,i,k,j,fuelenergysum[i][k][\"sum\"]*energyshare[i][j],0,fuelenergysum[i][k][\"data\"])\n",  " \n",  " foodinput+=fuelenergysum[0][\"Fossil Fuels\"][\"sum\"] "  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 78  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Fertilizers Part

"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "def fertilizers_part(country, year):\n",  " \n",  " ###############################################################################################\n",  " ###### FERTILIZERS PART ######\n",  " ###############################################################################################\n",  " \n",  " if True: # placeholder dummy\n",  " \n",  " global foodinput\n",  " global foodoutput\n",  " \n",  " fertenergysum=[]\n",  " fertsupplysum=[]\n",  " for i in range (n):\n",  " fertenergysum.append({})\n",  " fertsupplysum.append({})\n",  " \n",  " if (year<2003):\n",  " fertilizers=fertilizers2 #inverted on purpose\n",  " fertnames=fert1\n",  " fertelement='Consumption'\n",  " else:\n",  " fertilizers=fertilizers1\n",  " fertnames=fert2\n",  " fertelement='Consumption in nutrients'\n",  " for fert in fertnames:\n",  " for i in fertilizers[(fertilizers.Element==fertelement)&(fertilizers.Year==year)&(fertilizers.Country==country)\\\n",  " &(fertilizers.Item==fert)].values:\n",  " fertvalue=i[9] #choose column in which fert item values are stored, might need to change it\n",  " if (~np.isnan(fertvalue)&(fertvalue!=0)):\n",  " fertitem=i[3] #choose column in which fert item names are stored, might need to change it\n",  " #define energy content+packaging and transport (http://www.engr.usask.ca/societies/csae/c9915.pdf page10)\n",  " if fertitem in ['Phosphate fertilizers', 'Phosphate Fertilizers (P205 total nutrients)']:\n",  " fertenergycontent=6.82+8.33 #MJ/kg \n",  " fertitem='Phosphate'\n",  " if fertitem in ['Potash fertilizers', 'Potash Fertilizers (K20 total nutrients)']:\n",  " fertenergycontent=2.88+6.35 #MJ/kg\n",  " fertitem='Potash'\n",  " if fertitem in ['Nitrogenous fertilizers', 'Nitrogen Fertilizers (N total nutrients)']:\n",  " fertenergycontent=59+7.05 #MJ/kg\n",  " fertitem='Nitrogen'\n",  " \n",  " fertvalue=fertvalue/1000 #model units are in 1000 tonnes\n",  " fertenergy=fertvalue*fertenergycontent * 0.000277777778 #TWh \n",  " #MJ/kg / TJ/1000tonnes * #1 joule = 0.000277777778 Wh\n",  " for i in range(n): #sum fert item values over all groups\n",  " supergroup[i][fertitem]=\"Fertilizers\"\n",  " #save sum, but also components for the piechart breakdown\n",  " if supergroup[i][fertitem] in fertenergysum[i]:\n",  " fertenergysum[i][supergroup[i][fertitem]][\"data\"].append({\"l\":fertitem, \"v\":fertenergy})\n",  " fertenergysum[i][supergroup[i][fertitem]][\"sum\"]+=fertenergy\n",  " fertsupplysum[i][supergroup[i][fertitem]][\"data\"].append({\"l\":fertitem, \"v\":fertvalue})\n",  " fertsupplysum[i][supergroup[i][fertitem]][\"sum\"]+=fertvalue\n",  " else:\n",  " fertenergysum[i][supergroup[i][fertitem]]={\"data\":[{\"l\":fertitem, \"v\":fertenergy}],\"sum\":fertenergy}\n",  " fertsupplysum[i][supergroup[i][fertitem]]={\"data\":[{\"l\":fertitem, \"v\":fertvalue}],\"sum\":fertvalue}\n",  " f_save=copy.deepcopy(fertenergysum)\n",  " for i in range(n):\n",  " for j in parentsupplysum[i]:\n",  " fertenergysum=copy.deepcopy(f_save)\n",  " if (parent[j]=='None'):\n",  " for k in fertenergysum[i]:\n",  " for f in fertenergysum[i][k][\"data\"]:\n",  " f[\"v\"]*=energyshare[i][j]\n",  " add_to_json(energydata,i,k,j,fertenergysum[i][k][\"sum\"]*energyshare[i][j],\\\n",  " fertsupplysum[i][k][\"sum\"]*energyshare[i][j],fertenergysum[i][k][\"data\"])\n",  " \n",  " foodinput+=fertenergysum[0][\"Fertilizers\"][\"sum\"] "  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 85  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Labor Part

"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  " def labor_part(country, year):\n",  " \n",  " ###############################################################################################\n",  " ###### LABOR PART ######\n",  " ###############################################################################################\n",  " \n",  " if True: # placeholder dummy\n",  " \n",  " global foodinput\n",  " global foodoutput\n",  " \n",  " calorietotal=balance[(balance.Element=='Food supply')&(balance.Year==year)&(balance.Country==country)\\\n",  " &(balance.Item=='Grand Total + (Total)')].values[0][9] \n",  " #second index: choose column in which calorie item values are stored, might need to change it\n",  " popvalue=population[(population.Element=='Total economically active population in Agr')&(population.Year==year)\\\n",  " &(population.Country==country)].values[0][9] \n",  " #second index: choose column in which pop item values are stored, might need to change it\n",  " \n",  " if (~np.isnan(popvalue)&(popvalue!=0)):\n",  " workyear=5.0/7.0*365.25 #working days spent with farming per year\n",  " \n",  " hourlycalorieburn=204 #calories needed per hour of farming\n",  " hoursperday=12 #hours per day spent farming\n",  " minimumcalorieburn=hourlycalorieburn*hoursperday\n",  " calorieburn=0.9 #percent of calorie intake burned for farming, if below minimum\n",  " \n",  " laborenergy=min(minimumcalorieburn,calorieburn*calorietotal)\\\n",  " *workyear\\\n",  " *popvalue\\\n",  " *1000\\\n",  " *0.00116222222/1000000000\n",  " # 0.00116222222 kwh/kcal *10^-9 TWh/kWh\n",  " \n",  " for i in range(n):\n",  " for j in parentsupplysum[i]:\n",  " if (parent[j]=='None'):\n",  " add_to_json(energydata,i,\"Labor\",j,laborenergy*energyshare[i][j],0,\\\n",  " '[{\"l\":'+repr(\"Labor\")+',\"v\":'+repr(laborenergy*energyshare[i][j])+'}]')\n",  " \n",  " foodinput+=laborenergy"  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 87  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Save Data

"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "def save_data(country, year):\n",  " \n",  " ###############################################################################################\n",  " ###### SAVE DATA ######\n",  " ###############################################################################################\n",  " \n",  " if True: # placeholder dummy\n",  " \n",  " for i in range(n):\n",  " file('../json/'+country+repr(year)+'d'+repr(i+1)+'.json','w').write(json.dumps(supplydata[i]))\n",  " file('../json/'+country+repr(year)+'e'+repr(i+1)+'.json','w').write(json.dumps(energydata[i]))\n",  " \n",  " #create global input-output graph (top right corner)\n",  " fooddata={\"nodes\":[{\"name\": \"Input\"},{\"name\": \"Food\"},{\"name\": \"Output\"}],\"links\":[]}\n",  " \n",  " links_innerdict={} \n",  " links_innerdict[\"source\"]=0\n",  " links_innerdict[\"target\"]=1\n",  " links_innerdict[\"value\"]=foodinput\n",  " fooddata[\"links\"].append(links_innerdict)\n",  " \n",  " links_innerdict={} \n",  " links_innerdict[\"source\"]=1\n",  " links_innerdict[\"target\"]=2\n",  " links_innerdict[\"value\"]=foodoutput\n",  " fooddata[\"links\"].append(links_innerdict)\n",  " \n",  " file('../json/'+country+repr(year)+'.json','w').write(json.dumps(fooddata)) "  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 81  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

Main

"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "n = 4 #number of grouping variations = number of detail levels\n",  "\n",  "#for country in set(balance.Country.values):\n",  "#for country in ['United States of America', 'Canada', 'China', 'India']:\n",  "for country in ['World + (Total)']:\n",  " \n",  " #for year in set(balance[balance.Country==country].Year.values):\n",  " #for year in range(1981,2009):\n",  " for year in [1981]:\n",  " \n",  " #try: \n",  " \n",  " # initialize global variables\n",  " foodinput=0 #initialize food input variable for sum collection and processing for EROEI\n",  " foodoutput=0 #initialize food output variable for sum collection and processing for EROEI\n",  " count = [] #counter for the groupdict below\n",  " groupdict=[{}] #initialize dictionary for item order of addition - necessary format for sankey.js\n",  " parentsupplysum=[] #dictionary to keep track of food item sums in case of aggregations, based on the supergroup\n",  " parentenergysum=[] #dictionary to keep track of food item sums in case of aggregations, based on the supergroup \n",  " supergroup=[] #group to which food item entry belongs to. defined in food_groups()\n",  " #custom groups possible, along the FAO official ones \n",  " #i.e. Cereals for Wheat, Meat for Poultrymeat, Alcoholic Beverages for Wine\n",  " parent={} #food item parent(source) node, where applicable (secondary food items) \n",  " #i.e. Feed for Animal Products, Milk for Cheese, Processing for Vegetable Oils & Sugar\n",  " energydata=[] #initialize dictionary array for later JSON export for D3 & sankey.js visualization\n",  " supplydata=[] #initialize dictionary array for later JSON export for D3 & sankey.js visualization\n",  " supplyshare=[] #save source crop share for supply input distribution later\n",  " energyshare=[] #save source crop share for energy input distribution later\n",  " \n",  " for i in range (n):\n",  " count.append(0)\n",  " groupdict.append({})\n",  " parentsupplysum.append({})\n",  " parentenergysum.append({})\n",  " supergroup.append({})\n",  " energydata.append({\"nodes\":[],\"links\":[]})\n",  " supplydata.append({\"nodes\":[],\"links\":[]})\n",  " supplyshare.append({})\n",  " energyshare.append({})\n",  " \n",  " # run algorithm parts\n",  " food_groups(country,year)\n",  " food_part(country, year)\n",  " fuels_part1(country, year)\n",  " fuels_part2(country, year)\n",  " fertilizers_part(country, year)\n",  " labor_part(country, year)\n",  " save_data(country, year)\n",  " \n",  " #except:\n",  " # KeyError "  ],  "language": "python",  "metadata": {},  "outputs": [],  "prompt_number": 90  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "After the *JSON* files have been generated, they are used as input for generating the *Sankey* diagrams. Using the *HTML* library, we can display a snippet of the final website below. The driving logic of the *HTML* is as follows: the main function change loads the corresponding *JSON* based on the *country* and *year* selections."  ]  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "
  • Data
      \t
    • Select the country, year and the desired detail level from the menu on the right.
    • \n",  "
    • You can choose from 3 different datasets (vertical):
      • Gross energy [TWh]
      • Food energy [TWh]
      • Food weight [ktonnes].
    • \n",
        "
    • and 4 detail levels (horizontal):
      • Food supergroups
      • *FAO* food groups
      • *FAO* crop groups
      • Test
    • \n",
        "
  • Layout
      \n",  "
    • The recommended minimum resolution is 1070x670
    • \n",
        "
    • Scrolling over the diagram will advance time
    • \n",
        "
    • Node and flow heights will be defined by the main unit of the dataset.
    • \n",
        "
    • Drag nodes around to adjust the layout of the diagram.
    • \n",
        "
    • Enable/disable horizontal/vertical node movement from the menu on the right.
    • \n",
        "
    • Double click a node to hide/show all of its incoming flows.
    • \n",
        "
    • Hover over a flow to display:
      • its value and breakdown in [TWh] and [ktonnes]
      • its caloric value in [kcal/100g]
    • \n",
        "
    • Hover over a node to display its value in the main unit of the dataset.
    • \n",
        "
  • Pie chart
      \n",  "
    • Upon hover over a node or a flow, a pie chart will show in the bottom right corner.
    • \n",
        "
    • When over a flow, its breakdown will be displayed, with the value at the center.
    • \n",
        "
    • When over a node, the incoming and outgoing flow breakdowns will be displayed.
    • \n",
        "
    • You can enable or disable items on the pie chart by clicking on the legend entries.
    • \n",
        "
    • Hover over a pie chart slice to display its value.
    • \n",
        "
    • Pie chart units always correspond to the main unit of the dataset.
    • \n",
        "
  • Help
      \n",  "
    • Use the menu next to the title to view the world map and the detailed help.
    \n",  "
"
  ]  },  {  "cell_type": "code",  "collapsed": false,  "input": [  "from IPython.display import HTML\n",  "HTML('') "  ],  "language": "python",  "metadata": {},  "outputs": [  {  "html": [  ""  ],  "metadata": {},  "output_type": "pyout",  "prompt_number": 95,  "text": [  ""  ]  }  ],  "prompt_number": 95  },  {  "cell_type": "markdown",  "metadata": {},  "source": [  "

We would like to express our gratitude to all of the developers of the libraries used and especially to the affiliates of *FAOSTAT* for their great database and openly accesible data. The data manipulation algorithms and visualization techniques are open sourced and freely reproducible, forks are welcome on GitHub. The concept and methodology are subject to copyright of the authors.


\n",
  "

© Sgouris Sgouridis, Denes Csala

\n",
  "

Masdar Institute, Abu Dhabi, 2014


\n",
  "

http://food.csaladen.es

"
  ]  }  ],  "metadata": {}  }  ]  }