{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading the rock property catalog\n", "\n", "This notebook accompanies a blog post — [The Rock Property Catalog again](http://www.agilegeoscience.com/blog/2015/10/5/the-rock-property-catalog-again)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Querying" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is very hacky. It will change. Stay tuned." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import requests\n", "import pandas as pd\n", "\n", "class RPC(object):\n", " def __init__(self):\n", " pass\n", " \n", " def _query_ssw(self, filters, properties, options):\n", " \n", " base_url = \"http://www.subsurfwiki.org/api.php\"\n", " q = \"action=ask&query=[[RPC:%2B]]\"\n", " q += ''.join(filters) if filters else ''\n", " q += '|%3F' + '|%3F'.join(properties) if properties else ''\n", " q += '|' + '|'.join(options) if options else ''\n", " q += '&format=json'\n", " \n", " return requests.get(base_url, params=q)\n", " \n", " def _get_formats(self, response):\n", " formats = {}\n", " for item in response.json()['query']['printrequests']:\n", " if item[u'mode'] == 1:\n", " formats[item[u'label']] = item[u'typeid'].lstrip('_')\n", " return formats\n", " \n", " def _build_dataframe(self, response):\n", " \"\"\"\n", " Takes the response of a query and returns a pandas\n", " dataframe containing the results.\n", " \"\"\"\n", " try:\n", " s = list(response.json()['query']['results'].keys())\n", " except Exception as e:\n", " raise e\n", " samples = [i[4:] for i in s]\n", " df = pd.DataFrame(samples)\n", "\n", " # We'll need to know the formats of the columns.\n", " formats = self._get_formats(response)\n", " properties = formats.keys()\n", "\n", " # Now traverse the JSON and build the DataFrame.\n", " for prop in properties:\n", " temp = []\n", " for row in list(s):\n", " p = response.json()['query']['results'][row]['printouts']\n", " if p[prop]:\n", " if formats[prop] == 'qty': # Quantity, number + unit\n", " temp.append(p[prop][0]['value'])\n", " elif formats[prop] == 'wpg': # Wiki page\n", " temp.append(p[prop][0]['fulltext'])\n", " else: # Anything else: num, txt, tem, etc.\n", " temp.append(p[prop][0])\n", " else:\n", " temp.append(None)\n", " df[prop] = temp\n", " \n", " df = df.set_index(0)\n", " df.index.name = None\n", "\n", " return df\n", " \n", " def query(self, filters=None, properties=None, options=None):\n", " r = self._query_ssw(filters, properties, options)\n", " if r.status_code == 200:\n", " return self._build_dataframe(r)\n", " else:\n", " print(\"Something went wrong.\")\n", " " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | Delta | \n", "Lithology | \n", "Vp | \n", "Epsilon | \n", "Vs | \n", "Citation | \n", "Rho | \n", "Description | \n", "
---|---|---|---|---|---|---|---|---|
100015 | \n", "0.6263 | \n", "Shale | \n", "NaN | \n", "0.955311 | \n", "NaN | \n", "Sondergeld, 2011 | \n", "None | \n", "Haynesville Shale 2 | \n", "
100072 | \n", "0.1180 | \n", "Shale | \n", "3774.891853 | \n", "0.245000 | \n", "2508.933067 | \n", "Wang, 2002 | \n", "None | \n", "Hard shale | \n", "
100010 | \n", "0.7319 | \n", "Shale | \n", "NaN | \n", "0.586081 | \n", "NaN | \n", "Sondergeld, 2011 | \n", "None | \n", "Haynesville Shale 1 | \n", "
100051 | \n", "0.0320 | \n", "Shale | \n", "2464.704037 | \n", "0.258000 | \n", "1470.468283 | \n", "Wang, 2002 | \n", "None | \n", "Shale | \n", "
100077 | \n", "0.0950 | \n", "Shale | \n", "3792.983055 | \n", "0.240000 | \n", "2276.750803 | \n", "Wang, 2002 | \n", "None | \n", "Hard shale | \n", "
© Agile Geoscience 2015. This notebook is licensed under the terms of Creative Commons Attribution 4.0. Please use it!
" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }