Esempio n. 1
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. 2
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)
Esempio n. 3
0
def plot():
    """Do plotting work"""
    cmap = plt.get_cmap('inferno_r')
    # cmap.set_under('black')
    # cmap.set_over('red')
    minval = (np.load('minval.npy') * units.degK).to(units.degF)
    maxval = (np.load('maxval.npy') * units.degK).to(units.degF)
    diff = maxval - minval
    lons = np.load('lons.npy')
    lats = np.load('lats.npy')
    mp = MapPlot(sector='conus',
                 title=(r"Difference between warmest 3 Oct and coldest 4 "
                        "Oct 2m Temperature"),
                 subtitle=("based on hourly NCEP Real-Time Mesoscale Analysis "
                           "(RTMA) ending midnight CDT"))
    mp.ax.text(0.5, 0.97,
               (r"Pixel Difference Range: %.1f$^\circ$F to %.1f$^\circ$F, "
                r"Domain Analysis Range: %.1f$^\circ$F to %.1f$^\circ$F"
                ) % (np.min(diff).magnitude,
                     np.max(diff).magnitude,
                     np.min(minval).magnitude,
                     np.max(maxval).magnitude),
               transform=mp.ax.transAxes, fontsize=12, ha='center',
               bbox=dict(pad=0, color='white'), zorder=50)
    mp.pcolormesh(lons, lats, diff, range(0, 61, 5),
                  cmap=cmap, clip_on=False,
                  units=r"$^\circ$F")
    mp.postprocess(filename='test.png')
Esempio n. 4
0
def plot():
    """Do plotting work"""
    cmap1 = plt.get_cmap('inferno_r')
    colors = list(cmap1(np.arange(10) / 10.))
    cmap2 = plt.get_cmap('Pastel1')
    colors.extend(list(cmap2(np.arange(2) / 2.)))
    cmap = ListedColormap(colors)
    
    cmap.set_under('tan')
    cmap.set_over('white')
    minval = np.load('minval.npy')
    maxval = np.load('maxval.npy')
    diff = maxval - minval
    lons = np.load('lons.npy')
    lats = np.load('lats.npy')
    mp = MapPlot(sector='midwest', statebordercolor='white',
                 title=(r"Diff between coldest wind chill and warmest "
                        "air temp 29 Jan - 3 Feb 2019"),
                 subtitle=("based on hourly NCEP Real-Time Mesoscale Analysis "
                           "(RTMA) ending midnight CST"))

    levels = list(range(0, 101, 10))
    levels.extend([105, 110])
    mp.pcolormesh(lons, lats, diff, levels,
                  cmap=cmap, clip_on=False,
                  units=r"$^\circ$F", spacing='proportional')
    mp.postprocess(filename='test.png')
Esempio n. 5
0
def main():
    """Go Main"""
    pgconn = get_dbconn('postgis')
    df = read_postgis("""
    select geom, issue from sbw where wfo = 'PUB' and phenomena = 'TO'
    and significance = 'W' and status = 'NEW' and issue > '2007-10-01'
    and issue < '2019-01-01'
    """, pgconn, geom_col='geom', crs={'init': 'epsg:4326', 'no_defs': True})

    bounds = df['geom'].total_bounds
    # bounds = [-102.90293903,   40.08745967,  -97.75622311,   43.35172981]
    bbuf = 0.25
    mp = MapPlot(
        sector='custom', west=bounds[0] - bbuf,
        south=bounds[1] - bbuf,
        east=bounds[2] + bbuf, north=bounds[3] + bbuf,
        continentalcolor='white',  # '#b3242c',
        title='NWS Pueblo Issued Tornado Warnings [2008-2018]',
        subtitle='%s warnings plotted' % (len(df.index), ))
    crs_new = ccrs.Mercator()
    crs = ccrs.PlateCarree()
    new_geometries = [crs_new.project_geometry(ii, src_crs=crs)
                      for ii in df['geom'].values]
    # mp.draw_cwas()
    mp.ax.add_geometries(new_geometries, crs=crs_new, lw=0.5,
                         edgecolor='red', facecolor='None', alpha=1,
                         zorder=5)
    mp.drawcounties()
    mp.postprocess(filename='test.png')
Esempio n. 6
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')
Esempio n. 7
0
def run(ts, routes):
    """ Run for a given UTC timestamp """
    fn = ts.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/model/rtma/%H/rtma.t%Hz.awp2p5f000.grib2")
    if not os.path.isfile(fn):
        print "wind_power.py missing", fn
        return

    grb = pygrib.open(fn)
    u = grb.select(name="10 metre U wind component")[0]
    v = grb.select(name="10 metre V wind component")[0]
    mag = (u["values"] ** 2 + v["values"] ** 2) ** 0.5

    mag = (mag * 1.35) ** 3 * 0.002641
    # 0.002641

    lats, lons = u.latlons()
    lts = ts.astimezone(pytz.timezone("America/Chicago"))
    pqstr = "plot %s %s00 midwest/rtma_wind_power.png midwest/rtma_wind_power_%s00.png png" % (
        routes,
        ts.strftime("%Y%m%d%H"),
        ts.strftime("%H"),
    )
    m = MapPlot(
        sector="midwest",
        title=r"Wind Power Potential :: (speed_mps_10m * 1.35)$^3$ * 0.002641",
        subtitle="valid: %s based on NOAA Realtime Mesoscale Analysis" % (lts.strftime("%d %b %Y %I %p")),
    )
    m.pcolormesh(lons, lats, mag, numpy.array(levels), units="MW")

    m.postprocess(pqstr=pqstr)
Esempio n. 8
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')
Esempio n. 9
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)
Esempio n. 10
0
def compute(valid):
    ''' Get me files '''
    prob = None
    for hr in range(-15,0):
        ts = valid + datetime.timedelta(hours=hr)
        fn = ts.strftime("hrrr.ref.%Y%m%d%H00.grib2")
        if not os.path.isfile(fn):
            continue

        grbs = pygrib.open(fn)
        gs = grbs.select(level=1000,forecastTime=(-1 * hr * 60))
        ref = generic_filter(gs[0]['values'], np.max, size=10)
        if prob is None:
            lats, lons = gs[0].latlons()
            prob = np.zeros( np.shape(ref) )
        
        prob = np.where(ref > 29, prob+1, prob)

    prob = np.ma.array(prob / 15. * 100.)
    prob.mask = np.ma.where(prob < 1, True, False)    
    
    m = MapPlot(sector='iowa',
                title='HRRR Composite Forecast 4 PM 20 May 2014 30+ dbZ Reflectivity',
                subtitle='frequency of previous 15 model runs all valid at %s, ~15km smoothed' % (valid.astimezone(pytz.timezone("America/Chicago")).strftime("%-d %b %Y %I:%M %p %Z"),))

    m.pcolormesh(lons, lats, prob, np.arange(0,101,10), units='%',
                     clip_on=False)
    m.map.drawcounties()
    m.postprocess(filename='test.ps')
    m.close()
Esempio n. 11
0
def main():
    """Do Something"""
    cursor = IEM.cursor()
    data = []
    cursor.execute("""SELECT ST_x(geom), ST_y(geom), tsf0, tsf1, tsf2, tsf3,
    id, rwis_subf from current c JOIN stations t on (t.iemid = c.iemid)
    WHERE c.valid > now() - '1 hour'::interval""")
    for row in cursor:
        val = cln(row[2:6])
        if val is None:
            continue
        d = dict(lat=row[1], lon=row[0], tmpf=val, id=row[6])
        if row[7] is not None and not np.isnan(row[7]):
            d['dwpf'] = row[7]
        data.append(d)

    now = datetime.datetime.now()
    m = MapPlot(axisbg='white',
                title='Iowa RWIS Average Pavement + Sub-Surface Temperature',
                subtitle=("Valid: %s (pavement in red, sub-surface in blue)"
                          "") % (now.strftime("%-d %b %Y %-I:%M %p"),))
    m.plot_station(data)
    m.drawcounties()
    pqstr = ("plot c %s rwis_sf.png rwis_sf.png png"
             "") % (datetime.datetime.utcnow().strftime("%Y%m%d%H%M"), )
    m.postprocess(view=False, pqstr=pqstr)
Esempio n. 12
0
def main():
    """Go Main"""
    grbs = pygrib.open('ds.snow.bin')
    # skip 1-off first field
    total = None
    lats = lons = None
    for grb in grbs[1:]:
        if lats is None:
            lats, lons = grb.latlons()
            total = grb['values']
            continue
        total += grb['values']
    # TODO tz-hack here
    analtime = grb.analDate - datetime.timedelta(hours=5)

    mp = MapPlot(
        sector='custom', west=-100, east=-92, north=45, south=41,
        axisbg='tan',
        title=("NWS Forecasted Accumulated Snowfall "
               "thru 7 PM 12 April 2019"),
        subtitle='NDFD Forecast Issued %s' % (
            analtime.strftime("%-I %p %-d %B %Y"), )
    )
    cmap = nwssnow()
    cmap.set_bad('tan')
    mp.pcolormesh(
        lons, lats, total * 39.3701,
        [0.01, 1, 2, 3, 4, 6, 8, 12, 18, 24, 30, 36],
        cmap=cmap,
        units='inch')

    mp.drawcounties()
    mp.drawcities()
    mp.postprocess(filename='test.png')
    mp.close()
Esempio n. 13
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. 14
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')
Esempio n. 15
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)
Esempio n. 16
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. 17
0
def main():
    """Go MAin"""
    df = pd.read_csv('flood_emergencies.csv')
    df2 = df[['source', 'eventid', 'phenomena', 'significance', 'year']
             ].drop_duplicates()
    gdf = df2.groupby('source').count()
    vals = {}
    labels = {}
    for wfo, row in gdf.iterrows():
        if wfo == 'TJSJ':
            wfo = 'SJU'
        else:
            wfo = wfo[1:]
        vals[wfo] = int(row['eventid'])
        labels[wfo] = "%s" % (row['eventid'], )

    bins = list(range(0, 31, 3))
    bins[0] = 1.
    cmap = plt.get_cmap('plasma_r')
    cmap.set_over('black')
    cmap.set_under('white')
    mp = MapPlot(sector='nws', continentalcolor='white', figsize=(12., 9.),
                 title=("2003-2018 Flash Flood Emergency Events"),
                 subtitle=('based on unofficial IEM archives, searching '
                           '"FFS", "FLW", "FFS".'))
    mp.fill_cwas(vals, bins=bins, lblformat='%s', labels=labels,
                 cmap=cmap, ilabel=True,  # clevlabels=month_abbr[1:],
                 units='count')
    mp.postprocess(filename='test.png')
Esempio n. 18
0
def draw_map():
    """make maps, not war."""
    m = MapPlot(sector='conus',
                title='4 March 2019 :: DEP Precip Points')
    update_grid(m.ax)
    m.postprocess(filename='/tmp/map_clipoints.png')
    m.close()
Esempio n. 19
0
def make_plots(nc):
    ''' Generate some plots '''
    sts = compute_sts(nc)
    lats = nc.variables['lat'][:]
    lons = nc.variables['lon'][:]
    rts = (sts.astimezone(pytz.timezone("America/Chicago"))).strftime(
                                                            "%d %b %Y %H %p")
    for i, tm in enumerate(nc.variables['time'][:]):
        dt = sts + datetime.timedelta(minutes=float(tm))
        if dt.minute != 0:
            continue
        fhour = int( tm / 60.0 )
        fts = (dt.astimezone(pytz.timezone("America/Chicago"))).strftime(
                                                            "%d %b %Y %H %p")
        for pvar in PVARS:
            m = MapPlot(title='ISUMM5/Bridget Modelled %s' % (
                                                    PVARS[pvar]['title'],),
                        subtitle='Model Run: %s Forecast Valid: %s' % (rts, fts))
            vals = nc.variables[pvar][i,:,:]
            if pvar == 'bdeckt':
                vals = temperature(vals, 'K').value('F')
            m.pcolormesh(lons, lats, vals, PVARS[pvar]['levels'], units='mm')
            pqstr = "plot c %s model/frost/bridget/%02i/%s_%02i_f%03i.png bogus png" % (
                                        sts.strftime("%Y%m%d%H%M"), sts.hour,
                                        pvar, sts.hour, fhour)
            m.postprocess(pqstr=pqstr)
            m.close()
Esempio n. 20
0
def do_month(year, month, routes):
    """ Generate a MRMS plot for the month!"""

    sts = datetime.datetime(year,month,1)
    ets = sts + datetime.timedelta(days=35)
    ets = ets.replace(day=1)

    today = datetime.datetime.now()
    if ets > today:
        ets = today

    idx0 = iemre.daily_offset(sts)
    idx1 = iemre.daily_offset(ets)

    nc = netCDF4.Dataset("/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (year,),
                          'r')

    lats = nc.variables['lat'][:]
    lons = nc.variables['lon'][:]
    p01d = np.sum(nc.variables['p01d'][idx0:idx1,:,:],0) / 24.5
    nc.close()

    m = MapPlot(sector='iowa', title='MRMS %s - %s Total Precipitation' % (
            sts.strftime("%-d %b"), 
            (ets - datetime.timedelta(days=1)).strftime("%-d %b %Y")),
            subtitle='Data from NOAA MRMS Project')
    x,y = np.meshgrid(lons, lats)
    bins = [0.01, 0.1, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20]
    m.pcolormesh(x, y, p01d, bins, units='inches')
    m.drawcounties()
    currentfn = "summary/iowa_mrms_q3_month.png"
    archivefn = sts.strftime("%Y/%m/summary/iowa_mrms_q3_month.png")
    pqstr = "plot %s %s00 %s %s png" % (
                routes, sts.strftime("%Y%m%d%H"), currentfn, archivefn)
    m.postprocess(pqstr=pqstr)
