def test_centered_bins(self): """See that we can compute some nice centered bins""" a = plot.centered_bins(10, bins=9) self.assertEquals(a[0], -12) a = plot.centered_bins(55, bins=9) self.assertEquals(a[0], -56) a = plot.centered_bins(99, bins=9) self.assertEquals(a[0], -100) a = plot.centered_bins(0.9, bins=9) self.assertEquals(a[0], -0.9)
def test_centered_bins(): """See that we can compute some nice centered bins""" a = centered_bins(10, bins=9) assert a[0] == -12 a = centered_bins(55, bins=9) assert a[0] == -56 a = centered_bins(99, bins=9) assert a[0] == -100 a = centered_bins(0.9, bins=9) assert a[0] == -0.9
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') ctx = get_autoplot_context(fdict, get_description()) state = ctx['state'][:2] sector = ctx['sector'] opt = ctx['opt'] p1syear = ctx['p1syear'] p1eyear = ctx['p1eyear'] p2syear = ctx['p2syear'] p2eyear = ctx['p2eyear'] varname = ctx['var'] table = "alldata" if sector == 'state': table = "alldata_%s" % (state, ) df = read_sql(""" WITH season1 as ( SELECT station, year, min(case when month > 7 and low < 32 then extract(doy from day) else 366 end) as first_freeze, max(case when month < 7 and low < 32 then extract(doy from day) else 0 end) as last_freeze from """ + table + """ WHERE year >= %s and year <= %s GROUP by station, year), season2 as ( SELECT station, year, min(case when month > 7 and low < 32 then extract(doy from day) else 366 end) as first_freeze, max(case when month < 7 and low < 32 then extract(doy from day) else 0 end) as last_freeze from """ + table + """ WHERE year >= %s and year <= %s GROUP by station, year), agg as ( SELECT p1.station, avg(p1.first_freeze) as p1_first_fall, avg(p1.last_freeze) as p1_last_spring, avg(p2.first_freeze) as p2_first_fall, avg(p2.last_freeze) as p2_last_spring from season1 as p1 JOIN season2 as p2 on (p1.station = p2.station) GROUP by p1.station) SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, d.* from agg d JOIN stations t ON (d.station = t.id) WHERE t.network in ('IACLIMATE', 'NDCLIMATE', 'SDCLIMATE', 'NECLIMATE', 'KSCLIMATE', 'MOCLIMATE', 'ILCLIMATE', 'WICLIMATE', 'MNCLIMATE', 'MICLIMATE', 'INCLIMATE', 'OHCLIMATE', 'KYCLIMATE') and substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000' """, pgconn, params=[p1syear, p1eyear, p2syear, p2eyear], index_col='station') df['p1_season'] = df['p1_first_fall'] - df['p1_last_spring'] df['p2_season'] = df['p2_first_fall'] - df['p2_last_spring'] df['season_delta'] = df['p2_season'] - df['p1_season'] df['spring_delta'] = df['p2_last_spring'] - df['p1_last_spring'] df['fall_delta'] = df['p2_first_fall'] - df['p1_first_fall'] # Reindex so that most extreme values are first df = df.reindex(df[varname + '_delta'].abs().sort_values(ascending=False).index) title = PDICT3[varname] m = MapPlot(sector=sector, state=state, axisbg='white', title=('%.0f-%.0f minus %.0f-%.0f %s Difference') % (p2syear, p2eyear, p1syear, p1eyear, title), subtitle=('based on IEM Archives'), titlefontsize=14) # Create 9 levels centered on zero abval = df[varname + '_delta'].abs().max() levels = centered_bins(abval) if opt in ['both', 'contour']: m.contourf(df['lon'].values, df['lat'].values, df[varname + '_delta'].values, levels, cmap=plt.get_cmap('seismic'), units='days') if sector == 'state': m.drawcounties() if opt in ['both', 'values']: m.plot_values(df['lon'].values, df['lat'].values, df[varname + '_delta'].values, fmt='%.1f', labelbuffer=5) return m.fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) state = ctx['state'] varname = ctx['var'] sector = ctx['sector'] threshold = ctx['threshold'] opt = ctx['opt'] month = ctx['month'] p1syear = ctx['p1syear'] p1eyear = ctx['p1eyear'] p1yearreq = (p1eyear - p1syear) p2syear = ctx['p2syear'] p2eyear = ctx['p2eyear'] p2yearreq = (p2eyear - p2syear) opt1 = ctx['opt1'] if month == 'all': months = range(1, 13) elif month == 'fall': months = [9, 10, 11] elif month == 'winter': months = [12, 1, 2] elif month == 'spring': months = [3, 4, 5] elif month == 'summer': months = [6, 7, 8] elif month == 'gs': months = [5, 6, 7, 8, 9] else: ts = datetime.datetime.strptime("2000-"+month+"-01", '%Y-%b-%d') # make sure it is length two for the trick below in SQL months = [ts.month] table = "alldata" if sector == 'state': # optimization table = "alldata_%s" % (state,) df = read_sql(""" WITH period1 as ( SELECT station, year, sum(precip) as total_precip, avg((high+low) / 2.) as avg_temp, avg(high) as avg_high, avg(low) as avg_low, sum(gddxx(50, 86, high, low)) as sum_gdd, sum(case when high > 86 then high - 86 else 0 end) as sum_sdd, sum(case when high >= %s then 1 else 0 end) as days_high_above from """ + table + """ WHERE year >= %s and year < %s and month in %s GROUP by station, year), period2 as ( SELECT station, year, sum(precip) as total_precip, avg((high+low) / 2.) as avg_temp, avg(high) as avg_high, avg(low) as avg_low, sum(gddxx(50, 86, high, low)) as sum_gdd, sum(case when high > 86 then high - 86 else 0 end) as sum_sdd, sum(case when high >= %s then 1 else 0 end) as days_high_above from """ + table + """ WHERE year >= %s and year < %s and month in %s GROUP by station, year), p1agg as ( SELECT station, avg(total_precip) as precip, avg(avg_temp) as avg_temp, avg(avg_high) as avg_high, avg(avg_low) as avg_low, avg(sum_sdd) as sdd, avg(sum_gdd) as gdd, avg(days_high_above) as avg_days_high_above, count(*) as count from period1 GROUP by station), p2agg as ( SELECT station, avg(total_precip) as precip, avg(avg_temp) as avg_temp, avg(avg_high) as avg_high, avg(avg_low) as avg_low, avg(sum_sdd) as sdd, avg(sum_gdd) as gdd, avg(days_high_above) as avg_days_high_above, count(*) as count from period2 GROUP by station), agg as ( SELECT p2.station, p2.precip as p2_total_precip, p1.precip as p1_total_precip, p2.gdd as p2_gdd, p1.gdd as p1_gdd, p2.sdd as p2_sdd, p1.sdd as p1_sdd, p2.avg_temp as p2_avg_temp, p1.avg_temp as p1_avg_temp, p1.avg_high as p1_avg_high, p2.avg_high as p2_avg_high, p1.avg_low as p1_avg_low, p2.avg_low as p2_avg_low, p1.avg_days_high_above as p1_days_high_above, p2.avg_days_high_above as p2_days_high_above from p1agg p1 JOIN p2agg p2 on (p1.station = p2.station) WHERE p1.count >= %s and p2.count >= %s) SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, d.* from agg d JOIN stations t ON (d.station = t.id) WHERE t.network ~* 'CLIMATE' and substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000' """, pgconn, params=[threshold, p1syear, p1eyear, tuple(months), threshold, p2syear, p2eyear, tuple(months), p1yearreq, p2yearreq], index_col=None) df['total_precip'] = df['p2_total_precip'] - df['p1_total_precip'] df['avg_temp'] = df['p2_avg_temp'] - df['p1_avg_temp'] df['avg_high'] = df['p2_avg_high'] - df['p1_avg_high'] df['avg_low'] = df['p2_avg_low'] - df['p1_avg_low'] df['gdd'] = df['p2_gdd'] - df['p1_gdd'] df['sdd'] = df['p2_sdd'] - df['p1_sdd'] df['days_high_above'] = (df['p2_days_high_above'] - df['p1_days_high_above']) column = varname title = "%s %s" % (MDICT[month], PDICT3[varname]) title = title.replace("[Threshold]", '%.1f' % (threshold,)) if opt1 == 'p1': column = 'p1_%s' % (varname,) title = '%.0f-%.0f %s' % (p1syear, p1eyear, title) else: title = ('%.0f-%.0f minus %.0f-%.0f %s Difference' ) % (p2syear, p2eyear, p1syear, p1eyear, title) # Reindex so that most extreme values are first df = df.reindex(df[column].abs().sort_values(ascending=False).index) # drop 5% most extreme events, too much? df2 = df.iloc[int(len(df.index) * 0.05):] mp = MapPlot(sector=sector, state=state, axisbg='white', title=title, subtitle=('based on IEM Archives'), titlefontsize=12) if opt1 == 'diff': # Create 9 levels centered on zero abval = df2[column].abs().max() levels = centered_bins(abval) else: levels = [round(v, PRECISION[varname]) for v in np.percentile(df2[column].values, range(0, 101, 10))] if opt in ['both', 'contour']: mp.contourf(df2['lon'].values, df2['lat'].values, df2[column].values, levels, cmap=plt.get_cmap(('seismic_r' if varname == 'total_precip' else 'seismic')), units=UNITS[varname]) if sector == 'state': mp.drawcounties() if opt in ['both', 'values']: mp.plot_values(df2['lon'].values, df2['lat'].values, df2[column].values, fmt='%%.%if' % (PRECISION[varname],), labelbuffer=5) return mp.fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn("coop") ctx = get_autoplot_context(fdict, get_description()) state = ctx["state"][:2] sector = ctx["sector"] opt = ctx["opt"] p1syear = ctx["p1syear"] p1eyear = ctx["p1eyear"] p2syear = ctx["p2syear"] p2eyear = ctx["p2eyear"] varname = ctx["var"] table = "alldata" if sector == "state": table = "alldata_%s" % (state,) df = read_sql( """ WITH season1 as ( SELECT station, year, min(case when month > 7 and low < 32 then extract(doy from day) else 366 end) as first_freeze, max(case when month < 7 and low < 32 then extract(doy from day) else 0 end) as last_freeze from """ + table + """ WHERE year >= %s and year <= %s GROUP by station, year), season2 as ( SELECT station, year, min(case when month > 7 and low < 32 then extract(doy from day) else 366 end) as first_freeze, max(case when month < 7 and low < 32 then extract(doy from day) else 0 end) as last_freeze from """ + table + """ WHERE year >= %s and year <= %s GROUP by station, year), agg as ( SELECT p1.station, avg(p1.first_freeze) as p1_first_fall, avg(p1.last_freeze) as p1_last_spring, avg(p2.first_freeze) as p2_first_fall, avg(p2.last_freeze) as p2_last_spring from season1 as p1 JOIN season2 as p2 on (p1.station = p2.station) GROUP by p1.station) SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, d.* from agg d JOIN stations t ON (d.station = t.id) WHERE t.network ~* 'CLIMATE' and substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000' """, pgconn, params=[p1syear, p1eyear, p2syear, p2eyear], index_col="station", ) if df.empty: raise NoDataFound("No Data Found") df["p1_season"] = df["p1_first_fall"] - df["p1_last_spring"] df["p2_season"] = df["p2_first_fall"] - df["p2_last_spring"] df["season_delta"] = df["p2_season"] - df["p1_season"] df["spring_delta"] = df["p2_last_spring"] - df["p1_last_spring"] df["fall_delta"] = df["p2_first_fall"] - df["p1_first_fall"] # Reindex so that most extreme values are first df = df.reindex( df[varname + "_delta"].abs().sort_values(ascending=False).index ) title = PDICT3[varname] mp = MapPlot( sector=sector, state=state, axisbg="white", title=("%.0f-%.0f minus %.0f-%.0f %s Difference") % (p2syear, p2eyear, p1syear, p1eyear, title), subtitle=("based on IEM Archives"), titlefontsize=14, ) # Create 9 levels centered on zero abval = df[varname + "_delta"].abs().max() levels = centered_bins(abval) if opt in ["both", "contour"]: mp.contourf( df["lon"].values, df["lat"].values, df[varname + "_delta"].values, levels, cmap=get_cmap(ctx["cmap"]), units="days", ) if sector == "state": mp.drawcounties() if opt in ["both", "values"]: mp.plot_values( df["lon"].values, df["lat"].values, df[varname + "_delta"].values, fmt="%.1f", labelbuffer=5, ) return mp.fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn("coop") ctx = get_autoplot_context(fdict, get_description()) state = ctx["state"] varname = ctx["var"] sector = ctx["sector"] threshold = ctx["threshold"] opt = ctx["opt"] month = ctx["month"] p1syear = ctx["p1syear"] p1eyear = ctx["p1eyear"] p1yearreq = p1eyear - p1syear p2syear = ctx["p2syear"] p2eyear = ctx["p2eyear"] p2yearreq = p2eyear - p2syear opt1 = ctx["opt1"] if month == "all": months = range(1, 13) elif month == "fall": months = [9, 10, 11] elif month == "winter": months = [12, 1, 2] elif month == "spring": months = [3, 4, 5] elif month == "summer": months = [6, 7, 8] elif month == "gs": months = [5, 6, 7, 8, 9] else: ts = datetime.datetime.strptime("2000-" + month + "-01", "%Y-%b-%d") # make sure it is length two for the trick below in SQL months = [ts.month] table = "alldata" if sector == "state": # optimization table = "alldata_%s" % (state, ) df = read_sql( """ WITH period1 as ( SELECT station, year, sum(precip) as total_precip, avg((high+low) / 2.) as avg_temp, avg(high) as avg_high, avg(low) as avg_low, sum(gddxx(50, 86, high, low)) as sum_gdd, sum(case when high > 86 then high - 86 else 0 end) as sum_sdd, sum(case when high >= %s then 1 else 0 end) as days_high_above from """ + table + """ WHERE year >= %s and year < %s and month in %s GROUP by station, year), period2 as ( SELECT station, year, sum(precip) as total_precip, avg((high+low) / 2.) as avg_temp, avg(high) as avg_high, avg(low) as avg_low, sum(gddxx(50, 86, high, low)) as sum_gdd, sum(case when high > 86 then high - 86 else 0 end) as sum_sdd, sum(case when high >= %s then 1 else 0 end) as days_high_above from """ + table + """ WHERE year >= %s and year < %s and month in %s GROUP by station, year), p1agg as ( SELECT station, avg(total_precip) as precip, avg(avg_temp) as avg_temp, avg(avg_high) as avg_high, avg(avg_low) as avg_low, avg(sum_sdd) as sdd, avg(sum_gdd) as gdd, avg(days_high_above) as avg_days_high_above, count(*) as count from period1 GROUP by station), p2agg as ( SELECT station, avg(total_precip) as precip, avg(avg_temp) as avg_temp, avg(avg_high) as avg_high, avg(avg_low) as avg_low, avg(sum_sdd) as sdd, avg(sum_gdd) as gdd, avg(days_high_above) as avg_days_high_above, count(*) as count from period2 GROUP by station), agg as ( SELECT p2.station, p2.precip as p2_total_precip, p1.precip as p1_total_precip, p2.gdd as p2_gdd, p1.gdd as p1_gdd, p2.sdd as p2_sdd, p1.sdd as p1_sdd, p2.avg_temp as p2_avg_temp, p1.avg_temp as p1_avg_temp, p1.avg_high as p1_avg_high, p2.avg_high as p2_avg_high, p1.avg_low as p1_avg_low, p2.avg_low as p2_avg_low, p1.avg_days_high_above as p1_days_high_above, p2.avg_days_high_above as p2_days_high_above from p1agg p1 JOIN p2agg p2 on (p1.station = p2.station) WHERE p1.count >= %s and p2.count >= %s) SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, d.* from agg d JOIN stations t ON (d.station = t.id) WHERE t.network ~* 'CLIMATE' and substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000' """, pgconn, params=[ threshold, p1syear, p1eyear, tuple(months), threshold, p2syear, p2eyear, tuple(months), p1yearreq, p2yearreq, ], index_col=None, ) if df.empty: raise NoDataFound("No Data Found.") df["total_precip"] = df["p2_total_precip"] - df["p1_total_precip"] df["avg_temp"] = df["p2_avg_temp"] - df["p1_avg_temp"] df["avg_high"] = df["p2_avg_high"] - df["p1_avg_high"] df["avg_low"] = df["p2_avg_low"] - df["p1_avg_low"] df["gdd"] = df["p2_gdd"] - df["p1_gdd"] df["sdd"] = df["p2_sdd"] - df["p1_sdd"] df["days_high_above"] = df["p2_days_high_above"] - df["p1_days_high_above"] column = varname title = "%s %s" % (MDICT[month], PDICT3[varname]) title = title.replace("[Threshold]", "%.1f" % (threshold, )) if opt1 == "p1": column = "p1_%s" % (varname, ) title = "%.0f-%.0f %s" % (p1syear, p1eyear, title) else: title = ("%.0f-%.0f minus %.0f-%.0f %s Difference") % ( p2syear, p2eyear, p1syear, p1eyear, title, ) # Reindex so that most extreme values are first df = df.reindex(df[column].abs().sort_values(ascending=False).index) # drop 5% most extreme events, too much? df2 = df.iloc[int(len(df.index) * 0.05):] mp = MapPlot( sector=sector, state=state, axisbg="white", title=title, subtitle=("based on IEM Archives"), titlefontsize=12, ) if opt1 == "diff": # Create 9 levels centered on zero abval = df2[column].abs().max() levels = centered_bins(abval) else: levels = [ round(v, PRECISION[varname]) for v in np.percentile(df2[column].values, range(0, 101, 10)) ] if opt in ["both", "contour"]: mp.contourf( df2["lon"].values, df2["lat"].values, df2[column].values, levels, cmap=plt.get_cmap(ctx["cmap"]), units=UNITS[varname], ) if sector == "state": mp.drawcounties() if opt in ["both", "values"]: mp.plot_values( df2["lon"].values, df2["lat"].values, df2[column].values, fmt="%%.%if" % (PRECISION[varname], ), labelbuffer=5, ) return mp.fig, df
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') ctx = get_autoplot_context(fdict, get_description()) state = ctx['state'][:2] sector = ctx['sector'] opt = ctx['opt'] p1syear = ctx['p1syear'] p1eyear = ctx['p1eyear'] p2syear = ctx['p2syear'] p2eyear = ctx['p2eyear'] varname = ctx['var'] table = "alldata" if sector == 'state': table = "alldata_%s" % (state, ) df = read_sql(""" WITH season1 as ( SELECT station, year, min(case when month > 7 and low < 32 then extract(doy from day) else 366 end) as first_freeze, max(case when month < 7 and low < 32 then extract(doy from day) else 0 end) as last_freeze from """ + table + """ WHERE year >= %s and year <= %s GROUP by station, year), season2 as ( SELECT station, year, min(case when month > 7 and low < 32 then extract(doy from day) else 366 end) as first_freeze, max(case when month < 7 and low < 32 then extract(doy from day) else 0 end) as last_freeze from """ + table + """ WHERE year >= %s and year <= %s GROUP by station, year), agg as ( SELECT p1.station, avg(p1.first_freeze) as p1_first_fall, avg(p1.last_freeze) as p1_last_spring, avg(p2.first_freeze) as p2_first_fall, avg(p2.last_freeze) as p2_last_spring from season1 as p1 JOIN season2 as p2 on (p1.station = p2.station) GROUP by p1.station) SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, d.* from agg d JOIN stations t ON (d.station = t.id) WHERE t.network in ('IACLIMATE', 'NDCLIMATE', 'SDCLIMATE', 'NECLIMATE', 'KSCLIMATE', 'MOCLIMATE', 'ILCLIMATE', 'WICLIMATE', 'MNCLIMATE', 'MICLIMATE', 'INCLIMATE', 'OHCLIMATE', 'KYCLIMATE') and substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000' """, pgconn, params=[p1syear, p1eyear, p2syear, p2eyear], index_col='station') df['p1_season'] = df['p1_first_fall'] - df['p1_last_spring'] df['p2_season'] = df['p2_first_fall'] - df['p2_last_spring'] df['season_delta'] = df['p2_season'] - df['p1_season'] df['spring_delta'] = df['p2_last_spring'] - df['p1_last_spring'] df['fall_delta'] = df['p2_first_fall'] - df['p1_first_fall'] # Reindex so that most extreme values are first df = df.reindex(df[varname + '_delta'].abs().sort_values( ascending=False).index) title = PDICT3[varname] m = MapPlot(sector=sector, state=state, axisbg='white', title=('%.0f-%.0f minus %.0f-%.0f %s Difference' ) % (p2syear, p2eyear, p1syear, p1eyear, title), subtitle=('based on IEM Archives'), titlefontsize=14) # Create 9 levels centered on zero abval = df[varname + '_delta'].abs().max() levels = centered_bins(abval) if opt in ['both', 'contour']: m.contourf(df['lon'].values, df['lat'].values, df[varname + '_delta'].values, levels, cmap=plt.get_cmap('seismic'), units='days') if sector == 'state': m.drawcounties() if opt in ['both', 'values']: m.plot_values(df['lon'].values, df['lat'].values, df[varname + '_delta'].values, fmt='%.1f', labelbuffer=5) return m.fig, df
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') state = fdict.get('state', 'IA')[:2] varname = fdict.get('var', 'total_precip') sector = fdict.get('sector', 'state') threshold = float(fdict.get('threshold', -99)) opt = fdict.get('opt', 'both') month = fdict.get('month', 'all') p1syear = int(fdict.get('p1syear', 1951)) p1eyear = int(fdict.get('p1eyear', 1980)) p2syear = int(fdict.get('p2syear', 1981)) p2eyear = int(fdict.get('p2eyear', 2010)) if month == 'all': months = range(1, 13) elif month == 'fall': months = [9, 10, 11] elif month == 'winter': months = [12, 1, 2] elif month == 'spring': months = [3, 4, 5] elif month == 'summer': months = [6, 7, 8] elif month == 'gs': months = [5, 6, 7, 8, 9] else: ts = datetime.datetime.strptime("2000-"+month+"-01", '%Y-%b-%d') # make sure it is length two for the trick below in SQL months = [ts.month] table = "alldata" if sector == 'state': # optimization table = "alldata_%s" % (state,) df = read_sql(""" WITH period1 as ( SELECT station, year, sum(precip) as total_precip, avg((high+low) / 2.) as avg_temp, avg(high) as avg_high, avg(low) as avg_low, sum(gddxx(50, 86, high, low)) as sum_gdd, sum(case when high > 86 then high - 86 else 0 end) as sum_sdd, sum(case when high >= %s then 1 else 0 end) as days_high_above from """ + table + """ WHERE year >= %s and year < %s and month in %s GROUP by station, year), period2 as ( SELECT station, year, sum(precip) as total_precip, avg((high+low) / 2.) as avg_temp, avg(high) as avg_high, avg(low) as avg_low, sum(gddxx(50, 86, high, low)) as sum_gdd, sum(case when high > 86 then high - 86 else 0 end) as sum_sdd, sum(case when high >= %s then 1 else 0 end) as days_high_above from """ + table + """ WHERE year >= %s and year < %s and month in %s GROUP by station, year), p1agg as ( SELECT station, avg(total_precip) as precip, avg(avg_temp) as avg_temp, avg(avg_high) as avg_high, avg(avg_low) as avg_low, avg(sum_sdd) as sdd, avg(sum_gdd) as gdd, avg(days_high_above) as avg_days_high_above from period1 GROUP by station), p2agg as ( SELECT station, avg(total_precip) as precip, avg(avg_temp) as avg_temp, avg(avg_high) as avg_high, avg(avg_low) as avg_low, avg(sum_sdd) as sdd, avg(sum_gdd) as gdd, avg(days_high_above) as avg_days_high_above from period2 GROUP by station), agg as ( SELECT p2.station, p2.precip as p2_precip, p1.precip as p1_precip, p2.gdd as p2_gdd, p1.gdd as p1_gdd, p2.sdd as p2_sdd, p1.sdd as p1_sdd, p2.avg_temp as p2_avg_temp, p1.avg_temp as p1_avg_temp, p1.avg_high as p1_avg_high, p2.avg_high as p2_avg_high, p1.avg_low as p1_avg_low, p2.avg_low as p2_avg_low, p1.avg_days_high_above as p1_avg_days_high_above, p2.avg_days_high_above as p2_avg_days_high_above from p1agg p1 JOIN p2agg p2 on (p1.station = p2.station)) SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, d.* from agg d JOIN stations t ON (d.station = t.id) WHERE t.network in ('IACLIMATE', 'NDCLIMATE', 'SDCLIMATE', 'NECLIMATE', 'KSCLIMATE', 'MOCLIMATE', 'ILCLIMATE', 'WICLIMATE', 'MNCLIMATE', 'MICLIMATE', 'INCLIMATE', 'OHCLIMATE', 'KYCLIMATE') and substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000' """, pgconn, params=[threshold, p1syear, p1eyear, tuple(months), threshold, p2syear, p2eyear, tuple(months)], index_col=None) df['total_precip'] = df['p2_precip'] - df['p1_precip'] df['avg_temp'] = df['p2_avg_temp'] - df['p1_avg_temp'] df['avg_high'] = df['p2_avg_high'] - df['p1_avg_high'] df['avg_low'] = df['p2_avg_low'] - df['p1_avg_low'] df['gdd'] = df['p2_gdd'] - df['p1_gdd'] df['sdd'] = df['p2_sdd'] - df['p1_sdd'] df['days_high_above'] = (df['p2_avg_days_high_above'] - df['p1_avg_days_high_above']) # Reindex so that most extreme values are first df = df.reindex(df[varname].abs().sort_values(ascending=False).index) # drop 5% most extreme events, too much? df2 = df.iloc[int(len(df.index) * 0.05):] title = "%s %s" % (MDICT[month], PDICT3[varname]) title = title.replace("[Threshold]", '%.1f' % (threshold,)) m = MapPlot(sector=sector, state=state, axisbg='white', title=('%.0f-%.0f minus %.0f-%.0f %s Difference' ) % (p2syear, p2eyear, p1syear, p1eyear, title), subtitle=('based on IEM Archives'), titlefontsize=12) # Create 9 levels centered on zero abval = df2[varname].abs().max() levels = centered_bins(abval) if opt in ['both', 'contour']: m.contourf(df2['lon'].values, df2['lat'].values, df2[varname].values, levels, cmap=plt.get_cmap(('seismic_r' if varname == 'total_precip' else 'seismic')), units=UNITS[varname]) if sector == 'state': m.drawcounties() if opt in ['both', 'values']: m.plot_values(df2['lon'].values, df2['lat'].values, df2[varname].values, fmt='%%.%if' % (PRECISION[varname],)) return m.fig, df