Esempio n. 1
0
def run(year):
    cursor.execute(
        """
    WITH obs as (
    SELECT station, sum(precip) from alldata_ia where year = %s GROUP by station
    ), climate as (
    SELECT station, sum(precip) from climate51 GROUP by station
    )
    SELECT o.station, o.sum - c.sum as diff from obs o JOIN climate c 
    on (c.station = o.station) ORDER by diff ASC
    """, (year, ))
    lats = []
    lons = []
    vals = []
    for row in cursor:
        if not nt.sts.has_key(row[0]) or row[0] in rejs or row[0][2] == 'C':
            continue
        print row
        lats.append(nt.sts[row[0]]['lat'])
        lons.append(nt.sts[row[0]]['lon'])
        vals.append(row[1])

    m = MapPlot(title='%s Precipitation Departure' % (year, ))
    cmap = cm.get_cmap('BrBG')
    #cmap.set_over('blue')
    #cmap.set_under('red')
    m.contourf(lons,
               lats,
               vals,
               np.arange(-24, 24.1, 2),
               cmap=cmap,
               units='inch')
    #m.plot_values(lons, lats, vals, '%.02f')
    m.drawcounties()
    m.postprocess(filename='%s.png' % (year, ))
Esempio n. 2
0
def run(base, ceil, now, fn):
    """ Generate the plot """
    # Compute normal from the climate database
    sql = """SELECT station,
       sum(gddxx(%s, %s, high, low)) as gdd
       from alldata_ia WHERE year = %s and month in (5,6,7,8,9,10)
       and station != 'IA0000' and substr(station,2,1) != 'C'
       GROUP by station""" % (base, ceil, now.year)

    lats = []
    lons = []
    gdd50 = []
    ccursor.execute(sql)
    for row in ccursor:
        if row[0] not in nt.sts:
            continue
        lats.append(nt.sts[row[0]]['lat'])
        lons.append(nt.sts[row[0]]['lon'])
        gdd50.append(float(row[1]))

    m = MapPlot(title=("Iowa 1 May - %s GDD Accumulation"
                       ) % (now.strftime("%-d %B %Y"), ),
                subtitle="base %s" % (base,))
    bins = np.linspace(min(gdd50)-1, max(gdd50)+1, num=10, dtype=np.int)
    m.contourf(lons, lats, gdd50, bins)
    m.drawcounties()

    pqstr = "plot c 000000000000 summary/%s.png bogus png" % (fn,)
    m.postprocess(pqstr=pqstr)
Esempio n. 3
0
def test_contourf():
    ''' Test the contourf plot with labels specified '''
    mp = MapPlot(sector='iowa', nocaption=True)
    mp.contourf(np.arange(-94, -89), np.arange(40, 45),
                np.arange(5), np.arange(5),
                clevlabels=['a', 'b', 'c', 'd', 'e'])
    return mp.fig
Esempio n. 4
0
def do_month(ts, routes='m'):
    """
    Generate the plot for a given month, please
    """
    sql = """SELECT station, sum(precip) as total, max(day) as lastday
           from alldata_ia WHERE year = %s and month = %s
           and station != 'IA0000' and substr(station,2,1) != 'C'
           GROUP by station""" % (ts.year, ts.month)

    lats = []
    lons = []
    vals = []
    lastday = None
    ccursor.execute(sql)
    for row in ccursor:
        if row['station'] not in nt.sts:
            continue
        if lastday is None:
            lastday = row['lastday']
        lats.append(nt.sts[row['station']]['lat'])
        lons.append(nt.sts[row['station']]['lon'])
        vals.append(row['total'])

    m = MapPlot(title='%s - %s' %
                (ts.strftime("%d %B %Y"), lastday.strftime("%d %B %Y")),
                subtitle="%s Total Precipitation [inch]" %
                (ts.strftime("%B %Y"), ))
    m.contourf(lons, lats, vals,
               [0, 0.1, 0.25, 0.5, 0.75, 1, 2, 3, 4, 5, 6, 7])
    m.plot_values(lons, lats, vals, fmt='%.2f')

    pqstr = ("plot %s %s summary/iemre_iowa_total_precip.png "
             "%s/summary/iemre_iowa_total_precip.png png") % (
                 routes, ts.strftime("%Y%m%d%H%M"), ts.strftime("%Y/%m"))
    m.postprocess(pqstr=pqstr)
Esempio n. 5
0
def do_month(ts, routes='m'):
    """
    Generate the plot for a given month, please
    """
    sql = """SELECT station, sum(precip) as total, max(day) as lastday
           from alldata_ia WHERE year = %s and month = %s
           and station != 'IA0000' and substr(station,2,1) != 'C'
           GROUP by station""" % (ts.year, ts.month)

    lats = []
    lons = []
    vals = []
    lastday = None
    ccursor.execute(sql)
    for row in ccursor:
        if row['station'] not in nt.sts:
            continue
        if lastday is None:
            lastday = row['lastday']
        lats.append(nt.sts[row['station']]['lat'])
        lons.append(nt.sts[row['station']]['lon'])
        vals.append(row['total'])

    m = MapPlot(title='%s - %s' % (ts.strftime("%d %B %Y"),
                                   lastday.strftime("%d %B %Y")),
                subtitle="%s Total Precipitation [inch]" % (
                                    ts.strftime("%B %Y"),))
    m.contourf(lons, lats, vals, [0, 0.1, 0.25, 0.5, 0.75, 1, 2, 3, 4, 5, 6,
                                  7])
    m.plot_values(lons, lats, vals, fmt='%.2f')

    pqstr = ("plot %s %s summary/iemre_iowa_total_precip.png "
             "%s/summary/iemre_iowa_total_precip.png png"
             ) % (routes, ts.strftime("%Y%m%d%H%M"), ts.strftime("%Y/%m"))
    m.postprocess(pqstr=pqstr)
Esempio n. 6
0
def test_drawiowawfo():
    """Iowa Contour Plot"""
    mp = MapPlot(sector='iowawfo', title='Iowa Contour plot', nocaption=True)
    mp.contourf(np.arange(-94, -85), np.arange(36, 45), np.arange(9),
                np.arange(9),
                clevlabels=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
    return mp.fig
Esempio n. 7
0
def main(argv):
    """Go Main Go"""
    st = NetworkTable("IACLIMATE")
    now = datetime.datetime.now() - datetime.timedelta(days=1)
    pgconn = get_dbconn("coop", user="******")
    ccursor = pgconn.cursor()

    gfunc = "gdd50"
    gbase = 50
    if len(argv) == 2 and argv[1] == "gdd52":
        gfunc = "gdd52"
        gbase = 52
    if len(argv) == 2 and argv[1] == "gdd48":
        gfunc = "gdd48"
        gbase = 48

    # Compute normal from the climate database
    ccursor.execute(
        """
        SELECT station,
        sum(%s(high, low)) as gdd
        from alldata_ia WHERE station != 'IA0000'
        and substr(station, 3, 1) != 'C' and year = %s
        GROUP by station
    """
        % (gfunc, now.year)
    )

    lats = []
    lons = []
    gdd50 = []
    valmask = []

    for row in ccursor:
        station = row[0]
        if station not in st.sts:
            continue
        lats.append(st.sts[station]["lat"])
        lons.append(st.sts[station]["lon"])
        gdd50.append(float(row[1]))
        valmask.append(True)

    mp = MapPlot(
        axisbg="white",
        title=("Iowa %s GDD (base=%s) Accumulation")
        % (now.strftime("%Y"), gbase),
        subtitle="1 Jan - %s" % (now.strftime("%d %b %Y"),),
    )
    minval = min(gdd50)
    rng = max([int(max(gdd50) - minval), 10])
    ramp = np.linspace(minval, minval + rng, 10, dtype=np.int)
    mp.contourf(lons, lats, gdd50, ramp)
    pqstr = "plot c 000000000000 summary/gdd_jan1.png bogus png"
    if gbase == 52:
        pqstr = "plot c 000000000000 summary/gdd52_jan1.png bogus png"
    elif gbase == 48:
        pqstr = "plot c 000000000000 summary/gdd48_jan1.png bogus png"
    mp.drawcounties()
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Esempio n. 8
0
File: p97.py Progetto: raprasad/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    import matplotlib.cm as cm

    pgconn = psycopg2.connect(database='coop', host='iemdb', user='******')
    cursor = pgconn.cursor()

    sector = fdict.get('sector', 'IA')
    date1 = datetime.datetime.strptime(fdict.get('date1', '2015-01-01'),
                                       '%Y-%m-%d')
    date2 = datetime.datetime.strptime(fdict.get('date2', '2015-02-01'),
                                       '%Y-%m-%d')

    table = "alldata_%s" % (sector, ) if sector != 'midwest' else "alldata"
    cursor.execute("""
    WITH obs as (
        SELECT station, sday, day, precip from """ + table + """ WHERE
        day >= %s and day < %s and precip >= 0 and
        substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000'),
    climo as (
        SELECT station, to_char(valid, 'mmdd') as sday, precip from
        climate51),
    combo as (
        SELECT o.station, o.precip - c.precip as d from obs o JOIN climo c ON
        (o.station = c.station and o.sday = c.sday)),
    deltas as (
        SELECT station, sum(d) from combo GROUP by station)

    SELECT d.station, d.sum, ST_x(t.geom), ST_y(t.geom) from deltas d
    JOIN stations t on (d.station = t.id) WHERE t.network ~* 'CLIMATE'
    """, (date1, date2))

    rows = []
    for row in cursor:
        rows.append(dict(station=row[0], delta=row[1], lon=row[2],
                         lat=row[3]))
    df = pd.DataFrame(rows)
    lons = np.array(df['lon'])
    vals = np.array(df['delta'])
    lats = np.array(df['lat'])
    sector2 = "state" if sector != 'midwest' else 'midwest'
    m = MapPlot(sector=sector2, state=sector, axisbg='white',
                title=('%s - %s Precipitation Departure [inch]'
                       ) % (date1.strftime("%d %b %Y"),
                            date2.strftime("%d %b %Y")),
                subtitle='%s vs 1950-2014 Climatology' % (date1.year,))
    rng = int(max([0 - np.min(vals), np.max(vals)]))
    cmap = cm.get_cmap('RdYlBu')
    cmap.set_bad('white')
    m.contourf(lons, lats, vals, np.linspace(0 - rng - 0.5, rng + 0.6, 10,
                                             dtype='i'),
               cmap=cmap, units='inch')
    m.plot_values(lons, lats, vals, fmt='%.2f')
    if sector == 'iowa':
        m.drawcounties()

    return m.fig, df
Esempio n. 9
0
def test_michigan():
    """See what we do with Michigan"""
    mp = MapPlot(sector='state', state='MI', nocaption=True)
    mp.contourf(np.arange(-84, -75), np.arange(36, 45), np.arange(9),
                np.arange(9),
                clevlabels=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
    return mp.fig
Esempio n. 10
0
def main():
    """GO!"""
    now = datetime.datetime.now()

    df = get_df()
    # df = pd.read_csv('example.csv')
    rng = range(-30, 120, 2)

    for sector in ['iowa', 'midwest', 'conus']:
        mp = MapPlot(axisbg='white',
                     sector=sector,
                     title=("%s 2 meter Air Temperature") %
                     (sector.capitalize(), ),
                     subtitle=now.strftime("%d %b %Y %-I:%M %p"))
        mp.contourf(df['lon'].values,
                    df['lat'].values,
                    df['tmpf'].values,
                    rng,
                    clevstride=5,
                    units='F')
        mp.plot_values(df['lon'].values,
                       df['lat'].values,
                       df['tmpf'].values,
                       fmt='%.0f')
        if sector == 'iowa':
            mp.drawcounties()
        pqstr = ("plot ac %s00 %s_tmpf.png %s_tmpf_%s.png png"
                 "") % (datetime.datetime.utcnow().strftime("%Y%m%d%H"),
                        sector, sector,
                        datetime.datetime.utcnow().strftime("%H"))
        mp.postprocess(view=False, pqstr=pqstr)
        mp.close()
Esempio n. 11
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    pgconn = get_dbconn('coop')
    ctx = get_autoplot_context(fdict, get_description())
    state = ctx['state'][:2]
    varname = ctx['var']
    sector = ctx['sector']
    opt = ctx['opt']
    over = ctx['over']
    month = ctx['month']

    df = read_sql("""
    WITH data as (
        SELECT station, extract(month from valid) as month,
        sum(precip) as total_precip, avg(high) as avg_high,
        avg(low) as avg_low, avg((high+low)/2.) as avg_temp
        from ncdc_climate81 GROUP by station, month)

    SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, month,
    total_precip, avg_high, avg_low, avg_temp from data d JOIN stations t
    ON (d.station = t.id) WHERE t.network = 'NCDC81' and
    t.state in ('IA', 'ND', 'SD', 'NE', 'KS', 'MO', 'IL', 'WI', 'MN', 'MI',
    'IN', 'OH', 'KY')
    """, pgconn, index_col=['station', 'month'])
    if df.empty:
        return "No data was found for query, sorry."

    if over == 'monthly':
        title = "%s %s" % (calendar.month_name[month], PDICT3[varname])
        df.reset_index(inplace=True)
        df2 = df[df['month'] == month]
    else:
        title = "Annual %s" % (PDICT3[varname], )
        if varname == 'total_precip':
            df2 = df.sum(axis=0, level='station')
        else:
            df2 = df.mean(axis=0, level='station')
        df2['lat'] = df['lat'].mean(axis=0, level='station')
        df2['lon'] = df['lon'].mean(axis=0, level='station')
    mp = MapPlot(sector=sector, state=state, axisbg='white',
                 title=('NCEI 1981-2010 Climatology of %s'
                        ) % (title,),
                 subtitle=('based on National Centers for '
                           'Environmental Information (NCEI) 1981-2010'
                           ' Climatology'))
    levels = np.linspace(df2[varname].min(), df2[varname].max(), 10)
    levels = [round(x, PRECISION[varname]) for x in levels]
    if opt in ['both', 'contour']:
        mp.contourf(df2['lon'].values, df2['lat'].values,
                    df2[varname].values, levels, units=UNITS[varname])
    if sector == 'state':
        mp.drawcounties()
    if opt in ['both', 'values']:
        mp.plot_values(df2['lon'].values, df2['lat'].values,
                       df2[varname].values,
                       fmt='%%.%if' % (PRECISION[varname],))

    return mp.fig, df
Esempio n. 12
0
def run(base, ceil, now, fn):
    """ Generate the plot """
    # Compute normal from the climate database
    sql = """SELECT station,
       sum(gddxx(%s, %s, high, low)) as gdd
       from alldata_ia WHERE year = %s and month in (5,6,7,8,9,10)
       and station != 'IA0000' and substr(station,2,1) != 'C'
       GROUP by station""" % (
        base,
        ceil,
        now.year,
    )

    lats = []
    lons = []
    gdd50 = []
    ccursor.execute(sql)
    for row in ccursor:
        if row[0] not in nt.sts:
            continue
        lats.append(nt.sts[row[0]]["lat"])
        lons.append(nt.sts[row[0]]["lon"])
        gdd50.append(float(row[1]))

    mp = MapPlot(
        title=("Iowa 1 May - %s GDD Accumulation") %
        (now.strftime("%-d %B %Y"), ),
        subtitle="base %s" % (base, ),
    )
    bins = np.linspace(min(gdd50) - 1, max(gdd50) + 1, num=10, dtype=np.int)
    mp.contourf(lons, lats, gdd50, bins)
    mp.drawcounties()

    pqstr = "plot c 000000000000 summary/%s.png bogus png" % (fn, )
    mp.postprocess(pqstr=pqstr)
Esempio n. 13
0
def run(year):
    cursor.execute("""
    WITH obs as (
    SELECT station, sum(precip) from alldata_ia where year = %s GROUP by station
    ), climate as (
    SELECT station, sum(precip) from climate51 GROUP by station
    )
    SELECT o.station, o.sum - c.sum as diff from obs o JOIN climate c 
    on (c.station = o.station) ORDER by diff ASC
    """, (year,))
    lats = []
    lons = []
    vals = []
    for row in cursor:
        if not nt.sts.has_key(row[0]) or row[0] in rejs or row[0][2] == 'C':
            continue
        print row
        lats.append( nt.sts[row[0]]['lat'])
        lons.append( nt.sts[row[0]]['lon'])
        vals.append( row[1] )
    
    m = MapPlot(title='%s Precipitation Departure' % (year,))
    cmap = cm.get_cmap('BrBG')
    #cmap.set_over('blue')
    #cmap.set_under('red')
    m.contourf(lons, lats, vals, np.arange(-24,24.1,2), cmap=cmap, units='inch')
    #m.plot_values(lons, lats, vals, '%.02f')
    m.drawcounties()
    m.postprocess(filename='%s.png' % (year,))
Esempio n. 14
0
def main():
    """GO"""
    pgconn = get_dbconn("iem", user="******")
    cursor = pgconn.cursor()

    # Compute normal from the climate database
    sql = """
    SELECT
      id, network, vsby, ST_x(geom) as lon, ST_y(geom) as lat
    FROM
      current c JOIN stations s ON (s.iemid = c.iemid)
    WHERE
      s.network IN ('AWOS', 'IA_ASOS','IL_ASOS','MN_ASOS','WI_ASOS','SD_ASOS',
                  'NE_ASOS','MO_ASOS') and
      valid + '60 minutes'::interval > now() and
      vsby >= 0 and vsby <= 10
    """

    lats = []
    lons = []
    vals = []
    valmask = []
    cursor.execute(sql)
    for row in cursor:
        lats.append(row[4])
        lons.append(row[3])
        vals.append(row[2])
        valmask.append(row[1] in ["AWOS", "IA_ASOS"])

    if len(lats) < 5:
        return

    now = datetime.datetime.now()

    mp = MapPlot(
        sector="iowa",
        title="Iowa Visibility",
        subtitle="Valid: %s" % (now.strftime("%d %b %Y %-I:%M %p"), ),
    )

    mp.contourf(
        lons,
        lats,
        vals,
        np.array([0.01, 0.1, 0.25, 0.5, 1, 2, 3, 5, 8, 9.9]),
        units="miles",
        cmap=cm.get_cmap("gray"),
    )

    mp.plot_values(lons, lats, vals, "%.1f", valmask)
    mp.drawcounties()

    pqstr = ("plot ac %s00 iowa_vsby.png vsby_contour_%s00.png png"
             "") % (
                 datetime.datetime.utcnow().strftime("%Y%m%d%H"),
                 datetime.datetime.utcnow().strftime("%H"),
             )
    mp.postprocess(pqstr=pqstr)
    mp.close()
Esempio n. 15
0
File: p125.py Progetto: akrherz/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    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')
    opt = fdict.get('opt', 'both')
    over = fdict.get('over', 'monthly')
    month = int(fdict.get('month', datetime.date.today().month))

    df = read_sql("""
    WITH data as (
        SELECT station, extract(month from valid) as month,
        sum(precip) as total_precip, avg(high) as avg_high,
        avg(low) as avg_low, avg((high+low)/2.) as avg_temp
        from ncdc_climate81 GROUP by station, month)

    SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat, month,
    total_precip, avg_high, avg_low, avg_temp from data d JOIN stations t
    ON (d.station = t.id) WHERE t.network = 'NCDC81' and
    t.state in ('IA', 'ND', 'SD', 'NE', 'KS', 'MO', 'IL', 'WI', 'MN', 'MI',
    'IN', 'OH', 'KY')
    """, pgconn, index_col=['station', 'month'])

    if over == 'monthly':
        title = "%s %s" % (calendar.month_name[month], PDICT3[varname])
        df.reset_index(inplace=True)
        df2 = df[df['month'] == month]
    else:
        title = "Annual %s" % (PDICT3[varname], )
        if varname == 'total_precip':
            df2 = df.sum(axis=0, level='station')
        else:
            df2 = df.mean(axis=0, level='station')
        df2['lat'] = df['lat'].mean(axis=0, level='station')
        df2['lon'] = df['lon'].mean(axis=0, level='station')
    m = MapPlot(sector=sector, state=state, axisbg='white',
                title=('NCEI 1981-2010 Climatology of %s'
                       ) % (title,),
                subtitle=('based on National Centers for '
                          'Environmental Information (NCEI) 1981-2010'
                          ' Climatology'))
    levels = np.linspace(df2[varname].min(), df2[varname].max(), 10)
    levels = [round(x, PRECISION[varname]) for x in levels]
    if opt in ['both', 'contour']:
        m.contourf(df2['lon'].values, df2['lat'].values,
                   df2[varname].values, levels, 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
Esempio n. 16
0
def test_cwa():
    """Exercise the cwa plotting routines"""
    mp = MapPlot(sector='cwa', cwa='MKX', nocaption=True)
    mp.contourf(np.arange(-94, -89), np.arange(40, 45),
                np.arange(5), np.arange(5),
                clevlabels=['a', 'b', 'c', 'd', 'e'])
    mp.draw_cwas()
    assert mp.cwa == 'MKX'
    return mp.fig
Esempio n. 17
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    pgconn = get_dbconn('coop')
    ctx = get_autoplot_context(fdict, get_description())
    sector = ctx['sector']
    varname = ctx['var']
    year = ctx['year']
    popt = ctx['popt']
    threshold = ctx['threshold']
    table = "alldata_%s" % (sector, )
    df = read_sql("""
    WITH data as (
        SELECT station, """ + SQLOPT[varname] + """ as doy
        from """ + table + """
        WHERE year = %s GROUP by station
    )
    select station, doy, st_x(geom) as lon, st_y(geom) as lat
    from data d JOIN stations t on (d.station = t.id) WHERE
    t.network = %s and substr(station, 3, 4) != '0000'
    and substr(station, 3, 1) != 'C' and doy not in (0, 400) ORDER by doy
    """,
                  pgconn,
                  params=(threshold, year, '%sCLIMATE' % (sector, )),
                  index_col='station')
    if df.empty:
        return "No data found!"

    def f(val):
        ts = datetime.date(year, 1, 1) + datetime.timedelta(days=(val - 1))
        return ts.strftime("%-m/%-d")

    df['pdate'] = df['doy'].apply(f)

    mp = MapPlot(sector='state',
                 state=sector,
                 continental_color='white',
                 nocaption=True,
                 title="%s %s %s$^\circ$F" %
                 (year, PDICT2[varname], threshold),
                 subtitle='based on NWS COOP and IEM Daily Estimates')
    levs = np.linspace(df['doy'].min() - 3, df['doy'].max() + 3, 7, dtype='i')
    levlables = map(f, levs)
    if popt == 'contour':
        mp.contourf(df['lon'],
                    df['lat'],
                    df['doy'],
                    levs,
                    clevlabels=levlables)
    mp.plot_values(df['lon'], df['lat'], df['pdate'], labelbuffer=5)
    mp.drawcounties()

    return mp.fig, df
Esempio n. 18
0
File: p86.py Progetto: raprasad/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    ptype = fdict.get('ptype', 'c')
    date = datetime.datetime.strptime(fdict.get('date', '2015-01-01'),
                                      '%Y-%m-%d')
    varname = fdict.get('var', 'rsds')

    idx0 = iemre.daily_offset(date)
    nc = netCDF4.Dataset(("/mesonet/data/iemre/%s_mw_daily.nc"
                          ) % (date.year, ), 'r')
    lats = nc.variables['lat'][:]
    lons = nc.variables['lon'][:]
    if varname == 'rsds':
        # Value is in W m**-2, we want MJ
        data = nc.variables[varname][idx0, :, :] * 86400. / 1000000.
        units = 'MJ d-1'
        clevs = np.arange(0, 37, 3.)
        clevs[0] = 0.01
        clevstride = 1
    elif varname in ['p01d', 'p01d_12z']:
        # Value is in W m**-2, we want MJ
        data = nc.variables[varname][idx0, :, :] / 25.4
        units = 'inch'
        clevs = np.arange(0, 0.25, 0.05)
        clevs = np.append(clevs, np.arange(0.25, 3., 0.25))
        clevs = np.append(clevs, np.arange(3., 10.0, 1))
        clevs[0] = 0.01
        clevstride = 1
    elif varname in ['high_tmpk', 'low_tmpk', 'high_tmpk_12z', 'low_tmpk_12z']:
        # Value is in W m**-2, we want MJ
        data = temperature(nc.variables[varname][idx0, :, :], 'K').value('F')
        units = 'F'
        clevs = np.arange(-30, 120, 2)
        clevstride = 5
    nc.close()

    title = date.strftime("%-d %B %Y")
    m = MapPlot(sector='midwest', axisbg='white', nocaption=True,
                title='IEM Reanalysis of %s for %s' % (PDICT.get(varname),
                                                       title),
                subtitle='Data derived from various NOAA datasets'
                )
    if np.ma.is_masked(np.max(data)):
        return 'Data Unavailable'
    x, y = np.meshgrid(lons, lats)
    if ptype == 'c':
        m.contourf(x, y, data, clevs, clevstride=clevstride, units=units)
    else:
        m.pcolormesh(x, y, data, clevs, clevstride=clevstride, units=units)

    return m.fig
Esempio n. 19
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    import matplotlib.cm as cm
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='******')

    date1 = datetime.datetime.strptime(fdict.get('date1', '2014-09-01'),
                                       '%Y-%m-%d')
    date2 = datetime.datetime.strptime(fdict.get('date2', '2014-09-22'),
                                       '%Y-%m-%d')
    date1 = date1.replace(year=2000)
    date2 = date2.replace(year=2000)

    varname = fdict.get('varname', 'low')

    df = read_sql("""
    WITH t2 as (
         SELECT station, high, low from ncdc_climate81 WHERE
         valid = %s
    ), t1 as (
        SELECT station, high, low from ncdc_climate81 where
        valid = %s
    ), data as (
        SELECT t2.station, t1.high as t1_high, t2.high as t2_high,
        t1.low as t1_low, t2.low as t2_low from t1 JOIN t2 on
        (t1.station = t2.station)
    )
    SELECT d.station, ST_x(geom) as lon, ST_y(geom) as lat,
    t2_high -  t1_high as high, t2_low - t1_low as low from data d JOIN
    stations s on (s.id = d.station) where s.network = 'NCDC81'
    and s.state not in ('HI', 'AK')
    """,
                  pgconn,
                  params=(date2, date1),
                  index_col='station')

    days = int((date2 - date1).days)
    extent = int(df[varname].abs().max())
    m = MapPlot(sector='conus',
                title=('%s Day Change in %s NCDC 81 Climatology') %
                (days, PDICT[varname]),
                subtitle='from %s to %s' %
                (date1.strftime("%-d %B"), date2.strftime("%-d %B")))
    cmap = cm.get_cmap("RdBu_r")
    m.contourf(df['lon'].values,
               df['lat'].values,
               df[varname].values,
               np.arange(0 - extent, extent + 1, 2),
               cmap=cmap,
               units='F')

    return m.fig, df
Esempio n. 20
0
def doit(now):
    """
      Generate some plots for the COOP data!
    """
    # We'll assume all COOP data is 12z, sigh for now
    sql = """SELECT id, pday, network
           from summary_%s s JOIN stations t ON (t.iemid = s.iemid) 
           WHERE day = '%s' and
           t.network ~* 'COOP' and pday >= 0""" % (now.year,
           now.strftime("%Y-%m-%d") )

    lats = []
    lons = []
    vals = []
    icursor.execute( sql )
    iamax = 0.
    for row in icursor:
        sid = row[0]
        if not st.sts.has_key(sid):
            continue
        #labels.append( id[2:] )
        lats.append( st.sts[sid]['lat'] )
        lons.append( st.sts[sid]['lon'] )
        vals.append( row[1] )
        if row[2] == 'IA_COOP' and row[1] > iamax:
            iamax = row[1]

#if iamax == 0:
# Dummy in some bad data to prevent the contouring from going mad
#      lats.append( 42. )
#      lons.append( -96.0 )
#      vals.append( 1. )
    
    # Plot Iowa
    m = MapPlot(sector='iowa',
                title='24 Hour NWS COOP Precipitation [inch]',
                subtitle='Ending %s at roughly 12Z' % (now.strftime("%d %B %Y"),))

    m.contourf(lons, lats, vals, clevs, units='inch')

    pqstr = "plot ac %s0000 iowa_coop_12z_precip.png iowa_coop_12z_precip.png png" % (now.strftime("%Y%m%d"),)
    m.postprocess(pqstr=pqstr)
    m.close()

    m = MapPlot(sector='midwest',
                title='24 Hour NWS COOP Precipitation [inch]',
                subtitle='Ending %s at roughly 12Z' % (now.strftime("%d %B %Y"),))

    m.contourf(lons, lats, vals, clevs, units='inch')

    pqstr = "plot ac %s0000 midwest_coop_12z_precip.png midwest_coop_12z_precip.png png" % (now.strftime("%Y%m%d"),)
    m.postprocess(pqstr=pqstr)
    m.close()
Esempio n. 21
0
def main():
    """Go Main Go"""
    now = datetime.datetime.now()
    pgconn = get_dbconn("iem", user="******")
    icursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    day1 = datetime.date.today().replace(day=1)
    day2 = (day1 + datetime.timedelta(days=35)).replace(day=1)

    lats = []
    lons = []
    vals = []
    valmask = []
    table = "summary_%s" % (day1.year, )
    icursor.execute(
        """
        SELECT id, s.network, ST_x(s.geom) as lon, ST_y(s.geom) as lat,
        avg( (max_tmpf + min_tmpf)/2.0 ) as avgt , count(*) as cnt
        from """ + table + """ c JOIN stations s ON (s.iemid = c.iemid)
        WHERE s.network in ('IA_ASOS', 'AWOS') and
        day >= %s and day < %s
        and max_tmpf > -30 and min_tmpf < 90 GROUP by id, s.network, lon, lat
    """,
        (day1, day2),
    )
    for row in icursor:
        if row["cnt"] != now.day:
            continue
        lats.append(row["lat"])
        lons.append(row["lon"])
        vals.append(row["avgt"])
        valmask.append(row["network"] in ["AWOS", "IA_ASOS"])

    if len(vals) < 3:
        return

    mp = MapPlot(
        axisbg="white",
        title="Iowa %s Average Temperature" % (now.strftime("%Y %B"), ),
        subtitle=("Average of the High + Low ending: %s"
                  "") % (now.strftime("%d %B"), ),
    )
    minval = int(min(vals))
    maxval = max([int(max(vals)) + 3, minval + 11])
    clevs = np.linspace(minval, maxval, 10, dtype="i")
    mp.contourf(lons, lats, vals, clevs)
    mp.drawcounties()
    mp.plot_values(lons, lats, vals, "%.1f")
    pqstr = "plot c 000000000000 summary/mon_mean_T.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Esempio n. 22
0
File: p21.py Progetto: KayneWest/iem
def plotter(fdict):
    """ Go """
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    date1 = datetime.datetime.strptime(fdict.get('date1', '2014-09-01'),
                                       '%Y-%m-%d')
    date2 = datetime.datetime.strptime(fdict.get('date2', '2014-09-22'),
                                       '%Y-%m-%d')
    date1 = date1.replace(year=2000)
    date2 = date2.replace(year=2000)

    varname = fdict.get('varname', 'low')

    cursor.execute("""
    WITH t2 as (
         SELECT station, high, low from ncdc_climate81 WHERE
         valid = %s
    ), t1 as (
        SELECT station, high, low from ncdc_climate81 where
        valid = %s
    ), data as (
        SELECT t2.station, t1.high as t1_high, t2.high as t2_high,
        t1.low as t1_low, t2.low as t2_low from t1 JOIN t2 on
        (t1.station = t2.station)
    )
    SELECT d.station, ST_x(geom) as lon, ST_y(geom) as lat,
    t2_high -  t1_high as high, t2_low - t1_low as low from data d JOIN
    stations s on (s.id = d.station) where s.network = 'NCDC81'
    """, (date2, date1))
    vals = []
    lons = []
    lats = []
    for row in cursor:
        lats.append(row['lat'])
        lons.append(row['lon'])
        vals.append(row[varname])

    days = int((date2 - date1).days)
    extent = int(max(abs(min(vals)), abs(max(vals))))
    m = MapPlot(sector='conus',
                title=('%s Day Change in %s NCDC 81 Climatology'
                       ) % (days, PDICT[varname]),
                subtitle='from %s to %s' % (date1.strftime("%-d %B"),
                                            date2.strftime("%-d %B"))
                )
    cmap = cm.get_cmap("RdBu_r")
    m.contourf(lons, lats, vals, np.arange(0-extent, extent+1, 2), cmap=cmap,
               units='F')

    return m.fig
Esempio n. 23
0
def doit(now):
    """
      Generate some plots for the COOP data!
    """
    # We'll assume all COOP data is 12z, sigh for now
    sql = """SELECT id, pday, network
           from summary_%s s JOIN stations t ON (t.iemid = s.iemid)
           WHERE day = '%s' and
           t.network ~* 'COOP' and pday >= 0""" % (now.year,
                                                   now.strftime("%Y-%m-%d"))

    lats = []
    lons = []
    vals = []
    icursor.execute(sql)
    iamax = 0.
    for row in icursor:
        sid = row[0]
        if sid not in st.sts:
            continue
        # labels.append( id[2:] )
        lats.append(st.sts[sid]['lat'])
        lons.append(st.sts[sid]['lon'])
        vals.append(row[1])
        if row[2] == 'IA_COOP' and row[1] > iamax:
            iamax = row[1]

    # Plot Iowa
    m = MapPlot(sector='iowa',
                title='24 Hour NWS COOP Precipitation [inch]',
                subtitle=('Ending %s at roughly 12Z') %
                (now.strftime("%d %B %Y"), ))

    m.contourf(lons, lats, vals, clevs, units='inch')

    pqstr = ("plot ac %s0000 iowa_coop_12z_precip.png "
             "iowa_coop_12z_precip.png png") % (now.strftime("%Y%m%d"), )
    m.postprocess(pqstr=pqstr)
    m.close()

    m = MapPlot(sector='midwest',
                title='24 Hour NWS COOP Precipitation [inch]',
                subtitle=('Ending %s at roughly 12Z') %
                (now.strftime("%d %B %Y"), ))

    m.contourf(lons, lats, vals, clevs, units='inch')

    pqstr = ("plot ac %s0000 midwest_coop_12z_precip.png "
             "midwest_coop_12z_precip.png png") % (now.strftime("%Y%m%d"), )
    m.postprocess(pqstr=pqstr)
    m.close()
Esempio n. 24
0
def main():
    """Go Main Go"""

    now = datetime.datetime.now()
    pgconn = get_dbconn('iem', user='******')
    icursor = pgconn.cursor()

    # Compute normal from the climate database
    sql = """
      select s.id, s.network,
      ST_x(s.geom) as lon, ST_y(s.geom) as lat,
      greatest(c.max_sknt, c.max_gust) as wind
      from summary_%s c, current c2, stations s
      WHERE s.iemid = c.iemid and c2.valid > 'TODAY' and c.day = 'TODAY'
      and c2.iemid = s.iemid
      and (s.network ~* 'ASOS' or s.network = 'AWOS') and s.country = 'US'
      ORDER by lon, lat
    """ % (now.year, )

    lats = []
    lons = []
    vals = []
    valmask = []
    icursor.execute(sql)
    for row in icursor:
        if row[4] == 0 or row[4] is None:
            continue
        lats.append(row[3])
        lons.append(row[2])
        vals.append(speed(row[4], 'KT').value('MPH'))
        valmask.append((row[1] in ['AWOS', 'IA_ASOS']))

    if len(vals) < 5 or True not in valmask:
        return

    clevs = np.arange(0, 40, 2)
    clevs = np.append(clevs, np.arange(40, 80, 5))
    clevs = np.append(clevs, np.arange(80, 120, 10))

    # Iowa
    pqstr = "plot ac %s summary/today_gust.png iowa_wind_gust.png png" % (
        now.strftime("%Y%m%d%H%M"), )
    mp = MapPlot(title="Iowa ASOS/AWOS Peak Wind Speed Reports",
                 subtitle="%s" % (now.strftime("%d %b %Y"), ),
                 sector='iowa')
    mp.contourf(lons, lats, vals, clevs, units='MPH')
    mp.plot_values(lons, lats, vals, '%.0f', valmask=valmask, labelbuffer=10)
    mp.drawcounties()
    mp.postprocess(pqstr=pqstr, view=False)
    mp.close()
Esempio n. 25
0
def main():
    """Go Main Go"""
    now = datetime.datetime.now()
    pgconn = get_dbconn("coop")
    ccursor = pgconn.cursor()

    nt = NetworkTable("IACLIMATE")

    # Compute normal from the climate database
    sql = """SELECT station,
       sum(gdd50(high, low)) as gdd
       from alldata_ia WHERE year = %s and month = %s
       GROUP by station""" % (
        now.year,
        now.month,
    )

    vals = []
    lats = []
    lons = []
    ccursor.execute(sql)
    for row in ccursor:
        if row[0] not in nt.sts:
            continue
        lats.append(nt.sts[row[0]]["lat"])
        lons.append(nt.sts[row[0]]["lon"])
        vals.append(float(row[1]))

    if len(vals) < 5:
        sys.exit()

    mp = MapPlot(
        title="Iowa %s GDD Accumulation" % (now.strftime("%B %Y"), ),
        axisbg="white",
    )
    mp.contourf(
        lons,
        lats,
        vals,
        np.linspace(int(min(vals)),
                    int(max(vals)) + 3, 10),
        units="base 50",
    )
    mp.plot_values(lons, lats, vals, fmt="%.0f")

    pqstr = "plot c 000000000000 summary/gdd_mon.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Esempio n. 26
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    import matplotlib.cm as cm
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='******')

    date1 = datetime.datetime.strptime(fdict.get('date1', '2014-09-01'),
                                       '%Y-%m-%d')
    date2 = datetime.datetime.strptime(fdict.get('date2', '2014-09-22'),
                                       '%Y-%m-%d')
    date1 = date1.replace(year=2000)
    date2 = date2.replace(year=2000)

    varname = fdict.get('varname', 'low')

    df = read_sql("""
    WITH t2 as (
         SELECT station, high, low from ncdc_climate81 WHERE
         valid = %s
    ), t1 as (
        SELECT station, high, low from ncdc_climate81 where
        valid = %s
    ), data as (
        SELECT t2.station, t1.high as t1_high, t2.high as t2_high,
        t1.low as t1_low, t2.low as t2_low from t1 JOIN t2 on
        (t1.station = t2.station)
    )
    SELECT d.station, ST_x(geom) as lon, ST_y(geom) as lat,
    t2_high -  t1_high as high, t2_low - t1_low as low from data d JOIN
    stations s on (s.id = d.station) where s.network = 'NCDC81'
    and s.state not in ('HI', 'AK')
    """, pgconn, params=(date2, date1), index_col='station')

    days = int((date2 - date1).days)
    extent = int(df[varname].abs().max())
    m = MapPlot(sector='conus',
                title=('%s Day Change in %s NCDC 81 Climatology'
                       ) % (days, PDICT[varname]),
                subtitle='from %s to %s' % (date1.strftime("%-d %B"),
                                            date2.strftime("%-d %B"))
                )
    cmap = cm.get_cmap("RdBu_r")
    m.contourf(df['lon'].values, df['lat'].values, df[varname].values,
               np.arange(0-extent, extent+1, 2), cmap=cmap, units='F')

    return m.fig, df
Esempio n. 27
0
File: p165.py Progetto: akrherz/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    pgconn = psycopg2.connect(dbname='coop', host='iemdb', user='******')
    ctx = util.get_autoplot_context(fdict, get_description())
    sector = ctx['sector']
    varname = ctx['var']
    year = ctx['year']
    popt = ctx['popt']
    threshold = ctx['threshold']
    table = "alldata_%s" % (sector,)
    df = read_sql("""
    WITH data as (
        SELECT station, """ + SQLOPT[varname] + """ as doy
        from """ + table + """
        WHERE year = %s GROUP by station
    )
    select station, doy, st_x(geom) as lon, st_y(geom) as lat
    from data d JOIN stations t on (d.station = t.id) WHERE
    t.network = %s and substr(station, 3, 4) != '0000'
    and substr(station, 3, 1) != 'C' and doy not in (0, 400) ORDER by doy
    """, pgconn, params=(threshold, year, '%sCLIMATE' % (sector,)),
                  index_col='station')
    if len(df.index) == 0:
        return "No data found!"

    def f(val):
        ts = datetime.date(year, 1, 1) + datetime.timedelta(days=(val - 1))
        return ts.strftime("%-m/%-d")

    df['pdate'] = df['doy'].apply(f)

    m = MapPlot(sector='state', state=sector, axisbg='white', nocaption=True,
                title="%s %s %s$^\circ$F" % (year, PDICT2[varname], threshold),
                subtitle='based on NWS COOP and IEM Daily Estimates')
    levs = np.linspace(df['doy'].min() - 3, df['doy'].max() + 3, 7, dtype='i')
    levlables = map(f, levs)
    if popt == 'contour':
        m.contourf(df['lon'], df['lat'], df['doy'], levs, clevlabels=levlables)
    m.plot_values(df['lon'], df['lat'], df['pdate'], labelbuffer=5)
    m.drawcounties()

    return m.fig, df
Esempio n. 28
0
def main():
    """Go Main Go"""
    now = datetime.datetime.now()

    pgconn = get_dbconn("coop", user="******")
    ccursor = pgconn.cursor()

    nt = NetworkTable("IACLIMATE")

    # Compute normal from the climate database
    sql = """SELECT station,
       sum(sdd86(high, low)) as sdd
       from alldata_ia WHERE year = %s and month = %s
       GROUP by station""" % (
        now.year,
        now.month,
    )

    lats = []
    lons = []
    sdd86 = []
    valmask = []
    ccursor.execute(sql)
    for row in ccursor:
        lats.append(nt.sts[row[0]]["lat"])
        lons.append(nt.sts[row[0]]["lon"])
        sdd86.append(float(row[1]))
        valmask.append(True)

    if len(sdd86) < 5:
        LOG.debug("aborting due to %s obs", len(sdd86))
        sys.exit()

    mp = MapPlot(
        axisbg="white",
        title="Iowa %s SDD Accumulation" % (now.strftime("%B %Y"), ),
    )
    if max(sdd86) > 5:
        mp.contourf(lons, lats, sdd86,
                    range(int(min(sdd86) - 1), int(max(sdd86) + 1)))
    else:
        mp.plot_values(lons, lats, sdd86, fmt="%.0f")
    pqstr = "plot c 000000000000 summary/sdd_mon.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Esempio n. 29
0
def do_month(ts, routes="m"):
    """
    Generate the plot for a given month, please
    """
    pgconn = get_dbconn("coop", user="******")
    ccursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    nt = NetworkTable("IACLIMATE")
    sql = """SELECT station, sum(precip) as total, max(day) as lastday
           from alldata_ia WHERE year = %s and month = %s
           and station != 'IA0000' and substr(station,2,1) != 'C'
           GROUP by station""" % (
        ts.year,
        ts.month,
    )

    lats = []
    lons = []
    vals = []
    lastday = None
    ccursor.execute(sql)
    for row in ccursor:
        if row["station"] not in nt.sts:
            continue
        if lastday is None:
            lastday = row["lastday"]
        lats.append(nt.sts[row["station"]]["lat"])
        lons.append(nt.sts[row["station"]]["lon"])
        vals.append(row["total"])

    mp = MapPlot(
        title="%s - %s"
        % (ts.strftime("%d %B %Y"), lastday.strftime("%d %B %Y")),
        subtitle="%s Total Precipitation [inch]" % (ts.strftime("%B %Y"),),
    )
    mp.contourf(
        lons, lats, vals, [0, 0.1, 0.25, 0.5, 0.75, 1, 2, 3, 4, 5, 6, 7]
    )
    mp.plot_values(lons, lats, vals, fmt="%.2f")

    pqstr = (
        "plot %s %s summary/iemre_iowa_total_precip.png "
        "%s/summary/iemre_iowa_total_precip.png png"
    ) % (routes, ts.strftime("%Y%m%d%H%M"), ts.strftime("%Y/%m"))
    mp.postprocess(pqstr=pqstr)
Esempio n. 30
0
def plotter(fdict):
    """ Go """
    ctx = get_autoplot_context(fdict, get_description())

    if ctx['t'] == 'state':
        bnds = reference.state_bounds[ctx['state']]
        title = reference.state_names[ctx['state']]
    else:
        bnds = reference.wfo_bounds[ctx['wfo']]
        nt = NetworkTable("WFO")
        title = "NWS CWA %s [%s]" % (nt.sts[ctx['wfo']]['name'], ctx['wfo'])
    df, valid = get_df(ctx, bnds)
    if df.empty:
        raise ValueError("No data was found for your query")
    mp = MapPlot(sector=('state' if ctx['t'] == 'state' else 'cwa'),
                 state=ctx['state'],
                 cwa=(ctx['wfo'] if len(ctx['wfo']) == 3 else ctx['wfo'][1:]),
                 axisbg='white',
                 title='%s for %s' % (PDICT2[ctx['v']], title),
                 subtitle=('Map valid: %s UTC') %
                 (valid.strftime("%d %b %Y %H:%M"), ),
                 nocaption=True,
                 titlefontsize=16)
    mp.contourf(df['lon'].values,
                df['lat'].values,
                df['vsby'].values,
                np.array([0.01, 0.1, 0.25, 0.5, 1, 2, 3, 5, 8, 9.9]),
                units='miles',
                cmap=plt.get_cmap('gray'))
    if ctx['t'] == 'state':
        df2 = df[df['state'] == ctx['state']]
    else:
        df2 = df[df['wfo'] == ctx['wfo']]

    mp.plot_values(df2['lon'].values, df2['lat'].values, df2['vsby'].values,
                   '%.1f')
    mp.drawcounties()
    if ctx['t'] == 'cwa':
        mp.draw_cwas()

    return mp.fig, df
Esempio n. 31
0
def main():
    """Go Main Go."""
    DBCONN = get_dbconn("postgis")
    cursor = DBCONN.cursor()

    cursor.execute("""
        SELECT
            ST_Transform(
                ST_Simplify(
                    ST_Union(ST_Transform(the_geom,2163)),
                    500.),
                4326) from states
        WHERE state_abbr in ('IA', 'IL', 'IN')
    ---    where state_abbr in ('MI','WI', 'IL', 'IN', 'OH', 'KY', 'MO', 'KS',
    ---    'NE', 'SD', 'ND', 'MN', 'IA')
    """)

    m = MapPlot("iowa")
    lons = np.linspace(-110, -70, 50)
    lats = np.linspace(28, 52, 50)
    vals = np.linspace(0, 50, 50)
    m.contourf(lons, lats, vals, vals)

    # """
    for row in cursor:
        multipoly = MultiPolygon([loads(row[0], hex=True)])
        for geo in multipoly.geoms:
            if geo.area < 1:
                continue
            (lons, lats) = geo.exterior.xy
            print("Masking with geo... %s %s" % (geo.area, len(lons)))

            ar = list(zip(lons, lats))
            ar.reverse()
            np.save("iailin_ccw.npy", ar)
            # mask_outside_polygon(ar, ax=m.ax)
            poly = PolygonPatch(Polygon(ar), fc="r", zorder=10000)
            m.ax.add_patch(poly)
    # """
    m.postprocess(filename="test.png")
Esempio n. 32
0
File: p86.py Progetto: danhreitz/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    ptype = fdict.get('ptype', 'c')
    date = datetime.datetime.strptime(fdict.get('date', '2015-01-01'),
                                      '%Y-%m-%d')
    varname = fdict.get('var', 'rsds')

    idx0 = iemre.daily_offset(date)
    nc = netCDF4.Dataset(("/mesonet/data/iemre/%s_mw_daily.nc"
                          ) % (date.year, ), 'r')
    lats = nc.variables['lat'][:]
    lons = nc.variables['lon'][:]
    if varname == 'rsds':
        # Value is in W m**-2, we want MJ
        data = nc.variables['rsds'][idx0, :, :] * 86400. / 1000000.
        units = 'MJ d-1'
    nc.close()

    title = date.strftime("%-d %B %Y")
    m = MapPlot(sector='midwest', axisbg='white', nocaption=True,
                title='IEM Reanalysis of %s for %s' % (PDICT.get(varname),
                                                       title),
                subtitle='Data derived from various NOAA datasets'
                )
    if np.ma.is_masked(np.max(data)):
        return 'Data Unavailable'
    clevs = np.arange(0, 37, 3.)
    clevs[0] = 0.01
    x, y = np.meshgrid(lons, lats)
    if ptype == 'c':
        m.contourf(x, y, data, clevs, units=units)
    else:
        m.pcolormesh(x, y, data, clevs, units=units)

    return m.fig
Esempio n. 33
0
def main():
    """GO!"""
    now = datetime.datetime.now()

    df = get_df()
    # df = pd.read_csv('example.csv')
    rng = range(-30, 120, 2)

    for sector in ["iowa", "midwest", "conus"]:
        mp = MapPlot(
            axisbg="white",
            sector=sector,
            title=("%s 2 meter Air Temperature") % (sector.capitalize(), ),
            subtitle=now.strftime("%d %b %Y %-I:%M %p"),
        )
        mp.contourf(
            df["lon"].values,
            df["lat"].values,
            df["tmpf"].values,
            rng,
            clevstride=5,
            units="F",
        )
        mp.plot_values(df["lon"].values,
                       df["lat"].values,
                       df["tmpf"].values,
                       fmt="%.0f")
        if sector == "iowa":
            mp.drawcounties()
        pqstr = ("plot ac %s00 %s_tmpf.png %s_tmpf_%s.png png"
                 "") % (
                     datetime.datetime.utcnow().strftime("%Y%m%d%H"),
                     sector,
                     sector,
                     datetime.datetime.utcnow().strftime("%H"),
                 )
        mp.postprocess(view=False, pqstr=pqstr)
        mp.close()
Esempio n. 34
0
def main():
    """Go Main Go"""
    now = datetime.datetime.now()

    pgconn = get_dbconn('coop', user='******')
    ccursor = pgconn.cursor()

    nt = NetworkTable("IACLIMATE")

    # Compute normal from the climate database
    sql = """SELECT station,
       sum(sdd86(high, low)) as sdd
       from alldata_ia WHERE year = %s and month = %s
       GROUP by station""" % (now.year, now.month)

    lats = []
    lons = []
    sdd86 = []
    valmask = []
    ccursor.execute(sql)
    for row in ccursor:
        lats.append(nt.sts[row[0]]['lat'])
        lons.append(nt.sts[row[0]]['lon'])
        sdd86.append(float(row[1]))
        valmask.append(True)

    if len(sdd86) < 5 or max(sdd86) == 0:
        sys.exit()

    mp = MapPlot(axisbg='white',
                 title="Iowa %s SDD Accumulation" % (now.strftime("%B %Y"), ))
    mp.contourf(lons, lats, sdd86,
                range(int(min(sdd86) - 1), int(max(sdd86) + 1)))
    pqstr = "plot c 000000000000 summary/sdd_mon.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Esempio n. 35
0
    rise2 = loc.next_rising(sun).datetime()
    loc.date = "2013/06/23"
    set2 = loc.next_setting(sun).datetime()

    day1 = (set1 - rise1).seconds + (set1 - rise1).microseconds / 1000000.0
    day2 = (set2 - rise2).seconds + (set2 - rise2).microseconds / 1000000.0
    return day1 - day2

xs, ys = np.meshgrid(np.concatenate([iemre.XAXIS, [iemre.XAXIS[-1] + 0.25, ]]),
                     np.concatenate([iemre.YAXIS, [iemre.YAXIS[-1] + 0.25, ]]))

secs = np.zeros(np.shape(w), 'f')

for i, lon in enumerate(iemre.XAXIS):
    print i, len(iemre.XAXIS)
    for j, lat in enumerate(iemre.YAXIS):
        secs[j, i] = do(lat, lon)

print np.shape(w)
print np.shape(secs)
# secs = np.where(w == 1, secs, 0)

m = MapPlot(sector='midwest',
            title='21 Jun to 22 Jun 2013 Decrease in Daylight Time',
            subtitle='No local topography considered')

m.contourf(iemre.XAXIS, iemre.YAXIS, secs, np.arange(2, 6.1, 0.25),
           units='seconds')

m.postprocess(filename='test.png')
Esempio n. 36
0
def plotter(fdict):
    """ Go """
    ctx = get_autoplot_context(fdict, get_description())
    varname = ctx["v"]

    if ctx["t"] == "state":
        bnds = reference.state_bounds[ctx["state"]]
        title = reference.state_names[ctx["state"]]
    else:
        bnds = reference.wfo_bounds[ctx["wfo"]]
        title = "NWS CWA %s [%s]" % (
            ctx["_nt"].sts[ctx["wfo"]]["name"],
            ctx["wfo"],
        )
    df, valid = get_df(ctx, bnds)
    if df.empty:
        raise NoDataFound("No data was found for your query")
    mp = MapPlot(
        sector=("state" if ctx["t"] == "state" else "cwa"),
        state=ctx["state"],
        cwa=(ctx["wfo"] if len(ctx["wfo"]) == 3 else ctx["wfo"][1:]),
        axisbg="white",
        title="%s for %s" % (PDICT2[ctx["v"]], title),
        subtitle=("Map valid: %s UTC") % (valid.strftime("%d %b %Y %H:%M"),),
        nocaption=True,
        titlefontsize=16,
    )
    if varname == "vsby":
        ramp = np.array([0.01, 0.1, 0.25, 0.5, 1, 2, 3, 5, 8, 9.9])
        valunit = "miles"
    elif varname == "feel":
        valunit = "F"
        df["feel"] = (
            apparent_temperature(
                df["tmpf"].values * units("degF"),
                df["relh"].values * units("percent"),
                df["sknt"].values * units("knots"),
            )
            .to(units("degF"))
            .m
        )
    # Data QC, cough
    if ctx.get("above"):
        df = df[df[varname] < ctx["above"]]
    if ctx.get("below"):
        df = df[df[varname] > ctx["below"]]
    # with QC done, we compute ramps
    if varname != "vsby":
        ramp = np.linspace(
            df[varname].min() - 5, df[varname].max() + 5, 10, dtype="i"
        )

    mp.contourf(
        df["lon"].values,
        df["lat"].values,
        df[varname].values,
        ramp,
        units=valunit,
        cmap=get_cmap(ctx["cmap"]),
    )
    if ctx["t"] == "state":
        df2 = df[df["state"] == ctx["state"]]
    else:
        df2 = df[df["wfo"] == ctx["wfo"]]

    mp.plot_values(
        df2["lon"].values,
        df2["lat"].values,
        df2[varname].values,
        "%.1f",
        labelbuffer=10,
    )
    mp.drawcounties()
    if ctx["t"] == "cwa":
        mp.draw_cwas()

    return mp.fig, df
Esempio n. 37
0
lons = []
vals = []
valmask = []
icursor.execute(sql)
for row in icursor:
    if row[4] == 0 or row[4] is None:
        continue
    lats.append(row[3])
    lons.append(row[2])
    vals.append(speed(row[4], 'KT').value('MPH'))
    valmask.append((row[1] in ['AWOS', 'IA_ASOS']))

if len(vals) < 5 or True not in valmask:
    sys.exit(0)

clevs = numpy.arange(0, 40, 2)
clevs = numpy.append(clevs, numpy.arange(40, 80, 5))
clevs = numpy.append(clevs, numpy.arange(80, 120, 10))

# Iowa
pqstr = "plot ac %s summary/today_gust.png iowa_wind_gust.png png" % (
        now.strftime("%Y%m%d%H%M"), )
m = MapPlot(title="Iowa ASOS/AWOS Peak Wind Speed Reports",
            subtitle="%s" % (now.strftime("%d %b %Y"), ),
            sector='iowa')
m.contourf(lons, lats, vals, clevs, units='MPH')
m.plot_values(lons, lats, vals, '%.0f', valmask=valmask)
m.drawcounties()
m.postprocess(pqstr=pqstr, view=False)
m.close()
Esempio n. 38
0
for row in ccursor:
    station = row[0]
    if station not in st.sts:
        continue
    nrain.append(row[1])
    lats.append(st.sts[station]['lat'])
    lons.append(st.sts[station]['lon'])

m = MapPlot(axisbg='white',
            title=("Iowa %s Normal Precipitation Accumulation"
                   ) % (ts.strftime("%Y"), ),
            subtitle="1 Jan - %s" % (ts.strftime("%d %b %Y"), ))
rng = np.arange(int(min(nrain))-1, int(max(nrain))+1)
if max(nrain) < 10:
    rng = np.arange(0, 10)
m.contourf(lons, lats, nrain, rng, units='inch')
pqstr = "plot c 000000000000 summary/year/normals.png bogus png"
m.postprocess(view=False, pqstr=pqstr)
m.close()

# ----------------------------------
# - Compute departures
nrain = []
lats = []
lons = []
ccursor.execute("""select climate.station, norm, obs from
    (select c.station, sum(c.precip) as norm from climate51 c
     where c.valid < '2000-%s' and substr(station,0,3) = 'IA'
     GROUP by c.station) as climate,
    (select a.station, sum(a.precip) as obs from alldata a
     WHERE a.year = %s and substr(a.station,0,3) = 'IA'
Esempio n. 39
0
def plotter(fdict):
    """ Go """
    ctx = get_autoplot_context(fdict, get_description())
    ptype = ctx["ptype"]
    date = ctx["date"]
    varname = ctx["var"]
    csector = ctx["csector"]
    title = date.strftime("%-d %B %Y")
    mp = MapPlot(
        sector=("state" if len(csector) == 2 else csector),
        state=ctx["csector"],
        axisbg="white",
        nocaption=True,
        title="IEM Reanalysis of %s for %s" % (PDICT.get(varname), title),
        subtitle="Data derived from various NOAA datasets",
    )
    (west, east, south, north) = mp.ax.get_extent(ccrs.PlateCarree())
    i0, j0 = iemre.find_ij(west, south)
    i1, j1 = iemre.find_ij(east, north)
    jslice = slice(j0, j1)
    islice = slice(i0, i1)

    idx0 = iemre.daily_offset(date)
    ncfn = iemre.get_daily_ncname(date.year)
    if not os.path.isfile(ncfn):
        raise NoDataFound("No Data Found.")
    with ncopen(ncfn) as nc:
        lats = nc.variables["lat"][jslice]
        lons = nc.variables["lon"][islice]
        cmap = ctx["cmap"]
        if varname in ["rsds", "power_swdn"]:
            # Value is in W m**-2, we want MJ
            multi = (86400.0 / 1000000.0) if varname == "rsds" else 1
            data = nc.variables[varname][idx0, jslice, islice] * multi
            plot_units = "MJ d-1"
            clevs = np.arange(0, 37, 3.0)
            clevs[0] = 0.01
            clevstride = 1
        elif varname in ["wind_speed"]:
            data = (masked_array(
                nc.variables[varname][idx0, jslice, islice],
                units("meter / second"),
            ).to(units("mile / hour")).m)
            plot_units = "mph"
            clevs = np.arange(0, 41, 2)
            clevs[0] = 0.01
            clevstride = 2
        elif varname in ["p01d", "p01d_12z", "snow_12z", "snowd_12z"]:
            # Value is in W m**-2, we want MJ
            data = (masked_array(nc.variables[varname][idx0, jslice, islice],
                                 units("mm")).to(units("inch")).m)
            plot_units = "inch"
            clevs = np.arange(0, 0.25, 0.05)
            clevs = np.append(clevs, np.arange(0.25, 3.0, 0.25))
            clevs = np.append(clevs, np.arange(3.0, 10.0, 1))
            clevs[0] = 0.01
            clevstride = 1
            cmap = stretch_cmap(ctx["cmap"], clevs)
        elif varname in [
                "high_tmpk",
                "low_tmpk",
                "high_tmpk_12z",
                "low_tmpk_12z",
                "avg_dwpk",
        ]:
            # Value is in W m**-2, we want MJ
            data = (masked_array(nc.variables[varname][idx0, jslice, islice],
                                 units("degK")).to(units("degF")).m)
            plot_units = "F"
            clevs = np.arange(-30, 120, 5)
            clevstride = 2
        elif varname in ["range_tmpk", "range_tmpk_12z"]:
            vname1 = "high_tmpk%s" % ("_12z"
                                      if varname == "range_tmpk_12z" else "", )
            vname2 = "low_tmpk%s" % ("_12z"
                                     if varname == "range_tmpk_12z" else "", )
            d1 = nc.variables[vname1][idx0, jslice, islice]
            d2 = nc.variables[vname2][idx0, jslice, islice]
            data = (masked_array(d1, units("degK")).to(units("degF")).m -
                    masked_array(d2, units("degK")).to(units("degF")).m)
            plot_units = "F"
            clevs = np.arange(0, 61, 5)
            clevstride = 2

    if np.ma.is_masked(np.max(data)):
        raise NoDataFound("Data Unavailable")
    x, y = np.meshgrid(lons, lats)
    if ptype == "c":
        # in the case of contour, use the centroids on the grids
        mp.contourf(
            x + 0.125,
            y + 0.125,
            data,
            clevs,
            clevstride=clevstride,
            units=plot_units,
            ilabel=True,
            labelfmt="%.0f",
            cmap=cmap,
        )
    else:
        x, y = np.meshgrid(lons, lats)
        mp.pcolormesh(
            x,
            y,
            data,
            clevs,
            clevstride=clevstride,
            cmap=cmap,
            units=plot_units,
        )

    return mp.fig
Esempio n. 40
0
    delta = rise.hour * 60 + rise.minute
    now += interval
    while True:
        ames.date = now.strftime("%Y/%m/%d")
        rise2 = mx.DateTime.strptime(str(ames.next_rising(sun)), "%Y/%m/%d %H:%M:%S")
        rise2 = rise2.localtime()
        delta2 = rise2.hour * 60 + rise2.minute
        if delta2 < delta:
            return (rise2 - rise).days

        now += interval

    return doy, arr, returnD


lats = []
lons = []
vals = []
for lon in tqdm.tqdm(range(-130, -60, 2)):
    for lat in range(20, 55, 1):
        lats.append(lat)
        lons.append(lon)
        vals.append(compute_sunrise(str(lat), str(lon)))

m = MapPlot(sector='conus',
            title='Days to Recover Morning Hour after Spring Saving Time Change',
            subtitle=('days until local time of sunrise is earlier '
                      'than on 10 March, local DST rules ignored for plot'))
m.contourf(lons, lats, vals, range(27, 78, 3), units='days')
m.postprocess(filename='180313.png')
Esempio n. 41
0
def run(basets, endts, view):
    """Generate this plot for the given basets"""

    df = read_sql("""SELECT state,
        max(magnitude) as val, ST_x(geom) as lon, ST_y(geom) as lat
        from lsrs WHERE type in ('S') and magnitude >= 0 and
        valid > %s and valid < %s GROUP by state, lon, lat
        """,
                  POSTGIS,
                  params=(basets, endts),
                  index_col=None)
    df['used'] = False
    df['textplot'] = True
    df.sort_values(by='val', ascending=False, inplace=True)

    # Now, we need to add in zeros, lets say we are looking at a .25 degree box
    mybuffer = 0.75
    newrows = []
    for lat in np.arange(reference.MW_SOUTH, reference.MW_NORTH, mybuffer):
        for lon in np.arange(reference.MW_WEST, reference.MW_EAST, mybuffer):
            df2 = df[(df['lat'] >= lat) & (df['lat'] < (lat + mybuffer)) &
                     (df['lon'] >= lon) & (df['lon'] < (lon + mybuffer))]
            if len(df2.index) == 0:
                newrows.append(
                    dict(lon=(lon + mybuffer / 2.),
                         lat=(lat + mybuffer / 2.),
                         val=0,
                         used=True,
                         textplot=False))
                continue
            maxval = df.at[df2.index[0], 'val']
            df.loc[df2[df2['val'] > (maxval * 0.5)].index, 'used'] = True
            df.loc[df2[df2['val'] < (maxval * 0.5)].index, 'textplot'] = False
    dfnew = pd.DataFrame(newrows)
    df = df.append(dfnew)
    cdf = df[df['used']]
    tdf = df[df['textplot']]

    rng = [0.01, 1, 2, 3, 4, 6, 8, 12, 18, 24, 30, 36]
    cmap = nwssnow()
    m = MapPlot(sector='iowa',
                axisbg='white',
                title="Local Storm Report Snowfall Total Analysis",
                subtitle=("Reports past 12 hours: %s"
                          "" % (endts.strftime("%d %b %Y %I:%M %p"), )))
    m.contourf(cdf['lon'].values,
               cdf['lat'].values,
               cdf['val'].values,
               rng,
               cmap=cmap)
    m.drawcounties()
    m.plot_values(tdf['lon'].values,
                  tdf['lat'].values,
                  tdf['val'].values,
                  fmt='%.1f')
    m.drawcities()
    pqstr = "plot c 000000000000 lsr_snowfall.png bogus png"
    m.postprocess(view=view, pqstr=pqstr)
    m.close()

    # slightly different title to help uniqueness
    m = MapPlot(sector='iowa',
                axisbg='white',
                title="Local Storm Report Snowfall Total Analysis",
                subtitle=("Reports valid over past 12 hours: %s"
                          "" % (endts.strftime("%d %b %Y %I:%M %p"), )))
    m.contourf(cdf['lon'].values,
               cdf['lat'].values,
               cdf['val'].values,
               rng,
               cmap=cmap)
    m.drawcounties()
    m.drawcities()
    pqstr = "plot c 000000000000 lsr_snowfall_nv.png bogus png"
    m.postprocess(view=view, pqstr=pqstr)
    m.close()

    m = MapPlot(sector='midwest',
                axisbg='white',
                title="Local Storm Report Snowfall Total Analysis",
                subtitle=("Reports past 12 hours: %s"
                          "" % (endts.strftime("%d %b %Y %I:%M %p"), )))
    m.contourf(cdf['lon'].values,
               cdf['lat'].values,
               cdf['val'].values,
               rng,
               cmap=cmap)
    m.drawcities()
    pqstr = "plot c 000000000000 mw_lsr_snowfall.png bogus png"
    m.postprocess(view=view, pqstr=pqstr)
    m.close()
Esempio n. 42
0
  tmpf >= -50 and tmpf < 140
"""

lats = []
lons = []
vals = []
valmask = []
icursor.execute(sql)
for row in icursor:
    lats.append(row['lat'])
    lons.append(row['lon'])
    vals.append(row['tmpf'])
    valmask.append((row['network'] in ['AWOS', 'IA_ASOS']))

rng = np.arange(-30, 120, 2)

for sector in ['iowa', 'midwest', 'conus']:
    m = MapPlot(axisbg='white',
                sector=sector,
                title="%s 2 meter Air Temperature" % (sector.capitalize(), ),
                subtitle=now.strftime("%d %b %Y %-I:%M %p"))
    m.contourf(lons, lats, vals, rng, clevstride=5, units='F')
    m.plot_values(lons, lats, vals, fmt='%.0f')
    if sector == 'iowa':
        m.drawcounties()
    pqstr = ("plot ac %s00 %s_tmpf.png %s_tmpf_%s.png png"
             "") % (datetime.datetime.utcnow().strftime("%Y%m%d%H"), sector,
                    sector, datetime.datetime.utcnow().strftime("%H"))
    m.postprocess(view=False, pqstr=pqstr)
    m.close()
Esempio n. 43
0
dates = []
sites = []
for row in cursor:
    station = row[0]
    if station == 'IA0000' or station[2] == 'C':
        continue
    if station not in nt.sts:
        continue
    if station in ['IA3909', 'IA0364', 'IA3473', 'IA1394']:
        continue
    sites.append(station)
    lats.append(nt.sts[station]['lat'])
    lons.append(nt.sts[station]['lon'])
    vals.append(float(row[1]))
    d = datetime.date(2015, 1, 1) + datetime.timedelta(days=int(row[1]))
    dates.append(d.strftime("%-d %b"))

rng = range(int(min(vals)-2), int(max(vals)+4), 4)
labels = []
for i in rng:
    dt = datetime.date(2015, 1, 1) + datetime.timedelta(days=i)
    labels.append(dt.strftime("%-d %b"))

m = MapPlot(title="Average First Date of Low Temperature At or Below 28$^\circ$F",
            subtitle="based on stations with data between 1951-2014")
m.contourf(lons, lats, vals, rng, clevlabels=labels,
           cmap=plt.get_cmap("jet"))
m.drawcounties()
#m.plot_values(lons, lats, dates, fmt='%s', labels=sites)
m.postprocess(filename='test.png')
Esempio n. 44
0
def doit(ts, hours):
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    # Start at 1 AM
    ts = ts.replace(minute=0, second=0, microsecond=0)
    now = ts - datetime.timedelta(hours=hours-1)
    interval = datetime.timedelta(hours=1)
    ets = datetime.datetime.utcnow()
    ets = ets.replace(tzinfo=pytz.timezone("UTC"))
    total = None
    while now < ets:
        gmt = now.astimezone(pytz.timezone("UTC"))
        gribfn = None
        for prefix in ['GaugeCorr', 'RadarOnly']:
            fn = gmt.strftime((prefix + "_QPE_01H_00.00_%Y%m%d-%H%M00"
                               ".grib2.gz"))
            res = requests.get(gmt.strftime(
                    ("http://mtarchive.geol.iastate.edu/%Y/%m/%d/mrms/ncep/" +
                     prefix + "_QPE_01H/" + fn)), timeout=30)
            if res.status_code != 200:
                continue
            o = open(TMP + "/" + fn, 'wb')
            o.write(res.content)
            o.close()
            gribfn = "%s/%s" % (TMP, fn)
            break
        if gribfn is None:
            print("q3_Xhour.py[%s] MISSING %s" % (hours, now))
            now += interval
            continue
        fp = gzip.GzipFile(gribfn, 'rb')
        (tmpfp, tmpfn) = tempfile.mkstemp()
        tmpfp = open(tmpfn, 'wb')
        tmpfp.write(fp.read())
        tmpfp.close()
        grbs = pygrib.open(tmpfn)
        grb = grbs[1]
        os.unlink(tmpfn)
        # careful here, how we deal with the two missing values!
        if total is None:
            total = grb['values']
        else:
            maxgrid = np.maximum(grb['values'], total)
            total = np.where(np.logical_and(grb['values'] >= 0,
                                            total >= 0),
                             grb['values'] + total, maxgrid)
        now += interval
        os.unlink(gribfn)

    if total is None:
        print("q3_Xhour.py no data ts: %s hours: %s" % (ts, hours))
        return

    # Scale factor is 10
    routes = "c"
    if ts.minute == 0:
        routes = "ac"
    pqstr = "plot %s %s iowa_q2_%sh.png q2/iowa_q2_%sh_%s00.png png" % (
            routes, ts.strftime("%Y%m%d%H%M"), hours, hours,
            ts.strftime("%H"))

    lts = ts.astimezone(pytz.timezone("America/Chicago"))
    subtitle = 'Total up to %s' % (lts.strftime("%d %B %Y %I:%M %p %Z"),)
    m = MapPlot(title=("NCEP MRMS Q3 (RADAR Only) %s Hour "
                       "Precipitation [inch]") % (hours,),
                subtitle=subtitle)

    clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10]

    m.contourf(mrms.XAXIS, mrms.YAXIS,
               distance(np.flipud(total), 'MM').value('IN'), clevs,
               cmap=nwsprecip())
    m.drawcounties()
    m.postprocess(pqstr=pqstr, view=False)
    m.close()
Esempio n. 45
0
File: p97.py Progetto: akrherz/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    import matplotlib.cm as cm

    pgconn = psycopg2.connect(database='coop', host='iemdb', user='******')

    sector = fdict.get('sector', 'IA')
    date1 = datetime.datetime.strptime(fdict.get('date1', '2015-01-01'),
                                       '%Y-%m-%d')
    date2 = datetime.datetime.strptime(fdict.get('date2', '2015-02-01'),
                                       '%Y-%m-%d')
    varname = fdict.get('var', 'precip_depart')

    table = "alldata_%s" % (sector, ) if sector != 'midwest' else "alldata"
    df = read_sql("""
    WITH obs as (
        SELECT station, gddxx(50, 86, high, low) as gdd50,
        sday, high, low, precip from """ + table + """ WHERE
        day >= %s and day < %s and
        substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000'),
    climo as (
        SELECT station, to_char(valid, 'mmdd') as sday, precip, high, low,
        gdd50 from climate51),
    combo as (
        SELECT o.station, o.precip - c.precip as precip_diff,
        o.high, o.low, o.gdd50, c.gdd50 as cgdd50,
        o.gdd50 - c.gdd50 as gdd50_diff,
        (o.high + o.low)/2. - (c.high + c.low)/2. as temp_diff
        from obs o JOIN climo c ON
        (o.station = c.station and o.sday = c.sday)),
    agg as (
        SELECT station, sum(precip_diff) as precip_depart,
        min(low) as min_low_temp, sum(gdd50_diff) as gdd_depart,
        avg(temp_diff) as avg_temp_depart, sum(gdd50) as gdd_sum,
        sum(cgdd50) as cgdd_sum
        from combo GROUP by station)

    SELECT d.station, d.precip_depart, d.min_low_temp, d.avg_temp_depart,
    d.gdd_depart, d.gdd_sum, d.cgdd_sum,
    ST_x(t.geom) as lon, ST_y(t.geom) as lat
    from agg d JOIN stations t on (d.station = t.id)
    WHERE t.network ~* 'CLIMATE'
    """, pgconn, params=(date1, date2), index_col='station')

    sector2 = "state" if sector != 'midwest' else 'midwest'
    m = MapPlot(sector=sector2, state=sector, axisbg='white',
                title=('%s - %s %s [%s]'
                       ) % (date1.strftime("%d %b %Y"),
                            date2.strftime("%d %b %Y"), PDICT2.get(varname),
                            UNITS.get(varname)),
                subtitle=('%s is compared with 1951-%s Climatology'
                          ' to compute departures'
                          ) % (date1.year, datetime.date.today().year - 1))
    if varname in ['precip_depart', 'avg_temp_depart']:
        rng = df[varname].abs().describe(percentiles=[0.95])['95%']
        clevels = np.linspace(0 - rng, rng, 7)
        fmt = '%.2f'
    else:
        minv = df[varname].min() - 5
        maxv = df[varname].max() + 5
        clevels = np.linspace(minv, maxv, 6, dtype='i')
        fmt = '%.0f'
    clevlabels = [fmt % x for x in clevels]
    cmap = cm.get_cmap('RdYlBu' if varname == 'precip_depart' else 'RdYlBu_r')
    cmap.set_bad('white')
    m.contourf(df['lon'].values, df['lat'].values,
               df[varname].values, clevels, clevlabels=clevlabels,
               cmap=cmap, units=UNITS.get(varname))
    m.plot_values(df['lon'].values, df['lat'].values,
                  df[varname].values, fmt=fmt, labelbuffer=10)
    if sector == 'IA':
        m.drawcounties()

    return m.fig, df
Esempio n. 46
0
def doit(ts, hours):
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    # Start at 1 AM
    ts = ts.replace(minute=0, second=0, microsecond=0)
    now = ts - datetime.timedelta(hours=hours - 1)
    interval = datetime.timedelta(hours=1)
    ets = datetime.datetime.utcnow()
    ets = ets.replace(tzinfo=pytz.timezone("UTC"))
    total = None
    while now < ets:
        gmt = now.astimezone(pytz.timezone("UTC"))
        for prefix in ["GaugeCorr", "RadarOnly"]:
            gribfn = gmt.strftime(
                (
                    "/mnt/a4/data/%Y/%m/%d/mrms/ncep/" + prefix + "_QPE_01H/" + prefix + "_QPE_01H_00.00_%Y%m%d-%H%M00"
                    ".grib2.gz"
                )
            )
            if os.path.isfile(gribfn):
                break
        if not os.path.isfile(gribfn):
            print("q3_Xhour.py MISSING %s" % (gribfn,))
            now += interval
            continue
        fp = gzip.GzipFile(gribfn, "rb")
        (tmpfp, tmpfn) = tempfile.mkstemp()
        tmpfp = open(tmpfn, "wb")
        tmpfp.write(fp.read())
        tmpfp.close()
        grbs = pygrib.open(tmpfn)
        grb = grbs[1]
        os.unlink(tmpfn)
        # careful here, how we deal with the two missing values!
        if total is None:
            total = grb["values"]
        else:
            maxgrid = np.maximum(grb["values"], total)
            total = np.where(np.logical_and(grb["values"] >= 0, total >= 0), grb["values"] + total, maxgrid)
        now += interval

    if total is None:
        print("q3_Xhour.py no data ts: %s hours: %s" % (ts, hours))
        return

    # Scale factor is 10
    routes = "c"
    if ts.minute == 0:
        routes = "ac"
    pqstr = "plot %s %s iowa_q2_%sh.png q2/iowa_q2_%sh_%s00.png png" % (
        routes,
        ts.strftime("%Y%m%d%H%M"),
        hours,
        hours,
        ts.strftime("%H"),
    )

    lts = ts.astimezone(pytz.timezone("America/Chicago"))
    subtitle = "Total up to %s" % (lts.strftime("%d %B %Y %I:%M %p %Z"),)
    m = MapPlot(title=("NCEP MRMS Q3 (RADAR Only) %s Hour " "Precipitation [inch]") % (hours,), subtitle=subtitle)

    clevs = np.arange(0, 0.2, 0.05)
    clevs = np.append(clevs, np.arange(0.2, 1.0, 0.1))
    clevs = np.append(clevs, np.arange(1.0, 5.0, 0.25))
    clevs = np.append(clevs, np.arange(5.0, 10.0, 1.0))
    clevs[0] = 0.01

    m.contourf(mrms.XAXIS, mrms.YAXIS, np.flipud(total) / 24.5, clevs)
    m.drawcounties()
    m.postprocess(pqstr=pqstr)
Esempio n. 47
0
import pygrib
from pyiem.plot import MapPlot
from pyiem import reference
import numpy as np
from pyiem.datatypes import distance

g = pygrib.open('p168m_2016090600f168.grb')

grb = g[1]
lats, lons = grb.latlons()

m = MapPlot(sector='custom', project='aea',
            south=reference.MW_SOUTH, north=reference.MW_NORTH,
            east=reference.MW_EAST, west=reference.MW_WEST,
            axisbg='tan',
            title=("Weather Prediction Center (WPC) "
                   "7 Day Forecasted Precipitation"),
            subtitle='7 PM 5 September thru 7 PM 12 September 2016')

m.contourf(lons, lats, distance(grb['values'], 'MM').value('IN'),
           np.arange(0, 4.6, 0.25), units='inches')

m.postprocess(filename='test.png')
Esempio n. 48
0
  s.iemid = c.iemid and
  (valid + '30 minutes'::interval) > now() and
  tmpf >= -50 and tmpf < 140
"""

lats = []
lons = []
vals = []
valmask = []
icursor.execute(sql)
for row in icursor:
    lats.append(row['lat'])
    lons.append(row['lon'])
    vals.append(row['tmpf'])
    valmask.append((row['network'] in ['AWOS', 'IA_ASOS']))

rng = np.arange(-30, 120, 10)

for sector in ['iowa', 'midwest', 'conus']:
    m = MapPlot(axisbg='white', sector=sector,
                title="%s 2 meter Air Temperature" % (sector.capitalize(), ),
                subtitle=now.strftime("%d %b %Y %-I:%M %p"))
    m.contourf(lons, lats, vals, rng)
    if sector == 'iowa':
        m.plot_values(lons, lats, vals, fmt='%.0f')
    pqstr = ("plot ac %s00 %s_tmpf.png %s_tmpf_%s.png png"
             "") % (datetime.datetime.utcnow().strftime("%Y%m%d%H"),
                    sector, sector, datetime.datetime.utcnow().strftime("%H"))
    m.postprocess(view=False, pqstr=pqstr)
    m.close()
Esempio n. 49
0
dates = []
sites = []
for row in cursor:
    station = row[0]
    if station == 'IA0000' or station[2] == 'C':
        continue
    if station not in nt.sts:
        continue
    if station in ['IA3909', 'IA0364', 'IA3473', 'IA1394']:
        continue
    sites.append(station)
    lats.append(nt.sts[station]['lat'])
    lons.append(nt.sts[station]['lon'])
    vals.append(float(row[1]))
    d = datetime.date(2015, 1, 1) + datetime.timedelta(days=int(row[1]))
    dates.append(d.strftime("%-d %b"))

rng = range(int(min(vals) - 2), int(max(vals) + 4), 4)
labels = []
for i in rng:
    dt = datetime.date(2015, 1, 1) + datetime.timedelta(days=i)
    labels.append(dt.strftime("%-d %b"))

m = MapPlot(
    title="Average First Date of Low Temperature At or Below 28$^\circ$F",
    subtitle="based on stations with data between 1951-2014")
m.contourf(lons, lats, vals, rng, clevlabels=labels, cmap=plt.get_cmap("jet"))
m.drawcounties()
#m.plot_values(lons, lats, dates, fmt='%s', labels=sites)
m.postprocess(filename='test.png')
Esempio n. 50
0
def run(basets, endts, view):
    """Generate this plot for the given basets"""

    df = read_sql("""SELECT state,
        max(magnitude) as val, ST_x(geom) as lon, ST_y(geom) as lat
        from lsrs WHERE type in ('S') and magnitude >= 0 and
        valid > %s and valid < %s GROUP by state, lon, lat
        """, POSTGIS, params=(basets, endts), index_col=None)
    df['used'] = False
    df['textplot'] = True
    df.sort_values(by='val', ascending=False, inplace=True)

    # Now, we need to add in zeros, lets say we are looking at a .25 degree box
    mybuffer = 0.75
    newrows = []
    for lat in np.arange(reference.MW_SOUTH, reference.MW_NORTH, mybuffer):
        for lon in np.arange(reference.MW_WEST, reference.MW_EAST, mybuffer):
            df2 = df[(df['lat'] >= lat) & (df['lat'] < (lat+mybuffer)) &
                     (df['lon'] >= lon) & (df['lon'] < (lon+mybuffer))]
            if len(df2.index) == 0:
                newrows.append(dict(lon=(lon+mybuffer/2.),
                                    lat=(lat+mybuffer/2.),
                                    val=0, used=True, textplot=False))
                continue
            maxval = df.at[df2.index[0], 'val']
            df.loc[df2[df2['val'] > (maxval * 0.5)].index, 'used'] = True
            df.loc[df2[df2['val'] < (maxval * 0.5)].index, 'textplot'] = False
    dfnew = pd.DataFrame(newrows)
    df = df.append(dfnew)
    cdf = df[df['used']]
    tdf = df[df['textplot']]

    rng = [0.01, 1, 2, 3, 4, 6, 8, 12, 18, 24, 30, 36]
    cmap = nwssnow()
    m = MapPlot(sector='iowa', axisbg='white',
                title="Local Storm Report Snowfall Total Analysis",
                subtitle=("Reports past 12 hours: %s"
                          "" % (endts.strftime("%d %b %Y %I:%M %p"), )))
    m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng,
               cmap=cmap)
    m.drawcounties()
    m.plot_values(tdf['lon'].values, tdf['lat'].values, tdf['val'].values,
                  fmt='%.1f')
    pqstr = "plot c 000000000000 lsr_snowfall.png bogus png"
    m.postprocess(view=view, pqstr=pqstr)
    m.close()

    # slightly different title to help uniqueness
    m = MapPlot(sector='iowa', axisbg='white',
                title="Local Storm Report Snowfall Total Analysis",
                subtitle=("Reports valid over past 12 hours: %s"
                          "" % (endts.strftime("%d %b %Y %I:%M %p"), )))
    m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng,
               cmap=cmap)
    m.drawcounties()
    pqstr = "plot c 000000000000 lsr_snowfall_nv.png bogus png"
    m.postprocess(view=view, pqstr=pqstr)
    m.close()

    m = MapPlot(sector='midwest', axisbg='white',
                title="Local Storm Report Snowfall Total Analysis",
                subtitle=("Reports past 12 hours: %s"
                          "" % (endts.strftime("%d %b %Y %I:%M %p"), )))
    m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng,
               cmap=cmap)
    pqstr = "plot c 000000000000 mw_lsr_snowfall.png bogus png"
    m.postprocess(view=view, pqstr=pqstr)
    m.close()
Esempio n. 51
0
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
Esempio n. 52
0
  from
    (SELECT station, year, min(low) as m from alldata_ia where
     month > 6 and sday < '1005' and year > 1950 GROUP by station, year) as foo
  ) as foo2 WHERE station != 'IA0000' and substr(station,3,1) != 'C' 
  GROUP by station ORDER by ma DESC
""")
lats = []
lons = []
vals = []
for row in ccursor:

    if not nt.sts.has_key(row[0]):
        continue
    #if nt.sts[row[0]]['archive_begin'].year > 1893:
    #    continue
    print row
    #vals.append( float(row[3]) / float(row[4]) * 100.0 )
    vals.append( row[1] / float(row[2]) * 100.0 )
    lats.append( nt.sts[row[0]]['lat'] )
    lons.append( nt.sts[row[0]]['lon'] )

import numpy as np
m = MapPlot(sector='iowa', title='Fall Minimum Temperature Percentile [100=warmest]',
            subtitle='thru 5 October, 2014 vs 1951-2013')
cmap = cm.get_cmap('jet')
m.contourf(lons, lats, vals, np.arange(0,101,10), cmap=cmap)
m.plot_values(lons, lats, vals, '%.0f')
m.drawcounties()
m.postprocess(filename='test.ps')
iemplot.makefeature('test')
Esempio n. 53
0
File: mapit.py Progetto: akrherz/DEV
def main():
    """Go Main Go."""
    pgconn = get_dbconn('iem')
    cursor = pgconn.cursor()

    cursor.execute("""SELECT ST_x(geom) as lon, ST_y(geom) as lat,
    pday from summary_2016 s JOIN stations t on (s.iemid = t.iemid)
    where day = '2016-08-24' and network in ('WI_COOP', 'MN_COOP', 'IA_COOP')
    and pday > 0 ORDER by pday DESC""")
    llons = []
    llats = []
    vals = []
    for row in cursor:
        llons.append(row[0])
        llats.append(row[1])
        vals.append("%.2f" % (row[2], ))

    pgconn = get_dbconn('postgis')
    cursor = pgconn.cursor()

    cursor.execute("""SELECT ST_x(geom) as lon, ST_y(geom) as lat,
    max(magnitude) from lsrs_2016
    where wfo in ('DMX', 'DVN', 'ARX') and typetext = 'HEAVY RAIN' and
    valid > '2016-08-23' GROUP by lon, lat ORDER by max DESC""")
    for row in cursor:
        llons.append(row[0])
        llats.append(row[1])
        vals.append("%.2f" % (row[2], ))

    img = Image.open("p24h_201608241200.png")
    data = np.flipud(np.asarray(img))
    # 7000,3500 == -130,-60,55,25 ===  -100 to -90 then 38 to 45
    sample = data[1800:2501, 3000:4501]
    sample = np.where(sample == 255, 0, sample)
    data = sample * 0.01
    data = np.where(sample > 100, 1. + (sample - 100) * 0.05, data)
    data = np.where(sample > 180, 5. + (sample - 180) * 0.2, data)
    lons = np.arange(-100, -84.99, 0.01)
    lats = np.arange(38, 45.01, 0.01)

    x, y = np.meshgrid(lons, lats)

    buff = 0.5
    m = MapPlot(sector='custom', projection='aea', west=-93.2,
                east=-90.3, south=42.5,
                north=44.,
                title='NOAA MRMS 24 Hour RADAR-Only Precipitation Estimate',
                subtitle=("MRMS valid 7 AM 23 Aug 2016 to 7 AM 24 Aug 2016, "
                          "NWS Local Storm + COOP Reports Overlaid"))
    clevs = [0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8,
             10]

    m.contourf(x[:, :-1], y[:, :-1], data, clevs, cmap=nwsprecip())

    nt = NetworkTable("IA_ASOS")
    lo = []
    la = []
    va = []
    for sid in nt.sts.keys():
        lo.append(nt.sts[sid]['lon'])
        la.append(nt.sts[sid]['lat'])
        va.append(nt.sts[sid]['name'])

    # m.plot_values(lo, la, va, fmt='%s', textsize=10, color='black')
    m.drawcounties(zorder=4, linewidth=1.)
    m.drawcities(labelbuffer=25, textsize=10, color='white',
                 outlinecolor='#000000')
    m.textmask[:, :] = 0
    m.plot_values(llons, llats, vals, fmt='%s', labelbuffer=5)

    m.postprocess(filename='test.png')
