Ejemplo n.º 1
0
 def test_drct(self):
     """Conversion of u and v to direction"""
     self.assertEquals(
         meteorology.drct(datatypes.speed(np.array([10, 20]), 'KT'),
                          datatypes.speed(np.array([10, 20]),
                                          'KT')).value("DEG")[0], 225)
     self.assertEquals(
         meteorology.drct(datatypes.speed(-10, 'KT'),
                          datatypes.speed(10, 'KT')).value("DEG"), 135)
     self.assertEquals(
         meteorology.drct(datatypes.speed(-10, 'KT'),
                          datatypes.speed(-10, 'KT')).value("DEG"), 45)
     self.assertEquals(
         meteorology.drct(datatypes.speed(10, 'KT'),
                          datatypes.speed(-10, 'KT')).value("DEG"), 315)
Ejemplo n.º 2
0
def test_drct():
    """Conversion of u and v to direction"""
    r = meteorology.drct(datatypes.speed(np.array([10, 20]), 'KT'),
                         datatypes.speed(np.array([10, 20]),
                                         'KT')).value("DEG")
    assert r[0] == 225
    r = meteorology.drct(datatypes.speed(-10, 'KT'),
                         datatypes.speed(10, 'KT')).value("DEG")
    assert r == 135
    r = meteorology.drct(datatypes.speed(-10, 'KT'),
                         datatypes.speed(-10, 'KT')).value("DEG")
    assert r == 45
    r = meteorology.drct(datatypes.speed(10, 'KT'),
                         datatypes.speed(-10, 'KT')).value("DEG")
    assert r == 315
Ejemplo n.º 3
0
 def test_drct(self):
     """Conversion of u and v to direction"""
     self.assertEquals(
         meteorology.drct(datatypes.speed(np.array([10, 20]), 'KT'),
                          datatypes.speed(np.array([10, 20]), 'KT')
                          ).value("DEG")[0], 225)
     self.assertEquals(meteorology.drct(datatypes.speed(-10, 'KT'),
                                        datatypes.speed(10, 'KT')
                                        ).value("DEG"), 135)
     self.assertEquals(meteorology.drct(datatypes.speed(-10, 'KT'),
                                        datatypes.speed(-10, 'KT')
                                        ).value("DEG"), 45)
     self.assertEquals(meteorology.drct(datatypes.speed(10, 'KT'),
                                        datatypes.speed(-10, 'KT')
                                        ).value("DEG"), 315)
Ejemplo n.º 4
0
Archivo: p138.py Proyecto: akrherz/iem
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']
    units = ctx['units']

    nt = NetworkTable(network)

    df = read_sql("""
        select date_trunc('hour', valid) as ts, avg(sknt) as sknt,
        max(drct) as drct from alldata
        WHERE station = %s and sknt is not null and drct is not null
        GROUP by ts
        """, pgconn, params=(station, ), parse_dates=('ts',),
                  index_col=None)
    sknt = speed(df['sknt'].values, 'KT')
    drct = direction(df['drct'].values, 'DEG')
    df['u'], df['v'] = [x.value('MPS') for x in meteorology.uv(sknt, drct)]
    df['month'] = df['ts'].dt.month
    grp = df[['month', 'u', 'v', 'sknt']].groupby('month').mean()
    grp['u_%s' % (units,)] = speed(grp['u'].values, 'KT').value(units.upper())
    grp['v_%s' % (units,)] = speed(grp['u'].values, 'KT').value(units.upper())
    grp['sped_%s' % (units,)] = speed(grp['sknt'].values,
                                      'KT').value(units.upper())
    drct = meteorology.drct(speed(grp['u'].values, 'KT'),
                            speed(grp['v'].values, 'KT'))
    grp['drct'] = drct.value('DEG')
    maxval = grp['sped_%s' % (units,)].max()
    (fig, ax) = plt.subplots(1, 1)
    ax.barh(grp.index.values, grp['sped_%s' % (units,)].values,
            align='center')
    ax.set_xlabel("Average Wind Speed [%s]" % (UNITS[units],))
    ax.set_yticks(grp.index.values)
    ax.set_yticklabels(calendar.month_abbr[1:])
    ax.grid(True)
    ax.set_xlim(0, maxval * 1.2)
    for mon, row in grp.iterrows():
        ax.text(maxval * 1.1, mon, drct2text(row['drct']), ha='center',
                va='center', bbox=dict(color='white'))
        ax.text(row['sped_%s' % (units,)] * 0.98, mon,
                "%.1f" % (row['sped_%s' % (units,)],), ha='right',
                va='center', bbox=dict(color='white',
                                       boxstyle='square,pad=0.03',))
    ax.set_ylim(12.5, 0.5)
    ax.set_title(("[%s] %s [%s-%s]\nMonthly Average Wind Speed and"
                  " Vector Average Direction"
                  ) % (station, nt.sts[station]['name'],
                       df['ts'].min().year,
                       df['ts'].max().year))

    return fig, grp
