示例#1
0
def test_states():
    """Exercise the state plotting routines"""
    mp = MapPlot(sector='state', state='CA', nocaption=True)
    assert mp.state == 'CA'
    return mp.fig
示例#2
0
文件: plot_ref.py 项目: nbackas/iem
def run(utc, routes):
    ''' Generate the plot for the given UTC time '''

    subprocess.call("python dl_hrrrref.py %s" %
                    (utc.strftime("%Y %m %d %H"), ),
                    shell=True)

    fn = "/tmp/ncep_hrrr_%s.grib2" % (utc.strftime("%Y%m%d%H"), )

    grbs = pygrib.open(fn)

    subprocess.call("rm /tmp/hrrr_ref_???.gif",
                    shell=True,
                    stderr=subprocess.PIPE,
                    stdout=subprocess.PIPE)

    lats = None
    lons = None
    i = 0
    for minute in range(0, 901, 15):
        now = utc + datetime.timedelta(minutes=minute)
        now = now.astimezone(pytz.timezone("America/Chicago"))
        grbs.seek(0)
        try:
            gs = grbs.select(level=1000, forecastTime=minute)
        except:
            continue
        g = gs[0]
        if lats is None:
            lats, lons = g.latlons()
            x1, x2, y1, y2 = compute_bounds(lons, lats)
            lats = lats[x1:x2, y1:y2]
            lons = lons[x1:x2, y1:y2]

        ref = g['values'][x1:x2, y1:y2]

        m = MapPlot(sector='midwest',
                    axisbg='tan',
                    title=('%s UTC NCEP HRRR 1 km AGL Reflectivity') %
                    (utc.strftime("%-d %b %Y %H"), ),
                    subtitle=('valid: %s') %
                    (now.strftime("%-d %b %Y %I:%M %p %Z"), ))

        m.pcolormesh(lons,
                     lats,
                     ref,
                     np.arange(0, 75, 5),
                     units='dBZ',
                     clip_on=False)
        m.postprocess(filename='/tmp/hrrr_ref_%03i.png' % (i, ))
        m.close()

        subprocess.call(("convert /tmp/hrrr_ref_%03i.png "
                         "/tmp/hrrr_ref_%03i.gif") % (i, i),
                        shell=True)

        i += 1

    # Generate anim GIF
    subprocess.call(("gifsicle --loopcount=0 --delay=50 "
                     "/tmp/hrrr_ref_???.gif > /tmp/hrrr_ref.gif"),
                    shell=True,
                    stderr=subprocess.PIPE,
                    stdout=subprocess.PIPE)

    pqstr = ("plot %s %s model/hrrr/hrrr_1km_ref.gif "
             "model/hrrr/hrrr_1km_ref_%02i.gif gif") % (
                 routes, utc.strftime("%Y%m%d%H%M"), utc.hour)
    subprocess.call("/home/ldm/bin/pqinsert -p '%s' /tmp/hrrr_ref.gif" %
                    (pqstr, ),
                    shell=True,
                    stderr=subprocess.PIPE,
                    stdout=subprocess.PIPE)

    # os.remove("/tmp/hrrr_ref.gif")
    os.remove(fn)
