Skip to content

Instantly share code, notes, and snippets.

@blankdots
Last active October 18, 2015 12:55
Show Gist options
  • Save blankdots/81986a0a8d1a851b855e to your computer and use it in GitHub Desktop.
Save blankdots/81986a0a8d1a851b855e to your computer and use it in GitHub Desktop.
Software for Scientists - October 2015
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"value = 42\n",
"print \"\"\"The answer to life and everything is %s\"\"\" %value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"value = [4,2]\n",
"print \"\"\"The answer to life and everything is composed of {} and {}\"\"\".format(*value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"input_string = \"\"\"Some text <br /> has some <br /> additional\\\n",
" &nbsp; weird sequences &nbsp; between &nbsp; spaces <br /> &nbsp;.\"\"\"\n",
"print input_string.replace('<br /> ', '').replace(' &nbsp;', '')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"replace_awfull_seq = ['<br /> ', ' &nbsp;']\n",
"input_string = \"\"\"Some text <br /> has some <br /> additional\\\n",
" &nbsp; weird sequences &nbsp; between &nbsp; spaces <br /> &nbsp;.\"\"\"\n",
"\n",
"def replace_all(text, items):\n",
" for i in items:\n",
" text = text.replace(i, '')\n",
" return text\n",
"replace_all(input_string, replace_awfull_seq)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as pyplot\n",
"from datetime import datetime\n",
"import time\n",
"import os, calendar\n",
"import urllib"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Download Weather Data and Save it to a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"weather_info = ['Prague', 'Czech+Republic', '2015', '10', '17']\n",
"\n",
"file_name = 'weather_data.csv'\n",
"clean_file_name = 'clean_weather_data.csv'\n",
"\n",
"def download_data(weather_info, weather_file):\n",
" weather_url = \"\"\"http://www.wunderground.com/history/airport/LKPR/{2}/{3}/{4}\\\n",
" /DailyHistory.html?req_city={0}\\\n",
" &req_state=&req_statename={1}\\\n",
" &reqdb.zip=00000&reqdb.magic=1&reqdb.wmo=11518&format=1\"\"\".format(*weather_info)\n",
" download_file = urllib.URLopener()\n",
" download_file.retrieve(weather_url, weather_file)\n",
"\n",
"download_data(weather_info, file_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Clean the file to a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"noisy_data_file = open(file_name, \"r\")\n",
"cleaned_file = open(clean_file_name, \"w\")\n",
"\n",
"cleaned_file.write(noisy_data_file.read().replace(\"<br />\",\"\").replace('\\n','',1))\n",
"noisy_data_file.close()\n",
"\n",
"cleaned_file.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Process the data in the file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def date2int(date_str):\n",
" date = datetime.strptime(date_str, '%I:%M %p')\n",
" date = date.strftime('%H:%M')\n",
" return date\n",
"\n",
"def read_weather(file_name):\n",
" dtypes = np.dtype({ 'names' : ('timestamp', 'temp', 'events'),\n",
" 'formats' : ['S100', np.float, 'S100'] })\n",
"\n",
" data = np.loadtxt(file_name, delimiter=',', skiprows=1, \n",
" converters = { 0 : date2int },\n",
" usecols=(0,1,11), dtype=dtypes)\n",
"\n",
" return data\n",
"data = read_weather('clean_weather_data.csv')\n",
"# mean_temps = np.mean(data['temp'])\n",
"# min_temps = np.min(data['temp'])\n",
"# max_temps = np.max(data['temp'])\n",
"hour_list = [int(t[:2]) for t in data['timestamp']]\n",
"temperature = data['temp']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Display results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"temp_hourly = pyplot.plot(hour_list,temperature)\n",
"pyplot.title('Temperatures in Prague 17 Oct 2015')\n",
"pyplot.ylabel('Temperature (C)')\n",
"pyplot.xlabel('Hours of the day')\n",
"pyplot.xticks(np.arange(min(hour_list), max(hour_list)+2, 2.0))\n",
"pyplot.show(temp_hourly)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment