|
|
@@ -1,9 +1,12 @@ |
|
|
import pandas as pd
|
|
|
import datetime
|
|
|
+import psycopg2
|
|
|
+from pandas.io.sql import read_sql
|
|
|
from collections import OrderedDict
|
|
|
from pyiem.meteorology import gdd
|
|
|
from pyiem.datatypes import temperature, distance
|
|
|
from pyiem.util import get_autoplot_context
|
|
|
+from scipy import stats
|
|
|
|
|
|
STATIONS = OrderedDict([
|
|
|
('ames', 'Central (Ames)'),
|
|
@@ -18,6 +21,26 @@ |
|
|
('jan1', 'January 1'),
|
|
|
('mar15', 'March 15'),
|
|
|
])
|
|
|
+COUNTY = {'ames': 169, 'cobs': 169, 'crawfordsville': 183, 'lewis': 155,
|
|
|
+ 'nashua': 67, 'sutherland': 141}
|
|
|
+PDICT = {'yes': 'Colorize Labels by Corn Yield Trend',
|
|
|
+ 'no': 'No Colorize'}
|
|
|
+
|
|
|
+
|
|
|
+def load_yields(location):
|
|
|
+ """Loads up the county corn yields"""
|
|
|
+ pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')
|
|
|
+ df = read_sql("""select year, num_value as yield
|
|
|
+ from nass_quickstats where
|
|
|
+ county_ansi = %s and state_alpha = 'IA' and year >= 1980
|
|
|
+ and commodity_desc = 'CORN' and statisticcat_desc = 'YIELD'
|
|
|
+ and unit_desc = 'BU / ACRE' ORDER by year ASC
|
|
|
+ """, pgconn, params=(COUNTY[location],), index_col='year')
|
|
|
+ slp, intercept, _, _, _ = stats.linregress(df.index.values,
|
|
|
+ df['yield'].values)
|
|
|
+ df['model'] = slp * df.index.values + intercept
|
|
|
+ df['departure'] = 100. * (df['yield'] - df['model']) / df['model']
|
|
|
+ return df
|
|
|
|
|
|
|
|
|
def get_description():
|
|
@@ -29,6 +52,8 @@ def get_description(): |
|
|
label='Select Location:', options=STATIONS),
|
|
|
dict(type='select', name='s', default='jan1',
|
|
|
label='Select Plot Start Date:', options=SDATES),
|
|
|
+ dict(type='select', name='opt', default='no',
|
|
|
+ label='Plot Corn Yield Trends:', options=PDICT),
|
|
|
]
|
|
|
return d
|
|
|
|
|
@@ -89,8 +114,11 @@ def plotter(fdict): |
|
|
import matplotlib
|
|
|
matplotlib.use('agg')
|
|
|
import matplotlib.pyplot as plt
|
|
|
+ import matplotlib.patheffects as PathEffects
|
|
|
ctx = get_autoplot_context(fdict, get_description())
|
|
|
location = ctx['location']
|
|
|
+ opt = ctx['opt']
|
|
|
+ yields = load_yields(location)
|
|
|
sdate = ctx['s']
|
|
|
# we need to compute totals using two datasources
|
|
|
df = load("/mesonet/share/pickup/yieldfx", location, sdate)
|
|
@@ -120,10 +148,26 @@ def plotter(fdict): |
|
|
|
|
|
(fig, ax) = plt.subplots(1, 1)
|
|
|
for year, row in resdf.iterrows():
|
|
|
- color = 'r' if year == today.year else 'k'
|
|
|
- ax.text(row['avgt'], row['rain'], "%s" % (str(year)[-2:],),
|
|
|
- color=color, ha='center', va='center', zorder=4)
|
|
|
-
|
|
|
+ c = 'k'
|
|
|
+ sz = 10.
|
|
|
+ if year in yields.index and opt == 'yes':
|
|
|
+ myyield = yields.at[year, 'departure']
|
|
|
+ sz = 10. + (abs(int(myyield)) / 50.) * 20.
|
|
|
+ c = 'g' if myyield > 0 else 'r'
|
|
|
+ txt = ax.text(row['avgt'], row['rain'], "%s" % (str(year)[-2:],),
|
|
|
+ color=c, ha='center', va='center', zorder=4,
|
|
|
+ fontsize=sz)
|
|
|
+ txt.set_path_effects([PathEffects.withStroke(linewidth=2,
|
|
|
+ foreground="w")])
|
|
|
+ if opt == 'yes':
|
|
|
+ for y, x in enumerate(range(-50, 51, 10)):
|
|
|
+ if x == 0:
|
|
|
+ continue
|
|
|
+ sz = 10. + (abs(x) / 50.) * 20.
|
|
|
+ c = 'g' if x > 0 else 'r'
|
|
|
+ p = '+' if x > 0 else ''
|
|
|
+ ax.text(1.01, float(y / 11.), "%s%s%%" % (p, x),
|
|
|
+ transform=ax.transAxes, color=c, fontsize=sz)
|
|
|
xavg = resdf['avgt'].mean()
|
|
|
ax.axvline(xavg)
|
|
|
dx = (resdf['avgt'] - xavg).abs().max()
|
|
@@ -134,9 +178,10 @@ def plotter(fdict): |
|
|
dy = (resdf['rain'] - yavg).abs().max()
|
|
|
ax.set_ylim(max([0, yavg - (dy * 1.1)]), yavg + (dy * 1.1))
|
|
|
|
|
|
+ sts = datetime.datetime.strptime(sdate, '%b%d')
|
|
|
ax.set_title(("%s %s to %s [%s-%s]"
|
|
|
) % (STATIONS[location],
|
|
|
- '1 November' if sdate == 'nov1' else '15 March',
|
|
|
+ sts.strftime("%-d %B"),
|
|
|
today.strftime("%-d %B"), 1980,
|
|
|
today.year))
|
|
|
ax.set_xlabel("Average Temperature [$^\circ$F]")
|
|
@@ -150,6 +195,7 @@ def plotter(fdict): |
|
|
ax.text(0.85, 0.05, "Hot & Dry", transform=ax.transAxes,
|
|
|
fontsize=14, color='b', zorder=3, ha='center', va='center')
|
|
|
ax.grid(True)
|
|
|
+ ax.set_position([0.1, 0.1, 0.7, 0.8])
|
|
|
|
|
|
return fig, resdf
|
|
|
|
|
|
0 comments on commit
026f154