Ejemplo n.º 5
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = get_dbconn('asos')
    ctx = get_autoplot_context(fdict, get_description())

    station = ctx['zstation']
    network = ctx['network']
    units = ctx['units']

    nt = NetworkTable(network)

    df = read_sql("""
        select date_trunc('hour', valid at time zone 'UTC') as ts,
        avg(sknt) as sknt, max(drct) as drct from alldata
        WHERE station = %s and sknt is not null and drct is not null
        GROUP by ts
        """, pgconn, params=(station, ), index_col=None)
    sknt = speed(df['sknt'].values, 'KT')
    drct = direction(df['drct'].values, 'DEG')
    df['u'], df['v'] = [x.value('MPS') for x in meteorology.uv(sknt, drct)]
    df['month'] = df['ts'].dt.month
    grp = df[['month', 'u', 'v', 'sknt']].groupby('month').mean()
    grp['u_%s' % (units,)] = speed(grp['u'].values, 'KT').value(units.upper())
    grp['v_%s' % (units,)] = speed(grp['u'].values, 'KT').value(units.upper())
    grp['sped_%s' % (units,)] = speed(grp['sknt'].values,
                                      'KT').value(units.upper())
    drct = meteorology.drct(speed(grp['u'].values, 'KT'),
                            speed(grp['v'].values, 'KT'))
    grp['drct'] = drct.value('DEG')
    maxval = grp['sped_%s' % (units,)].max()
    (fig, ax) = plt.subplots(1, 1)
    ax.barh(grp.index.values, grp['sped_%s' % (units,)].values,
            align='center')
    ax.set_xlabel("Average Wind Speed [%s]" % (UNITS[units],))
    ax.set_yticks(grp.index.values)
    ax.set_yticklabels(calendar.month_abbr[1:])
    ax.grid(True)
    ax.set_xlim(0, maxval * 1.2)
    for mon, row in grp.iterrows():
        ax.text(maxval * 1.1, mon, drct2text(row['drct']), ha='center',
                va='center', bbox=dict(color='white'))
        ax.text(row['sped_%s' % (units,)] * 0.98, mon,
                "%.1f" % (row['sped_%s' % (units,)],), ha='right',
                va='center', bbox=dict(color='white',
                                       boxstyle='square,pad=0.03',))
    ax.set_ylim(12.5, 0.5)
    ax.set_title(("[%s] %s [%s-%s]\nMonthly Average Wind Speed and"
                  " Vector Average Direction"
                  ) % (station, nt.sts[station]['name'],
                       df['ts'].min().year,
                       df['ts'].max().year))

    return fig, grp
Ejemplo n.º 6
0
def test_drct():
    """Conversion of u and v to direction"""
    r = meteorology.drct(
        datatypes.speed(np.array([10, 20]), 'KT'),
        datatypes.speed(np.array([10, 20]), 'KT')
    ).value("DEG")
    assert r[0] == 225
    r = meteorology.drct(
        datatypes.speed(-10, 'KT'),
        datatypes.speed(10, 'KT')
    ).value("DEG")
    assert r == 135
    r = meteorology.drct(
        datatypes.speed(-10, 'KT'),
        datatypes.speed(-10, 'KT')
    ).value("DEG")
    assert r == 45
    r = meteorology.drct(
        datatypes.speed(10, 'KT'),
        datatypes.speed(-10, 'KT')
    ).value("DEG")
    assert r == 315
