def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) csector = ctx['csector'] date = ctx['date'] z = ctx['z'] period = ctx['f'] scale = ctx['scale'] valid = utc(date.year, date.month, date.day, int(z)) gribfn = valid.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/wpc/" "p" + period + "m_%Y%m%d%Hf" + period + ".grb")) if not os.path.isfile(gribfn): raise ValueError("gribfn %s missing" % (gribfn, )) grbs = pygrib.open(gribfn) grb = grbs[1] precip = distance(grb.values, 'MM').value('IN') lats, lons = grb.latlons() title = ("Weather Prediction Center %s Quantitative " "Precipitation Forecast") % (PDICT[period]) subtitle = ("%sWPC Forcast %s UTC to %s UTC") % ( ("US Drought Monitor Overlaid, " if ctx['opt'] == 'both' else ''), valid.strftime("%d %b %Y %H"), (valid + datetime.timedelta(hours=int(period))).strftime("%d %b %Y %H")) mp = MapPlot(sector=('state' if len(csector) == 2 else csector), state=ctx['csector'], title=title, subtitle=subtitle, continentalcolor='white', titlefontsize=16) cmap = plt.get_cmap('gist_ncar') cmap.set_under('#EEEEEE') cmap.set_over('black') if scale == 'auto': levs = np.linspace(0, np.max(precip) * 1.1, 10) levs = [round(lev, 2) for lev in levs] levs[0] = 0.01 elif scale == '10': levs = np.arange(0, 10.1, 1.) levs[0] = 0.01 elif scale == '7': levs = np.arange(0, 7.1, 0.5) levs[0] = 0.01 elif scale == '3.5': levs = np.arange(0, 3.6, 0.25) levs[0] = 0.01 mp.pcolormesh(lons, lats, precip, levs, cmap=cmap, units='inch', clip_on=False) if ctx['opt'] == 'both': mp.draw_usdm(valid=valid, filled=False, hatched=True) return mp.fig
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot import matplotlib.cm as cm pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) sector = ctx['sector'] date1 = ctx['date1'] date2 = ctx['date2'] varname = ctx['var'] usdm = ctx['usdm'] table = "alldata_%s" % (sector, ) if sector != 'midwest' else "alldata" df = read_sql(""" WITH obs as ( SELECT station, gddxx(50, 86, high, low) as gdd50, sday, high, low, precip 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, gdd50 from climate51), combo as ( SELECT o.station, o.precip - c.precip as precip_diff, o.precip as precip, c.precip as cprecip, o.high, o.low, o.gdd50, c.gdd50 as cgdd50, o.gdd50 - c.gdd50 as gdd50_diff, (o.high + o.low)/2. - (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, sum(precip_diff) as precip_depart, sum(precip) as precip, sum(cprecip) as cprecip, min(low) as min_low_temp, sum(gdd50_diff) as gdd_depart, avg(temp_diff) as avg_temp_depart, sum(gdd50) as gdd_sum, sum(cgdd50) as cgdd_sum from combo GROUP by station) SELECT d.station, t.name, d.precip as precip_sum, d.cprecip as cprecip_sum, d.precip_depart, d.min_low_temp, d.avg_temp_depart, d.gdd_depart, d.gdd_sum, d.cgdd_sum, 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' """, pgconn, params=(date1, date2), index_col='station') df = df.reindex(df[varname].abs().sort_values(ascending=False).index) sector2 = "state" if sector != 'midwest' else 'midwest' datefmt = "%d %b %Y" if varname != 'cgdd_sum' else '%d %b' subtitle = ("Climatology is based on data from 1951-%s") % ( datetime.date.today().year - 1, ) if varname != 'cgdd_sum': subtitle = ('%s is compared with 1951-%s Climatology' ' to compute departures') % ( date1.year, datetime.date.today().year - 1) mp = MapPlot(sector=sector2, state=sector, axisbg='white', title=('%s - %s %s [%s]') % (date1.strftime(datefmt), date2.strftime(datefmt), PDICT2.get(varname), UNITS.get(varname)), subtitle=subtitle) if varname in ['precip_depart', 'avg_temp_depart']: rng = df[varname].abs().describe(percentiles=[0.95])['95%'] clevels = np.linspace(0 - rng, rng, 7) fmt = '%.2f' 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 = cm.get_cmap('RdYlBu' if varname == 'precip_depart' else 'RdYlBu_r') cmap.set_bad('white') mp.contourf(df['lon'].values, df['lat'].values, df[varname].values, clevels, clevlabels=clevlabels, cmap=cmap, units=UNITS.get(varname)) mp.plot_values(df['lon'].values, df['lat'].values, df[varname].values, fmt=fmt, labelbuffer=10) if sector == 'IA': mp.drawcounties() if usdm == 'yes': mp.draw_usdm(date2, filled=False, hatched=True) return mp.fig, df
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) csector = ctx["csector"] date = ctx["date"] z = ctx["z"] period = ctx["f"] scale = ctx["scale"] valid = utc(date.year, date.month, date.day, int(z)) gribfn = valid.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/wpc/" "p" + period + "m_%Y%m%d%Hf" + period + ".grb")) if not os.path.isfile(gribfn): raise NoDataFound("gribfn %s missing" % (gribfn, )) grbs = pygrib.open(gribfn) grb = grbs[1] precip = distance(grb.values, "MM").value("IN") lats, lons = grb.latlons() title = ("Weather Prediction Center %s Quantitative " "Precipitation Forecast") % (PDICT[period]) subtitle = ("%sWPC Forecast %s UTC to %s UTC") % ( ("US Drought Monitor Overlaid, " if ctx["opt"] == "both" else ""), valid.strftime("%d %b %Y %H"), (valid + datetime.timedelta(hours=int(period))).strftime("%d %b %Y %H"), ) mp = MapPlot( sector=("state" if len(csector) == 2 else csector), state=ctx["csector"], title=title, subtitle=subtitle, continentalcolor="white", titlefontsize=16, ) cmap = plt.get_cmap(ctx["cmap"]) cmap.set_under("#EEEEEE") cmap.set_over("black") if scale == "auto": levs = np.linspace(0, np.max(precip) * 1.1, 10) levs = [round(lev, 2) for lev in levs] levs[0] = 0.01 elif scale == "10": levs = np.arange(0, 10.1, 1.0) levs[0] = 0.01 elif scale == "7": levs = np.arange(0, 7.1, 0.5) levs[0] = 0.01 elif scale == "3.5": levs = np.arange(0, 3.6, 0.25) levs[0] = 0.01 mp.pcolormesh( lons, lats, precip, levs, cmap=cmap, units="inch", clip_on=(ctx["csector"] == "iailin"), ) if ctx["opt"] == "both": mp.draw_usdm(valid=valid, filled=False, hatched=True) if ctx["csector"] == "iailin": mp.drawcounties() return mp.fig
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) / sum(cprecip) * 100. as precip_percent, 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, precip_percent, 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", ) if df.empty: raise NoDataFound("No Data Found.") 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
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 sector != 'midwest' else "alldata" 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, 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, 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' """, pgconn, params=(ctx['gddbase'], date1, date2), index_col='station') df = df.reindex(df[varname].abs().sort_values(ascending=False).index) sector2 = "state" if sector != 'midwest' else 'midwest' 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=sector2, 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' if varname in ['precip_depart', 'avg_temp_depart']: rng = df[varname].abs().describe(percentiles=[0.95])['95%'] clevels = np.linspace(0 - rng, rng, 7) cmap = cm.get_cmap('RdYlBu' if varname == 'precip_depart' else 'RdYlBu_r') elif varname in ['precip_sum']: rng = df[varname].abs().describe(percentiles=[0.95])['95%'] clevels = np.linspace(0, rng, 7) cmap = cm.get_cmap('plasma_r') cmap.set_under('white') cmap.set_over('black') else: minv = df[varname].min() - 5 maxv = df[varname].max() + 5 clevels = np.linspace(minv, maxv, 6, dtype='i') fmt = '%.0f' cmap = cm.get_cmap('RdYlBu_r') 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)) mp.plot_values(df['lon'].values, df['lat'].values, df[varname].values, fmt=fmt, labelbuffer=5) if len(sector) == 2: mp.drawcounties() if ctx['usdm'] == 'yes': mp.draw_usdm(date2, filled=False, hatched=True) return mp.fig, df
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
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