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 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]
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)
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 test_windchill(): """Wind Chill Conversion""" temp = datatypes.temperature(0, "F") sknt = datatypes.speed(30, "MPH") val = meteorology.windchill(temp, sknt).value("F") assert abs(val - -24.50) < 0.01
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)
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)
def test_windchill(self): """Wind Chill Conversion""" temp = datatypes.temperature(0, 'F') sknt = datatypes.speed(30, 'MPH') val = meteorology.windchill(temp, sknt).value('F') self.assertAlmostEquals(val, -24.50, 2)
def test_windchill(): """Wind Chill Conversion""" temp = datatypes.temperature(0, 'F') sknt = datatypes.speed(30, 'MPH') val = meteorology.windchill(temp, sknt).value('F') assert abs(val - -24.50) < 0.01