Skip to content

Instantly share code, notes, and snippets.

@pmarshwx
Created May 4, 2016 18:41
Show Gist options
  • Save pmarshwx/e0051bab2d04b80bcaf4a2bc24584423 to your computer and use it in GitHub Desktop.
Save pmarshwx/e0051bab2d04b80bcaf4a2bc24584423 to your computer and use it in GitHub Desktop.
Climate Traces
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import datetime\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.dates as mdates\n",
"from scipy.ndimage import convolve\n",
"from matplotlib.lines import Line2D\n",
"from matplotlib.patches import Rectangle"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def moving_average(x, n, type='simple'):\n",
" \"\"\"\n",
" compute an n period moving average.\n",
"\n",
" type is 'simple' | 'exponential'\n",
"\n",
" \"\"\"\n",
" x = np.asarray(x)\n",
" if type=='simple':\n",
" weights = np.ones(n)\n",
" else:\n",
" weights = np.exp(np.linspace(-1., 0., n))\n",
" weights /= weights.sum()\n",
" try:\n",
" return convolve(x, weights, mode='mirror')\n",
" except:\n",
" return convolve(x, weights, mode='same')\n",
"\n",
"def add_legend(ax):\n",
" dist = 30\n",
" line0 = Line2D(range(dist), range(dist), ls='-', lw=4, color='r')\n",
" line1 = Line2D(range(dist), range(dist), ls='-', lw=4, color='b')\n",
" line2 = Line2D(range(dist), range(dist), ls='--', lw=2, color='r')\n",
" line3 = Line2D(range(dist), range(dist), ls='--', lw=2, color='b')\n",
" rect0 = Rectangle((0,0), 1, 1, ec='k', fc='k', alpha=0.35, lw=3)\n",
" rect1 = Rectangle((0,0), 1, 1, ec='#C0C0C0', fc='#888888', alpha=0.67, lw=3)\n",
" label0 = 'High Temperature'\n",
" label1 = 'Low Temperature'\n",
" label2 = '15-day High Temperature Running Mean'\n",
" label3 = '15-day Low Temperature Running Mean'\n",
" label4 = 'Daily Normals'\n",
" label5 = 'All-time Temperature Range'\n",
" lines = [line0, line1, line2, line3, rect0, rect1]\n",
" labels = [label0, label1, label2, label3, label4, label5]\n",
" leg = ax.legend(lines, labels, loc=8, shadow=True, fancybox=True)\n",
" return leg"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"datapath = r\"/Users/pmarshwx/dropbox/data/datasets/climo/stations\"\n",
"stn = 'mspthr'\n",
"STN = stn[:3].upper()\n",
"df = pd.read_csv(os.path.join(datapath, stn+'.csv'), delimiter='|', na_values=['M'])\n",
"dfYR = df.groupby(\"yr\")\n",
"dfDOY366 = df.groupby(\"doy366\")\n",
"genericYear = np.asarray([datetime.datetime.strptime(str(i), '%j') for i in range(1, 367)])\n",
"YR = 2016\n",
"doys = range(1, 367)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"min_year = df.yr.values[0]\n",
"max_year = df.yr.values[-1]\n",
"movingMaxT = moving_average(dfYR.get_group(YR).maxt.values, 15)\n",
"movingMinT = moving_average(dfYR.get_group(YR).mint.values, 15)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment