Exemplo n.º 1
0
def main():
    """Map some CLI data"""
    pgconn = get_dbconn('iem')

    df = read_sql("""
    WITH data as (
        SELECT station, snow_jul1 - snow_jul1_normal as s
        from cli_data where valid = '2019-02-18' and snow_jul1 > 0
        and snow_jul1_normal > 0)

    select station, st_x(geom) as lon, st_y(geom) as lat, c.s as val from
    data c JOIN stations s on (s.id = c.station)
    WHERE s.network = 'NWSCLI'
    """, pgconn, index_col=None)
    df['color'] = '#ff0000'
    df.loc[df['val'] > 0, 'color'] = '#0000ff'

    mp = MapPlot(sector='midwest', axisbg='white',
                 title=("2018-2019 Snowfall Total Departure "
                        "from Average [inches]"),
                 subtitle='18 Feb 2019 Based on NWS CLI Reporting Sites')
    mp.plot_values(
        df['lon'].values, df['lat'].values,
        df['val'].values, fmt='%.1f', textsize=12, color=df['color'].values,
        labelbuffer=1)
    mp.postprocess(filename='test.png')
Exemplo n.º 2
0
def plot(data, v):
    ''' Actually plot this data '''
    nt = NetworkTable("ISUSM")
    lats = []
    lons = []
    vals = []
    valid = None
    for sid in data.keys():
        if data[sid][v] is None:
            continue
        lats.append(nt.sts[sid]['lat'])
        lons.append(nt.sts[sid]['lon'])
        vals.append(data[sid][v])
        valid = data[sid]['valid']

    if valid is None:
        m = MapPlot(sector='iowa', axisbg='white',
                    title=('ISU Soil Moisture Network :: %s'
                           '') % (CTX[v]['title'], ),
                    figsize=(8.0, 6.4))
        m.plot_values([-95, ], [41.99, ], ['No Data Found'], '%s', textsize=30)
        m.postprocess(web=True)
        return

    m = MapPlot(sector='iowa', axisbg='white',
                title='ISU Soil Moisture Network :: %s' % (CTX[v]['title'],),
                subtitle='valid %s' % (valid.strftime("%-d %B %Y %I:%M %p"),),
                figsize=(8.0, 6.4))
    m.plot_values(lons, lats, vals, '%.1f')
    m.drawcounties()
    m.postprocess(web=True)
Exemplo n.º 3
0
def plot(df):
    from pyiem.plot import MapPlot
    m = MapPlot(sector='midwest', continentalcolor='white')
    m.plot_values(df['lon'].values, df['lat'].values, df['highdata'].values,
                  labelbuffer=0)
    m.postprocess(filename='test.png')
    m.close()
Exemplo n.º 4
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
Exemplo n.º 5
0
def runYear(year):
    # Grab the data
    sql = """SELECT station,
        sum(case when precip >= 0.01 then 1 else 0 end) as days, max(day)
        from alldata_ia WHERE year = %s and substr(station,3,1) != 'C' 
        and station != 'IA0000' GROUP by station""" % (year,)

    lats = []
    lons = []
    vals = []
    labels = []
    ccursor.execute( sql )
    for row in ccursor:
        sid = row['station'].upper()
        if not nt.sts.has_key(sid):
            continue
        labels.append( sid[2:] )
        lats.append( nt.sts[sid]['lat'] )
        lons.append( nt.sts[sid]['lon'] )
        vals.append( row['days'] )
        maxday = row['max']

    #---------- Plot the points
    m = MapPlot(title="Days with Measurable Precipitation (%s)" % (year,),
                subtitle='Map valid January 1 - %s' % (maxday.strftime("%b %d")),
                axisbg='white')
    m.plot_values(lons, lats, vals, fmt='%.0f', labels=labels,
                  labeltextsize=8, labelcolor='tan')
    m.drawcounties()
    pqstr = "plot m %s bogus %s/summary/precip_days.png png" % (
                                        now.strftime("%Y%m%d%H%M"), year,)
    m.postprocess(pqstr=pqstr)
Exemplo n.º 6
0
def main():
    """Go Main Go"""
    pgconn = get_dbconn('iem', user='******')
    icursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    lats = []
    lons = []
    vals = []
    valmask = []
    labels = []
    icursor.execute("""
        select id, ST_x(s.geom) as lon, ST_y(s.geom) as lat, pday
        from summary c, stations s
        WHERE day = 'TODAY' and pday >= 0 and pday < 20
        and s.network = 'IA_COOP' and s.iemid = c.iemid
    """)
    for row in icursor:
        lats.append(row['lat'])
        lons.append(row['lon'])
        vals.append(n(row['pday']))
        labels.append(row['id'])
        valmask.append(True)

    mp = MapPlot(title="Iowa COOP 24 Hour Precipitation",
                 axisbg='white',
                 subtitle="ending approximately %s 7 AM" %
                 (datetime.datetime.now().strftime("%-d %b %Y"), ))
    mp.plot_values(lons, lats, vals)
    pqstr = "plot ac %s iowa_coop_precip.png iowa_coop_precip.png png" % (
        datetime.datetime.now().strftime("%Y%m%d%H%M"), )
    mp.postprocess(pqstr=pqstr)
    mp.close()
Exemplo n.º 7
0
def main():
    """Go!"""
    title = 'NOAA MRMS Q3: RADAR + Guage Corrected Rainfall Estimates + NWS Storm Reports'
    mp = MapPlot(sector='custom',
                 north=42.3, east=-93.0, south=41.65, west=-94.1,
                 axisbg='white',
                 titlefontsize=14,
                 title=title,
                 subtitle='Valid: 14 June 2018')

    shp = shapefile.Reader('cities.shp')
    for record in shp.shapeRecords():
        geo = shape(record.shape)
        mp.ax.add_geometries([geo], ccrs.PlateCarree(), zorder=Z_OVERLAY2,
                             facecolor='None', edgecolor='k', lw=2)

    grbs = pygrib.open('MRMS_GaugeCorr_QPE_24H_00.00_20180614-200000.grib2')
    grb = grbs.message(1)
    pcpn = distance(grb['values'], 'MM').value('IN')
    lats, lons = grb.latlons()
    lons -= 360.
    clevs = [0.01, 0.1, 0.3, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10]
    cmap = nwsprecip()
    cmap.set_over('k')

    mp.pcolormesh(lons, lats, pcpn, clevs, cmap=cmap, latlon=True,
                  units='inch')
    lons, lats, vals, labels = get_data()
    mp.drawcounties()
    mp.plot_values(lons, lats, vals, "%s", labels=labels,
                   labelbuffer=1, labelcolor='white')

    mp.drawcities(labelbuffer=5, minarea=0.2)
    mp.postprocess(filename='test.png')
