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 """ ctx = get_autoplot_context(fdict, get_description()) if ctx["p"] == "day": get_daily_data(ctx) else: get_monthly_data(ctx) ctx["lastyear"] = datetime.date.today().year ctx["years"] = ctx["lastyear"] - 1893 + 1 csector = ctx["csector"] subtitle = ("Based on IEM Estimates, " "1 is %s out of %s total years (1893-%s)") % ( "wettest" if ctx["var"] == "precip" else "hottest", ctx["years"], ctx["lastyear"], ) if ctx["var"] == "arridity": subtitle = "Std Average High Temp Departure minus Std Precip Departure" mp = MapPlot( sector=("state" if len(csector) == 2 else csector), state=ctx["csector"], continentalcolor="white", title="%s %s %sby Climate District" % ( ctx["label"], PDICT[ctx["var"]], "Ranks " if ctx["var"] != "arridity" else "", ), subtitle=subtitle, titlefontsize=14, ) cmap = get_cmap(ctx["cmap"]) bins = [ 1, 5, 10, 25, 50, 75, 100, ctx["years"] - 10, ctx["years"] - 5, ctx["years"], ] pvar = ctx["var"] + "_rank" fmt = "%.0f" if ctx["var"] == "arridity": bins = np.arange(-4, 4.1, 1) pvar = ctx["var"] fmt = "%.1f" mp.fill_climdiv( ctx["df"][pvar], ilabel=True, plotmissing=False, lblformat=fmt, bins=bins, cmap=cmap, ) return mp.fig, ctx["df"]
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot utc = datetime.datetime.utcnow() bins = [0, 1, 14, 31, 91, 182, 273, 365, 730, 1460, 2920, 3800] pgconn = psycopg2.connect(database='postgis', host='iemdb', user='******') cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) phenomena = fdict.get('phenomena', 'TO') significance = fdict.get('significance', 'W') cursor.execute(""" select wfo, extract(days from ('TODAY'::date - max(issue))) as m from warnings where significance = %s and phenomena = %s GROUP by wfo ORDER by m ASC """, (significance, phenomena)) data = {} rows = [] for row in cursor: wfo = row[0] if row[0] != 'JSJ' else 'SJU' rows.append(dict(wfo=wfo, days=row[1])) data[wfo] = max([row[1], 0]) df = pd.DataFrame(rows) m = MapPlot(sector='nws', axisbg='white', nocaption=True, title='Days since Last %s %s by NWS Office' % ( vtec._phenDict.get(phenomena, phenomena), vtec._sigDict.get(significance, significance)), subtitle='Valid %s' % (utc.strftime("%d %b %Y %H%M UTC"),)) m.fill_cwas(data, bins=bins, ilabel=True, units='Days', lblformat='%.0f') return m.fig, df
def test_contourf(): ''' Test the contourf plot with labels specified ''' mp = MapPlot(sector='iowa', nocaption=True) mp.contourf(np.arange(-94, -89), np.arange(40, 45), np.arange(5), np.arange(5), clevlabels=['a', 'b', 'c', 'd', 'e']) return mp.fig
def test_fillstates(): """Can we fill states""" data = {'AK': 10, 'HI': 30, 'IA': 40, 'NY': 80} mp = MapPlot(sector='nws', title='Fill AK, HI, IA, NY States', subtitle='test_fillstates', nocaption=True) mp.fill_states(data, ilabel=True) return mp.fig
def draw_map(): """make maps, not war.""" m = MapPlot(sector='conus', title='4 March 2019 :: DEP Precip Points') update_grid(m.ax) m.postprocess(filename='/tmp/map_clipoints.png') m.close()
def main(): """Go Main""" pgconn = get_dbconn('postgis') df = read_postgis(""" select geom, issue from sbw where wfo = 'PUB' and phenomena = 'TO' and significance = 'W' and status = 'NEW' and issue > '2007-10-01' and issue < '2019-01-01' """, pgconn, geom_col='geom', crs={'init': 'epsg:4326', 'no_defs': True}) bounds = df['geom'].total_bounds # bounds = [-102.90293903, 40.08745967, -97.75622311, 43.35172981] bbuf = 0.25 mp = MapPlot( sector='custom', west=bounds[0] - bbuf, south=bounds[1] - bbuf, east=bounds[2] + bbuf, north=bounds[3] + bbuf, continentalcolor='white', # '#b3242c', title='NWS Pueblo Issued Tornado Warnings [2008-2018]', subtitle='%s warnings plotted' % (len(df.index), )) crs_new = ccrs.Mercator() crs = ccrs.PlateCarree() new_geometries = [crs_new.project_geometry(ii, src_crs=crs) for ii in df['geom'].values] # mp.draw_cwas() mp.ax.add_geometries(new_geometries, crs=crs_new, lw=0.5, edgecolor='red', facecolor='None', alpha=1, zorder=5) mp.drawcounties() mp.postprocess(filename='test.png')
def main(): """Go Main Go""" now = datetime.datetime.now() pgconn = get_dbconn("iem", user="******") icursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) # Compute normal from the climate database sql = """ SELECT s.id, tmpf, dwpf, sknt, drct, ST_x(s.geom) as lon, ST_y(s.geom) as lat FROM current c, stations s WHERE s.network IN ('IA_RWIS') and c.iemid = s.iemid and valid + '20 minutes'::interval > now() and tmpf > -50 and dwpf > -50 """ data = [] icursor.execute(sql) for row in icursor: data.append(row) mp = MapPlot( axisbg="white", title="Iowa DOT RWIS Mesoplot", subtitle="plot valid %s" % (now.strftime("%-d %b %Y %H:%M %P"),), ) mp.plot_station(data) mp.drawcounties(color="#EEEEEE") pqstr = "plot c 000000000000 iowa_rwis.png bogus png" mp.postprocess(pqstr=pqstr) mp.close()
def test_michigan(): """See what we do with Michigan""" mp = MapPlot(sector='state', state='MI', nocaption=True) mp.contourf(np.arange(-84, -75), np.arange(36, 45), np.arange(9), np.arange(9), clevlabels=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']) return mp.fig
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 main(): """Go Main""" pgconn = get_dbconn('postgis') df = read_postgis(""" select geom, issue from bot_warnings where wfo = 'PUB' """, pgconn, geom_col='geom', crs={'init': 'epsg:4326', 'no_defs': True}) bounds = df['geom'].total_bounds # bounds = [-102.90293903, 40.08745967, -97.75622311, 43.35172981] bbuf = 0.25 mp = MapPlot(sector='custom', west=bounds[0] - bbuf, south=bounds[1] - bbuf, east=bounds[2] + bbuf, north=bounds[3] + bbuf, continentalcolor='white', title='Bot Issued Tornado Warnings [2008-2018] for PUB', subtitle='%s warnings plotted' % (len(df.index), )) crs_new = ccrs.Mercator() crs = ccrs.PlateCarree() new_geometries = [crs_new.project_geometry(ii, src_crs=crs) for ii in df['geom'].values] mp.draw_cwas() mp.ax.add_geometries(new_geometries, crs=crs_new, edgecolor='r', facecolor='None', alpha=1., lw=0.5, zorder=10) mp.postprocess(filename='test.png')
def main(): """Go Main""" df = get_database_data() print(df) vals = {} labels = {} for wfo, row in df.iterrows(): if wfo == 'JSJ': wfo = 'SJU' vals[wfo] = row['percent'] labels[wfo] = '%.0f%%' % (row['percent'], ) #if row['count'] == 0: # labels[wfo] = '-' bins = np.arange(0, 101, 10) #bins = [1, 25, 50, 75, 100, 125, 150, 200, 300] #bins = [-50, -25, -10, -5, 0, 5, 10, 25, 50] # bins[0] = 1 #clevlabels = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N'] cmap = plt.get_cmap('PuOr') mp = MapPlot(sector='nws', continentalcolor='white', figsize=(12., 9.), title=("2018 Percentage of Time with 1+ Flood Warning Active"), subtitle=('1 January - 30 September 2018, based on IEM archives')) mp.fill_cwas(vals, bins=bins, lblformat='%s', labels=labels, cmap=cmap, ilabel=True, # clevlabels=clevlabels, units='percent') 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 main(): """Go MAin""" df = pd.read_csv('flood_emergencies.csv') df2 = df[['source', 'eventid', 'phenomena', 'significance', 'year'] ].drop_duplicates() gdf = df2.groupby('source').count() vals = {} labels = {} for wfo, row in gdf.iterrows(): if wfo == 'TJSJ': wfo = 'SJU' else: wfo = wfo[1:] vals[wfo] = int(row['eventid']) labels[wfo] = "%s" % (row['eventid'], ) bins = list(range(0, 31, 3)) bins[0] = 1. cmap = plt.get_cmap('plasma_r') cmap.set_over('black') cmap.set_under('white') mp = MapPlot(sector='nws', continentalcolor='white', figsize=(12., 9.), title=("2003-2018 Flash Flood Emergency Events"), subtitle=('based on unofficial IEM archives, searching ' '"FFS", "FLW", "FFS".')) mp.fill_cwas(vals, bins=bins, lblformat='%s', labels=labels, cmap=cmap, ilabel=True, # clevlabels=month_abbr[1:], units='count') mp.postprocess(filename='test.png')
def test_drawugcs2(): """3 filled zones""" mp = MapPlot(sector='iowa', title='Zones, 3 filled in Iowa, label', subtitle='test_drawugcs2', nocaption=True) mp.fill_ugcs({"IAZ001": 10, "IAZ003": 20, "IAZ005": 30}, ilabel=True) return mp.fig
def runYear(year): # Grab the data sql = """SELECT station, sum(precip) as total, max(day) from alldata_ia WHERE year = %s and station != 'IA0000' and substr(station,3,1) != 'C' and precip is not null GROUP by station""" % (year,) lats = [] lons = [] vals = [] labels = [] ccursor.execute( sql ) for row in ccursor: sid = row['station'] if not nt.sts.has_key(sid): continue labels.append( sid[2:] ) lats.append( nt.sts[sid]['lat'] ) lons.append( nt.sts[sid]['lon'] ) vals.append( row['total'] ) maxday = row['max'] m = MapPlot(title="Total Precipitation [inch] (%s)" % (year,), subtitle='1 January - %s' % (maxday.strftime("%d %B"),), axisbg='white') m.plot_values(lons, lats, vals, labels=labels, fmt='%.2f', labeltextsize=8, labelcolor='tan') pqstr = "plot m %s bogus %s/summary/total_precip.png png" % ( now.strftime("%Y%m%d%H%M"), year,) m.postprocess(pqstr=pqstr)
def test_filter_functions(): """Make sure our filter functions are doing what we want!""" mp = MapPlot(sector='iowa') assert plot.state_filter(mp, b'IAC001', dict()) assert not plot.state_filter(mp, b'MNC001', dict()) mp = MapPlot(cwa='DMX') assert plot.state_filter(mp, b'IAC001', dict())
def main(): """Map some CLI data""" pgconn = get_dbconn('iem') df = read_sql(""" WITH data as ( SELECT station, snow_jul1 - snow_jul1_normal as s from cli_data where valid = '2019-02-18' and snow_jul1 > 0 and snow_jul1_normal > 0) select station, st_x(geom) as lon, st_y(geom) as lat, c.s as val from data c JOIN stations s on (s.id = c.station) WHERE s.network = 'NWSCLI' """, pgconn, index_col=None) df['color'] = '#ff0000' df.loc[df['val'] > 0, 'color'] = '#0000ff' mp = MapPlot(sector='midwest', axisbg='white', title=("2018-2019 Snowfall Total Departure " "from Average [inches]"), subtitle='18 Feb 2019 Based on NWS CLI Reporting Sites') mp.plot_values( df['lon'].values, df['lat'].values, df['val'].values, fmt='%.1f', textsize=12, color=df['color'].values, labelbuffer=1) mp.postprocess(filename='test.png')
def main(argv): """Go Main Go""" st = NetworkTable("IACLIMATE") now = datetime.datetime.now() - datetime.timedelta(days=1) pgconn = get_dbconn("coop", user="******") ccursor = pgconn.cursor() gfunc = "gdd50" gbase = 50 if len(argv) == 2 and argv[1] == "gdd52": gfunc = "gdd52" gbase = 52 if len(argv) == 2 and argv[1] == "gdd48": gfunc = "gdd48" gbase = 48 # Compute normal from the climate database ccursor.execute( """ SELECT station, sum(%s(high, low)) as gdd from alldata_ia WHERE station != 'IA0000' and substr(station, 3, 1) != 'C' and year = %s GROUP by station """ % (gfunc, now.year) ) lats = [] lons = [] gdd50 = [] valmask = [] for row in ccursor: station = row[0] if station not in st.sts: continue lats.append(st.sts[station]["lat"]) lons.append(st.sts[station]["lon"]) gdd50.append(float(row[1])) valmask.append(True) mp = MapPlot( axisbg="white", title=("Iowa %s GDD (base=%s) Accumulation") % (now.strftime("%Y"), gbase), subtitle="1 Jan - %s" % (now.strftime("%d %b %Y"),), ) minval = min(gdd50) rng = max([int(max(gdd50) - minval), 10]) ramp = np.linspace(minval, minval + rng, 10, dtype=np.int) mp.contourf(lons, lats, gdd50, ramp) pqstr = "plot c 000000000000 summary/gdd_jan1.png bogus png" if gbase == 52: pqstr = "plot c 000000000000 summary/gdd52_jan1.png bogus png" elif gbase == 48: pqstr = "plot c 000000000000 summary/gdd48_jan1.png bogus png" mp.drawcounties() mp.postprocess(view=False, pqstr=pqstr) mp.close()
def test_drawiowawfo(): """Iowa Contour Plot""" mp = MapPlot(sector='iowawfo', title='Iowa Contour plot', nocaption=True) mp.contourf(np.arange(-94, -85), np.arange(36, 45), np.arange(9), np.arange(9), clevlabels=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']) return mp.fig
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 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 main(): """Go Main Go""" cursor = POSTGIS.cursor() cursor2 = POSTGIS.cursor() phenomena = 'WS' cursor.execute(""" SELECT ugc, issue, init_expire, wfo from warnings where phenomena = %s and significance = 'A' and issue > '2005-10-01' ORDER by issue ASC """, (phenomena, )) total = cursor.rowcount print('Events is %s' % (total, )) hits = {} hits2 = {} totals = {} misses = 0 for row in tqdm(cursor, total=total): wfo = row[3] if wfo not in hits: hits[wfo] = {} if wfo not in totals: totals[wfo] = 0 totals[wfo] += 1 cursor2.execute(""" SELECT distinct phenomena, significance from warnings where ugc = %s and expire > %s and issue < %s and wfo = %s """, (row[0], row[1], row[2], wfo)) for row2 in cursor2: key = "%s.%s" % (row2[0], row2[1]) if key not in hits[wfo]: hits[wfo][key] = 0 hits[wfo][key] += 1 if key not in hits2: hits2[key] = 0 hits2[key] += 1 if cursor2.rowcount == 0: misses += 1 data = {} for wfo in hits: data[wfo] = hits[wfo].get( '%s.W' % (phenomena,), 0) / float(totals[wfo]) * 100.0 mp = MapPlot(sector='nws', axisbg='white', title=("Conversion [%] of Winter Storm Watch " "Counties/Parishes into Winter Storm Warnings"), titlefontsize=14, subtitle=('1 Oct 2005 - 29 Mar 2018, Overall %s/%s %.1f%%' ) % (hits2['%s.W' % (phenomena, )], total, hits2['%s.W' % (phenomena, )] / float(total) * 100.)) mp.fill_cwas(data, ilabel=True, lblformat='%.0f') mp.postprocess(filename='test.png') print('Misses %s %.1f%%' % (misses, misses / float(total) * 100.0)) for key in hits2: print('%s %s %.1f%%' % (key, hits2[key], hits2[key] / float(total) * 100.0))
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 test_scatter(): """ Test scatter plots """ mp = MapPlot(sector='midwest', title='Should see 100 dots', subtitle='test_scatter', nocaption=True) mp.scatter(np.linspace(-99, -94, 100), np.linspace(40, 45, 100), np.arange(100), np.arange(0, 101, 10)) return mp.fig
def test_drawcities(): """Draw Cities""" mp = MapPlot(title='Fill and Draw Cities', subtitle="This is my subtitle", continentalcolor='blue', sector='iowa', nocaption=True) mp.drawcities() return mp.fig
def test_drawrandomtext(): """See if we can handle the fun that is drawing random text""" mp = MapPlot(sector='iowa', title='Fun Text, here and there', continentalcolor='white', debug=True, nocaption=True) mp.plot_values([-94, -92, -91, -92], [42, 41, 43, 42.4], ['One', 'Two\nTwo', 'Three\nThree\nThree', 'Four\nFour\nFour\nFour'], showmarker=True) return mp.fig
def test_cwa(): """Exercise the cwa plotting routines""" mp = MapPlot(sector='cwa', cwa='MKX', nocaption=True) mp.contourf(np.arange(-94, -89), np.arange(40, 45), np.arange(5), np.arange(5), clevlabels=['a', 'b', 'c', 'd', 'e']) mp.draw_cwas() assert mp.cwa == 'MKX' return mp.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"].filled(0) lats, lons = g.latlons() else: total += grbs[1]["values"].filled(0) 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, distance(total, 'MM').value('IN'), 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 test_overlap(): """ Do some checking of our overlaps logic """ mp = MapPlot(sector='midwest', continentalcolor='white', nocaption=True) lons = np.linspace(-99, -90, 100) lats = np.linspace(38, 44, 100) vals = lats labels = ['%.2f' % (s,) for s in lats] mp.plot_values(lons, lats, vals, fmt='%.2f', labels=labels) return mp.fig
def test_stationplot(): """Testing the plotting of wind barbs""" mp = MapPlot(continentalcolor='white', nocaption=True) data = [ dict(lat=41.5, lon=-96, tmpf=50, dwpf=30, id='BOOI4'), dict(lat=42.0, lon=-95.5, tmpf=50, dwpf=30, id='CSAI4'), ] mp.plot_station(data, fontsize=12) return mp.fig
def test_colorbar(): """Run tests against the colorbar algorithm""" mp = MapPlot(sector='iowa', nocaption=True) cmap = plot.maue() cmap.set_under('white') clevs = list(range(0, 101, 10)) norm = mpcolors.BoundaryNorm(clevs, cmap.N) mp.drawcities() mp.draw_colorbar(clevs, cmap, norm) return mp.fig
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(): """ 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 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 test_colorbar3(): """draw another colorbar""" mp = MapPlot(sector='iowa', nocaption=True) cmap = plot.maue() cmap.set_over('black') clevs = [0, 100, 250, 500, 1000, 2000, 20000] norm = mpcolors.BoundaryNorm(clevs, cmap.N) mp.draw_colorbar( clevs, cmap, norm, title="Erosion $kg/m^2$", spacing='uniform' ) return mp.fig
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot import matplotlib.cm as cm pgconn = psycopg2.connect(database='coop', host='iemdb', user='******') date1 = datetime.datetime.strptime(fdict.get('date1', '2014-09-01'), '%Y-%m-%d') date2 = datetime.datetime.strptime(fdict.get('date2', '2014-09-22'), '%Y-%m-%d') date1 = date1.replace(year=2000) date2 = date2.replace(year=2000) varname = fdict.get('varname', 'low') df = read_sql(""" WITH t2 as ( SELECT station, high, low from ncdc_climate81 WHERE valid = %s ), t1 as ( SELECT station, high, low from ncdc_climate81 where valid = %s ), data as ( SELECT t2.station, t1.high as t1_high, t2.high as t2_high, t1.low as t1_low, t2.low as t2_low from t1 JOIN t2 on (t1.station = t2.station) ) SELECT d.station, ST_x(geom) as lon, ST_y(geom) as lat, t2_high - t1_high as high, t2_low - t1_low as low from data d JOIN stations s on (s.id = d.station) where s.network = 'NCDC81' and s.state not in ('HI', 'AK') """, pgconn, params=(date2, date1), index_col='station') days = int((date2 - date1).days) extent = int(df[varname].abs().max()) m = MapPlot(sector='conus', title=('%s Day Change in %s NCDC 81 Climatology') % (days, PDICT[varname]), subtitle='from %s to %s' % (date1.strftime("%-d %B"), date2.strftime("%-d %B"))) cmap = cm.get_cmap("RdBu_r") m.contourf(df['lon'].values, df['lat'].values, df[varname].values, np.arange(0 - extent, extent + 1, 2), cmap=cmap, units='F') return m.fig, df
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 doday(ts, realtime): """ Create a plot of precipitation stage4 estimates for some day """ nt = NetworkTable(["AWOS", "IA_ASOS"]) pgconn = get_dbconn("iem", user="******") df = read_sql( """ SELECT id as station, min(feel) as wcht from current_log c JOIN stations t on (c.iemid = t.iemid) WHERE t.network in ('IA_ASOS', 'AWOS') and valid >= %s and valid < %s + '24 hours'::interval and feel is not null and sknt > 0 GROUP by id """, pgconn, params=(ts, ts), index_col="station", ) routes = "ac" if not realtime: routes = "a" lons = [] lats = [] vals = [] labels = [] for station, row in df.iterrows(): lons.append(nt.sts[station]["lon"]) lats.append(nt.sts[station]["lat"]) vals.append(row["wcht"]) labels.append(station) pqstr = ("plot %s %s00 summary/iowa_min_windchill.png " "summary/iowa_min_windchill.png png") % (routes, ts.strftime("%Y%m%d%H")) mp = MapPlot( title=(r"%s Minimum Wind Chill Temperature $^\circ$F") % (ts.strftime("%-d %b %Y"), ), subtitle="Calm conditions are excluded from analysis", continentalcolor="white", ) mp.plot_values( lons, lats, vals, "%.1f", labels=labels, textsize=12, labelbuffer=5, labeltextsize=10, ) mp.drawcounties() mp.postprocess(pqstr=pqstr, view=False) mp.close()
def test_colorbar2(): """draw a colorbar""" mp = MapPlot(sector='iowa', nocaption=True) cmap = plot.maue() clevs = list(range(0, 101, 10)) clevlabels = ["One", "Three", "Blahh", "Longest", "Five", "Six", "Ten", "Fourty", 100000, "Hi\nHo", 100] norm = mpcolors.BoundaryNorm(clevs, cmap.N) mp.draw_colorbar( clevs, cmap, norm, clevlabels=clevlabels ) return mp.fig
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 test_hexbin(): """See if we can do hexbin OKish""" mp = MapPlot(sector='north_america', 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.hexbin(lons.flatten(), lats.flatten(), vals.flatten(), np.arange(0, 1, 0.1), cmap='jet') return mp.fig
def plot_precip_month(valid): """ Go Main Go Args: valid (datetime): The timestamp we are interested in! """ pgconn = get_dbconn("iem", user="******") cursor = pgconn.cursor() d1 = valid.replace(day=1) d2 = d1 + datetime.timedelta(days=35) d2 = d2.replace(day=1) cursor.execute( """SELECT sum(pday), id, st_x(geom), st_y(geom) from summary s JOIN stations t on (t.iemid = s.iemid) WHERE s.day >= %s and s.day < %s and t.network in ('IA_COOP', 'NE_COOP', 'MO_COOP', 'IL_COOP', 'WI_COOP', 'MN_COOP') and pday is not null and pday >= 0 and extract(hour from coop_valid) between 5 and 10 GROUP by id, st_x, st_y""", (d1.date(), d2.date()), ) labels = [] vals = [] lats = [] lons = [] for row in cursor: labels.append(row[1]) vals.append(pretty(row[0])) lats.append(row[3]) lons.append(row[2]) mp = MapPlot( title="%s NWS COOP Month Precipitation Totals [inch]" % (valid.strftime("%-d %b %Y"),), subtitle="Reports valid between 6 and 9 AM", axisbg="white", figsize=(10.24, 7.68), ) mp.plot_values(lons, lats, vals, fmt="%s", labels=labels, labelcolor="tan") mp.drawcounties() pqstr = "plot ac %s0000 coopMonthPlot.gif coopMonthPlot.gif gif" % ( valid.strftime("%Y%m%d"), ) mp.postprocess(pqstr=pqstr) mp.close() pgconn.close()
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') from pyiem.plot import MapPlot bins = [0, 1, 14, 31, 91, 182, 273, 365, 730, 1460, 2920, 3800] pgconn = get_dbconn('postgis') cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) ctx = get_autoplot_context(fdict, get_description()) phenomena = ctx['phenomena'] significance = ctx['significance'] edate = ctx.get('edate') if edate is not None: edate = utc(edate.year, edate.month, edate.day, 0, 0) cursor.execute( """ select wfo, extract(days from (%s::date - max(issue))) as m from warnings where significance = %s and phenomena = %s and issue < %s GROUP by wfo ORDER by m ASC """, (edate, significance, phenomena, edate)) else: cursor.execute( """ select wfo, extract(days from ('TODAY'::date - max(issue))) as m from warnings where significance = %s and phenomena = %s GROUP by wfo ORDER by m ASC """, (significance, phenomena)) edate = datetime.datetime.utcnow() if cursor.rowcount == 0: raise ValueError( ("No Events Found for %s (%s.%s)") % (vtec.get_ps_string( phenomena, significance), phenomena, significance)) data = {} rows = [] for row in cursor: wfo = row[0] if row[0] != 'JSJ' else 'SJU' rows.append(dict(wfo=wfo, days=row[1])) data[wfo] = max([row[1], 0]) df = pd.DataFrame(rows) df.set_index('wfo', inplace=True) mp = MapPlot(sector='nws', axisbg='white', nocaption=True, title='Days since Last %s by NWS Office' % (vtec.get_ps_string(phenomena, significance), ), subtitle='Valid %s' % (edate.strftime("%d %b %Y %H%M UTC"), )) mp.fill_cwas(data, bins=bins, ilabel=True, units='Days', lblformat='%.0f') return mp.fig, df
def main(argv): """Do Great Things""" pgconn = get_dbconn("idep") cursor = pgconn.cursor() mp = MapPlot( continentalcolor="#EEEEEE", nologo=True, subtitle="3 HUC12s choosen per MLRA", nocaption=True, title=("Tillage Timing Experiment 30 Random HUC12s"), ) df = read_postgis( """ SELECT geom, mlra_id from mlra WHERE mlra_id in ( select distinct mlra_id from huc12 where huc_12 in %s and scenario = 0) """, pgconn, params=(tuple(MYHUCS), ), geom_col="geom", index_col="mlra_id", ) for _i, row in df.iterrows(): for poly in row["geom"]: arr = np.asarray(poly.exterior) points = mp.ax.projection.transform_points(ccrs.Geodetic(), arr[:, 0], arr[:, 1]) p = Polygon(points[:, :2], fc="None", ec="k", zorder=2, lw=0.2) mp.ax.add_patch(p) df = read_postgis( """ select huc_12, ST_transform(geom, 4326) as geom from huc12 where huc_12 in %s and scenario = 0 """, pgconn, params=(tuple(MYHUCS), ), geom_col="geom", index_col="huc_12", ) for _i, row in df.iterrows(): for poly in row["geom"]: arr = np.asarray(poly.exterior) points = mp.ax.projection.transform_points(ccrs.Geodetic(), arr[:, 0], arr[:, 1]) p = Polygon(points[:, :2], fc="r", ec="k", zorder=3, lw=0.5) mp.ax.add_patch(p) mp.postprocess(filename="test.png")
def main(): """Go Main Go""" pgconn = get_dbconn('iem', user='******') icursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) lats = [] lons = [] vals = [] valmask = [] labels = [] icursor.execute(""" select id, ST_x(s.geom) as lon, ST_y(s.geom) as lat, pday from summary c, stations s WHERE day = 'TODAY' and pday >= 0 and pday < 20 and s.network = 'IA_COOP' and s.iemid = c.iemid """) for row in icursor: lats.append(row['lat']) lons.append(row['lon']) vals.append(n(row['pday'])) labels.append(row['id']) valmask.append(True) mp = MapPlot(title="Iowa COOP 24 Hour Precipitation", axisbg='white', subtitle="ending approximately %s 7 AM" % (datetime.datetime.now().strftime("%-d %b %Y"), )) mp.plot_values(lons, lats, vals) pqstr = "plot ac %s iowa_coop_precip.png iowa_coop_precip.png png" % ( datetime.datetime.now().strftime("%Y%m%d%H%M"), ) mp.postprocess(pqstr=pqstr) 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 = 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 main(): """Go Main Go""" now = datetime.datetime.now() pgconn = get_dbconn("iem", user="******") icursor = pgconn.cursor() # Compute normal from the climate database sql = """ select s.id, s.network, ST_x(s.geom) as lon, ST_y(s.geom) as lat, (case when c.pday < 0 or c.day is null then 0 else c.pday end) as rainfall from summary_%s c, current c2, stations s WHERE s.iemid = c2.iemid and c2.iemid = c.iemid and c2.valid > (now() - '2 hours'::interval) and c.day = 'TODAY' and s.country = 'US' and (s.network ~* 'ASOS' or s.network = 'AWOS') and s.state in ('IA','MN','WI','IL','MO','NE','KS','SD','ND') """ % ( now.year, ) lats = [] lons = [] vals = [] iavals = [] valmask = [] icursor.execute(sql) for row in icursor: lats.append(row[3]) lons.append(row[2]) vals.append(t(row[4])) iowa = row[1] in ["AWOS", "IA_ASOS"] valmask.append(iowa) if iowa: iavals.append(row[4]) if len(lats) < 3: return mp = MapPlot( title="Iowa ASOS/AWOS Rainfall Reports", axisbg="white", subtitle="%s" % (now.strftime("%d %b %Y"),), ) mp.drawcounties() mp.plot_values(lons, lats, vals) pqstr = "plot c 000000000000 summary/today_prec.png bogus png" mp.postprocess(view=False, pqstr=pqstr) mp.close()