Esempio n. 54
0
vals = []
valmask = []
cursor.execute(sql)
for row in cursor:
    lats.append(row[4])
    lons.append(row[3])
    vals.append(row[2])
    valmask.append(row[1] in ['AWOS', 'IA_ASOS'])

if len(lats) < 5:
    sys.exit(0)

now = datetime.datetime.now()

m = MapPlot(sector='iowa',
            title='Iowa Visibility',
            subtitle='Valid: %s' % (now.strftime("%d %b %Y %-I:%M %p"),))

m.contourf(lons, lats, vals, np.array([0.01, 0.1, 0.25, 0.5, 1, 2, 3,
                                       5, 8, 9.9]),
           units='miles', cmap=cm.get_cmap('gray'))

m.plot_values(lons, lats, vals, '%.1f', valmask)
m.drawcounties()

pqstr = ("plot ac %s00 iowa_vsby.png vsby_contour_%s00.png png"
         "") % (datetime.datetime.utcnow().strftime("%Y%m%d%H"),
                datetime.datetime.utcnow().strftime("%H"))
m.postprocess(pqstr=pqstr)
m.close()
Esempio n. 55
0
    ("IACLIMATE", "MNCLIMATE", "NDCLIMATE", "SDCLIMATE",
     "NECLIMATE", "KSCLIMATE", "MOCLIMATE", "KYCLIMATE", "ILCLIMATE",
     "WICLIMATE", "INCLIMATE", "OHCLIMATE", "MICLIMATE"))