Exemplo n.º 8
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)
Exemplo n.º 9
0
def runYear(year):
    # Grab the data
    sql = """SELECT station, sum(precip) as total, max(day)
           from alldata_ia WHERE year = %s and
           station != 'IA0000' and
           substr(station,3,1) != 'C' and
           precip is not null GROUP by station""" % (year,)

    lats = []
    lons = []
    vals = []
    labels = []
    ccursor.execute( sql )
    for row in ccursor:
        sid = row['station']
        if not nt.sts.has_key(sid):
            continue
        labels.append( sid[2:] )
        lats.append( nt.sts[sid]['lat'] )
        lons.append( nt.sts[sid]['lon'] )
        vals.append( row['total'] )
        maxday = row['max']

    m = MapPlot(title="Total Precipitation [inch] (%s)" % (year,),
                subtitle='1 January - %s' % (maxday.strftime("%d %B"),),
                axisbg='white')
    m.plot_values(lons, lats, vals, labels=labels, fmt='%.2f',
                  labeltextsize=8, labelcolor='tan')
    pqstr = "plot m %s bogus %s/summary/total_precip.png png" % (
                                        now.strftime("%Y%m%d%H%M"), year,)
    m.postprocess(pqstr=pqstr)
Exemplo n.º 10
0
Arquivo: p97.py Projeto: 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
Exemplo n.º 11
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)
Exemplo n.º 12
0
def doday():
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    sts = mx.DateTime.DateTime(2013,5,25,12)
    ets = mx.DateTime.DateTime(2013,5,31,12)
    interval = mx.DateTime.RelativeDateTime(days=1)
    now = sts
    total = None
    while now < ets:
        fp = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.24h.grib" % (
            now.strftime("%Y/%m/%d"), 
            now.strftime("%Y%m%d%H") )
        if os.path.isfile(fp):
            lts = now
            grbs = pygrib.open(fp)

            if total is None:
                g = grbs[1]
                total = g["values"]
                lats, lons = g.latlons()
            else:
                total += grbs[1]["values"]
            grbs.close()
        now += interval
        
    m = MapPlot(sector='iowa', title='NOAA Stage IV & Iowa ASOS Precipitation',
                subtitle='25-30 May 2013')
    m.pcolormesh(lons, lats, total / 25.4, numpy.arange(0,14.1,1), latlon=True,
                 units='inch')
    m.drawcounties()
    m.plot_values(dlons, dlats, dvals, '%.02f')
    m.postprocess(filename='test.svg')
    import iemplot
    iemplot.makefeature('test')
Exemplo n.º 13
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()
Exemplo 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()
Exemplo n.º 15
0
def main():
    """Go Main Go"""
    pgconn = get_dbconn('iem', user='******')
    df = read_sql("""
     select station, st_x(geom), st_y(geom), snow_jul1, snow_jul1_normal
     from cli_data c JOIN stations t on (t.id = c.station)
     WHERE c.valid = 'YESTERDAY' and t.network = 'NWSCLI'
     and snow_jul1 is not null and snow_jul1_normal is not null
     and t.id not in ('RAP', 'DVN', 'FGF', 'OAX', 'MPX')
    """,
                  pgconn,
                  index_col='station')
    df['departure'] = df['snow_jul1'] - df['snow_jul1_normal']
    df['colors'] = df['departure'].apply(lambda x: '#ff0000'
                                         if x < 0 else '#0000ff')

    yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
    year = yesterday.year if yesterday.month > 6 else yesterday.year - 1

    mp = MapPlot(sector='midwest',
                 axisbg='white',
                 title='NWS Total Snowfall (inches) thru %s' %
                 (yesterday.strftime("%-d %B %Y"), ),
                 subtitle=('1 July %s - %s') % (
                     year,
                     datetime.datetime.today().strftime("%-d %B %Y"),
                 ))
    mp.plot_values(df['st_x'].values,
                   df['st_y'].values,
                   df['snow_jul1'].values,
                   fmt='%.1f',
                   labelbuffer=5)
    pqstr = ("data ac %s0000 summary/mw_season_snowfall.png "
             "mw_season_snowfall.png png") % (
                 datetime.datetime.today().strftime("%Y%m%d"), )
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()

    # Depature
    mp = MapPlot(sector='midwest',
                 axisbg='white',
                 title='NWS Total Snowfall Departure (inches) thru %s' %
                 (yesterday.strftime("%-d %B %Y"), ),
                 subtitle=('1 July %s - %s') % (
                     year,
                     datetime.datetime.today().strftime("%-d %B %Y"),
                 ))
    mp.plot_values(df['st_x'].values,
                   df['st_y'].values,
                   df['departure'].values,
                   color=df['colors'].values,
                   fmt='%.1f',
                   labelbuffer=5)
    pqstr = ("data ac %s0000 summary/mw_season_snowfall_departure.png "
             "mw_season_snowfall_departure.png png") % (
                 datetime.datetime.today().strftime("%Y%m%d"), )
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Exemplo n.º 16
0
Arquivo: p125.py Projeto: 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
Exemplo n.º 17
0
def test_drawrandomtext():
    """See if we can handle the fun that is drawing random text"""
    mp = MapPlot(sector='iowa', title='Fun Text, here and there',
                 continentalcolor='white', debug=True, nocaption=True)
    mp.plot_values([-94, -92, -91, -92],
                   [42, 41, 43, 42.4],
                   ['One', 'Two\nTwo', 'Three\nThree\nThree',
                   'Four\nFour\nFour\nFour'], showmarker=True)
    return mp.fig
Exemplo n.º 18
0
def test_overlap():
    """ Do some checking of our overlaps logic """
    mp = MapPlot(sector='midwest', continentalcolor='white', nocaption=True)
    lons = np.linspace(-99, -90, 100)
    lats = np.linspace(38, 44, 100)
    vals = lats
    labels = ['%.2f' % (s,) for s in lats]
    mp.plot_values(lons, lats, vals, fmt='%.2f', labels=labels)
    return mp.fig