Esempio n. 21
0
def main():
    """Go Main"""
    df = get_database_data()
    print(df)
    vals = {}
    labels = {}
    for wfo, row in df.iterrows():
        if wfo == 'JSJ':
            wfo = 'SJU'
        vals[wfo] = row['percent']
        labels[wfo] = '%.0f%%' % (row['percent'], )
        #if row['count'] == 0:
        #    labels[wfo] = '-'

    bins = np.arange(0, 101, 10)    
    #bins = [1, 25, 50, 75, 100, 125, 150, 200, 300]
    #bins = [-50, -25, -10, -5, 0, 5, 10, 25, 50]
    # bins[0] = 1
    #clevlabels = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N']
    cmap = plt.get_cmap('PuOr')
    mp = MapPlot(sector='nws', continentalcolor='white', figsize=(12., 9.),
                 title=("2018 Percentage of Time with 1+ Flood Warning Active"),
                 subtitle=('1 January - 30 September 2018, based on IEM archives'))
    mp.fill_cwas(vals, bins=bins, lblformat='%s', labels=labels,
                 cmap=cmap, ilabel=True,  # clevlabels=clevlabels,
                 units='percent')
    
    mp.postprocess(filename='test.png')
Esempio n. 22
0
def run(ts, routes):
    """ Run for a given UTC timestamp """
    fn = ts.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/rtma/%H/"
                      "rtma.t%Hz.awp2p5f000.grib2"))
    if not os.path.isfile(fn):
        print 'wind_power.py missing', fn
        return

    grb = pygrib.open(fn)
    try:
        u = grb.select(name='10 metre U wind component')[0]
        v = grb.select(name='10 metre V wind component')[0]
    except:
        print('Missing u/v wind for wind_power.py\nFN: %s' % (fn,))
        return
    mag = (u['values']**2 + v['values']**2)**.5

    mag = (mag * 1.35)**3 * 0.002641
    # 0.002641

    lats, lons = u.latlons()
    lts = ts.astimezone(pytz.timezone("America/Chicago"))
    pqstr = ("plot %s %s00 midwest/rtma_wind_power.png "
             "midwest/rtma_wind_power_%s00.png png"
             ) % (routes, ts.strftime("%Y%m%d%H"), ts.strftime("%H"))
    m = MapPlot(sector='midwest',
                title=(r'Wind Power Potential :: '
                       '(speed_mps_10m * 1.35)$^3$ * 0.002641'),
                subtitle=('valid: %s based on NOAA Realtime '
                          'Mesoscale Analysis'
                          ) % (lts.strftime("%d %b %Y %I %p")))
    m.pcolormesh(lons, lats, mag, numpy.array(levels), units='MW')

    m.postprocess(pqstr=pqstr)
Esempio n. 23
0
def main():
    """Go Main"""
    pgconn = get_dbconn('postgis')
    df = read_postgis("""
    select geom, issue from bot_warnings where wfo = 'PUB'
    """, pgconn, geom_col='geom', crs={'init': 'epsg:4326', 'no_defs': True})

    bounds = df['geom'].total_bounds
    # bounds = [-102.90293903,   40.08745967,  -97.75622311,   43.35172981]
    bbuf = 0.25
    mp = MapPlot(sector='custom', west=bounds[0] - bbuf,
                 south=bounds[1] - bbuf,
                 east=bounds[2] + bbuf, north=bounds[3] + bbuf,
                 continentalcolor='white',
                 title='Bot Issued Tornado Warnings [2008-2018] for PUB',
                 subtitle='%s warnings plotted' % (len(df.index), ))
    crs_new = ccrs.Mercator()
    crs = ccrs.PlateCarree()
    new_geometries = [crs_new.project_geometry(ii, src_crs=crs)
                      for ii in df['geom'].values]
    mp.draw_cwas()
    mp.ax.add_geometries(new_geometries, crs=crs_new,
                         edgecolor='r', facecolor='None', alpha=1., lw=0.5,
                         zorder=10)
    mp.postprocess(filename='test.png')
Esempio n. 24
0
def main():
    """Go Main Go"""
    cursor = POSTGIS.cursor()
    cursor2 = POSTGIS.cursor()

    phenomena = 'WS'

    cursor.execute("""
    SELECT ugc, issue, init_expire, wfo from warnings where phenomena = %s and
    significance = 'A' and issue > '2005-10-01' ORDER by issue ASC
    """, (phenomena, ))
    total = cursor.rowcount
    print('Events is %s' % (total, ))

    hits = {}
    hits2 = {}
    totals = {}
    misses = 0
    for row in tqdm(cursor, total=total):
        wfo = row[3]
        if wfo not in hits:
            hits[wfo] = {}
        if wfo not in totals:
            totals[wfo] = 0
        totals[wfo] += 1
        cursor2.execute("""
        SELECT distinct phenomena, significance from warnings
        where ugc = %s and expire > %s and issue < %s and wfo = %s
        """, (row[0], row[1], row[2], wfo))
        for row2 in cursor2:
            key = "%s.%s" % (row2[0], row2[1])
            if key not in hits[wfo]:
                hits[wfo][key] = 0
            hits[wfo][key] += 1
            if key not in hits2:
                hits2[key] = 0
            hits2[key] += 1
        if cursor2.rowcount == 0:
            misses += 1

    data = {}
    for wfo in hits:
        data[wfo] = hits[wfo].get(
                    '%s.W' % (phenomena,), 0) / float(totals[wfo]) * 100.0

    mp = MapPlot(sector='nws', axisbg='white',
                 title=("Conversion [%] of Winter Storm Watch "
                        "Counties/Parishes into Winter Storm Warnings"),
                 titlefontsize=14,
                 subtitle=('1 Oct 2005 - 29 Mar 2018, Overall %s/%s %.1f%%'
                           ) % (hits2['%s.W' % (phenomena, )], total,
                                hits2['%s.W' % (phenomena, )] / float(total) * 100.))
    mp.fill_cwas(data, ilabel=True, lblformat='%.0f')
    mp.postprocess(filename='test.png')

    print('Misses %s %.1f%%' % (misses, misses / float(total) * 100.0))
    for key in hits2:
        print('%s %s %.1f%%' % (key, hits2[key],
                                hits2[key] / float(total) * 100.0))
Esempio n. 25
0
def doday(ts, realtime):
    """
    Create a plot of precipitation stage4 estimates for some day

    We should total files from 1 AM to midnight local time
    """
    sts = ts.replace(hour=1)
    ets = sts + datetime.timedelta(hours=24)
    interval = datetime.timedelta(hours=1)
    now = sts
    total = None
    lts = None
    while now < ets:
        gmt = now.astimezone(pytz.timezone("UTC"))
        fn = gmt.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/"
                           +"stage4/ST4.%Y%m%d%H.01h.grib"))
        if os.path.isfile(fn):
            lts = now
            grbs = pygrib.open(fn)

            if total is None:
                g = grbs[1]
                total = g["values"]
                lats, lons = g.latlons()
            else:
                total += grbs[1]["values"]
            grbs.close()
        now += interval

    if lts is None and ts.hour > 1:
        print 'stage4_today_total.py found no data!'
    if lts is None:
        return
    lts = lts - datetime.timedelta(minutes=1)
    subtitle = "Total between 12:00 AM and %s" % (lts.strftime("%I:%M %p %Z"),)
    routes = 'ac'
    if not realtime:
        routes = 'a'
    for sector in ['iowa', 'midwest', 'conus']:
        pqstr = "plot %s %s00 %s_stage4_1d.png %s_stage4_1d.png png" % (routes,
                ts.strftime("%Y%m%d%H"), sector, sector )
        
        m = MapPlot(sector=sector,
                    title="%s NCEP Stage IV Today's Precipitation" % (
                                                    ts.strftime("%-d %b %Y"),),
                    subtitle=subtitle)
            
        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
    
        m.pcolormesh(lons, lats, total / 24.5, clevs, units='inch')
    
        #map.drawstates(zorder=2)
        if sector == 'iowa':
            m.drawcounties()
        m.postprocess(pqstr=pqstr)
        m.close()
