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')
def test_cwa(): """Exercise the cwa plotting routines""" mp = MapPlot(sector='cwa', cwa='MKX', nocaption=True) mp.contourf(np.arange(-94, -89), np.arange(40, 45), np.arange(5), np.arange(5), clevlabels=['a', 'b', 'c', 'd', 'e']) mp.draw_cwas() assert mp.cwa == 'MKX' return mp.fig
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) if ctx['t'] == 'state': bnds = reference.state_bounds[ctx['state']] title = reference.state_names[ctx['state']] else: bnds = reference.wfo_bounds[ctx['wfo']] nt = NetworkTable("WFO") title = "NWS CWA %s [%s]" % (nt.sts[ctx['wfo']]['name'], ctx['wfo']) df, valid = get_df(ctx, bnds) if df.empty: raise ValueError("No data was found for your query") mp = MapPlot(sector=('state' if ctx['t'] == 'state' else 'cwa'), state=ctx['state'], cwa=(ctx['wfo'] if len(ctx['wfo']) == 3 else ctx['wfo'][1:]), axisbg='white', title='%s for %s' % (PDICT2[ctx['v']], title), subtitle=('Map valid: %s UTC') % (valid.strftime("%d %b %Y %H:%M"), ), nocaption=True, titlefontsize=16) mp.contourf(df['lon'].values, df['lat'].values, df['vsby'].values, np.array([0.01, 0.1, 0.25, 0.5, 1, 2, 3, 5, 8, 9.9]), units='miles', cmap=plt.get_cmap('gray')) if ctx['t'] == 'state': df2 = df[df['state'] == ctx['state']] else: df2 = df[df['wfo'] == ctx['wfo']] mp.plot_values(df2['lon'].values, df2['lat'].values, df2['vsby'].values, '%.1f') mp.drawcounties() if ctx['t'] == 'cwa': mp.draw_cwas() return mp.fig, df
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) varname = ctx["v"] if ctx["t"] == "state": bnds = reference.state_bounds[ctx["state"]] title = reference.state_names[ctx["state"]] else: bnds = reference.wfo_bounds[ctx["wfo"]] title = "NWS CWA %s [%s]" % ( ctx["_nt"].sts[ctx["wfo"]]["name"], ctx["wfo"], ) df, valid = get_df(ctx, bnds) if df.empty: raise NoDataFound("No data was found for your query") mp = MapPlot( sector=("state" if ctx["t"] == "state" else "cwa"), state=ctx["state"], cwa=(ctx["wfo"] if len(ctx["wfo"]) == 3 else ctx["wfo"][1:]), axisbg="white", title="%s for %s" % (PDICT2[ctx["v"]], title), subtitle=("Map valid: %s UTC") % (valid.strftime("%d %b %Y %H:%M"),), nocaption=True, titlefontsize=16, ) if varname == "vsby": ramp = np.array([0.01, 0.1, 0.25, 0.5, 1, 2, 3, 5, 8, 9.9]) valunit = "miles" elif varname == "feel": valunit = "F" df["feel"] = ( apparent_temperature( df["tmpf"].values * units("degF"), df["relh"].values * units("percent"), df["sknt"].values * units("knots"), ) .to(units("degF")) .m ) # Data QC, cough if ctx.get("above"): df = df[df[varname] < ctx["above"]] if ctx.get("below"): df = df[df[varname] > ctx["below"]] # with QC done, we compute ramps if varname != "vsby": ramp = np.linspace( df[varname].min() - 5, df[varname].max() + 5, 10, dtype="i" ) mp.contourf( df["lon"].values, df["lat"].values, df[varname].values, ramp, units=valunit, cmap=get_cmap(ctx["cmap"]), ) if ctx["t"] == "state": df2 = df[df["state"] == ctx["state"]] else: df2 = df[df["wfo"] == ctx["wfo"]] mp.plot_values( df2["lon"].values, df2["lat"].values, df2[varname].values, "%.1f", labelbuffer=10, ) mp.drawcounties() if ctx["t"] == "cwa": mp.draw_cwas() return mp.fig, df
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) varname = ctx['v'] if ctx['t'] == 'state': bnds = reference.state_bounds[ctx['state']] title = reference.state_names[ctx['state']] else: bnds = reference.wfo_bounds[ctx['wfo']] title = "NWS CWA %s [%s]" % (ctx['_nt'].sts[ctx['wfo']]['name'], ctx['wfo']) df, valid = get_df(ctx, bnds) if df.empty: raise NoDataFound("No data was found for your query") mp = MapPlot(sector=('state' if ctx['t'] == 'state' else 'cwa'), state=ctx['state'], cwa=(ctx['wfo'] if len(ctx['wfo']) == 3 else ctx['wfo'][1:]), axisbg='white', title='%s for %s' % (PDICT2[ctx['v']], title), subtitle=('Map valid: %s UTC') % (valid.strftime("%d %b %Y %H:%M"), ), nocaption=True, titlefontsize=16) if varname == 'vsby': ramp = np.array([0.01, 0.1, 0.25, 0.5, 1, 2, 3, 5, 8, 9.9]) valunit = 'miles' elif varname == 'feel': valunit = 'F' df['feel'] = apparent_temperature(df['tmpf'].values * units('degF'), df['relh'].values * units('percent'), df['sknt'].values * units('knots')).to(units('degF')).m # Data QC, cough if ctx.get('above'): df = df[df[varname] < ctx['above']] if ctx.get('below'): df = df[df[varname] > ctx['below']] # with QC done, we compute ramps if varname != 'vsby': ramp = np.linspace(df[varname].min() - 5, df[varname].max() + 5, 10, dtype='i') mp.contourf(df['lon'].values, df['lat'].values, df[varname].values, ramp, units=valunit, cmap=plt.get_cmap(ctx['cmap'])) if ctx['t'] == 'state': df2 = df[df['state'] == ctx['state']] else: df2 = df[df['wfo'] == ctx['wfo']] mp.plot_values(df2['lon'].values, df2['lat'].values, df2[varname].values, '%.1f', labelbuffer=10) mp.drawcounties() if ctx['t'] == 'cwa': mp.draw_cwas() return mp.fig, df
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 = 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" if varname in ["max_gust", "max_sknt"] else "%.0f", labelbuffer=3, ) mp.drawcounties() if ctx["t"] == "cwa": mp.draw_cwas() return mp.fig, df
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) if ctx["sz"] < 5: ctx["sz"] = 5 if ctx["hours"] > 300: ctx["hours"] = 300 endts = ctx["endts"] basets = endts - datetime.timedelta(hours=ctx["hours"]) # Retrieve available obs df = load_data(ctx, basets, endts) # figure out our grid bounds ctx["bnds2163"] = compute_grid_bounds(ctx) # add zeros and QC df = add_zeros(df, ctx) # do gridding df2 = df[df[USEME]] lons, lats, vals = do_analysis(df2, ctx) rng = [0.01, 1, 2, 3, 4, 6, 8, 12, 18, 24, 30, 36] cmap = nwssnow() if ctx["t"] == "cwa": sector = "cwa" else: sector = "state" if len(ctx["csector"]) == 2 else ctx["csector"] mp = MapPlot( sector=sector, state=ctx["csector"], cwa=(ctx["wfo"] if len(ctx["wfo"]) == 3 else ctx["wfo"][1:]), axisbg="white", title=("NWS Local Storm Report%s Snowfall Total Analysis") % (" & COOP" if ctx["coop"] == "yes" else "", ), subtitle=("%.0f reports over past %.0f hours till %s, " "grid size: %.0fkm, Rbf: %s" "" % ( len(df2.index), ctx["hours"], endts.strftime("%d %b %Y %I:%M %p"), ctx["sz"], ctx["f"], )), ) if df2["val"].max() > 0 and ctx["p"] in ["both", "contour"]: mp.contourf(lons, lats, vals, rng, cmap=cmap, clip_on=(ctx["t"] != "cwa")) # Allow analysis to bleed outside the CWA per request. if ctx["t"] == "cwa": mp.draw_mask(sector="conus") mp.draw_cwas(linewidth=2) mp.drawcounties() if not df.empty and ctx["p"] in ["both", "plot"]: df2 = df[df["plotme"]] mp.plot_values( df2["lon"].values, df2["lat"].values, df2["val"].values, fmt="%.1f", labelbuffer=2, ) if ctx["z"] == "plot": df2 = df[df["state"] == "Z"] if not df2.empty: mp.plot_values( df2["lon"].values, df2["lat"].values, df2["state"].values, fmt="%s", color="#FF0000", labelbuffer=0, ) mp.drawcities() return mp.fig, df.drop(["geo", "plotme"], axis=1)
def test_illinois(): """Produce a plot that doesn't suck""" mp = MapPlot(sector='state', state='IL', nocaption=True) mp.draw_cwas() return mp.fig