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 main(argv): """Go Main Go""" nt = Table("ISUSM") qdict = loadqc() idbconn = get_dbconn('isuag', user='******') icursor = idbconn.cursor(cursor_factory=psycopg2.extras.DictCursor) pdbconn = get_dbconn('postgis', user='******') pcursor = pdbconn.cursor(cursor_factory=psycopg2.extras.DictCursor) day_ago = int(argv[1]) ts = datetime.datetime.now() - datetime.timedelta(days=day_ago) # Query out the data soil_obs = [] lats = [] lons = [] icursor.execute(""" SELECT station, tsoil_c_avg_qc from sm_daily where valid = '%s' and tsoil_c_avg_qc > -40 and station not in ('AHTI4', 'FRUI4') """ % (ts.strftime("%Y-%m-%d"), )) for row in icursor: stid = row['station'] if qdict.get(stid, {}).get('soil4', False): # print '%s was QCd out' % (stid,) continue soil_obs.append(temperature(row['tsoil_c_avg_qc'], 'C').value('F')) lats.append(nt.sts[stid]['lat']) lons.append(nt.sts[stid]['lon']) if len(lats) < 5: print(("isuag/fancy_4inch found %s obs for %s") % (len(lats), ts.strftime("%Y-%m-%d"))) return # Grid it # numxout = 40 # numyout = 40 # xmin = min(lons) - 2. # ymin = min(lats) - 2. # xmax = max(lons) + 2. # ymax = max(lats) + 2. # xc = (xmax-xmin)/(numxout-1) # yc = (ymax-ymin)/(numyout-1) # xo = xmin + xc * np.arange(0, numxout) # yo = ymin + yc * np.arange(0, numyout) # analysis = griddata((lons, lats), soil_obs, (xo, yo) ) # rbfi = Rbf(lons, lats, soil_obs, function='cubic') # analysis = rbfi(xo, yo) nn = NearestNDInterpolator((lons, lats), np.array(soil_obs)) # analysis = nn(xo, yo) # Query out centroids of counties... pcursor.execute("""SELECT ST_x(ST_centroid(the_geom)) as lon, ST_y(ST_centroid(the_geom)) as lat from uscounties WHERE state_name = 'Iowa' """) clons = [] clats = [] for row in pcursor: clats.append(row['lat']) clons.append(row['lon']) cobs = nn(clons, clats) mp = MapPlot(sector='iowa', title=("Iowa Average 4 inch Soil Temperatures %s") % (ts.strftime("%b %d %Y"), ), subtitle=("Based on gridded analysis (black numbers) of " "ISUSM network observations (red numbers)")) mp.contourf(clons, clats, cobs, np.arange(10, 101, 5), cmap=cm.get_cmap('jet'), units=r'$^\circ$F') mp.plot_values(lons, lats, soil_obs, fmt='%.0f', color='r', labelbuffer=5) mp.plot_values(clons, clats, cobs, fmt='%.0f', textsize=11, labelbuffer=5) # for lo, la, ob in zip(clons, clats, cobs): # xi, yi = m.map(lo, la) # txt = m.ax.text(xi, yi, "%.0f" % (ob,)) 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()
import datetime import sys # thirdparty import numpy as np from scipy.interpolate import NearestNDInterpolator import matplotlib.cm as cm import psycopg2.extras # pyiem from pyiem.plot import MapPlot from pyiem.datatypes import temperature from pyiem.tracker import loadqc from pyiem.network import Table nt = Table("ISUSM") qdict = loadqc() ISUAG = psycopg2.connect(database='isuag', host='iemdb', user='******') icursor = ISUAG.cursor(cursor_factory=psycopg2.extras.DictCursor) POSTGIS = psycopg2.connect(database='postgis', host='iemdb', user='******') pcursor = POSTGIS.cursor(cursor_factory=psycopg2.extras.DictCursor) day_ago = int(sys.argv[1]) ts = datetime.datetime.now() - datetime.timedelta(days=day_ago) # Query out the data soil_obs = [] lats = [] lons = [] icursor.execute("""
PAJN | OSO PHFO | OSO PACR | RVF PAMC | SCD PAFA | SCD PANC | SHP PTSA | SSM PAER | STQ PALU | STQ PAAQ | TST PHEB | TST PAFG | ZFP""" from pyiem.network import Table nt = Table("WFO") data = {} labels = {} uniq = [] for line in text.split("\n"): tokens = line.replace(" ", "").split("|") wfo = tokens[0][1:] if tokens[0][0] == 'P': wfo = tokens[0] key = "%s" % (tokens[1], ) if not nt.sts.has_key(wfo): continue # P wfo = tokens[0][1:] if not key in uniq: