def main(): """Go Main Go.""" fig, ax = plt.subplots(1, 1) for huc12 in tqdm(MYHUCS): dfs = [] for scenario in [0, 1002]: for fn in glob.glob("/i/%s/env/%s/%s/*.env" % (scenario, huc12[:8], huc12[8:])): fpath = int(fn.split("/")[-1].split(".")[0].split("_")[1]) df = dep_utils.read_env(fn) df["fpath"] = fpath df["av_det"] = df["av_det"] * 4.163 df["scenario"] = scenario dfs.append(df) df = pd.concat(dfs) df["year"] = df["date"].dt.year gdf = (df[["year", "scenario", "fpath", "av_det"]].groupby(["year", "scenario", "fpath"]).sum()) gdf = gdf.reset_index() ptiles = np.arange(95, 100.01, 0.1) pc1 = np.percentile(gdf[gdf["scenario"] == 0]["av_det"].values, ptiles) pc2 = np.percentile(gdf[gdf["scenario"] == 1002]["av_det"].values, ptiles) ax.plot(pc1, pc2) ax.scatter(pc1[-1], pc2[-1], s=40, marker="s", color="b", zorder=3) ax.grid(True) ax.set_title(("95th Percentile and Higher Yearly Soil Delivery\n" "Max value shown by dot")) ax.set_xlabel("Production Yearly Soil Delivery T/a") ax.set_ylabel("New Flowpaths Yearly Soil Delivery T/a") ymax = max([ax.get_xlim()[1], ax.get_ylim()[1]]) ax.plot([0, ymax], [0, ymax], color="k", lw=2, label="x=y") fig.savefig("/tmp/test.png")
def main(): """Plot""" hits = {} for line in open('KBRO.log'): tokens = line.split() ts = datetime.datetime.strptime(tokens[3].split()[0], "[%d/%b/%Y:%H:%M:%S") yyyymmdd = ts.strftime("%Y%m%d") if yyyymmdd not in hits: hits[yyyymmdd] = 0 hits[yyyymmdd] += 1 keys = hits.keys() keys.sort() xs = [] ys = [] for yyyymmdd in keys: if yyyymmdd == '20170825': continue date = datetime.datetime.strptime(yyyymmdd, '%Y%m%d') xs.append(date) ys.append(hits[yyyymmdd]) (fig, ax) = plt.subplots(1, 1) ax.bar(xs, ys) ax.grid(True) ax.set_ylabel("Website Requests") ax.set_xlim(datetime.datetime(2017, 7, 23, 12), datetime.datetime(2017, 8, 25)) ax.set_title( ("IEM NEXRAD Level II Download Website (24 Jul - 24 Aug 2017)\n" "Daily Requests for KBRO Brownsville Level II Data") ) ax.xaxis.set_major_formatter(mdates.DateFormatter("%-d\n%b")) fig.savefig('test.png')
def plotter(fdict): """ Go """ ctx = get_context(fdict) df = ctx["df"] if df.empty: raise NoDataFound("Error, no results returned!") (fig, ax) = plt.subplots(1, 1) ax.set_title("%s\n%s" % (ctx["title"], ctx["subtitle"])) ax.bar(df.index.values, df["count"], align="center", fc="green", ec="green") if ctx["year"] in df.index: ax.bar( ctx["year"], df.at[ctx["year"], "count"], align="center", fc="red", ec="red", zorder=5, ) ax.grid(True) ax.set_ylabel("Days Per Period") ax.set_xlim(df.index.min() - 0.5, df.index.max() + 0.5) avgv = df["count"].mean() ax.axhline(avgv) ax.text(df.index.max() + 1, avgv, "%.1f" % (avgv, )) return fig, df
def process(uri): """Process this request This should look something like "/onsite/features/2016/11/161125.png" """ if uri is None: send_content_type("text") ssw("ERROR!") return match = PATTERN.match(uri) if match is None: send_content_type("text") ssw("ERROR!") sys.stderr.write("feature content failure: %s\n" % (repr(uri), )) return data = match.groupdict() fn = ("/mesonet/share/features/%(yyyy)s/%(mm)s/" "%(yymmdd)s%(extra)s.%(suffix)s") % data if os.path.isfile(fn): send_content_type(data['suffix']) ssw(open(fn, 'rb').read()) dblog(data['yymmdd']) else: send_content_type('png') from io import BytesIO from pyiem.plot.use_agg import plt (_, ax) = plt.subplots(1, 1) ax.text(0.5, 0.5, "Feature Image was not Found!", transform=ax.transAxes, ha='center') plt.axis('off') ram = BytesIO() plt.savefig(ram, format='png') ram.seek(0) ssw(ram.read())
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] month = ctx['month'] mydir = ctx['dir'] ts = datetime.datetime(2000, month, 1) ets = ts + datetime.timedelta(days=35) ets = ets.replace(day=1) days = int((ets - ts).days) table = "alldata_%s" % (station[:2], ) s = "ASC" if mydir == 'cold' else 'DESC' df = read_sql(""" with ranks as ( select day, high, low, rank() OVER (PARTITION by year ORDER by high """ + s + """) as high_rank, rank() OVER (PARTITION by year ORDER by low """ + s + """) as low_rank from """ + table + """ where station = %s and month = %s), highs as ( SELECT extract(day from day) as dom, count(*) from ranks where high_rank = 1 GROUP by dom), lows as ( SELECT extract(day from day) as dom, count(*) from ranks where low_rank = 1 GROUP by dom) select coalesce(h.dom, l.dom) as dom, h.count as high_count, l.count as low_count from highs h FULL OUTER JOIN lows l on (h.dom = l.dom) ORDER by h.dom """, pgconn, params=(station, month)) fig, ax = plt.subplots(2, 1, sharex=True) lbl = 'Coldest' if mydir == 'cold' else 'Hottest' ax[0].set_title( ("[%s] %s\nFrequency of Day in %s\n" "Having %s High Temperature of %s") % (station, ctx['_nt'].sts[station]['name'], calendar.month_name[month], lbl, calendar.month_name[month]), fontsize=10) ax[0].set_ylabel("Years (ties split)") ax[0].grid(True) ax[0].bar(df['dom'], df['high_count'], align='center') ax[1].set_title(("Having %s Low Temperature of %s") % ( lbl, calendar.month_name[month], ), fontsize=10) ax[1].set_ylabel("Years (ties split)") ax[1].grid(True) ax[1].set_xlabel("Day of %s" % (calendar.month_name[month], )) ax[1].bar(df['dom'], df['low_count'], align='center') ax[1].set_xlim(0.5, days + 0.5) return fig, df
def make_battery_plot(ctx): """Generate a plot of battery""" icursor = ctx['pgconn'].cursor(cursor_factory=psycopg2.extras.DictCursor) icursor.execute("""SELECT valid, battv_min_qc from sm_hourly where station = '%s' and valid >= '%s 00:00' and valid < '%s 23:56' and battv_min_qc is not null ORDER by valid ASC """ % (ctx['station'], ctx['sts'].strftime("%Y-%m-%d"), ctx['ets'].strftime("%Y-%m-%d"))) dates = [] battv = [] for row in icursor: dates.append(row[0]) battv.append(row[1]) df = pd.DataFrame(dict(dates=dates, battv=battv)) (fig, ax) = plt.subplots(1, 1, figsize=(8, 6)) ax.plot(dates, battv) ax.grid(True) ax.set_ylabel("Battery Voltage [V]") ax.set_title( ("ISUSM Station: %s Timeseries\n" "Battery Voltage") % (ctx['nt'].sts[ctx['station']]['name'], )) ax.xaxis.set_major_formatter(mdates.DateFormatter('%-d %b\n%Y')) # interval = len(dates) / 7 + 1 # ax.xaxis.set_major_locator(mdates.DayLocator(interval=interval)) ax.legend(loc='best', ncol=2, fontsize=10) return fig, df
def main(): """Go Main Go.""" (fig, ax) = plt.subplots(1, 1) ax.set_position([0.1, 0.2, 0.85, 0.75]) for scenario in THRESHOLDS: df = pd.read_csv("scenario%s_dates.txt" % (scenario, )) df["date"] = pd.to_datetime(df["date"]) ax.plot( df["date"].values, df["total"].values / df["total"].max() * 100.0, label="%.0f%%" % (THRESHOLDS[scenario], ), ) for scenario in PL_THRESHOLDS: df = pd.read_csv("scenario%s_dates.txt" % (scenario, )) df["date"] = pd.to_datetime(df["date"]) if scenario != 90: label = "PL%.1f" % (PL_THRESHOLDS[scenario], ) else: label = "PL(SAT)" ax.plot( df["date"].values, df["total"].values / df["total"].max() * 100.0, label=label, linestyle="-.", ) ax.grid(True) ax.xaxis.set_major_formatter(mdates.DateFormatter("%-d %b")) ax.set_xlabel("Date of 2018, tillage done on 30 May if threshold unmeet.") ax.set_ylabel("Percentage of Flowpaths Tilled") ax.set_title("DEP Tillage Timing based on 0-10cm VWC") ax.legend(loc=(-0.02, -0.26), ncol=5) ax.set_yticks([0, 5, 10, 25, 50, 75, 90, 95, 100]) ax.set_ylim(0, 100.1) fig.savefig("test.png")
def plotter(fdict): """ Go """ pgconn = get_dbconn('asos') ctx = get_autoplot_context(fdict, get_description()) station = ctx['zstation'] network = ctx['network'] hour = ctx['hour'] t1 = ctx['t1'] t2 = ctx['t2'] nt = NetworkTable(network) df = read_sql(""" WITH obs as ( SELECT date_trunc('hour', valid) as t, round(avg(tmpf)::numeric, 0) as tmp from alldata WHERE station = %s and (extract(minute from valid) > 50 or extract(minute from valid) = 10) and extract(hour from valid at time zone 'UTC') = %s and tmpf is not null GROUP by t ) SELECT extract(month from t) as month, sum(case when tmp >= %s and tmp <= %s then 1 else 0 end)::int as hits, sum(case when tmp > %s then 1 else 0 end) as above, sum(case when tmp < %s then 1 else 0 end) as below, count(*) from obs GROUP by month ORDER by month ASC """, pgconn, params=(station, hour, t1, t2, t2, t1), index_col='month') df['freq'] = df['hits'] / df['count'] * 100. df['above_freq'] = df['above'] / df['count'] * 100. df['below_freq'] = df['below'] / df['count'] * 100. (fig, ax) = plt.subplots(1, 1, figsize=(8, 6)) ax.scatter(df.index.values, df['below_freq'], marker='s', s=40, label="Below %s" % (t1,), color='b', zorder=3) bars = ax.bar(np.arange(1, 13), df['freq'], fc='tan', label='%s - %s' % (t1, t2), zorder=2, align='center') ax.scatter(df.index.values, df['above_freq'], marker='s', s=40, label="Above %s" % (t2,), color='r', zorder=3) for i, _bar in enumerate(bars): value = df.loc[i+1, 'hits'] label = "%.1f%%" % (_bar.get_height(),) if value == 0: label = 'None' ax.text(i+1, _bar.get_height()+3, label, ha='center', fontsize=12, zorder=4) ax.set_xticks(range(0, 13)) ax.set_xticklabels(calendar.month_abbr) ax.grid(True) ax.set_ylim(0, 100) ax.set_yticks([0, 25, 50, 75, 100]) ax.set_ylabel("Frequency [%]") ut = utc(2000, 1, 1, hour, 0) localt = ut.astimezone(pytz.timezone(nt.sts[station]['tzname'])) ax.set_xlim(0.5, 12.5) ax.set_title(("%s [%s]\nFrequency of %s UTC (%s LST) " r"Temp between %s$^\circ$F and %s$^\circ$F" ) % (nt.sts[station]['name'], station, hour, localt.strftime("%-I %p"), t1, t2)) ax.legend(loc=(0.05, -0.14), ncol=3, fontsize=14) pos = ax.get_position() ax.set_position([pos.x0, pos.y0 + 0.07, pos.width, pos.height * 0.93]) return fig, df
def make_vsm_histogram_plot(ctx): """Option 6""" df = read_sql(""" SELECT CASE WHEN t12_c_avg_qc > 1 then calc_vwc_12_avg_qc else null end as v12, CASE WHEN t24_c_avg_qc > 1 then calc_vwc_24_avg_qc else null end as v24, CASE WHEN t50_c_avg_qc > 1 then calc_vwc_50_avg_qc else null end as v50 from sm_hourly where station = %s and valid >= %s and valid < %s """, ctx['pgconn'], params=(ctx['station'], ctx['sts'], ctx['ets']), index_col=None) (fig, ax) = plt.subplots(3, 1, sharex=True) ax[0].set_title( ("ISUSM Station: %s VWC Histogram\n" "For un-frozen condition between %s and %s") % (ctx['nt'].sts[ctx['station']]['name'], ctx['sts'].strftime("%-d %b %Y"), ctx['ets'].strftime("%-d %b %Y"))) for i, col in enumerate(['v12', 'v24', 'v50']): ax[i].hist(df[col] * 100., bins=50, range=(0, 50), density=True) ax[i].set_ylabel("Frequency") ax[i].grid(True) ax[i].text(0.99, 0.99, "%s inches" % (col[1:], ), transform=ax[i].transAxes, ha='right', va='top') ax[i].set_yscale("log") ax[2].set_xlabel("Volumetric Water Content [%]") return fig, df
def plotter(fdict): """ Go """ ctx = get_context(fdict) (fig, ax) = plt.subplots(1, 1) ax.bar(ctx['df'].index.values, ctx['df'][ctx['varname']], fc='r', ec='r', align='center') mean = ctx['df'][ctx['varname']].mean() ax.axhline(mean) ax.set_title("%s\non %s" % (ctx['title'], ctx['subtitle'])) ax.text(ctx['df'].index.values[-1] + 1, mean, '%.2f' % (mean, ), ha='left', va='center') ax.grid(True) ax.set_xlim(ctx['df'].index.values.min() - 1, ctx['df'].index.values.max() + 1) ax.set_ylabel(PDICT2[ctx['varname']]) if ctx['varname'] != 'precip': ax.set_ylim(ctx['df'][ctx['varname']].min() - 5, ctx['df'][ctx['varname']].max() + 5) return fig, ctx['df']
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) get_df(ctx) (fig, ax) = plt.subplots() hty = ctx['df']['HT.Y%'] ax.bar(ctx['df'].index.values, hty, label='Heat Advisory', color=NWS_COLORS['HT.Y']) ehw = ctx['df']['EH.W%'] ax.bar(ctx['df'].index.values, ehw, bottom=hty, label='Extreme Heat Warning', color=NWS_COLORS['EH.W']) non = ctx['df']['None%'] ax.bar(ctx['df'].index.values, non, bottom=(hty + ehw), label='No Headline', color='#EEEEEE') ax.legend(loc=(-0.03, -0.22), ncol=3) ax.set_position([0.1, 0.2, 0.8, 0.7]) ax.grid(True) ax.set_xlabel(( r"Heat Index $^\circ$F, %s" ) % ("All Obs Considered" if ctx['opt'] == 'no' else "Only Obs with HI > Temp Considered")) ax.set_ylabel("Frequency [%]") ax.set_yticks([0, 5, 10, 25, 50, 75, 90, 95, 100]) ax.set_title(ctx['title']) return fig, ctx['df']
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] date = ctx['date'] df = get_data(fdict) if df.empty: raise NoDataFound('Error, no results returned!') (fig, ax) = plt.subplots(1, 1) ax.scatter(df['before'].values, df['after'].values) ax.set_xlim(left=-0.1) ax.set_ylim(bottom=-0.1) ax.set_xlabel("Snowfall Total [inch] Prior to %s" % (date.strftime("%-d %b"), )) ax.set_ylabel("Snowfall Total [inch] On and After %s" % (date.strftime("%-d %b"), )) ax.grid(True) ax.set_title( ("%s [%s] Snowfall Totals\nPrior to and after: %s") % (ctx['_nt'].sts[station]['name'], station, date.strftime("%-d %B"))) ax.axvline(df['before'].mean(), color='r', lw=2, label='Before Avg: %.1f' % (df['before'].mean(), )) ax.axhline(df['after'].mean(), color='b', lw=2, label='After Avg: %.1f' % (df['after'].mean(), )) ax.legend(ncol=2, fontsize=12) return fig, df
def plotter(fdict): """ Go """ ctx = get_context(fdict) (fig, ax) = plt.subplots(1, 1) for year in ctx['df'].index.values: s = ctx['df'].loc[[year]].transpose() s = s.dropna().astype('timedelta64[h]') ax.plot(s.index.values, s[year].values / 24., c='tan') if 'season' in ctx['lines']: ax.plot(ctx['lines']['season']['x'], ctx['lines']['season']['y'], c=ctx['lines']['season']['c'], zorder=5, label=ctx['lines']['season']['label'], lw=2) for lbl in ['25%', 'mean', '75%']: ax.plot(ctx['lines'][lbl]['x'], ctx['lines'][lbl]['y'], c=ctx['lines'][lbl]['c'], zorder=2, label=ctx['lines'][lbl]['label'], lw=2) ax.legend(loc=2) ax.grid(True) ax.set_xlim(-50, 32) ax.set_xlabel(r"Wind Chill Temperature $^\circ$F") ax.set_ylabel("Total Time Hours [expressed in days]") ax.set_title("%s\n%s" % (ctx['title'], ctx['subtitle'])) return fig, ctx['df']
def plotter(fdict): """ Go """ ctx = get_context(fdict) df = ctx['df'] if df.empty: raise NoDataFound('Error, no results returned!') (fig, ax) = plt.subplots(1, 1) ax.set_title("%s\n%s" % (ctx['title'], ctx['subtitle'])) ax.bar(df.index.values, df['count'], align='center', fc='green', ec='green') if ctx['year'] in df.index: ax.bar(ctx['year'], df.at[ctx['year'], 'count'], align='center', fc='red', ec='red', zorder=5) ax.grid(True) ax.set_ylabel("Days Per Period") ax.set_xlim(df.index.min() - 0.5, df.index.max() + 0.5) avgv = df['count'].mean() ax.axhline(avgv) ax.text(df.index.max() + 1, avgv, "%.1f" % (avgv, )) return fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'].upper() table = "alldata_%s" % (station[:2], ) df = read_sql(""" SELECT year, month, avg(high) as avg_high_all, avg(low) as avg_low_all, avg(case when snowd > 0 then high else null end) as avg_high_snow, avg(case when snowd > 0 then low else null end) as avg_low_snow, avg(case when snowd = 0 then high else null end) as avg_high_nosnow, avg(case when snowd = 0 then low else null end) as avg_low_nosnow, sum(case when snowd > 0 then 1 else 0 end) as coverdays from """ + table + """ WHERE station = %s GROUP by year, month """, pgconn, params=(station, ), index_col=None) if df.empty: raise NoDataFound("No data found.") # Only use months that had at least one day of snowcover df2 = df[df['coverdays'] > 0] df3 = df2.groupby('month').mean() (fig, ax) = plt.subplots(2, 1) for i, lbl in enumerate(['high', 'low']): ys = df3.loc[[11, 12, 1, 2, 3], 'avg_%s_nosnow' % (lbl, )] ax[i].bar(np.arange(5) - 0.2, ys.values, width=0.4, align='center', label='Without Snowcover', fc='brown', zorder=4) for x, y in enumerate(ys): ax[i].text(x - 0.2, y + 2, "%.0f" % (y, ), ha='center', color='brown') ys2 = df3.loc[[11, 12, 1, 2, 3], 'avg_%s_snow' % (lbl, )] ax[i].bar(np.arange(5) + 0.2, ys2.values, width=0.4, align='center', label='With Snowcover', fc='blue', zorder=4) for x, y in enumerate(ys2): ax[i].text(x + 0.2, y + 2, "%.0f" % (y, ), ha='center', color='blue') ys3 = df3.loc[[11, 12, 1, 2, 3], 'avg_%s_all' % (lbl, )] ax[i].scatter(np.arange(5), ys3.values, marker='s', s=50, zorder=5, label='Overall', c='yellow') for x, y in enumerate(ys3): ax[i].text(x - 0.05, y, "%.0f" % (y, ), ha='right', zorder=6, va='top', color='yellow') ax[i].set_xticks(range(5)) ax[i].set_xticklabels(['Nov', 'Dec', 'Jan', 'Feb', 'Mar']) ax[i].legend(ncol=3, fontsize=10) ax[i].grid(True) ax[i].set_ylim([(ys2.min() - 10), (ys.max() + 20)]) ax[0].set_title(("%s [%s]\nSnow Cover Impact on Average Temp [%s-%s]" ) % (ctx['_nt'].sts[station]['name'], station, df2['year'].min(), df2['year'].max())) ax[0].set_ylabel(r"Avg High Temp $^\circ$F") ax[1].set_ylabel(r"Avg Low Temp $^\circ$F") return fig, df
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) get_data(ctx) df = ctx["df"] fig, ax = plt.subplots(1, 1) colname = f"{ctx['var']}_{ctx['year']}" x = df.index.values ax.plot(x, df[colname].values, label=str(ctx["year"]), zorder=4, color="r") ax.fill_between( x, df[ctx["var"], "min"].values, df[ctx["var"], "max"].values, zorder=1, color="lightblue", ) ax.fill_between( x, df[ctx["var"], "25%"].values, df[ctx["var"], "75%"].values, zorder=2, color="tan", label="25-75 %tile", ) ax.plot(x, df[ctx["var"], "mean"].values, color="k", zorder=3, label="Avg") ax.legend() ax.set_ylabel(PDICT3[ctx["var"]]) plt.gcf().text(0.5, 0.9, ctx["title"], ha="center", va="bottom") ax.grid(True) return fig, df
def plotter(fdict): """ Go """ ctx = get_context(fdict) if ctx['df'].empty and ctx['odf'].empty: raise ValueError("No Data Found!") df = ctx['df'] (fig, ax) = plt.subplots(1, 1, figsize=(10, 6)) fxs = df['id'].unique() for fx in fxs: df2 = df[df['id'] == fx] issued = df2.iloc[0]['issued'].strftime("%-m/%-d %Hz") ax.plot(df2['valid'], df2[ctx['var'] + '_value'], zorder=2, label=issued) if not ctx['odf'].empty: ax.plot(ctx['odf'].index.values, ctx['odf'][ctx[ctx['var']]], lw=2, color='k', label='Obs', zorder=4) ax.set_ylabel(ctx[ctx['var']]) ax.xaxis.set_major_formatter(mdates.DateFormatter('%-d %b\n%Y')) pos = ax.get_position() ax.grid(True) ax.set_title("%s\n%s" % (ctx['title'], ctx['subtitle'])) ax.set_position([pos.x0, pos.y0, 0.6, 0.8]) ax.legend(loc=(1.0, 0.0)) return fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] days = ctx['days'] nt = NetworkTable("CSCAP") clstation = nt.sts[station]['climate_site'] (model, scenario) = ctx['model'].split("=") (fig, ax) = plt.subplots(1, 1) df = read_sql(""" WITH data as ( SELECT day, sum(precip) OVER (ORDER by day ASC ROWS BETWEEN %s preceding and current row) from hayhoe_daily WHERE precip is not null and station = %s and model = %s and scenario = %s ) SELECT extract(year from day) as yr, sum(case when sum < 0.01 then 1 else 0 end) as precip from data WHERE extract(month from day) in (3,4,5,6,7,8) GROUP by yr ORDER by yr ASC """, pgconn, params=(days - 1, clstation, model, scenario), index_col='yr') ax.bar(df.index.values, df['precip'].values, ec='b', fc='b') ax.grid(True) ax.set_ylabel("Days Per Year") ax.set_title( ("%s %s\n%s %s :: Spring/Summer with No Precip over %s days") % (station, nt.sts[station]['name'], model, scenario, days)) return fig, df
def main(): """Go Main Go.""" myhucs = [x.strip() for x in open("myhucs.txt").readlines()] pgconn = get_dbconn("idep") df = read_sql( """ SELECT extract(year from valid), extract(doy from valid)::int as doy, qc_precip from results_by_huc12 where scenario = 0 and valid < '2019-01-01' and valid >= '2008-01-01' and huc_12 in %s """, pgconn, params=(tuple(myhucs),), ) (fig, ax) = plt.subplots(1, 1) # How many to average by total = 30.0 * 11.0 # Create two week windows before and inclusive after a given date top = [] bottom = [] idxs = [] for date in pd.date_range("2000/4/1", "2000/5/31"): idx = int(date.strftime("%j")) idxs.append(idx) sdoy = int((date - datetime.timedelta(days=20)).strftime("%j")) edoy = int((date + datetime.timedelta(days=20)).strftime("%j")) before = df[(df["doy"] >= sdoy) & (df["doy"] < idx)].sum() / total bottom.append(0 - before["qc_precip"]) after = df[(df["doy"] >= idx) & (df["doy"] < edoy)].sum() / total top.append(after["qc_precip"]) ax.bar(idxs, top, label="After Date") ax.bar(idxs, bottom, label="Before Date") ax.legend(loc=2, ncol=2) xticks = [] xticklabels = [] for date in pd.date_range("2000/4/5", "2000/5/30", freq="5d"): xticks.append(int(date.strftime("%j"))) xticklabels.append(date.strftime("%b\n%-d")) ax.set_xticks(xticks) ax.set_xticklabels(xticklabels) ax.set_xlim(xticks[0] - 6, xticks[-1] + 3) # Force labels to update ax.set_ylim(-105, 105) ax.set_yticklabels(ax.get_yticks()) labels = [] for y in ax.get_yticklabels(): val = float(y.get_text()) if val < 0: val = 0 - val labels.append("%.0f" % (val,)) ax.set_yticklabels(labels) ax.grid(True) ax.set_title("2008-2018 HUC12 Precipitation for 14 Day Periods") ax.set_ylabel("Precipitation per Year [mm]") fig.savefig("test.png")
def plotter(fdict): """ Go """ ctx = get_data(fdict) (fig, ax) = plt.subplots(1, 1) xticks = [] xticklabels = [] now = ctx['sdate'] cdates = [] chighs = [] clows = [] while ctx['climo'] is not None and now <= ctx['edate']: cdates.append(now) chighs.append(ctx['climo'][now.strftime("%m%d")]['high']) clows.append(ctx['climo'][now.strftime("%m%d")]['low']) if now.day == 1 or (now.day % 12 == 0 and ctx['days'] < 180): xticks.append(now) fmt = "%-d" if now.day == 1: fmt = "%-d\n%b" xticklabels.append(now.strftime(fmt)) now += datetime.timedelta(days=1) while now <= ctx['edate']: if now.day == 1 or (now.day % 12 == 0 and ctx['days'] < 180): xticks.append(now) fmt = "%-d" if now.day == 1: fmt = "%-d\n%b" xticklabels.append(now.strftime(fmt)) now += datetime.timedelta(days=1) if chighs: chighs = np.array(chighs) clows = np.array(clows) ax.bar(cdates, chighs - clows, bottom=clows, fc='lightblue', ec='lightblue', label="Daily Climatology") ax.plot(ctx['df'].index.values, ctx['df']['datum'], color='r', label='Hourly Obs') ax.set_ylabel("%s %s" % (MDICT[ctx['var']], UNITS[ctx['var']])) ax.set_xticks(xticks) ax.set_xticklabels(xticklabels) ax.set_xlim(ctx['sdate'], ctx['edate']) ax.set_ylim(top=ctx['df'].datum.max() + 15.) ax.legend(loc=2, ncol=2) ax.axhline(32, linestyle='-.') ax.grid(True) ax.set_title(("%s [%s]\n%s Timeseries %s - %s") % (ctx['nt'].sts[ctx['station']]['name'], ctx['station'], MDICT[ctx['var']], ctx['sdate'].strftime("%d %b %Y"), ctx['edate'].strftime("%d %b %Y"))) return fig, ctx['df']
def plotter(fdict): """ Go """ ctx = get_context(fdict) (fig, ax) = plt.subplots(1, 1, figsize=(8, 6)) df = ctx['df'] ax.bar(df.index.values, df['rank'], fc='tan', zorder=1, ec='orange', align='edge', width=0.4) ax.set_title("%s\n%s" % (ctx['title'], ctx['subtitle'])) ax.grid(True) ax.set_ylabel("Percentile [%]", color='tan') ax.set_ylim(0, 105) ax2 = ax.twinx() ax2.bar(df.index.values, df['return_interval'], width=-0.4, align='edge', fc='blue', ec='k', zorder=1) ax2.set_ylabel("Return Interval (years) (%.2f Days per Year)" % ( df['avg_days'].sum(),), color='b') ax2.set_ylim(0, df['return_interval'].max() * 1.1) ax.set_xticks(range(1, 13)) ax.set_xticklabels(calendar.month_abbr[1:]) ax.set_xlim(0.5, 12.5) for idx, row in df.iterrows(): ax.text(idx, row['rank'], "%.1f" % (row['rank'],), ha='left', color='k', zorder=5, fontsize=11) if not np.isnan(row['return_interval']): ax2.text(idx, row['return_interval'], "%.1f" % (row['return_interval'],), ha='right', color='k', zorder=5, fontsize=11) else: ax2.text(idx, 0., "n/a", ha='right') return fig, df
def plotter(fdict): """ Go """ ctx = get_context(fdict) if "df" not in ctx or (ctx["df"].empty and ctx["odf"].empty): raise NoDataFound("No Data Found!") df = ctx["df"] (fig, ax) = plt.subplots(1, 1, figsize=(10, 6)) fxs = df["id"].unique() for fx in fxs: df2 = df[df["id"] == fx] issued = df2.iloc[0]["issued"].strftime("%-m/%-d %Hz") ax.plot(df2["valid"], df2[ctx["var"] + "_value"], zorder=2, label=issued) if not ctx["odf"].empty: ax.plot( ctx["odf"].index.values, ctx["odf"][ctx[ctx["var"]]], lw=2, color="k", label="Obs", zorder=4, ) ax.set_ylabel(ctx[ctx["var"]]) ax.xaxis.set_major_formatter(mdates.DateFormatter("%-d %b\n%Y")) pos = ax.get_position() ax.grid(True) ax.set_title("%s\n%s" % (ctx["title"], ctx["subtitle"])) ax.set_position([pos.x0, pos.y0, 0.6, 0.8]) ax.legend(loc=(1.0, 0.0)) df["issued"] = df["issued"].apply(lambda x: x.strftime("%Y-%m-%d %H:%M")) df["valid"] = df["valid"].apply(lambda x: x.strftime("%Y-%m-%d %H:%M")) return fig, df
def main(): """Go Main Go""" pgconn = get_dbconn('asos') dfin = read_sql(""" with mob as ( select date_trunc('hour', valid) as ts, avg(dwpf) from alldata where station = 'MOB' and dwpf is not null GROUP by ts), cmi as ( select date_trunc('hour', valid) as ts, avg(dwpf) from alldata where station = 'CMI' and dwpf is not null GROUP by ts), agg as ( select m.ts, m.avg as dwpf, c.avg as tmpf from mob m JOIN cmi c on (m.ts = c.ts)) select extract(month from ts) as month, extract(hour from ts) as hour, sum(case when dwpf >= tmpf then 1 else 0 end) / count(*)::float * 100. as freq from agg GROUP by month, hour ORDER by month, hour """, pgconn, index_col=None) df = dfin.pivot("month", "hour", "freq") fig, ax = plt.subplots(figsize=(9, 6)) ax.set_title(("Hourly Frequency of Mobile (MOB) Dew Point\n" "greater than or equal to Champaign (CMI) Dew Point")) sns.heatmap(df, annot=True, fmt=".0f", linewidths=.5, ax=ax, vmin=5, vmax=100) print(ax.get_yticks()) ax.set_xlabel("Hour of Day (CDT or CST)") ax.set_xticklabels(["Mid", "1AM", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "Noon", "1PM", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]) ax.set_yticklabels(calendar.month_abbr[1:]) fig.savefig('test.png')
def plot(): """Go Plot Go.""" dfs = {} for scenario in [0, 36, 37, 38]: dfs[scenario] = pd.read_csv( '/tmp/s%s.csv' % (scenario, )).set_index('sday') dfs[scenario]['accum'] = dfs[scenario]['avg'].cumsum() for i, sday in enumerate(dfs[0].index.values): if i == 0: continue (fig, ax) = plt.subplots(1, 1) for scenario in [0, 36, 37, 38]: df = dfs[scenario] ax.plot( range(i), df.iloc[:i]['avg'], label=SCENARIOS[scenario], lw=2) ax.set_xlim(0, 366) ax.set_ylim(0, 0.2) ax.grid(True) ax.legend(loc=2) ax.set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 365)) ax.set_xticklabels(calendar.month_abbr[1:]) ax.set_ylabel("Hillslope Soil Loss [T/a/day]") ax.set_title("2008-2017 DEP Daily Average Hillslope Soil Loss") fig.savefig('/tmp/frames/%05i.png' % (i - 1, )) plt.close()
def plotter(fdict): """ Go """ ctx = get_context(fdict) (fig, ax) = plt.subplots(1, 1) for year in ctx["df"].index.values: s = ctx["df"].loc[[year]].transpose() s = s.dropna().astype("timedelta64[h]") ax.plot(s.index.values, s[year].values / 24.0, c="tan") if "season" in ctx["lines"]: ax.plot( ctx["lines"]["season"]["x"], ctx["lines"]["season"]["y"], c=ctx["lines"]["season"]["c"], zorder=5, label=ctx["lines"]["season"]["label"], lw=2, ) for lbl in ["25%", "mean", "75%"]: ax.plot( ctx["lines"][lbl]["x"], ctx["lines"][lbl]["y"], c=ctx["lines"][lbl]["c"], zorder=2, label=ctx["lines"][lbl]["label"], lw=2, ) ax.legend(loc=2) ax.grid(True) ax.set_xlim(-50, 32) ax.set_xlabel(r"Wind Chill Temperature $^\circ$F") ax.set_ylabel("Total Time Hours [expressed in days]") ax.set_title("%s\n%s" % (ctx["title"], ctx["subtitle"])) return fig, ctx["df"]
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) daythres = ctx['daythres'] trailthres = ctx['trailthres'] period = ctx['period'] state = ctx['state'][:2] df = get_data(ctx) (fig, ax) = plt.subplots(2, 1, sharex=True, figsize=(10, 7)) ax[0].bar(ctx['days'], df['coverage'], fc='g', ec='g', zorder=1, label='Daily %.2fin' % (daythres, )) ax[0].bar(ctx['days'], df['hits'], fc='b', ec='b', zorder=2, label='Over "Dry" Areas') ax[0].legend(loc=2, ncol=2, fontsize=10) ax[0].set_title(("IEM Estimated Areal Coverage Percent of %s\n" " receiving daily %.2fin vs trailing %s day %.2fin" ) % (reference.state_names[state], daythres, period, trailthres)) ax[0].set_ylabel("Areal Coverage [%]") ax[0].grid(True) ax[1].bar(ctx['days'], df['needed'], fc='tan', ec='tan', zorder=1) ax[1].bar(ctx['days'], df['efficiency'], fc='b', ec='b', zorder=2) ax[1].xaxis.set_major_formatter(mdates.DateFormatter('%-d %b\n%Y')) ax[1].grid(True) ax[1].set_ylabel("Areal Coverage [%]") ax[1].set_title(("Percentage of Dry Area (tan) below (%.2fin over %s days)" " got %.2fin precip (blue) that day" ) % (trailthres, period, daythres), fontsize=12) return fig, df
def main(argv): """Get the tillage date.""" huc12 = argv[1] fpath = argv[2] wbfn = "/i/59/wb/%s/%s/%s_%s.wb" % (huc12[:8], huc12[8:], huc12, fpath) wbdf = read_wb(wbfn) wbdf = wbdf[wbdf["ofe"] == 1] (fig, ax) = plt.subplots(1, 1) for threshold in range(15, 50, 5): df = wbdf[wbdf["sw1"] < threshold].groupby("jday").count() df = df.reindex(list(range(1, 367))).fillna(0) vals = (df["sw1"].rolling(7).mean() / 13.0 * 100, ) ax.plot(df.index.values, vals[0], label="%s%%" % (threshold, )) ax.set_xticks([1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]) ax.set_xticklabels(calendar.month_abbr[1:]) ax.grid(True) ax.set_xlabel("* 7 Day Smoother Applied") ax.set_xlim(90, 153) ax.set_ylabel("Frequency [%]") ax.set_title( "huc12: %s fpath: %s\n2007-2019 Frequency of 0-10 cm Soil Moisture below threshold" % (huc12, fpath)) ax.legend() fig.savefig("test.png")
def plotter(fdict): """ Go """ pgconn = get_dbconn("coop") cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) ctx = get_autoplot_context(fdict, get_description()) station = ctx["station"] threshold = ctx["threshold"] varname = ctx["var"] table = "alldata_%s" % (station[:2], ) cursor.execute( """ SELECT extract(doy from day)::int as d, high, low, day from """ + table + """ where station = %s ORDER by day ASC """, (station, ), ) maxperiod = [0] * 367 enddate = [""] * 367 running = 0 col = "high" if varname.find("high") == 0 else "low" myfunc = greater_than_or_equal if varname.find("over") > 0 else less_than for row in cursor: doy = row["d"] if myfunc(row[col], threshold): running += 1 else: running = 0 if running > maxperiod[doy]: maxperiod[doy] = running enddate[doy] = row["day"] sts = datetime.datetime(2012, 1, 1) xticks = [] for i in range(1, 13): ts = sts.replace(month=i) xticks.append(int(ts.strftime("%j"))) df = pd.DataFrame( dict( mmdd=pd.date_range("1/1/2000", "12/31/2000").strftime("%m%d"), jdate=pd.Series(np.arange(1, 367)), maxperiod=pd.Series(maxperiod[1:]), enddate=pd.Series(enddate[1:]), )) (fig, ax) = plt.subplots(1, 1, sharex=True) ax.bar(np.arange(1, 367), maxperiod[1:], fc="b", ec="b") ax.grid(True) ax.set_ylabel("Consecutive Days") ax.set_title( ("%s %s\nMaximum Straight Days with %s %s$^\circ$F") % (station, ctx["_nt"].sts[station]["name"], PDICT[varname], threshold)) ax.set_xticks(xticks) ax.set_xticklabels(calendar.month_abbr[1:]) ax.set_xlim(0, 366) return fig, df
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) station1 = ctx["station1"] station2 = ctx["station2"] mag = ctx["mag"] pvar = ctx["pvar"] pgconn = get_dbconn("coop") table1 = "alldata_%s" % (station1[:2], ) table2 = "alldata_%s" % (station2[:2], ) df = read_sql( """ WITH obs1 as ( SELECT day, high, low, precip, (high+low)/2. as avgt from """ + table1 + """ WHERE station = %s), obs2 as ( SELECT day, high, low, precip, (high+low)/2. as avgt from """ + table2 + """ WHERE station = %s) SELECT extract(doy from o.day) as doy, count(*), sum(case when o.high >= (t.high + %s) then 1 else 0 end) as high_hits, sum(case when o.low >= (t.low + %s) then 1 else 0 end) as low_hits, sum(case when o.precip >= (t.precip + %s) then 1 else 0 end) as precip_hits, sum(case when o.avgt >= (t.avgt + %s) then 1 else 0 end) as avgt_hits from obs1 o JOIN obs2 t on (o.day = t.day) GROUP by doy ORDER by doy ASC """, pgconn, params=(station1, station2, mag, mag, mag, mag), index_col="doy", ) for _v in ["high", "low", "avgt", "precip"]: df["%s_freq[%%]" % (_v, )] = df["%s_hits" % (_v, )] / df["count"] * 100.0 (fig, ax) = plt.subplots(1, 1) ax.bar(df.index.values, df[pvar + "_freq[%]"], fc="r", ec="r") ax.axhline(df[pvar + "_freq[%]"].mean()) ax.grid(True) ax.set_ylabel( ("Percentage [%%], Ave: %.1f%%") % (df[pvar + "_freq[%]"].mean(), )) v = int(mag) if pvar != "precip" else round(mag, 2) units = " inch" if pvar == "precip" else r"$^\circ$F" ax.set_title(("%s [%s] Daily %s\n%s+%s %s Than %s [%s]") % ( ctx["_nt1"].sts[station1]["name"], station1, PDICT[pvar], v, units, "Warmer" if pvar != "precip" else "Wetter", ctx["_nt2"].sts[station2]["name"], station2, )) ax.set_xlim(0, 366) ax.set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 365)) ax.set_xticklabels(calendar.month_abbr[1:]) return fig, df
def plotter(fdict): """ Go """ ctx = get_ctx(fdict) (fig, ax) = plt.subplots(1, 1) ax.set_position([0.1, 0.1, 0.75, 0.75]) y2 = ax.twinx() y2.set_position([0.1, 0.1, 0.75, 0.75]) y2.plot(np.arange(-365, 0), ctx["y2"][::-1], color="b", lw=2) if ctx["opt"] == "rank": y2.axhline(ctx["years"], color="b", linestyle="-.") y2.text(-180, ctx["years"] + 2, "Total Years", ha="center") y2.set_ylim(0, ctx["years"] + 30) ax.grid(True) ax.set_xticks(ctx["xticks"]) ax.set_xticklabels(ctx["xticklabels"]) ax.set_title(("%s\n%s") % (ctx["title"], ctx["subtitle"]), fontsize=10) y2.set_ylabel("%s (blue line)" % (ctx["y2label"], ), color="b") ax.set_xlim(-367, 0.5) ax.plot( np.arange(-365, 0), ctx["totals"][::-1], color="r", lw=2, label="This Period", ) ax.plot( np.arange(-365, 0), ctx["avgs"][::-1], color="purple", lw=2, label="Average", ) ax.plot( np.arange(-365, 0), ctx["maxes"][::-1], color="g", lw=2, label="Maximum", ) ax.set_ylabel("Precipitation [inch]") ax.set_zorder(y2.get_zorder() + 1) ax.patch.set_visible(False) ax.legend(loc="best", ncol=3) ax.set_ylim(bottom=0) df = pd.DataFrame( dict( day=np.arange(-365, 0), maxaccum=ctx["maxes"][::-1], accum=ctx["totals"][::-1], rank=ctx["ranks"][::-1], spi=ctx["spi"][::-1], percentages=ctx["percentages"][::-1], departures=ctx["departures"][::-1], )) return fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] t1 = ctx['t1'] t2 = ctx['t2'] table = "alldata_%s" % (station[:2], ) nt = NetworkTable("%sCLIMATE" % (station[:2], )) df = read_sql(""" SELECT year, min(low) as min_low, min(case when low < %s then extract(doy from day) else 999 end) as t1_doy, min(case when low < %s then extract(doy from day) else 999 end) as t2_doy from """ + table + """ where station = %s and month > 6 GROUP by year ORDER by year ASC """, pgconn, params=(t1, t2, station), index_col='year') df = df[df['t2_doy'] < 400] doy = np.array(df['t1_doy'], 'i') doy2 = np.array(df['t2_doy'], 'i') sts = datetime.datetime(2000, 1, 1) xticks = [] xticklabels = [] for i in range(min(doy), max(doy2) + 1): ts = sts + datetime.timedelta(days=i) if ts.day in [1, 8, 15, 22]: xticks.append(i) fmt = "%b %-d" if ts.day == 1 else "%-d" xticklabels.append(ts.strftime(fmt)) (fig, ax) = plt.subplots(1, 1) ax.set_xticks(xticks) ax.set_xticklabels(xticklabels) ax.scatter(doy, doy2 - doy) for x in xticks: ax.plot((x - 100, x), (100, 0), ':', c=('#000000')) ax.set_ylim(-1, max(doy2 - doy) + 4) ax.set_xlim(min(doy) - 4, max(doy) + 4) ax.set_title("[%s] %s\nFirst Fall Temperature Occurences" % (station, nt.sts[station]['name'])) ax.set_ylabel(r"Days until first sub %s$^{\circ}\mathrm{F}$" % (t2, )) ax.set_xlabel(r"First day of sub %s$^{\circ}\mathrm{F}$" % (t1, )) ax.grid(True) return fig, df
def plotter(fdict): """ Go """ import seaborn as sns pgconn = get_dbconn('postgis') ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] phenomena = ctx['phenomena'] significance = ctx['significance'] opt = ctx['opt'] state = ctx['state'] ctx['_nt'].sts['_ALL'] = {'name': 'All Offices'} wfo_limiter = (" and wfo = '%s' ") % (station if len(station) == 3 else station[1:], ) if station == '_ALL': wfo_limiter = '' if opt == 'state': wfo_limiter = " and substr(ugc, 1, 2) = '%s'" % (state, ) # NB we added a hack here that may lead to some false positives when events # cross over months, sigh, recall the 2017 eventid pain df = read_sql(""" with data as ( SELECT distinct extract(year from issue)::int as yr, extract(month from issue)::int as mo, wfo, eventid from warnings where phenomena = %s and significance = %s """ + wfo_limiter + """ GROUP by yr, mo, wfo, eventid) SELECT yr, mo, count(*) from data GROUP by yr, mo ORDER by yr, mo ASC """, pgconn, params=(phenomena, significance), index_col=None) if df.empty: raise NoDataFound("Sorry, no data found!") (fig, ax) = plt.subplots(1, 1, figsize=(8, 8)) df2 = df.pivot('yr', 'mo', 'count') df2 = df2.reindex(index=range(df2.index.min(), df2.index.max() + 1), columns=range(1, 13)) title = "NWS %s" % (ctx['_nt'].sts[station]['name'], ) if opt == 'state': title = ("NWS Issued for Counties/Zones for State of %s") % ( reference.state_names[state], ) title += ("\n%s (%s.%s) Issued by Year,Month") % (vtec.get_ps_string( phenomena, significance), phenomena, significance) ax.set_title(title) sns.heatmap(df2, annot=True, fmt=".0f", linewidths=.5, ax=ax, vmin=1) ax.set_xticks(np.arange(12) + 0.5) ax.set_xticklabels(calendar.month_abbr[1:]) ax.set_ylabel("Year") ax.set_xlabel("Month") return fig, df
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) station = ctx['station'] (fig, ax) = plt.subplots(1, 1) ax.text(0.5, 0.5, 'To Be Determined %s.' % (station, )) return fig
def main(argv): """Go Main Go.""" school = argv[1] req = requests.get( ('https://api.weather.gov/points/%s,%s/forecast/hourly' ) % (conf[school][2], conf[school][3])) jdata = req.json() rows = [] for data in jdata['properties']['periods']: ts = datetime.datetime.strptime(data['endTime'][:13], '%Y-%m-%dT%H') rows.append( dict(valid=ts, tmpf=data['temperature'], smph=int(data['windSpeed'].split()[0]))) df = pd.DataFrame(rows[:-24]) df['wcht'] = windchill( df['tmpf'].values * units('degF'), df['smph'].values * units('mph') ) (fig, ax) = plt.subplots(1, 1, figsize=(8, 6)) ax.plot(df['valid'], df['wcht'], lw=2, color='g', label='Wind Chill') ax.plot(df['valid'], df['tmpf'], lw=2, color='r', label='Temperature') ax.set_ylabel(r"Temperature $^\circ$F") ax.set_title( ("NWS NDFD Grid Point Forecast for %s Airport" ) % (conf[school][4]) ) ax.grid(True) ax.legend(loc=1, ncol=2) ax.set_ylim(-60, 60) ax.set_yticks(range(-60, 61, 10)) ax2 = ax.twinx() ax2.scatter(df['valid'].values, df['smph'].values) ax2.set_ylim(0, 24) ax2.set_yticks(range(0, 25, 2)) ax2.set_ylabel("Wind Speed [mph], blue dots") ax2.xaxis.set_major_locator(mdates.HourLocator(byhour=[0, 12])) ax2.xaxis.set_major_formatter(mdates.DateFormatter('%-I %p\n%-d %b')) ax.axvspan( datetime.datetime(2019, 1, 29, 17), datetime.datetime(2019, 1, 31, 12), zorder=-1, color='tan') ax.text( datetime.datetime(2019, 1, 30, 14), -55, "%s CLOSED!" % (school, ), bbox=dict(color=conf[school][0]), ha='center', va='center', color=conf[school][1], fontsize=18 ) fig.savefig('%s.png' % (school, ))
def main(): """Go Main.""" SQUAW = get_dbconn("squaw") cursor = SQUAW.cursor() data = np.ma.ones((datetime.date.today().year - 1990 + 1, 53), "f") * -99.0 data.mask = np.where(data < 0, True, False) cursor.execute(""" SELECT extract(year from valid)::int as yr, extract(week from valid)::int as week, avg(cfs) from real_flow GROUP by yr, week """) for row in cursor: data[row[0] - 1990, row[1] - 1] = row[2] levels = np.percentile( np.where(data < 0, 0, data), [10, 20, 30, 40, 50, 60, 70, 80, 90, 99] ) norm = mpcolors.BoundaryNorm(levels, 12) cmap = james() cmap.set_under("#FFFFFF") (fig, ax) = plt.subplots(1, 1) res = ax.imshow( np.flipud(data), interpolation="nearest", extent=(1, 54, 1989.5, 2015.5), cmap=cmap, norm=norm, aspect="auto", ) ax.set_xticks(np.arange(1, 56, 7)) ax.set_xlim(1, 53) ax.set_xticklabels( ("Jan 1", "Feb 19", "Apr 8", "May 27", "Jul 15", "Sep 2", "Oct 21", "Dec 9") ) ax.grid() ax.set_title( "Squaw Creek @ Lincoln Way in Ames\nWeekly Average Streamflow" ) ax.set_ylabel("Year") ax.set_xlabel("Day of Year") fig.colorbar(res, extend="both", label="Cubic Feet per Second") fig.savefig("test.png")
def main(): """Go Main Go.""" # get_data() df = pd.read_csv('data.csv') df['dt'] = pd.to_datetime(df['dt']) - datetime.timedelta(hours=5) df['s'] = df['dt'].dt.strftime("%Y%m%d%H%M") x = [] y = [] sts = datetime.datetime(2018, 9, 1, 19) ets = datetime.datetime(2018, 9, 2, 2, 1) interval = datetime.timedelta(minutes=1) now = sts current = None xticks = [] xticklabels = [] while now < ets: if now.minute == 0: xticks.append(now) xticklabels.append(now.strftime("%-I %p")) x.append(now) if now.strftime("%Y%m%d%H%M") in df['s'].values: current = 0 else: if current is not None: current += 1 y.append(current) now += interval (fig, ax) = plt.subplots(1, 1) ax.plot(x, y) ax.set_xticks(xticks) ax.set_xticklabels(xticklabels) ax.set_yticks(list(range(0,91, 15))) ax.set_ylabel("Minutes since Last Lightning Strike") ax.set_xlabel("Evening of 1 September 2018 (CDT)") ax.axhline(30, lw=2, color='r') ax.grid(True) ax.set_title(( "Iowa State vs South Dakota State Football Game\n" "Time since Last Lightning Strike within 8 miles of Jack Trice Stadium\n" "Data courtesy of National Lightning Detection Network")) fig.savefig('test.png')
def main(): """Go Main Go.""" pgconn = get_dbconn('iem') df = read_sql(""" SELECT valid, station, snow_jul1 - snow_jul1_normal as departure from cli_data WHERE valid > '2018-10-01' and station in ('KDSM', 'KDBQ', 'KSUX', 'KMCW', 'KMLI', 'KOMA') ORDER by valid ASC """, pgconn, index_col=None) (fig, ax) = plt.subplots(1, 1) for station, gdf in df.groupby('station'): ax.plot( gdf['valid'].values, gdf['departure'].values, label=STATIONS[station], lw=2) ax.xaxis.set_major_formatter(mdates.DateFormatter("%b\n%Y")) ax.grid(True) ax.legend(loc=2) ax.set_title( ("Season to Date Snowfall Departures\n" "Data from NWS CLI Reporting Sites")) ax.set_ylabel("Snowfall Departure [inch]") fig.savefig('test.png')
def main(argv): """Run main Run.""" fn = argv[1] df = pd.read_csv(fn) sdate = datetime.datetime.strptime(df.columns[0], '%Y%m%d') df.columns = ['precip_mm'] df['date'] = pd.date_range(sdate, periods=len(df.index)) df.set_index('date', inplace=True) gdf = df.groupby([df.index.year, df.index.month]).sum().copy() gdf.reset_index(level=1, inplace=True) gdf.columns = ['month', 'precip_mm'] gdf.reset_index(inplace=True) gdf.columns = ['year', 'month', 'precip_mm'] gdf = pd.pivot_table(gdf, index='year', values='precip_mm', columns='month') print(gdf) fig, ax = plt.subplots(figsize=(9, 6)) sns.heatmap(gdf, annot=True, fmt='.0f', cmap='YlGnBu', linewidths=.5, ax=ax) ax.set_xticklabels(calendar.month_abbr[1:]) tokens = fn.split("/") ax.set_title("Monthly Precipitation [mm] %s %s" % (tokens[-3], tokens[-1])) fig.savefig('%s_monthly_total.png' % (tokens[-1][:-4], )) plt.close() # ------------------------------------- for threshold in [0.25, 25]: df2 = df[df['precip_mm'] > threshold] gdf = df2.groupby( [df2.index.year, df2.index.month]).count().copy() gdf.reset_index(level=1, inplace=True) gdf.columns = ['month', 'precip_mm'] gdf.reset_index(inplace=True) gdf.columns = ['year', 'month', 'precip_mm'] gdf = pd.pivot_table(gdf, index='year', values='precip_mm', columns='month') print(gdf) fig, ax = plt.subplots(figsize=(9, 6)) sns.heatmap(gdf, annot=True, fmt='.0f', cmap='YlGnBu', linewidths=.5, ax=ax) ax.set_xticklabels(calendar.month_abbr[1:]) tokens = fn.split("/") ax.set_title("Daily Events >= %smm %s %s" % ( threshold, tokens[-3], tokens[-1])) fig.savefig('%s_%s_counts.png' % (tokens[-1][:-4], threshold)) plt.close() # ------------------------------------- gdf = df.groupby( [df.index.year, df.index.month]).max().copy() gdf.reset_index(level=1, inplace=True) gdf.columns = ['month', 'precip_mm'] gdf.reset_index(inplace=True) gdf.columns = ['year', 'month', 'precip_mm'] gdf = pd.pivot_table(gdf, index='year', values='precip_mm', columns='month') print(gdf) fig, ax = plt.subplots(figsize=(9, 6)) sns.heatmap(gdf, annot=True, fmt='.0f', cmap='YlGnBu', linewidths=.5, ax=ax) ax.set_xticklabels(calendar.month_abbr[1:]) tokens = fn.split("/") ax.set_title("Max Daily Precip [mm] %s %s" % (tokens[-3], tokens[-1])) fig.savefig('%s_monthly_max.png' % (tokens[-1][:-4], )) plt.close()
0.11 | 1.54 | 14 0.12 | 1.32 | 11 0.13 | 0.78 | 6 0.14 | 0.14 | 1 0.15 | 0.15 | 1 0.16 | 0.48 | 3 0.17 | 0.34 | 2 0.18 | 0.18 | 1 0.2 | 0.4 | 2 0.21 | 0.63 | 3""") df = pd.read_csv(data, sep=r"\s+\|\s+", engine='python') df['total_ratio'] = df['total'] / df['total'].sum() * 100. df['count_ratio'] = df['count'] / df['count'].sum() * 100. (fig, ax) = plt.subplots(2, 1) ax[0].bar(df['rate'].values, df['total_ratio'], align='edge', color='b', width=0.0035, label='Total Rain Volume') ax[0].bar(df['rate'].values, df['count_ratio'], align='edge', color='r', width=-0.0035, label='Total Count') ax[0].set_ylabel("Contribution Percentage [%]") ax[0].set_title("2000-2016 Des Moines Precipitation Contributions") ax[0].legend() ax[0].grid(True) ax[1].bar(df['rate'].values, df['total_ratio'], align='edge', color='b', width=0.0035, label='Total Rain Volume') ax[1].bar(df['rate'].values, df['count_ratio'], align='edge', color='r', width=-0.0035, label='Total Count') ax[1].set_xlabel("One Minute Accumulation [inch]")
snow500 = np.array(snow500) snowtmpf = np.array(snowtmpf) raintmpf = np.array(raintmpf) np.save('snowtmpf', snowtmpf) np.save('raintmpf', raintmpf) np.save('snow500', snow500) np.save('rain500', rain500) snowtmpf = np.load('snowtmpf.npy') snow500 = np.load('snow500.npy') rain500 = np.load('rain500.npy') raintmpf = np.load('raintmpf.npy') (fig, ax) = plt.subplots(2,1, sharex=True) ax[0].scatter(snowtmpf, snow500, marker='*', color='b', label='Snow', zorder=1) ax[0].set_title("1961-2013 Omaha 1000-500 hPa Thickness and 2m Temps\nWhen Rain/Snow is reported within 1 Hour of sounding") ax[0].set_ylim(4900,6000) ax[0].set_ylabel("1000-500 hPa Thickness [m]") ax[0].axhline(5400, c='k') ax[0].axvline(32, c='k') ax[0].grid(True) ax[0].legend(loc=2) ax[0].text(33,5050, "32$^\circ$F") ax[1].scatter(raintmpf, rain500, facecolor='none', edgecolor='g', label='Rain', zorder=2) ax[1].set_ylim(4900,6000) ax[1].legend(loc=2) ax[1].grid(True)
def main(): """Go Main Go""" pgconn = get_dbconn('coop') df = read_sql(""" select sday, to_char(day, 'YYYYMMDD') as date, high, low, precip from alldata_ia where station = 'IA2203' """, pgconn, index_col='date') climo = df.groupby('sday').mean() hevents = [] levents = [] pevents = [] for line in open('dates.txt'): date = line.strip() if date not in df.index: continue dt = datetime.datetime.strptime(date, '%Y%m%d') harr = [] larr = [] parr = [] for dy in range(-5, 6): dt2 = dt + datetime.timedelta(days=dy) deltahigh = (df.at[dt2.strftime("%Y%m%d"), 'high'] - climo.at[dt2.strftime("%m%d"), 'high']) harr.append(deltahigh) deltalow = (df.at[dt2.strftime("%Y%m%d"), 'low'] - climo.at[dt2.strftime("%m%d"), 'low']) larr.append(deltalow) parr.append( 1 if df.at[dt2.strftime("%Y%m%d"), 'precip'] >= 0.01 else 0 ) hevents.append(harr) levents.append(larr) pevents.append(parr) hdata = np.array(hevents) highs = np.mean(hdata, axis=0) ldata = np.array(levents) lows = np.mean(ldata, axis=0) pdata = np.array(pevents) precip = np.mean(pdata, axis=0) (fig, ax) = plt.subplots(3, 1, sharex=True) fig.text(0.1, 0.95, ("Des Moines Daily Weather (1880-2017)\n" "for dates around US East Coast Landfalling Hurricane" ), ha='left', va='center', fontsize=14) ax[0].text(0.02, 1.02, "High Temperature Departure", transform=ax[0].transAxes) ax[0].grid(True) ax[0].set_ylabel(r"Departure $^\circ$F") ax[0].set_ylim(-3, 3) ax[0].set_yticks(range(-2, 3)) bars = ax[0].bar(np.arange(-5, 6), highs) for i, _bar in enumerate(bars): if highs[i] > 0: _bar.set_color('r') ax[1].text(0.02, 1.02, "Low Temperature Departure", transform=ax[1].transAxes) ax[1].grid(True) ax[1].set_ylabel(r"Departure $^\circ$F") ax[1].set_yticks(range(-2, 3)) ax[1].set_ylim(-3, 3) bars = ax[1].bar(np.arange(-5, 6), lows) for i, _bar in enumerate(bars): if lows[i] > 0: _bar.set_color('r') ax[2].bar(np.arange(-5, 6), precip * 100.) ax[2].text(0.02, 1.02, "Measurable Rainfall Frequency", transform=ax[2].transAxes) ax[2].set_ylim(bottom=0) ax[2].set_ylabel("Frequency [%]") ax[2].grid(True) ax[2].set_xlabel(("Relative Days to Hurricane Landfall, " "%s events considered" ) % (len(hevents), )) fig.savefig('test.png')