<commit_before>import iemdb
import numpy
import mx.DateTime
COOP = iemdb.connect('coop', bypass=True)
ccursor = COOP.cursor()

MESOSITE = iemdb.connect('mesosite', bypass=True)
mcursor = MESOSITE.cursor()

elnino = []
mcursor.execute("""SELECT anom_34 from elnino where monthdate >= '2007-01-01'""")
for row in mcursor:
    elnino.append( float(row[0]) )

elnino = numpy.array(elnino)

climate = []
ccursor.execute("""
 SELECT avg(d), month from (SELECT year, month, sum(precip) as d from alldata_ia 
 where station = 'IA0200' and day < '2013-01-01'
 GROUP by year, month) as foo GROUP by month ORDER by month ASC
""")
for row in ccursor:
  climate.append( float(row[0]) )

diff = []
ccursor.execute("""
 SELECT year, month, sum(precip) from alldata_ia where 
 station = 'IA0200' and year > 2006 
 GROUP by year, month ORDER by year, month ASC
""")
for row in ccursor:
    diff.append( float(row[2]) - climate[ row[1] -1] )

diff = numpy.array(diff)

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("Ames Monthly Precipitation Departure\nEl Nino 3.4 Index")
#"""
xticks = []
xticklabels = []
for i in range(0, len(diff),6):
  ts = mx.DateTime.DateTime(2007,1,1) + mx.DateTime.RelativeDateTime(months=i)
  if ts.month == 1:
    fmt = "%b\n%Y"
  else:
    fmt = "%b"
  xticklabels.append( ts.strftime(fmt) )
  xticks.append( i )

bars = ax.bar(numpy.arange(0, len(diff))-0.4, diff, fc='b', ec='b')
for bar in bars:
  if bar.get_xy()[1] < 0:
    bar.set_facecolor('r')
    bar.set_edgecolor('r')

ax2 = ax.twinx()

ax2.plot(numpy.arange(0, len(elnino)), elnino, zorder=2, color='k', lw=2.0)
ax2.set_ylabel("El Nino 3.4 Index (line)")

ax.set_ylabel("Precip Departure [inch] (bars)")
#ax.set_xlabel("* Thru 28 May 2013")
ax.grid(True)
ax.set_xticks( xticks )
ax.set_xticklabels( xticklabels )
ax.set_xlim(-0.5, len(diff)+0.5)
ax.set_ylim(-8,8)
"""
import scipy.stats<commit_msg>1 May feature<commit_after>import psycopg2
import numpy
import datetime
COOP = psycopg2.connect(database='coop', host='iemdb', user='nobody')
ccursor = COOP.cursor()

MESOSITE = psycopg2.connect(database='mesosite', host='iemdb', user='nobody')
mcursor = MESOSITE.cursor()

elnino = []
elninovalid = []
mcursor.execute("""SELECT anom_34, monthdate from elnino 
    where monthdate >= '2007-01-01' ORDER by monthdate ASC""")
for row in mcursor:
    elnino.append( float(row[0]) )
    elninovalid.append( row[1] )

elnino = numpy.array(elnino)

ccursor.execute("""
 WITH climo as (
 SELECT month, avg((high+low)/2.) from alldata_ia where station = 'IA0200'
 and day < '2014-05-01' GROUP by month),
 
 obs as (
 SELECT year, month, avg((high+low)/2.) from alldata_ia where station = 'IA0200'
 and day < '2014-05-01' and year > 1899 GROUP by year, month)

 SELECT obs.year, obs.month, obs.avg - climo.avg from obs JOIN climo on
 (climo.month = obs.month) 
 ORDER by obs.year ASC, obs.month ASC

""")
diff = []
valid = []
running  = 0
maxrunning = 0
for row in ccursor:
    #print row
    valid.append( datetime.datetime(row[0], row[1], 1) )
    diff.append( row[2] )
    if row[2] < 0:
        running += 1
    else:
        if running > maxrunning:
            print '----->', running, row[0], row[1]
            maxrunning = running
        running = 0

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("Ames Monthly Departure of Average Temperature\nEl Nino 3.4 Index")
#"""
xticks = []
xticklabels = []
for v in valid:
    if v.month not in [1,7]:
        continue
    if v.month == 1:
        fmt = "%b\n%Y"
    else:
        fmt = "%b"
    xticklabels.append( v.strftime(fmt) )
    xticks.append( v )

bars = ax.bar(valid, diff, fc='r', ec='r', width=30)
for bar in bars:
    if bar.get_xy()[1] < 0:
        bar.set_facecolor('b')
        bar.set_edgecolor('b')

ax2 = ax.twinx()

ax2.plot(elninovalid, elnino, zorder=2, color='k', lw=2.0)
ax2.set_ylabel("El Nino 3.4 Index (line)")

ax.set_ylabel("Avg Temperature Departure [F] (bars)")
#ax.set_xlabel("* Thru 28 May 2013")
ax.grid(True)
ax.set_xticks( xticks )
ax.set_xticklabels( xticklabels )
ax.set_xlim(datetime.datetime(2007,1,1), datetime.datetime(2014,5,1))
#ax.set_ylim(-8,8)
"""
import scipy.stats
{'fraction_non_alphanumeric': 0.16125207493478777, 'fraction_numerical': 0.04102442494664454, 'mean_word_length': 3.887601390498262, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5512232', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1645, 'n_words': 412}