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()
def test_colorramps(self): """make sure our colorramps are happy""" c = plot.james() self.assertEquals(c.N, 12) c = plot.james2() self.assertEquals(c.N, 12) c = plot.whitebluegreenyellowred() self.assertEquals(c.N, 236) c = plot.nwssnow() self.assertEquals(c.N, 11)
def test_colorramps(): """make sure our colorramps are happy""" c = plot.james() assert c.N == 12 c = plot.james2() assert c.N == 12 c = plot.whitebluegreenyellowred() assert c.N == 236 c = plot.nwssnow() assert c.N == 11
def run(basets, endts, view): """Generate this plot for the given basets""" df = read_sql("""SELECT state, max(magnitude) as val, ST_x(geom) as lon, ST_y(geom) as lat from lsrs WHERE type in ('S') and magnitude >= 0 and valid > %s and valid < %s GROUP by state, lon, lat """, POSTGIS, params=(basets, endts), index_col=None) df['used'] = False df['textplot'] = True df.sort_values(by='val', ascending=False, inplace=True) # Now, we need to add in zeros, lets say we are looking at a .25 degree box mybuffer = 0.75 newrows = [] for lat in np.arange(reference.MW_SOUTH, reference.MW_NORTH, mybuffer): for lon in np.arange(reference.MW_WEST, reference.MW_EAST, mybuffer): df2 = df[(df['lat'] >= lat) & (df['lat'] < (lat + mybuffer)) & (df['lon'] >= lon) & (df['lon'] < (lon + mybuffer))] if len(df2.index) == 0: newrows.append( dict(lon=(lon + mybuffer / 2.), lat=(lat + mybuffer / 2.), val=0, used=True, textplot=False)) continue maxval = df.at[df2.index[0], 'val'] df.loc[df2[df2['val'] > (maxval * 0.5)].index, 'used'] = True df.loc[df2[df2['val'] < (maxval * 0.5)].index, 'textplot'] = False dfnew = pd.DataFrame(newrows) df = df.append(dfnew) cdf = df[df['used']] tdf = df[df['textplot']] rng = [0.01, 1, 2, 3, 4, 6, 8, 12, 18, 24, 30, 36] cmap = nwssnow() m = MapPlot(sector='iowa', axisbg='white', title="Local Storm Report Snowfall Total Analysis", subtitle=("Reports past 12 hours: %s" "" % (endts.strftime("%d %b %Y %I:%M %p"), ))) m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng, cmap=cmap) m.drawcounties() m.plot_values(tdf['lon'].values, tdf['lat'].values, tdf['val'].values, fmt='%.1f') m.drawcities() pqstr = "plot c 000000000000 lsr_snowfall.png bogus png" m.postprocess(view=view, pqstr=pqstr) m.close() # slightly different title to help uniqueness m = MapPlot(sector='iowa', axisbg='white', title="Local Storm Report Snowfall Total Analysis", subtitle=("Reports valid over past 12 hours: %s" "" % (endts.strftime("%d %b %Y %I:%M %p"), ))) m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng, cmap=cmap) m.drawcounties() m.drawcities() pqstr = "plot c 000000000000 lsr_snowfall_nv.png bogus png" m.postprocess(view=view, pqstr=pqstr) m.close() m = MapPlot(sector='midwest', axisbg='white', title="Local Storm Report Snowfall Total Analysis", subtitle=("Reports past 12 hours: %s" "" % (endts.strftime("%d %b %Y %I:%M %p"), ))) m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng, cmap=cmap) m.drawcities() pqstr = "plot c 000000000000 mw_lsr_snowfall.png bogus png" m.postprocess(view=view, pqstr=pqstr) m.close()
def run(basets, endts, view): """Generate this plot for the given basets""" df = read_sql("""SELECT state, max(magnitude) as val, ST_x(geom) as lon, ST_y(geom) as lat from lsrs WHERE type in ('S') and magnitude >= 0 and valid > %s and valid < %s GROUP by state, lon, lat """, POSTGIS, params=(basets, endts), index_col=None) df['used'] = False df['textplot'] = True df.sort_values(by='val', ascending=False, inplace=True) # Now, we need to add in zeros, lets say we are looking at a .25 degree box mybuffer = 0.75 newrows = [] for lat in np.arange(reference.MW_SOUTH, reference.MW_NORTH, mybuffer): for lon in np.arange(reference.MW_WEST, reference.MW_EAST, mybuffer): df2 = df[(df['lat'] >= lat) & (df['lat'] < (lat+mybuffer)) & (df['lon'] >= lon) & (df['lon'] < (lon+mybuffer))] if len(df2.index) == 0: newrows.append(dict(lon=(lon+mybuffer/2.), lat=(lat+mybuffer/2.), val=0, used=True, textplot=False)) continue maxval = df.at[df2.index[0], 'val'] df.loc[df2[df2['val'] > (maxval * 0.5)].index, 'used'] = True df.loc[df2[df2['val'] < (maxval * 0.5)].index, 'textplot'] = False dfnew = pd.DataFrame(newrows) df = df.append(dfnew) cdf = df[df['used']] tdf = df[df['textplot']] rng = [0.01, 1, 2, 3, 4, 6, 8, 12, 18, 24, 30, 36] cmap = nwssnow() m = MapPlot(sector='iowa', axisbg='white', title="Local Storm Report Snowfall Total Analysis", subtitle=("Reports past 12 hours: %s" "" % (endts.strftime("%d %b %Y %I:%M %p"), ))) m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng, cmap=cmap) m.drawcounties() m.plot_values(tdf['lon'].values, tdf['lat'].values, tdf['val'].values, fmt='%.1f') pqstr = "plot c 000000000000 lsr_snowfall.png bogus png" m.postprocess(view=view, pqstr=pqstr) m.close() # slightly different title to help uniqueness m = MapPlot(sector='iowa', axisbg='white', title="Local Storm Report Snowfall Total Analysis", subtitle=("Reports valid over past 12 hours: %s" "" % (endts.strftime("%d %b %Y %I:%M %p"), ))) m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng, cmap=cmap) m.drawcounties() pqstr = "plot c 000000000000 lsr_snowfall_nv.png bogus png" m.postprocess(view=view, pqstr=pqstr) m.close() m = MapPlot(sector='midwest', axisbg='white', title="Local Storm Report Snowfall Total Analysis", subtitle=("Reports past 12 hours: %s" "" % (endts.strftime("%d %b %Y %I:%M %p"), ))) m.contourf(cdf['lon'].values, cdf['lat'].values, cdf['val'].values, rng, cmap=cmap) pqstr = "plot c 000000000000 mw_lsr_snowfall.png bogus png" m.postprocess(view=view, pqstr=pqstr) m.close()
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.sort_values(by='val', ascending=False, inplace=True) df['useme'] = False # First, we need to filter down the in-bound values to get rid of small cellsize = 0.33 newrows = [] for lat in np.arange(reference.MW_SOUTH, reference.MW_NORTH, cellsize): for lon in np.arange(reference.MW_WEST, reference.MW_EAST, cellsize): # Look around this box at 1x df2 = df[(df['lat'] >= (lat - cellsize)) & (df['lat'] < (lat + cellsize)) & (df['lon'] >= (lon - cellsize)) & (df['lon'] < (lon + cellsize))] if df2.empty: # If nothing was found, check 2x df3 = df[(df['lat'] >= (lat - cellsize * 2.)) & (df['lat'] < (lat + cellsize * 2.)) & (df['lon'] >= (lon - cellsize * 2.)) & (df['lon'] < (lon + cellsize * 2.))] if df3.empty: # If nothing found, place a zero here newrows.append({ 'lon': lon, 'lat': lat, 'val': 0, 'useme': True, 'state': 'NA' }) continue maxval = df.at[df2.index[0], 'val'] df.loc[df2[df2['val'] > (maxval * 0.8)].index, 'useme'] = True dfall = pd.concat([df, pd.DataFrame(newrows)], ignore_index=True) df2 = dfall[dfall['useme']] xi = np.arange(reference.MW_WEST, reference.MW_EAST, cellsize) yi = np.arange(reference.MW_SOUTH, reference.MW_NORTH, cellsize) xi, yi = np.meshgrid(xi, yi) gridder = Rbf(df2['lon'].values, df2['lat'].values, pd.to_numeric(df2['val'].values, errors='ignore'), function='thin_plate') vals = gridder(xi, yi) vals[np.isnan(vals)] = 0 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 df['val'].max() > 0: mp.contourf(xi, yi, vals, rng, cmap=cmap) mp.drawcounties() if not df.empty: mp.plot_values(df['lon'].values, df['lat'].values, df['val'].values, fmt='%.1f', labelbuffer=2) 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 df['val'].max() > 0: mp.contourf(xi, yi, vals, 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 df['val'].max() > 0: mp.contourf(xi, yi, vals, rng, cmap=cmap) mp.drawcities() pqstr = "plot c 000000000000 mw_lsr_snowfall.png bogus png" mp.postprocess(view=view, pqstr=pqstr) mp.close()
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)