示例#3
0
def run(basets, endts, view):
    """Generate this plot for the given basets"""
    pgconn = get_dbconn('postgis', user='******')

    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
        """,
                  pgconn,
                  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 df2.empty:
                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()
    mp = 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:
        mp.contourf(cdf['lon'].values,
                    cdf['lat'].values,
                    cdf['val'].values,
                    rng,
                    cmap=cmap)
    mp.drawcounties()
    if len(tdf.index) > 0:
        mp.plot_values(tdf['lon'].values,
                       tdf['lat'].values,
                       tdf['val'].values,
                       fmt='%.1f')
    mp.drawcities()
    pqstr = "plot c 000000000000 lsr_snowfall.png bogus png"
    mp.postprocess(view=view, pqstr=pqstr)
    mp.close()

    # slightly different title to help uniqueness
    mp = 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:
        mp.contourf(cdf['lon'].values,
                    cdf['lat'].values,
                    cdf['val'].values,
                    rng,
                    cmap=cmap)
    mp.drawcounties()
    mp.drawcities()
    pqstr = "plot c 000000000000 lsr_snowfall_nv.png bogus png"
    mp.postprocess(view=view, pqstr=pqstr)
    mp.close()

    mp = 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:
        mp.contourf(cdf['lon'].values,
                    cdf['lat'].values,
                    cdf['val'].values,
                    rng,
                    cmap=cmap)
    mp.drawcities()
    pqstr = "plot c 000000000000 mw_lsr_snowfall.png bogus png"
    mp.postprocess(view=view, pqstr=pqstr)
    mp.close()
示例#4
0
from pyiem.plot import MapPlot
import pandas as pd

df = pd.read_csv('vertex.csv', index_col='WFO', na_values=['None'])
df['r'] = (df['CNTY_HITS'] - df['CWA_HITS']) / df['ALL'] * 100.
avgv = (df['CNTY_HITS'] - df['CWA_HITS']).sum() / float(df['ALL'].sum()) * 100.

m = MapPlot(
    sector='nws',
    axisbg='white',
    title='Percent of SVR+TOR Warning Vertices within 2km of County Border',
    subtitle=
    '1 Oct 2007 through 23 May 2016, Overall Avg: %.1f%%, * CWA Borders Excluded'
    % (avgv, ))
m.fill_cwas(df['r'], ilabel=True, lblformat='%.0f')
m.postprocess(filename='test.png')
示例#5
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:
        raise ValueError("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
示例#6
0
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'])
示例#7
0
def doday(ts, realtime):
    """
    Create a plot of precipitation stage4 estimates for some day
    """
    # Start at 1 AM
    now = ts.replace(hour=1, minute=0)
    ets = now + datetime.timedelta(hours=24)
    interval = datetime.timedelta(hours=1)
    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
        for prefix in ['GaugeCorr', 'RadarOnly']:
            gribfn = gmt.strftime(
                ("/mnt/a4/data/%Y/%m/%d/mrms/ncep/" + prefix + "_QPE_01H/" +
                 prefix + "_QPE_01H_00.00_%Y%m%d-%H%M00"
                 ".grib2.gz"))
            if os.path.isfile(gribfn):
                break
        if not os.path.isfile(gribfn):
            print("q3_today_total.py MISSING %s" % (gribfn, ))
            now += interval
            continue
        fp = gzip.GzipFile(gribfn, 'rb')
        (tmpfp, tmpfn) = tempfile.mkstemp()
        tmpfp = open(tmpfn, 'wb')
        tmpfp.write(fp.read())
        tmpfp.close()
        grbs = pygrib.open(tmpfn)
        grb = grbs[1]
        os.unlink(tmpfn)
        # careful here, how we deal with the two missing values!
        if total is None:
            total = grb['values']
        else:
            maxgrid = np.maximum(grb['values'], total)
            total = np.where(np.logical_and(grb['values'] >= 0, total >= 0),
                             grb['values'] + total, maxgrid)

        lastts = now

        now += interval
    if lastts is None:
        print(('No MRMS Q3 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_q2_1d.png %s_q2_1d.png png") % (
        routes, ts.strftime("%Y%m%d%H"), sector, sector)
    m = MapPlot(title=("%s NCEP MRMS Q3 Today's Precipitation") %
                (ts.strftime("%-d %b %Y"), ),
                subtitle=subtitle,
                sector=sector)

    (x, y) = np.meshgrid(mrms.XAXIS, mrms.YAXIS)

    m.pcolormesh(x, y, np.flipud(total) / 24.5, clevs, units='inch')
    m.drawcounties()
    m.postprocess(pqstr=pqstr, view=False)
    m.close()
示例#8
0
def test_plot22():
    """plot cwas that are filled"""
    mp = MapPlot(sector='iowa', continentalcolor='white', nocaption=True)
    mp.fill_cwas({'DMX': 80, 'MKX': 5, 'SJU': 30, 'AJK': 40, 'HFO': 50},
                 units='NWS Something or Another')
    return mp.fig
示例#9
0
def test_plot3():
    """ Exercise climdiv plot API """
    mp = MapPlot(sector='iowa', nocaption=True)
    mp.fill_climdiv({'IAC001': 80, 'AKC003': 5, 'HIC003': 30,
                     'AJK': 40, 'HFO': 50})
    return mp.fig
示例#10
0
def test_plot():
    """ Exercise the API """
    mp = MapPlot(sector='midwest', nocaption=True)
    mp.fill_cwas({'DMX': 80, 'MKX': 5, 'SJU': 30, 'AJK': 40},
                 units='no units')
    return mp.fig
示例#11
0
def test_plot2():
    """ Exercise NWS plot API """
    mp = MapPlot(sector='nws', continentalcolor='white', nocaption=True)
    mp.fill_cwas({'DMX': 80, 'MKX': 5, 'SJU': 30, 'AJK': 40, 'HFO': 50},
                 units='NWS Something or Another', ilabel=True)
    return mp.fig
示例#12
0
def test_textplot2():
    """plot values on a map"""
    mp = MapPlot(sector='iowa', nocaption=True)
    mp.plot_values(np.arange(-99, -94), np.arange(40, 45), np.arange(5),
                   labels=range(5, 11))
    return mp.fig
示例#13
0
def test_textplot():
    ''' Can we plot text and place labels on them '''
    mp = MapPlot(sector='iowa', nocaption=True)
    mp.plot_values(np.arange(-99, -94), np.arange(40, 45), np.arange(5))
    return mp.fig
示例#14
0
def test_illinois():
    """Produce a plot that doesn't suck"""
    mp = MapPlot(sector='state', state='IL', nocaption=True)
    mp.draw_cwas()
    return mp.fig
示例#15
0
"""Diagnostic check of the climdiv_weights.nc file"""
import netCDF4
from pyiem.plot import MapPlot
import numpy
from pyiem import iemre

nc = netCDF4.Dataset("/mesonet/data/iemre/climdiv_weights.nc")
lons = nc.variables['lon'][:]
lons = numpy.append(lons, iemre.EAST)
lats = nc.variables['lat'][:]
lats = numpy.append(lats, iemre.NORTH)

m = MapPlot(sector='midwest')
x, y = numpy.meshgrid(lons, lats)
m.map.pcolormesh(x[:, :], y[:, :], nc.variables['IAC001'][:, :], latlon=True)
#           numpy.linspace(0,1,20))

# x = [iemre.WEST, iemre.WEST, iemre.EAST, iemre.EAST, iemre.WEST]
# y = [iemre.SOUTH, iemre.NORTH, iemre.NORTH, iemre.SOUTH, iemre.SOUTH]
# x, y = m.map(x, y)
# m.ax.plot(x, y, lw=3, color='r', zorder=5)

m.postprocess(filename='test.png')
示例#16
0
def test_usdm():
    """Can we plot the current USDM"""
    mp = MapPlot(sector='conus', nocaption=True)
    mp.draw_usdm(valid=datetime.date(2018, 5, 7), hatched=True, filled=False)
    return mp.fig
示例#17
0
def do(valid):
    """Do Something"""
    clevs = np.arange(0, 0.25, 0.05)
    clevs = np.append(clevs, np.arange(0.25, 3.0, 0.25))
    clevs = np.append(clevs, np.arange(3.0, 10.0, 1))
    clevs[0] = 0.01

    precip = load_precip(valid)
    stage4 = load_precip(valid, "stage4")
    inqcprecip = load_precip(valid, "inqcprecip")
    outqcprecip = load_precip(valid, "outqcprecip")
    multiplier = load_precip(valid, "multiplier")

    yidx = int((43.27 - SOUTH) / 0.01)
    xidx = int((-94.39 - WEST) / 0.01)
    # print(("yidx:%s xidx:%s precip:%.2f stage4: %.2f inqc: %.2f outqc: %.2f "
    #       "mul: %.2f"
    #       ) % (yidx, xidx, precip[yidx, xidx], stage4[yidx, xidx],
    #            inqcprecip[yidx, xidx], outqcprecip[yidx, xidx],
    #            multiplier[yidx, xidx]))
    projection = ccrs.Mercator()
    pgconn = get_dbconn("idep")
    df = read_postgis(
        """
        SELECT ST_Transform(simple_geom, %s) as geom, huc_12,
        ST_x(ST_Transform(ST_Centroid(geom), 4326)) as centroid_x,
        ST_y(ST_Transform(ST_Centroid(geom), 4326)) as centroid_y,
        hu_12_name
        from huc12 i WHERE i.scenario = 0 and huc_12 = '071100010901'
    """,
        pgconn,
        params=(projection.proj4_init,),
        geom_col="geom",
        index_col="huc_12",
    )

    mp = MapPlot(
        sector="custom",
        axisbg="white",
        west=-92.0,
        south=40.24,
        east=-91.69,
        north=40.4,
        subtitle="Flowpath numbers are labelled.",
        title=("%s DEP Quality Controlled Precip Totals for 071100010901")
        % (valid.strftime("%-d %b %Y"),),
    )
    (lons, lats) = np.meshgrid(XS, YS)
    mp.pcolormesh(lons, lats, precip / 25.4, clevs, units="inch")
    mp.drawcounties()
    for _huc12, row in df.iterrows():
        p = Polygon(row["geom"].exterior, ec="k", fc="None", zorder=100, lw=2)
        mp.ax.add_patch(p)
    df = read_postgis(
        """
        SELECT ST_Transform(geom, %s) as geom, fpath
        from flowpaths WHERE scenario = 0 and huc_12 = '071100010901'
    """,
        pgconn,
        params=(projection.proj4_init,),
        geom_col="geom",
        index_col="fpath",
    )
    for fpath, row in df.iterrows():
        mp.ax.plot(row["geom"].xy[0], row["geom"].xy[1], c="k")

    df = read_postgis(
        """
        SELECT ST_Transform(st_pointn(geom, 1), %s) as geom, fpath
        from flowpaths WHERE scenario = 0 and huc_12 = '071100010901'
    """,
        pgconn,
        params=(projection.proj4_init,),
        geom_col="geom",
        index_col="fpath",
    )
    for fpath, row in df.iterrows():
        mp.ax.text(row["geom"].x, row["geom"].y, str(fpath), fontsize=12)

    mp.postprocess(filename="qc.png")
    mp.close()

    mp = MapPlot(
        sector=SECTOR,
        axisbg="white",
        title=("%s Stage IV Precip Totals") % (valid.strftime("%-d %b %Y"),),
    )
    mp.pcolormesh(lons, lats, stage4 / 25.4, clevs, units="inch")
    mp.drawcounties()
    mp.postprocess(filename="stageIV.png")
    mp.close()

    mp = MapPlot(
        sector=SECTOR,
        axisbg="white",
        title=("%s High-res total prior to QC")
        % (valid.strftime("%-d %b %Y"),),
    )
    mp.pcolormesh(lons, lats, inqcprecip / 25.4, clevs, units="inch")
    mp.drawcounties()
    mp.postprocess(filename="inqcprecip.png")
    mp.close()

    mp = MapPlot(
        sector=SECTOR,
        axisbg="white",
        title=("%s High-res total after QC") % (valid.strftime("%-d %b %Y"),),
    )
    mp.pcolormesh(lons, lats, outqcprecip / 25.4, clevs, units="inch")
    mp.drawcounties()
    mp.postprocess(filename="outqcprecip.png")
    mp.close()

    mp = MapPlot(
        sector=SECTOR,
        axisbg="white",
        title=("%s QC Change in Precip Out - In")
        % (valid.strftime("%-d %b %Y"),),
    )
    diff = (outqcprecip - inqcprecip) / 25.4
    mp.pcolormesh(
        lons,
        lats,
        diff,
        np.arange(-1.4, 1.5, 0.1),
        cmap=plt.get_cmap("BrBG"),
        units="inch",
    )
    mp.drawcounties()
    mp.postprocess(filename="qcprecipdiff.png")
    mp.close()

    mp = MapPlot(
        sector=SECTOR,
        axisbg="white",
        title=("%s multiplier") % (valid.strftime("%-d %b %Y"),),
    )
    mp.pcolormesh(
        lons,
        lats,
        multiplier,
        np.arange(0.0, 2.5, 0.2),
        cmap=plt.get_cmap("jet"),
        units="ratio",
    )
    mp.drawcounties()
    mp.postprocess(filename="multiplier.png")
    mp.close()

    mp = MapPlot(
        sector=SECTOR,
        axisbg="white",
        title=("%s manually computed QC Precip  mul * stage4")
        % (valid.strftime("%-d %b %Y"),),
    )
    mp.pcolormesh(lons, lats, multiplier * stage4 / 25.4, clevs, units="inch")
    mp.drawcounties()
    mp.postprocess(filename="qcmancalc.png")
    mp.close()

    # Go MRMS
    ncfn = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (valid.year,)
    if not os.path.isfile(ncfn):
        return
    nc = netCDF4.Dataset(ncfn, "r")
    xidx = int((WEST - nc.variables["lon"][0]) * 100.0)
    yidx = int((SOUTH - nc.variables["lat"][0]) * 100.0)
    idx = daily_offset(valid)
    mrms = nc.variables["p01d"][idx, yidx : (yidx + 800), xidx : (xidx + 921)]
    nc.close()
示例#18
0
def test_alaska():
    """See that Alaska plots nicely."""
    mp = MapPlot(sector='state', state='AK', nocaption=True)
    return mp.fig
示例#19
0
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn("coop")
    ctx = get_autoplot_context(fdict, get_description())

    state = ctx["state"]
    varname = ctx["var"]
    sector = ctx["sector"]
    threshold = ctx["threshold"]
    opt = ctx["opt"]
    month = ctx["month"]
    p1syear = ctx["p1syear"]
    p1eyear = ctx["p1eyear"]
    p1yearreq = p1eyear - p1syear
    p2syear = ctx["p2syear"]
    p2eyear = ctx["p2eyear"]
    p2yearreq = p2eyear - p2syear
    opt1 = ctx["opt1"]

    if month == "all":
        months = range(1, 13)
    elif month == "fall":
        months = [9, 10, 11]
    elif month == "winter":
        months = [12, 1, 2]
    elif month == "spring":
        months = [3, 4, 5]
    elif month == "summer":
        months = [6, 7, 8]
    elif month == "gs":
        months = [5, 6, 7, 8, 9]
    else:
        ts = datetime.datetime.strptime("2000-" + month + "-01", "%Y-%b-%d")
        # make sure it is length two for the trick below in SQL
        months = [ts.month]

    table = "alldata"
    if sector == "state":
        # optimization
        table = "alldata_%s" % (state, )

    df = read_sql(
        """
    WITH period1 as (
        SELECT station, year, sum(precip) as total_precip,
        avg((high+low) / 2.) as avg_temp, avg(high) as avg_high,
        avg(low) as avg_low,
        sum(gddxx(50, 86, high, low)) as sum_gdd,
        sum(case when high > 86 then high - 86 else 0 end) as sum_sdd,
        sum(case when high >= %s then 1 else 0 end) as days_high_above
        from """ + table + """ WHERE year >= %s and year < %s
        and month in %s GROUP by station, year),
    period2 as (
        SELECT station, year, sum(precip) as total_precip,
        avg((high+low) / 2.) as avg_temp, avg(high) as avg_high,
        avg(low) as avg_low,
        sum(gddxx(50, 86, high, low)) as sum_gdd,
        sum(case when high > 86 then high - 86 else 0 end) as sum_sdd,
        sum(case when high >= %s then 1 else 0 end) as days_high_above
        from """ + table + """ WHERE year >= %s and year < %s
        and month in %s GROUP by station, year),
    p1agg as (
        SELECT station, avg(total_precip) as precip,
        avg(avg_temp) as avg_temp, avg(avg_high) as avg_high,
        avg(avg_low) as avg_low, avg(sum_sdd) as sdd,
        avg(sum_gdd) as gdd,
        avg(days_high_above) as avg_days_high_above,
        count(*) as count
        from period1 GROUP by station),
    p2agg as (
        SELECT station, avg(total_precip) as precip,
        avg(avg_temp) as avg_temp, avg(avg_high) as avg_high,
        avg(avg_low) as avg_low, avg(sum_sdd) as sdd,
        avg(sum_gdd) as gdd,
        avg(days_high_above) as avg_days_high_above,
        count(*) as count
        from period2 GROUP by station),
    agg as (
        SELECT p2.station,
        p2.precip as p2_total_precip, p1.precip as p1_total_precip,
        p2.gdd as p2_gdd, p1.gdd as p1_gdd,
        p2.sdd as p2_sdd, p1.sdd as p1_sdd,
        p2.avg_temp as p2_avg_temp, p1.avg_temp as p1_avg_temp,
        p1.avg_high as p1_avg_high, p2.avg_high as p2_avg_high,
        p1.avg_low as p1_avg_low, p2.avg_low as p2_avg_low,
        p1.avg_days_high_above as p1_days_high_above,
        p2.avg_days_high_above as p2_days_high_above
        from p1agg p1 JOIN p2agg p2 on
        (p1.station = p2.station)
        WHERE p1.count >= %s and p2.count >= %s)

    SELECT station, ST_X(geom) as lon, ST_Y(geom) as lat,
    d.* from agg d JOIN stations t ON (d.station = t.id)
    WHERE t.network ~* 'CLIMATE'
    and substr(station, 3, 1) != 'C' and substr(station, 3, 4) != '0000'
    """,
        pgconn,
        params=[
            threshold,
            p1syear,
            p1eyear,
            tuple(months),
            threshold,
            p2syear,
            p2eyear,
            tuple(months),
            p1yearreq,
            p2yearreq,
        ],
        index_col=None,
    )
    if df.empty:
        raise NoDataFound("No Data Found.")
    df["total_precip"] = df["p2_total_precip"] - df["p1_total_precip"]
    df["avg_temp"] = df["p2_avg_temp"] - df["p1_avg_temp"]
    df["avg_high"] = df["p2_avg_high"] - df["p1_avg_high"]
    df["avg_low"] = df["p2_avg_low"] - df["p1_avg_low"]
    df["gdd"] = df["p2_gdd"] - df["p1_gdd"]
    df["sdd"] = df["p2_sdd"] - df["p1_sdd"]
    df["days_high_above"] = df["p2_days_high_above"] - df["p1_days_high_above"]
    column = varname
    title = "%s %s" % (MDICT[month], PDICT3[varname])
    title = title.replace("[Threshold]", "%.1f" % (threshold, ))
    if opt1 == "p1":
        column = "p1_%s" % (varname, )
        title = "%.0f-%.0f %s" % (p1syear, p1eyear, title)
    else:
        title = ("%.0f-%.0f minus %.0f-%.0f %s Difference (%s)") % (
            p2syear,
            p2eyear,
            p1syear,
            p1eyear,
            title,
            UNITS[varname],
        )

    # Reindex so that most extreme values are first
    df = df.reindex(df[column].abs().sort_values(ascending=False).index)
    # drop 5% most extreme events, too much?
    df2 = df.iloc[int(len(df.index) * 0.05):]

    mp = MapPlot(
        sector=sector,
        state=state,
        axisbg="white",
        title=title,
        subtitle=("based on IEM Archives"),
        titlefontsize=12,
    )
    if opt1 == "diff":
        # Create 9 levels centered on zero
        abval = df2[column].abs().max()
        levels = centered_bins(abval)
    else:
        levels = [
            round(v, PRECISION[varname])
            for v in np.percentile(df2[column].values, range(0, 101, 10))
        ]
    if opt in ["both", "contour"]:
        mp.contourf(
            df2["lon"].values,
            df2["lat"].values,
            df2[column].values,
            levels,
            cmap=get_cmap(ctx["cmap"]),
            units=UNITS[varname],
        )
    if sector == "state":
        mp.drawcounties()
    if opt in ["both", "values"]:
        mp.plot_values(
            df2["lon"].values,
            df2["lat"].values,
            df2[column].values,
            fmt="%%.%if" % (PRECISION[varname], ),
            labelbuffer=5,
        )

    return mp.fig, df
示例#20
0
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn("coop")
    ctx = get_autoplot_context(fdict, get_description())
    state = ctx["state"][:2]
    varname = ctx["var"]
    sector = ctx["sector"]
    opt = ctx["opt"]
    month = ctx["month"]
    if month == "all":
        months = range(1, 13)
    elif month == "fall":
        months = [9, 10, 11]
    elif month == "winter":
        months = [12, 1, 2]
    elif month == "spring":
        months = [3, 4, 5]
    elif month == "mjj":
        months = [5, 6, 7]
    elif month == "gs":
        months = [5, 6, 7, 8, 9]
    elif month == "summer":
        months = [6, 7, 8]
    else:
        ts = datetime.datetime.strptime("2000-" + month + "-01", "%Y-%b-%d")
        # make sure it is length two for the trick below in SQL
        months = [ts.month]

    if len(months) == 1:
        title = "%s %s" % (calendar.month_name[months[0]], PDICT3[varname])
    else:
        title = "%s" % (MDICT[month], )
    mp = MapPlot(
        sector=sector,
        state=state,
        axisbg="white",
        title="%s %s for %s" % (PDICT5[ctx["src"]], PDICT3[varname], title),
        nocaption=True,
    )
    bnds = mp.ax.get_extent(crs=ccrs.PlateCarree())

    joincol = "ncdc81" if ctx["src"] == "ncdc_climate81" else "id"
    extra = ""
    if not ctx["src"].startswith("ncdc_"):
        extra = """,
        sum(cdd65) as total_cdd65,
        sum(hdd65) as total_hdd65,
        sum(gdd32) as total_gdd32,
        sum(gdd41) as total_gdd41,
        sum(gdd46) as total_gdd46,
        sum(gdd48) as total_gdd48,
        sum(gdd50) as total_gdd50,
        sum(gdd51) as total_gdd51,
        sum(gdd52) as total_gdd52
        """
    df = read_sql(
        """
        WITH mystations as (
            select """ + joincol + """ as myid,
            max(ST_x(geom)) as lon, max(ST_y(geom)) as lat from stations
            where network ~* 'CLIMATE' and
            ST_Contains(ST_MakeEnvelope(%s, %s, %s, %s, 4326), geom)
            GROUP by myid
        )
        SELECT station, extract(month from valid) as month,
        max(lon) as lon, min(lat) as lat,
        sum(precip) as total_precip,
        avg(high) as avg_high,
        avg(low) as avg_low,
        avg((high+low)/2.) as avg_temp """ + extra + """
        from """ + ctx["src"] + """ c
        JOIN mystations t on (c.station = t.myid)
        WHERE extract(month from valid) in %s
        GROUP by station, month
        """,
        pgconn,
        params=(bnds[0], bnds[2], bnds[1], bnds[3], tuple(months)),
        index_col=["station", "month"],
    )
    if df.empty:
        raise NoDataFound("No data was found for query, sorry.")

    if len(months) == 1:
        df2 = df
    else:
        if varname.startswith("total"):
            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")
    levels = np.linspace(df2[varname].min(), df2[varname].max(), 10)
    levels = [round(x, PRECISION.get(varname, 1)) for x in levels]
    if opt in ["both", "contour"]:
        mp.contourf(
            df2["lon"].values,
            df2["lat"].values,
            df2[varname].values,
            levels,
            units=UNITS.get(varname, "F"),
            cmap=get_cmap(ctx["cmap"]),
            clip_on=False,
        )
    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.get(varname, 1), ),
            labelbuffer=5,
        )

    return mp.fig, df
