示例#1
0
def get(station):
    cursor.execute(
        """
      SELECT valid, sknt, tmpf, dwpf from alldata where station = %s 
      and tmpf is not null and dwpf is not null 
      and valid > '1971-01-01' ORDER by valid ASC 
    """,
        (station,),
    )

    hits = {}
    running = False
    startr = None
    for row in cursor:
        relh = met.relh(temperature(row[2], "F"), temperature(row[3], "F")).value("%")
        if relh > 25 or row[1] < (25.0 / 1.15):
            if running:
                delta = (row[0] - startr).seconds
                if delta >= 60 * 60 * 1:
                    # print station, delta, row
                    hits[row[0].strftime("%Y%m%d")] = 1
            running = False
        else:
            running = True
            startr = row[0]

    return len(hits.keys())
示例#2
0
    def test_relh(self):
        """ Simple check of bad units in temperature """
        tmp = datatypes.temperature(24, 'C')
        dwp = datatypes.temperature(24, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertEquals(100.0, relh.value("%"))

        tmp = datatypes.temperature(32, 'C')
        dwp = datatypes.temperature(10, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertAlmostEquals(25.79, relh.value("%"), 2)

        tmp = datatypes.temperature(32, 'C')
        dwp = datatypes.temperature(15, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertAlmostEquals(35.81, relh.value("%"), 2)

        tmp = datatypes.temperature(5, 'C')
        dwp = datatypes.temperature(4, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertAlmostEquals(93.24, relh.value("%"), 2)
示例#3
0
def test_relh():
    """ Simple check of bad units in temperature """
    tmp = datatypes.temperature(24, 'C')
    dwp = datatypes.temperature(24, 'C')
    relh = meteorology.relh(tmp, dwp)
    assert relh.value("%") == 100.

    tmp = datatypes.temperature(32, 'C')
    dwp = datatypes.temperature(10, 'C')
    relh = meteorology.relh(tmp, dwp)
    assert abs(25.79 - relh.value("%")) < 0.01

    tmp = datatypes.temperature(32, 'C')
    dwp = datatypes.temperature(15, 'C')
    relh = meteorology.relh(tmp, dwp)
    assert abs(35.81 - relh.value("%")) < 0.01

    tmp = datatypes.temperature(5, 'C')
    dwp = datatypes.temperature(4, 'C')
    relh = meteorology.relh(tmp, dwp)
    assert abs(93.24 - relh.value("%")) < 0.01
示例#4
0
def test_relh():
    """ Simple check of bad units in temperature """
    tmp = datatypes.temperature(24, "C")
    dwp = datatypes.temperature(24, "C")
    relh = meteorology.relh(tmp, dwp)
    assert relh.value("%") == 100.0

    tmp = datatypes.temperature(32, "C")
    dwp = datatypes.temperature(10, "C")
    relh = meteorology.relh(tmp, dwp)
    assert abs(25.79 - relh.value("%")) < 0.01

    tmp = datatypes.temperature(32, "C")
    dwp = datatypes.temperature(15, "C")
    relh = meteorology.relh(tmp, dwp)
    assert abs(35.81 - relh.value("%")) < 0.01

    tmp = datatypes.temperature(5, "C")
    dwp = datatypes.temperature(4, "C")
    relh = meteorology.relh(tmp, dwp)
    assert abs(93.24 - relh.value("%")) < 0.01
示例#5
0
    def test_relh(self):
        """ Simple check of bad units in temperature """
        tmp = datatypes.temperature(24, 'C')
        dwp = datatypes.temperature(24, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertEquals(100.0, relh.value("%"))

        tmp = datatypes.temperature(32, 'C')
        dwp = datatypes.temperature(10, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertAlmostEquals(25.79, relh.value("%"), 2)
        
        tmp = datatypes.temperature(32, 'C')
        dwp = datatypes.temperature(15, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertAlmostEquals(35.81, relh.value("%"), 2)
        
        tmp = datatypes.temperature(5, 'C')
        dwp = datatypes.temperature(4, 'C')
        relh = meteorology.relh(tmp, dwp)
        self.assertAlmostEquals(93.24, relh.value("%"), 2)
示例#6
0
def computeOthers(d):
    r = {}
    # Need something to compute other values needed for output
    for sid in d.keys():
        ob = d[sid]
        ob["ticks"] = calendar.timegm(ob['utc_valid'].timetuple())
        if ob['sknt'] is not None:
            ob["sped"] = ob["sknt"] * 1.17
        if ob.get('tmpf') is not None and ob.get('dwpf') is not None:
            tmpf = temperature(ob['tmpf'], 'F')
            dwpf = temperature(ob['dwpf'], 'F')
            ob["relh"] = meteorology.relh(tmpf, dwpf).value('%')
        else:
            ob['relh'] = None
        if ob['relh'] == 'M':
            ob['relh'] = None

        if (ob.get('tmpf') is not None and ob.get('dwpf') is not None and
                ob.get('sped') is not None):
            ob['feel'] = meteorology.mcalc_feelslike(
                masked_array([ob['tmpf'], ], units('degF'), mask=[False, ]),
                masked_array([ob['dwpf'], ],  units('degF'), mask=[False, ]),
                masked_array([ob['sped'], ], units('mile per hour'),
                             mask=[False, ])
                ).to(units('degF')).magnitude[0]
        else:
            ob['feel'] = None
        if ob['feel'] == 'M':
            ob['feel'] = None

        ob["altiTend"] = 'S'
        ob["drctTxt"] = util.drct2text(ob["drct"])
        if ob["max_drct"] is None:
            ob["max_drct"] = 0
        ob["max_drctTxt"] = util.drct2text(ob["max_drct"])
        ob["20gu"] = 0
        if ob['gust'] is not None:
            ob["gmph"] = ob["gust"] * 1.17
        if ob['max_gust'] is not None:
            ob["max_sped"] = ob["max_gust"] * 1.17
        else:
            ob['max_sped'] = 0
        ob['pday'] = 0 if ob['pday'] is None else ob['pday']
        ob['pmonth'] = 0 if ob['pmonth'] is None else ob['pmonth']
        ob["gtim"] = "0000"
        ob["gtim2"] = "12:00 AM"
        if ob["max_gust_ts"] is not None and ob["max_gust_ts"] != "null":
            ob["gtim"] = ob["max_gust_ts"].strftime("%H%M")
            ob["gtim2"] = ob["max_gust_ts"].strftime("%-I:%M %p")
        r[sid] = ob
    return r
示例#7
0
def computeOthers(d):
    r = {}
    # Need something to compute other values needed for output
    for sid in d.keys():
        ob = d[sid]
        ob["ticks"] = calendar.timegm(ob['utc_valid'].timetuple())
        if ob['sknt'] is not None:
            ob["sped"] = ob["sknt"] * 1.17
        if ob.get('tmpf') is not None and ob.get('dwpf') is not None:
            tmpf = temperature(ob['tmpf'], 'F')
            dwpf = temperature(ob['dwpf'], 'F')
            ob["relh"] = meteorology.relh(tmpf, dwpf).value('%')
        else:
            ob['relh'] = None
        if ob['relh'] == 'M':
            ob['relh'] = None

        if (ob.get('tmpf') is not None and ob.get('dwpf') is not None and
                ob.get('sped') is not None):
            tmpf = temperature(ob['tmpf'], 'F')
            dwpf = temperature(ob['dwpf'], 'F')
            sknt = speed(ob['sped'], 'MPH')
            ob["feel"] = meteorology.feelslike(tmpf, dwpf, sknt).value("F")
        else:
            ob['feel'] = None
        if ob['feel'] == 'M':
            ob['feel'] = None

        ob["altiTend"] = 'S'
        ob["drctTxt"] = util.drct2text(ob["drct"])
        if ob["max_drct"] is None:
            ob["max_drct"] = 0
        ob["max_drctTxt"] = util.drct2text(ob["max_drct"])
        ob["20gu"] = 0
        if ob['gust'] is not None:
            ob["gmph"] = ob["gust"] * 1.17
        if ob['max_gust'] is not None:
            ob["max_sped"] = ob["max_gust"] * 1.17
        else:
            ob['max_sped'] = 0
        ob['pday'] = 0 if ob['pday'] is None else ob['pday']
        ob['pmonth'] = 0 if ob['pmonth'] is None else ob['pmonth']
        ob["gtim"] = "0000"
        ob["gtim2"] = "12:00 AM"
        if ob["max_gust_ts"] is not None and ob["max_gust_ts"] != "null":
            ob["gtim"] = ob["max_gust_ts"].strftime("%H%M")
            ob["gtim2"] = ob["max_gust_ts"].strftime("%-I:%M %p")
        r[sid] = ob
    return r
示例#8
0
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(nt.sts[rs[i]['station']]['lat'])
            lons.append(nt.sts[rs[i]['station']]['lon'])
            vals.append((meteorology.relh(temperature(rs[i]['max_tmpf'], 'F'),
                                          temperature(rs[i]['max_dwpf'], 'F'))
                         ).value('%'))
    if len(vals) < 4:
        print "No RELH data at all for time: %s" % (ts,)
        return
    grid = griddata((lons, lats), np.array(vals), (XI, YI), method='nearest')
    offset = int((ts - BASE).total_seconds() / 3600.)
    if grid is not None:
        nc.variables['relh'][offset, :, :] = np.where(grid < 12., 12., grid)
    else:
        print "RELH gridding failed, len vals %s" % (len(vals),)
示例#9
0
def main():
    """ Go main Go """
    check_load()
    form = cgi.FieldStorage()
    try:
        tzinfo = pytz.timezone(form.getfirst("tz", "Etc/UTC"))
    except pytz.exceptions.UnknownTimeZoneError as exp:
        sys.stdout.write("Content-type: text/plain\n\n")
        sys.stdout.write("Invalid Timezone (tz) provided")
        sys.stderr.write("asos.py invalid tz: %s\n" % (exp, ))
        sys.exit()
    pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')
    acursor = pgconn.cursor('mystream',
                            cursor_factory=psycopg2.extras.DictCursor)

    # Save direct to disk or view in browser
    direct = True if form.getfirst('direct', 'no') == 'yes' else False
    report_type = form.getlist('report_type')
    stations = get_stations(form)
    if direct:
        sys.stdout.write('Content-type: application/octet-stream\n')
        fn = "%s.txt" % (stations[0],)
        if len(stations) > 1:
            fn = "asos.txt"
        sys.stdout.write('Content-Disposition: attachment; filename=%s\n\n' % (
                         fn,))
    else:
        sys.stdout.write("Content-type: text/plain \n\n")

    dbstations = stations
    if len(dbstations) == 1:
        dbstations.append('XYZXYZ')

    dataVars = form.getlist("data")
    sts, ets = get_time_bounds(form, tzinfo)

    delim = form.getfirst("format", "onlycomma")

    if "all" in dataVars:
        queryCols = ("tmpf, dwpf, relh, drct, sknt, p01i, alti, mslp, "
                     "vsby, gust, skyc1, skyc2, skyc3, skyc4, skyl1, "
                     "skyl2, skyl3, skyl4, presentwx, metar")
        outCols = ['tmpf', 'dwpf', 'relh', 'drct', 'sknt', 'p01i', 'alti',
                   'mslp', 'vsby', 'gust', 'skyc1', 'skyc2', 'skyc3',
                   'skyc4', 'skyl1', 'skyl2', 'skyl3', 'skyl4',
                   'presentwx', 'metar']
    else:
        for _colname in ['station', 'valid']:
            if _colname in dataVars:
                dataVars.remove(_colname)
        dataVars = tuple(dataVars)
        outCols = dataVars
        dataVars = str(dataVars)[1:-2]
        queryCols = re.sub("'", " ", dataVars)

    if delim in ["tdf", "onlytdf"]:
        rD = "\t"
        queryCols = re.sub(",", "\t", queryCols)
    else:
        rD = ","

    gtxt = {}
    gisextra = False
    if form.getfirst("latlon", "no") == "yes":
        gisextra = True
        MESOSITE = psycopg2.connect(database='mesosite', host='iemdb',
                                    user='******')
        mcursor = MESOSITE.cursor()
        mcursor.execute("""SELECT id, ST_x(geom) as lon, ST_y(geom) as lat
             from stations WHERE id in %s
             and (network ~* 'AWOS' or network ~* 'ASOS')
        """, (tuple(dbstations),))
        for row in mcursor:
            gtxt[row[0]] = "%.4f%s%.4f%s" % (row[1], rD, row[2], rD)

    rlimiter = ""
    if len(report_type) == 1:
        rlimiter = " and report_type = %s" % (int(report_type[0]),)
    elif len(report_type) > 1:
        rlimiter = (" and report_type in %s"
                    ) % (tuple([int(a) for a in report_type]), )
    acursor.execute("""SELECT * from alldata
      WHERE valid >= %s and valid < %s and station in %s """+rlimiter+"""
      ORDER by valid ASC""", (sts, ets, tuple(dbstations)))

    if delim not in ['onlytdf', 'onlycomma']:
        sys.stdout.write("#DEBUG: Format Typ    -> %s\n" % (delim,))
        sys.stdout.write("#DEBUG: Time Period   -> %s %s\n" % (sts, ets))
        sys.stdout.write("#DEBUG: Time Zone     -> %s\n" % (tzinfo,))
        sys.stdout.write(("#DEBUG: Data Contact   -> daryl herzmann "
                          "[email protected] 515-294-5978\n"))
        sys.stdout.write("#DEBUG: Entries Found -> %s\n" % (acursor.rowcount,))
    sys.stdout.write("station"+rD+"valid"+rD)
    if gisextra:
        sys.stdout.write("lon"+rD+"lat"+rD)
    sys.stdout.write(queryCols+"\n")

    for row in acursor:
        sys.stdout.write(row["station"] + rD)
        sys.stdout.write(
            (row["valid"].astimezone(tzinfo)).strftime("%Y-%m-%d %H:%M") + rD)
        if gisextra:
            sys.stdout.write(gtxt.get(row['station'], "M%sM%s" % (rD, rD)))
        r = []
        for data1 in outCols:
            if data1 == 'relh':
                if row['tmpf'] is not None and row['dwpf'] is not None:
                    tmpf = temperature(row['tmpf'], 'F')
                    dwpf = temperature(row['dwpf'], 'F')
                    val = meteorology.relh(tmpf, dwpf)
                    r.append("%.2f" % (val.value("%"),))
                else:
                    r.append("M")
            elif data1 == 'sped':
                if row['sknt'] >= 0:
                    r.append("%.1f" % (speed(row['sknt'],
                                             'KT').value('MPH'), ))
                else:
                    r.append("M")
            elif data1 == 'gust_mph':
                if row['gust'] >= 0:
                    r.append("%.1f" % (speed(row['gust'],
                                             'KT').value('MPH'), ))
                else:
                    r.append("M")
            elif data1 == 'p01m':
                if row['p01i'] >= 0:
                    r.append("%.2f" % (row['p01i'] * 25.4, ))
                else:
                    r.append("M")
            elif data1 == 'tmpc':
                if row['tmpf'] is not None:
                    val = temperature(row['tmpf'], 'F').value('C')
                    r.append("%.2f" % (val, ))
                else:
                    r.append("M")
            elif data1 == 'dwpc':
                if row['dwpf'] is not None:
                    val = temperature(row['dwpf'], 'F').value('C')
                    r.append("%.2f" % (val, ))
                else:
                    r.append("M")
            elif data1 == 'presentwx':
                if row['presentwx'] is not None:
                    r.append("%s" % (row['presentwx'].replace(",", " "), ))
                else:
                    r.append("M")
            elif data1 in ["metar", "skyc1", "skyc2", "skyc3", "skyc4"]:
                if row[data1] is None:
                    r.append("M")
                else:
                    r.append("%s" % (row[data1].replace(",", " "), ))
            elif (row.get(data1) is None or row[data1] <= -99.0 or
                  row[data1] == "M"):
                r.append("M")
            else:
                r.append("%2.2f" % (row[data1], ))
        sys.stdout.write("%s\n" % (rD.join(r),))
示例#10
0
from pyiem import meteorology

dbconn = psycopg2.connect(database='asos', host='iemdb', user='******')
cursor = dbconn.cursor()

otmpf = []
odwpf = []
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
示例#11
0
for sid in indices:
    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']
示例#12
0
from pyiem.datatypes import temperature, speed, distance
from pyiem.meteorology import relh

pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')

# air temperature, RH, Radiation, WS, and precipitation
df = read_sql("""SELECT valid, tmpf, dwpf, sknt, p01i,
  extract(month from valid) as month,
  extract(hour from valid + '10 minutes'::interval) as hour,
  extract(day from valid + '10 minutes'::interval) as day from alldata
  WHERE station = 'DSM' and extract(month from valid) in (10, 11)
  and extract(minute from valid) = 54
  and valid > '1986-01-01'
  """, pgconn, index_col=None)

df['relh'] = relh(temperature(df['tmpf'], 'F'),
                  temperature(df['dwpf'], 'F')).value('%')

gdf = df.groupby(by=['month', 'day', 'hour']).mean()

pgconn = psycopg2.connect(database='isuag', host='iemdb', user='******')

# air temperature, RH, Radiation, WS, and precipitation
df2 = read_sql("""SELECT
  extract(month from valid) as month,
  extract(hour from valid + '10 minutes'::interval) as hour,
  extract(day from valid + '10 minutes'::interval) as day,
  c800 from hourly
  WHERE station = 'A130209' and extract(month from valid) in (10, 11)
  """, pgconn, index_col=None)

gdf2 = df2.groupby(by=['month', 'day', 'hour']).mean()
示例#13
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['zstation']
    network = ctx['network']
    nt = NetworkTable(network)
    year = ctx['year']
    varname = ctx['var']
    _ = PDICT[varname]

    df = read_sql("""
        SELECT extract(year from valid) as year,
        extract(doy from valid) as doy, tmpf, dwpf from alldata
        where station = %s and dwpf > -50 and dwpf < 90 and
        tmpf > -50 and tmpf < 120 and valid > '1950-01-01'
        and report_type = 2
    """, pgconn, params=(station,), index_col=None)
    df['relh'] = relh(temperature(df['tmpf'].values, 'F'),
                      temperature(df['dwpf'].values, 'F')).value('%')
    df['tmpc'] = temperature(df['tmpf'].values, 'F').value('C')
    df['svp'] = 0.611 * np.exp(17.502 * df['tmpc'].values /
                               (240.97 + df['tmpc'].values))
    df['vpd'] = (1. - (df['relh'] / 100.)) * df['svp']
    df['mixing_ratio'] = mixing_ratio(
                            temperature(df['dwpf'].values, 'F')).value('KG/KG')

    dailymeans = df[['year', 'doy', varname]].groupby(['year', 'doy']).mean()
    dailymeans = dailymeans.reset_index()

    df2 = dailymeans[['doy', varname]].groupby('doy').describe()

    dyear = df[df['year'] == year]
    df3 = dyear[['doy', varname]].groupby('doy').describe()
    df3[(varname, 'diff')] = df3[(varname, 'mean')] - df2[(varname, 'mean')]

    (fig, ax) = plt.subplots(2, 1, figsize=(8, 6))
    multiplier = 1000. if varname == 'mixing_ratio' else 10.

    ax[0].fill_between(df2[(varname, 'min')].index.values,
                       df2[(varname, 'min')].values * multiplier,
                       df2[(varname, 'max')].values * multiplier,
                       color='gray')

    ax[0].plot(df2[(varname, 'mean')].index.values,
               df2[(varname, 'mean')].values * multiplier,
               label="Climatology")
    ax[0].plot(df3[(varname, 'mean')].index.values,
               df3[(varname, 'mean')].values * multiplier,
               color='r', label="%s" % (year, ))

    ax[0].set_title(("%s [%s]\nDaily Mean Surface %s"
                     ) % (station, nt.sts[station]['name'],
                          PDICT[varname]))
    lbl = ("Mixing Ratio ($g/kg$)" if varname == 'mixing_ratio'
           else PDICT[varname])
    ax[0].set_ylabel(lbl)
    ax[0].set_xlim(0, 366)
    ax[0].set_ylim(bottom=0)
    ax[0].set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
                      365))
    ax[0].set_xticklabels(calendar.month_abbr[1:])
    ax[0].grid(True)
    ax[0].legend(loc=2, fontsize=10)

    cabove = 'b' if varname == 'mixing_ratio' else 'r'
    cbelow = 'r' if cabove == 'b' else 'b'
    rects = ax[1].bar(df3[(varname, 'diff')].index.values,
                      df3[(varname, 'diff')].values * multiplier,
                      facecolor=cabove, edgecolor=cabove)
    for rect in rects:
        if rect.get_height() < 0.:
            rect.set_facecolor(cbelow)
            rect.set_edgecolor(cbelow)

    units = '$g/kg$' if varname == 'mixing_ratio' else 'hPa'
    ax[1].set_ylabel("%.0f Departure (%s)" % (year, units))
    ax[1].set_xlim(0, 366)
    ax[1].set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
                      365))
    ax[1].set_xticklabels(calendar.month_abbr[1:])
    ax[1].grid(True)
    return fig, df3
示例#14
0
def main(argv):
    """Go Main Go"""
    network = argv[1]
    wxcfn = argv[2]

    utc = datetime.datetime.utcnow()
    utc = utc.replace(tzinfo=pytz.timezone("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 = netCDF4.Dataset(fn, 'r')

    for i, provider in enumerate(nc.variables["dataProvider"][:]):
        if provider.tostring().replace('\x00', '') != network:
            continue
        sid = nc.variables["stationId"][i].tostring().replace('\x00', '')
        # We have an ob!
        ticks = nc.variables["observationTime"][i]
        ts = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=ticks)
        ts = ts.replace(tzinfo=pytz.timezone("UTC"))

        if ts > indices.get(sid, {'ts': BOGUS})['ts']:
            indices[sid] = {'ts': ts, 'idx': i}

    for sid in indices:
        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, 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)
示例#15
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['zstation']
    network = ctx['network']
    month = ctx['month']

    nt = NetworkTable(network)

    if month == 'all':
        months = range(1, 13)
    elif month == 'fall':
        months = [9, 10, 11]
    elif month == 'winter':
        months = [12, 1, 2]
    elif month == 'spring':
        months = [3, 4, 5]
    elif month == 'summer':
        months = [6, 7, 8]
    else:
        ts = datetime.datetime.strptime("2000-"+month+"-01", '%Y-%b-%d')
        # make sure it is length two for the trick below in SQL
        months = [ts.month, 999]

    cursor.execute("""
        SELECT tmpf::int as t, dwpf from alldata where station = %s
        and tmpf is not null and dwpf is not null and dwpf <= tmpf
        and tmpf >= 0 and tmpf <= 140
        and extract(month from valid) in %s
        """, (station,  tuple(months)))
    sums = np.zeros((140,), 'f')
    counts = np.zeros((140,), 'f')
    for row in cursor:
        r = mixing_ratio(temperature(row[1], 'F')).value('KG/KG')
        sums[row[0]] += r
        counts[row[0]] += 1

    rows = []
    for i in range(140):
        if counts[i] < 3:
            continue
        r = sums[i] / float(counts[i])
        d = dewpoint_from_pq(pressure(1000, 'MB'),
                             mixingratio(r, 'KG/KG')
                             ).value('F')
        rh = relh(temperature(i, 'F'), temperature(d, 'F')).value('%')
        rows.append(dict(tmpf=i, dwpf=d, rh=rh))

    df = pd.DataFrame(rows)
    tmpf = df['tmpf']
    dwpf = df['dwpf']
    rh = df['rh']

    (fig, ax) = plt.subplots(1, 1, figsize=(8, 6))
    ax.bar(tmpf-0.5, dwpf, ec='green', fc='green', width=1)
    ax.grid(True, zorder=11)
    ax.set_title(("%s [%s]\nAverage Dew Point by Air Temperature (month=%s) "
                  "(%s-%s)\n"
                  "(must have 3+ hourly observations at the given temperature)"
                  ) % (nt.sts[station]['name'], station, month.upper(),
                       nt.sts[station]['archive_begin'].year,
                       datetime.datetime.now().year), size=10)

    ax.plot([0, 140], [0, 140], color='b')
    ax.set_ylabel("Dew Point [F]")
    y2 = ax.twinx()
    y2.plot(tmpf, rh, color='k')
    y2.set_ylabel("Relative Humidity [%] (black line)")
    y2.set_yticks([0, 5, 10, 25, 50, 75, 90, 95, 100])
    y2.set_ylim(0, 100)
    ax.set_ylim(0, max(tmpf)+2)
    ax.set_xlim(0, max(tmpf)+2)
    ax.set_xlabel("Air Temperature $^\circ$F")

    return fig, df
示例#16
0
def do(ts):
    """Process this date timestamp"""
    asos = psycopg2.connect(database='asos', host='iemdb', user='******')
    cursor = asos.cursor()
    iemaccess = psycopg2.connect(database='iem', host='iemdb')
    icursor = iemaccess.cursor()
    cursor.execute("""
    select station, network, iemid, drct, sknt, valid at time zone tzname,
    tmpf, dwpf from
    alldata d JOIN stations t on (t.id = d.station)
    where (network ~* 'ASOS' or network = 'AWOS')
    and valid between %s and %s and t.tzname is not null
    ORDER by valid ASC
    """, (ts - datetime.timedelta(days=2), ts + datetime.timedelta(days=2)))
    wdata = dict()
    rhdata = dict()
    for row in cursor:
        if row[5].strftime("%m%d") != ts.strftime("%m%d"):
            continue
        station = "%s|%s|%s" % (row[0], row[1], row[2])
        if row[6] is not None and row[7] is not None:
            tmpf = temperature(row[6], 'F')
            dwpf = temperature(row[7], 'F')
            rh = meteorology.relh(tmpf, dwpf)
            if station not in rhdata:
                rhdata[station] = dict(valid=[], rh=[])
            rhdata[station]['valid'].append(row[5])
            rhdata[station]['rh'].append(rh.value('%'))
        if row[4] is not None and row[3] is not None:
            sknt = speed(row[4], 'KT')
            drct = direction(row[3], 'DEG')
            (u, v) = meteorology.uv(sknt, drct)
            if station not in wdata:
                wdata[station] = {'valid': [], 'sknt': [], 'u': [], 'v': []}
            wdata[station]['valid'].append(row[5])
            wdata[station]['sknt'].append(row[4])
            wdata[station]['u'].append(u.value('KT'))
            wdata[station]['v'].append(v.value('KT'))

    table = "summary_%s" % (ts.year,)
    for stid in rhdata:
        # Not enough data
        if len(rhdata[stid]['valid']) < 6:
            continue
        station, network, iemid = stid.split("|")
        now = datetime.datetime(ts.year, ts.month, ts.day)
        runningrh = 0
        runningtime = 0
        for i, valid in enumerate(rhdata[stid]['valid']):
            delta = (valid - now).seconds
            runningtime += delta
            runningrh += (delta * rhdata[stid]['rh'][i])
            now = valid

        if runningtime == 0:
            print(("compute_daily %s has time domain %s %s"
                   ) % (stid, rhdata[stid]['valid'][0],
                        rhdata[stid]['valid'][-1]))
            continue
        avg_rh = clean_rh(runningrh / runningtime)
        min_rh = clean_rh(min(rhdata[stid]['rh']))
        max_rh = clean_rh(max(rhdata[stid]['rh']))

        def do_update():
            icursor.execute("""UPDATE """ + table + """
            SET avg_rh = %s, min_rh = %s, max_rh = %s WHERE
            iemid = %s and day = %s""", (avg_rh, min_rh, max_rh, iemid, ts))
        do_update()
        if icursor.rowcount == 0:
            print(('compute_daily Adding %s for %s %s %s'
                   ) % (table, station, network, ts))
            icursor.execute("""INSERT into """ + table + """
            (iemid, day) values (%s, %s)""", (iemid, ts))
            do_update()

    for stid in wdata:
        # Not enough data
        if len(wdata[stid]['valid']) < 6:
            continue
        station, network, iemid = stid.split("|")
        now = datetime.datetime(ts.year, ts.month, ts.day)
        runningsknt = 0
        runningtime = 0
        runningu = 0
        runningv = 0
        for i, valid in enumerate(wdata[stid]['valid']):
            delta = (valid - now).seconds
            runningtime += delta
            runningsknt += (delta * wdata[stid]['sknt'][i])
            runningu += (delta * wdata[stid]['u'][i])
            runningv += (delta * wdata[stid]['v'][i])
            now = valid

        if runningtime == 0:
            print(("compute_daily %s has time domain %s %s"
                   ) % (stid, wdata[stid]['valid'][0],
                        wdata[stid]['valid'][-1]))
            continue
        sknt = runningsknt / runningtime
        u = speed(runningu / runningtime, 'KT')
        v = speed(runningv / runningtime, 'KT')
        drct = meteorology.drct(u, v).value("DEG")

        def do_update():
            icursor.execute("""UPDATE """ + table + """
            SET avg_sknt = %s, vector_avg_drct = %s WHERE
            iemid = %s and day = %s""", (sknt, drct, iemid, ts))
        do_update()
        if icursor.rowcount == 0:
            print(('compute_daily Adding %s for %s %s %s'
                   ) % (table, station, network, ts))
            icursor.execute("""INSERT into """ + table + """
            (iemid, day) values (%s, %s)""", (iemid, ts))
            do_update()

    icursor.close()
    iemaccess.commit()
    iemaccess.close()
示例#17
0
def main():
    """Do Something"""
    sys.stdout.write("Content-type: text/plain \n\n")
    form = cgi.FormContent()
    metadata = {}
    mcursor.execute(
        """SELECT id, ST_x(geom) as lon, ST_y(geom) as lat
         from stations WHERE network ~* 'ASOS' or network = 'AWOS'
         and country = 'US' """
    )
    for row in mcursor:
        metadata[row[0]] = {"lon": "%.4f" % (row[1],), "lat": "%.4f" % (row[2],)}

    year = int(form["year"][0])
    month = int(form["month"][0])
    day = int(form["day"][0])
    hour = int(form["hour"][0])
    ts = mx.DateTime.DateTime(year, month, day, hour)

    queryCols = (
        "tmpf, dwpf, relh, drct, sknt, p01i, alti, mslp, vsby, "
        "gust, skyc1, skyc2, skyc3, skyc4, skyl1, skyl2, skyl3, "
        "skyl4, metar"
    )
    outCols = [
        "tmpf",
        "dwpf",
        "relh",
        "drct",
        "sknt",
        "p01i",
        "alti",
        "mslp",
        "vsby",
        "gust",
        "skyc1",
        "skyc2",
        "skyc3",
        "skyc4",
        "skyl1",
        "skyl2",
        "skyl3",
        "skyl4",
        "metar",
    ]

    fmt = "%Y-%m-%d %H:%M"
    queryStr = """SELECT * from t%s
        WHERE valid >= '%s' and valid < '%s'
        ORDER by station ASC
        """ % (
        ts.year,
        (ts - mx.DateTime.RelativeDateTime(minutes=10)).strftime(fmt),
        (ts + mx.DateTime.RelativeDateTime(minutes=1)).strftime(fmt),
    )

    rD = ","

    acursor.execute("SET TIME ZONE 'UTC'")
    acursor.execute(queryStr)

    print "station" + rD + "valid (UTC timezone)" + rD + "lon" + rD + "lat" + rD,
    print queryCols

    for row in acursor:
        if row["station"] not in metadata:
            continue
        sys.stdout.write(row["station"] + rD)
        sys.stdout.write(row["valid"].strftime("%Y-%m-%d %H:%M") + rD)
        sys.stdout.write(metadata[row["station"]]["lon"] + rD)
        sys.stdout.write(metadata[row["station"]]["lat"] + rD)
        for data1 in outCols:
            if data1 == "relh":
                temp = temperature(row["tmpf"], "F")
                dewp = temperature(row["dwpf"], "F")
                val = meteorology.relh(temp, dewp).value("%")
                if val != "M":
                    sys.stdout.write("%.2f%s" % (val, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 == "p01m":
                if row["p01i"] >= 0:
                    sys.stdout.write("%.2f%s" % (row["p01i"] * 25.4, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 == "tmpc":
                if row["tmpf"] is not None:
                    val = temperature(row["tmpf"], "F").value("C")
                    sys.stdout.write("%.2f%s" % (val, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 == "dwpc":
                if row["dwpf"] is not None:
                    val = temperature(row["dwpf"], "F").value("C")
                    sys.stdout.write("%.2f%s" % (val, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 in ["metar", "skyc1", "skyc2", "skyc3", "skyc4"]:
                sys.stdout.write("%s%s" % (row[data1], rD))
            elif row[data1] is None or row[data1] <= -99.0:
                sys.stdout.write("M%s" % (rD,))
            else:
                sys.stdout.write("%2.2f%s" % (row[data1], rD))
        print
示例#18
0
from pyiem import meteorology

dbconn = psycopg2.connect(database='asos', host='iemdb', user='******')
cursor = dbconn.cursor()

otmpf = []
odwpf = []
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
示例#19
0
文件: asos.py 项目: KayneWest/iem
def main():
    """ Go main Go """
    form = cgi.FieldStorage()
    try:
        tzinfo = pytz.timezone(form.getfirst("tz", "Etc/UTC"))
    except:
        sys.stdout.write("Invalid Timezone (tz) provided")
        sys.exit()

    # Save direct to disk or view in browser
    direct = True if form.getfirst('direct', 'no') == 'yes' else False
    stations = get_stations(form)
    if direct:
        sys.stdout.write('Content-type: application/octet-stream\n')
        fn = "%s.txt" % (stations[0],)
        if len(stations) > 1:
            fn = "asos.txt"
        sys.stdout.write('Content-Disposition: attachment; filename=%s\n\n' % (
                         fn,))
    else:
        sys.stdout.write("Content-type: text/plain \n\n")

    dbstations = stations
    if len(dbstations) == 1:
        dbstations.append('XYZXYZ')

    dataVars = form.getlist("data")
    sts, ets = get_time_bounds(form, tzinfo)

    delim = form.getfirst("format")

    if "all" in dataVars:
        queryCols = ("tmpf, dwpf, relh, drct, sknt, p01i, alti, mslp, "
                     "vsby, gust, skyc1, skyc2, skyc3, skyc4, skyl1, "
                     "skyl2, skyl3, skyl4, presentwx, metar")
        outCols = ['tmpf', 'dwpf', 'relh', 'drct', 'sknt', 'p01i', 'alti',
                   'mslp', 'vsby', 'gust', 'skyc1', 'skyc2', 'skyc3',
                   'skyc4', 'skyl1', 'skyl2', 'skyl3', 'skyl4',
                   'presentwx', 'metar']
    else:
        dataVars = tuple(dataVars)
        outCols = dataVars
        dataVars = str(dataVars)[1:-2]
        queryCols = re.sub("'", " ", dataVars)

    if delim == "tdf":
        rD = "\t"
        queryCols = re.sub("," , "\t", queryCols) 
    else:
        rD = ","

    gtxt = {}
    gisextra = False
    if form.getfirst("latlon", "no") == "yes": 
        gisextra = True
        mcursor.execute("""SELECT id, ST_x(geom) as lon, ST_y(geom) as lat 
             from stations WHERE id in %s
             and (network ~* 'AWOS' or network ~* 'ASOS')""", 
             (tuple(dbstations),))
        for row in mcursor:
            gtxt[row[0]] = "%.4f%s%.4f%s" % (row['lon'], rD, row['lat'], rD)

    acursor.execute("""SELECT * from alldata
      WHERE valid >= %s and valid < %s and station in %s
      ORDER by valid ASC""", (sts, ets, tuple(dbstations)))

    sys.stdout.write("#DEBUG: Format Typ    -> %s\n" % (delim,))
    sys.stdout.write("#DEBUG: Time Period   -> %s %s\n" % (sts, ets))
    sys.stdout.write("#DEBUG: Time Zone     -> %s\n" % (tzinfo,))
    sys.stdout.write(("#DEBUG: Data Contact   -> daryl herzmann "
                      "[email protected] 515-294-5978\n"))
    sys.stdout.write("#DEBUG: Entries Found -> %s\n" % (acursor.rowcount,))
    sys.stdout.write("station"+rD+"valid"+rD)
    if gisextra:
        sys.stdout.write("lon"+rD+"lat"+rD)
    sys.stdout.write(queryCols+"\n")

    for row in acursor:
        sys.stdout.write(row["station"] + rD)
        sys.stdout.write( 
            (row["valid"].astimezone(tzinfo)).strftime("%Y-%m-%d %H:%M") + rD )
        if gisextra:
            sys.stdout.write( gtxt.get(row['station'], "M%sM%s" % (rD, rD)) )
        r = []
        for data1 in outCols:
            if data1 == 'relh':
                if row['tmpf'] is not None and row['dwpf'] is not None:
                    tmpf = temperature(row['tmpf'], 'F')
                    dwpf = temperature(row['dwpf'], 'F')
                    val = meteorology.relh( tmpf, dwpf )
                    r.append("%.2f" % (val.value("%"),))
                else:
                    r.append("M")
            elif data1 == 'sped':
                if row['sknt'] >= 0:
                    r.append("%.1f" % (row['sknt'] * 1.14, ))
                else:
                    r.append("M")
            elif data1 == 'gust_mph':
                if row['gust'] >= 0:
                    r.append("%.1f" % (row['gust'] * 1.14, ))
                else:
                    r.append("M")
            elif data1 == 'p01m':
                if row['p01i'] >= 0:
                    r.append("%.2f" % (row['p01i'] * 25.4, ))
                else:
                    r.append("M")
            elif data1 == 'tmpc':
                if row['tmpf'] is not None:
                    val = temperature( row['tmpf'] , 'F').value('C')
                    r.append("%.2f" % (val, ))
                else:
                    r.append("M")
            elif data1 == 'dwpc':
                if row['dwpf'] is not None:
                    val = temperature( row['dwpf'], 'F').value('C')
                    r.append("%.2f" % (val, ))
                else:
                    r.append("M")
            elif data1 == 'presentwx':
                if row['presentwx'] is not None:
                    r.append("%s" % (row['presentwx'].replace(",", " "), ))
                else:
                    r.append("M")
            elif data1 in ["metar","skyc1","skyc2","skyc3","skyc4"]:
                if row[data1] is None:
                    r.append("M")
                else:
                    r.append("%s" % (row[data1], ))
            elif row[ data1 ] is None or row[ data1 ] <= -99.0 or row[ data1 ] == "M":
                r.append("M")
            else:
                r.append("%2.2f" % (row[ data1 ],))
        sys.stdout.write("%s\n" % (rD.join(r),))
示例#20
0
pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')

# air temperature, RH, Radiation, WS, and precipitation
df = read_sql("""SELECT valid, tmpf, dwpf, sknt, p01i,
  extract(month from valid) as month,
  extract(hour from valid + '10 minutes'::interval) as hour,
  extract(day from valid + '10 minutes'::interval) as day from alldata
  WHERE station = 'DSM' and extract(month from valid) in (10, 11)
  and extract(minute from valid) = 54
  and valid > '1986-01-01'
  """,
              pgconn,
              index_col=None)

df['relh'] = relh(temperature(df['tmpf'], 'F'), temperature(df['dwpf'],
                                                            'F')).value('%')

gdf = df.groupby(by=['month', 'day', 'hour']).mean()

pgconn = psycopg2.connect(database='isuag', host='iemdb', user='******')

# air temperature, RH, Radiation, WS, and precipitation
df2 = read_sql("""SELECT
  extract(month from valid) as month,
  extract(hour from valid + '10 minutes'::interval) as hour,
  extract(day from valid + '10 minutes'::interval) as day,
  c800 from hourly
  WHERE station = 'A130209' and extract(month from valid) in (10, 11)
  """,
               pgconn,
               index_col=None)
示例#21
0
for sid in indices:
    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']
示例#22
0
        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('%') )
            tmpf.append( row2[1] )
            if sky in ['Overcast', 'Broken']:
                clcount += 1

    cloudy = 'N'
    if clcount > 4:
        cloudy = 'Y'
    relh = np.array(relh)
    tmpf = np.array(tmpf)
    avgrelh = np.average(relh)
    avgtmpf = np.average(tmpf)
    climatetmpf = (climate[day.strftime("%m%d")]['high'] + 
                climate[day.strftime("%m%d")]['low']) / 2.0
    T = 'BELOW'
    if avgtmpf > climatetmpf:
示例#23
0
文件: p126.py 项目: akrherz/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')

    station = fdict.get('zstation', 'AMW')
    network = fdict.get('network', 'IA_ASOS')
    nt = NetworkTable(network)
    year = int(fdict.get('year', 2015))
    varname = fdict.get('var', 'mixing_ratio')
    _ = PDICT[varname]

    df = read_sql("""
        SELECT extract(year from valid) as year,
        extract(doy from valid) as doy, tmpf, dwpf from alldata
        where station = %s and dwpf > -50 and dwpf < 90 and
        tmpf > -50 and tmpf < 120 and valid > '1950-01-01'
        and report_type = 2
    """, pgconn, params=(station,), index_col=None)
    df['relh'] = relh(temperature(df['tmpf'].values, 'F'),
                      temperature(df['dwpf'].values, 'F')).value('%')
    df['tmpc'] = temperature(df['tmpf'].values, 'F').value('C')
    df['svp'] = 0.611 * np.exp(17.502 * df['tmpc'].values /
                               (240.97 + df['tmpc'].values))
    df['vpd'] = (1. - (df['relh'] / 100.)) * df['svp']
    df['mixing_ratio'] = mixing_ratio(
                            temperature(df['dwpf'].values, 'F')).value('KG/KG')

    dailymeans = df[['year', 'doy', varname]].groupby(['year', 'doy']).mean()
    dailymeans = dailymeans.reset_index()

    df2 = dailymeans[['doy', varname]].groupby('doy').describe()
    df2 = df2.unstack(level=-1)

    dyear = df[df['year'] == year]
    df3 = dyear[['doy', varname]].groupby('doy').describe()
    df3 = df3.unstack(level=-1)
    df3['diff'] = df3[(varname, 'mean')] - df2[(varname, 'mean')]

    (fig, ax) = plt.subplots(2, 1)
    multiplier = 1000. if varname == 'mixing_ratio' else 10.
    ax[0].fill_between(df2.index.values, df2[(varname, 'min')] * multiplier,
                       df2[(varname, 'max')] * multiplier, color='gray')

    ax[0].plot(df2.index.values, df2[(varname, 'mean')] * multiplier,
               label="Climatology")
    ax[0].plot(df3.index.values, df3[(varname, 'mean')] * multiplier,
               color='r', label="%s" % (year, ))

    ax[0].set_title(("%s [%s]\nDaily Mean Surface %s"
                     ) % (station, nt.sts[station]['name'],
                          PDICT[varname]))
    lbl = ("Mixing Ratio ($g/kg$)" if varname == 'mixing_ratio'
           else PDICT[varname])
    ax[0].set_ylabel(lbl)
    ax[0].set_xlim(0, 366)
    ax[0].set_ylim(bottom=0)
    ax[0].set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
                      365))
    ax[0].set_xticklabels(calendar.month_abbr[1:])
    ax[0].grid(True)
    ax[0].legend(loc=2, fontsize=10)

    cabove = 'b' if varname == 'mixing_ratio' else 'r'
    cbelow = 'r' if cabove == 'b' else 'b'
    rects = ax[1].bar(df3.index.values, df3['diff'] * multiplier,
                      facecolor=cabove, edgecolor=cabove)
    for rect in rects:
        if rect.get_y() < 0.:
            rect.set_facecolor(cbelow)
            rect.set_edgecolor(cbelow)

    units = '$g/kg$' if varname == 'mixing_ratio' else 'hPa'
    ax[1].set_ylabel("%.0f Departure (%s)" % (year, units))
    ax[1].set_xlim(0, 366)
    ax[1].set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335,
                      365))
    ax[1].set_xticklabels(calendar.month_abbr[1:])
    ax[1].grid(True)
    return fig, df3
示例#24
0
文件: p78.py 项目: muthulatha/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    station = fdict.get('zstation', 'AMW')
    network = fdict.get('network', 'IA_ASOS')
    month = fdict.get('month', 'all')

    nt = NetworkTable(network)

    if month == 'all':
        months = range(1, 13)
    elif month == 'fall':
        months = [9, 10, 11]
    elif month == 'winter':
        months = [12, 1, 2]
    elif month == 'spring':
        months = [3, 4, 5]
    elif month == 'summer':
        months = [6, 7, 8]
    else:
        ts = datetime.datetime.strptime("2000-"+month+"-01", '%Y-%b-%d')
        # make sure it is length two for the trick below in SQL
        months = [ts.month, 999]

    cursor.execute("""
        SELECT tmpf::int as t, dwpf from alldata where station = %s
        and tmpf is not null and dwpf is not null and dwpf <= tmpf
        and tmpf >= 0 and tmpf <= 140
        and extract(month from valid) in %s
        """, (station,  tuple(months)))
    sums = np.zeros((140,), 'f')
    counts = np.zeros((140,), 'f')
    for row in cursor:
        r = mixing_ratio(temperature(row[1], 'F')).value('KG/KG')
        sums[row[0]] += r
        counts[row[0]] += 1

    rows = []
    for i in range(140):
        if counts[i] < 3:
            continue
        r = sums[i] / float(counts[i])
        d = dewpoint_from_pq(pressure(1000, 'MB'),
                             mixingratio(r, 'KG/KG')
                             ).value('F')
        rh = relh(temperature(i, 'F'), temperature(d, 'F')).value('%')
        rows.append(dict(tmpf=i, dwpf=d, rh=rh))

    df = pd.DataFrame(rows)
    tmpf = df['tmpf']
    dwpf = df['dwpf']
    rh = df['rh']

    (fig, ax) = plt.subplots(1, 1)
    ax.bar(tmpf-0.5, dwpf, ec='green', fc='green', width=1)
    ax.grid(True, zorder=11)
    ax.set_title(("%s [%s]\nAverage Dew Point by Air Temperature (month=%s) "
                  "(%s-%s)\n"
                  "(must have 3+ hourly observations at the given temperature)"
                  ) % (nt.sts[station]['name'], station, month.upper(),
                       nt.sts[station]['archive_begin'].year,
                       datetime.datetime.now().year), size=10)

    ax.plot([0, 140], [0, 140], color='b')
    ax.set_ylabel("Dew Point [F]")
    y2 = ax.twinx()
    y2.plot(tmpf, rh, color='k')
    y2.set_ylabel("Relative Humidity [%] (black line)")
    y2.set_yticks([0, 5, 10, 25, 50, 75, 90, 95, 100])
    y2.set_ylim(0, 100)
    ax.set_ylim(0, max(tmpf)+2)
    ax.set_xlim(0, max(tmpf)+2)
    ax.set_xlabel("Air Temperature $^\circ$F")

    return fig, df
示例#25
0
def main():
    """ Go main Go """
    form = cgi.FieldStorage()
    try:
        tzinfo = pytz.timezone(form.getfirst("tz", "Etc/UTC"))
    except:
        sys.stdout.write("Invalid Timezone (tz) provided")
        sys.exit()

    # Save direct to disk or view in browser
    direct = True if form.getfirst('direct', 'no') == 'yes' else False
    stations = get_stations(form)
    if direct:
        sys.stdout.write('Content-type: application/octet-stream\n')
        fn = "%s.txt" % (stations[0], )
        if len(stations) > 1:
            fn = "asos.txt"
        sys.stdout.write('Content-Disposition: attachment; filename=%s\n\n' %
                         (fn, ))
    else:
        sys.stdout.write("Content-type: text/plain \n\n")

    dbstations = stations
    if len(dbstations) == 1:
        dbstations.append('XYZXYZ')

    dataVars = form.getlist("data")
    sts, ets = get_time_bounds(form, tzinfo)

    delim = form.getfirst("format")

    if "all" in dataVars:
        queryCols = ("tmpf, dwpf, relh, drct, sknt, p01i, alti, mslp, "
                     "vsby, gust, skyc1, skyc2, skyc3, skyc4, skyl1, "
                     "skyl2, skyl3, skyl4, presentwx, metar")
        outCols = [
            'tmpf', 'dwpf', 'relh', 'drct', 'sknt', 'p01i', 'alti', 'mslp',
            'vsby', 'gust', 'skyc1', 'skyc2', 'skyc3', 'skyc4', 'skyl1',
            'skyl2', 'skyl3', 'skyl4', 'presentwx', 'metar'
        ]
    else:
        dataVars = tuple(dataVars)
        outCols = dataVars
        dataVars = str(dataVars)[1:-2]
        queryCols = re.sub("'", " ", dataVars)

    if delim == "tdf":
        rD = "\t"
        queryCols = re.sub(",", "\t", queryCols)
    else:
        rD = ","

    gtxt = {}
    gisextra = False
    if form.getfirst("latlon", "no") == "yes":
        gisextra = True
        mcursor.execute(
            """SELECT id, ST_x(geom) as lon, ST_y(geom) as lat 
             from stations WHERE id in %s
             and (network ~* 'AWOS' or network ~* 'ASOS')""",
            (tuple(dbstations), ))
        for row in mcursor:
            gtxt[row[0]] = "%.4f%s%.4f%s" % (row['lon'], rD, row['lat'], rD)

    acursor.execute(
        """SELECT * from alldata
      WHERE valid >= %s and valid < %s and station in %s
      ORDER by valid ASC""", (sts, ets, tuple(dbstations)))

    sys.stdout.write("#DEBUG: Format Typ    -> %s\n" % (delim, ))
    sys.stdout.write("#DEBUG: Time Period   -> %s %s\n" % (sts, ets))
    sys.stdout.write("#DEBUG: Time Zone     -> %s\n" % (tzinfo, ))
    sys.stdout.write(("#DEBUG: Data Contact   -> daryl herzmann "
                      "[email protected] 515-294-5978\n"))
    sys.stdout.write("#DEBUG: Entries Found -> %s\n" % (acursor.rowcount, ))
    sys.stdout.write("station" + rD + "valid" + rD)
    if gisextra:
        sys.stdout.write("lon" + rD + "lat" + rD)
    sys.stdout.write(queryCols + "\n")

    for row in acursor:
        sys.stdout.write(row["station"] + rD)
        sys.stdout.write(
            (row["valid"].astimezone(tzinfo)).strftime("%Y-%m-%d %H:%M") + rD)
        if gisextra:
            sys.stdout.write(gtxt.get(row['station'], "M%sM%s" % (rD, rD)))
        r = []
        for data1 in outCols:
            if data1 == 'relh':
                if row['tmpf'] is not None and row['dwpf'] is not None:
                    tmpf = temperature(row['tmpf'], 'F')
                    dwpf = temperature(row['dwpf'], 'F')
                    val = meteorology.relh(tmpf, dwpf)
                    r.append("%.2f" % (val.value("%"), ))
                else:
                    r.append("M")
            elif data1 == 'sped':
                if row['sknt'] >= 0:
                    r.append("%.1f" % (row['sknt'] * 1.14, ))
                else:
                    r.append("M")
            elif data1 == 'gust_mph':
                if row['gust'] >= 0:
                    r.append("%.1f" % (row['gust'] * 1.14, ))
                else:
                    r.append("M")
            elif data1 == 'p01m':
                if row['p01i'] >= 0:
                    r.append("%.2f" % (row['p01i'] * 25.4, ))
                else:
                    r.append("M")
            elif data1 == 'tmpc':
                if row['tmpf'] is not None:
                    val = temperature(row['tmpf'], 'F').value('C')
                    r.append("%.2f" % (val, ))
                else:
                    r.append("M")
            elif data1 == 'dwpc':
                if row['dwpf'] is not None:
                    val = temperature(row['dwpf'], 'F').value('C')
                    r.append("%.2f" % (val, ))
                else:
                    r.append("M")
            elif data1 == 'presentwx':
                if row['presentwx'] is not None:
                    r.append("%s" % (row['presentwx'].replace(",", " "), ))
                else:
                    r.append("M")
            elif data1 in ["metar", "skyc1", "skyc2", "skyc3", "skyc4"]:
                if row[data1] is None:
                    r.append("M")
                else:
                    r.append("%s" % (row[data1], ))
            elif row[data1] is None or row[data1] <= -99.0 or row[data1] == "M":
                r.append("M")
            else:
                r.append("%2.2f" % (row[data1], ))
        sys.stdout.write("%s\n" % (rD.join(r), ))
示例#26
0
def main():
    """Do Something"""
    sys.stdout.write("Content-type: text/plain \n\n")
    form = cgi.FormContent()
    metadata = {}
    mcursor.execute("""SELECT id, ST_x(geom) as lon, ST_y(geom) as lat
         from stations WHERE network ~* 'ASOS' or network = 'AWOS'
         and country = 'US' """)
    for row in mcursor:
        metadata[row[0]] = {'lon': "%.4f" % (row[1],),
                            'lat': "%.4f" % (row[2],)}

    year = int(form["year"][0])
    month = int(form["month"][0])
    day = int(form["day"][0])
    hour = int(form["hour"][0])
    ts = mx.DateTime.DateTime(year, month, day, hour)

    queryCols = ("tmpf, dwpf, relh, drct, sknt, p01i, alti, mslp, vsby, "
                 "gust, skyc1, skyc2, skyc3, skyc4, skyl1, skyl2, skyl3, "
                 "skyl4, metar")
    outCols = ['tmpf', 'dwpf', 'relh', 'drct', 'sknt', 'p01i', 'alti', 'mslp',
               'vsby', 'gust', 'skyc1', 'skyc2', 'skyc3', 'skyc4',
               'skyl1', 'skyl2', 'skyl3', 'skyl4', 'metar']

    fmt = "%Y-%m-%d %H:%M"
    queryStr = """SELECT * from t%s
        WHERE valid >= '%s' and valid < '%s'
        ORDER by station ASC
        """ % (ts.year,
               (ts - mx.DateTime.RelativeDateTime(minutes=10)).strftime(fmt),
               (ts + mx.DateTime.RelativeDateTime(minutes=1)).strftime(fmt))

    rD = ","

    acursor.execute("SET TIME ZONE 'UTC'")
    acursor.execute(queryStr)

    print "station"+rD+"valid (UTC timezone)"+rD+"lon"+rD+"lat"+rD,
    print queryCols

    for row in acursor:
        if row['station'] not in metadata:
            continue
        sys.stdout.write(row["station"] + rD)
        sys.stdout.write(row["valid"].strftime("%Y-%m-%d %H:%M") + rD)
        sys.stdout.write(metadata[row['station']]['lon'] + rD)
        sys.stdout.write(metadata[row['station']]['lat'] + rD)
        for data1 in outCols:
            if data1 == 'relh':
                temp = temperature(row['tmpf'], 'F')
                dewp = temperature(row['dwpf'], 'F')
                val = meteorology.relh(temp, dewp).value('%')
                if val != "M":
                    sys.stdout.write("%.2f%s" % (val, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 == 'p01m':
                if row['p01i'] >= 0:
                    sys.stdout.write("%.2f%s" % (row['p01i'] * 25.4, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 == 'tmpc':
                if row['tmpf'] is not None:
                    val = temperature(row['tmpf'], 'F').value('C')
                    sys.stdout.write("%.2f%s" % (val, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 == 'dwpc':
                if row['dwpf'] is not None:
                    val = temperature(row['dwpf'], 'F').value('C')
                    sys.stdout.write("%.2f%s" % (val, rD))
                else:
                    sys.stdout.write("M%s" % (rD,))
            elif data1 in ["metar", "skyc1", "skyc2", "skyc3", "skyc4"]:
                sys.stdout.write("%s%s" % (row[data1], rD))
            elif row[data1] is None or row[data1] <= -99.0:
                sys.stdout.write("M%s" % (rD,))
            else:
                sys.stdout.write("%2.2f%s" % (row[data1], rD))
        print