def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) if ctx["p"] == "day": get_daily_data(ctx) else: get_monthly_data(ctx) ctx["lastyear"] = datetime.date.today().year ctx["years"] = ctx["lastyear"] - 1893 + 1 csector = ctx["csector"] subtitle = ("Based on IEM Estimates, " "1 is %s out of %s total years (1893-%s)") % ( "wettest" if ctx["var"] == "precip" else "hottest", ctx["years"], ctx["lastyear"], ) if ctx["var"] == "arridity": subtitle = "Std Average High Temp Departure minus Std Precip Departure" mp = MapPlot( sector=("state" if len(csector) == 2 else csector), state=ctx["csector"], continentalcolor="white", title="%s %s %sby Climate District" % ( ctx["label"], PDICT[ctx["var"]], "Ranks " if ctx["var"] != "arridity" else "", ), subtitle=subtitle, titlefontsize=14, ) cmap = get_cmap(ctx["cmap"]) bins = [ 1, 5, 10, 25, 50, 75, 100, ctx["years"] - 10, ctx["years"] - 5, ctx["years"], ] pvar = ctx["var"] + "_rank" fmt = "%.0f" if ctx["var"] == "arridity": bins = np.arange(-4, 4.1, 1) pvar = ctx["var"] fmt = "%.1f" mp.fill_climdiv( ctx["df"][pvar], ilabel=True, plotmissing=False, lblformat=fmt, bins=bins, cmap=cmap, ) return mp.fig, ctx["df"]
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) if ctx['p'] == 'day': get_daily_data(ctx) else: get_monthly_data(ctx) ctx['lastyear'] = datetime.date.today().year ctx['years'] = ctx['lastyear'] - 1893 + 1 csector = ctx['csector'] subtitle = ('Based on IEM Estimates, ' '1 is %s out of %s total years (1893-%s)') % ( 'wettest' if ctx['var'] == 'precip' else 'hottest', ctx['years'], ctx['lastyear']) if ctx['var'] == 'arridity': subtitle = "Std Average High Temp Departure minus Std Precip Departure" mp = MapPlot(sector=('state' if len(csector) == 2 else csector), state=ctx['csector'], continentalcolor='white', title='%s %s %sby Climate District' % (ctx['label'], PDICT[ctx['var']], 'Ranks ' if ctx['var'] != 'arridity' else ''), subtitle=subtitle, titlefontsize=14) cmap = cm.get_cmap("BrBG_r" if ctx['var'] in ['precip', 'arridity'] else 'BrBG') cmap.set_under('white') cmap.set_over('black') bins = [ 1, 5, 10, 25, 50, 75, 100, ctx['years'] - 10, ctx['years'] - 5, ctx['years'] ] pvar = ctx['var'] + '_rank' fmt = '%.0f' if ctx['var'] == 'arridity': bins = np.arange(-4, 4.1, 1) pvar = ctx['var'] fmt = '%.1f' mp.fill_climdiv(ctx['df'][pvar], ilabel=True, plotmissing=False, lblformat=fmt, bins=bins, cmap=cmap) return mp.fig, ctx['df']
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') year = int(fdict.get('year', 2014)) month = int(fdict.get('month', 9)) varname = fdict.get('var', 'precip') lastyear = datetime.date.today().year years = lastyear - 1893 + 1 df = read_sql(""" with monthly as ( SELECT year, station, sum(precip) as p, avg((high+low)/2.) as avgt from alldata WHERE substr(station,3,1) = 'C' and month = %s GROUP by year, station), ranks as ( SELECT station, year, rank() OVER (PARTITION by station ORDER by p DESC) as precip_rank, rank() OVER (PARTITION by station ORDER by avgt DESC) as avgt_rank from monthly) SELECT station, precip_rank, avgt_rank from ranks where year = %s """, pgconn, params=(month, year), index_col='station') m = MapPlot(sector='midwest', axisbg='white', title='%s %s %s Ranks by Climate District' % ( year, calendar.month_name[month], PDICT[varname]), subtitle=('Based on IEM Estimates, ' '1 is %s out of %s total years (1893-%s)' ) % ('wettest' if varname == 'precip' else 'hottest', years, lastyear) ) cmap = cm.get_cmap("BrBG_r" if varname == 'precip' else 'BrBG') cmap.set_under('white') cmap.set_over('black') m.fill_climdiv(df[varname+'_rank'], ilabel=True, plotmissing=False, bins=[1, 5, 10, 25, 50, 75, 100, years-10, years-5, years], cmap=cmap) return m.fig, df
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) year = int(fdict.get('year', 2014)) month = int(fdict.get('month', 9)) lastyear = datetime.date.today().year years = lastyear - 1893 + 1 cursor.execute(""" with monthly as ( SELECT year, station, sum(precip) as p from alldata WHERE substr(station,3,1) = 'C' and month = %s GROUP by year, station), ranks as ( SELECT station, year, rank() OVER (PARTITION by station ORDER by p DESC) from monthly) SELECT station, rank from ranks where year = %s """, (month, year)) data = {} rows = [] for row in cursor: data[row['station']] = row['rank'] rows.append(dict(station=row['station'], rank=row['rank'])) df = pd.DataFrame(rows) m = MapPlot(sector='midwest', axisbg='white', title='%s %s Precipitation Total Ranks by Climate District' % ( year, calendar.month_name[month]), subtitle=('Based on IEM Estimates, ' '1 is wettest out of %s total years (1893-%s)' ) % (years, lastyear) ) cmap = cm.get_cmap("BrBG_r") cmap.set_under('white') cmap.set_over('black') m.fill_climdiv(data, bins=[1, 5, 10, 25, 50, 75, 100, years-10, years-5, years], cmap=cmap) return m.fig, df
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') year = int(fdict.get('year', 2014)) month = fdict.get('month', 9) varname = fdict.get('var', 'precip') l = "0 days" if month == 'fall': months = [9, 10, 11] label = "Fall (SON)" elif month == 'winter': months = [12, 1, 2] l = "32 days" label = "Winter (DJF)" elif month == 'spring': months = [3, 4, 5] label = "Spring (MAM)" elif month == 'summer': months = [6, 7, 8] label = "Summer (JJA)" else: months = [ int(month), ] label = calendar.month_name[int(month)] lastyear = datetime.date.today().year years = lastyear - 1893 + 1 df = read_sql(""" with monthly as ( SELECT extract(year from day + '""" + l + """'::interval) as myyear, station, sum(precip) as p, avg((high+low)/2.) as avgt, avg(high) as avghi from alldata WHERE substr(station,3,1) = 'C' and month in %s GROUP by myyear, station), ranks as ( SELECT station, myyear as year, avg(p) OVER (PARTITION by station) as avg_precip, stddev(p) OVER (PARTITION by station) as std_precip, p as precip, avg(avghi) OVER (PARTITION by station) as avg_high, stddev(avghi) OVER (PARTITION by station) as std_high, avghi as high, rank() OVER (PARTITION by station ORDER by p DESC) as precip_rank, rank() OVER (PARTITION by station ORDER by avgt DESC) as avgt_rank from monthly) SELECT station, precip_rank, avgt_rank, ((high - avg_high) / std_high) - ((precip - avg_precip) / std_precip) as arridity from ranks where year = %s """, pgconn, params=(tuple(months), year), index_col='station') subtitle = ('Based on IEM Estimates, ' '1 is %s out of %s total years (1893-%s)') % ( 'wettest' if varname == 'precip' else 'hottest', years, lastyear) if varname == 'arridity': subtitle = "Std Average High Temp Departure minus Std Precip Departure" m = MapPlot(sector='midwest', axisbg='white', title='%s %s %s %sby Climate District' % (year, label, PDICT[varname], 'Ranks ' if varname != 'arridity' else ''), subtitle=subtitle) cmap = cm.get_cmap("BrBG_r" if varname in ['precip', 'arridity'] else 'BrBG') cmap.set_under('white') cmap.set_over('black') bins = [1, 5, 10, 25, 50, 75, 100, years - 10, years - 5, years] pvar = varname + '_rank' fmt = '%.0f' if varname == 'arridity': bins = np.arange(-4, 4.1, 1) pvar = varname fmt = '%.1f' m.fill_climdiv(df[pvar], ilabel=True, plotmissing=False, lblformat=fmt, bins=bins, cmap=cmap) return m.fig, df
from pyiem.plot import MapPlot import numpy import psycopg2 dbconn = psycopg2.connect(database='coop', host='iemdb', user='******') cursor = dbconn.cursor() clim = {} cursor.execute("""SELECT station, avg(sum) from (SELECT station, year, sum(precip) from alldata where year < 2013 and year > 1950 and substr(station,3,1) = 'C' and month in (4,5) and sday < '0520' GROUP by station, year) as foo GROUP by station""") for row in cursor: clim[ row[0] ] = row[1] data = {} cursor.execute("""SELECT station, sum(precip) from alldata where year = 2013 and substr(station,3,1) = 'C' and month in (4,5) GROUP by station""") for row in cursor: data[ row[0] ] = row[1] - clim[ row[0] ] m = MapPlot(sector='midwest', title='IEM Estimated Climate Division Precipitation Departure [inch]', subtitle='1 April thru 19 May 2013 vs 1950-2012 Climate') m.fill_climdiv(data, bins=numpy.arange(5,-5.1,-0.25), lblformat='%.1f') m.postprocess(filename='test.svg') import iemplot iemplot.makefeature('test')
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') year = int(fdict.get('year', 2014)) month = fdict.get('month', 9) varname = fdict.get('var', 'precip') l = "0 days" if month == 'fall': months = [9, 10, 11] label = "Fall (SON)" elif month == 'winter': months = [12, 1, 2] l = "32 days" label = "Winter (DJF)" elif month == 'spring': months = [3, 4, 5] label = "Spring (MAM)" elif month == 'summer': months = [6, 7, 8] label = "Summer (JJA)" else: months = [int(month), ] label = calendar.month_name[int(month)] lastyear = datetime.date.today().year years = lastyear - 1893 + 1 df = read_sql(""" with monthly as ( SELECT extract(year from day + '"""+l+"""'::interval) as myyear, station, sum(precip) as p, avg((high+low)/2.) as avgt, avg(high) as avghi from alldata WHERE substr(station,3,1) = 'C' and month in %s GROUP by myyear, station), ranks as ( SELECT station, myyear as year, avg(p) OVER (PARTITION by station) as avg_precip, stddev(p) OVER (PARTITION by station) as std_precip, p as precip, avg(avghi) OVER (PARTITION by station) as avg_high, stddev(avghi) OVER (PARTITION by station) as std_high, avghi as high, rank() OVER (PARTITION by station ORDER by p DESC) as precip_rank, rank() OVER (PARTITION by station ORDER by avgt DESC) as avgt_rank from monthly) SELECT station, precip_rank, avgt_rank, ((high - avg_high) / std_high) - ((precip - avg_precip) / std_precip) as arridity from ranks where year = %s """, pgconn, params=(tuple(months), year), index_col='station') subtitle = ('Based on IEM Estimates, ' '1 is %s out of %s total years (1893-%s)' ) % ('wettest' if varname == 'precip' else 'hottest', years, lastyear) if varname == 'arridity': subtitle = "Std Average High Temp Departure minus Std Precip Departure" m = MapPlot(sector='midwest', axisbg='white', title='%s %s %s %sby Climate District' % ( year, label, PDICT[varname], 'Ranks ' if varname != 'arridity' else ''), subtitle=subtitle) cmap = cm.get_cmap("BrBG_r" if varname in ['precip', 'arridity'] else 'BrBG') cmap.set_under('white') cmap.set_over('black') bins = [1, 5, 10, 25, 50, 75, 100, years-10, years-5, years] pvar = varname+'_rank' fmt = '%.0f' if varname == 'arridity': bins = np.arange(-4, 4.1, 1) pvar = varname fmt = '%.1f' m.fill_climdiv(df[pvar], ilabel=True, plotmissing=False, lblformat=fmt, bins=bins, cmap=cmap) return m.fig, df
import matplotlib.cm as cm pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) cursor.execute(""" with monthly as ( SELECT station, extract(year from day + '40 days'::interval) as yr, avg((high+low)/2.) from alldata where month in (12,1,2) and substr(station,3,1) = 'C' GROUP by station, yr ), nextup as ( SELECT station, yr, rank() OVER (PARTITION by station ORDER by avg ASC) from monthly ) SELECT station, rank from nextup where yr = 2015 """) data = {} for row in cursor: data[row['station']] = row['rank'] m = MapPlot(sector='midwest', axisbg='white', title='Dec 2014 - Jan/Feb 2015 Average Temperature Rank (1 is coldest)', subtitle='Based on IEM Estimates of Climate District Data (1893-2015)') cmap = cm.get_cmap("BrBG_r") cmap.set_under('black') cmap.set_over('black') m.fill_climdiv(data, cmap=cmap, bins=[1, 10, 25, 50, 75, 100, 115, 123]) m.postprocess(filename='test.png')
def test_plot3(): """ Exercise climdiv plot API """ mp = MapPlot(sector='iowa', nocaption=True) mp.fill_climdiv({'IAC001': 80, 'AKC003': 5, 'HIC003': 30, 'AJK': 40, 'HFO': 50}) return mp.fig
def test_climdiv(): """Run tests agains the fill_climdiv""" mp = MapPlot(sector='conus', title="Climate Divisions", nocaption=True) data = {'IAC001': 10, 'MNC001': 20, 'NMC001': 30} mp.fill_climdiv(data, ilabel=True) return mp.fig
pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) cursor.execute(""" with monthly as ( SELECT station, extract(year from day + '40 days'::interval) as yr, avg((high+low)/2.) from alldata where month in (12,1,2) and substr(station,3,1) = 'C' GROUP by station, yr ), nextup as ( SELECT station, yr, rank() OVER (PARTITION by station ORDER by avg ASC) from monthly ) SELECT station, rank from nextup where yr = 2015 """) data = {} for row in cursor: data[row['station']] = row['rank'] m = MapPlot( sector='midwest', axisbg='white', title='Dec 2014 - Jan/Feb 2015 Average Temperature Rank (1 is coldest)', subtitle='Based on IEM Estimates of Climate District Data (1893-2015)') cmap = cm.get_cmap("BrBG_r") cmap.set_under('black') cmap.set_over('black') m.fill_climdiv(data, cmap=cmap, bins=[1, 10, 25, 50, 75, 100, 115, 123]) m.postprocess(filename='test.png')