示例#21
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    pgconn = get_dbconn('postgis')
    ctx = get_autoplot_context(fdict, get_description())
    sts = ctx['sdate']
    sts = sts.replace(tzinfo=pytz.utc)
    ets = ctx['edate']
    ets = ets.replace(tzinfo=pytz.utc)
    p1 = ctx['phenomenav1']
    p2 = ctx['phenomenav2']
    p3 = ctx['phenomenav3']
    p4 = ctx['phenomenav4']
    phenomena = []
    for p in [p1, p2, p3, p4]:
        if p is not None:
            phenomena.append(p[:2])
    s1 = ctx['significancev1']
    s2 = ctx['significancev2']
    s3 = ctx['significancev3']
    s4 = ctx['significancev4']
    significance = []
    for s in [s1, s2, s3, s4]:
        if s is not None:
            significance.append(s[0])

    pstr = []
    subtitle = ""
    title = ""
    for p, s in zip(phenomena, significance):
        pstr.append("(phenomena = '%s' and significance = '%s')" % (p, s))
        subtitle += "%s.%s " % (p, s)
        title += vtec.get_ps_string(p, s)
    if len(phenomena) > 1:
        title = "VTEC Unique Event"
    pstr = " or ".join(pstr)
    pstr = "(%s)" % (pstr, )

    df = read_sql("""
with total as (
  select distinct wfo, extract(year from issue at time zone 'UTC') as year,
  phenomena, significance, eventid from warnings
  where """ + pstr + """ and
  issue >= %s and issue < %s
)

SELECT wfo, phenomena, significance, year, count(*) from total
GROUP by wfo, phenomena, significance, year
    """,
                  pgconn,
                  params=(sts, ets))

    df2 = df.groupby('wfo')['count'].sum()

    nt = NetworkTable("WFO")
    for sid in nt.sts:
        sid = sid[-3:]
        if sid not in df2:
            df2[sid] = 0
    maxv = df2.max()
    bins = [0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 200]
    if maxv > 200:
        bins = [
            0, 1, 3, 5, 10, 20, 35, 50, 75, 100, 150, 200, 250, 500, 750, 1000
        ]
    elif maxv > 1000:
        bins = [
            0, 1, 5, 10, 50, 100, 150, 200, 250, 500, 750, 1000, 1250, 1500,
            2000
        ]

    mp = MapPlot(sector='nws',
                 axisbg='white',
                 title='%s Counts by NWS Office' % (title, ),
                 subtitle=('Valid %s - %s UTC, based on VTEC: %s') %
                 (sts.strftime("%d %b %Y %H:%M"),
                  ets.strftime("%d %b %Y %H:%M"), subtitle))
    mp.fill_cwas(df2, bins=bins, ilabel=True)

    return mp.fig, df
示例#22
0
def doit(ts):
    """
    Generate hourly plot of stage4 data
    """
    gmtnow = datetime.datetime.utcnow()
    gmtnow = gmtnow.replace(tzinfo=pytz.utc)
    routes = "a"
    if ((gmtnow - ts).days * 86400.0 + (gmtnow - ts).seconds) < 7200:
        routes = "ac"

    fn = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.01h.grib" % (
        ts.strftime("%Y/%m/%d"),
        ts.strftime("%Y%m%d%H"),
    )
    if not os.path.isfile(fn):
        print("current/stage4_hourly.py Missing stage4 %s" % (fn, ))
        return

    grbs = pygrib.open(fn)
    grib = grbs[1]
    lats, lons = grib.latlons()
    vals = grib.values / 25.4

    cmap = cm.get_cmap("jet")
    cmap.set_under("white")
    cmap.set_over("black")
    clevs = [
        0.01,
        0.05,
        0.1,
        0.2,
        0.3,
        0.4,
        0.5,
        0.6,
        0.7,
        0.8,
        0.9,
        1,
        1.5,
        2,
        3,
    ]
    localtime = ts.astimezone(pytz.timezone("America/Chicago"))

    for sector in ["iowa", "midwest", "conus"]:
        mp = MapPlot(
            sector=sector,
            title="Stage IV One Hour Precipitation",
            subtitle="Hour Ending %s" %
            (localtime.strftime("%d %B %Y %I %p %Z"), ),
        )
        mp.pcolormesh(lons, lats, vals, clevs, units="inch")
        pqstr = "plot %s %s00 %s_stage4_1h.png %s_stage4_1h_%s.png png" % (
            routes,
            ts.strftime("%Y%m%d%H"),
            sector,
            sector,
            ts.strftime("%H"),
        )
        if sector == "iowa":
            mp.drawcounties()
        mp.postprocess(view=False, pqstr=pqstr)
        mp.close()
示例#23
0
def plotter(fdict):
    """ Go """
    ctx = get_autoplot_context(fdict, get_description())
    varname = ctx["v"]

    df = get_df(ctx)
    if df.empty:
        raise NoDataFound("No data was found for your query")
    mp = MapPlot(
        sector=("state" if ctx["t"] == "state" else "cwa"),
        state=ctx["state"],
        cwa=(ctx["wfo"] if len(ctx["wfo"]) == 3 else ctx["wfo"][1:]),
        axisbg="white",
        title="%s for %s on %s" % (PDICT2[ctx["v"]], ctx["title"], ctx["day"]),
        nocaption=True,
        titlefontsize=16,
    )
    ramp = None
    cmap = plt.get_cmap(ctx["cmap"])
    extend = "both"
    if varname in ["max_gust", "max_sknt"]:
        extend = "max"
        ramp = np.arange(0, 40, 4)
        ramp = np.append(ramp, np.arange(40, 80, 10))
        ramp = np.append(ramp, np.arange(80, 120, 20))
    # Data QC, cough
    if ctx.get("above"):
        df = df[df[varname] < ctx["above"]]
    if ctx.get("below"):
        df = df[df[varname] > ctx["below"]]
    # with QC done, we compute ramps
    if ramp is None:
        ramp = np.linspace(df[varname].min() - 5,
                           df[varname].max() + 5,
                           10,
                           dtype="i")

    if ctx["p"] == "both":
        mp.contourf(
            df["lon"].values,
            df["lat"].values,
            df[varname].values,
            ramp,
            units=VARUNITS[varname],
            cmap=cmap,
            spacing="proportional",
            extend=extend,
        )
    if ctx["t"] == "state":
        df2 = df[df[ctx["t"]] == ctx[ctx["t"]]]
    else:
        df2 = df[df["wfo"] == ctx["wfo"]]

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

    return mp.fig, df
示例#24
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']:
            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()
示例#25
0
cursor.execute("""select station, st_x(geom), st_y(geom),
    max(valid) from
    cli_data c JOIN stations s on (s.id = c.station)
    WHERE s.network = 'NWSCLI' and c.valid > '2016-01-01'
    and low < 32 GROUP by station, st_x, st_y""")

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

