def plot(argv): """Make a plot""" df = pd.read_csv("%s_maxdailyprecip.txt" % (argv[1], ), dtype={'huc12': str}) df.set_index('huc12', inplace=True) pgconn = get_dbconn('idep') huc12df = gpd.GeoDataFrame.from_postgis(""" SELECT huc12, simple_geom as geo from wbd_huc12 WHERE swat_use ORDER by huc12 """, pgconn, index_col='huc12', geom_col='geo') mp = MapPlot( sector='custom', south=34, north=48, west=-98, east=-77, title="%s Max Daily Precipitation" % (argv[1].split("_", 1)[1], ) ) bins = range(0, 201, 20) cmap = stretch_cmap('terrain_r', bins) norm = mpcolors.BoundaryNorm(bins, cmap.N) for huc12, row in huc12df.iterrows(): for poly in row['geo']: arr = np.asarray(poly.exterior) points = mp.ax.projection.transform_points(ccrs.Geodetic(), arr[:, 0], arr[:, 1]) color = cmap(norm([df.at[huc12, 'maxp'], ]))[0] poly = Polygon(points[:, :2], fc=color, ec='None', zorder=2, lw=.1) mp.ax.add_patch(poly) mp.draw_colorbar(bins, cmap, norm, units='mm') mp.postprocess(filename='test.png')
def main(): """Go Main Go.""" pgconn = get_dbconn("idep") df = read_postgis( """ with centroids as ( select huc_12, st_centroid(geom) as center, simple_geom from huc12 where scenario = 0), agg as ( select c.huc_12, sum(case when st_y(center) < st_ymax(geom) then 1 else 0 end) as west, count(*) from flowpaths f JOIN centroids c on (f.huc_12 = c.huc_12) WHERE f.scenario = 0 GROUP by c.huc_12) select a.huc_12, st_transform(c.simple_geom, 4326) as geo, a.west, a.count from agg a JOIN centroids c ON (a.huc_12 = c.huc_12) """, pgconn, index_col=None, geom_col="geo", ) df["percent"] = df["west"] / df["count"] * 100.0 bins = np.arange(0, 101, 10) cmap = plt.get_cmap("RdBu") norm = mpcolors.BoundaryNorm(bins, cmap.N) mp = MapPlot( continentalcolor="thistle", nologo=True, sector="custom", south=36.8, north=48.0, west=-99.2, east=-88.9, subtitle="", title=("DEP Flowpaths North of HUC12 Centroid (%.0f/%.0f %.2f%%)" % ( df["west"].sum(), df["count"].sum(), df["west"].sum() / df["count"].sum() * 100.0, )), ) for _i, row in df.iterrows(): c = cmap(norm([row["percent"]]))[0] arr = np.asarray(row["geo"].exterior) points = mp.ax.projection.transform_points(ccrs.Geodetic(), arr[:, 0], arr[:, 1]) p = Polygon(points[:, :2], fc=c, ec="None", zorder=2, lw=0.1) mp.ax.add_patch(p) mp.drawcounties() mp.draw_colorbar(bins, cmap, norm, title="Percent", extend="neither") mp.postprocess(filename="/tmp/huc12_north.png")
def main(): """Go Main Go.""" pgconn = get_dbconn("idep") df = read_postgis( """ select f.huc_12, count(*) as fps, st_transform(h.simple_geom, 4326) as geo from flowpaths f JOIN huc12 h on (f.huc_12 = h.huc_12) WHERE f.scenario = 0 and h.scenario = 0 GROUP by f.huc_12, geo ORDER by fps ASC """, pgconn, index_col=None, geom_col="geo", ) bins = np.arange(1, 42, 2) cmap = plt.get_cmap("copper") cmap.set_over("white") cmap.set_under("thistle") norm = mpcolors.BoundaryNorm(bins, cmap.N) mp = MapPlot( continentalcolor="thistle", nologo=True, sector="custom", south=36.8, north=45.0, west=-99.2, east=-88.9, subtitle="", title=("DEP HUCs with <40 Flowpaths (%.0f/%.0f %.2f%%)" % ( len(df[df["fps"] < 40].index), len(df.index), len(df[df["fps"] < 40].index) / len(df.index) * 100.0, )), ) for _i, row in df.iterrows(): c = cmap(norm([row["fps"]]))[0] arr = np.asarray(row["geo"].exterior) points = mp.ax.projection.transform_points(ccrs.Geodetic(), arr[:, 0], arr[:, 1]) p = Polygon(points[:, :2], fc=c, ec="None", zorder=2, lw=0.1) mp.ax.add_patch(p) mp.drawcounties() mp.draw_colorbar(bins, cmap, norm, title="Count") mp.postprocess(filename="/tmp/huc12_cnts.png")
def main(): """Go Main Go.""" pgconn = get_dbconn('idep') df = read_postgis(""" select f.huc_12, count(*) as fps, st_transform(h.simple_geom, 4326) as geo from flowpaths f JOIN huc12 h on (f.huc_12 = h.huc_12) WHERE f.scenario = 0 and h.scenario = 0 GROUP by f.huc_12, geo ORDER by fps ASC """, pgconn, index_col=None, geom_col='geo') bins = np.arange(1, 42, 2) cmap = plt.get_cmap('copper') cmap.set_over('white') cmap.set_under('thistle') norm = mpcolors.BoundaryNorm(bins, cmap.N) mp = MapPlot( continentalcolor='thistle', nologo=True, sector='custom', south=36.8, north=45.0, west=-99.2, east=-88.9, subtitle='', title=('DEP HUCs with <40 Flowpaths (%.0f/%.0f %.2f%%)' % ( len(df[df['fps'] < 40].index), len(df.index), len(df[df['fps'] < 40].index) / len(df.index) * 100. ))) for _i, row in df.iterrows(): c = cmap(norm([row['fps'], ]))[0] arr = np.asarray(row['geo'].exterior) points = mp.ax.projection.transform_points( ccrs.Geodetic(), arr[:, 0], arr[:, 1]) p = Polygon(points[:, :2], fc=c, ec='None', zorder=2, lw=0.1) mp.ax.add_patch(p) mp.drawcounties() mp.draw_colorbar( bins, cmap, norm, title='Count') mp.postprocess(filename='/tmp/huc12_cnts.png')
def make_map(ts, ts2, scenario, v): """Make the map""" import matplotlib matplotlib.use('agg') from pyiem.plot.geoplot import MapPlot import matplotlib.pyplot as plt from matplotlib.patches import Polygon import matplotlib.colors as mpcolors import cartopy.crs as ccrs # suggested for runoff and precip if v in ['qc_precip', 'avg_runoff']: c = ['#ffffa6', '#9cf26d', '#76cc94', '#6399ba', '#5558a1'] # suggested for detachment elif v in ['avg_loss']: c = ['#cbe3bb', '#c4ff4d', '#ffff4d', '#ffc44d', '#ff4d4d', '#c34dee'] # suggested for delivery elif v in ['avg_delivery']: c = ['#ffffd2', '#ffff4d', '#ffe0a5', '#eeb74d', '#ba7c57', '#96504d'] cmap = mpcolors.ListedColormap(c, 'james') cmap.set_under('white') cmap.set_over('black') pgconn = get_dbconn('idep') cursor = pgconn.cursor() title = "for %s" % (ts.strftime("%-d %B %Y"), ) if ts != ts2: title = "for period between %s and %s" % (ts.strftime("%-d %b %Y"), ts2.strftime("%-d %b %Y")) m = MapPlot(axisbg='#EEEEEE', nologo=True, sector='custom', south=36.8, north=45.0, west=-99.2, east=-88.9, title='DEP %s by HUC12 %s' % (V2NAME[v], title), caption='Daily Erosion Project') # Check that we have data for this date! cursor.execute(""" SELECT value from properties where key = 'last_date_0' """) lastts = datetime.datetime.strptime(cursor.fetchone()[0], '%Y-%m-%d') floor = datetime.date(2007, 1, 1) if ts > lastts.date() or ts2 > lastts.date() or ts < floor: m.ax.text(0.5, 0.5, "Data Not Available\nPlease Check Back Later!", transform=m.ax.transAxes, fontsize=20, ha='center') ram = cStringIO.StringIO() plt.savefig(ram, format='png', dpi=100) ram.seek(0) return ram.read(), False cursor.execute( """ WITH data as ( SELECT huc_12, sum(""" + v + """) as d from results_by_huc12 WHERE scenario = %s and valid between %s and %s GROUP by huc_12) SELECT ST_Transform(simple_geom, 4326), coalesce(d.d, 0) from huc12 i LEFT JOIN data d ON (i.huc_12 = d.huc_12) WHERE i.scenario = %s """, (scenario, ts.strftime("%Y-%m-%d"), ts2.strftime("%Y-%m-%d"), scenario)) patches = [] data = [] for row in cursor: polygon = loads(row[0].decode('hex')) a = np.asarray(polygon.exterior) points = m.ax.projection.transform_points(ccrs.Geodetic(), a[:, 0], a[:, 1]) p = Polygon(points[:, :2], fc='white', ec='k', zorder=2, lw=.1) patches.append(p) data.append(row[1]) data = np.array(data) * V2MULTI[v] if np.max(data) < 0.05: bins = [0.01, 0.02, 0.03, 0.04, 0.05] else: bins = myjenks(data, 'bah', len(c)) norm = mpcolors.BoundaryNorm(bins, cmap.N) for val, patch in zip(data, patches): c = cmap(norm([ val, ]))[0] patch.set_facecolor(c) m.ax.add_patch(patch) lbl = [round(_, 2) for _ in bins] m.draw_colorbar(bins, cmap, norm, units=V2UNITS[v], clevlabels=lbl) ram = cStringIO.StringIO() plt.savefig(ram, format='png', dpi=100) ram.seek(0) return ram.read(), True
def make_map(huc, ts, ts2, scenario, v, form): """Make the map""" projection = EPSG[5070] plt.close() # suggested for runoff and precip if v in ["qc_precip", "avg_runoff"]: # c = ['#ffffa6', '#9cf26d', '#76cc94', '#6399ba', '#5558a1'] cmap = james() # suggested for detachment elif v in ["avg_loss"]: # c =['#cbe3bb', '#c4ff4d', '#ffff4d', '#ffc44d', '#ff4d4d', '#c34dee'] cmap = dep_erosion() # suggested for delivery elif v in ["avg_delivery"]: # c =['#ffffd2', '#ffff4d', '#ffe0a5', '#eeb74d', '#ba7c57', '#96504d'] cmap = dep_erosion() pgconn = get_dbconn("idep") cursor = pgconn.cursor() title = "for %s" % (ts.strftime("%-d %B %Y"),) if ts != ts2: title = "for period between %s and %s" % ( ts.strftime("%-d %b %Y"), ts2.strftime("%-d %b %Y"), ) if "averaged" in form: title = "averaged between %s and %s (2008-2017)" % ( ts.strftime("%-d %b"), ts2.strftime("%-d %b"), ) # Check that we have data for this date! cursor.execute( "SELECT value from properties where key = 'last_date_0'", ) lastts = datetime.datetime.strptime(cursor.fetchone()[0], "%Y-%m-%d") floor = datetime.date(2007, 1, 1) if ts > lastts.date() or ts2 > lastts.date() or ts < floor: plt.text( 0.5, 0.5, "Data Not Available\nPlease Check Back Later!", fontsize=20, ha="center", ) ram = BytesIO() plt.savefig(ram, format="png", dpi=100) plt.close() ram.seek(0) return ram.read(), False if huc is None: huclimiter = "" elif len(huc) == 8: huclimiter = " and substr(i.huc_12, 1, 8) = '%s' " % (huc,) elif len(huc) == 12: huclimiter = " and i.huc_12 = '%s' " % (huc,) if "iowa" in form: huclimiter += " and i.states ~* 'IA' " if "mn" in form: huclimiter += " and i.states ~* 'MN' " if "averaged" in form: # 11 years of data is standard # 10 years is for the switchgrass one-off with get_sqlalchemy_conn("idep") as conn: df = read_postgis( f""" WITH data as ( SELECT huc_12, sum({v}) / 10. as d from results_by_huc12 WHERE scenario = %s and to_char(valid, 'mmdd') between %s and %s and valid between '2008-01-01' and '2018-01-01' GROUP by huc_12) SELECT simple_geom as geom, coalesce(d.d, 0) * %s as data from huc12 i LEFT JOIN data d ON (i.huc_12 = d.huc_12) WHERE i.scenario = %s {huclimiter} """, conn, params=( scenario, ts.strftime("%m%d"), ts2.strftime("%m%d"), V2MULTI[v], 0, ), geom_col="geom", ) else: with get_sqlalchemy_conn("idep") as conn: df = read_postgis( f""" WITH data as ( SELECT huc_12, sum({v}) as d from results_by_huc12 WHERE scenario = %s and valid between %s and %s GROUP by huc_12) SELECT simple_geom as geom, coalesce(d.d, 0) * %s as data from huc12 i LEFT JOIN data d ON (i.huc_12 = d.huc_12) WHERE i.scenario = %s {huclimiter} """, conn, params=( scenario, ts.strftime("%Y-%m-%d"), ts2.strftime("%Y-%m-%d"), V2MULTI[v], 0, ), geom_col="geom", ) minx, miny, maxx, maxy = df["geom"].total_bounds buf = 10000.0 # 10km m = MapPlot( axisbg="#EEEEEE", logo="dep", sector="custom", south=miny - buf, north=maxy + buf, west=minx - buf, east=maxx + buf, projection=projection, title="DEP %s by HUC12 %s" % (V2NAME[v], title), titlefontsize=16, caption="Daily Erosion Project", ) if ts == ts2: # Daily bins = RAMPS["english"][0] else: bins = RAMPS["english"][1] norm = mpcolors.BoundaryNorm(bins, cmap.N) for _, row in df.iterrows(): p = Polygon( row["geom"].exterior.coords, fc=cmap(norm([row["data"]]))[0], ec="k", zorder=5, lw=0.1, ) m.ax.add_patch(p) label_scenario(m.ax, scenario, pgconn) lbl = [round(_, 2) for _ in bins] if huc is not None: m.drawcounties() m.drawcities() m.draw_colorbar( bins, cmap, norm, units=V2UNITS[v], clevlabels=lbl, spacing="uniform" ) if "progressbar" in form: fig = plt.gcf() avgval = df["data"].mean() fig.text( 0.01, 0.905, "%s: %4.1f T/a" % (ts.year if "averaged" not in form else "Avg", avgval), fontsize=14, ) bar_width = 0.758 # yes, a small one off with years having 366 days proportion = (ts2 - ts).days / 365.0 * bar_width rect1 = Rectangle( (0.15, 0.905), bar_width, 0.02, color="k", zorder=40, transform=fig.transFigure, figure=fig, ) fig.patches.append(rect1) rect2 = Rectangle( (0.151, 0.907), proportion, 0.016, color=cmap(norm([avgval]))[0], zorder=50, transform=fig.transFigure, figure=fig, ) fig.patches.append(rect2) if "cruse" in form: # Crude conversion of T/a to mm depth depth = avgval / 5.0 m.ax.text( 0.9, 0.92, "%.2fmm" % (depth,), zorder=1000, fontsize=24, transform=m.ax.transAxes, ha="center", va="center", bbox=dict(color="k", alpha=0.5, boxstyle="round,pad=0.1"), color="white", ) ram = BytesIO() plt.savefig(ram, format="png", dpi=100) plt.close() ram.seek(0) return ram.read(), True