Esempio n. 26
0
def main():
    """GO!"""
    pgconn = get_dbconn('idep')
    cursor = pgconn.cursor()

    scenario = int(sys.argv[1])
    mp = MapPlot(sector='iowa', axisbg='white', nologo=True,
                 subtitle='1 Jan 2014 thru 31 Dec 2014',
                 caption='Daily Erosion Project',
                 title=('Harvest Index 0.8 Change in 2014 Soil Delivery '
                        'from Baseline'))

    cursor.execute("""
    with baseline as (
        SELECT huc_12, sum(avg_delivery) * 4.463 as loss from results_by_huc12
        where valid between '2014-01-01' and '2015-01-01' and
        scenario = 0 GROUP by huc_12),
    scenario as (
        SELECT huc_12, sum(avg_delivery) * 4.463 as loss from results_by_huc12
        where valid between '2014-01-01' and '2015-01-01' and
        scenario = %s GROUP by huc_12),
    agg as (
        SELECT b.huc_12, b.loss as baseline_loss, s.loss as scenario_loss from
        baseline b LEFT JOIN scenario s on (b.huc_12 = s.huc_12))

     SELECT ST_Transform(simple_geom, 4326),
     (scenario_loss  - baseline_loss) / 1.0 as val, i.huc_12
     from huc12 i JOIN agg d on (d.huc_12 = i.huc_12)
     WHERE i.states ~* 'IA' ORDER by val DESC

    """, (scenario, ))

    # bins = np.arange(0, 101, 10)
    bins = [-5, -2, -1, -0.5, 0, 0.5, 1, 2, 5]
    cmap = plt.get_cmap("BrBG_r")
    cmap.set_under('purple')
    cmap.set_over('black')
    norm = mpcolors.BoundaryNorm(bins, cmap.N)

    for row in cursor:
        # print "%s,%s" % (row[2], row[1])
        polygon = loads(row[0].decode('hex'))
        arr = np.asarray(polygon.exterior)
        points = mp.ax.projection.transform_points(ccrs.Geodetic(),
                                                   arr[:, 0], arr[:, 1])
        val = float(row[1])
        # We have very small negative numbers that should just be near a
        # positive zero
        if val < 0 and val > -0.1:
            val = 0.001
        color = cmap(norm([val, ]))[0]
        poly = Polygon(points[:, :2], fc=color, ec='k', zorder=2, lw=0.1)
        mp.ax.add_patch(poly)

    mp.draw_colorbar(bins, cmap, norm, units='T/a/yr')

    mp.drawcounties()
    mp.postprocess(filename='test.png')
Esempio n. 27
0
def do(ts, hours):
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    ts = ts.replace(minute=0)
    sts = ts - datetime.timedelta(hours=hours)
    ets = ts 
    interval = datetime.timedelta(hours=1)
    now = sts
    total = None
    lts = None
    while now < ets:
        fn = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.01h.grib" % (
            now.strftime("%Y/%m/%d"), 
            now.strftime("%Y%m%d%H") )

        if os.path.isfile(fn):
            lts = now
            grbs = pygrib.open(fn)

            if total is None:
                g = grbs[1]
                total = g["values"]
                lats, lons = g.latlons()
            else:
                total += grbs[1]["values"]
            grbs.close()
        now += interval
    
    if lts is None and ts.hour > 1:
        print 'Missing StageIV data!'
    if lts is None:
        return
    
    cmap = cm.get_cmap("jet")
    cmap.set_under('white')
    cmap.set_over('black')
    clevs = [0.01,0.1,0.25,0.5,1,2,3,5,8,9.9]
    localtime = (ts - datetime.timedelta(minutes=1)).astimezone(
                                        pytz.timezone("America/Chicago"))

    for sector in ['iowa', 'midwest', 'conus']:
        m = MapPlot(sector=sector,
                    title='NCEP Stage IV %s Hour Precipitation' % (hours,),
                    subtitle='Total up to %s' % (
                                    localtime.strftime("%d %B %Y %I %p %Z"),))
        m.pcolormesh(lons, lats, total / 24.5, clevs, units='inch')
        pqstr = "plot %s %s00 %s_stage4_%sh.png %s_stage4_%sh_%s.png png" % (
                                'ac', ts.strftime("%Y%m%d%H"), sector, hours,
                                sector, hours, ts.strftime("%H"))
        if sector == 'iowa':
            m.drawcounties()
        m.postprocess(pqstr=pqstr)
        m.close()
Esempio n. 28
0
def run( bcsdfn, bccafn, title ):
    ''' Run for a given filename! '''
    nc = netCDF4.Dataset(bcsdfn, 'r')
    # time
    idx0, idx1 = get_time( nc )
    # somewhat a hack for now
    dmul = 1.0
    if (idx1 - idx0) / 19.0 < 13:
        dmul = 365.0 / 12.0
    # Either pr or Prcp
    pvar = 'pr' if nc.variables.has_key('pr') else 'Prcp'
    pmul = 1.0 if nc.variables[pvar].units == 'mm/d' else 86400.0
    bcsdprecip = np.sum(nc.variables[pvar][idx0:idx1+1,:,:],0) * dmul * pmul / 19.0 / 24.5
    
    nc2 = netCDF4.Dataset(bccafn, 'r')
    # time
    idx0, idx1 = get_time( nc2 )
    # somewhat a hack for now
    dmul = 1.0
    if (idx1 - idx0) / 19.0 < 13:
        dmul = 365.0 / 12.0
    # Either pr or Prcp
    pvar = 'pr' if nc2.variables.has_key('pr') else 'Prcp'
    pmul = 1.0 if nc2.variables[pvar].units == 'mm/d' else 86400.0
    bccaprecip = np.sum(nc2.variables[pvar][idx0:idx1+1,:,:],0) * dmul * pmul / 19.0 / 24.5
    
    
    # lat or latitude
    latvar = 'lat' if nc.variables.has_key('lat') else 'latitude'
    lats = nc.variables[latvar][:]
    lats = np.concatenate([lats, [lats[-1]+(lats[-1]-lats[-2])]])
    # lon or longitude 
    lonvar = 'lon' if nc.variables.has_key('lon') else 'longitude'
    lons = nc.variables[lonvar][:]
    lons = np.concatenate([lons, [lons[-1]+(lons[-1]-lons[-2])]])
    
    print np.shape(lons), np.shape(lats), np.max(bccaprecip), np.max(bcsdprecip)

    title = '81-99 Precip BCCA over BCSD %s' % (title,)
    #subtitle = 'filename: %s' % (bccafn,)
    m = MapPlot(title=title, subtitle='',
                sector='conus', nologo=True, caption='')
    x,y = np.meshgrid(lons, lats)
    cmap = cm.get_cmap('Spectral')
    cmap.set_over('black')
    m.pcolormesh(x, y, bccaprecip  / bcsdprecip * 100.0, 
                 np.arange(0,201,20),
                 cmap=cmap, units='percentage')
    png = '../cplots/%s.png' % (title.replace(" ", "_").lower(),)
    print png
    m.postprocess(filename=png)
    nc.close()
    nc2.close()