Exemplo n.º 19
0
def runYear(year):
    """Do Work."""
    # Grab the data
    now = datetime.datetime.now()
    nt = NetworkTable("IACLIMATE")
    # Help plot readability
    nt.sts["IA0200"]["lon"] = -93.4
    nt.sts["IA5992"]["lat"] = 41.65
    pgconn = get_dbconn("coop", user="******")
    ccursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    sql = """SELECT station, sum(precip) as total, max(day)
           from alldata_ia WHERE year = %s and
           station != 'IA0000' and
           substr(station,3,1) != 'C' and
           precip is not null GROUP by station""" % (
        year,
    )

    lats = []
    lons = []
    vals = []
    labels = []
    ccursor.execute(sql)
    for row in ccursor:
        sid = row["station"]
        if sid not in nt.sts:
            continue
        labels.append(sid[2:])
        lats.append(nt.sts[sid]["lat"])
        lons.append(nt.sts[sid]["lon"])
        vals.append(row["total"])
        maxday = row["max"]

    # pre-1900 dates cause troubles
    lastday = "31 December"
    if now.year == maxday.year:
        lastday = maxday.strftime("%d %B")
    mp = MapPlot(
        title="Total Precipitation [inch] (%s)" % (year,),
        subtitle="1 January - %s" % (lastday,),
        axisbg="white",
    )
    mp.plot_values(
        lons,
        lats,
        vals,
        labels=labels,
        fmt="%.2f",
        labeltextsize=8,
        labelcolor="tan",
    )
    pqstr = "plot m %s bogus %s/summary/total_precip.png png" % (
        now.strftime("%Y%m%d%H%M"),
        year,
    )
    mp.postprocess(pqstr=pqstr)
Exemplo n.º 20
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
Exemplo n.º 21
0
def plot(df):
    """Diagnostic"""
    m = MapPlot(sector="midwest", continentalcolor="white")
    m.plot_values(
        df["lon"].values,
        df["lat"].values,
        df["highdata"].values,
        labelbuffer=0,
    )
    m.postprocess(filename="test.png")
    m.close()
Exemplo n.º 22
0
def doday(ts, realtime):
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    nt = NetworkTable(["AWOS", "IA_ASOS"])
    pgconn = get_dbconn("iem", user="******")
    df = read_sql(
        """
    SELECT id as station, min(feel) as wcht from current_log c JOIN stations t
    on (c.iemid = t.iemid) WHERE t.network in ('IA_ASOS', 'AWOS')
    and valid >= %s and valid < %s + '24 hours'::interval
    and feel is not null and sknt > 0 GROUP by id
    """,
        pgconn,
        params=(ts, ts),
        index_col="station",
    )
    routes = "ac"
    if not realtime:
        routes = "a"
    lons = []
    lats = []
    vals = []
    labels = []
    for station, row in df.iterrows():
        lons.append(nt.sts[station]["lon"])
        lats.append(nt.sts[station]["lat"])
        vals.append(row["wcht"])
        labels.append(station)

    pqstr = ("plot %s %s00 summary/iowa_min_windchill.png "
             "summary/iowa_min_windchill.png png") % (routes,
                                                      ts.strftime("%Y%m%d%H"))
    mp = MapPlot(
        title=(r"%s Minimum Wind Chill Temperature $^\circ$F") %
        (ts.strftime("%-d %b %Y"), ),
        subtitle="Calm conditions are excluded from analysis",
        continentalcolor="white",
    )

    mp.plot_values(
        lons,
        lats,
        vals,
        "%.1f",
        labels=labels,
        textsize=12,
        labelbuffer=5,
        labeltextsize=10,
    )
    mp.drawcounties()
    mp.postprocess(pqstr=pqstr, view=False)
    mp.close()
Exemplo n.º 23
0
def runYear(year):
    """Do as I say"""
    # Grab the data
    now = datetime.datetime.now()
    nt = NetworkTable("IACLIMATE")
    nt.sts["IA0200"]["lon"] = -93.4
    nt.sts["IA5992"]["lat"] = 41.65
    pgconn = get_dbconn("coop", user="******")
    ccursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    lats = []
    lons = []
    vals = []
    labels = []
    ccursor.execute(
        """
        SELECT station,
        sum(case when precip > 0.009 then 1 else 0 end) as days, max(day)
        from alldata_ia WHERE year = %s and substr(station,3,1) != 'C'
        and station != 'IA0000' GROUP by station
    """,
        (year, ),
    )
    for row in ccursor:
        sid = row["station"].upper()
        if sid not in nt.sts:
            continue
        labels.append(sid[2:])
        lats.append(nt.sts[sid]["lat"])
        lons.append(nt.sts[sid]["lon"])
        vals.append(row["days"])
        maxday = row["max"]

    mp = MapPlot(
        title="Days with Measurable Precipitation (%s)" % (year, ),
        subtitle="Map valid January 1 - %s" % (maxday.strftime("%b %d")),
        axisbg="white",
    )
    mp.plot_values(
        lons,
        lats,
        vals,
        fmt="%.0f",
        labels=labels,
        labeltextsize=8,
        labelcolor="tan",
    )
    mp.drawcounties()
    pqstr = "plot m %s bogus %s/summary/precip_days.png png" % (
        now.strftime("%Y%m%d%H%M"),
        year,
    )
    mp.postprocess(pqstr=pqstr)
Exemplo n.º 24
0
def plot_precip_month(valid):
    """ Go Main Go

    Args:
      valid (datetime): The timestamp we are interested in!
    """
    pgconn = get_dbconn("iem", user="******")
    cursor = pgconn.cursor()

    d1 = valid.replace(day=1)
    d2 = d1 + datetime.timedelta(days=35)
    d2 = d2.replace(day=1)

    cursor.execute(
        """SELECT sum(pday), id, st_x(geom), st_y(geom)
    from summary s JOIN stations t on
    (t.iemid = s.iemid) WHERE s.day >= %s and s.day < %s
    and t.network in ('IA_COOP', 'NE_COOP', 'MO_COOP', 'IL_COOP', 'WI_COOP',
    'MN_COOP')
    and pday is not null and pday >= 0 and
    extract(hour from coop_valid) between 5 and 10
    GROUP by id, st_x, st_y""",
        (d1.date(), d2.date()),
    )
    labels = []
    vals = []
    lats = []
    lons = []
    for row in cursor:
        labels.append(row[1])
        vals.append(pretty(row[0]))
        lats.append(row[3])
        lons.append(row[2])

    mp = MapPlot(
        title="%s NWS COOP Month Precipitation Totals [inch]"
        % (valid.strftime("%-d %b %Y"),),
        subtitle="Reports valid between 6 and 9 AM",
        axisbg="white",
        figsize=(10.24, 7.68),
    )
    mp.plot_values(lons, lats, vals, fmt="%s", labels=labels, labelcolor="tan")
    mp.drawcounties()

    pqstr = "plot ac %s0000 coopMonthPlot.gif coopMonthPlot.gif gif" % (
        valid.strftime("%Y%m%d"),
    )

    mp.postprocess(pqstr=pqstr)
    mp.close()

    pgconn.close()