cursor.execute("""
 SELECT station, sum(case when precip >= 2 then precip else 0 end) /
 sum(precip) from alldata WHERE year > 1980 and precip > 0
 and year < 2011 GROUP by station
""")

lats = []
lons = []
vals = []
for row in cursor:
    if row[0][2] == 'C' or row[0][2:] == '0000':
        continue
    if row[0] not in nt.sts:
        continue
    if row[0][:2] == 'MI' and row[1] > 0.2:
        print row
    lats.append(nt.sts[row[0]]['lat'])
    lons.append(nt.sts[row[0]]['lon'])
    vals.append(row[1] * 100.0)

m = MapPlot(sector='midwest',
            title=('Contribution of Daily 2+ inch '
                   'Precip Events to Yearly Total'),
            subtitle='1981-2010 based on IEM archive of NWS COOP Data')
m.contourf(lons, lats, vals, np.arange(0, 41, 2), units='%')
m.postprocess(filename='twoinch_1981_2010.png')
Esempio n. 56
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    import matplotlib.cm as cm

    pgconn = get_dbconn('coop')
    ctx = get_autoplot_context(fdict, get_description())
    sector = ctx['sector']
    date1 = ctx['date1']
    date2 = ctx['date2']
    varname = ctx['var']
    usdm = ctx['usdm']

    table = "alldata_%s" % (sector, ) if sector != 'midwest' else "alldata"
    df = read_sql("""
    WITH obs as (
        SELECT station, gddxx(50, 86, high, low) as gdd50,
        sday, high, low, precip from """ + table + """ WHERE
        day >= %s and day < %s and
        substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000'),
    climo as (
        SELECT station, to_char(valid, 'mmdd') as sday, precip, high, low,
        gdd50 from climate51),
    combo as (
        SELECT o.station, o.precip - c.precip as precip_diff,
        o.precip as precip, c.precip as cprecip,
        o.high, o.low, o.gdd50, c.gdd50 as cgdd50,
        o.gdd50 - c.gdd50 as gdd50_diff,
        (o.high + o.low)/2. - (c.high + c.low)/2. as temp_diff
        from obs o JOIN climo c ON
        (o.station = c.station and o.sday = c.sday)),
    agg as (
        SELECT station, sum(precip_diff) as precip_depart,
        sum(precip) as precip, sum(cprecip) as cprecip,
        min(low) as min_low_temp, sum(gdd50_diff) as gdd_depart,
        avg(temp_diff) as avg_temp_depart, sum(gdd50) as gdd_sum,
        sum(cgdd50) as cgdd_sum
        from combo GROUP by station)

    SELECT d.station, t.name,
    d.precip as precip_sum, d.cprecip as cprecip_sum,
    d.precip_depart, d.min_low_temp, d.avg_temp_depart,
    d.gdd_depart, d.gdd_sum, d.cgdd_sum,
    ST_x(t.geom) as lon, ST_y(t.geom) as lat
    from agg d JOIN stations t on (d.station = t.id)
    WHERE t.network ~* 'CLIMATE'
    """,
                  pgconn,
                  params=(date1, date2),
                  index_col='station')
    df = df.reindex(df[varname].abs().sort_values(ascending=False).index)

    sector2 = "state" if sector != 'midwest' else 'midwest'
    datefmt = "%d %b %Y" if varname != 'cgdd_sum' else '%d %b'
    subtitle = ("Climatology is based on data from 1951-%s") % (
        datetime.date.today().year - 1, )
    if varname != 'cgdd_sum':
        subtitle = ('%s is compared with 1951-%s Climatology'
                    ' to compute departures') % (
                        date1.year, datetime.date.today().year - 1)
    mp = MapPlot(sector=sector2,
                 state=sector,
                 axisbg='white',
                 title=('%s - %s %s [%s]') %
                 (date1.strftime(datefmt), date2.strftime(datefmt),
                  PDICT2.get(varname), UNITS.get(varname)),
                 subtitle=subtitle)
    if varname in ['precip_depart', 'avg_temp_depart']:
        rng = df[varname].abs().describe(percentiles=[0.95])['95%']
        clevels = np.linspace(0 - rng, rng, 7)
        fmt = '%.2f'
    else:
        minv = df[varname].min() - 5
        maxv = df[varname].max() + 5
        clevels = np.linspace(minv, maxv, 6, dtype='i')
        fmt = '%.0f'
    clevlabels = [fmt % x for x in clevels]
    cmap = cm.get_cmap('RdYlBu' if varname == 'precip_depart' else 'RdYlBu_r')
    cmap.set_bad('white')
    mp.contourf(df['lon'].values,
                df['lat'].values,
                df[varname].values,
                clevels,
                clevlabels=clevlabels,
                cmap=cmap,
                units=UNITS.get(varname))
    mp.plot_values(df['lon'].values,
                   df['lat'].values,
                   df[varname].values,
                   fmt=fmt,
                   labelbuffer=10)
    if sector == 'IA':
        mp.drawcounties()
    if usdm == 'yes':
        mp.draw_usdm(date2, filled=False, hatched=True)

    return mp.fig, df