def grid_relh(nc, ts, rs): lats = [] lons = [] vals = [] for i in range(len(rs)): if rs[i]['max_tmpf'] is not None and rs[i]['max_dwpf'] is not None: lats.append( locs[rs[i]['station']]['lat'] ) lons.append( locs[rs[i]['station']]['lon'] ) vals.append( mesonet.relh( rs[i]['max_tmpf'], rs[i]['max_dwpf'] ) ) if len(vals) < 4: print "No RELH data at all for time: %s" % (ts,) return grid = Ngl.natgrid(lons, lats, vals, XAXIS, YAXIS) offset = int((ts - BASE).hours ) if grid is not None: gt = grid.transpose() gt = numpy.where(gt < 100.1, gt, 100.) nc.variables['relh'][offset,:,:] = numpy.where(gt < 12., 12., gt) else: print "RELH gridding failed, len vals %s" % (len(vals),)
relh = [] vsby = [] raining = False for row2 in acursor2: if row2[3] >= maxD and row2[1] >= 100: print row2[0], row2[3] maxD = row2[3] if row2[2] > 0: raining = True if len(times) > 10 and row2[0].hour == 0: times.append(row2[0].hour * 60 + row2[0].minute + 1440) else: times.append(row2[0].hour * 60 + row2[0].minute) if row2[0].hour in [5, 9]: print row2 relh.append(mesonet.relh(row2[1], row2[3])) vsby.append(row2[4]) # c = '#E8AFAF' # if raining: # c = 'b' c = colors.pop() ax.plot(times, relh, c=c, label=station) ax2.plot(times, vsby, linestyle="--", c=c) # ax.plot( [6*60, 10*60, 10*60, 6*60,6*60], [69,75,84,77,69] , c='black') # ax.text(6*60, 66, "6-10 AM Diff: %.1f$^{\circ}\mathrm{F}$" % ((diff/3.,) )) # # ax.text(14*60, 66, "Newton, IA (KTNU AWOS)") ax.grid(True)
#output = open('ragbrai.dat', 'w') for sdate, edate in DATES: acursor.execute(""" SELECT tmpf, dwpf, sknt, drct, valid from t%s WHERE station = 'DSM' and valid BETWEEN '%s 00:00' and '%s 23:59' and tmpf > 0 and dwpf > 0 and sknt >= 0 and drct >= 0 ORDER by valid ASC """ % (sdate.year, sdate.strftime("%Y-%m-%d"), edate.strftime("%Y-%m-%d"))) cnt = 0 tot = 0 ttot = 0 utot = 0 ucnt = 0 vtot = 0 for row in acursor: ttot += row[0] h = mesonet.heatidx(row[0], mesonet.relh(row[0], row[1])) if row[4].hour > 5 and row[4].hour < 22: u, v = uv(row[2], row[3]) #output.write("%s,%s,%s,%.1f,%.2f,%.2f\n" % (row[4].strftime("%Y,%m,%d,%H,%M"), row[0], row[1], h, u, v)) utot += u vtot += v ucnt += 1 tot += h cnt += 1 print "%s %3s %4.1f %4.1f %4.1f %4.1f" % ( sdate.year, cnt, ttot / float(cnt), tot / float(cnt), utot / float(ucnt), vtot / float(ucnt)) uwnd[sdate.year - 1973] = utot / float(ucnt) * 1.15 hindex[sdate.year - 1973] = tot / float(cnt) #output.close()
#output = open('ragbrai.dat', 'w') for sdate,edate in DATES: acursor.execute(""" SELECT tmpf, dwpf, sknt, drct, valid from t%s WHERE station = 'DSM' and valid BETWEEN '%s 00:00' and '%s 23:59' and tmpf > 0 and dwpf > 0 and sknt >= 0 and drct >= 0 ORDER by valid ASC """ % (sdate.year, sdate.strftime("%Y-%m-%d"), edate.strftime("%Y-%m-%d") )) cnt = 0 tot = 0 ttot = 0 utot = 0 ucnt = 0 vtot = 0 for row in acursor: ttot += row[0] h = mesonet.heatidx(row[0], mesonet.relh(row[0], row[1])) if row[4].hour > 5 and row[4].hour < 22: u,v = uv(row[2], row[3]) #output.write("%s,%s,%s,%.1f,%.2f,%.2f\n" % (row[4].strftime("%Y,%m,%d,%H,%M"), row[0], row[1], h, u, v)) utot += u vtot += v ucnt += 1 tot += h cnt += 1 print "%s %3s %4.1f %4.1f %4.1f %4.1f" % (sdate.year, cnt, ttot / float(cnt), tot / float(cnt), utot / float(ucnt), vtot / float(ucnt)) uwnd[sdate.year-1973] = utot / float(ucnt) * 1.15 hindex[sdate.year-1973] = tot / float(cnt) #output.close() import matplotlib.pyplot as plt fig = plt.figure()
sql = "SELECT *, \ case when sknt > 0 THEN sknt ELSE 0 END as sknt0,\ case when p01m > 0 THEN p01m ELSE 0 END as p,\ case when skyc > -1 THEN skyc ELSE 0 END as sk from t%s WHERE station = '%s' and tmpf > -50 and extract(year from valid) = %s ORDER by valid ASC" % ( yr, stid, yr) rs = asos.query(sql).dictresult() for i in range(len(rs)): ts = mx.DateTime.strptime(rs[i]['valid'][:13], "%Y-%m-%d %H") if (now == ts): #print 'DUP', now continue tmpf = rs[i]['tmpf'] mph = rs[i]['sknt0'] / 1.18 psun = pcsun[int(rs[i]['sk'])] phour = rs[i]['p'] * 25.4 relh = mesonet.relh(rs[i]['tmpf'], rs[i]['dwpf']) if (relh == "M"): relh = 65 while ((now + mx.DateTime.RelativeDateTime(hours=1)) < ts): now += mx.DateTime.RelativeDateTime(hours=1) #print 'MISSING', now missing += 1 out.write(fmt % (now.strftime("%Y%m%d%H"), tmpf, mph, psun, phour, relh)) out.write(fmt % (ts.strftime("%Y%m%d%H"), tmpf, mph, psun, phour, relh)) now = ts out.close() print 'Total missing:', missing
import mx.DateTime data = {} sts = mx.DateTime.DateTime(2010,7,1) ets = mx.DateTime.DateTime(2010,8,17) interval = mx.DateTime.RelativeDateTime(days=1) now = sts while now < ets: data[now.strftime("%Y%m%d")] = [0]*24 now += interval rs = asos.query("SELECT valid, tmpf, dwpf from t2010 WHERE station = 'DSM' and valid > '2010-07-01' ORDER by valid ASC").dictresult() for i in range(len(rs)): ts = mx.DateTime.strptime(rs[i]['valid'][:16], '%Y-%m-%d %H:%M') h = mesonet.heatidx(rs[i]['tmpf'], mesonet.relh(rs[i]['tmpf'], rs[i]['dwpf'])) data[ ts.strftime("%Y%m%d") ][ ts.hour ] = h sts = mx.DateTime.DateTime(2010,7,1) ets = mx.DateTime.DateTime(2010,8,17) interval = mx.DateTime.RelativeDateTime(days=1) now = sts while now < ets: lkp = now.strftime("%Y%m%d") sz = 24.0 cln = [] for v in data[lkp]: if v != 0: cln.append(v) print "%s,%.2f,%.2f,%.2f" % (now.strftime("%Y-%m-%d"), max(cln), sum( cln ) / float(len(cln)), min(cln) )
now = mx.DateTime.DateTime(1972,12,31,23) for yr in range(1973,2007): sql = "SELECT *, \ case when sknt > 0 THEN sknt ELSE 0 END as sknt0,\ case when p01m > 0 THEN p01m ELSE 0 END as p,\ case when skyc > -1 THEN skyc ELSE 0 END as sk from t%s WHERE station = '%s' and tmpf > -50 and extract(year from valid) = %s ORDER by valid ASC" % (yr,stid,yr) rs = asos.query(sql).dictresult() for i in range(len(rs)): ts = mx.DateTime.strptime(rs[i]['valid'][:13], "%Y-%m-%d %H") if (now == ts): #print 'DUP', now continue tmpf = rs[i]['tmpf'] mph = rs[i]['sknt0'] / 1.18 psun = pcsun[ int(rs[i]['sk']) ] phour = rs[i]['p'] * 25.4 relh = mesonet.relh( rs[i]['tmpf'], rs[i]['dwpf']) if (relh == "M"): relh = 65 while ((now + mx.DateTime.RelativeDateTime(hours=1)) < ts): now += mx.DateTime.RelativeDateTime(hours=1) #print 'MISSING', now missing += 1 out.write( fmt % (now.strftime("%Y%m%d%H"),tmpf, mph, psun, phour, relh)) out.write(fmt % (ts.strftime("%Y%m%d%H"), tmpf, mph, psun, phour, relh)) now = ts out.close() print 'Total missing:', missing