base = datetime.date(2016, 4, 15)

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].strftime("%-m/%-d"))
    colors.append('#ff0000' if row[3] < base else '#0000ff')


m = MapPlot(sector='midwest', axisbg='white',
            title="2016 Date of Last Sub 32$^\circ$F Temperature",
            subtitle='2 May 2016 based on NWS issued CLI Reports, pre April 15 dates in red')
m.plot_values(lons, lats, vals, fmt='%s', textsize=12, color=colors,
              labelbuffer=5)
m.postprocess(filename='test.png')

示例#26
0
文件: p84.py 项目: tutuhuang/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    from pyiem.plot import MapPlot, nwsprecip
    ctx = util.get_autoplot_context(fdict, get_description())
    ptype = ctx['ptype']
    sdate = ctx['sdate']
    edate = ctx['edate']
    src = ctx['src']
    opt = ctx['opt']
    usdm = ctx['usdm']
    if sdate.year != edate.year:
        raise ValueError('Sorry, do not support multi-year plots yet!')
    days = (edate - sdate).days
    sector = ctx['sector']

    if sdate == edate:
        title = sdate.strftime("%-d %B %Y")
    else:
        title = "%s to %s (inclusive)" % (sdate.strftime("%-d %b"),
                                          edate.strftime("%-d %b %Y"))
    x0 = 0
    x1 = -1
    y0 = 0
    y1 = -1
    if sector == 'midwest':
        state = None
    else:
        state = sector
        sector = 'state'

    if src == 'mrms':
        ncfn = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (sdate.year, )
        clncfn = "/mesonet/data/iemre/mw_mrms_dailyc.nc"
        ncvar = 'p01d'
        source = 'MRMS Q3'
        subtitle = 'NOAA MRMS Project, GaugeCorr and RadarOnly'
    else:
        ncfn = "/mesonet/data/prism/%s_daily.nc" % (sdate.year, )
        clncfn = "/mesonet/data/prism/prism_dailyc.nc"
        ncvar = 'ppt'
        source = 'OSU PRISM'
        subtitle = ('PRISM Climate Group, Oregon State Univ., '
                    'http://prism.oregonstate.edu, created 4 Feb 2004.')

    mp = MapPlot(sector=sector,
                 state=state,
                 axisbg='white',
                 nocaption=True,
                 title='%s:: %s Precip %s' % (source, title, PDICT3[opt]),
                 subtitle='Data from %s' % (subtitle, ),
                 titlefontsize=14)

    idx0 = iemre.daily_offset(sdate)
    idx1 = iemre.daily_offset(edate) + 1
    if not os.path.isfile(ncfn):
        raise ValueError("No data for that year, sorry.")
    nc = netCDF4.Dataset(ncfn, 'r')
    if state is not None:
        x0, y0, x1, y1 = util.grid_bounds(nc.variables['lon'][:],
                                          nc.variables['lat'][:],
                                          state_bounds[state])
    lats = nc.variables['lat'][y0:y1]
    lons = nc.variables['lon'][x0:x1]
    if (idx1 - idx0) < 32:
        p01d = distance(
            np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0),
            'MM').value('IN')
    else:
        # Too much data can overwhelm this app, need to chunk it
        for i in range(idx0, idx1, 10):
            i2 = min([i + 10, idx1])
            if idx0 == i:
                p01d = distance(
                    np.sum(nc.variables[ncvar][i:i2, y0:y1, x0:x1], 0),
                    'MM').value('IN')
            else:
                p01d += distance(
                    np.sum(nc.variables[ncvar][i:i2, y0:y1, x0:x1], 0),
                    'MM').value('IN')
    nc.close()
    if np.ma.is_masked(np.max(p01d)):
        raise ValueError("Data Unavailable")
    units = 'inches'
    if opt == 'dep':
        # Do departure work now
        nc = netCDF4.Dataset(clncfn)
        climo = distance(
            np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0),
            'MM').value('IN')
        p01d = p01d - climo
        cmap = plt.get_cmap('RdBu')
        [maxv] = np.percentile(np.abs(p01d), [
            99,
        ])
        clevs = np.around(np.linspace(0 - maxv, maxv, 11), decimals=2)
    elif opt == 'per':
        nc = netCDF4.Dataset(clncfn)
        climo = distance(
            np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0),
            'MM').value('IN')
        p01d = p01d / climo * 100.
        cmap = plt.get_cmap('RdBu')
        cmap.set_under('white')
        cmap.set_over('black')
        clevs = [1, 10, 25, 50, 75, 100, 125, 150, 200, 300, 500]
        units = 'percent'
    else:
        clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10]
        if days > 6:
            clevs = [0.01, 0.3, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 10, 15, 20]
        if days > 29:
            clevs = [0.01, 0.5, 1, 2, 3, 4, 5, 6, 8, 10, 15, 20, 25, 30, 35]
        if days > 90:
            clevs = [0.01, 1, 2, 3, 4, 5, 6, 8, 10, 15, 20, 25, 30, 35, 40]
        cmap = nwsprecip()
        cmap.set_over('k')

    x2d, y2d = np.meshgrid(lons, lats)
    if ptype == 'c':
        mp.contourf(x2d, y2d, p01d, clevs, cmap=cmap, units=units, iline=False)
    else:
        res = mp.pcolormesh(x2d, y2d, p01d, clevs, cmap=cmap, units=units)
        res.set_rasterized(True)
    if sector != 'midwest':
        mp.drawcounties()
        mp.drawcities()
    if usdm == 'yes':
        mp.draw_usdm(edate, filled=False, hatched=True)

    return mp.fig