Ejemplo n.º 7
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()
Ejemplo n.º 8
0
def write_grids(fp, valid, fhour):
    """Do the write to disk"""
    gribfn = "%s/%sF%03i.grib2" % (TMP, valid.strftime("%Y%m%d%H%M"), fhour)
    if not os.path.isfile(gribfn):
        print("Skipping write_grids because of missing fn: %s" % (gribfn, ))
        return
    gribs = pygrib.open(gribfn)
    grids = dict()
    for grib in gribs:
        grids[grib.name] = grib
    d = dict()
    if '2 metre temperature' in grids:
        g = grids['2 metre temperature']
        if G['LATS'] is None:
            G['LATS'], G['LONS'] = g.latlons()
        vals = temperature(g.values, 'K').value('C')
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['tmpc'] = nn(XI, YI)
        if 'Relative humidity' in grids:
            g = grids['Relative humidity']
            vals = g.values
            nn = NearestNDInterpolator(
                (G['LONS'].flatten(), G['LATS'].flatten()), vals.flatten())
            rh = nn(XI, YI)
            d['dwpc'] = dewpoint(temperature(d['tmpc'], 'C'),
                                 humidity(rh, '%')).value('C')
    if ('10 metre U wind component' in grids
            and '10 metre V wind component' in grids):
        u = grids['10 metre U wind component'].values
        v = grids['10 metre V wind component'].values
        vals = ((u**2) + (v**2))**0.5
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['smps'] = nn(XI, YI)
        vals = drct(speed(u, 'MPS'), speed(v, 'MPS')).value('deg')
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['drct'] = nn(XI, YI)
    if 'Total Precipitation' in grids:
        vals = grids['Total Precipitation'].values
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['pcpn'] = nn(XI, YI)
    if 'Visibility' in grids:
        vals = grids['Visibility'].values / 1000.  # km
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['vsby'] = nn(XI, YI)

    fp.write("""{"forecast_hour": "%03i",
    "gids": [
""" % (fhour, ))
    fmt = ('{"gid": %s, "tmpc": %s, "dwpc": %s, '
           '"smps": %s, "drct": %s, "vsby": %s, "pcpn": %s}')
    i = 1
    ar = []

    def f(label, row, col, fmt):
        if label not in d:
            return 'null'
        return fmt % d[label][row, col]

    for row in range(len(YAXIS)):
        for col in range(len(XAXIS)):
            ar.append(
                fmt %
                (i, f('tmpc', row, col, '%.2f'), f('dwpc', row, col, '%.2f'),
                 f('smps', row, col, '%.1f'), f('drct', row, col, '%i'),
                 f('vsby', row, col, '%.3f'), f('pcpn', row, col, '%.2f')))
            i += 1
    fp.write(",\n".join(ar))
    fp.write("]}%s\n" % ("," if fhour != 84 else '', ))
Ejemplo n.º 9
0
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn("asos")
    ctx = get_autoplot_context(fdict, get_description())

    station = ctx["zstation"]
    units = ctx["units"]
    df = read_sql(
        """
        select date_trunc('hour', valid at time zone 'UTC') as ts,
        avg(sknt) as sknt, max(drct) as drct from alldata
        WHERE station = %s and sknt is not null and drct is not null
        GROUP by ts
    """,
        pgconn,
        params=(station, ),
        index_col=None,
    )
    if df.empty:
        raise NoDataFound("No Data Found.")
    sknt = speed(df["sknt"].values, "KT")
    drct = direction(df["drct"].values, "DEG")
    df["u"], df["v"] = [x.value("MPS") for x in meteorology.uv(sknt, drct)]
    df["month"] = df["ts"].dt.month
    grp = df[["month", "u", "v", "sknt"]].groupby("month").mean()
    grp["u_%s" % (units, )] = speed(grp["u"].values, "KT").value(units.upper())
    grp["v_%s" % (units, )] = speed(grp["u"].values, "KT").value(units.upper())
    grp["sped_%s" % (units, )] = speed(grp["sknt"].values,
                                       "KT").value(units.upper())
    drct = meteorology.drct(speed(grp["u"].values, "KT"),
                            speed(grp["v"].values, "KT"))
    grp["drct"] = drct.value("DEG")
    maxval = grp["sped_%s" % (units, )].max()
    (fig, ax) = plt.subplots(1, 1)
    ax.barh(grp.index.values,
            grp["sped_%s" % (units, )].values,
            align="center")
    ax.set_xlabel("Average Wind Speed [%s]" % (UNITS[units], ))
    ax.set_yticks(grp.index.values)
    ax.set_yticklabels(calendar.month_abbr[1:])
    ax.grid(True)
    ax.set_xlim(0, maxval * 1.2)
    for mon, row in grp.iterrows():
        ax.text(
            maxval * 1.1,
            mon,
            drct2text(row["drct"]),
            ha="center",
            va="center",
            bbox=dict(color="white"),
        )
        ax.text(
            row["sped_%s" % (units, )] * 0.98,
            mon,
            "%.1f" % (row["sped_%s" % (units, )], ),
            ha="right",
            va="center",
            bbox=dict(color="white", boxstyle="square,pad=0.03"),
        )
    ax.set_ylim(12.5, 0.5)
    ax.set_title(("[%s] %s [%s-%s]\nMonthly Average Wind Speed and"
                  " Vector Average Direction") % (
                      station,
                      ctx["_nt"].sts[station]["name"],
                      df["ts"].min().year,
                      df["ts"].max().year,
                  ))

    return fig, grp
