{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Create State Physical Supply Usage Table (PSUT)\n", "Uses values in the WaterBalanceData.csv file to populate values in the StatePSUT.xlsx file\n", "\n", "Requires the openyxl module: https://openpyxl.readthedocs.io/en/default/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "No module named openpyxl", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mopenpyxl\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mload_workbook\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mImportError\u001b[0m: No module named openpyxl" ] } ], "source": [ "#Import modules\n", "import sys, os\n", "import pandas as pd\n", "from openpyxl import load_workbook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get/Set the filenames required" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Set the location of the data directory\n", "dataDir = '../../Data'\n", "#Get the water balance input csv file\n", "inDataFN = dataDir + os.sep + 'StateData' + os.sep + 'la_2010.csv'\n", "#Get the template\n", "inXlsxFN = dataDir + os.sep + 'Templates' + os.sep + 'StatePSUTTemplate.xlsx' #The template that will be filled in" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Load the water balance data into a pandas dataframe\n", "dfData = pd.read_csv(inDataFN)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below we set the field to column mappings. The number on the right of the '=' refers to the column in the template in which the field on the left occurs. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Row and column indices\n", "#--Columns--\n", "Aq = 5 #Aquaculture\n", "Do = 19 #Domestic\n", "In = 16 #Industrial\n", "Ic = 2 #Irrigation-cropland\n", "Ig = 17 #Irrigation-golf courses\n", "Li = 3 #Livestock\n", "Mi = 7 #Mining\n", "PS = 14 #Public supply\n", "TC = 10 #Thermoelectric-once thru\n", "TR = 9 #Thermoelectric-recirculated\n", "Supply = 20 #Environment\n", "#--Rows--\n", "Sf = 21 #Surface\n", "Gw = 22 #Groundwater" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Create the dictionary of GroupNames and cell locations\n", "celLocs = {'Aquaculture_Surface':(Sf,Aq),\n", " 'Aquaculture_Groundwater':(Gw,Aq),\n", " 'Domestic_Surface':(Sf,Do),\n", " 'Domestic_Groundwater':(Gw,Do),\n", " 'Industrial_Surface':(Sf,In),\n", " 'Industrial_Groundwater':(Gw,In),\n", " 'Irrigation_Crop_Surface':(Sf,Ic),\n", " 'Irrigation_Crop_Groundwater':(Gw,Ic),\n", " 'Irrigation_Golf_Surface':(Sf,Ig),\n", " 'Irrigation_Golf_Groundwater':(Gw,Ig),\n", " 'Livestock_Surface':(Sf,Li),\n", " 'Livestock_Groundwater':(Gw,Li),\n", " 'Mining_Surface':(Sf,Mi),\n", " 'Mining_Groundwater':(Gw,Mi),\n", " 'PublicSupply_Surface':(Sf,PS),\n", " 'PublicSupply_Groundwater':(Gw,PS),\n", " 'ThermoElec_OnceThru_Surface':(Sf,TC),\n", " 'ThermoElec_OnceThru_Groundwater':(Gw,TC),\n", " 'ThermoElec_Recirc_Surface':(Sf,TR),\n", " 'ThermoElec_Recirc_Groundwater':(Gw,TR),\n", " 'Supply':(4,12)\n", " } " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Create the workbook object\n", "wb = load_workbook(filename = inXlsxFN)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for year in (2000,2005, 2010):\n", " #Get the year worksheet in the workbook\n", " ws = wb.get_sheet_by_name(str(year))\n", " \n", " #Label the sheet\n", " ws.cell(column=1,row=1,value=\"US Water Balance: {}. Values in MGal/Year\".format(year))\n", " \n", " #use the dictionary to insert values\n", " for name, cellLoc in celLocs.items():\n", " #Get the value for selected year\n", " val = dfData[(dfData.Group == name) & (dfData.YEAR == year)]['MGal'].iloc[0]\n", " #insert it into the Excel file\n", " ws.cell(column = cellLoc[1],row = cellLoc[0],value = val)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "wb.save(dataDir+os.sep+'BalanceSheet.xlsx')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 2 }