Exemplo n.º 25
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()
Exemplo n.º 26
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,
      (case when c.pday < 0 or c.day is null then 0
          else c.pday end) as rainfall
     from summary_%s c, current c2, stations s
     WHERE s.iemid = c2.iemid and c2.iemid = c.iemid and
     c2.valid > (now() - '2 hours'::interval)
     and c.day = 'TODAY'
     and s.country = 'US' and (s.network ~* 'ASOS' or s.network = 'AWOS')
     and s.state in ('IA','MN','WI','IL','MO','NE','KS','SD','ND')
    """ % (
        now.year,
    )

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

    if len(lats) < 3:
        return

    mp = MapPlot(
        title="Iowa ASOS/AWOS Rainfall Reports",
        axisbg="white",
        subtitle="%s" % (now.strftime("%d %b %Y"),),
    )
    mp.drawcounties()
    mp.plot_values(lons, lats, vals)
    pqstr = "plot c 000000000000 summary/today_prec.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Exemplo n.º 27
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()
Exemplo n.º 28
0
def doday(ts, realtime):
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    nt = NetworkTable(['AWOS', 'IA_ASOS'])
    pgconn = get_dbconn('iem', user='******')
    df = read_sql("""
    SELECT id as station, tmpf, sknt from current_log c JOIN stations t
    on (c.iemid = t.iemid) WHERE t.network in ('IA_ASOS', 'AWOS')
    and valid >= %s and valid < %s + '24 hours'::interval
    and tmpf is not null and sknt > 0
    """,
                  pgconn,
                  params=(ts, ts),
                  index_col=None)
    df['wcht'] = windchill(df['tmpf'].values * units.degF, df['sknt'].values *
                           units.knots).to(units.degF).magnitude
    gdf = df.groupby('station').min()
    routes = 'ac'
    if not realtime:
        routes = 'a'
    lons = []
    lats = []
    vals = []
    labels = []
    for station, row in gdf.iterrows():
        lons.append(nt.sts[station]['lon'])
        lats.append(nt.sts[station]['lat'])
        vals.append(row['wcht'])
        labels.append(station)

    pqstr = ("plot %s %s00 summary/iowa_min_windchill.png "
             "summary/iowa_min_windchill.png png") % (routes,
                                                      ts.strftime("%Y%m%d%H"))
    mp = MapPlot(title=(r"%s Minimum Wind Chill Temperature $^\circ$F") %
                 (ts.strftime("%-d %b %Y"), ),
                 subtitle="Calm conditions are excluded from analysis",
                 continentalcolor='white')

    mp.plot_values(lons,
                   lats,
                   vals,
                   '%.1f',
                   labels=labels,
                   textsize=12,
                   labelbuffer=5,
                   labeltextsize=10)
    mp.drawcounties()
    mp.postprocess(pqstr=pqstr, view=False)
    mp.close()
Exemplo n.º 29
0
def main():
    """Go Main Go"""
    now = datetime.date.today()
    pgconn = get_dbconn("iem", user="******")
    icursor = pgconn.cursor()

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

    # Compute normal from the climate database
    sql = """SELECT id,
        sum(pday) as precip,
        sum(CASE when pday is null THEN 1 ELSE 0 END) as missing,
        ST_x(s.geom) as lon, ST_y(s.geom) as lat
        from summary_%s c JOIN stations s
        ON (s.iemid = c.iemid)
        WHERE s.network in ('IA_COOP') and s.iemid = c.iemid and
        day >= '%s' and day < '%s'
        GROUP by id, lat, lon""" % (
        now.year,
        day1.strftime("%Y-%m-%d"),
        day2.strftime("%Y-%m-%d"),
    )

    lats = []
    lons = []
    precip = []
    labels = []
    icursor.execute(sql)
    for row in icursor:
        if row[2] > (now.day / 3) or row[1] is None:
            continue

        sid = row[0]
        labels.append(sid)
        lats.append(row[4])
        lons.append(row[3])
        precip.append(row[1])

    mp = MapPlot(
        title="This Month's Precipitation [inch] (NWS COOP Network)",
        subtitle=now.strftime("%b %Y"),
        axisbg="white",
    )
    mp.plot_values(lons, lats, precip, fmt="%.2f", labels=labels)
    mp.drawcounties()
    pqstr = "plot c 000000000000 coopMonthPlot.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Exemplo n.º 30
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()
Exemplo n.º 31
0
def plot_snowdepth(valid):
    """ Go Main Go

    Args:
      valid (datetime): The timestamp we are interested in!
    """
    pgconn = get_dbconn("iem", user="******")
    cursor = pgconn.cursor()

    cursor.execute(
        """SELECT snowd, id, st_x(geom), st_y(geom)
    from summary s JOIN stations t on
    (t.iemid = s.iemid) WHERE s.day = %s
    and t.network in ('IA_COOP', 'NE_COOP', 'MO_COOP', 'IL_COOP', 'WI_COOP',
    'MN_COOP')
    and snowd is not null and snowd >= 0 and
    extract(hour from coop_valid) between 5 and 10""",
        (valid.date(),),
    )
    labels = []
    vals = []
    lats = []
    lons = []
    for row in cursor:
        labels.append(row[1])
        vals.append(pretty(row[0], precision=0))
        lats.append(row[3])
        lons.append(row[2])

    mp = MapPlot(
        title="%s NWS COOP Snowfall Depth Reports [inch]"
        % (valid.strftime("%-d %b %Y"),),
        subtitle="Reports valid between 6 and 9 AM",
        axisbg="white",
        figsize=(10.24, 7.68),
    )
    mp.plot_values(lons, lats, vals, fmt="%s", labels=labels, labelcolor="tan")
    mp.drawcounties()

    pqstr = "plot ac %s0000 coopSnowDepth.gif coopSnowDepth.gif gif" % (
        valid.strftime("%Y%m%d"),
    )

    mp.postprocess(pqstr=pqstr)
    mp.close()

    pgconn.close()
Exemplo n.º 32
0
def plot_snow_month(valid):
    """ Go Main Go

    Args:
      valid (datetime): The timestamp we are interested in!
    """
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor()

    d1 = valid.replace(day=1)
    d2 = d1 + datetime.timedelta(days=35)
    d2 = d2.replace(day=1)

    cursor.execute(
        """SELECT sum(snow), id, st_x(geom), st_y(geom)
    from summary s JOIN stations t on
    (t.iemid = s.iemid) WHERE s.day >= %s and s.day < %s
    and t.network in ('IA_COOP', 'NE_COOP', 'MO_COOP', 'IL_COOP', 'WI_COOP',
    'MN_COOP')
    and snow is not null and snow >= 0 and
    extract(hour from coop_valid) between 5 and 10
    GROUP by id, st_x, st_y""", (d1.date(), d2.date()))
    labels = []
    vals = []
    lats = []
    lons = []
    for row in cursor:
        labels.append(row[1])
        vals.append(pretty(row[0], 0))
        lats.append(row[3])
        lons.append(row[2])

    mp = MapPlot(title='%s NWS COOP Month Snowfall Totals [inch]' %
                 (valid.strftime("%-d %b %Y"), ),
                 subtitle='Reports valid between 6 and 9 AM',
                 axisbg='white',
                 figsize=(10.24, 7.68))
    mp.plot_values(lons, lats, vals, fmt='%s', labels=labels, labelcolor='tan')
    mp.drawcounties()

    pqstr = "plot ac %s0000 coopMonthSPlot.gif coopMonthSPlot.gif gif" % (
        valid.strftime("%Y%m%d"), )

    mp.postprocess(pqstr=pqstr)
    mp.close()

    pgconn.close()
Exemplo n.º 33
0
def main():
    """Go Main Go"""
    now = datetime.datetime.now()

    qdict = pyiem.tracker.loadqc()

    pgconn = get_dbconn("iem")
    icursor = pgconn.cursor()

    # Compute normal from the climate database
    sql = """SELECT id,
          sum(pday) as precip,
          ST_x(geom) as lon, ST_y(geom) as lat from summary_%s s, stations t
         WHERE t.network in ('IA_ASOS', 'AWOS') and
          extract(month from day) = %s
          and extract(year from day) = extract(year from now())
         and t.iemid = s.iemid GROUP by id, lat, lon
    """ % (
        now.year,
        now.strftime("%m"),
    )

    lats = []
    lons = []
    precip = []
    labels = []
    icursor.execute(sql)
    for row in icursor:
        sid = row[0]
        labels.append(sid)
        lats.append(row[3])
        lons.append(row[2])
        if not qdict.get(sid, {}).get("precip", False) and row[1] is not None:
            precip.append("%.2f" % (row[1], ))
        else:
            precip.append("M")

    mp = MapPlot(
        title="This Month's Precipitation [inch]",
        subtitle=now.strftime("%b %Y"),
        axisbg="white",
    )
    mp.plot_values(lons, lats, precip, labels=labels)
    pqstr = "plot c 000000000000 summary/month_prec.png bogus png"
    mp.postprocess(pqstr=pqstr)
    mp.close()
Exemplo n.º 34
0
def plot_precip_month(valid):
    """ Go Main Go

    Args:
      valid (datetime): The timestamp we are interested in!
    """
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor()

    d1 = valid.replace(day=1)
    d2 = d1 + datetime.timedelta(days=35)
    d2 = d2.replace(day=1)

    cursor.execute("""SELECT sum(pday), id, st_x(geom), st_y(geom)
    from summary s JOIN stations t on
    (t.iemid = s.iemid) WHERE s.day >= %s and s.day < %s
    and t.network in ('IA_COOP', 'NE_COOP', 'MO_COOP', 'IL_COOP', 'WI_COOP',
    'MN_COOP')
    and pday is not null and pday >= 0 and
    extract(hour from coop_valid) between 5 and 10
    GROUP by id, st_x, st_y""", (d1.date(), d2.date()))
    labels = []
    vals = []
    lats = []
    lons = []
    for row in cursor:
        labels.append(row[1])
        vals.append(pretty(row[0]))
        lats.append(row[3])
        lons.append(row[2])

    m = MapPlot(title='%s NWS COOP Month Precipitation Totals [inch]' % (
                                            valid.strftime("%-d %b %Y"),),
                subtitle='Reports valid between 6 and 9 AM',
                axisbg='white', figsize=(10.24, 7.68))
    m.plot_values(lons, lats, vals, fmt='%s', labels=labels,
                  labelcolor='tan')
    m.drawcounties()

    pqstr = "plot ac %s0000 coopMonthPlot.gif coopMonthPlot.gif gif" % (
                                                    valid.strftime("%Y%m%d"),)

    m.postprocess(pqstr=pqstr)
    m.close()

    pgconn.close()
Exemplo n.º 35
0
Arquivo: p165.py Projeto: 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
Exemplo n.º 36
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()
Exemplo n.º 37
0
def test_issue98_labelbar():
    """Sometimes our label bar sucks."""
    mp = MapPlot(
        title='Proportional Colorbar with some rotation',
        sector='iowa', nocaption=True)
    cmap = plot.maue()
    cmap.set_under('white')
    cmap.set_over('black')
    clevs = np.arange(0, 1., 0.1)
    clevs[-1] = 3.987654
    norm = mpcolors.BoundaryNorm(clevs, cmap.N)
    mp.plot_values(
        [-94, -92, -91, -92], [42, 41, 43, 42.4],
        ['0.5', '0.25', '1.0', '5.0'], color=cmap(norm([0.5, 0.25, 1.0, 5.0])),
        showmarker=True
    )
    mp.draw_colorbar(clevs, cmap, norm, spacing='proportional')
    return mp.fig
Exemplo n.º 38
0
def main():
    """Go Main Go"""
    pgconn = get_dbconn("iem", user="******")
    icursor = pgconn.cursor()
    now = datetime.datetime.now()

    sql = """
    WITH lows as (
     SELECT c.iemid, min(tmpf) as low12z from current_log c JOIN stations s
     ON (s.iemid = c.iemid)
     WHERE tmpf > -90 and valid > '%s 00:00:00+00' and valid < '%s 12:00:00+00'
     and s.network in ('IA_ASOS', 'AWOS') GROUP by c.iemid
    )

    select t.id, ST_x(t.geom) as lon, ST_y(t.geom) as lat, l.low12z from
    lows l JOIN stations t on (t.iemid = l.iemid)
    """ % (
        now.strftime("%Y-%m-%d"),
        now.strftime("%Y-%m-%d"),
    )

    lats = []
    lons = []
    vals = []
    valmask = []
    labels = []
    icursor.execute(sql)
    for row in icursor:
        lats.append(row[2])
        lons.append(row[1])
        vals.append(float(row[3]))
        labels.append(row[0])
        valmask.append(True)

    mp = MapPlot(
        title="Iowa ASOS/AWOS 12Z Morning Low Temperature",
        subtitle="%s" % (now.strftime("%d %b %Y"), ),
        axisbg="white",
    )
    mp.drawcounties()
    mp.plot_values(lons, lats, vals, fmt="%.0f", labels=labels, labelbuffer=5)
    pqstr = ("plot ac %s summary/iowa_asos_12z_low.png "
             "iowa_asos_12z_low.png png") % (now.strftime("%Y%m%d%H%M"), )
    mp.postprocess(pqstr=pqstr)
Exemplo n.º 39
0
def main():
    """Go Main!"""
    nt = NetworkTable("IACLIMATE")
    pgconn = get_dbconn('coop')

    df = read_sql("""
    with monthly as (
        select station, year, month, avg((high+low)/2.) from alldata_ia
        WHERE day < '2018-06-01' and high is not null
        GROUP by station, year, month),
    agg as (
        select station, year, month, avg,
        lag(avg) OVER (PARTITION by station ORDER by year ASC, month ASC)
        from monthly),
    agg2 as (
        select station, year, month, avg, lag, avg - lag as val,
        rank() OVER (PARTITION by station ORDER by avg - lag DESC)
        from agg WHERE lag is not null)
    select * from agg2 where rank = 1 ORDER by station
    """, pgconn, index_col='station')
    df['lat'] = 0.
    df['lon'] = 0.
    for station, _ in df.iterrows():
        if station in nt.sts and station != 'IA0000' and station[2] != 'C':
            df.at[station, 'lat'] = nt.sts[station]['lat']
            df.at[station, 'lon'] = nt.sts[station]['lon']

    mp = MapPlot(title="Largest Positive Change in Month to Month Average Temperature",
                 subtitle=('values in red set record for April to May 2018'), sector='state',
                 state='IA',
                 drawstates=True, continentalcolor='white')
    df2 = df[df['year'] == 2018]
    mp.plot_values(df2['lon'].values,
                   df2['lat'].values,
                   df2['val'].values, fmt='%.1f', textsize=12, labelbuffer=5,
                   color='r')
    df2 = df[df['year'] != 2018]
    mp.plot_values(df2['lon'].values,
                   df2['lat'].values,
                   df2['val'].values, fmt='%.1f', textsize=12, labelbuffer=5,
                   color='b')
    mp.drawcounties()
    mp.postprocess(filename='test.png')
    mp.close()
Exemplo n.º 40
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)
Exemplo n.º 41
0
def main(days, argv):
    """Go!"""
    today = datetime.date.today()
    routes = "ac"
    if len(argv) == 4:
        today = datetime.date(int(argv[1]), int(argv[2]), int(argv[3]))
        routes = "a"
    sixago = today - datetime.timedelta(days=(days - 1))

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

    # Compute normal from the climate database
    df = read_sql("""
        select s.id, ST_x(s.geom) as lon, ST_y(s.geom) as lat,
        sum(pday) as rainfall
        from summary c JOIN stations s on (c.iemid = s.iemid)
        WHERE day >= %s and day <= %s
        and s.network in ('AWOS', 'IA_ASOS')
        and pday >= 0 and pday < 30
        GROUP by s.id, lon, lat
    """,
                  pgconn,
                  params=(sixago, today),
                  index_col='id')
    df['label'] = df['rainfall'].apply(fmter)

    mp = MapPlot(title=('Iowa %s Day Precipitation Total [inch] (ASOS/AWOS)') %
                 (days, ),
                 subtitle=("%s - %s inclusive") %
                 (sixago.strftime("%d %b %Y"), today.strftime("%d %b %Y")),
                 continentalcolor='white')
    mp.plot_values(df['lon'].values,
                   df['lat'].values,
                   df['label'].values,
                   '%s',
                   labels=df.index.values,
                   labelbuffer=5)
    mp.drawcounties()
    pqstr = ("plot %s %s0000 summary/%sday/ia_precip.png "
             "summary/%sday/ia_precip.png png") % (
                 routes, today.strftime("%Y%m%d"), days, days)
    mp.postprocess(pqstr=pqstr)
    mp.close()
Exemplo n.º 42
0
def plot_precip(valid):
    """ Go Main Go

    Args:
      valid (datetime): The timestamp we are interested in!
    """
    pgconn = get_dbconn('iem', user='******')
    cursor = pgconn.cursor()

    cursor.execute(
        """SELECT pday, id, st_x(geom), st_y(geom)
    from summary s JOIN stations t on
    (t.iemid = s.iemid) WHERE s.day = %s
    and t.network in ('IA_COOP', 'NE_COOP', 'MO_COOP', 'IL_COOP', 'WI_COOP',
    'MN_COOP')
    and pday is not null and pday >= 0 and
    extract(hour from coop_valid) between 5 and 10""", (valid.date(), ))
    labels = []
    vals = []
    lats = []
    lons = []
    for row in cursor:
        labels.append(row[1])
        vals.append(pretty(row[0]))
        lats.append(row[3])
        lons.append(row[2])

    mp = MapPlot(title='%s NWS COOP 24 Hour Precipitation Reports [inch]' %
                 (valid.strftime("%-d %b %Y"), ),
                 subtitle='Reports valid between 6 and 9 AM',
                 axisbg='white',
                 figsize=(10.24, 7.68))
    mp.plot_values(lons, lats, vals, fmt='%s', labels=labels, labelcolor='tan')
    mp.drawcounties()

    pqstr = "plot ac %s0000 coopPrecPlot.gif coopPrecPlot.gif gif" % (
        valid.strftime("%Y%m%d"), )

    mp.postprocess(pqstr=pqstr)
    mp.close()

    pgconn.close()
Exemplo n.º 43
0
def plot_snowdepth(valid):
    """ Go Main Go

    Args:
      valid (datetime): The timestamp we are interested in!
    """
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor()

    cursor.execute("""SELECT snowd, id, st_x(geom), st_y(geom)
    from summary s JOIN stations t on
    (t.iemid = s.iemid) WHERE s.day = %s
    and t.network in ('IA_COOP', 'NE_COOP', 'MO_COOP', 'IL_COOP', 'WI_COOP',
    'MN_COOP')
    and snowd is not null and snowd >= 0 and
    extract(hour from coop_valid) between 5 and 10""", (valid.date(),))
    labels = []
    vals = []
    lats = []
    lons = []
    for row in cursor:
        labels.append(row[1])
        vals.append(pretty(row[0], precision=0))
        lats.append(row[3])
        lons.append(row[2])

    m = MapPlot(title='%s NWS COOP Snowfall Depth Reports [inch]' % (
                                            valid.strftime("%-d %b %Y"),),
                subtitle='Reports valid between 6 and 9 AM',
                axisbg='white', figsize=(10.24, 7.68))
    m.plot_values(lons, lats, vals, fmt='%s', labels=labels,
                  labelcolor='tan')
    m.drawcounties()

    pqstr = "plot ac %s0000 coopSnowDepth.gif coopSnowDepth.gif gif" % (
                                                    valid.strftime("%Y%m%d"),)

    m.postprocess(pqstr=pqstr)
    m.close()

    pgconn.close()
Exemplo n.º 44
0
def main():
    """Go Main Go."""
    nt = NetworkTable(['IA_ASOS', 'AWOS'])
    pgconn = get_dbconn('iem')
    cursor = pgconn.cursor()

    lats = []
    lons = []
    vals = []

    cursor.execute("""WITH today as (
        SELECT id, max_tmpf from summary_2017 s JOIN stations t
        ON (s.iemid = t.iemid) WHERE t.network in ('IA_ASOS', 'AWOS')
        and day = '2017-01-18'),
        agg as (
        SELECT t.id, max(day) from summary s, stations t, today t2
        WHERE s.iemid = t.iemid and t.id = t2.id and
        t.network in ('IA_ASOS', 'AWOS') and day > '2016-12-01'
        and day < '2017-01-18' and s.max_tmpf >= t2.max_tmpf
        GROUP by t.id)
        SELECT id, max from agg ORDER by max ASC
        """)

    colors = []
    for row in cursor:
        lats.append(nt.sts[row[0]]['lat'])
        lons.append(nt.sts[row[0]]['lon'])
        vals.append(row[1].strftime("%-m/%-d"))
        colors.append('r' if row[1] == datetime.date(2016, 12, 25) else 'k')

    m = MapPlot(continentalcolor='white',
                title=("Iowa ASOS/AWOS Last Date of as Warm of "
                       "Daily High as 18 Jan 2017"))
    m.plot_values(lons, lats, vals, fmt='%s', color=colors, labelbuffer=5)
    m.drawcounties()
    m.postprocess(filename='170119.png')
Exemplo n.º 45
0
def main():
    """Go"""
    pgconn = psycopg2.connect(database='iem', host='localhost', port=5555,
                              user='******')

    df = read_sql("""
        SELECT id, network, ST_x(geom) as lon,
        ST_y(geom) as lat, min(min_tmpf)
        from summary_2017 s JOIN stations t on (t.iemid = s.iemid)
        WHERE network IN ('IA_ASOS','AWOS') and min_tmpf > -50 and
        day > '2017-08-01' and id not in ('XXX')
        GROUP by id, network, lon, lat ORDER by min ASC
    """, pgconn, index_col=None)

    mp = MapPlot(title=r"2017 Fall Season Minimum Temperature $^\circ$F",
                 axisbg='white',
                 subtitle=('Automated Weather Stations ASOS/AWOS, '
                           'Valid Fall 2017 thru %s'
                           ) % (datetime.datetime.now().strftime("%d %b %Y"),))
    # m.contourf(lons, lats, vals, np.arange(-30,1,4))
    mp.plot_values(df['lon'].values, df['lat'].values, df['min'].values,
                   '%.0f', labels=df['id'].values, labelbuffer=3)
    mp.drawcounties()
    mp.postprocess(filename='test.png')
Exemplo n.º 46
0
import psycopg2
from pyiem.plot import MapPlot
IEM = psycopg2.connect(database='iem', host='iemdb', user='******')
cursor = IEM.cursor()

cursor.execute("""select station, st_x(geom), st_y(geom),
    precip_jun1 - precip_jun1_normal from
    cli_data c JOIN stations s on (s.id = c.station)
    WHERE s.network = 'NWSCLI' and c.valid = '2015-08-12' """)

lats = []
lons = []
colors = []
vals = []

for row in cursor:
    if row[3] is None or row[0] in ['KGFK', 'KRAP']:
        continue
    lats.append(row[2])
    lons.append(row[1])
    vals.append(row[3])
    colors.append('#ff0000' if row[3] < 0 else '#00ff00')


m = MapPlot(sector='midwest', axisbg='white',
            title='NWS 1 June - 12 August Precipitation Departure [inches]',
            subtitle='12 August 2015 based on NWS issued CLI Reports')
m.plot_values(lons, lats, vals, fmt='%.2f', textsize=16, color=colors)
m.postprocess(filename='150813.png')
Exemplo n.º 47
0
Arquivo: p97.py Projeto: 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
Exemplo n.º 48
0
hole = np.array(hole)

lons = []
lats = []
vals = []
for key in cnts.keys():
    if key[2] == 'C' or key == 'IA0000':
        continue
    
    lons.append( nt.sts[key]['lon'] )
    lats.append( nt.sts[key]['lat'] )
    vals.append( cnts[key] )

m = MapPlot(sector='iowa', title='10 Sept 2013 US Drought Monitor',
            subtitle=r"Count of 30 / 60 / 90 / 120 Day Standardized Precip Index below -1.2 (D2+) $\sigma$")
m.plot_values(lons, lats, vals, textsize=18)

colors = ['#f6eb13', '#ffcc66', '#ff9900', '#ff3333', '#FF00FF']
classes = ['D0: Abnormal', 'D1: Moderate', 'D2: Severe']
m.map.readshapefile('dm_current', 'dm', ax=m.ax)
for nshape, seg in enumerate(m.map.dm):
    cat = m.map.dm_info[nshape]['DM']
    poly=Polygon(seg, fc=colors[cat], ec='k', lw=.4, zorder=3)
    m.ax.add_patch(poly)

x,y = m.map(hole[:,0], hole[:,1])
poly=Polygon(zip(x,y), fc='w', ec='k', lw=.4, zorder=3)
m.ax.add_patch(poly)

for i in range(len(classes)):
    m.fig.text(0.9, 0.91 + ((i*2.5) /100.0), "%s" % (classes[i],),
Exemplo n.º 49
0
 GROUP by ugc_county ORDER by count DESC
""")