Ejemplo n.º 10
0
def write_grids(fp, valid, fhour):
    """Do the write to disk"""
    gribfn = "%s/%sF%03i.grib2" % (TMP, valid.strftime("%Y%m%d%H%M"),
                                   fhour)
    if not os.path.isfile(gribfn):
        print("Skipping write_grids because of missing fn: %s" % (gribfn,))
        return
    gribs = pygrib.open(gribfn)
    grids = dict()
    for grib in gribs:
        grids[grib.name] = grib
    d = dict()
    if '2 metre temperature' in grids:
        g = grids['2 metre temperature']
        if G['LATS'] is None:
            G['LATS'], G['LONS'] = g.latlons()
        vals = temperature(g.values, 'K').value('C')
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['tmpc'] = nn(XI, YI)
        if 'Relative humidity' in grids:
            g = grids['Relative humidity']
            vals = g.values
            nn = NearestNDInterpolator((G['LONS'].flatten(),
                                        G['LATS'].flatten()),
                                       vals.flatten())
            rh = nn(XI, YI)
            d['dwpc'] = dewpoint(temperature(d['tmpc'], 'C'),
                                 humidity(rh, '%')).value('C')
    if ('10 metre U wind component' in grids and
            '10 metre V wind component' in grids):
        u = grids['10 metre U wind component'].values
        v = grids['10 metre V wind component'].values
        vals = ((u ** 2) + (v ** 2)) ** 0.5
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['smps'] = nn(XI, YI)
        vals = drct(speed(u, 'MPS'), speed(v, 'MPS')).value('deg')
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['drct'] = nn(XI, YI)
    if 'Total Precipitation' in grids:
        vals = grids['Total Precipitation'].values
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['pcpn'] = nn(XI, YI)
    if 'Visibility' in grids:
        vals = grids['Visibility'].values / 1000.  # km
        nn = NearestNDInterpolator((G['LONS'].flatten(), G['LATS'].flatten()),
                                   vals.flatten())
        d['vsby'] = nn(XI, YI)

    fp.write("""{"forecast_hour": "%03i",
    "gids": [
""" % (fhour,))
    fmt = ('{"gid": %s, "tmpc": %s, "dwpc": %s, '
           '"smps": %s, "drct": %s, "vsby": %s, "pcpn": %s}')
    i = 1
    ar = []

    def f(label, row, col, fmt):
        if label not in d:
            return 'null'
        return fmt % d[label][row, col]

    for row in range(len(YAXIS)):
        for col in range(len(XAXIS)):
            ar.append(fmt % (i, f('tmpc', row, col, '%.2f'),
                             f('dwpc', row, col, '%.2f'),
                             f('smps', row, col, '%.1f'),
                             f('drct', row, col, '%i'),
                             f('vsby', row, col, '%.3f'),
                             f('pcpn', row, col, '%.2f')))
            i += 1
    fp.write(",\n".join(ar))
    fp.write("]}%s\n" % ("," if fhour != 84 else '',))
