def main(): """Go Main""" total = None years = 0. for yr in range(1981, 2018): print(yr) ncfn = "/mesonet/data/prism/%s_daily.nc" % (yr, ) nc = netCDF4.Dataset(ncfn) if total is None: lons = nc.variables['lon'][:] lats = nc.variables['lat'][:] total = np.zeros(nc.variables['tmax'].shape[1:], np.float) days = np.zeros(nc.variables['tmax'].shape[1:], np.float) sidx = daily_offset(datetime.date(yr, 1, 1)) eidx = daily_offset(datetime.date(yr, 7, 4)) for idx in range(sidx, eidx): days += np.where(nc.variables['tmax'][idx, :, :] > THRESHOLD, 1, 0) nc.close() years += 1. total += days val = days - (total / years) print(np.max(val)) print(np.min(val)) mp = MapPlot(sector='conus', title=("OSU PRISM 2017 Days with High >= 90$^\circ$F " "Departure"), subtitle=("2017 thru 4 July against 1981-2016 " "Year to Date Average")) mp.contourf(lons, lats, val, np.arange(-25, 26, 5), units='days', cmap=plt.get_cmap('seismic')) mp.postprocess(filename='test.png')
def main(): """Go Main""" nt = NetworkTable(['AWOS', 'IA_ASOS']) pgconn = psycopg2.connect(database='mos', host='localhost', user='******', port=5555) df = read_sql(""" select station, avg(pwater) from model_gridpoint where model = 'NAM' and extract(hour from runtime at time zone 'UTC') = 0 and pwater > 0 and pwater < 100 and extract(month from runtime) between 4 and 9 and ftime = runtime GROUP by station ORDER by avg """, pgconn, index_col='station') df['lat'] = None df['lon'] = None df['pwater'] = distance(df['avg'].values, 'MM').value('IN') for station in df.index.values: df.at[station, 'lat'] = nt.sts[station[1:]]['lat'] df.at[station, 'lon'] = nt.sts[station[1:]]['lon'] mp = MapPlot(title=('00z Analysis NAM Warm-Season Average ' 'Precipitable Water [in]'), subtitle=("based on grid point samples " "from 2004-2017 (April-September)")) cmap = plt.get_cmap("plasma_r") cmap.set_under('white') cmap.set_over('black') mp.contourf(df['lon'], df['lat'], df['pwater'], np.arange(0.94, 1.13, 0.03), cmap=cmap, units='inch') mp.drawcounties() mp.drawcities() mp.postprocess(filename='170901.png') mp.close()
def plotter(fdict): """ Go """ pgconn = get_dbconn("coop") ctx = get_autoplot_context(fdict, get_description()) date1 = ctx["date1"] date2 = ctx["date2"] date1 = date1.replace(year=2000) date2 = date2.replace(year=2000) varname = ctx["varname"] 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", ) if df.empty: raise NoDataFound("No Data Found.") days = int((date2 - date1).days) extent = int(df[varname].abs().max()) mp = 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")), ) mp.contourf( df["lon"].values, df["lat"].values, df[varname].values, np.arange(0 - extent, extent + 1, 2), cmap=get_cmap(ctx["cmap"]), units="F", ) return mp.fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) sector = ctx['sector'] if len(sector) != 2: raise ValueError("Sorry, this app does not support multi-state plots.") varname = ctx['var'] year = ctx['year'] popt = ctx['popt'] threshold = ctx['threshold'] table = "alldata_%s" % (sector, ) df = read_sql(""" WITH data as ( SELECT station, """ + SQLOPT[varname] + """ as doy from """ + table + """ WHERE year = %s GROUP by station ) select station, doy, st_x(geom) as lon, st_y(geom) as lat from data d JOIN stations t on (d.station = t.id) WHERE t.network = %s and substr(station, 3, 4) != '0000' and substr(station, 3, 1) != 'C' and doy not in (0, 400) ORDER by doy """, pgconn, params=(threshold, year, '%sCLIMATE' % (sector, )), index_col='station') if df.empty: raise ValueError("No data found!") def f(val): ts = datetime.date(year, 1, 1) + datetime.timedelta(days=int(val - 1)) return ts.strftime("%-m/%-d") df['pdate'] = df['doy'].apply(f) mp = MapPlot(sector=('state' if len(sector) == 2 else sector), state=ctx['sector'], continental_color='white', nocaption=True, title=r"%s %s %s$^\circ$F" % (year, PDICT2[varname], threshold), subtitle='based on NWS COOP and IEM Daily Estimates') levs = np.linspace(df['doy'].min() - 3, df['doy'].max() + 3, 7, dtype='i') levlables = list(map(f, levs)) if popt == 'contour': mp.contourf(df['lon'], df['lat'], df['doy'], levs, clevlabels=levlables) mp.plot_values(df['lon'], df['lat'], df['pdate'], labelbuffer=5) mp.drawcounties() return mp.fig, df
def plotter(fdict): """ Go """ pgconn = get_dbconn('coop') ctx = get_autoplot_context(fdict, get_description()) date1 = ctx['date1'] date2 = ctx['date2'] date1 = date1.replace(year=2000) date2 = date2.replace(year=2000) varname = ctx['varname'] 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()) mp = 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"))) mp.contourf(df['lon'].values, df['lat'].values, df[varname].values, np.arange(0 - extent, extent + 1, 2), cmap=cm.get_cmap(ctx['cmap']), units='F') return mp.fig, df
def plotter(fdict): """ Go """ ctx = util.get_autoplot_context(fdict, get_description()) ptype = ctx["ptype"] sdate = ctx["sdate"] edate = ctx["edate"] src = ctx["src"] opt = ctx["opt"] usdm = ctx["usdm"] if sdate.year != edate.year: raise NoDataFound("Sorry, do not support multi-year plots yet!") days = (edate - sdate).days sector = ctx["sector"] x0 = 0 x1 = -1 y0 = 0 y1 = -1 state = None if len(sector) == 2: state = sector sector = "state" title = compute_title(src, sdate, edate) if src == "mrms": ncfn = iemre.get_daily_mrms_ncname(sdate.year) clncfn = iemre.get_dailyc_mrms_ncname() ncvar = "p01d" source = "MRMS Q3" subtitle = "NOAA MRMS Project, GaugeCorr and RadarOnly" elif src == "iemre": ncfn = iemre.get_daily_ncname(sdate.year) clncfn = iemre.get_dailyc_ncname() ncvar = "p01d_12z" source = "IEM Reanalysis" subtitle = "IEM Reanalysis is derived from various NOAA datasets" else: ncfn = "/mesonet/data/prism/%s_daily.nc" % (sdate.year, ) clncfn = "/mesonet/data/prism/prism_dailyc.nc" ncvar = "ppt" source = "OSU PRISM" subtitle = ("PRISM Climate Group, Oregon State Univ., " "http://prism.oregonstate.edu, created 4 Feb 2004.") mp = MapPlot( sector=sector, state=state, axisbg="white", nocaption=True, title="%s:: %s Precip %s" % (source, title, PDICT3[opt]), subtitle="Data from %s" % (subtitle, ), titlefontsize=14, ) idx0 = iemre.daily_offset(sdate) idx1 = iemre.daily_offset(edate) + 1 if not os.path.isfile(ncfn): raise NoDataFound("No data for that year, sorry.") with util.ncopen(ncfn) as nc: if state is not None: x0, y0, x1, y1 = util.grid_bounds( nc.variables["lon"][:], nc.variables["lat"][:], state_bounds[state], ) elif sector in SECTORS: bnds = SECTORS[sector] x0, y0, x1, y1 = util.grid_bounds( nc.variables["lon"][:], nc.variables["lat"][:], [bnds[0], bnds[2], bnds[1], bnds[3]], ) lats = nc.variables["lat"][y0:y1] lons = nc.variables["lon"][x0:x1] if sdate == edate: p01d = mm2inch(nc.variables[ncvar][idx0, y0:y1, x0:x1]) elif (idx1 - idx0) < 32: p01d = mm2inch( np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0)) 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 = mm2inch( np.sum(nc.variables[ncvar][i:i2, y0:y1, x0:x1], 0)) else: p01d += mm2inch( np.sum(nc.variables[ncvar][i:i2, y0:y1, x0:x1], 0)) if np.ma.is_masked(np.max(p01d)): raise NoDataFound("Data Unavailable") plot_units = "inches" cmap = get_cmap(ctx["cmap"]) cmap.set_bad("white") if opt == "dep": # Do departure work now with util.ncopen(clncfn) as nc: climo = mm2inch( np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0)) p01d = p01d - climo [maxv] = np.percentile(np.abs(p01d), [99]) clevs = np.around(np.linspace(0 - maxv, maxv, 11), decimals=2) elif opt == "per": with util.ncopen(clncfn) as nc: climo = mm2inch( np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0)) p01d = p01d / climo * 100.0 cmap.set_under("white") cmap.set_over("black") clevs = [1, 10, 25, 50, 75, 100, 125, 150, 200, 300, 500] plot_units = "percent" else: p01d = np.where(p01d < 0.001, np.nan, p01d) cmap.set_under("white") 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] x2d, y2d = np.meshgrid(lons, lats) if ptype == "c": mp.contourf(x2d, y2d, p01d, clevs, cmap=cmap, units=plot_units, iline=False) else: res = mp.pcolormesh(x2d, y2d, p01d, clevs, cmap=cmap, units=plot_units) res.set_rasterized(True) if sector != "midwest": mp.drawcounties() mp.drawcities() if usdm == "yes": mp.draw_usdm(edate, filled=False, hatched=True) return mp.fig
def plotter(fdict): """ Go """ ctx = util.get_autoplot_context(fdict, get_description()) ptype = ctx['ptype'] sdate = ctx['sdate'] edate = ctx['edate'] src = ctx['src'] opt = ctx['opt'] usdm = ctx['usdm'] if sdate.year != edate.year: raise NoDataFound('Sorry, do not support multi-year plots yet!') days = (edate - sdate).days sector = ctx['sector'] if sdate == edate: title = sdate.strftime("%-d %B %Y") else: title = "%s to %s (inclusive)" % (sdate.strftime("%-d %b"), edate.strftime("%-d %b %Y")) x0 = 0 x1 = -1 y0 = 0 y1 = -1 state = None if len(sector) == 2: state = sector sector = 'state' if src == 'mrms': ncfn = iemre.get_daily_mrms_ncname(sdate.year) clncfn = iemre.get_dailyc_mrms_ncname() ncvar = 'p01d' source = 'MRMS Q3' subtitle = 'NOAA MRMS Project, GaugeCorr and RadarOnly' elif src == 'iemre': ncfn = iemre.get_daily_ncname(sdate.year) clncfn = iemre.get_dailyc_ncname() ncvar = 'p01d_12z' source = 'IEM Reanalysis' subtitle = 'IEM Reanalysis is derived from various NOAA datasets' else: ncfn = "/mesonet/data/prism/%s_daily.nc" % (sdate.year, ) clncfn = "/mesonet/data/prism/prism_dailyc.nc" ncvar = 'ppt' source = 'OSU PRISM' subtitle = ('PRISM Climate Group, Oregon State Univ., ' 'http://prism.oregonstate.edu, created 4 Feb 2004.') mp = MapPlot(sector=sector, state=state, axisbg='white', nocaption=True, title='%s:: %s Precip %s' % (source, title, PDICT3[opt]), subtitle='Data from %s' % (subtitle, ), titlefontsize=14) idx0 = iemre.daily_offset(sdate) idx1 = iemre.daily_offset(edate) + 1 if not os.path.isfile(ncfn): raise NoDataFound("No data for that year, sorry.") with util.ncopen(ncfn) as nc: if state is not None: x0, y0, x1, y1 = util.grid_bounds(nc.variables['lon'][:], nc.variables['lat'][:], state_bounds[state]) elif sector in SECTORS: bnds = SECTORS[sector] x0, y0, x1, y1 = util.grid_bounds( nc.variables['lon'][:], nc.variables['lat'][:], [bnds[0], bnds[2], bnds[1], bnds[3]]) lats = nc.variables['lat'][y0:y1] lons = nc.variables['lon'][x0:x1] if sdate == edate: p01d = distance(nc.variables[ncvar][idx0, y0:y1, x0:x1], 'MM').value('IN') elif (idx1 - idx0) < 32: p01d = distance( np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0), 'MM').value('IN') else: # Too much data can overwhelm this app, need to chunk it for i in range(idx0, idx1, 10): i2 = min([i + 10, idx1]) if idx0 == i: p01d = distance( np.sum(nc.variables[ncvar][i:i2, y0:y1, x0:x1], 0), 'MM').value('IN') else: p01d += distance( np.sum(nc.variables[ncvar][i:i2, y0:y1, x0:x1], 0), 'MM').value('IN') if np.ma.is_masked(np.max(p01d)): raise NoDataFound("Data Unavailable") units = 'inches' cmap = plt.get_cmap(ctx['cmap']) cmap.set_bad('white') if opt == 'dep': # Do departure work now with util.ncopen(clncfn) as nc: climo = distance( np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0), 'MM').value('IN') p01d = p01d - climo [maxv] = np.percentile(np.abs(p01d), [ 99, ]) clevs = np.around(np.linspace(0 - maxv, maxv, 11), decimals=2) elif opt == 'per': with util.ncopen(clncfn) as nc: climo = distance( np.sum(nc.variables[ncvar][idx0:idx1, y0:y1, x0:x1], 0), 'MM').value('IN') p01d = p01d / climo * 100. cmap.set_under('white') cmap.set_over('black') clevs = [1, 10, 25, 50, 75, 100, 125, 150, 200, 300, 500] units = 'percent' else: p01d = np.where(p01d < 0.001, np.nan, p01d) cmap.set_under('white') 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] x2d, y2d = np.meshgrid(lons, lats) if ptype == 'c': mp.contourf(x2d, y2d, p01d, clevs, cmap=cmap, units=units, iline=False) else: res = mp.pcolormesh(x2d, y2d, p01d, clevs, cmap=cmap, units=units) res.set_rasterized(True) if sector != 'midwest': mp.drawcounties() mp.drawcities() if usdm == 'yes': mp.draw_usdm(edate, filled=False, hatched=True) return mp.fig
def plotter(fdict): """ Go """ pgconn = get_dbconn("coop") ctx = get_autoplot_context(fdict, get_description()) sector = ctx["sector"] if len(sector) != 2: raise NoDataFound("Sorry, this app doesn't support multi-state plots.") varname = ctx["var"] year = ctx["year"] popt = ctx["popt"] threshold = ctx["threshold"] table = "alldata_%s" % (sector,) nt = NetworkTable("%sCLIMATE" % (sector,)) syear = ctx.get("syear", 1893) eyear = ctx.get("eyear", datetime.date.today().year) df = read_sql( """ -- get the domain of data WITH events as ( SELECT station, month, case when month < 7 then year - 1 else year end as winter_year, year, extract(doy from day) as doy, day from """ + table + """ WHERE """ + SQLOPT[varname] + """ and month in %s and substr(station, 3, 4) != '0000' and substr(station, 3, 1) not in ('C', 'T') and year >= %s and year <= %s ), agg as ( SELECT station, winter_year, year, doy, day, case when month < 7 then doy + 366 else doy end as winter_doy, rank() OVER ( PARTITION by """ + YRGP[varname] + """, station ORDER by day """ + ORDER[varname] + """) from events) select * from agg where rank = 1 """, pgconn, params=(threshold, tuple(MONTH_DOMAIN[varname]), syear, eyear), index_col="station", ) doy = USEDOY[varname] def f(val): """Make a pretty date.""" base = datetime.date(2000, 1, 1) date = base + datetime.timedelta(days=int(val)) return date.strftime("%-m/%-d") if ctx.get("p") is None: df2 = df[df[YRGP[varname]] == year].copy() title = r"%s %s %s$^\circ$F" % (year, PDICT2[varname], threshold) df2["pdate"] = df2["day"].apply(lambda x: x.strftime("%-m/%-d")) extra = "" else: df2 = df[[doy]].groupby("station").quantile(ctx["p"] / 100.0).copy() title = r"%.0f%s Percentile Date of %s %s$^\circ$F" % ( ctx["p"], th(str(ctx["p"])), PDICT2[varname], threshold, ) df2["pdate"] = df2[doy].apply(f) extra = ", period of record: %.0f-%.0f" % ( df["year"].min(), df["year"].max(), ) if df2.empty: raise NoDataFound("No Data was found") for station in df2.index.values: if station not in nt.sts: continue df2.at[station, "lat"] = nt.sts[station]["lat"] df2.at[station, "lon"] = nt.sts[station]["lon"] mp = MapPlot( sector="state", state=ctx["sector"], continental_color="white", nocaption=True, title=title, subtitle="based on NWS COOP and IEM Daily Estimates%s" % (extra,), ) levs = np.linspace(df2[doy].min() - 1, df2[doy].max() + 1, 7, dtype="i") if "cint" in ctx: levs = np.arange( df2[doy].min() - 1, df2[doy].max() + 1, ctx["cint"], dtype="i" ) levlables = list(map(f, levs)) if popt == "contour": mp.contourf( df2["lon"], df2["lat"], df2[doy], levs, clevlabels=levlables, cmap=ctx["cmap"], ) mp.plot_values(df2["lon"], df2["lat"], df2["pdate"], labelbuffer=5) mp.drawcounties() return mp.fig, df[["year", "winter_doy", "doy"]]