Esempio n. 29
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. 30
0
 def draw(self):
     ''' For debugging, draw the polygons!'''
     from descartes.patch import PolygonPatch
     from pyiem.plot import MapPlot
     for sig in self.sigmets:
         m = MapPlot(sector='conus')
         x, y = m.map(sig.geom.exterior.xy[0], sig.geom.exterior.xy[1])
         patch = PolygonPatch(Polygon(zip(x, y)), fc='r', label='Outlook')
         m.ax.add_patch(patch)
         fn = '/tmp/%s.png' % (sig.label,)
         print ':: creating plot %s' % (fn,)
         m.postprocess(filename=fn)
         m.close()
Esempio n. 31
0
ccursor = COOP.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()

m = MapPlot(axisbg='white',
            title="Iowa %s SDD Accumulation" % (now.strftime("%B %Y"), ))
m.contourf(lons, lats, sdd86, range(int(min(sdd86) - 1), int(max(sdd86) + 1)))
pqstr = "plot c 000000000000 summary/sdd_mon.png bogus png"
m.postprocess(view=False, pqstr=pqstr)
m.close()
Esempio n. 32
0
 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)

m = MapPlot(title='Iowa ASOS/AWOS 12Z Morning Low Temperature',
            subtitle="%s" % (now.strftime("%d %b %Y"), ),
            axisbg='white')
m.drawcounties()
m.plot_values(lons, lats, vals, fmt='%.0f', labels=labels)
pqstr = "plot ac %s summary/iowa_asos_12z_low.png iowa_asos_12z_low.png png" % (
    now.strftime("%Y%m%d%H%M"), )
m.postprocess(pqstr=pqstr)
Esempio n. 33
0
labels = {}
uniq = []
for line in text.split("\n"):
    tokens = line.replace(" ", "").split("|")
    wfo = tokens[0][1:]
    if tokens[0][0] == 'P':
        wfo = tokens[0]
    key = "%s" % (tokens[1], )
    if not nt.sts.has_key(wfo):
        continue
    # P
    wfo = tokens[0][1:]
    if not key in uniq:
        uniq.append(key)
    data[wfo] = len(uniq) - 1
    labels[wfo] = key
    if wfo == 'JSJ':
        labels['SJU'] = labels['JSJ']

bins = range(len(uniq) + 1)
uniq.append('')

p = MapPlot(sector='nws',
            axisbg='white',
            title="2009-2013 Most Frequently issued non-SHEF 3char AWIPS ID",
            subtitle='RR* products were excluded from this analysis')
p.fill_cwas(data, bins=bins, labels=labels, lblformat='%s', clevlabels=uniq)
p.postprocess(filename='test.png')
#import iemplot
#iemplot.makefeature('test')
Esempio n. 34
0
def two(year):
    """Compare yearly totals in a scatter plot"""
    coop = psycopg2.connect(database='coop',
                            host='localhost',
                            port=5555,
                            user='******')
    ccursor = coop.cursor()
    idep = psycopg2.connect(database='idep',
                            host='localhost',
                            port=5555,
                            user='******')
    icursor = idep.cursor()

    ccursor.execute(
        """
        SELECT station, sum(precip) from alldata_ia
        WHERE year = %s and station != 'IA0000'
        and substr(station, 3, 1) != 'C' GROUP by station ORDER by station ASC
    """, (year, ))
    nt = NetworkTable("IACLIMATE")
    rows = []
    for row in ccursor:
        station = row[0]
        precip = row[1]
        if station not in nt.sts:
            continue
        lon = nt.sts[station]['lon']
        lat = nt.sts[station]['lat']
        icursor.execute(
            """
            select huc_12 from huc12
            where ST_Contains(geom,
            ST_Transform(ST_SetSRID(ST_Point(%s, %s), 4326), 5070))
            and scenario = 0
        """, (lon, lat))
        if icursor.rowcount == 0:
            continue
        huc12 = icursor.fetchone()[0]
        icursor.execute(
            """
        select sum(qc_precip) from results_by_huc12
        WHERE valid between %s and %s and huc_12 = %s and scenario = 0
        """, (datetime.date(year, 1, 1), datetime.date(year, 12, 31), huc12))
        val = icursor.fetchone()[0]
        if val is None:
            continue
        iprecip = distance(val, 'MM').value('IN')
        rows.append(
            dict(station=station,
                 precip=precip,
                 iprecip=iprecip,
                 lat=lat,
                 lon=lon))
        # print("%s %s %5.2f %5.2f" % (station, huc12, precip, iprecip))
    df = pd.DataFrame(rows)
    df['diff'] = df['iprecip'] - df['precip']
    bias = df['diff'].mean()
    print("%s %5.2f %5.2f %5.2f" %
          (year, df['iprecip'].mean(), df['precip'].mean(), bias))
    m = MapPlot(title=("%s IDEP Precipitation minus IEM Climodat (inch)") %
                (year, ),
                subtitle=("HUC12 Average minus point observation, "
                          "Overall bias: %.2f") % (bias, ),
                axisbg='white')
    m.plot_values(df['lon'], df['lat'], df['diff'], fmt='%.2f', labelbuffer=1)
    m.postprocess(filename='%s_map.png' % (year, ))
    m.close()

    (fig, ax) = plt.subplots(1, 1)
    ax.scatter(df['precip'], df['iprecip'])
    ax.grid(True)
    ylim = ax.get_ylim()
    ax.plot([ylim[0], ylim[1]], [ylim[0], ylim[1]], lw=2)
    ax.set_xlabel("IEM Climodat Precip")
    ax.set_ylabel("IDEP HUC12 Precip")
    ax.set_title("%s Precipitation Comparison, bias=%.2f" % (year, bias))
    fig.savefig('%s_xy.png' % (year, ))
    plt.close()
Esempio n. 35
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. 36
0
    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 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 12 March')