示例#27
0
def plotter(fdict):
    """ Go """

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

    table = "alldata_%s" % (sector, ) if len(sector) == 2 else "alldata"
    state_limiter = ""
    if sector == 'iailin':
        state_limiter = (
            " and network in ('IACLIMATE', 'ILCLIMATE', 'INCLIMATE') ")
    df = read_sql("""
    WITH obs as (
        SELECT station, gddxx(%s, 86, high, low) as gdd,
        cdd(high, low, 65) as cdd65, hdd(high, low, 65) as hdd65,
        sday, high, low, precip,
        (high + low)/2. as avg_temp
        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,
        gdd""" + str(ctx['gddbase']) + """ as gdd, cdd65, hdd65
        from climate51),
    combo as (
        SELECT o.station, o.precip - c.precip as precip_diff,
        o.precip as precip, c.precip as cprecip,
        o.avg_temp, o.cdd65, o.hdd65,
        o.high, o.low, o.gdd, c.gdd as cgdd,
        o.gdd - c.gdd as gdd_diff,
        o.cdd65 - c.cdd65 as cdd_diff,
        o.hdd65 - c.hdd65 as hdd_diff,
        o.avg_temp - (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,
        avg(avg_temp) as avg_temp,
        sum(precip_diff) as precip_depart,
        sum(precip) as precip, sum(cprecip) as cprecip,
        avg(high) as avg_high_temp,
        avg(low) as avg_low_temp,
        max(high) as max_high_temp,
        min(low) as min_low_temp, sum(gdd_diff) as gdd_depart,
        sum(gdd) / greatest(1, sum(cgdd)) * 100. as gdd_percent,
        avg(temp_diff) as avg_temp_depart, sum(gdd) as gdd_sum,
        sum(cgdd) as cgdd_sum,
        sum(cdd65) as cdd_sum,
        sum(hdd65) as hdd_sum,
        sum(cdd_diff) as cdd_depart,
        sum(hdd_diff) as hdd_depart
        from combo GROUP by station)

    SELECT d.station, t.name,
    precip as precip_sum,
    avg_temp,
    cprecip as cprecip_sum,
    precip_depart,
    min_low_temp,
    avg_temp_depart,
    gdd_depart,
    gdd_sum,
    gdd_percent,
    cgdd_sum,
    max_high_temp,
    avg_high_temp,
    avg_low_temp,
    cdd_sum, hdd_sum, cdd_depart, hdd_depart,
    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' """ + state_limiter + """
    """,
                  pgconn,
                  params=(ctx['gddbase'], date1, date2),
                  index_col='station')
    df = df.reindex(df[varname].abs().sort_values(ascending=False).index)

    datefmt = "%d %b %Y" if varname != 'cgdd_sum' else '%d %b'
    subtitle = ''
    if varname.find('depart') > -1:
        subtitle = ('%s is compared with 1951-%s Climatology'
                    ' to compute departures') % (
                        date1.year, datetime.date.today().year - 1)
    elif varname.startswith('c'):
        subtitle = ("Climatology is based on data from 1951-%s") % (
            datetime.date.today().year - 1, )
    mp = MapPlot(sector="state" if len(sector) == 2 else sector,
                 state=sector,
                 axisbg='white',
                 title='%s - %s %s [%s]' %
                 (date1.strftime(datefmt), date2.strftime(datefmt),
                  PDICT2.get(varname).replace("$base", str(
                      ctx['gddbase'])), UNITS.get(varname)),
                 subtitle=subtitle)
    fmt = '%.2f'
    cmap = cm.get_cmap(ctx['cmap'])
    if varname in ['precip_depart', 'avg_temp_depart', 'gdd_depart']:
        rng = df[varname].abs().describe(percentiles=[0.95])['95%']
        clevels = np.linspace(0 - rng,
                              rng,
                              7,
                              dtype='i' if varname == 'gdd_depart' else 'f')
        if varname == 'gdd_depart':
            fmt = '%.0f'
    elif varname in ['precip_sum']:
        rng = df[varname].abs().describe(percentiles=[0.95])['95%']
        clevels = np.linspace(0, rng, 7)
        cmap.set_under('white')
        cmap.set_over('black')
    elif varname.endswith("_percent"):
        clevels = np.array([10, 25, 50, 75, 100, 125, 150, 175, 200])
        fmt = '%.0f'
    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.set_bad('white')
    if ctx['p'] == 'contour':
        mp.contourf(df['lon'].values,
                    df['lat'].values,
                    df[varname].values,
                    clevels,
                    clevlabels=clevlabels,
                    cmap=cmap,
                    units=UNITS.get(varname))
    if ctx['c'] == 'yes':
        mp.plot_values(df['lon'].values,
                       df['lat'].values,
                       df[varname].values,
                       fmt=fmt,
                       labelbuffer=5)
    if len(sector) == 2 or sector == 'iailin':
        mp.drawcounties()
    if ctx['usdm'] == 'yes':
        mp.draw_usdm(date2, filled=False, hatched=True)

    return mp.fig, df
示例#28
0
文件: p86.py 项目: nbackas/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    ptype = fdict.get('ptype', 'c')
    date = datetime.datetime.strptime(fdict.get('date', '2015-01-01'),
                                      '%Y-%m-%d')
    varname = fdict.get('var', 'rsds')

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

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

    return m.fig
示例#29
0
    WHERE s.network in ('IA_COOP') and s.iemid = c.iemid and
    extract(month from day) = %s and
    extract(year from day) = extract(year from now())
    GROUP by id, lat, lon""" % (
    now.year,
    now.strftime("%m"),
)

lats = []
lons = []
precip = []
labels = []
icursor.execute(sql)
for row in icursor:
    if row[2] > (now.day / 3):
        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)
示例#30
0
def test_drawugcs():
    """test drawing of UGCS"""
    mp = MapPlot(sector='conus', title='Counties, 3 filled in Iowa',
                 nocaption=True)
    mp.fill_ugcs({"IAC001": 10, "IAC003": 20, "IAC005": 30})
    return mp.fig