def test_heatindex(self): ''' Test our heat index calculations ''' t = datatypes.temperature(80.0, 'F') td = datatypes.temperature(70.0, 'F') hdx = meteorology.heatindex(t, td) self.assertAlmostEqual( hdx.value("F"), 83.93, 2) t = datatypes.temperature(30.0, 'F') hdx = meteorology.heatindex(t, td) self.assertAlmostEqual( hdx.value("F"), 30.00, 2)
def test_heatindex(self): ''' Test our heat index calculations ''' t = datatypes.temperature(80.0, 'F') td = datatypes.temperature(70.0, 'F') hdx = meteorology.heatindex(t, td) self.assertAlmostEqual(hdx.value("F"), 83.93, 2) t = datatypes.temperature(30.0, 'F') hdx = meteorology.heatindex(t, td) self.assertAlmostEqual(hdx.value("F"), 30.00, 2)
def test_heatindex(): """ Test our heat index calculations """ t = datatypes.temperature(80.0, "F") td = datatypes.temperature(70.0, "F") hdx = meteorology.heatindex(t, td) assert abs(hdx.value("F") - 83.93) < 0.01 t = datatypes.temperature(30.0, "F") hdx = meteorology.heatindex(t, td) assert abs(hdx.value("F") - 30.00) < 0.01
def test_heatindex(): ''' Test our heat index calculations ''' t = datatypes.temperature(80.0, 'F') td = datatypes.temperature(70.0, 'F') hdx = meteorology.heatindex(t, td) assert abs(hdx.value("F") - 83.93) < 0.01 t = datatypes.temperature(30.0, 'F') hdx = meteorology.heatindex(t, td) assert abs(hdx.value("F") - 30.00) < 0.01
def test_vectorized(self): """See that heatindex and windchill can do lists""" temp = datatypes.temperature([0, 10], 'F') sknt = datatypes.speed([30, 40], 'MPH') val = meteorology.windchill(temp, sknt).value('F') self.assertAlmostEquals(val[0], -24.50, 2) t = datatypes.temperature([80.0, 90.0], 'F') td = datatypes.temperature([70.0, 60.0], 'F') hdx = meteorology.heatindex(t, td) self.assertAlmostEqual(hdx.value("F")[0], 83.93, 2)
def one(): pgconn = psycopg2.connect(database='asos', host='localhost', port=5555, user='******') cursor = pgconn.cursor() nt = NetworkTable(["IA_ASOS", "AWOS"]) ids = nt.sts.keys() ids.sort() print """ <table class="table table-condensed table-striped"> <thead> <tr><th>ID</th><th>Station Name</th><th>3 Sep Peak Heat Index</th> <th>Last Highest</th><th>Date</th></tr> </thead> """ bah = nt.sts.keys() for sid in ids: cursor.execute( """ select valid, tmpf, dwpf from alldata where station = %s and extract(month from valid) = 9 and dwpf is not null and tmpf > 84 and valid > '1990-01-01' ORDER by valid DESC """, (sid, )) thisdate = [0, None, None, None] for _, row in enumerate(cursor): hdx = heatindex(temperature(row[1], 'F'), temperature(row[2], 'F')).value('F') if row[0].strftime("%Y%m%d") == '20150903': if hdx > thisdate[0]: thisdate = [hdx, row[0], row[1], row[2]] continue if thisdate[1] is None: break if hdx >= thisdate[0]: bah.remove(sid) print(('%s,%s,%.0f,(%.0f/%.0f),%.0f,(%.0f/%.0f),%s') % (sid, nt.sts[sid]['name'], thisdate[0], thisdate[2], thisdate[3], hdx, row[1], row[2], row[0].strftime("%d %b %Y %I:%M %p"))) break print 'missed', bah print "</table>"
def one(): pgconn = psycopg2.connect(database='asos', host='localhost', port=5555, user='******') cursor = pgconn.cursor() nt = NetworkTable(["IA_ASOS", "AWOS"]) ids = nt.sts.keys() ids.sort() print """ <table class="table table-condensed table-striped"> <thead> <tr><th>ID</th><th>Station Name</th><th>3 Sep Peak Heat Index</th> <th>Last Highest</th><th>Date</th></tr> </thead> """ bah = nt.sts.keys() for sid in ids: cursor.execute(""" select valid, tmpf, dwpf from alldata where station = %s and extract(month from valid) = 9 and dwpf is not null and tmpf > 84 and valid > '1990-01-01' ORDER by valid DESC """, (sid,)) thisdate = [0, None, None, None] for _, row in enumerate(cursor): hdx = heatindex(temperature(row[1], 'F'), temperature(row[2], 'F')).value('F') if row[0].strftime("%Y%m%d") == '20150903': if hdx > thisdate[0]: thisdate = [hdx, row[0], row[1], row[2]] continue if thisdate[1] is None: break if hdx >= thisdate[0]: bah.remove(sid) print(('%s,%s,%.0f,(%.0f/%.0f),%.0f,(%.0f/%.0f),%s' ) % (sid, nt.sts[sid]['name'], thisdate[0], thisdate[2], thisdate[3], hdx, row[1], row[2], row[0].strftime("%d %b %Y %I:%M %p"))) break print 'missed', bah print "</table>"
def test_vectorized(): """See that heatindex and windchill can do lists""" temp = datatypes.temperature([0, 10], "F") sknt = datatypes.speed([30, 40], "MPH") val = meteorology.windchill(temp, sknt).value("F") assert abs(val[0] - -24.50) < 0.01 t = datatypes.temperature([80.0, 90.0], "F") td = datatypes.temperature([70.0, 60.0], "F") hdx = meteorology.heatindex(t, td) assert abs(hdx.value("F")[0] - 83.93) < 0.01 tmpf = np.array([80.0, 90.0]) * units("degF") dwpf = np.array([70.0, 60.0]) * units("degF") smps = np.array([10.0, 20.0]) * units("meter per second") feels = meteorology.mcalc_feelslike(tmpf, dwpf, smps) assert abs(feels.to(units("degF")).magnitude[0] - 83.15) < 0.01 tmpf = masked_array([80.0, np.nan], units("degF"), mask=[False, True]) feels = meteorology.mcalc_feelslike(tmpf, dwpf, smps) assert abs(feels.to(units("degF")).magnitude[0] - 83.15) < 0.01 assert feels.mask[1]
def test_vectorized(): """See that heatindex and windchill can do lists""" temp = datatypes.temperature([0, 10], 'F') sknt = datatypes.speed([30, 40], 'MPH') val = meteorology.windchill(temp, sknt).value('F') assert abs(val[0] - -24.50) < 0.01 t = datatypes.temperature([80.0, 90.0], 'F') td = datatypes.temperature([70.0, 60.0], 'F') hdx = meteorology.heatindex(t, td) assert abs(hdx.value("F")[0] - 83.93) < 0.01 tmpf = np.array([80., 90.]) * units('degF') dwpf = np.array([70., 60.]) * units('degF') smps = np.array([10., 20.]) * units('meter per second') feels = meteorology.mcalc_feelslike(tmpf, dwpf, smps) assert abs(feels.to(units("degF")).magnitude[0] - 83.15) < 0.01 tmpf = masked_array([80., np.nan], units('degF'), mask=[False, True]) feels = meteorology.mcalc_feelslike(tmpf, dwpf, smps) assert abs(feels.to(units("degF")).magnitude[0] - 83.15) < 0.01 assert feels.mask[1]
cursor.execute("""SELECT tmpf, dwpf from alldata where station = 'DSM' and tmpf >= 80 and dwpf > 0 and tmpf < 110""") for row in cursor: otmpf.append(row[0]) odwpf.append(row[1]) otmpf = dt.temperature(numpy.array(otmpf), 'F') odwpf = dt.temperature(numpy.array(odwpf), 'F') orelh = meteorology.relh(otmpf, odwpf) tmpf = dt.temperature(numpy.arange(80, 110), 'F') dwpf = dt.temperature(numpy.arange(40, 80), 'F') (t, d) = numpy.meshgrid(tmpf.value("F"), dwpf.value("F")) hindex = meteorology.heatindex(dt.temperature(t, 'F'), dt.temperature(d, 'F')) counts = numpy.zeros(numpy.shape(hindex.value("F")), 'f') for otmp, odwp in zip(otmpf.value("F"), odwpf.value("F")): if odwp < 40 or odwp >= 79.5: continue counts[(int(round(odwp)) - 40), int(round(otmp)) - 80] += 1.0 ttot = numpy.sum(counts, 0) print ttot ratio = numpy.ma.array(counts / ttot * 100.0) #ratio.mask = numpy.where(ratio == 0, True, False) (fig, ax) = plt.subplots(1, 1) cmap = cm.get_cmap('jet') cmap.set_under("tan")
idx = indices[sid]['idx'] name = nc.variables["stationName"][idx].tostring().replace('\x00','') latitude = nc.variables['latitude'][idx] longitude = nc.variables['longitude'][idx] tmpf = s(nc.variables['temperature'][idx]) dwpf = s(nc.variables['dewpoint'][idx]) qcd = nc.variables['temperatureQCD'][idx][0] if qcd < -10 or qcd > 10: tmpf = "M" dwpf = "M" heat = "M" if tmpf != "M" and dwpf != "M": t = temperature(nc.variables['temperature'][idx], 'K') d = temperature(nc.variables['dewpoint'][idx], 'K') relh = meteorology.relh(t, d).value("%") heat = "%5.1f" % (meteorology.heatindex(t, d).value("F"),) drct = s2( nc.variables['windDir'][idx]) smps = s2( nc.variables['windSpeed'][idx]) sped = "M" if smps != "M": sped = "%5.1f" % (nc.variables['windSpeed'][idx] * 2.23694,) wcht = "M" if tmpf != "M" and sped != "M": t = temperature(nc.variables['temperature'][idx], 'K') sped = speed( nc.variables['windSpeed'][idx], 'MPS') wcht = "%5.1f" % (meteorology.windchill(t, sped).value("F"),) ts = indices[sid]['ts'] out.write("%5.5s %25.25s %8.4f %10.4f %02i %02i %5s %5s %5s %5s %5s %5s\n" % (sid, name, latitude,
cursor.execute("""SELECT tmpf, dwpf from alldata where station = 'DSM' and tmpf >= 80 and dwpf > 0 and tmpf < 110""") for row in cursor: otmpf.append( row[0] ) odwpf.append( row[1] ) otmpf = dt.temperature(numpy.array(otmpf), 'F') odwpf = dt.temperature(numpy.array(odwpf), 'F') orelh = meteorology.relh(otmpf, odwpf) tmpf = dt.temperature(numpy.arange(80,110), 'F') dwpf = dt.temperature(numpy.arange(40,80), 'F') (t, d) = numpy.meshgrid(tmpf.value("F"), dwpf.value("F")) hindex = meteorology.heatindex(dt.temperature(t,'F'), dt.temperature(d, 'F')) counts = numpy.zeros( numpy.shape(hindex.value("F")), 'f') for otmp, odwp in zip(otmpf.value("F"), odwpf.value("F")): if odwp < 40 or odwp >= 79.5: continue counts[(int(round(odwp)) - 40), int(round(otmp)) - 80 ] += 1.0 ttot = numpy.sum(counts,0) print ttot ratio = numpy.ma.array(counts / ttot * 100.0) #ratio.mask = numpy.where(ratio == 0, True, False) (fig, ax) = plt.subplots(1,1) cmap = cm.get_cmap('jet') cmap.set_under("tan")
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt ASOS = psycopg2.connect(database='asos', host='iemdb', user='******') ctx = get_autoplot_context(fdict, get_description()) station = ctx['zstation'] network = ctx['network'] highlightyear = ctx['year'] ytd = ctx['ytd'] varname = ctx['var'] inc = ctx['inc'] nt = NetworkTable(network) doylimiter = get_doylimit(ytd, varname) tmpflimit = "and tmpf >= 50" if varname != 'windchill' else 'and tmpf < 50' if varname not in ['windchill', 'heatindex']: tmpflimit = "" df = read_sql(""" SELECT to_char(valid, 'YYYYmmddHH24') as d, avg(tmpf)::int as tmpf, avg(dwpf)::int as dwpf, avg(coalesce(sknt, 0)) as sknt from alldata WHERE station = %s """ + tmpflimit + """ and dwpf <= tmpf and valid > '1973-01-01' and report_type = 2 """ + doylimiter + """ GROUP by d """, ASOS, params=(station, ), index_col=None) df['year'] = df['d'].apply(lambda x: int(x[:4])) df2 = df title2 = VDICT[varname] compop = np.greater inctitle = '' if varname == 'heatindex': df['heatindex'] = pymet.heatindex(temperature(df['tmpf'].values, 'F'), temperature(df['dwpf'].values, 'F')).value('F') inctitle = " [All Obs Included]" if inc == 'no': df2 = df[df['heatindex'] > df['tmpf']] inctitle = " [Only Additive]" else: df2 = df maxval = int(df2['heatindex'].max() + 1) LEVELS[varname] = np.arange(maxval - 31, maxval) elif varname == 'windchill': compop = np.less df['year'] = df['d'].apply(lambda x: (int(x[:4]) - 1) if int(x[4:6]) < 7 else int(x[:4])) df['windchill'] = pymet.windchill(temperature(df['tmpf'].values, 'F'), speed(df['sknt'].values, 'KT')).value('F') inctitle = " [All Obs Included]" if inc == 'no': df2 = df[df['windchill'] < df['tmpf']] inctitle = " [Only Additive]" else: df2 = df minval = int(df2['windchill'].min() - 1) LEVELS[varname] = np.arange(minval, minval + 51) else: maxval = int(df2[varname].max() + 1) LEVELS[varname] = np.arange(maxval - 31, maxval) minyear = max([1973, nt.sts[station]['archive_begin'].year]) maxyear = datetime.date.today().year years = float((maxyear - minyear) + 1) x = [] y = [] y2 = [] fig = plt.figure(figsize=(9, 6)) ax = fig.add_axes([0.1, 0.1, 0.6, 0.8]) yloc = 1.0 xloc = 1.13 yrlabel = ("%s" % (highlightyear, ) if varname != 'windchill' else '%s-%s' % (highlightyear, highlightyear + 1)) ax.text(xloc + 0.08, yloc + 0.04, 'Avg:', transform=ax.transAxes, color='b') ax.text(xloc + 0.21, yloc + 0.04, yrlabel, transform=ax.transAxes, color='r') df3 = df2[df2['year'] == highlightyear] for level in LEVELS[varname]: x.append(level) y.append(len(df2[compop(df2[varname], level)]) / years) y2.append(len(df3[compop(df3[varname], level)])) if level % 2 == 0: ax.text(xloc, yloc, '%s' % (level, ), transform=ax.transAxes) ax.text(xloc + 0.08, yloc, '%.1f' % (y[-1], ), transform=ax.transAxes, color='b') ax.text(xloc + 0.21, yloc, '%.0f' % (y2[-1], ), transform=ax.transAxes, color='r') yloc -= 0.04 ax.text(xloc, yloc, 'n=%s' % (len(df2.index), ), transform=ax.transAxes) for x0, y0, y02 in zip(x, y, y2): ax.plot([x0, x0], [y0, y02], color='k') rdf = pd.DataFrame({'level': x, 'avg': y, 'd%s' % (highlightyear, ): y2}) x = np.array(x, dtype=np.float64) ax.scatter(x, y, color='b', label='Avg') ax.scatter(x, y2, color='r', label=yrlabel) ax.grid(True) ymax = int(max([max(y), max(y2)])) ax.set_xlim(x[0] - 0.5, x[-1] + 0.5) dy = 24 * (int(ymax / 240) + 1) ax.set_yticks(range(0, ymax, dy)) ax.set_ylim(-0.5, ymax + 5) ax2 = ax.twinx() ax2.set_ylim(-0.5, ymax + 5) ax2.set_yticks(range(0, ymax, dy)) ax2.set_yticklabels(["%.0f" % (s, ) for s in np.arange(0, ymax, dy) / 24]) ax2.set_ylabel("Expressed in 24 Hour Days") ax.set_ylabel("Hours Per Year") ax.set_xlabel("%s $^\circ$F" % (VDICT[varname], )) title = 'till %s' % (datetime.date.today().strftime("%-d %b"), ) title = "Entire Year" if ytd == 'no' else title ax.set_title(("[%s] %s %s-%s\n" "%s Histogram (%s)%s") % (station, nt.sts[station]['name'], minyear, datetime.date.today().year, title2, title, inctitle)) ax.legend(loc='best', scatterpoints=1) return fig, rdf
def main(argv): """Go Main Go""" network = argv[1] wxcfn = argv[2] utc = datetime.datetime.utcnow() utc = utc.replace(tzinfo=pytz.UTC) out = open(wxcfn, 'w') out.write("""Weather Central 001d0300 Surface Data TimeStamp=%s 12 5 Station 25 Station Name 8 Lat 10 Lon 2 Hour 2 Minute 5 Air Temperature F 5 Dew Point F 5 Wind Direction deg 5 Wind Speed mph 5 Heat Index F 5 Wind Chill F """ % (utc.strftime("%Y.%m.%d.%H%M"), )) fn = None for i in range(4): now = utc - datetime.timedelta(hours=i) testfn = now.strftime("/mesonet/data/madis/mesonet1/%Y%m%d_%H00.nc") if os.path.isfile(testfn): fn = testfn break if fn is None: sys.exit() indices = {} BOGUS = datetime.datetime(2000, 1, 1) BOGUS = BOGUS.replace(tzinfo=pytz.UTC) nc = ncopen(fn) providers = chartostring(nc.variables["dataProvider"][:]) stations = chartostring(nc.variables["stationId"][:]) names = chartostring(nc.variables["stationName"][:]) for i, provider in enumerate(providers): if provider != network: continue sid = stations[i] # We have an ob! ticks = int(nc.variables["observationTime"][i]) ts = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=ticks) ts = ts.replace(tzinfo=pytz.UTC) if ts > indices.get(sid, {'ts': BOGUS})['ts']: indices[sid] = {'ts': ts, 'idx': i} for sid in indices: idx = indices[sid]['idx'] name = names[idx] latitude = nc.variables['latitude'][idx] longitude = nc.variables['longitude'][idx] tmpf = s(nc.variables['temperature'][idx]) dwpf = s(nc.variables['dewpoint'][idx]) qcd = nc.variables['temperatureQCD'][idx][0] if qcd < -10 or qcd > 10: tmpf = "M" dwpf = "M" heat = "M" if tmpf != "M" and dwpf != "M": t = temperature(nc.variables['temperature'][idx], 'K') d = temperature(nc.variables['dewpoint'][idx], 'K') # relh = meteorology.relh(t, d).value("%") heat = "%5.1f" % (meteorology.heatindex(t, d).value("F"), ) drct = s2(nc.variables['windDir'][idx]) smps = s2(nc.variables['windSpeed'][idx]) sped = "M" if smps != "M": sped = "%5.1f" % (nc.variables['windSpeed'][idx] * 2.23694, ) wcht = "M" if tmpf != "M" and sped != "M": t = temperature(nc.variables['temperature'][idx], 'K') sped = speed(nc.variables['windSpeed'][idx], 'MPS') wcht = "%5.1f" % (meteorology.windchill(t, sped).value("F"), ) ts = indices[sid]['ts'] out.write(("%5.5s %25.25s %8.4f %10.4f " "%02i %02i %5s %5s %5s %5s %5s %5s\n") % (sid, name, latitude, longitude, ts.hour, ts.minute, tmpf, dwpf, drct, sped, heat, wcht)) nc.close() out.close() pqstr = "data c 000000000000 wxc/wxc_%s.txt bogus txt" % ( network.lower(), ) subprocess.call("/home/ldm/bin/pqinsert -p '%s' %s" % (pqstr, wxcfn), shell=True) os.remove(wxcfn)
idx = indices[sid]['idx'] name = nc.variables["stationName"][idx].tostring().replace('\x00', '') latitude = nc.variables['latitude'][idx] longitude = nc.variables['longitude'][idx] tmpf = s(nc.variables['temperature'][idx]) dwpf = s(nc.variables['dewpoint'][idx]) qcd = nc.variables['temperatureQCD'][idx][0] if qcd < -10 or qcd > 10: tmpf = "M" dwpf = "M" heat = "M" if tmpf != "M" and dwpf != "M": t = temperature(nc.variables['temperature'][idx], 'K') d = temperature(nc.variables['dewpoint'][idx], 'K') relh = meteorology.relh(t, d).value("%") heat = "%5.1f" % (meteorology.heatindex(t, d).value("F"), ) drct = s2(nc.variables['windDir'][idx]) smps = s2(nc.variables['windSpeed'][idx]) sped = "M" if smps != "M": sped = "%5.1f" % (nc.variables['windSpeed'][idx] * 2.23694, ) wcht = "M" if tmpf != "M" and sped != "M": t = temperature(nc.variables['temperature'][idx], 'K') sped = speed(nc.variables['windSpeed'][idx], 'MPS') wcht = "%5.1f" % (meteorology.windchill(t, sped).value("F"), ) ts = indices[sid]['ts'] out.write(
acursor.execute("""SELECT valid, tmpf, dwpf, sknt, skyc1, skyc2, skyc3 from alldata where station = 'AMW' and valid BETWEEN '%s 00:00' and '%s 23:59' and tmpf > -30 and dwpf > -30 """ % (day.strftime("%Y-%m-%d"),day.strftime("%Y-%m-%d"))) relh = [] tmpf = [] heat = 'N' windchill = 'N' clcount = 0 for row2 in acursor: t = temperature(row2[1], 'F') td = temperature(row2[2], 'F') if met.heatindex(t, td).value('F') > 90: heat = 'Y' if t.value('F') < 50: print t.value('F'), td.value('F'), met.heatindex(t, td).value('F') if row2[1] < 32 and row2[3] > 10: windchill = 'Y' #approx sky = 'Clear' if 'OVC' in [row2[4], row2[5], row2[6]]: sky = 'Overcast' elif 'BKN' in [row2[4], row2[5], row2[6]]: sky = 'Broken' elif 'FEW' in [row2[4], row2[5], row2[6]]: sky = 'Fair Skies' if row2[0].hour > 7 and row2[0].hour < 18: relh.append( met.relh(t,td).value('%') )
ASOS = psycopg2.connect(database='asos', host='iemdb', user='******') cursor = ASOS.cursor() cursor.execute(""" SELECT valid, tmpf, dwpf from alldata where station = 'DSM' and tmpf >= 80 and dwpf > -50 and valid > '1935-01-01' and (extract(minute from valid) = 0 or extract(minute from valid) > 52) """) hits3 = np.zeros( (2014-1935), 'f') hits5 = np.zeros( (2014-1935), 'f') total = np.zeros( (2014-1935), 'f') for row in cursor: t = dt.temperature(row[1], 'F') d = dt.temperature(row[2], 'F') hdx = met.heatindex(t, d) if (hdx.value("F") - 3) >= row[1]: hits3[ row[0].year - 1935 ] += 1.0 if (hdx.value("F") - 5) >= row[1]: hits5[ row[0].year - 1935 ] += 1.0 total[ row[0].year - 1935 ] += 1.0 (fig, ax) = plt.subplots(2,1, sharex=True) val = hits3 / total * 100.0 avg = np.average(val) bars = ax[0].bar(np.arange(1935,2014)-0.4, val , ec='b', fc='b') ax[0].plot([1933,2013], [avg,avg], c='k') for bar in bars:
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt ASOS = psycopg2.connect(database='asos', host='iemdb', user='******') cursor = ASOS.cursor(cursor_factory=psycopg2.extras.DictCursor) station = fdict.get('zstation', 'DSM') network = fdict.get('network', 'IA_ASOS') highlightyear = int(fdict.get('year', 2015)) nt = NetworkTable(network) cursor.execute(""" SELECT to_char(valid, 'YYYYmmddHH24') as d, avg(tmpf), avg(dwpf) from alldata WHERE station = %s and tmpf >= 80 and dwpf >= 30 and dwpf <= tmpf and valid > '1973-01-01' GROUP by d """, (station, )) rows = [] for row in cursor: t = temperature(row[1], 'F') d = temperature(row[2], 'F') h = pymet.heatindex(t, d) if h.value('F') >= t.value('F'): rows.append(dict(year=int(row[0][:4]), heatindex=h.value('F'))) minyear = max([1973, nt.sts[station]['archive_begin'].year]) maxyear = datetime.date.today().year years = float((maxyear - minyear) + 1) df = pd.DataFrame(rows) x = [] y = [] y2 = [] (fig, ax) = plt.subplots(1, 1) yloc = 0.9 ax.text(0.7, 0.94, 'Avg:', transform=ax.transAxes, color='b') ax.text(0.85, 0.94, '%s:' % (highlightyear,), transform=ax.transAxes, color='r') for level in range(90, 121): x.append(level) y.append(len(df[df['heatindex'] >= level]) / years) y2.append(len(df[np.logical_and(df['heatindex'] >= level, df['year'] == highlightyear)])) if level % 2 == 0: ax.text(0.6, yloc, '%s' % (level,), transform=ax.transAxes) ax.text(0.7, yloc, '%.1f' % (y[-1],), transform=ax.transAxes, color='b') ax.text(0.85, yloc, '%.0f' % (y2[-1],), transform=ax.transAxes, color='r') yloc -= 0.04 x = np.array(x, dtype=np.float64) ax.scatter(x, y, color='b', label='Avg') ax.scatter(x, y2, color='r', label="%s" % (highlightyear,)) ax.grid(True) ax.set_ylim(-0.5, int(max(y)) + 5) ax.set_xlim(89.5, 120.5) ax.set_yticks(range(0, int(max(y)), 24)) ax.set_ylabel("Hours Per Year") ax.set_xlabel("Heat Index Temp $^\circ$F") ax.set_title(("[%s] %s %s-%s\n" "Heat Index (when accretive to air temp) Histogram" ) % (station, nt.sts[station]['name'], minyear, datetime.date.today().year)) ax.legend(loc=(0.2, 0.8), scatterpoints=1) return fig, df
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt ASOS = psycopg2.connect(database='asos', host='iemdb', user='******') ctx = get_autoplot_context(fdict, get_description()) station = ctx['zstation'] network = ctx['network'] highlightyear = ctx['year'] ytd = ctx['ytd'] varname = ctx['var'] nt = NetworkTable(network) doylimiter = "" if ytd == 'yes': doylimiter = (" and extract(doy from valid) < " " extract(doy from 'TODAY'::date) ") df = read_sql(""" SELECT to_char(valid, 'YYYYmmddHH24') as d, avg(tmpf)::int as tmpf, avg(dwpf)::int as dwpf from alldata WHERE station = %s and tmpf >= 50 and dwpf <= tmpf and valid > '1973-01-01' and report_type = 2 """ + doylimiter + """ GROUP by d """, ASOS, params=(station, ), index_col=None) df['year'] = df['d'].apply(lambda x: int(x[:4])) df2 = df title2 = VDICT[varname] if varname == 'heatindex': title2 = "Heat Index (when accretive to air temp)" df['heatindex'] = df[['tmpf', 'dwpf']].apply( lambda x: pymet.heatindex(temperature(x[0], 'F'), temperature(x[1], 'F')).value('F'), axis=1) df2 = df[df['heatindex'] > df['tmpf']] minyear = max([1973, nt.sts[station]['archive_begin'].year]) maxyear = datetime.date.today().year years = float((maxyear - minyear) + 1) x = [] y = [] y2 = [] (fig, ax) = plt.subplots(1, 1) yloc = 0.9 ax.text(0.7, 0.94, 'Avg:', transform=ax.transAxes, color='b') ax.text(0.85, 0.94, '%s:' % (highlightyear,), transform=ax.transAxes, color='r') for level in LEVELS[varname]: x.append(level) y.append(len(df2[df2[varname] >= level]) / years) y2.append(len(df[np.logical_and(df[varname] >= level, df['year'] == highlightyear)])) if level % 2 == 0: ax.text(0.6, yloc, '%s' % (level,), transform=ax.transAxes) ax.text(0.7, yloc, '%.1f' % (y[-1],), transform=ax.transAxes, color='b') ax.text(0.85, yloc, '%.0f' % (y2[-1],), transform=ax.transAxes, color='r') yloc -= 0.04 for x0, y0, y02 in zip(x, y, y2): c = 'r' if y02 > y0 else 'b' ax.plot([x0, x0], [y0, y02], color=c) rdf = pd.DataFrame({'level': x, 'avg': y, 'd%s' % (highlightyear,): y2}) x = np.array(x, dtype=np.float64) ax.scatter(x, y, color='b', label='Avg') ax.scatter(x, y2, color='r', label="%s" % (highlightyear,)) ax.grid(True) ymax = int(max([max(y), max(y2)])) ax.set_xlim(x[0] - 0.5, x[-1] + 0.5) dy = 24 * (int(ymax / 240) + 1) ax.set_yticks(range(0, ymax, dy)) ax.set_ylim(-0.5, ymax + 5) ax2 = ax.twinx() ax2.set_ylim(-0.5, ymax + 5) ax2.set_yticks(range(0, ymax, dy)) ax2.set_yticklabels(["%.0f" % (s,) for s in np.arange(0, ymax, dy) / 24]) ax2.set_ylabel("Expressed in 24 Hour Days") ax.set_ylabel("Hours Per Year") ax.set_xlabel("%s $^\circ$F" % (VDICT[varname],)) title = 'till %s' % (datetime.date.today().strftime("%-d %b"),) title = "Entire Year" if ytd == 'no' else title ax.set_title(("[%s] %s %s-%s\n" "%s Histogram (%s)" ) % (station, nt.sts[station]['name'], minyear, datetime.date.today().year, title2, title)) ax.legend(loc=(0.2, 0.8), scatterpoints=1) return fig, rdf
def plotter(fdict): """ Go """ import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt ASOS = psycopg2.connect(database='asos', host='iemdb', user='******') cursor = ASOS.cursor(cursor_factory=psycopg2.extras.DictCursor) station = fdict.get('zstation', 'DSM') network = fdict.get('network', 'IA_ASOS') highlightyear = int(fdict.get('year', 2015)) nt = NetworkTable(network) cursor.execute( """ SELECT to_char(valid, 'YYYYmmddHH24') as d, avg(tmpf), avg(dwpf) from alldata WHERE station = %s and tmpf >= 80 and dwpf >= 30 and dwpf <= tmpf and valid > '1973-01-01' GROUP by d """, (station, )) rows = [] for row in cursor: t = temperature(row[1], 'F') d = temperature(row[2], 'F') h = pymet.heatindex(t, d) if h.value('F') >= t.value('F'): rows.append(dict(year=int(row[0][:4]), heatindex=h.value('F'))) minyear = max([1973, nt.sts[station]['archive_begin'].year]) maxyear = datetime.date.today().year years = float((maxyear - minyear) + 1) df = pd.DataFrame(rows) x = [] y = [] y2 = [] (fig, ax) = plt.subplots(1, 1) yloc = 0.9 ax.text(0.7, 0.94, 'Avg:', transform=ax.transAxes, color='b') ax.text(0.85, 0.94, '%s:' % (highlightyear, ), transform=ax.transAxes, color='r') for level in range(90, 121): x.append(level) y.append(len(df[df['heatindex'] >= level]) / years) y2.append( len(df[np.logical_and(df['heatindex'] >= level, df['year'] == highlightyear)])) if level % 2 == 0: ax.text(0.6, yloc, '%s' % (level, ), transform=ax.transAxes) ax.text(0.7, yloc, '%.1f' % (y[-1], ), transform=ax.transAxes, color='b') ax.text(0.85, yloc, '%.0f' % (y2[-1], ), transform=ax.transAxes, color='r') yloc -= 0.04 x = np.array(x, dtype=np.float64) ax.scatter(x, y, color='b', label='Avg') ax.scatter(x, y2, color='r', label="%s" % (highlightyear, )) ax.grid(True) ax.set_ylim(-0.5, int(max(y)) + 5) ax.set_xlim(89.5, 120.5) ax.set_yticks(range(0, int(max(y)), 24)) ax.set_ylabel("Hours Per Year") ax.set_xlabel("Heat Index Temp $^\circ$F") ax.set_title(("[%s] %s %s-%s\n" "Heat Index (when accretive to air temp) Histogram") % (station, nt.sts[station]['name'], minyear, datetime.date.today().year)) ax.legend(loc=(0.2, 0.8), scatterpoints=1) return fig, df