def main(): """Go!""" title = 'NOAA MRMS Q3: RADAR + Guage Corrected Rainfall Estimates + NWS Storm Reports' mp = MapPlot(sector='custom', north=42.3, east=-93.0, south=41.65, west=-94.1, axisbg='white', titlefontsize=14, title=title, subtitle='Valid: 14 June 2018') shp = shapefile.Reader('cities.shp') for record in shp.shapeRecords(): geo = shape(record.shape) mp.ax.add_geometries([geo], ccrs.PlateCarree(), zorder=Z_OVERLAY2, facecolor='None', edgecolor='k', lw=2) grbs = pygrib.open('MRMS_GaugeCorr_QPE_24H_00.00_20180614-200000.grib2') grb = grbs.message(1) pcpn = distance(grb['values'], 'MM').value('IN') lats, lons = grb.latlons() lons -= 360. clevs = [0.01, 0.1, 0.3, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] cmap = nwsprecip() cmap.set_over('k') mp.pcolormesh(lons, lats, pcpn, clevs, cmap=cmap, latlon=True, units='inch') lons, lats, vals, labels = get_data() mp.drawcounties() mp.plot_values(lons, lats, vals, "%s", labels=labels, labelbuffer=1, labelcolor='white') mp.drawcities(labelbuffer=5, minarea=0.2) mp.postprocess(filename='test.png')
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ lts = utc(ts.year, ts.month, ts.day, 12) lts = lts.astimezone(pytz.timezone("America/Chicago")) # make assumptions about the last valid MRMS data if realtime: # Up until :59 after of the last hour lts = (datetime.datetime.now() - datetime.timedelta(hours=1)).replace(minute=59) else: lts = lts.replace(year=ts.year, month=ts.month, day=ts.day, hour=23, minute=59) idx = iemre.daily_offset(ts) ncfn = iemre.get_daily_mrms_ncname(ts.year) if not os.path.isfile(ncfn): LOG.info("File %s missing, abort.", ncfn) return with ncopen(ncfn, timeout=300) as nc: precip = nc.variables['p01d'][idx, :, :] lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] subtitle = "Total between 12:00 AM and %s" % ( lts.strftime("%I:%M %p %Z"), ) routes = 'ac' if not realtime: routes = 'a' # clevs = np.arange(0, 0.25, 0.05) # clevs = np.append(clevs, np.arange(0.25, 3., 0.25)) # clevs = np.append(clevs, np.arange(3., 10.0, 1)) clevs = [ 0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10 ] (xx, yy) = np.meshgrid(lons, lats) for sector in ['iowa', 'midwest']: pqstr = ("plot %s %s00 %s_q2_1d.png %s_q2_1d.png png") % ( routes, ts.strftime("%Y%m%d%H"), sector, sector) mp = MapPlot(title=("%s NCEP MRMS Q3 Today's Precipitation") % (ts.strftime("%-d %b %Y"), ), subtitle=subtitle, sector=sector) mp.pcolormesh(xx, yy, distance(precip, 'MM').value('IN'), clevs, cmap=nwsprecip(), units='inch') if sector == 'iowa': mp.drawcounties() mp.postprocess(pqstr=pqstr, view=False) mp.close()
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day We should total files from 1 AM to midnight local time """ sts = ts.replace(hour=1) ets = sts + datetime.timedelta(hours=24) interval = datetime.timedelta(hours=1) now = sts total = None lts = None while now < ets: gmt = now.astimezone(pytz.timezone("UTC")) fn = gmt.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/" "stage4/ST4.%Y%m%d%H.01h.grib")) if os.path.isfile(fn): lts = now grbs = pygrib.open(fn) if total is None: g = grbs[1] total = g["values"] lats, lons = g.latlons() else: total += grbs[1]["values"] grbs.close() now += interval if lts is None and ts.hour > 1: print 'stage4_today_total.py found no data!' if lts is None: return lts = lts - datetime.timedelta(minutes=1) subtitle = "Total between 12:00 AM and %s" % (lts.strftime("%I:%M %p %Z"),) routes = 'ac' if not realtime: routes = 'a' for sector in ['iowa', 'midwest', 'conus']: pqstr = ("plot %s %s00 %s_stage4_1d.png %s_stage4_1d.png png" ) % (routes, ts.strftime("%Y%m%d%H"), sector, sector) m = MapPlot(sector=sector, title="%s NCEP Stage IV Today's Precipitation" % ( ts.strftime("%-d %b %Y"),), subtitle=subtitle) clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] m.pcolormesh(lons, lats, distance(total, 'MM').value('IN'), clevs, cmap=nwsprecip(), units='inch') # map.drawstates(zorder=2) if sector == 'iowa': m.drawcounties() m.postprocess(pqstr=pqstr) m.close()
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ lts = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("UTC")) lts = lts.astimezone(pytz.timezone("America/Chicago")) # make assumptions about the last valid MRMS data if realtime: # Up until :59 after of the last hour lts = (lts - datetime.timedelta(hours=1)).replace(minute=59) else: lts = lts.replace(year=ts.year, month=ts.month, day=ts.day, hour=23, minute=59) idx = iemre.daily_offset(ts) ncfn = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (ts.year, ) nc = netCDF4.Dataset(ncfn) precip = nc.variables['p01d'][idx, :, :] lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] subtitle = "Total between 12:00 AM and %s" % ( lts.strftime("%I:%M %p %Z"), ) routes = 'ac' if not realtime: routes = 'a' # clevs = np.arange(0, 0.25, 0.05) # clevs = np.append(clevs, np.arange(0.25, 3., 0.25)) # clevs = np.append(clevs, np.arange(3., 10.0, 1)) clevs = [ 0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10 ] sector = 'iowa' pqstr = ("plot %s %s00 %s_q2_1d.png %s_q2_1d.png png") % ( routes, ts.strftime("%Y%m%d%H"), sector, sector) m = MapPlot(title=("%s NCEP MRMS Q3 Today's Precipitation") % (ts.strftime("%-d %b %Y"), ), subtitle=subtitle, sector=sector) (x, y) = np.meshgrid(lons, lats) m.pcolormesh(x, y, distance(precip, 'MM').value('IN'), clevs, cmap=nwsprecip(), units='inch') m.drawcounties() m.postprocess(pqstr=pqstr, view=False) m.close()
def main(argv): """Do Great Things""" year = int(argv[1]) pgconn = psycopg2.connect(database='idep', host='localhost', port=5555, user='******') cursor = pgconn.cursor() mp = MapPlot(continentalcolor='white', nologo=True, sector='custom', south=36.8, north=45.0, west=-99.2, east=-88.9, subtitle='Assumes 56 lb test weight', title=('%s Corn Yield HUC12 Average') % (year, )) cursor.execute( """ with hucs as ( select huc_12, ST_Transform(simple_geom, 4326) as geo from huc12), data as ( SELECT huc12, avg(yield_kgm2) * 8921.8 / 56. as val from harvest where crop = 'Corn' and valid between %s and %s GROUP by huc12 ) SELECT geo, huc12, val from hucs h JOIN data d on (h.huc_12 = d.huc12) """, (datetime.date(year, 1, 1), datetime.date(year, 12, 31))) bins = np.arange(0, 310, 30) cmap = nwsprecip() cmap.set_under('white') cmap.set_over('black') norm = mpcolors.BoundaryNorm(bins, cmap.N) for row in cursor: polygon = loads(row[0].decode('hex')) arr = np.asarray(polygon.exterior) points = mp.ax.projection.transform_points(ccrs.Geodetic(), arr[:, 0], arr[:, 1]) color = cmap(norm([ float(row[2]), ]))[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='bu/acre') # mp.drawcounties() mp.postprocess(filename='test.png')
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ lts = datetime.datetime.utcnow().replace( tzinfo=pytz.timezone("UTC")) lts = lts.astimezone(pytz.timezone("America/Chicago")) # make assumptions about the last valid MRMS data if realtime: # Up until :59 after of the last hour lts = (lts - datetime.timedelta(hours=1)).replace(minute=59) else: lts = lts.replace(year=ts.year, month=ts.month, day=ts.day, hour=23, minute=59) idx = iemre.daily_offset(ts) ncfn = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (ts.year,) nc = netCDF4.Dataset(ncfn) precip = nc.variables['p01d'][idx, :, :] lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] subtitle = "Total between 12:00 AM and %s" % ( lts.strftime("%I:%M %p %Z"),) routes = 'ac' if not realtime: routes = 'a' # clevs = np.arange(0, 0.25, 0.05) # clevs = np.append(clevs, np.arange(0.25, 3., 0.25)) # clevs = np.append(clevs, np.arange(3., 10.0, 1)) clevs = [0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] sector = 'iowa' pqstr = ("plot %s %s00 %s_q2_1d.png %s_q2_1d.png png" ) % (routes, ts.strftime("%Y%m%d%H"), sector, sector) m = MapPlot(title=("%s NCEP MRMS Q3 Today's Precipitation" ) % (ts.strftime("%-d %b %Y"),), subtitle=subtitle, sector=sector) (x, y) = np.meshgrid(lons, lats) m.pcolormesh(x, y, distance(precip, 'MM').value('IN'), clevs, cmap=nwsprecip(), units='inch') m.drawcounties() m.postprocess(pqstr=pqstr, view=False) m.close()
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ nc = netCDF4.Dataset("/mesonet/data/iemre/%s_ifc_daily.nc" % (ts.year, )) idx = daily_offset(ts) xaxis = nc.variables['lon'][:] yaxis = nc.variables['lat'][:] total = nc.variables['p01d'][idx, :, :] nc.close() lastts = datetime.datetime(ts.year, ts.month, ts.day, 23, 59) if realtime: now = datetime.datetime.now() - datetime.timedelta(minutes=60) lastts = now.replace(minute=59) subtitle = "Total between 12:00 AM and %s" % ( lastts.strftime("%I:%M %p"), ) routes = 'ac' if not realtime: routes = 'a' clevs = [ 0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10 ] pqstr = ("plot %s %s00 iowa_ifc_1d.png iowa_ifc_1d.png png") % ( routes, ts.strftime("%Y%m%d%H")) m = MapPlot(title=("%s Iowa Flood Center Today's Precipitation") % (ts.strftime("%-d %b %Y"), ), subtitle=subtitle, sector='custom', west=xaxis[0], east=xaxis[-1], south=yaxis[0], north=yaxis[-1]) (x, y) = np.meshgrid(xaxis, yaxis) m.pcolormesh(x, y, distance(total, 'MM').value("IN"), clevs, cmap=nwsprecip(), units='inch') m.drawcounties() m.postprocess(pqstr=pqstr, view=False) m.close()
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ nc = netCDF4.Dataset("/mesonet/data/iemre/%s_ifc_daily.nc" % (ts.year,)) idx = daily_offset(ts) xaxis = nc.variables['lon'][:] yaxis = nc.variables['lat'][:] total = nc.variables['p01d'][idx, :, :] nc.close() lastts = datetime.datetime(ts.year, ts.month, ts.day, 23, 59) if realtime: now = datetime.datetime.now() - datetime.timedelta(minutes=60) lastts = now.replace(minute=59) subtitle = "Total between 12:00 AM and %s" % ( lastts.strftime("%I:%M %p"),) routes = 'ac' if not realtime: routes = 'a' clevs = [0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] pqstr = ("plot %s %s00 iowa_ifc_1d.png iowa_ifc_1d.png png" ) % (routes, ts.strftime("%Y%m%d%H")) m = MapPlot(title=("%s Iowa Flood Center Today's Precipitation" ) % (ts.strftime("%-d %b %Y"),), subtitle=subtitle, sector='custom', west=xaxis[0], east=xaxis[-1], south=yaxis[0], north=yaxis[-1]) (x, y) = np.meshgrid(xaxis, yaxis) m.pcolormesh(x, y, distance(total, 'MM').value("IN"), clevs, cmap=nwsprecip(), units='inch') m.drawcounties() m.postprocess(pqstr=pqstr, view=False) m.close()
def doit(ts, hours): """ Create a plot of precipitation stage4 estimates for some day """ # Start at 1 AM ts = ts.replace(minute=0, second=0, microsecond=0) now = ts - datetime.timedelta(hours=hours-1) interval = datetime.timedelta(hours=1) ets = datetime.datetime.utcnow() ets = ets.replace(tzinfo=pytz.timezone("UTC")) total = None while now < ets: gmt = now.astimezone(pytz.timezone("UTC")) gribfn = None for prefix in ['GaugeCorr', 'RadarOnly']: fn = gmt.strftime((prefix + "_QPE_01H_00.00_%Y%m%d-%H%M00" ".grib2.gz")) res = requests.get(gmt.strftime( ("http://mtarchive.geol.iastate.edu/%Y/%m/%d/mrms/ncep/" + prefix + "_QPE_01H/" + fn)), timeout=30) if res.status_code != 200: continue o = open(TMP + "/" + fn, 'wb') o.write(res.content) o.close() gribfn = "%s/%s" % (TMP, fn) break if gribfn is None: print("q3_Xhour.py[%s] MISSING %s" % (hours, now)) now += interval continue fp = gzip.GzipFile(gribfn, 'rb') (tmpfp, tmpfn) = tempfile.mkstemp() tmpfp = open(tmpfn, 'wb') tmpfp.write(fp.read()) tmpfp.close() grbs = pygrib.open(tmpfn) grb = grbs[1] os.unlink(tmpfn) # careful here, how we deal with the two missing values! if total is None: total = grb['values'] else: maxgrid = np.maximum(grb['values'], total) total = np.where(np.logical_and(grb['values'] >= 0, total >= 0), grb['values'] + total, maxgrid) now += interval os.unlink(gribfn) if total is None: print("q3_Xhour.py no data ts: %s hours: %s" % (ts, hours)) return # Scale factor is 10 routes = "c" if ts.minute == 0: routes = "ac" pqstr = "plot %s %s iowa_q2_%sh.png q2/iowa_q2_%sh_%s00.png png" % ( routes, ts.strftime("%Y%m%d%H%M"), hours, hours, ts.strftime("%H")) lts = ts.astimezone(pytz.timezone("America/Chicago")) subtitle = 'Total up to %s' % (lts.strftime("%d %B %Y %I:%M %p %Z"),) m = MapPlot(title=("NCEP MRMS Q3 (RADAR Only) %s Hour " "Precipitation [inch]") % (hours,), subtitle=subtitle) clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] m.contourf(mrms.XAXIS, mrms.YAXIS, distance(np.flipud(total), 'MM').value('IN'), clevs, cmap=nwsprecip()) m.drawcounties() m.postprocess(pqstr=pqstr, view=False) m.close()
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') 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'] if sdate.year != edate.year: raise Exception('Sorry, do not support multi-year plots yet!') days = (edate - sdate).days sector = ctx['sector'] idx0 = iemre.daily_offset(sdate) idx1 = iemre.daily_offset(edate) + 1 if src == 'mrms': ncfn = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (sdate.year, ) ncvar = 'p01d' source = 'NOAA MRMS Q3' subtitle = 'NOAA MRMS Project, GaugeCorr and RadarOnly' else: ncfn = "/mesonet/data/prism/%s_daily.nc" % (sdate.year, ) ncvar = 'ppt' source = 'OSU PRISM' subtitle = ('PRISM Climate Group, Oregon State Univ., ' 'http://prism.oregonstate.edu, created 4 Feb 2004.') if not os.path.isfile(ncfn): raise Exception("No data for that year, sorry.") nc = netCDF4.Dataset(ncfn, 'r') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] if (idx1 - idx0) < 32: p01d = distance(np.sum(nc.variables[ncvar][idx0:idx1, :, :], 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, :, :], 0), 'MM').value('IN') else: p01d += distance(np.sum(nc.variables[ncvar][i:i2, :, :], 0), 'MM').value('IN') nc.close() if sdate == edate: title = sdate.strftime("%-d %B %Y") else: title = "%s to %s (inclusive)" % (sdate.strftime("%-d %b %Y"), edate.strftime("%-d %b %Y")) if sector == 'midwest': state = None else: state = sector sector = 'state' m = MapPlot(sector=sector, state=state, axisbg='white', nocaption=True, title='%s:: %s Total Precip' % (source, title), subtitle='Data from %s' % (subtitle, )) if np.ma.is_masked(np.max(p01d)): return 'Data Unavailable' 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] x, y = np.meshgrid(lons, lats) cmap = nwsprecip() cmap.set_over('k') if ptype == 'c': m.contourf(x, y, p01d, clevs, cmap=cmap, label='inches') else: m.pcolormesh(x, y, p01d, clevs, cmap=cmap, label='inches') if sector != 'midwest': m.drawcounties() m.drawcities() return m.fig
import matplotlib.pyplot as plt import datetime import numpy as np nc = netCDF4.Dataset("/mesonet/data/iemre/2016_mw_mrms_daily.nc", 'r') idx = iemre.daily_offset(datetime.date(2016, 7, 3)) grid = np.zeros((len(nc.dimensions['lat']), len(nc.dimensions['lon']))) total = np.zeros((len(nc.dimensions['lat']), len(nc.dimensions['lon']))) for i, x in enumerate(range(idx, idx - 60, -1)): total += nc.variables['p01d'][x, :, :] grid = np.where(np.logical_and(grid == 0, total > 25.4), i, grid) m = MapPlot( sector='iowa', title='NOAA MRMS Q3: Number of Recent Days till Accumulating 1" of Precip', subtitle= 'valid 4 July 2016: based on per calendar day estimated preciptation, GaugeCorr and RadarOnly products' ) lon = np.append(nc.variables['lon'][:], [-80.5]) print lon lat = np.append(nc.variables['lat'][:], [49.]) print lat x, y = np.meshgrid(lon, lat) cmap = nwsprecip() m.pcolormesh(x, y, grid, np.arange(0, 53, 7), cmap=cmap, units='days') m.drawcounties() m.drawcities() m.postprocess(filename='test.png') m.close() nc.close()
def doit(ts, hours): """ Create a plot of precipitation stage4 estimates for some day """ # Start at 1 AM ts = ts.replace(minute=0, second=0, microsecond=0) now = ts - datetime.timedelta(hours=hours - 1) interval = datetime.timedelta(hours=1) ets = datetime.datetime.utcnow() ets = ets.replace(tzinfo=pytz.utc) total = None while now < ets: gmt = now.astimezone(pytz.utc) gribfn = None for prefix in ["MultiSensor_QPE_01H_Pass2", "RadarOnly_QPE_01H"]: gribfn = mrms.fetch(prefix, gmt) if gribfn is None: continue break if gribfn is None: LOG.info("%s MISSING %s", hours, now) now += interval continue fp = gzip.GzipFile(gribfn, "rb") (tmpfp, tmpfn) = tempfile.mkstemp() tmpfp = open(tmpfn, "wb") tmpfp.write(fp.read()) tmpfp.close() grbs = pygrib.open(tmpfn) grb = grbs[1] os.unlink(tmpfn) # careful here, how we deal with the two missing values! if total is None: total = grb["values"] else: maxgrid = np.maximum(grb["values"], total) total = np.where( np.logical_and(grb["values"] >= 0, total >= 0), grb["values"] + total, maxgrid, ) now += interval os.unlink(gribfn) if total is None: LOG.info("no data ts: %s hours: %s", ts, hours) return # Scale factor is 10 routes = "c" if ts.minute == 0: routes = "ac" pqstr = "plot %s %s iowa_q2_%sh.png q2/iowa_q2_%sh_%s00.png png" % ( routes, ts.strftime("%Y%m%d%H%M"), hours, hours, ts.strftime("%H"), ) lts = ts.astimezone(pytz.timezone("America/Chicago")) subtitle = "Total up to %s" % (lts.strftime("%d %B %Y %I:%M %p %Z"), ) mp = MapPlot( title=("NCEP MRMS Q3 (RADAR Only) %s Hour " "Precipitation [inch]") % (hours, ), subtitle=subtitle, ) clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] mp.contourf( mrms.XAXIS, mrms.YAXIS, distance(np.flipud(total), "MM").value("IN"), clevs, cmap=nwsprecip(), ) mp.drawcounties() mp.postprocess(pqstr=pqstr, view=False) mp.close()
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot, nwsprecip ctx = util.get_autoplot_context(fdict, get_description()) ptype = ctx['ptype'] sdate = ctx['sdate'] edate = ctx['edate'] if sdate.year != edate.year: return 'Sorry, do not support multi-year plots yet!' days = (edate - sdate).days sector = ctx['sector'] idx0 = iemre.daily_offset(sdate) idx1 = iemre.daily_offset(edate) + 1 ncfn = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (sdate.year, ) if not os.path.isfile(ncfn): return "No MRMS data for that year, sorry." nc = netCDF4.Dataset(ncfn, 'r') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] if (idx1 - idx0) < 32: p01d = distance(np.sum(nc.variables['p01d'][idx0:idx1, :, :], 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['p01d'][i:i2, :, :], 0), 'MM').value('IN') else: p01d += distance(np.sum(nc.variables['p01d'][i:i2, :, :], 0), 'MM').value('IN') nc.close() if sdate == edate: title = sdate.strftime("%-d %B %Y") else: title = "%s to %s (inclusive)" % (sdate.strftime("%-d %b %Y"), edate.strftime("%-d %b %Y")) if sector == 'midwest': state = None else: state = sector sector = 'state' m = MapPlot(sector=sector, state=state, axisbg='white', nocaption=True, title='NOAA MRMS Q3:: %s Total Precip' % (title,), subtitle='Data from NOAA MRMS Project, GaugeCorr and RadarOnly' ) if np.ma.is_masked(np.max(p01d)): return 'Data Unavailable' 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] x, y = np.meshgrid(lons, lats) cmap = nwsprecip() cmap.set_over('k') if ptype == 'c': m.contourf(x, y, p01d, clevs, cmap=cmap, label='inches') else: m.pcolormesh(x, y, p01d, clevs, cmap=cmap, label='inches') if sector != 'midwest': m.drawcounties() m.drawcities() return m.fig
def doit(ts, hours): """ Create a plot of precipitation stage4 estimates for some day """ # Start at 1 AM ts = ts.replace(minute=0, second=0, microsecond=0) now = ts - datetime.timedelta(hours=hours - 1) interval = datetime.timedelta(hours=1) ets = datetime.datetime.utcnow() ets = ets.replace(tzinfo=pytz.timezone("UTC")) total = None while now < ets: gmt = now.astimezone(pytz.timezone("UTC")) gribfn = None for prefix in ['GaugeCorr', 'RadarOnly']: fn = gmt.strftime((prefix + "_QPE_01H_00.00_%Y%m%d-%H%M00" ".grib2.gz")) res = requests.get(gmt.strftime( ("http://mtarchive.geol.iastate.edu/%Y/%m/%d/mrms/ncep/" + prefix + "_QPE_01H/" + fn)), timeout=30) if res.status_code != 200: continue o = open(TMP + "/" + fn, 'wb') o.write(res.content) o.close() gribfn = "%s/%s" % (TMP, fn) break if gribfn is None: print("q3_Xhour.py[%s] MISSING %s" % (hours, now)) now += interval continue fp = gzip.GzipFile(gribfn, 'rb') (tmpfp, tmpfn) = tempfile.mkstemp() tmpfp = open(tmpfn, 'wb') tmpfp.write(fp.read()) tmpfp.close() grbs = pygrib.open(tmpfn) grb = grbs[1] os.unlink(tmpfn) # careful here, how we deal with the two missing values! if total is None: total = grb['values'] else: maxgrid = np.maximum(grb['values'], total) total = np.where(np.logical_and(grb['values'] >= 0, total >= 0), grb['values'] + total, maxgrid) now += interval os.unlink(gribfn) if total is None: print("q3_Xhour.py no data ts: %s hours: %s" % (ts, hours)) return # Scale factor is 10 routes = "c" if ts.minute == 0: routes = "ac" pqstr = "plot %s %s iowa_q2_%sh.png q2/iowa_q2_%sh_%s00.png png" % ( routes, ts.strftime("%Y%m%d%H%M"), hours, hours, ts.strftime("%H")) lts = ts.astimezone(pytz.timezone("America/Chicago")) subtitle = 'Total up to %s' % (lts.strftime("%d %B %Y %I:%M %p %Z"), ) m = MapPlot(title=("NCEP MRMS Q3 (RADAR Only) %s Hour " "Precipitation [inch]") % (hours, ), subtitle=subtitle) clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] m.contourf(mrms.XAXIS, mrms.YAXIS, distance(np.flipud(total), 'MM').value('IN'), clevs, cmap=nwsprecip()) m.drawcounties() m.postprocess(pqstr=pqstr, view=False) m.close()
def main(): """Go Main Go.""" pgconn = get_dbconn('iem') cursor = pgconn.cursor() cursor.execute("""SELECT ST_x(geom) as lon, ST_y(geom) as lat, pday from summary_2016 s JOIN stations t on (s.iemid = t.iemid) where day = '2016-08-24' and network in ('WI_COOP', 'MN_COOP', 'IA_COOP') and pday > 0 ORDER by pday DESC""") llons = [] llats = [] vals = [] for row in cursor: llons.append(row[0]) llats.append(row[1]) vals.append("%.2f" % (row[2], )) pgconn = get_dbconn('postgis') cursor = pgconn.cursor() cursor.execute("""SELECT ST_x(geom) as lon, ST_y(geom) as lat, max(magnitude) from lsrs_2016 where wfo in ('DMX', 'DVN', 'ARX') and typetext = 'HEAVY RAIN' and valid > '2016-08-23' GROUP by lon, lat ORDER by max DESC""") for row in cursor: llons.append(row[0]) llats.append(row[1]) vals.append("%.2f" % (row[2], )) img = Image.open("p24h_201608241200.png") data = np.flipud(np.asarray(img)) # 7000,3500 == -130,-60,55,25 === -100 to -90 then 38 to 45 sample = data[1800:2501, 3000:4501] sample = np.where(sample == 255, 0, sample) data = sample * 0.01 data = np.where(sample > 100, 1. + (sample - 100) * 0.05, data) data = np.where(sample > 180, 5. + (sample - 180) * 0.2, data) lons = np.arange(-100, -84.99, 0.01) lats = np.arange(38, 45.01, 0.01) x, y = np.meshgrid(lons, lats) buff = 0.5 m = MapPlot(sector='custom', projection='aea', west=-93.2, east=-90.3, south=42.5, north=44., title='NOAA MRMS 24 Hour RADAR-Only Precipitation Estimate', subtitle=("MRMS valid 7 AM 23 Aug 2016 to 7 AM 24 Aug 2016, " "NWS Local Storm + COOP Reports Overlaid")) clevs = [0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] m.contourf(x[:, :-1], y[:, :-1], data, clevs, cmap=nwsprecip()) nt = NetworkTable("IA_ASOS") lo = [] la = [] va = [] for sid in nt.sts.keys(): lo.append(nt.sts[sid]['lon']) la.append(nt.sts[sid]['lat']) va.append(nt.sts[sid]['name']) # m.plot_values(lo, la, va, fmt='%s', textsize=10, color='black') m.drawcounties(zorder=4, linewidth=1.) m.drawcities(labelbuffer=25, textsize=10, color='white', outlinecolor='#000000') m.textmask[:, :] = 0 m.plot_values(llons, llats, vals, fmt='%s', labelbuffer=5) m.postprocess(filename='test.png')
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day We should total files from 1 AM to midnight local time """ sts = ts.replace(hour=1) ets = sts + datetime.timedelta(hours=24) interval = datetime.timedelta(hours=1) now = sts total = None lts = None while now < ets: gmt = now.astimezone(pytz.timezone("UTC")) fn = gmt.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/" "stage4/ST4.%Y%m%d%H.01h.grib")) if os.path.isfile(fn): lts = now grbs = pygrib.open(fn) if total is None: g = grbs[1] total = g["values"] lats, lons = g.latlons() else: total += grbs[1]["values"] grbs.close() now += interval if lts is None and ts.hour > 1: print 'stage4_today_total.py found no data!' if lts is None: return lts = lts - datetime.timedelta(minutes=1) subtitle = "Total between 12:00 AM and %s" % ( lts.strftime("%I:%M %p %Z"), ) routes = 'ac' if not realtime: routes = 'a' for sector in ['iowa', 'midwest', 'conus']: pqstr = ("plot %s %s00 %s_stage4_1d.png %s_stage4_1d.png png") % ( routes, ts.strftime("%Y%m%d%H"), sector, sector) m = MapPlot(sector=sector, title="%s NCEP Stage IV Today's Precipitation" % (ts.strftime("%-d %b %Y"), ), subtitle=subtitle) clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] m.pcolormesh(lons, lats, distance(total, 'MM').value('IN'), clevs, cmap=nwsprecip(), units='inch') # map.drawstates(zorder=2) if sector == 'iowa': m.drawcounties() m.postprocess(pqstr=pqstr) m.close()
x, y = np.meshgrid(lons, lats) buff = 0.5 m = MapPlot(sector='custom', projection='aea', west=-93.2, east=-90.3, south=42.5, north=44., title='NOAA MRMS 24 Hour RADAR-Only Precipitation Estimate', subtitle=("MRMS valid 7 AM 23 Aug 2016 to 7 AM 24 Aug 2016, " "NWS Local Storm + COOP Reports Overlaid")) clevs = [0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] m.contourf(x[:, :-1], y[:, :-1], data, clevs, cmap=nwsprecip()) nt = NetworkTable("IA_ASOS") lo = [] la = [] va = [] for sid in nt.sts.keys(): lo.append(nt.sts[sid]['lon']) la.append(nt.sts[sid]['lat']) va.append(nt.sts[sid]['name']) # m.plot_values(lo, la, va, fmt='%s', textsize=10, color='black') m.map.drawcounties(zorder=4, linewidth=1.) m.drawcities(labelbuffer=25, textsize=10, color='white',
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
import matplotlib.pyplot as plt import datetime import numpy as np nc = netCDF4.Dataset("/mesonet/data/iemre/2016_mw_mrms_daily.nc", 'r') idx = iemre.daily_offset(datetime.date(2016, 7, 3)) grid = np.zeros((len(nc.dimensions['lat']), len(nc.dimensions['lon']))) total = np.zeros((len(nc.dimensions['lat']), len(nc.dimensions['lon']))) for i, x in enumerate(range(idx, idx-60, -1)): total += nc.variables['p01d'][x, :, :] grid = np.where(np.logical_and(grid == 0, total > 25.4), i, grid) m = MapPlot(sector='iowa', title='NOAA MRMS Q3: Number of Recent Days till Accumulating 1" of Precip', subtitle='valid 4 July 2016: based on per calendar day estimated preciptation, GaugeCorr and RadarOnly products') lon = np.append(nc.variables['lon'][:], [-80.5]) print lon lat = np.append(nc.variables['lat'][:], [49.]) print lat x, y = np.meshgrid(lon, lat) cmap = nwsprecip() m.pcolormesh(x, y, grid, np.arange(0, 53, 7), cmap=cmap, units='days') m.drawcounties() m.drawcities() m.postprocess(filename='test.png') m.close() nc.close()
caption='Data from Iowa Flood Center, USGS, NWS') img = Image.open("p24h_201608241200.png") data = np.flipud(np.asarray(img)) # 7000,3500 == -130,-60,55,25 === -100 to -90 then 38 to 45 sample = data[1800:2501, 3000:4501] sample = np.where(sample == 255, 0, sample) data = sample * 0.01 data = np.where(sample > 100, 1. + (sample - 100) * 0.05, data) data = np.where(sample > 180, 5. + (sample - 180) * 0.2, data) lons2 = np.arange(-100, -84.99, 0.01) lats2 = np.arange(38, 45.01, 0.01) x, y = np.meshgrid(lons2, lats2) clevs = [0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] m.contourf(x[:, :-1], y[:, :-1], data, clevs, cmap=nwsprecip()) for lon, lat, myid, c in zip(lons, lats, ids, colors): m.plot_values([lon], [lat], [myid], '%s', color=c, labelbuffer=0, outlinecolor='white') m.drawcities() m.drawcounties() m.ax.text(0.0, -0.1, "MRMS 24 Hour Precip ending 7 AM 24 Aug", transform=m.ax.transAxes) plt.gcf().set_size_inches(10, 5) plt.gcf().savefig('test.png', figsize=(9, 5))
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ idx = daily_offset(ts) with ncopen("/mesonet/data/iemre/%s_ifc_daily.nc" % (ts.year, ), timeout=300) as nc: xaxis = nc.variables["lon"][:] yaxis = nc.variables["lat"][:] total = nc.variables["p01d"][idx, :, :] lastts = datetime.datetime(ts.year, ts.month, ts.day, 23, 59) if realtime: now = datetime.datetime.now() - datetime.timedelta(minutes=60) lastts = now.replace(minute=59) subtitle = "Total between 12:00 AM and %s" % ( lastts.strftime("%I:%M %p"), ) routes = "ac" if not realtime: routes = "a" clevs = [ 0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10, ] pqstr = ("plot %s %s00 iowa_ifc_1d.png iowa_ifc_1d.png png") % ( routes, ts.strftime("%Y%m%d%H"), ) mp = MapPlot( title=("%s Iowa Flood Center Today's Precipitation") % (ts.strftime("%-d %b %Y"), ), subtitle=subtitle, sector="custom", west=xaxis[0], east=xaxis[-1], south=yaxis[0], north=yaxis[-1], ) (lons, lats) = np.meshgrid(xaxis, yaxis) mp.pcolormesh( lons, lats, distance(total, "MM").value("IN"), clevs, cmap=nwsprecip(), units="inch", ) mp.drawcounties() mp.postprocess(pqstr=pqstr, view=False) mp.close()
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day We should total files from 1 AM to midnight local time """ sts = ts.replace(hour=1) ets = sts + datetime.timedelta(hours=24) interval = datetime.timedelta(hours=1) now = sts total = None lts = None while now < ets: gmt = now.astimezone(pytz.utc) fn = gmt.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/" "stage4/ST4.%Y%m%d%H.01h.grib")) if os.path.isfile(fn): lts = now grbs = pygrib.open(fn) if total is None: total = grbs[1]["values"] lats, lons = grbs[1].latlons() else: total += grbs[1]["values"] grbs.close() now += interval if lts is None: if ts.hour > 1: LOG.info("found no data for date: %s", ts) return lts = lts - datetime.timedelta(minutes=1) subtitle = "Total between 12:00 AM and %s" % ( lts.strftime("%I:%M %p %Z"), ) routes = "ac" if not realtime: routes = "a" total = masked_array(total, units("mm")).to(units("inch")).m for sector in ["iowa", "midwest", "conus"]: pqstr = ("plot %s %s00 %s_stage4_1d.png %s_stage4_1d.png png") % ( routes, ts.strftime("%Y%m%d%H"), sector, sector, ) mp = MapPlot( sector=sector, title=("%s NCEP Stage IV Today's Precipitation") % (ts.strftime("%-d %b %Y"), ), subtitle=subtitle, ) clevs = [0.01, 0.1, 0.3, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8, 10] mp.pcolormesh(lons, lats, total, clevs, cmap=nwsprecip(), units="inch") # map.drawstates(zorder=2) if sector == "iowa": mp.drawcounties() mp.postprocess(pqstr=pqstr) mp.close()