data = {}
for row in icursor:
    data[row[0]] = row[1]

# Query out centroids of counties...
pcursor.execute("""
    SELECT ugc, ST_x(ST_centroid(geom)) as lon,
    ST_y(ST_centroid(geom)) as lat
    from ugcs WHERE state = 'IA' and end_ts is null and
    substr(ugc,3,1) = 'C'
    """)
clons = []
clats = []
cvals = []
for row in pcursor:
    cvals.append(data.get(row[0], 0))
    clats.append(row[2])
    clons.append(row[1])


m = MapPlot(axisbg='white', title='Iowa CoCoRaHS Observers Per County',
            subtitle=("Sites with at least one report in past year "
                      "(Sep 2015-2016)"))
m.fill_ugcs(data, [1, 2, 3, 4, 5, 7, 10, 15, 20])
m.plot_values(clons, clats, cvals, labelbuffer=0)
m.drawcounties()
m.postprocess(filename='test.png')
Exemplo n.º 50
0
    sum(CASE when pday is null THEN 1 ELSE 0 END) as missing,
    ST_x(s.geom) as lon, ST_y(s.geom) as lat from summary_%s c JOIN stations s
    ON (s.iemid = c.iemid)
    WHERE s.network in ('IA_COOP') and s.iemid = c.iemid and
    day >= '%s' and day < '%s'
    GROUP by id, lat, lon""" % (now.year, day1.strftime("%Y-%m-%d"),
                                day2.strftime("%Y-%m-%d"))

lats = []
lons = []
precip = []
labels = []
icursor.execute(sql)
for row in icursor:
    if row[2] > (now.day / 3) or row[1] is None:
        continue

    sid = row[0]
    labels.append(sid)
    lats.append(row[4])
    lons.append(row[3])
    precip.append(row[1])


m = MapPlot(title="This Month's Precipitation [inch] (NWS COOP Network)",
            subtitle=now.strftime("%b %Y"), axisbg='white')
m.plot_values(lons, lats, precip, fmt='%.2f', labels=labels)
m.drawcounties()
pqstr = "plot c 000000000000 coopMonthPlot.png bogus png"
m.postprocess(view=False, pqstr=pqstr)
Exemplo n.º 51
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')
Exemplo n.º 52
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()
Exemplo n.º 53
0
import psycopg2
from pyiem.network import Table as NetworkTable
from pyiem.plot import MapPlot
nt = NetworkTable(['IA_ASOS', 'AWOS'])
pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
cursor = pgconn.cursor()

lats = []
lons = []
vals = []

cursor.execute("""SELECT id, max(day) from summary_2015 s JOIN stations t
 ON (t.iemid = s.iemid) WHERE t.network in ('IA_ASOS', 'AWOS')
 and min_tmpf < 31.5 and day < '2015-04-22' GROUP by id ORDER by max ASC""")

for row in cursor:
    lats.append(nt.sts[row[0]]['lat'])
    lons.append(nt.sts[row[0]]['lon'])
    vals.append(row[1].strftime("%-d %b"))

m = MapPlot(axisbg='white',
            title='Iowa ASOS/AWOS Last Date of Sub-Freezing Temperature',
            subtitle='thru 12 AM 22 April 2015')
m.plot_values(lons, lats, vals, fmt='%s')
m.drawcounties()
m.postprocess(filename='150422.png')
Exemplo n.º 54
0
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,
              labelbuffer=10)
m.drawcounties()
m.postprocess(pqstr=pqstr, view=False)
m.close()
Exemplo n.º 55
0
Arquivo: mapit.py Projeto: 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')
Exemplo n.º 56
0
from pyiem.plot import MapPlot
import datetime
import psycopg2
import numpy as np
IEM = psycopg2.connect(database='iem', host='iemdb', user='******')
icursor = IEM.cursor()

vals = []
lats = []
lons = []
valmask = []
icursor.execute("""SELECT id, network, ST_x(geom) as lon,
    ST_y(geom) as lat, min(min_tmpf)
    from summary_2015 s JOIN stations t on (t.iemid = s.iemid)
    WHERE network IN ('IA_ASOS','AWOS') and min_tmpf > -50 and
    day > '2012-08-01' and id not in ('XXX')
    GROUP by id, network, lon, lat ORDER by min ASC""")
for row in icursor:
    vals.append(row[4])
    lats.append(row[3])
    lons.append(row[2])
    valmask.append(row[1] in ['IA_ASOS', 'AWOS'])
    print row[4], row[0]

m = MapPlot(title="2014-2015 Winter Minimum Temperature $^\circ$F", axisbg='white',
            subtitle='Automated Weather Stations, Valid Fall 2014 - %s' % (datetime.datetime.now().strftime("%d %b %Y"),)
)
#m.contourf(lons, lats, vals, np.arange(-30,1,4))
m.plot_values(lons, lats, vals, '%.0f', valmask)
m.drawcounties()
m.postprocess(filename='150223.png')
Exemplo n.º 57
0
    nc = netCDF4.Dataset("data/hght12_%s.nc" % (row[0].strftime("%Y%m%d"),), 'r')
    idx = np.digitize([500.0], nc.variables['isobaric'][:])[0]
    mslp = nc.variables['Geopotential_height'][0,idx,:,:]
    mx = np.max(mslp[mslp >0])
    if total is None:
        total = mslp
        lats = nc.variables['lat'][:]
        lons = nc.variables['lon'][:]
    else:
        total += mslp
    mlons = lons[mslp==mx]
    mlats = lats[mslp==mx]
    if np.max(mlons) - np.min(mlons) < 10 and np.min(lats) - np.max(lats) < 10:
        xs.append( np.average(mlons) )
        ys.append( np.average(mlats) )
        xx.append('X')
    else:
        print mlons, mlats
        print np.max(mlons) - np.min(mlons), np.min(lats) - np.max(lats)
    nc.close()

m = MapPlot('conus', title='1979-2012 NCEP NARR Composite 500 hPa Geopotential Heights',
            subtitle='12 UTC analysis for %s days in August where Des Moines hit 100+$^\circ$F High' % (cursor.rowcount,))
m.pcolormesh(lons, lats, total / float(cursor.rowcount), np.arange(5650,5951,20),
             units='meters')
#x,y = m.map(xs,ys)
#print xx
m.plot_values(xs,ys, xx, '%s', textsize=16)
m.postprocess(filename='test.ps')
import iemplot
iemplot.makefeature('test')