Ejemplo n.º 11
0
def simple(grids, valid, iarchive):
    """Simple gridder (stub for now)"""
    if iarchive:
        pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')
        df = read_sql("""
            SELECT ST_x(geom) as lon, ST_y(geom) as lat,
            tmpf, dwpf, sknt, drct, vsby
            from alldata c JOIN stations t on
            (c.station = t.id)
            WHERE c.valid >= %s and c.valid < %s and
            t.network in ('IA_ASOS', 'AWOS', 'MN_ASOS', 'WI_ASOS', 'IL_ASOS',
            'MO_ASOS', 'NE_ASOS', 'KS_ASOS', 'SD_ASOS') and sknt is not null
            and drct is not null and tmpf is not null and dwpf is not null
            and vsby is not null
            """,
                      pgconn,
                      params=((valid - datetime.timedelta(minutes=30)),
                              (valid + datetime.timedelta(minutes=30))),
                      index_col=None)
    else:
        pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
        df = read_sql("""
            SELECT ST_x(geom) as lon, ST_y(geom) as lat,
            tmpf, dwpf, sknt, drct, vsby
            from current c JOIN stations t on (c.iemid = t.iemid)
            WHERE c.valid > now() - '1 hour'::interval and
            t.network in ('IA_ASOS', 'AWOS', 'MN_ASOS', 'WI_ASOS', 'IL_ASOS',
            'MO_ASOS', 'NE_ASOS', 'KS_ASOS', 'SD_ASOS') and sknt is not null
            and drct is not null and tmpf is not null and dwpf is not null
            and vsby is not null
            """,
                      pgconn,
                      index_col=None)

    if len(df.index) < 5:
        print(("i5gridder abort len(data): %s for %s iarchive: %s" %
               (len(df.index), valid, iarchive)))
        sys.exit()

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               temperature(df['tmpf'].values, 'F').value('C'))
    grids['tmpc'] = nn(XI, YI)

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               temperature(df['dwpf'].values, 'F').value('C'))
    grids['dwpc'] = nn(XI, YI)

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               speed(df['sknt'].values, 'KT').value('MPS'))
    grids['smps'] = nn(XI, YI)

    u, v = meteorology.uv(speed(df['sknt'].values, 'KT'),
                          direction(df['drct'].values, 'DEG'))
    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               u.value('MPS'))
    ugrid = nn(XI, YI)
    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               v.value('MPS'))
    vgrid = nn(XI, YI)
    drct = meteorology.drct(speed(ugrid.ravel(), 'MPS'),
                            speed(vgrid.ravel(),
                                  'MPS')).value('DEG').astype('i')
    grids['drct'] = np.reshape(drct, (len(YAXIS), len(XAXIS)))

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               distance(df['vsby'].values, 'MI').value('KM'))
    grids['vsby'] = nn(XI, YI)
Ejemplo n.º 12
0
def simple(grids, valid, iarchive):
    """Simple gridder (stub for now)"""
    if iarchive:
        pgconn = psycopg2.connect(database='asos', host='iemdb', user='******')
        df = read_sql("""
            SELECT ST_x(geom) as lon, ST_y(geom) as lat,
            tmpf, dwpf, sknt, drct, vsby
            from alldata c JOIN stations t on
            (c.station = t.id)
            WHERE c.valid >= %s and c.valid < %s and
            t.network in ('IA_ASOS', 'AWOS', 'MN_ASOS', 'WI_ASOS', 'IL_ASOS',
            'MO_ASOS', 'NE_ASOS', 'KS_ASOS', 'SD_ASOS') and sknt is not null
            and drct is not null and tmpf is not null and dwpf is not null
            and vsby is not null
            """, pgconn, params=((valid - datetime.timedelta(minutes=30)),
                                 (valid + datetime.timedelta(minutes=30))),
                      index_col=None)
    else:
        pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
        df = read_sql("""
            SELECT ST_x(geom) as lon, ST_y(geom) as lat,
            tmpf, dwpf, sknt, drct, vsby
            from current c JOIN stations t on (c.iemid = t.iemid)
            WHERE c.valid > now() - '1 hour'::interval and
            t.network in ('IA_ASOS', 'AWOS', 'MN_ASOS', 'WI_ASOS', 'IL_ASOS',
            'MO_ASOS', 'NE_ASOS', 'KS_ASOS', 'SD_ASOS') and sknt is not null
            and drct is not null and tmpf is not null and dwpf is not null
            and vsby is not null
            """, pgconn, index_col=None)

    if len(df.index) < 5:
        print(("i5gridder abort len(data): %s for %s iarchive: %s"
               % (len(df.index), valid, iarchive)))
        sys.exit()

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               temperature(df['tmpf'].values, 'F').value('C'))
    grids['tmpc'] = nn(XI, YI)

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               temperature(df['dwpf'].values, 'F').value('C'))
    grids['dwpc'] = nn(XI, YI)

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               speed(df['sknt'].values, 'KT').value('MPS'))
    grids['smps'] = nn(XI, YI)

    u, v = meteorology.uv(speed(df['sknt'].values, 'KT'),
                          direction(df['drct'].values, 'DEG'))
    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               u.value('MPS'))
    ugrid = nn(XI, YI)
    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               v.value('MPS'))
    vgrid = nn(XI, YI)
    drct = meteorology.drct(speed(ugrid.ravel(), 'MPS'),
                            speed(vgrid.ravel(), 'MPS')
                            ).value('DEG').astype('i')
    grids['drct'] = np.reshape(drct, (len(YAXIS), len(XAXIS)))

    nn = NearestNDInterpolator((df['lon'].values, df['lat'].values),
                               distance(df['vsby'].values, 'MI').value('KM'))
    grids['vsby'] = nn(XI, YI)