m.contourf(lons, lats, vals, range(27, 78, 3), units='days')
m.postprocess(filename='160314.png')
Esempio n. 37
0
def main():
    """Go Main Go"""
    # Run for a period of 121 days
    ets = datetime.datetime.now() - datetime.timedelta(days=1)
    sts = ets - datetime.timedelta(days=121)

    # Get the normal accumm
    cnc = ncopen(iemre.get_dailyc_ncname(), "r")
    lons = cnc.variables["lon"][:]
    lats = cnc.variables["lat"][:]
    index0 = iemre.daily_offset(sts)
    index1 = iemre.daily_offset(ets)
    if index1 < index0:  # Uh oh, we are spanning a year
        clprecip = np.sum(cnc.variables["p01d"][:index1, :, :], 0)
        clprecip = clprecip + np.sum(cnc.variables["p01d"][index0:, :, :], 0)
    else:
        clprecip = np.sum(cnc.variables["p01d"][index0:index1, :, :], 0)
    cnc.close()

    # Get the observed precip
    if sts.year != ets.year:  # spanner, darn
        onc = ncopen(iemre.get_daily_ncname(sts.year))
        obprecip = np.sum(onc.variables["p01d"][index0:, :, :], 0)
        onc.close()
        onc = ncopen(iemre.get_daily_ncname(ets.year))
        obprecip = obprecip + np.sum(onc.variables["p01d"][:index1, :, :], 0)
        onc.close()
    else:
        ncfn = iemre.get_daily_ncname(sts.year)
        onc = ncopen(ncfn, "r")
        obprecip = np.sum(onc.variables["p01d"][index0:index1, :, :], 0)
        onc.close()

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

    # Plot departure from normal
    mp = MapPlot(
        sector="midwest",
        title=("Precipitation Departure %s - %s") %
        (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")),
        subtitle="based on IEM Estimates",
    )

    mp.pcolormesh(lons, lats, (obprecip - clprecip) / 25.4,
                  np.arange(-10, 10, 1))
    mp.postprocess(pqstr="plot c 000000000000 summary/4mon_diff.png bogus png")
    mp.close()

    # Plot normals
    mp = MapPlot(
        sector="midwest",
        title=("Normal Precipitation:: %s - %s") %
        (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")),
        subtitle="based on IEM Estimates",
    )

    mp.pcolormesh(lons, lats, (clprecip) / 25.4, np.arange(0, 30, 2))
    mp.postprocess(
        pqstr="plot c 000000000000 summary/4mon_normals.png bogus png")
    mp.close()

    # Plot Obs
    mp = MapPlot(
        sector="midwest",
        title=("Estimated Precipitation:: %s - %s") %
        (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")),
        subtitle="based on IEM Estimates",
    )

    mp.pcolormesh(lons, lats, (obprecip) / 25.4, np.arange(0, 30, 2))
    mp.postprocess(
        pqstr="plot c 000000000000 summary/4mon_stage4obs.png bogus png")
    mp.close()
Esempio n. 38
0
def main():
    """Go Main Go"""
    st = NetworkTable("IACLIMATE")
    pgconn = get_dbconn("coop", user="******")
    ccursor = pgconn.cursor()

    ts = datetime.datetime.now() - datetime.timedelta(days=1)

    nrain = []
    lats = []
    lons = []

    # Get normals!
    ccursor.execute("""SELECT station, sum(precip) as acc from climate51
        WHERE valid <= '2000-%s' and station NOT IN ('IA7842','IA4381')
        and substr(station,0,3) = 'IA'
        GROUP by station ORDER by acc ASC""" % (ts.strftime("%m-%d"), ))
    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"])

    mp = 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)
    mp.contourf(lons, lats, nrain, rng, units="inch")
    pqstr = "plot c 000000000000 summary/year/normals.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.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'
         GROUP by station) as obs
      WHERE obs.station = climate.station""" % (ts.strftime("%m-%d"), ts.year))
    for row in ccursor:
        station = row[0]
        if station not in st.sts:
            continue
        nrain.append(row[2] - row[1])
        lats.append(st.sts[station]["lat"])
        lons.append(st.sts[station]["lon"])

    mp = MapPlot(
        axisbg="white",
        title=("Iowa %s Precipitation Depature") % (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)
    mp.contourf(lons, lats, nrain, rng, units="inch")
    pqstr = "plot c 000000000000 summary/year/diff.png bogus png"
    mp.postprocess(view=False, pqstr=pqstr)
    mp.close()
Esempio n. 39
0
    except:
        continue

H2, xedges, yedges = np.histogram2d(y,
                                    x,
                                    bins=(50, 100),
                                    range=[[25, 50], [-130, -60]])
H22, xedges, yedges = np.histogram2d(y2,
                                     x2,
                                     bins=(50, 100),
                                     range=[[25, 50], [-130, -60]])

m = MapPlot(sector='conus',
            title='Heat Map of Change in Visitors %s(n=%s) minus %s(n=%s)' %
            (year, len(x), year2, len(x2)),
            subtitle='from visit_history_093013_st12.csv',
            nologo=True)
x, y = np.meshgrid(yedges, xedges)

#levels = [1,2,5,7,10,15,20,25,30,40,50,60,70,80,90,100,200]
levels = np.arange(-60, 61, 5)
H3 = ma.array(H2 - H22)
H3.mask = np.where(H2 < 1, True, False)

cmap = cm.get_cmap('jet')
cmap.set_under('black')
cmap.set_over('black')
m.pcolormesh(x, y, H3, levels, cmap=cmap, units='count')
#m.drawcounties()
m.postprocess(filename='conus_heatmap_%s_%s.png' % (year, year2))
Esempio n. 40
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"), )))
    if cdf['val'].max() > 0:
        m.contourf(cdf['lon'].values,
                   cdf['lat'].values,
                   cdf['val'].values,
                   rng,
                   cmap=cmap)
    m.drawcounties()
    if len(tdf.index) > 0:
        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"), )))
    if cdf['val'].max() > 0:
        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"), )))
    if cdf['val'].max() > 0:
        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. 41
0
def main():
    """Go Main Go"""
    pgconn = get_dbconn('coop')
    ccursor = pgconn.cursor()

    form = cgi.FieldStorage()
    if ("year1" in form and "year2" in form and "month1" in form
            and "month2" in form and "day1" in form and "day2" in form):
        sts = datetime.datetime(int(form["year1"].value),
                                int(form["month1"].value),
                                int(form["day1"].value))
        ets = datetime.datetime(int(form["year2"].value),
                                int(form["month2"].value),
                                int(form["day2"].value))
    else:
        sts = datetime.datetime(2011, 5, 1)
        ets = datetime.datetime(2011, 10, 1)
    baseV = 50
    if "base" in form:
        baseV = int(form["base"].value)
    maxV = 86
    if "max" in form:
        maxV = int(form["max"].value)

    # Make sure we aren't in the future
    now = datetime.datetime.today()
    if ets > now:
        ets = now

    st = NetworkTable("IACLIMATE")
    # Compute normal from the climate database
    sql = """
        SELECT station,
        sum(gddXX(%s, %s, high, low)) as gdd, count(*)
        from alldata_ia WHERE year = %s and day >= '%s' and day < '%s'
        and substr(station, 2, 1) != 'C' and station != 'IA0000'
        GROUP by station
    """ % (baseV, maxV, sts.year, sts.strftime("%Y-%m-%d"),
           ets.strftime("%Y-%m-%d"))

    lats = []
    lons = []
    gdd50 = []
    valmask = []
    ccursor.execute(sql)
    total_days = (ets - sts).days
    for row in ccursor:
        sid = row[0]
        if sid not in st.sts:
            continue
        if row[2] < (total_days * 0.9):
            continue
        lats.append(st.sts[sid]['lat'])
        lons.append(st.sts[sid]['lon'])
        gdd50.append(float(row[1]))
        valmask.append(True)

    m = MapPlot(
        title=("Iowa %s thru %s GDD(base=%s,max=%s) Accumulation"
               "") %
        (sts.strftime("%Y: %d %b"),
         (ets - datetime.timedelta(days=1)).strftime("%d %b"), baseV, maxV),
        axisbg='white')
    m.contourf(lons, lats, gdd50, range(int(min(gdd50)), int(max(gdd50)), 25))
    m.plot_values(lons, lats, gdd50, fmt='%.0f')
    m.drawcounties()
    m.postprocess(web=True)
    m.close()
Esempio n. 42
0
def runYear(year):
    """Hack"""
    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)
    sql = """SELECT station, avg(high) as avg_high, avg(low) as avg_low,
           avg( (high+low)/2 ) as avg_tmp, max(day)
           from alldata_ia WHERE year = %s and station != 'IA0000' and
           high is not Null and low is not Null and substr(station,3,1) != 'C'
           GROUP by station""" % (year, )
    ccursor.execute(sql)
    # Plot Average Highs
    lats = []
    lons = []
    vals = []
    labels = []
    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['avg_high'])
        maxday = row['max']

    # ---------- Plot the points
    mp = MapPlot(title="Average Daily High Temperature [F] (%s)" % (year, ),
                 subtitle='1 January - %s' % (maxday.strftime("%d %B"), ),
                 axisbg='white')
    mp.plot_values(lons,
                   lats,
                   vals,
                   labels=labels,
                   labeltextsize=8,
                   labelcolor='tan',
                   fmt='%.1f')
    pqstr = "plot m %s bogus %s/summary/avg_high.png png" % (
        now.strftime("%Y%m%d%H%M"),
        year,
    )
    mp.postprocess(pqstr=pqstr)
    mp.close()

    # Plot Average Lows
    lats = []
    lons = []
    vals = []
    labels = []
    ccursor.execute(sql)
    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['avg_low'])

    # ---------- Plot the points
    mp = MapPlot(title="Average Daily Low Temperature [F] (%s)" % (year, ),
                 subtitle='1 January - %s' % (maxday.strftime("%d %B"), ),
                 axisbg='white')
    mp.plot_values(lons,
                   lats,
                   vals,
                   labels=labels,
                   labeltextsize=8,
                   labelcolor='tan',
                   fmt='%.1f')
    pqstr = "plot m %s bogus %s/summary/avg_low.png png" % (
        now.strftime("%Y%m%d%H%M"),
        year,
    )
    mp.postprocess(pqstr=pqstr)
    mp.close()

    # Plot Average Highs
    lats = []
    lons = []
    vals = []
    labels = []
    ccursor.execute(sql)
    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['avg_tmp'])

    # ---------- Plot the points
    mp = MapPlot(title="Average Daily Temperature [F] (%s)" % (year, ),
                 subtitle='1 January - %s' % (maxday.strftime("%d %B"), ),
                 axisbg='white')
    mp.plot_values(lons,
                   lats,
                   vals,
                   labels=labels,
                   labeltextsize=8,
                   labelcolor='tan',
                   fmt='%.1f')
    pqstr = "plot m %s bogus %s/summary/avg_temp.png png" % (
        now.strftime("%Y%m%d%H%M"),
        year,
    )
    mp.postprocess(pqstr=pqstr)
    mp.close()
Esempio n. 43
0
    SELECT station, valid, avg from ranks WHERE rank = 1
""")
lats = []
lons = []
vals = []
for row in ccursor:
    station = row[0]
    if station not in nt.sts:
        continue
    lats.append(nt.sts[station]['lat'])
    lons.append(nt.sts[station]['lon'])
    vals.append(int(row[1].strftime("%j")))

labels = []
sts = datetime.datetime(2000, 1, 1)
ticks = range(47, 74, 2)
for i in ticks:
    ts = sts + datetime.timedelta(days=i)
    labels.append(ts.strftime("%b %d"))

m = MapPlot(
    sector='conus',
    axisbg='white',
    title=('End Date of Coldest 91 Day Period '
           '(End of Winter, Start of Spring)'),
    subtitle='based on NCDC 1981-2010 Climatology of Average(high+low)')
m.contourf(lons, lats, vals, ticks, clevlabels=labels)
m.postprocess(filename='150225.png')
m.close()
Esempio n. 44
0
def main():
    """Go Main Go"""
    pgconn = get_dbconn("iem", user="******")
    now = utc().replace(hour=0, minute=0, second=0, microsecond=0)

    df = read_sql(
        """
    WITH lows as (
     SELECT c.iemid,
     min(tmpf) as calc_low,
     min(min_tmpf_6hr) as reported_low
     from current_log c JOIN stations s
     ON (s.iemid = c.iemid)
     WHERE valid > %s and valid < %s
     and (s.network ~* 'ASOS' or s.network = 'AWOS')
     and s.country = 'US' and s.state not in ('HI', 'AK') GROUP by c.iemid
    )

    select t.id, t.state, ST_x(t.geom) as lon, ST_y(t.geom) as lat,
    least(l.calc_low, l.reported_low) as low from
    lows l JOIN stations t on (t.iemid = l.iemid)
    """,
        pgconn,
        params=(now, now.replace(hour=12)),
        index_col="id",
    )
    df = df[df["low"].notnull()]
    LOG.debug("found %s observations for %s", len(df.index), now)

    for sector in [
            "iowa",
            "midwest",
            "conus",
            "SD",
            "NE",
            "ND",
            "KS",
            "MN",
            "MO",
            "WI",
            "IL",
    ]:
        mp = MapPlot(
            sector=sector if len(sector) > 2 else "state",
            state=sector if len(sector) == 2 else "IA",
            title="%s ASOS/AWOS 01-12 UTC Low Temperature" %
            (now.strftime("%-d %b %Y"), ),
            subtitle=("includes available 6z and 12z 6-hr mins, "
                      "does not include 0z observation"),
            axisbg="white",
        )
        if sector == "iowa" or len(sector) == 2:
            df2 = df[df["state"] == ("IA" if len(sector) > 2 else sector)]
            labels = df2.index.values
            mp.drawcounties()
            size = 14
        else:
            df2 = df
            labels = None
            size = 10
        mp.plot_values(
            df2["lon"].values,
            df2["lat"].values,
            df2["low"],
            fmt="%.0f",
            labels=labels,
            labelbuffer=1,
            textsize=size,
        )
        pqstr = ("plot ac %s summary/%s_asos_12z_low.png "
                 "%s_asos_12z_low.png png") % (now.strftime("%Y%m%d%H%M"),
                                               sector.lower(), sector.lower())
        LOG.debug(pqstr)
        mp.postprocess(pqstr=pqstr)
        mp.close()
