def run(ts, routes): """ Run for a given UTC timestamp """ fn = ts.strftime("/mesonet/ARCHIVE/data/%Y/%m/%d/model/rtma/%H/rtma.t%Hz.awp2p5f000.grib2") if not os.path.isfile(fn): print "wind_power.py missing", fn return grb = pygrib.open(fn) u = grb.select(name="10 metre U wind component")[0] v = grb.select(name="10 metre V wind component")[0] mag = (u["values"] ** 2 + v["values"] ** 2) ** 0.5 mag = (mag * 1.35) ** 3 * 0.002641 # 0.002641 lats, lons = u.latlons() lts = ts.astimezone(pytz.timezone("America/Chicago")) pqstr = "plot %s %s00 midwest/rtma_wind_power.png midwest/rtma_wind_power_%s00.png png" % ( routes, ts.strftime("%Y%m%d%H"), ts.strftime("%H"), ) m = MapPlot( sector="midwest", title=r"Wind Power Potential :: (speed_mps_10m * 1.35)$^3$ * 0.002641", subtitle="valid: %s based on NOAA Realtime Mesoscale Analysis" % (lts.strftime("%d %b %Y %I %p")), ) m.pcolormesh(lons, lats, mag, numpy.array(levels), units="MW") m.postprocess(pqstr=pqstr)
def main(): """Go Main""" grbs = pygrib.open('ds.snow.bin') # skip 1-off first field total = None lats = lons = None for grb in grbs[1:]: if lats is None: lats, lons = grb.latlons() total = grb['values'] continue total += grb['values'] # TODO tz-hack here analtime = grb.analDate - datetime.timedelta(hours=5) mp = MapPlot( sector='custom', west=-100, east=-92, north=45, south=41, axisbg='tan', title=("NWS Forecasted Accumulated Snowfall " "thru 7 PM 12 April 2019"), subtitle='NDFD Forecast Issued %s' % ( analtime.strftime("%-I %p %-d %B %Y"), ) ) cmap = nwssnow() cmap.set_bad('tan') mp.pcolormesh( lons, lats, total * 39.3701, [0.01, 1, 2, 3, 4, 6, 8, 12, 18, 24, 30, 36], cmap=cmap, units='inch') mp.drawcounties() mp.drawcities() mp.postprocess(filename='test.png') mp.close()
def plot(): """Do plotting work""" cmap = plt.get_cmap('inferno_r') # cmap.set_under('black') # cmap.set_over('red') minval = (np.load('minval.npy') * units.degK).to(units.degF) maxval = (np.load('maxval.npy') * units.degK).to(units.degF) diff = maxval - minval lons = np.load('lons.npy') lats = np.load('lats.npy') mp = MapPlot(sector='conus', title=(r"Difference between warmest 3 Oct and coldest 4 " "Oct 2m Temperature"), subtitle=("based on hourly NCEP Real-Time Mesoscale Analysis " "(RTMA) ending midnight CDT")) mp.ax.text(0.5, 0.97, (r"Pixel Difference Range: %.1f$^\circ$F to %.1f$^\circ$F, " r"Domain Analysis Range: %.1f$^\circ$F to %.1f$^\circ$F" ) % (np.min(diff).magnitude, np.max(diff).magnitude, np.min(minval).magnitude, np.max(maxval).magnitude), transform=mp.ax.transAxes, fontsize=12, ha='center', bbox=dict(pad=0, color='white'), zorder=50) mp.pcolormesh(lons, lats, diff, range(0, 61, 5), cmap=cmap, clip_on=False, units=r"$^\circ$F") mp.postprocess(filename='test.png')
def make_plots(nc): ''' Generate some plots ''' sts = compute_sts(nc) lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] rts = (sts.astimezone(pytz.timezone("America/Chicago"))).strftime( "%d %b %Y %H %p") for i, tm in enumerate(nc.variables['time'][:]): dt = sts + datetime.timedelta(minutes=float(tm)) if dt.minute != 0: continue fhour = int( tm / 60.0 ) fts = (dt.astimezone(pytz.timezone("America/Chicago"))).strftime( "%d %b %Y %H %p") for pvar in PVARS: m = MapPlot(title='ISUMM5/Bridget Modelled %s' % ( PVARS[pvar]['title'],), subtitle='Model Run: %s Forecast Valid: %s' % (rts, fts)) vals = nc.variables[pvar][i,:,:] if pvar == 'bdeckt': vals = temperature(vals, 'K').value('F') m.pcolormesh(lons, lats, vals, PVARS[pvar]['levels'], units='mm') pqstr = "plot c %s model/frost/bridget/%02i/%s_%02i_f%03i.png bogus png" % ( sts.strftime("%Y%m%d%H%M"), sts.hour, pvar, sts.hour, fhour) m.postprocess(pqstr=pqstr) m.close()
def do_month(year, month, routes): """ Generate a MRMS plot for the month!""" sts = datetime.datetime(year,month,1) ets = sts + datetime.timedelta(days=35) ets = ets.replace(day=1) today = datetime.datetime.now() if ets > today: ets = today idx0 = iemre.daily_offset(sts) idx1 = iemre.daily_offset(ets) nc = netCDF4.Dataset("/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (year,), 'r') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] p01d = np.sum(nc.variables['p01d'][idx0:idx1,:,:],0) / 24.5 nc.close() m = MapPlot(sector='iowa', title='MRMS %s - %s Total Precipitation' % ( sts.strftime("%-d %b"), (ets - datetime.timedelta(days=1)).strftime("%-d %b %Y")), subtitle='Data from NOAA MRMS Project') x,y = np.meshgrid(lons, lats) bins = [0.01, 0.1, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20] m.pcolormesh(x, y, p01d, bins, units='inches') m.drawcounties() currentfn = "summary/iowa_mrms_q3_month.png" archivefn = sts.strftime("%Y/%m/summary/iowa_mrms_q3_month.png") pqstr = "plot %s %s00 %s %s png" % ( routes, sts.strftime("%Y%m%d%H"), currentfn, archivefn) m.postprocess(pqstr=pqstr)
def plot(): """Do plotting work""" cmap1 = plt.get_cmap('inferno_r') colors = list(cmap1(np.arange(10) / 10.)) cmap2 = plt.get_cmap('Pastel1') colors.extend(list(cmap2(np.arange(2) / 2.))) cmap = ListedColormap(colors) cmap.set_under('tan') cmap.set_over('white') minval = np.load('minval.npy') maxval = np.load('maxval.npy') diff = maxval - minval lons = np.load('lons.npy') lats = np.load('lats.npy') mp = MapPlot(sector='midwest', statebordercolor='white', title=(r"Diff between coldest wind chill and warmest " "air temp 29 Jan - 3 Feb 2019"), subtitle=("based on hourly NCEP Real-Time Mesoscale Analysis " "(RTMA) ending midnight CST")) levels = list(range(0, 101, 10)) levels.extend([105, 110]) mp.pcolormesh(lons, lats, diff, levels, cmap=cmap, clip_on=False, units=r"$^\circ$F", spacing='proportional') mp.postprocess(filename='test.png')
def doday(): """ Create a plot of precipitation stage4 estimates for some day """ sts = mx.DateTime.DateTime(2013,5,25,12) ets = mx.DateTime.DateTime(2013,5,31,12) interval = mx.DateTime.RelativeDateTime(days=1) now = sts total = None while now < ets: fp = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.24h.grib" % ( now.strftime("%Y/%m/%d"), now.strftime("%Y%m%d%H") ) if os.path.isfile(fp): lts = now grbs = pygrib.open(fp) if total is None: g = grbs[1] total = g["values"] lats, lons = g.latlons() else: total += grbs[1]["values"] grbs.close() now += interval m = MapPlot(sector='iowa', title='NOAA Stage IV & Iowa ASOS Precipitation', subtitle='25-30 May 2013') m.pcolormesh(lons, lats, total / 25.4, numpy.arange(0,14.1,1), latlon=True, units='inch') m.drawcounties() m.plot_values(dlons, dlats, dvals, '%.02f') m.postprocess(filename='test.svg') import iemplot iemplot.makefeature('test')
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 run(ts, routes): """ Run for a given UTC timestamp """ fn = ts.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/rtma/%H/" "rtma.t%Hz.awp2p5f000.grib2")) if not os.path.isfile(fn): print 'wind_power.py missing', fn return grb = pygrib.open(fn) try: u = grb.select(name='10 metre U wind component')[0] v = grb.select(name='10 metre V wind component')[0] except: print('Missing u/v wind for wind_power.py\nFN: %s' % (fn,)) return mag = (u['values']**2 + v['values']**2)**.5 mag = (mag * 1.35)**3 * 0.002641 # 0.002641 lats, lons = u.latlons() lts = ts.astimezone(pytz.timezone("America/Chicago")) pqstr = ("plot %s %s00 midwest/rtma_wind_power.png " "midwest/rtma_wind_power_%s00.png png" ) % (routes, ts.strftime("%Y%m%d%H"), ts.strftime("%H")) m = MapPlot(sector='midwest', title=(r'Wind Power Potential :: ' '(speed_mps_10m * 1.35)$^3$ * 0.002641'), subtitle=('valid: %s based on NOAA Realtime ' 'Mesoscale Analysis' ) % (lts.strftime("%d %b %Y %I %p"))) m.pcolormesh(lons, lats, mag, numpy.array(levels), units='MW') m.postprocess(pqstr=pqstr)
def compute(valid): ''' Get me files ''' prob = None for hr in range(-15,0): ts = valid + datetime.timedelta(hours=hr) fn = ts.strftime("hrrr.ref.%Y%m%d%H00.grib2") if not os.path.isfile(fn): continue grbs = pygrib.open(fn) gs = grbs.select(level=1000,forecastTime=(-1 * hr * 60)) ref = generic_filter(gs[0]['values'], np.max, size=10) if prob is None: lats, lons = gs[0].latlons() prob = np.zeros( np.shape(ref) ) prob = np.where(ref > 29, prob+1, prob) prob = np.ma.array(prob / 15. * 100.) prob.mask = np.ma.where(prob < 1, True, False) m = MapPlot(sector='iowa', title='HRRR Composite Forecast 4 PM 20 May 2014 30+ dbZ Reflectivity', subtitle='frequency of previous 15 model runs all valid at %s, ~15km smoothed' % (valid.astimezone(pytz.timezone("America/Chicago")).strftime("%-d %b %Y %I:%M %p %Z"),)) m.pcolormesh(lons, lats, prob, np.arange(0,101,10), units='%', clip_on=False) m.map.drawcounties() m.postprocess(filename='test.ps') m.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 = 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] = 0.01 m.pcolormesh(lons, lats, total / 24.5, clevs, units='inch') #map.drawstates(zorder=2) if sector == 'iowa': m.drawcounties() m.postprocess(pqstr=pqstr) m.close()
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot ptype = fdict.get('ptype', 'c') date = datetime.datetime.strptime(fdict.get('date', '2015-01-01'), '%Y-%m-%d') varname = fdict.get('var', 'rsds') idx0 = iemre.daily_offset(date) nc = netCDF4.Dataset(("/mesonet/data/iemre/%s_mw_daily.nc" ) % (date.year, ), 'r') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] if varname == 'rsds': # Value is in W m**-2, we want MJ data = nc.variables[varname][idx0, :, :] * 86400. / 1000000. units = 'MJ d-1' clevs = np.arange(0, 37, 3.) clevs[0] = 0.01 clevstride = 1 elif varname in ['p01d', 'p01d_12z']: # Value is in W m**-2, we want MJ data = nc.variables[varname][idx0, :, :] / 25.4 units = 'inch' 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] = 0.01 clevstride = 1 elif varname in ['high_tmpk', 'low_tmpk', 'high_tmpk_12z', 'low_tmpk_12z']: # Value is in W m**-2, we want MJ data = temperature(nc.variables[varname][idx0, :, :], 'K').value('F') units = 'F' clevs = np.arange(-30, 120, 2) clevstride = 5 nc.close() title = date.strftime("%-d %B %Y") m = MapPlot(sector='midwest', axisbg='white', nocaption=True, title='IEM Reanalysis of %s for %s' % (PDICT.get(varname), title), subtitle='Data derived from various NOAA datasets' ) if np.ma.is_masked(np.max(data)): return 'Data Unavailable' x, y = np.meshgrid(lons, lats) if ptype == 'c': m.contourf(x, y, data, clevs, clevstride=clevstride, units=units) else: m.pcolormesh(x, y, data, clevs, clevstride=clevstride, units=units) return m.fig
def do(ts, hours): """ Create a plot of precipitation stage4 estimates for some day """ ts = ts.replace(minute=0) sts = ts - datetime.timedelta(hours=hours) ets = ts interval = datetime.timedelta(hours=1) now = sts total = None lts = None while now < ets: fn = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.01h.grib" % ( now.strftime("%Y/%m/%d"), now.strftime("%Y%m%d%H") ) 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 'Missing StageIV data!' if lts is None: return cmap = cm.get_cmap("jet") cmap.set_under('white') cmap.set_over('black') clevs = [0.01,0.1,0.25,0.5,1,2,3,5,8,9.9] localtime = (ts - datetime.timedelta(minutes=1)).astimezone( pytz.timezone("America/Chicago")) for sector in ['iowa', 'midwest', 'conus']: m = MapPlot(sector=sector, title='NCEP Stage IV %s Hour Precipitation' % (hours,), subtitle='Total up to %s' % ( localtime.strftime("%d %B %Y %I %p %Z"),)) m.pcolormesh(lons, lats, total / 24.5, clevs, units='inch') pqstr = "plot %s %s00 %s_stage4_%sh.png %s_stage4_%sh_%s.png png" % ( 'ac', ts.strftime("%Y%m%d%H"), sector, hours, sector, hours, ts.strftime("%H")) if sector == 'iowa': m.drawcounties() m.postprocess(pqstr=pqstr) m.close()
def run( bcsdfn, bccafn, title ): ''' Run for a given filename! ''' nc = netCDF4.Dataset(bcsdfn, 'r') # time idx0, idx1 = get_time( nc ) # somewhat a hack for now dmul = 1.0 if (idx1 - idx0) / 19.0 < 13: dmul = 365.0 / 12.0 # Either pr or Prcp pvar = 'pr' if nc.variables.has_key('pr') else 'Prcp' pmul = 1.0 if nc.variables[pvar].units == 'mm/d' else 86400.0 bcsdprecip = np.sum(nc.variables[pvar][idx0:idx1+1,:,:],0) * dmul * pmul / 19.0 / 24.5 nc2 = netCDF4.Dataset(bccafn, 'r') # time idx0, idx1 = get_time( nc2 ) # somewhat a hack for now dmul = 1.0 if (idx1 - idx0) / 19.0 < 13: dmul = 365.0 / 12.0 # Either pr or Prcp pvar = 'pr' if nc2.variables.has_key('pr') else 'Prcp' pmul = 1.0 if nc2.variables[pvar].units == 'mm/d' else 86400.0 bccaprecip = np.sum(nc2.variables[pvar][idx0:idx1+1,:,:],0) * dmul * pmul / 19.0 / 24.5 # lat or latitude latvar = 'lat' if nc.variables.has_key('lat') else 'latitude' lats = nc.variables[latvar][:] lats = np.concatenate([lats, [lats[-1]+(lats[-1]-lats[-2])]]) # lon or longitude lonvar = 'lon' if nc.variables.has_key('lon') else 'longitude' lons = nc.variables[lonvar][:] lons = np.concatenate([lons, [lons[-1]+(lons[-1]-lons[-2])]]) print np.shape(lons), np.shape(lats), np.max(bccaprecip), np.max(bcsdprecip) title = '81-99 Precip BCCA over BCSD %s' % (title,) #subtitle = 'filename: %s' % (bccafn,) m = MapPlot(title=title, subtitle='', sector='conus', nologo=True, caption='') x,y = np.meshgrid(lons, lats) cmap = cm.get_cmap('Spectral') cmap.set_over('black') m.pcolormesh(x, y, bccaprecip / bcsdprecip * 100.0, np.arange(0,201,20), cmap=cmap, units='percentage') png = '../cplots/%s.png' % (title.replace(" ", "_").lower(),) print png m.postprocess(filename=png) nc.close() nc2.close()
def test_pcolormesh(): """See if we can do pcolormesh OKish""" mp = MapPlot(sector='custom', north=43, east=-80, west=-96, south=38, projection=reference.EPSG[2163], continentalcolor='white', nocaption=True) lons = np.arange(-100, -80, 0.25) lats = np.arange(40, 50, 0.25) vals = np.linspace(0, 1, lats.shape[0] * lons.shape[0] ).reshape([lats.shape[0], lons.shape[0]]) lons, lats = np.meshgrid(lons, lats) mp.pcolormesh(lons, lats, vals, np.arange(0, 1, 0.1)) return mp.fig
def main(): """Go Main Go""" nc = ncopen("/mesonet/data/iemre/1979_narr.nc") data = np.sum(nc.variables['apcp'][:, :, :], axis=0) m = MapPlot(sector='conus', axisbg='tan', title=(""), subtitle='', titlefontsize=16) t = distance(data, 'MM').value('IN') m.pcolormesh(nc.variables['lon'][:], nc.variables['lat'][:], t, np.arange(0, 60, 2), units='F') m.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")) 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 plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt from pyiem.plot import MapPlot ctx = get_autoplot_context(fdict, get_description()) # Covert datetime to UTC ctx['sdate'] = ctx['sdate'].replace(tzinfo=pytz.utc) ctx['edate'] = ctx['edate'].replace(tzinfo=pytz.utc) state = ctx['state'] phenomena = ctx['phenomena'] significance = ctx['significance'] station = ctx['station'][:4] t = ctx['t'] ilabel = (ctx['ilabel'] == 'yes') geo = ctx['geo'] nt = NetworkTable("WFO") if geo == 'ugc': do_ugc(ctx) elif geo == 'polygon': do_polygon(ctx) subtitle = "based on IEM Archives %s" % (ctx.get('subtitle', ''),) if t == 'cwa': subtitle = "Plotted for %s (%s), %s" % (nt.sts[station]['name'], station, subtitle) else: subtitle = "Plotted for %s, %s" % (state_names[state], subtitle) m = MapPlot(sector=('state' if t == 'state' else 'cwa'), state=state, cwa=(station if len(station) == 3 else station[1:]), axisbg='white', title=('%s %s (%s.%s)' ) % (ctx['title'], vtec.get_ps_string(phenomena, significance), phenomena, significance), subtitle=subtitle, nocaption=True, titlefontsize=16 ) if geo == 'ugc': cmap = plt.get_cmap('Paired') cmap.set_under('white') cmap.set_over('white') m.fill_ugcs(ctx['data'], ctx['bins'], cmap=cmap, ilabel=ilabel) else: cmap = plt.get_cmap('jet') cmap.set_under('white') cmap.set_over('black') res = m.pcolormesh(ctx['lons'], ctx['lats'], ctx['data'], ctx['bins'], cmap=cmap, units='count') # Cut down on SVG et al size res.set_rasterized(True) if ctx['drawc'] == 'yes': m.drawcounties() return m.fig, ctx['df']
def do_month(year, month, routes): """ Generate a MRMS plot for the month!""" sts = datetime.datetime(year, month, 1) ets = sts + datetime.timedelta(days=35) ets = ets.replace(day=1) today = datetime.datetime.now() if ets > today: ets = today idx0 = iemre.daily_offset(sts) idx1 = iemre.daily_offset(ets) nc = ncopen(iemre.get_daily_mrms_ncname(year), "r") lats = nc.variables["lat"][:] lons = nc.variables["lon"][:] p01d = distance(np.sum(nc.variables["p01d"][idx0:idx1, :, :], 0), "MM").value("IN") nc.close() mp = MapPlot( sector="iowa", title="MRMS %s - %s Total Precipitation" % ( sts.strftime("%-d %b"), (ets - datetime.timedelta(days=1)).strftime("%-d %b %Y"), ), subtitle="Data from NOAA MRMS Project", ) x, y = np.meshgrid(lons, lats) bins = [0.01, 0.1, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20] mp.pcolormesh(x, y, p01d, bins, units="inches") mp.drawcounties() currentfn = "summary/iowa_mrms_q3_month.png" archivefn = sts.strftime("%Y/%m/summary/iowa_mrms_q3_month.png") pqstr = "plot %s %s00 %s %s png" % ( routes, sts.strftime("%Y%m%d%H"), currentfn, archivefn, ) mp.postprocess(pqstr=pqstr)
def compute(valid): ''' Get me files ''' prob = None for hr in range(-15, 0): ts = valid + datetime.timedelta(hours=hr) fn = ts.strftime("/tmp/ncep_hrrr_%Y%m%d%H.grib2") print hr, fn if not os.path.isfile(fn): continue grbs = pygrib.open(fn) try: gs = grbs.select(level=1000, forecastTime=(-1 * hr * 60)) except: print fn, 'ERROR' continue ref = gs[0]['values'] #ref = generic_filter(gs[0]['values'], np.max, size=10) if prob is None: lats, lons = gs[0].latlons() prob = np.zeros(np.shape(ref)) prob = np.where(ref > 29, prob + 1, prob) prob = np.ma.array(prob / 15. * 100.) prob.mask = np.ma.where(prob < 1, True, False) m = MapPlot( sector='iowa', title='HRRR Composite Forecast 6 PM 22 Sep 2015 30+ dbZ Reflectivity', subtitle='frequency of previous 15 NCEP model runs all valid at %s' % (valid.astimezone(pytz.timezone("America/Chicago")).strftime( "%-d %b %Y %I:%M %p %Z"), )) m.pcolormesh(lons, lats, prob, np.arange(0, 101, 10), units='% of runs', clip_on=False) m.map.drawcounties() m.postprocess(filename='test.png') m.close()
def run( fn ): ''' Run for a given filename! ''' nc = netCDF4.Dataset(fn, 'r') # time idx0, idx1 = get_time( nc ) # somewhat a hack for now dmul = 1.0 if (idx1 - idx0) == 19: dmul = 1.0 elif (idx1 - idx0) / 19.0 < 13: dmul = 365.0 / 12.0 # Either pr or Prcp pvar = 'pr' if nc.variables.has_key('pr') else 'Prcp' pmul = 1.0 if nc.variables[pvar].units in ['mm/d', 'mm'] else 86400.0 precip = np.sum(nc.variables[pvar][idx0:idx1+1,:,:],0) * dmul * pmul / 19.0 / 24.5 # lat or latitude latvar = 'lat' if nc.variables.has_key('lat') else 'latitude' lats = nc.variables[latvar][:] lats = np.concatenate([lats, [lats[-1]+(lats[-1]-lats[-2])]]) # lon or longitude lonvar = 'lon' if nc.variables.has_key('lon') else 'longitude' lons = nc.variables[lonvar][:] - 360. lons = np.concatenate([lons, [lons[-1]+(lons[-1]-lons[-2])]]) print fn, np.shape(lons), np.shape(lats), np.max(precip) try: title = '2046-2065 Precip %s' % (nc.title,) except: title = '2046-2065 Yearly Average Precipitation ' subtitle = 'filename: %s' % (fn,) m = MapPlot(title=title, subtitle=subtitle, sector='conus', nologo=True, caption='') x,y = np.meshgrid(lons, lats) cmap = cm.get_cmap('gist_rainbow') cmap.set_over('black') m.pcolormesh(x, y, precip, np.array([0,1,5,10,15,20,25,30,35,40,50,60,70,80,100]), cmap=cmap, units='inch/yr') m.postprocess(filename='/tmp/plots/%s.png' % (fn.replace(".nc", ""),)) nc.close() m.close()
def doit(ts): """ Generate hourly plot of stage4 data """ gmtnow = datetime.datetime.utcnow() gmtnow = gmtnow.replace(tzinfo=pytz.utc) routes = "a" if ((gmtnow - ts).days * 86400. + (gmtnow - ts).seconds) < 7200: routes = "ac" fn = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.01h.grib" % ( ts.strftime("%Y/%m/%d"), ts.strftime("%Y%m%d%H")) if not os.path.isfile(fn): print('current/stage4_hourly.py Missing stage4 %s' % (fn,)) return grbs = pygrib.open(fn) grib = grbs[1] lats, lons = grib.latlons() vals = grib.values / 25.4 cmap = cm.get_cmap("jet") cmap.set_under('white') cmap.set_over('black') clevs = [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3] localtime = ts.astimezone(pytz.timezone("America/Chicago")) for sector in ['iowa', 'midwest', 'conus']: mp = MapPlot(sector=sector, title='Stage IV One Hour Precipitation', subtitle='Hour Ending %s' % ( localtime.strftime("%d %B %Y %I %p %Z"),)) mp.pcolormesh(lons, lats, vals, clevs, units='inch') pqstr = "plot %s %s00 %s_stage4_1h.png %s_stage4_1h_%s.png png" % ( routes, ts.strftime("%Y%m%d%H"), sector, sector, ts.strftime("%H")) if sector == 'iowa': mp.drawcounties() mp.postprocess(view=False, pqstr=pqstr) mp.close()
def run( fn ): ''' Run for a given filename! ''' nc = netCDF4.Dataset(fn, 'r') # time idx0, idx1 = get_time( nc ) # somewhat a hack for now dmul = 1.0 if (idx1 - idx0) == 19: dmul = 1.0 elif (idx1 - idx0) / 19.0 < 13: dmul = 365.0 / 12.0 pvar = 'tmin' tmpf = c2f(np.average(nc.variables[pvar][idx0:idx1+1,:,:],0) * dmul ) # lat or latitude latvar = 'lat' if nc.variables.has_key('lat') else 'latitude' lats = nc.variables[latvar][:] lats = np.concatenate([lats, [lats[-1]+(lats[-1]-lats[-2])]]) # lon or longitude lonvar = 'lon' if nc.variables.has_key('lon') else 'longitude' lons = nc.variables[lonvar][:] - 360. lons = np.concatenate([lons, [lons[-1]+(lons[-1]-lons[-2])]]) print fn, np.shape(lons), np.shape(lats), np.max(tmpf) try: title = '2046-2065 Average Daily High Temp %s' % (nc.title,) except: title = '2046-2065 Average Daily Low Temperature ' subtitle = 'filename: %s' % (fn,) m = MapPlot(title=title, subtitle=subtitle, sector='conus', nologo=True, caption='') x,y = np.meshgrid(lons, lats) cmap = cm.get_cmap('gist_rainbow') cmap.set_over('black') m.pcolormesh(x, y, tmpf, np.arange(50,120,10), cmap=cmap, units='F') m.postprocess(filename='/tmp/plots/%s.png' % (fn.replace(".nc", ""),)) nc.close() m.close()
def main(): """Go Main""" nc = netCDF4.Dataset('/tmp/sfav2_CONUS_2018093012_to_2019021312.nc') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] data = nc.variables['Data'][:] * 1000. / 25.4 nc.close() mp = MapPlot( sector='iowa', continentalcolor='tan', title=("National Snowfall Analysis - NOHRSC " "- Season Total Snowfall"), subtitle='Snowfall up until 7 AM 13 Feb 2019') cmap = plt.get_cmap('terrain_r') levs = [0.1, 2, 5, 8, 12, 18, 24, 30, 36, 42, 48] mp.pcolormesh( lons, lats, data, levs, cmap=cmap, units='inch', clip_on=False, spacing='proportional' ) mp.drawcounties() mp.drawcities() mp.postprocess(filename='test.png')
def compute(valid): """ Get me files """ prob = None for hr in range(-15, 0): ts = valid + datetime.timedelta(hours=hr) fn = ts.strftime("/tmp/ncep_hrrr_%Y%m%d%H.grib2") print hr, fn if not os.path.isfile(fn): continue grbs = pygrib.open(fn) try: gs = grbs.select(level=1000, forecastTime=(-1 * hr * 60)) except: print fn, "ERROR" continue ref = gs[0]["values"] # ref = generic_filter(gs[0]['values'], np.max, size=10) if prob is None: lats, lons = gs[0].latlons() prob = np.zeros(np.shape(ref)) prob = np.where(ref > 29, prob + 1, prob) prob = np.ma.array(prob / 15.0 * 100.0) prob.mask = np.ma.where(prob < 1, True, False) m = MapPlot( sector="iowa", title="HRRR Composite Forecast 6 PM 22 Sep 2015 30+ dbZ Reflectivity", subtitle="frequency of previous 15 NCEP model runs all valid at %s" % (valid.astimezone(pytz.timezone("America/Chicago")).strftime("%-d %b %Y %I:%M %p %Z"),), ) m.pcolormesh(lons, lats, prob, np.arange(0, 101, 10), units="% of runs", clip_on=False) m.map.drawcounties() m.postprocess(filename="test.png") m.close()
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot ptype = fdict.get('ptype', 'c') date = datetime.datetime.strptime(fdict.get('date', '2015-01-01'), '%Y-%m-%d') varname = fdict.get('var', 'rsds') idx0 = iemre.daily_offset(date) nc = netCDF4.Dataset(("/mesonet/data/iemre/%s_mw_daily.nc" ) % (date.year, ), 'r') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] if varname == 'rsds': # Value is in W m**-2, we want MJ data = nc.variables['rsds'][idx0, :, :] * 86400. / 1000000. units = 'MJ d-1' nc.close() title = date.strftime("%-d %B %Y") m = MapPlot(sector='midwest', axisbg='white', nocaption=True, title='IEM Reanalysis of %s for %s' % (PDICT.get(varname), title), subtitle='Data derived from various NOAA datasets' ) if np.ma.is_masked(np.max(data)): return 'Data Unavailable' clevs = np.arange(0, 37, 3.) clevs[0] = 0.01 x, y = np.meshgrid(lons, lats) if ptype == 'c': m.contourf(x, y, data, clevs, units=units) else: m.pcolormesh(x, y, data, clevs, units=units) return m.fig
def doit(ts): """ Generate hourly plot of stage4 data """ routes = "a" if ((gmtnow - ts).days * 86400. + (gmtnow - ts).seconds) < 7200: routes = "ac" fn = "/mesonet/ARCHIVE/data/%s/stage4/ST4.%s.01h.grib" % ( ts.strftime("%Y/%m/%d"), ts.strftime("%Y%m%d%H") ) if not os.path.isfile(fn): print 'current/stage4_hourly.py Missing stage4 %s' % (fn,) return grbs = pygrib.open(fn) grib = grbs[1] lats, lons = grib.latlons() vals = grib.values / 25.4 cmap = cm.get_cmap("jet") cmap.set_under('white') cmap.set_over('black') clevs = [0.01,0.05,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.5,2,3] localtime = ts.astimezone(pytz.timezone("America/Chicago")) for sector in ['iowa', 'midwest', 'conus']: m = MapPlot(sector=sector, title='Stage IV One Hour Precipitation', subtitle='Hour Ending %s' % ( localtime.strftime("%d %B %Y %I %p %Z"),)) m.pcolormesh(lons, lats, vals, clevs, units='inch') pqstr = "plot %s %s00 %s_stage4_1h.png %s_stage4_1h_%s.png png" % ( routes, ts.strftime("%Y%m%d%H"), sector, sector, ts.strftime("%H")) if sector == 'iowa': m.drawcounties() m.postprocess(view=False, pqstr=pqstr) 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 main(): """Go Main Go""" pgconn = get_dbconn('postgis') cursor = pgconn.cursor() sts = datetime.date(2018, 1, 24) ets = datetime.date(2017, 8, 1) now = sts xaxis = np.arange(reference.IA_WEST, reference.IA_EAST, GX) yaxis = np.arange(reference.IA_SOUTH, reference.IA_NORTH, GX) """ days = np.ones((len(yaxis), len(xaxis)), 'i') * -1 count = 0 while now >= ets: cursor.execute("" select distinct st_x(geom) as lon, st_y(geom) as lat from nldn_all n, states s WHERE n.valid >= %s and n.valid < %s and s.state_abbr = 'IA' and n.geom && s.the_geom "", (now, now + datetime.timedelta(days=1))) print("date: %s rows: %s" % (now, cursor.rowcount)) for row in cursor: yidx = int((row[1] - reference.IA_SOUTH) / GX) xidx = int((row[0] - reference.IA_WEST) / GX) if days[yidx, xidx] < 0: days[yidx, xidx] = count now -= datetime.timedelta(days=1) count += 1 np.save('days', days) """ days = np.load('days.npy') mp = MapPlot() ramp = [1, 24, 55, 85, 116, 146, 177] mp.pcolormesh(xaxis, yaxis, days, ramp, cmap=plt.get_cmap('afmhot_r')) mp.postprocess(filename='test.png') mp.close()
def main(): """Go Main Go""" ets = datetime.datetime.now() - datetime.timedelta(days=1) sts = datetime.datetime(ets.year, 1, 1) # Get the normal accumm with ncopen(iemre.get_dailyc_ncname()) as cnc: lons = cnc.variables["lon"][:] lats = cnc.variables["lat"][:] index0 = iemre.daily_offset(sts) index1 = iemre.daily_offset(ets) clprecip = np.sum(cnc.variables["p01d"][index0:index1, :, :], 0) with ncopen(iemre.get_daily_ncname(sts.year)) as nc: obprecip = np.sum(nc.variables["p01d"][index0:index1, :, :], 0) lons, lats = np.meshgrid(lons, lats) # Plot departure from normal mp = MapPlot( sector="midwest", title=("Precipitation Departure %s - %s") % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle="based on IEM Estimates", ) mp.pcolormesh(lons, lats, (obprecip - clprecip) / 25.4, np.arange(-10, 10, 1)) mp.postprocess( pqstr="plot c 000000000000 summary/year/stage4_diff.png bogus png") mp.close() # Plot normals mp = MapPlot( sector="midwest", title=("Normal Precipitation:: %s - %s") % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle="based on IEM Estimates", ) mp.pcolormesh(lons, lats, (clprecip) / 25.4, np.arange(0, 30, 2)) mp.postprocess( pqstr="plot c 000000000000 summary/year/stage4_normals.png bogus png") mp.close() # Plot Obs mp = MapPlot( sector="midwest", title=("Estimated Precipitation:: %s - %s") % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle="based on IEM Estimates", ) mp.pcolormesh(lons, lats, (obprecip) / 25.4, np.arange(0, 30, 2)) mp.postprocess( pqstr="plot c 000000000000 summary/year/stage4obs.png bogus png") mp.close()
def do_month(year, month, routes): """ Generate a MRMS plot for the month!""" sts = datetime.datetime(year, month, 1) ets = sts + datetime.timedelta(days=35) ets = ets.replace(day=1) today = datetime.datetime.now() if ets > today: ets = today idx0 = iemre.daily_offset(sts) idx1 = iemre.daily_offset(ets) nc = netCDF4.Dataset("/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (year, ), 'r') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] p01d = distance(np.sum(nc.variables['p01d'][idx0:idx1, :, :], 0), 'MM').value('IN') nc.close() mp = MapPlot(sector='iowa', title='MRMS %s - %s Total Precipitation' % (sts.strftime("%-d %b"), (ets - datetime.timedelta(days=1)).strftime("%-d %b %Y")), subtitle='Data from NOAA MRMS Project') x, y = np.meshgrid(lons, lats) bins = [0.01, 0.1, 0.5, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20] mp.pcolormesh(x, y, p01d, bins, units='inches') mp.drawcounties() currentfn = "summary/iowa_mrms_q3_month.png" archivefn = sts.strftime("%Y/%m/summary/iowa_mrms_q3_month.png") pqstr = "plot %s %s00 %s %s png" % (routes, sts.strftime("%Y%m%d%H"), currentfn, archivefn) mp.postprocess(pqstr=pqstr)
def run(ts, routes): """ Run for a given UTC timestamp """ fn = ts.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/rtma/%H/" "rtma.t%Hz.awp2p5f000.grib2")) if not os.path.isfile(fn): print("rtma/wind_power.py missing %s" % (fn, )) return grb = pygrib.open(fn) try: u = grb.select(name="10 metre U wind component")[0] v = grb.select(name="10 metre V wind component")[0] except Exception as exp: print("Missing u/v wind for wind_power.py\nFN: %s\n%s" % (fn, exp)) return mag = np.hypot(u["values"], v["values"]) mag = (mag * 1.35)**3 * 0.002641 # 0.002641 lats, lons = u.latlons() lts = ts.astimezone(pytz.timezone("America/Chicago")) pqstr = ("plot %s %s00 midwest/rtma_wind_power.png " "midwest/rtma_wind_power_%s00.png png") % ( routes, ts.strftime("%Y%m%d%H"), ts.strftime("%H")) mp = MapPlot( sector="midwest", title=(r"Wind Power Potential :: " "(speed_mps_10m * 1.35)$^3$ * 0.002641"), subtitle=("valid: %s based on NOAA Realtime " "Mesoscale Analysis") % (lts.strftime("%d %b %Y %I %p")), ) mp.pcolormesh(lons, lats, mag, np.array(LEVELS), units="MW") mp.postprocess(pqstr=pqstr) mp.close()
def do(valid): """Do Something""" 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] = 0.01 precip = load_precip(valid) stage4 = load_precip(valid, 'stage4') inqcprecip = load_precip(valid, 'inqcprecip') outqcprecip = load_precip(valid, 'outqcprecip') multiplier = load_precip(valid, 'multiplier') yidx = int((43.27 - SOUTH) / 0.01) xidx = int((-94.39 - WEST) / 0.01) print(("yidx:%s xidx:%s precip:%.2f stage4: %.2f inqc: %.2f outqc: %.2f " "mul: %.2f" ) % (yidx, xidx, precip[yidx, xidx], stage4[yidx, xidx], inqcprecip[yidx, xidx], outqcprecip[yidx, xidx], multiplier[yidx, xidx])) mp = MapPlot(sector=SECTOR, axisbg='white', title=('%s DEP Quality Controlled Precip Totals' ) % (valid.strftime("%-d %b %Y"), )) (lons, lats) = np.meshgrid(XS, YS) mp.pcolormesh(lons, lats, precip / 25.4, clevs, units='inch') mp.drawcounties() mp.postprocess(filename='qc.png') mp.close() mp = MapPlot(sector=SECTOR, axisbg='white', title=('%s Stage IV Precip Totals' ) % (valid.strftime("%-d %b %Y"), )) mp.pcolormesh(lons, lats, stage4 / 25.4, clevs, units='inch') mp.drawcounties() mp.postprocess(filename='stageIV.png') mp.close() mp = MapPlot(sector=SECTOR, axisbg='white', title=('%s High-res total prior to QC' ) % (valid.strftime("%-d %b %Y"), )) mp.pcolormesh(lons, lats, inqcprecip / 25.4, clevs, units='inch') mp.drawcounties() mp.postprocess(filename='inqcprecip.png') mp.close() mp = MapPlot(sector=SECTOR, axisbg='white', title=('%s High-res total after QC' ) % (valid.strftime("%-d %b %Y"), )) mp.pcolormesh(lons, lats, outqcprecip / 25.4, clevs, units='inch') mp.drawcounties() mp.postprocess(filename='outqcprecip.png') mp.close() mp = MapPlot(sector=SECTOR, axisbg='white', title=('%s QC Change in Precip Out - In' ) % (valid.strftime("%-d %b %Y"), )) diff = (outqcprecip - inqcprecip) / 25.4 mp.pcolormesh(lons, lats, diff, np.arange(-1.4, 1.5, 0.1), cmap=plt.get_cmap('BrBG'), units='inch') mp.drawcounties() mp.postprocess(filename='qcprecipdiff.png') mp.close() mp = MapPlot(sector=SECTOR, axisbg='white', title=('%s multiplier' ) % (valid.strftime("%-d %b %Y"), )) mp.pcolormesh(lons, lats, multiplier, np.arange(0.0, 2.5, 0.2), cmap=plt.get_cmap('jet'), units='ratio') mp.drawcounties() mp.postprocess(filename='multiplier.png') mp.close() mp = MapPlot(sector=SECTOR, axisbg='white', title=('%s manually computed QC Precip mul * stage4' ) % (valid.strftime("%-d %b %Y"), )) mp.pcolormesh(lons, lats, multiplier * stage4 / 25.4, clevs, units='inch') mp.drawcounties() mp.postprocess(filename='qcmancalc.png') mp.close() # Go MRMS ncfn = "/mesonet/data/iemre/%s_mw_mrms_daily.nc" % (valid.year, ) if not os.path.isfile(ncfn): return nc = netCDF4.Dataset(ncfn, 'r') xidx = int((WEST - nc.variables['lon'][0]) * 100.) yidx = int((SOUTH - nc.variables['lat'][0]) * 100.) idx = daily_offset(valid) mrms = nc.variables['p01d'][idx, yidx:(yidx+800), xidx:(xidx+921)] nc.close()
lons = fut_nc.variables['longitude'][:] x, y = numpy.meshgrid(lons, lats) offset1, offset2 = numpy.digitize([days1,days2], obs_nc.variables['time'][:]) offset3, offset4 = numpy.digitize([days3,days4], fut_nc.variables['time'][:]) print offset1, offset2, offset2 - offset1 print offset3, offset4, offset4 - offset3 jan = numpy.average(obs_nc.variables['Prcp'][offset1+0:offset2:12,:,:],0) feb = numpy.average(obs_nc.variables['Prcp'][offset1+1:offset2:12,:,:],0) dec = numpy.average(obs_nc.variables['Prcp'][offset1+11:offset2:12,:,:],0) obs = (jan + feb + dec)/3.0 jan = numpy.average(obs_nc.variables['Prcp'][offset3+0:offset4:12,:,:],0) feb = numpy.average(obs_nc.variables['Prcp'][offset3+1:offset4:12,:,:],0) dec = numpy.average(obs_nc.variables['Prcp'][offset3+11:offset4:12,:,:],0) fut = (jan + feb + dec)/3.0 ratio = numpy.ma.array(fut / obs) ratio.mask = numpy.where(ratio == 1, True, False) print numpy.min(ratio), numpy.max(ratio) m = MapPlot(sector='conus', caption='', nologo=True, title=r'MIROC3.2 exp1 BCSD DJF Precipitation Ratio A1B / C20', subtitle='Period: Jan 2046 - Dec 2064 divided by Jan 1981- Dec 1999') m.pcolormesh(x, y, ratio, numpy.arange(0,2.5,0.1), latlon=True, units='ratio') m.postprocess(filename='djf_ratio_bcsd_miroc_only.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.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()
continue g = temperature(gs[0]['values'], 'K') g2 = temperature(gs2[0]['values'], 'K') if now == sts: lats, lons = gs[0].latlons() largest = np.zeros(np.shape(g)) delta = g.value('F') - g2.value('F') largest = np.minimum(delta, largest) now += interval np.save('maxinterval.npy', np.array(largest)) np.save('lats.npy', lats) np.save('lons.npy', lons) #do() #sys.exit() maxinterval = np.load('maxinterval.npy') print np.max(maxinterval), np.min(maxinterval) lats = np.load('lats.npy') lons = np.load('lons.npy') m = MapPlot( sector='conus', axisbg='#EEEEEE', title='16 - 18 March 2015 :: Coldest 24 Hour Temperature Change', subtitle='based on hourly HRRR Analyses 7 AM 16 Mar thru 5 AM 18 Mar') m.pcolormesh(lons, lats, maxinterval, range(-50, 1, 5), units='F') m.postprocess(filename='test.png')
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
import numpy as np import matplotlib.patheffects as PathEffects img = Image.open("p48h_201410150000.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) m = MapPlot(sector='iowa', title='NOAA MRMS 48 Hour Precipitation Estimate', subtitle='7 PM 12 Oct 2014 to 7 PM 14 Oct 2014') clevs = np.arange(0,0.2,0.05) clevs = np.append(clevs, np.arange(0.2, 1.0, 0.1)) clevs = np.append(clevs, np.arange(1.0, 5.0, 0.25)) clevs = np.append(clevs, np.arange(5.0, 10.0, 1.0)) clevs[0] = 0.01 m.pcolormesh(x, y, data, clevs) m.map.drawcounties(zorder=10, linewidth=1.) m.postprocess(filename='test.png')
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.) 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 """ ctx = get_autoplot_context(fdict, get_description()) csector = ctx['csector'] sdate = make_tuesday(ctx['sdate']) edate = make_tuesday(ctx['edate']) dlevel = ctx['d'] griddelta = 0.1 mp = MapPlot(sector=('state' if len(csector) == 2 else csector), state=ctx['csector'], title=('%s at or above "%s" %s - %s') % (PDICT2[ctx['w']], PDICT[dlevel], sdate.strftime("%b %-d, %Y"), edate.strftime("%b %-d, %Y")), subtitle=('based on weekly US Drought Monitor Analysis, ' '%.2f$^\circ$ grid analysis') % (griddelta, ), continentalcolor='white', titlefontsize=14) # compute the affine (west, east, south, north) = mp.ax.get_extent(ccrs.PlateCarree()) raster = np.zeros((int( (north - south) / griddelta), int((east - west) / griddelta))) lons = np.arange(raster.shape[1]) * griddelta + west lats = np.arange(0, 0 - raster.shape[0], -1) * griddelta + north lats = lats[::-1] affine = Affine(griddelta, 0., west, 0., 0 - griddelta, north) # get the geopandas data pgconn = get_dbconn('postgis') df = read_postgis(""" with d as ( select valid, (ST_Dump(st_simplify(geom, 0.01))).geom from usdm where valid >= %s and valid <= %s and dm >= %s and ST_Intersects(geom, ST_GeomFromEWKT('SRID=4326;POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))')) ) select valid, st_collect(geom) as the_geom from d GROUP by valid """, pgconn, params=(sdate, edate, dlevel, west, south, west, north, east, north, east, south, west, south), geom_col='the_geom') if df.empty: raise NoDataFound("No Data Found, sorry!") # loop over the cached stats czs = CachingZonalStats(affine) czs.compute_gridnav(df['the_geom'], raster) for nav in czs.gridnav: if nav is None: continue grid = np.ones((nav.ysz, nav.xsz)) grid[nav.mask] = 0. jslice = slice(nav.y0, nav.y0 + nav.ysz) islice = slice(nav.x0, nav.x0 + nav.xsz) raster[jslice, islice] += grid maxval = 10 if np.max(raster) < 11 else np.max(raster) ramp = np.linspace(1, maxval + 1, 11, dtype='i') if ctx['w'] == 'percent': ramp = np.arange(0, 101, 10) ramp[0] = 1. ramp[-1] = 100.1 # we add one since we are rectified to tuesdays, so we have an extra # week in there raster = raster / ((edate - sdate).days / 7. + 1.) * 100. # plot cmap = stretch_cmap(ctx['cmap'], ramp) cmap.set_under('white') cmap.set_bad('white') mp.pcolormesh(lons, lats, np.flipud(raster), ramp, cmap=cmap, units='count' if ctx['w'] == 'weeks' else 'Percent') if len(csector) == 2: mp.drawcounties() mp.drawcities() rows = [] for j in range(raster.shape[0]): for i in range(raster.shape[1]): rows.append(dict(lon=lons[i], lat=lats[j], value=raster[j, i])) return mp.fig, pd.DataFrame(rows)
maxinterval = np.where(current > maxinterval, current, maxinterval) now += interval np.save('maxinterval.npy', np.array(maxinterval)) np.save('lats.npy', lats) np.save('lons.npy', lons) #do() maxinterval = np.load('maxinterval.npy') lats = np.load('lats.npy') lons = np.load('lons.npy') m = MapPlot( sector='conus', axisbg='#EEEEEE', title='1-22 November 2014 :: NCEP HRRR Maximum Period Below Freezing', subtitle= 'based on hourly HRRR 2 meter AGL Temperature Analysis, thru 22 Nov 12 PM CST' ) m.pcolormesh( lons, lats, maxinterval, [ 1, 3, 6, 12, 24, 48, 72, 5 * 24, 7 * 24, 9 * 24, 11 * 24, 13 * 24, 15 * 24 ], units='time', clevlabels=['1h', '3h', '6h', '12h', '1d', '2', 3, 5, 7, 9, 11, 13, 15]) m.postprocess(filename='test.png')
x, y = np.meshgrid(lons, lats) buff = 0.5 m = MapPlot( sector='iowa', #west=min(llons)-buff, #east=max(llons)+buff, south=min(llats)-buff, #north=max(llats)+buff, title='NOAA MRMS 48 Hour RADAR-Only Precipitation Estimate', subtitle='MRMS valid 5 AM 6 Sep 2015 to 5 AM 8 Sep 2015') clevs = np.arange(0, 0.2, 0.05) clevs = np.append(clevs, np.arange(0.2, 1.0, 0.1)) clevs = np.append(clevs, np.arange(1.0, 5.0, 0.25)) clevs = np.append(clevs, np.arange(5.0, 10.0, 1.0)) clevs[0] = 0.01 m.pcolormesh(x, y, data, clevs) 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.plot_values(llons, llats, vals, fmt='%s') m.map.drawcounties(zorder=10, linewidth=1.)
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) ts = ctx["ts"].replace(tzinfo=pytz.utc) hour = int(ctx["hour"]) ilabel = ctx["ilabel"] == "yes" plot = MapPlot( sector=ctx["t"], continentalcolor="white", state=ctx["state"], cwa=ctx["wfo"], title=("NWS RFC %s Hour Flash Flood Guidance on %s UTC") % (hour, ts.strftime("%-d %b %Y %H")), subtitle=("Estimated amount of %s Rainfall " "needed for non-urban Flash Flooding to commence") % (HOURS[ctx["hour"]], ), ) cmap = plt.get_cmap(ctx["cmap"]) bins = [ 0.01, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.25, 2.5, 2.75, 3.0, 3.5, 4.0, 5.0, ] if ts.year < 2019: column = "hour%02i" % (hour, ) pgconn = get_dbconn("postgis") df = read_sql( """ WITH data as ( SELECT ugc, rank() OVER (PARTITION by ugc ORDER by valid DESC), hour01, hour03, hour06, hour12, hour24 from ffg WHERE valid >= %s and valid <= %s) SELECT *, substr(ugc, 3, 1) as ztype from data where rank = 1 """, pgconn, params=(ts - datetime.timedelta(hours=24), ts), index_col="ugc", ) df2 = df[df["ztype"] == "C"] plot.fill_ugcs( df2[column].to_dict(), bins, cmap=cmap, plotmissing=False, ilabel=ilabel, ) df2 = df[df["ztype"] == "Z"] plot.fill_ugcs( df2[column].to_dict(), bins, cmap=cmap, plotmissing=False, units="inches", ilabel=ilabel, ) else: # use grib data ts -= datetime.timedelta(hours=(ts.hour % 6)) ts = ts.replace(minute=0) fn = None for offset in range(0, 24, 4): ts2 = ts - datetime.timedelta(hours=offset) testfn = ts2.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/ffg/" "5kmffg_%Y%m%d00.grib2")) if os.path.isfile(testfn): fn = testfn break if fn is None: raise NoDataFound("No valid grib data found!") grbs = pygrib.index(fn, "stepRange") grb = grbs.select(stepRange="0-%s" % (hour, ))[0] lats, lons = grb.latlons() data = (masked_array(grb.values, data_units=units("mm")).to(units("inch")).m) plot.pcolormesh(lons, lats, data, bins, cmap=cmap) if ilabel: plot.drawcounties() df = pd.DataFrame() return plot.fig, df
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 main(argv): """Go Main Go""" nt = Table("ISUSM") qdict = loadqc() idbconn = get_dbconn("isuag", user="******") pdbconn = get_dbconn("postgis", user="******") day_ago = int(argv[1]) ts = datetime.date.today() - datetime.timedelta(days=day_ago) hlons, hlats, hvals = do_nam(ts) nam = temperature(hvals, "K").value("F") window = np.ones((3, 3)) nam = convolve2d(nam, window / window.sum(), mode="same", boundary="symm") # mp = MapPlot(sector='midwest') # mp.pcolormesh(hlons, hlats, nam, # range(20, 90, 5)) # mp.postprocess(filename='test.png') # sys.exit() # Query out the data df = read_sql( """ WITH ranges as ( select station, count(*), min(tsoil_c_avg_qc), max(tsoil_c_avg_qc) from sm_hourly WHERE valid >= %s and valid < %s and tsoil_c_avg_qc > -40 and tsoil_c_avg_qc < 50 GROUP by station ) SELECT d.station, d.tsoil_c_avg_qc, r.max as hourly_max_c, r.min as hourly_min_c, r.count from sm_daily d JOIN ranges r on (d.station = r.station) where valid = %s and tsoil_c_avg_qc > -40 and r.count > 19 """, idbconn, params=(ts, ts + datetime.timedelta(days=1), ts), index_col="station", ) for col, newcol in zip( ["tsoil_c_avg_qc", "hourly_min_c", "hourly_max_c"], ["ob", "min", "max"], ): df[newcol] = temperature(df[col].values, "C").value("F") df.drop(col, axis=1, inplace=True) for stid, row in df.iterrows(): df.at[stid, "ticket"] = qdict.get(stid, {}).get("soil4", False) x, y = get_idx(hlons, hlats, nt.sts[stid]["lon"], nt.sts[stid]["lat"]) df.at[stid, "nam"] = nam[x, y] df.at[stid, "lat"] = nt.sts[stid]["lat"] df.at[stid, "lon"] = nt.sts[stid]["lon"] # ticket is an object type from above df = df[~df["ticket"].astype("bool")] df["diff"] = df["ob"] - df["nam"] bias = df["diff"].mean() nam = nam + bias print("fancy_4inch NAM bias correction of: %.2fF applied" % (bias, )) # apply nam bias to sampled data df["nam"] += bias df["diff"] = df["ob"] - df["nam"] # we are going to require data be within 1 SD of sampled or 5 deg std = 5.0 if df["nam"].std() < 5.0 else df["nam"].std() for station in df[df["diff"].abs() > std].index.values: print(("fancy_4inch %s QC'd %s out std: %.2f, ob:%.1f nam:%.1f") % ( ts.strftime("%Y%m%d"), station, std, df.at[station, "ob"], df.at[station, "nam"], )) df.drop(station, inplace=True) # Query out centroids of counties... cdf = read_sql( """SELECT ST_x(ST_centroid(the_geom)) as lon, ST_y(ST_centroid(the_geom)) as lat from uscounties WHERE state_name = 'Iowa' """, pdbconn, index_col=None, ) for i, row in cdf.iterrows(): x, y = get_idx(hlons, hlats, row["lon"], row["lat"]) cdf.at[i, "nam"] = nam[x, y] mp = MapPlot( sector="iowa", title=("Average 4 inch Depth Soil Temperatures for %s") % (ts.strftime("%b %d, %Y"), ), subtitle=("County est. based on bias adj. " "NWS NAM Model (black numbers), " "ISUSM network observations (red numbers)"), ) mp.pcolormesh( hlons, hlats, nam, np.arange(10, 101, 5), cmap=cm.get_cmap("jet"), units=r"$^\circ$F", ) mp.plot_values(df["lon"], df["lat"], df["ob"], fmt="%.0f", color="r", labelbuffer=5) mp.plot_values( cdf["lon"], cdf["lat"], cdf["nam"], fmt="%.0f", textsize=11, labelbuffer=5, ) mp.drawcounties() routes = "a" if day_ago >= 4 else "ac" pqstr = ("plot %s %s0000 soilt_day%s.png isuag_county_4inch_soil.png png" ) % (routes, ts.strftime("%Y%m%d"), day_ago) mp.postprocess(pqstr=pqstr) mp.close()
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) ptype = ctx['ptype'] date = ctx['date'] varname = ctx['var'] csector = ctx['csector'] title = date.strftime("%-d %B %Y") mp = MapPlot(sector=('state' if len(csector) == 2 else csector), state=ctx['csector'], axisbg='white', nocaption=True, title='IEM Reanalysis of %s for %s' % (PDICT.get(varname), title), subtitle='Data derived from various NOAA datasets') (west, east, south, north) = mp.ax.get_extent(ccrs.PlateCarree()) i0, j0 = iemre.find_ij(west, south) i1, j1 = iemre.find_ij(east, north) jslice = slice(j0, j1) islice = slice(i0, i1) idx0 = iemre.daily_offset(date) with ncopen(iemre.get_daily_ncname(date.year)) as nc: lats = nc.variables['lat'][jslice] lons = nc.variables['lon'][islice] cmap = ctx['cmap'] if varname in ['rsds', 'power_swdn']: # Value is in W m**-2, we want MJ multi = (86400. / 1000000.) if varname == 'rsds' else 1 data = nc.variables[varname][idx0, jslice, islice] * multi units = 'MJ d-1' clevs = np.arange(0, 37, 3.) clevs[0] = 0.01 clevstride = 1 elif varname in [ 'wind_speed', ]: data = speed(nc.variables[varname][idx0, jslice, islice], 'MPS').value('MPH') units = 'mph' clevs = np.arange(0, 41, 2) clevs[0] = 0.01 clevstride = 2 elif varname in ['p01d', 'p01d_12z', 'snow_12z', 'snowd_12z']: # Value is in W m**-2, we want MJ data = distance(nc.variables[varname][idx0, jslice, islice], 'MM').value('IN') units = 'inch' 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] = 0.01 clevstride = 1 cmap = stretch_cmap(ctx['cmap'], clevs) elif varname in [ 'high_tmpk', 'low_tmpk', 'high_tmpk_12z', 'low_tmpk_12z', 'avg_dwpk' ]: # Value is in W m**-2, we want MJ data = temperature(nc.variables[varname][idx0, jslice, islice], 'K').value('F') units = 'F' clevs = np.arange(-30, 120, 5) clevstride = 2 elif varname in ['range_tmpk', 'range_tmpk_12z']: vname1 = 'high_tmpk%s' % ('_12z' if varname == 'range_tmpk_12z' else '', ) vname2 = 'low_tmpk%s' % ('_12z' if varname == 'range_tmpk_12z' else '', ) d1 = nc.variables[vname1][idx0, jslice, islice] d2 = nc.variables[vname2][idx0, jslice, islice] data = (temperature(d1, 'K').value('F') - temperature(d2, 'K').value('F')) units = 'F' clevs = np.arange(0, 61, 5) clevstride = 2 if np.ma.is_masked(np.max(data)): raise ValueError("Data Unavailable") x, y = np.meshgrid(lons, lats) if ptype == 'c': # in the case of contour, use the centroids on the grids mp.contourf(x + 0.125, y + 0.125, data, clevs, clevstride=clevstride, units=units, ilabel=True, labelfmt='%.0f', cmap=cmap) else: x, y = np.meshgrid(lons, lats) mp.pcolormesh(x, y, data, clevs, clevstride=clevstride, cmap=cmap, units=units) return mp.fig
onc.close() else: ncfn = "/mesonet/data/iemre/%s_mw_daily.nc" % (sts.year, ) onc = netCDF4.Dataset(ncfn, 'r') obprecip = np.sum(onc.variables['p01d'][index0:index1, :, :], 0) onc.close() lons, lats = np.meshgrid(lons, lats) # Plot departure from normal m = MapPlot(sector='midwest', title='Precipitation Departure %s - %s' % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle='based on IEM Estimates') m.pcolormesh(lons, lats, (obprecip - clprecip) / 25.4, np.arange(-10, 10, 1)) m.postprocess(pqstr="plot c 000000000000 summary/4mon_diff.png bogus png") # Plot normals m = MapPlot(sector='midwest', title='Normal Precipitation:: %s - %s' % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle='based on IEM Estimates') m.pcolormesh(lons, lats, (clprecip) / 25.4, np.arange(0, 30, 2)) m.postprocess(pqstr="plot c 000000000000 summary/4mon_normals.png bogus png") # Plot Obs m = MapPlot(sector='midwest', title='Estimated Precipitation:: %s - %s' % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")),
#pr = numpy.average(miroc_nc.variables['pr'][offset3:offset4,:,:] ,0) #pr2 = numpy.average(pr_nc2.variables['pr'][0,offset3:offset4,:,:] ,0) pr = numpy.average(bc3_nc.variables['Prcp'][0,offset1:offset2,:,:] ,0) m = MapPlot(sector='conus', caption='', nologo=True, title=r'1981-1999 MIROC3.2 Bias Correction 3 (bc3)', subtitle='Yearly Average Precipitation') # urcrnrlon=-78,urcrnrlat=49, projection='merc', # lat_0=45.,lon_0=-92.,lat_ts=42., # resolution='i', fix_aspect=False) x, y = numpy.meshgrid(lons, lats) #colormap = LevelColormap([1,6,12,18,24,30,36,42,48,54,60,66,72,78,84], plt.cm.gist_ncar) levels = [1,6,12,18,24,30,36,42,48,54,60,66,72,78,84] #levels = numpy.arange(-14,14,2) m.pcolormesh(x, y, pr * 365.0 / 25.4, levels, latlon=True, units='inches') #m.drawstates() #m.drawcoastlines() #ax.set_title(r"1981-1999 MIROC 3.2 downscaled BCCA [inch/yr]") #fig.colorbar(cs) #fig.savefig('test.png') m.postprocess(filename='test.png')
def doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ # Start at 1 AM now = ts.replace(hour=1, minute=0) ets = now + datetime.timedelta(hours=24) interval = datetime.timedelta(hours=1) currenttime = datetime.datetime.utcnow() currenttime = currenttime.replace(tzinfo=pytz.timezone("UTC")) total = None lastts = None while now < ets: gmt = now.astimezone(pytz.timezone("UTC")) if gmt > currenttime: break for prefix in ['GaugeCorr', 'RadarOnly']: gribfn = gmt.strftime( ("/mnt/a4/data/%Y/%m/%d/mrms/ncep/" + prefix + "_QPE_01H/" + prefix + "_QPE_01H_00.00_%Y%m%d-%H%M00" ".grib2.gz")) if os.path.isfile(gribfn): break if not os.path.isfile(gribfn): print("q3_today_total.py MISSING %s" % (gribfn, )) 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) lastts = now now += interval if lastts is None: print(('No MRMS Q3 Data found for date: %s') % (now.strftime("%d %B %Y"), )) return lastts = lastts - datetime.timedelta(minutes=1) subtitle = "Total between 12:00 AM and %s" % ( lastts.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] = 0.01 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(mrms.XAXIS, mrms.YAXIS) m.pcolormesh(x, y, np.flipud(total) / 24.5, clevs, units='inch') m.drawcounties() m.postprocess(pqstr=pqstr, view=False) m.close()
except: print fn2 now += interval continue g = temperature(gs[0]['values'], 'K') g2 = temperature(gs2[0]['values'], 'K') if now == sts: lats,lons = gs[0].latlons() largest = np.zeros(np.shape(g)) delta = g.value('F') - g2.value('F') largest = np.minimum(delta, largest) now += interval np.save('maxinterval.npy', np.array(largest)) np.save('lats.npy', lats) np.save('lons.npy', lons) #do() #sys.exit() maxinterval = np.load('maxinterval.npy') print np.max(maxinterval), np.min(maxinterval) lats = np.load('lats.npy') lons = np.load('lons.npy') m = MapPlot(sector='conus', axisbg='#EEEEEE', title='16 - 18 March 2015 :: Coldest 24 Hour Temperature Change', subtitle='based on hourly HRRR Analyses 7 AM 16 Mar thru 5 AM 18 Mar') m.pcolormesh(lons, lats, maxinterval, range(-50,1,5), units='F') m.postprocess(filename='test.png')
def plotter(fdict): """ Go """ ctx = get_autoplot_context(fdict, get_description()) ptype = ctx["ptype"] date = ctx["date"] varname = ctx["var"] csector = ctx["csector"] title = date.strftime("%-d %B %Y") mp = MapPlot( sector=("state" if len(csector) == 2 else csector), state=ctx["csector"], axisbg="white", nocaption=True, title="IEM Reanalysis of %s for %s" % (PDICT.get(varname), title), subtitle="Data derived from various NOAA datasets", ) (west, east, south, north) = mp.ax.get_extent(ccrs.PlateCarree()) i0, j0 = iemre.find_ij(west, south) i1, j1 = iemre.find_ij(east, north) jslice = slice(j0, j1) islice = slice(i0, i1) idx0 = iemre.daily_offset(date) ncfn = iemre.get_daily_ncname(date.year) if not os.path.isfile(ncfn): raise NoDataFound("No Data Found.") with ncopen(ncfn) as nc: lats = nc.variables["lat"][jslice] lons = nc.variables["lon"][islice] cmap = ctx["cmap"] if varname in ["rsds", "power_swdn"]: # Value is in W m**-2, we want MJ multi = (86400.0 / 1000000.0) if varname == "rsds" else 1 data = nc.variables[varname][idx0, jslice, islice] * multi plot_units = "MJ d-1" clevs = np.arange(0, 37, 3.0) clevs[0] = 0.01 clevstride = 1 elif varname in ["wind_speed"]: data = (masked_array( nc.variables[varname][idx0, jslice, islice], units("meter / second"), ).to(units("mile / hour")).m) plot_units = "mph" clevs = np.arange(0, 41, 2) clevs[0] = 0.01 clevstride = 2 elif varname in ["p01d", "p01d_12z", "snow_12z", "snowd_12z"]: # Value is in W m**-2, we want MJ data = (masked_array(nc.variables[varname][idx0, jslice, islice], units("mm")).to(units("inch")).m) plot_units = "inch" clevs = np.arange(0, 0.25, 0.05) clevs = np.append(clevs, np.arange(0.25, 3.0, 0.25)) clevs = np.append(clevs, np.arange(3.0, 10.0, 1)) clevs[0] = 0.01 clevstride = 1 cmap = stretch_cmap(ctx["cmap"], clevs) elif varname in [ "high_tmpk", "low_tmpk", "high_tmpk_12z", "low_tmpk_12z", "avg_dwpk", ]: # Value is in W m**-2, we want MJ data = (masked_array(nc.variables[varname][idx0, jslice, islice], units("degK")).to(units("degF")).m) plot_units = "F" clevs = np.arange(-30, 120, 5) clevstride = 2 elif varname in ["range_tmpk", "range_tmpk_12z"]: vname1 = "high_tmpk%s" % ("_12z" if varname == "range_tmpk_12z" else "", ) vname2 = "low_tmpk%s" % ("_12z" if varname == "range_tmpk_12z" else "", ) d1 = nc.variables[vname1][idx0, jslice, islice] d2 = nc.variables[vname2][idx0, jslice, islice] data = (masked_array(d1, units("degK")).to(units("degF")).m - masked_array(d2, units("degK")).to(units("degF")).m) plot_units = "F" clevs = np.arange(0, 61, 5) clevstride = 2 if np.ma.is_masked(np.max(data)): raise NoDataFound("Data Unavailable") x, y = np.meshgrid(lons, lats) if ptype == "c": # in the case of contour, use the centroids on the grids mp.contourf( x + 0.125, y + 0.125, data, clevs, clevstride=clevstride, units=plot_units, ilabel=True, labelfmt="%.0f", cmap=cmap, ) else: x, y = np.meshgrid(lons, lats) mp.pcolormesh( x, y, data, clevs, clevstride=clevstride, cmap=cmap, units=plot_units, ) return mp.fig
def run(utc, routes): ''' Generate the plot for the given UTC time ''' fn = utc.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/hrrr/%H/" "hrrr.t%Hz.refd.grib2")) grbs = pygrib.open(fn) subprocess.call("rm /tmp/hrrr_ref_???.gif", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) lats = None lons = None i = 0 for minute in range(0, 18 * 60 + 1, 15): now = utc + datetime.timedelta(minutes=minute) now = now.astimezone(pytz.timezone("America/Chicago")) grbs.seek(0) try: gs = grbs.select(level=1000, forecastTime=minute) except ValueError: continue g = gs[0] if lats is None: lats, lons = g.latlons() x1, x2, y1, y2 = compute_bounds(lons, lats) lats = lats[x1:x2, y1:y2] lons = lons[x1:x2, y1:y2] ref = g['values'][x1:x2, y1:y2] mp = MapPlot(sector='midwest', axisbg='tan', title=('%s UTC NCEP HRRR 1 km AGL Reflectivity') % (utc.strftime("%-d %b %Y %H"), ), subtitle=('valid: %s') % (now.strftime("%-d %b %Y %I:%M %p %Z"), )) mp.pcolormesh(lons, lats, ref, np.arange(0, 75, 5), units='dBZ', clip_on=False) mp.postprocess(filename='/tmp/hrrr_ref_%03i.png' % (i, )) mp.close() subprocess.call(("convert /tmp/hrrr_ref_%03i.png " "/tmp/hrrr_ref_%03i.gif") % (i, i), shell=True) i += 1 # Generate anim GIF subprocess.call(("gifsicle --loopcount=0 --delay=50 " "/tmp/hrrr_ref_???.gif > /tmp/hrrr_ref.gif"), shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) pqstr = ("plot %s %s model/hrrr/hrrr_1km_ref.gif " "model/hrrr/hrrr_1km_ref_%02i.gif gif") % ( routes, utc.strftime("%Y%m%d%H%M"), utc.hour) subprocess.call("/home/ldm/bin/pqinsert -p '%s' /tmp/hrrr_ref.gif" % (pqstr, ), shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) os.remove("/tmp/hrrr_ref.gif")
def main(): """Go Main Go""" # Run for a period of 121 days ets = datetime.datetime.now() - datetime.timedelta(days=1) sts = ets - datetime.timedelta(days=121) # Get the normal accumm cnc = ncopen(iemre.get_dailyc_ncname(), "r") lons = cnc.variables["lon"][:] lats = cnc.variables["lat"][:] index0 = iemre.daily_offset(sts) index1 = iemre.daily_offset(ets) if index1 < index0: # Uh oh, we are spanning a year clprecip = np.sum(cnc.variables["p01d"][:index1, :, :], 0) clprecip = clprecip + np.sum(cnc.variables["p01d"][index0:, :, :], 0) else: clprecip = np.sum(cnc.variables["p01d"][index0:index1, :, :], 0) cnc.close() # Get the observed precip if sts.year != ets.year: # spanner, darn onc = ncopen(iemre.get_daily_ncname(sts.year)) obprecip = np.sum(onc.variables["p01d"][index0:, :, :], 0) onc.close() onc = ncopen(iemre.get_daily_ncname(ets.year)) obprecip = obprecip + np.sum(onc.variables["p01d"][:index1, :, :], 0) onc.close() else: ncfn = iemre.get_daily_ncname(sts.year) onc = ncopen(ncfn, "r") obprecip = np.sum(onc.variables["p01d"][index0:index1, :, :], 0) onc.close() lons, lats = np.meshgrid(lons, lats) # Plot departure from normal mp = MapPlot( sector="midwest", title=("Precipitation Departure %s - %s") % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle="based on IEM Estimates", ) mp.pcolormesh(lons, lats, (obprecip - clprecip) / 25.4, np.arange(-10, 10, 1)) mp.postprocess(pqstr="plot c 000000000000 summary/4mon_diff.png bogus png") mp.close() # Plot normals mp = MapPlot( sector="midwest", title=("Normal Precipitation:: %s - %s") % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle="based on IEM Estimates", ) mp.pcolormesh(lons, lats, (clprecip) / 25.4, np.arange(0, 30, 2)) mp.postprocess( pqstr="plot c 000000000000 summary/4mon_normals.png bogus png") mp.close() # Plot Obs mp = MapPlot( sector="midwest", title=("Estimated Precipitation:: %s - %s") % (sts.strftime("%b %d %Y"), ets.strftime("%b %d %Y")), subtitle="based on IEM Estimates", ) mp.pcolormesh(lons, lats, (obprecip) / 25.4, np.arange(0, 30, 2)) mp.postprocess( pqstr="plot c 000000000000 summary/4mon_stage4obs.png bogus png") 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'] 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
X, Y = numpy.mgrid[reference.MW_WEST:reference.MW_EAST:50j, reference.MW_SOUTH:reference.MW_NORTH:50j] positions = numpy.vstack([X.ravel(), Y.ravel()]) values = numpy.vstack([lons, lats]) kernel = stats.gaussian_kde(values) Z = numpy.reshape(kernel(positions).T, X.shape) m = MapPlot( sector='midwest', title='Local Storm Reports of Hail (1+") :: Kernel Density Estimate', subtitle= '2003 - May 2013, gaussian kernel, 15min/15km duplicate rule applied') m.pcolormesh(X, Y, Z, numpy.arange(0, 0.006, .0003), cmap=plt.cm.gist_earth_r, latlon=True) #xs, ys = m.map(-93.72, 41.72) #m.ax.scatter(xs, ys, marker='o', zorder=20, s=50, color='k') #xs, ys = m.map(lons, lats) #m.ax.scatter(xs, ys, marker='+', zorder=20, s=100, color='k') #m.drawcounties() m.postprocess(filename='test.png') """ fig = plt.figure() ax = fig.add_subplot(111)
utcnow = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) for i, lon in enumerate(tqdm(lons)): for j, lat in enumerate(lats): cursor.execute(""" select eventid from sbw where ST_Covers(geom, ST_GeomFromEWKT('SRID=4326;POINT(%s %s)')) and status = 'NEW' and phenomena = 'TO' and significance = 'W' and issue < '2017-01-01' and issue > '2002-01-01' """, (lon, lat)) val = cursor.rowcount vals[j, i] = val / 15.0 # Number of years 2002-2016 print("Maximum is: %.1f" % (np.max(vals),)) m = MapPlot(sector='iowa', title='Avg Number of Storm Based Tornado Warnings per Year', subtitle=("(2003 through 2016) based on unofficial " "archives maintained by the IEM, %sx%s analysis grid" ) % (dx, dx)) cmap = plt.get_cmap('jet') cmap.set_under('white') cmap.set_over('black') lons, lats = np.meshgrid(lons, lats) rng = np.arange(0, 2.1, 0.2) rng[0] = 0.01 m.pcolormesh(lons, lats, vals, rng, cmap=cmap, units='count') m.drawcounties() m.postprocess(filename='count.png') m.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 plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot ctx = get_autoplot_context(fdict, get_description()) ptype = ctx['ptype'] date = ctx['date'] varname = ctx['var'] idx0 = iemre.daily_offset(date) nc = netCDF4.Dataset( ("/mesonet/data/iemre/%s_mw_daily.nc") % (date.year, ), 'r') lats = nc.variables['lat'][:] lons = nc.variables['lon'][:] if varname == 'rsds': # Value is in W m**-2, we want MJ data = nc.variables[varname][idx0, :, :] * 86400. / 1000000. units = 'MJ d-1' clevs = np.arange(0, 37, 3.) clevs[0] = 0.01 clevstride = 1 elif varname in [ 'wind_speed', ]: data = speed(nc.variables[varname][idx0, :, :], 'MPS').value('MPH') units = 'mph' clevs = np.arange(0, 41, 2) clevs[0] = 0.01 clevstride = 2 elif varname in ['p01d', 'p01d_12z']: # Value is in W m**-2, we want MJ data = nc.variables[varname][idx0, :, :] / 25.4 units = 'inch' 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] = 0.01 clevstride = 1 elif varname in [ 'high_tmpk', 'low_tmpk', 'high_tmpk_12z', 'low_tmpk_12z', 'avg_dwpk' ]: # Value is in W m**-2, we want MJ data = temperature(nc.variables[varname][idx0, :, :], 'K').value('F') units = 'F' clevs = np.arange(-30, 120, 5) clevstride = 2 nc.close() title = date.strftime("%-d %B %Y") mp = MapPlot(sector='midwest', axisbg='white', nocaption=True, title='IEM Reanalysis of %s for %s' % (PDICT.get(varname), title), subtitle='Data derived from various NOAA datasets') if np.ma.is_masked(np.max(data)): return 'Data Unavailable' x, y = np.meshgrid(lons, lats) if ptype == 'c': # in the case of contour, use the centroids on the grids mp.contourf(x + 0.125, y + 0.125, data, clevs, clevstride=clevstride, units=units, ilabel=True, labelfmt='%.0f') else: x, y = np.meshgrid(lons, lats) mp.pcolormesh(x, y, data, clevs, clevstride=clevstride, units=units) return mp.fig
def run(valid, routes): ''' Generate the plot for the given UTC time ''' fn = valid.strftime(("/mesonet/ARCHIVE/data/%Y/%m/%d/model/hrrr/%H/" "hrrr.t%Hz.refd.grib2")) if not os.path.isfile(fn): print("hrrr/plot_ref missing %s" % (fn, )) return grbs = pygrib.open(fn) lats = None lons = None i = 0 for minute in range(0, HOURS[valid.hour] * 60 + 1, 15): if minute > (18 * 60) and minute % 60 != 0: continue now = valid + datetime.timedelta(minutes=minute) now = now.astimezone(pytz.timezone("America/Chicago")) grbs.seek(0) try: gs = grbs.select(level=1000, forecastTime=(minute if minute <= (18 * 60) else int(minute / 60))) except ValueError: continue if lats is None: lats, lons = gs[0].latlons() x1, x2, y1, y2 = compute_bounds(lons, lats) lats = lats[x1:x2, y1:y2] lons = lons[x1:x2, y1:y2] # HACK.............. if len(gs) > 1 and minute > (18 * 60): reflect = gs[-1]['values'][x1:x2, y1:y2] else: reflect = gs[0]['values'][x1:x2, y1:y2] mp = MapPlot(sector='midwest', axisbg='tan', title=('%s UTC NCEP HRRR 1 km AGL Reflectivity') % (valid.strftime("%-d %b %Y %H"), ), subtitle=('valid: %s') % (now.strftime("%-d %b %Y %I:%M %p %Z"), )) mp.pcolormesh(lons, lats, reflect, np.arange(0, 75, 5), units='dBZ', clip_on=False) pngfn = '/tmp/hrrr_ref_%s_%03i.png' % (valid.strftime("%Y%m%d%H"), i) mp.postprocess(filename=pngfn) mp.close() subprocess.call(("convert %s " "%s.gif") % (pngfn, pngfn[:-4]), shell=True) i += 1 # Generate anim GIF subprocess.call(("gifsicle --loopcount=0 --delay=50 " "/tmp/hrrr_ref_%s_???.gif > /tmp/hrrr_ref_%s.gif") % (valid.strftime("%Y%m%d%H"), valid.strftime("%Y%m%d%H")), shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) pqstr = ("plot %s %s model/hrrr/hrrr_1km_ref.gif " "model/hrrr/hrrr_1km_ref_%02i.gif gif") % ( routes, valid.strftime("%Y%m%d%H%M"), valid.hour) subprocess.call(("/home/ldm/bin/pqinsert -p '%s' /tmp/hrrr_ref_%s.gif") % (pqstr, valid.strftime("%Y%m%d%H")), shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) subprocess.call("rm -f /tmp/hrrr_ref_%s*" % (valid.strftime("%Y%m%d%H"), ), shell=True)