Esempio n. 45
0
def main(argv):
    """Go Main Go"""
    nt = Table("ISUSM")
    qdict = loadqc()

    idbconn = get_dbconn('isuag', user='******')
    pdbconn = get_dbconn('postgis', user='******')

    day_ago = int(argv[1])
    ts = datetime.date.today() - datetime.timedelta(days=day_ago)
    hlons, hlats, hvals = do_nam(ts)
    nam = temperature(hvals, 'K').value('F')
    window = np.ones((3, 3))
    nam = convolve2d(nam, window / window.sum(), mode='same', boundary='symm')

    # mp = MapPlot(sector='midwest')
    # mp.pcolormesh(hlons, hlats, nam,
    #              range(20, 90, 5))
    # mp.postprocess(filename='test.png')
    # sys.exit()

    # Query out the data
    df = read_sql("""
        WITH ranges as (
            select station, count(*), min(tsoil_c_avg_qc),
            max(tsoil_c_avg_qc) from sm_hourly WHERE
            valid >= %s and valid < %s GROUP by station
        )
        SELECT d.station, d.tsoil_c_avg_qc,
        r.max as hourly_max_c, r.min as hourly_min_c, r.count
         from sm_daily d JOIN ranges r on (d.station = r.station)
        where valid = %s and tsoil_c_avg_qc > -40 and r.count > 19
    """,
                  idbconn,
                  params=(ts, ts + datetime.timedelta(days=1), ts),
                  index_col='station')
    for col, newcol in zip(['tsoil_c_avg_qc', 'hourly_min_c', 'hourly_max_c'],
                           ['ob', 'min', 'max']):
        df[newcol] = temperature(df[col].values, 'C').value('F')
        df.drop(col, axis=1, inplace=True)

    for stid, row in df.iterrows():
        df.at[stid, 'ticket'] = qdict.get(stid, {}).get('soil4', False)
        x, y = get_idx(hlons, hlats, nt.sts[stid]['lon'], nt.sts[stid]['lat'])
        df.at[stid, 'nam'] = nam[x, y]
        df.at[stid, 'lat'] = nt.sts[stid]['lat']
        df.at[stid, 'lon'] = nt.sts[stid]['lon']
    # ticket is an object type from above
    df = df[~df['ticket'].astype('bool')]
    df['diff'] = df['ob'] - df['nam']
    bias = df['diff'].mean()
    nam = nam + bias
    print("fancy_4inch NAM bias correction of: %.2fF applied" % (bias, ))
    # apply nam bias to sampled data
    df['nam'] += bias
    df['diff'] = df['ob'] - df['nam']
    # we are going to require data be within 1 SD of sampled or 5 deg
    std = 5. if df['nam'].std() < 5. else df['nam'].std()
    for station in df[df['diff'].abs() > std].index.values:
        print(("fancy_4inch %s QC'd %s out std: %.2f, ob:%.1f nam:%.1f") %
              (ts.strftime("%Y%m%d"), station, std, df.at[station, 'ob'],
               df.at[station, 'nam']))
        df.drop(station, inplace=True)

    # Query out centroids of counties...
    cdf = read_sql("""SELECT ST_x(ST_centroid(the_geom)) as lon,
        ST_y(ST_centroid(the_geom)) as lat
        from uscounties WHERE state_name = 'Iowa'
    """,
                   pdbconn,
                   index_col=None)
    for i, row in cdf.iterrows():
        x, y = get_idx(hlons, hlats, row['lon'], row['lat'])
        cdf.at[i, 'nam'] = nam[x, y]

    mp = MapPlot(sector='iowa',
                 title=("Average 4 inch Depth Soil Temperatures for %s") %
                 (ts.strftime("%b %d, %Y"), ),
                 subtitle=("County est. based on bias adj. "
                           "NWS NAM Model (black numbers), "
                           "ISUSM network observations (red numbers)"))
    mp.pcolormesh(hlons,
                  hlats,
                  nam,
                  np.arange(10, 101, 5),
                  cmap=cm.get_cmap('jet'),
                  units=r'$^\circ$F')
    mp.plot_values(df['lon'],
                   df['lat'],
                   df['ob'],
                   fmt='%.0f',
                   color='r',
                   labelbuffer=5)
    mp.plot_values(cdf['lon'],
                   cdf['lat'],
                   cdf['nam'],
                   fmt='%.0f',
                   textsize=11,
                   labelbuffer=5)
    mp.drawcounties()
    routes = "a" if day_ago >= 4 else "ac"
    pqstr = ("plot %s %s0000 soilt_day%s.png isuag_county_4inch_soil.png png"
             ) % (routes, ts.strftime("%Y%m%d"), day_ago)
    mp.postprocess(pqstr=pqstr)
    mp.close()
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.utc)
    total = None
    while now < ets:
        gmt = now.astimezone(pytz.utc)
        gribfn = None
        for prefix in ["GaugeCorr", "RadarOnly"]:
            gribfn = mrms.fetch(prefix + "_QPE_01H", gmt)
            if gribfn is None:
                continue
            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"), )
    mp = 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]

    mp.contourf(
        mrms.XAXIS,
        mrms.YAXIS,
        distance(np.flipud(total), "MM").value("IN"),
        clevs,
        cmap=nwsprecip(),
    )
    mp.drawcounties()
    mp.postprocess(pqstr=pqstr, view=False)
    mp.close()
Esempio n. 47
0
def doday(ts, realtime):
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    # Start at midnight
    now = ts.replace(hour=0, minute=0)
    ets = now + datetime.timedelta(hours=24)
    interval = datetime.timedelta(minutes=5)
    currenttime = datetime.datetime.utcnow()
    currenttime = currenttime.replace(tzinfo=pytz.timezone("UTC"))

    total = None
    lastts = None
    while now <= ets:
        gmt = now.astimezone(pytz.timezone("UTC"))
        if gmt > currenttime:
            break
        fn = gmt.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/"
                           "GIS/ifc/p05m_%Y%m%d%H%M.png"))
        if not os.path.isfile(fn):
            now += interval
            continue
        png = gdal.Open(fn, 0)
        data = np.flipud(png.ReadAsArray())  # units are mm per 5 minutes
        data = np.where(data > 254, 0, data) / 10.0
        if total is None:
            total = data
        else:
            total += data

        lastts = now

        now += interval
    if lastts is None:
        print(
            ('No IFC Data found for date: %s') % (now.strftime("%d %B %Y"), ))
        return
    lastts = lastts - datetime.timedelta(minutes=1)
    subtitle = "Total between 12:00 AM and %s" % (
        lastts.strftime("%I:%M %p %Z"), )
    routes = 'ac'
    if not realtime:
        routes = 'a'

    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

    sector = 'iowa'
    pqstr = ("plot %s %s00 %s_ifc_1d.png %s_ifc_1d.png png") % (
        routes, ts.strftime("%Y%m%d%H"), sector, sector)
    m = MapPlot(title=("%s Iowa Flood Center Today's Precipitation") %
                (ts.strftime("%-d %b %Y"), ),
                subtitle=subtitle,
                sector=sector)

    xaxis = -97.154167 + np.arange(1741) * 0.004167
    yaxis = 40.133331 + np.arange(1057) * 0.004167
    (x, y) = np.meshgrid(xaxis, yaxis)

    m.pcolormesh(x, y, distance(total, 'MM').value("IN"), clevs, units='inch')
    m.drawcounties()
    m.postprocess(pqstr=pqstr, view=False)
    m.close()