Exemple #1
1
def test_windrose():
    """Exercise the windrose code"""
    basevalid = utc(2015, 1, 1, 6)
    valid = [basevalid]
    sknt = [None]
    drct = [None]
    for s in range(360):
        basevalid += datetime.timedelta(hours=1)
        valid.append(basevalid)
        # Keep the max speed at ~24kts
        sknt.append(s / 13.)
        drct.append(s)
    fig = windrose('AMW2', sknt=sknt, drct=drct, valid=valid, sname='Ames')
    assert fig is not None

    res = windrose(
        'AMW2', sknt=sknt, drct=drct, valid=valid,
        sts=datetime.datetime(2015, 1, 1),
        ets=datetime.datetime(2015, 10, 2), justdata=True)
    assert isinstance(res, str)

    # allow _get_data to be excercised
    res = windrose('XXXXX')
    assert res is not None

    fig = windrose(
        'AMW2', sknt=sknt, drct=drct, valid=valid,
        sts=datetime.datetime(2001, 1, 1),
        ets=datetime.datetime(2016, 1, 1), nogenerated=True)
    return fig
Exemple #2
0
def main():
    """Go Main"""
    net = sys.argv[1]
    nt = NetworkTable(net)
    sid = sys.argv[2]

    database = "asos"
    if net in ("KCCI", "KELO", "KIMT"):
        database = "snet"
    elif net in ("IA_RWIS", ):
        database = "rwis"
    elif net in ("ISUSM", ):
        database = "isuag"
    elif net.find("_DCP") > 0:
        database = "hads"

    mydir = "%s/%s/%s" % (CACHE_DIR, net, sid)
    if not os.path.isdir(mydir):
        os.makedirs(mydir)
    fn = "%s/%s_yearly.png" % (mydir, sid)
    res = windrose(sid, database=database, sname=nt.sts[sid]["name"])
    res.savefig(fn)
    plt.close()
    for month in range(1, 13):
        fn = ("%s/%s_%s.png") % (
            mydir,
            sid,
            datetime.datetime(2000, month, 1).strftime("%b").lower(),
        )
        res = windrose(sid,
                       months=(month, ),
                       database=database,
                       sname=nt.sts[sid]["name"])
        res.savefig(fn)
        plt.close()
    def test_windrose(self):
        """Exercise the windrose code"""
        v = datetime.datetime(2015, 1, 1, 6)
        v = v.replace(tzinfo=pytz.utc)
        valid = []
        sknt = []
        drct = []
        for s in range(100):
            v += datetime.timedelta(hours=1)
            valid.append(v)
            sknt.append(s)
            drct.append(s)
        fig = windrose('AMW2', sknt=sknt, drct=drct, valid=valid, sname='Ames')
        self.assertTrue(fig is not None)
        fig = windrose('AMW2',
                       sknt=sknt,
                       drct=drct,
                       valid=valid,
                       sts=datetime.datetime(2001, 1, 1),
                       ets=datetime.datetime(2016, 1, 1))
        # fig.savefig('/tmp/test_plot_windrose.png')
        self.assertTrue(fig is not None)

        res = windrose('AMW2',
                       sknt=sknt,
                       drct=drct,
                       valid=valid,
                       sts=datetime.datetime(2015, 1, 1),
                       ets=datetime.datetime(2015, 10, 2),
                       justdata=True)
        assert isinstance(res, str)
Exemple #4
0
def test_windrose():
    """Exercise the windrose code"""
    valid, sknt, drct = faux_data()
    fig = windrose("AMW2", sknt=sknt, drct=drct, valid=valid, sname="Ames")
    assert fig is not None

    res = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        sts=datetime.datetime(2015, 1, 1),
        ets=datetime.datetime(2015, 10, 2),
        justdata=True,
    )
    assert isinstance(res, str)

    # allow _get_data to be excercised
    res = windrose("XXXXX")
    assert res is not None

    fig = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        sts=datetime.datetime(2001, 1, 1),
        ets=datetime.datetime(2016, 1, 1),
        nogenerated=True,
    )
    return fig
Exemple #5
0
def main():
    """Go Main"""
    net = sys.argv[1]
    nt = NetworkTable(net)
    sid = sys.argv[2]

    database = 'asos'
    if net in ('KCCI', 'KELO', 'KIMT'):
        database = 'snet'
    elif net in ('IA_RWIS',):
        database = 'rwis'
    elif net in ('ISUSM',):
        database = 'isuag'
    elif net.find('_DCP') > 0:
        database = 'hads'

    fn = "/mesonet/share/windrose/climate/yearly/%s_yearly.png" % (sid,)
    print("%4s %-20.20s -- YR" % (sid, nt.sts[sid]['name']), end='')
    res = windrose(sid, database=database, sname=nt.sts[sid]['name'])
    res.savefig(fn)
    plt.close()
    for month in range(1, 13):
        fn = ("/mesonet/share/windrose/climate/monthly/%02i/%s_%s.png"
              ) % (month, sid,
                   datetime.datetime(2000, month, 1).strftime("%b").lower())
        print(" %s" % (month,), end='')
        res = windrose(sid, months=(month,), database=database,
                       sname=nt.sts[sid]['name'])
        res.savefig(fn)
        plt.close()

    print()
Exemple #6
0
def test_windrose_without_units():
    """Ensure that we can deal with provided bins."""
    valid, sknt, drct = faux_data()
    res = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        bins=[10, 20, 40],
        justdata=True,
    )
    # python2-3 hackery here
    sio = BytesIO()
    sio.write(res.encode("ascii"))
    sio.seek(0)
    df = read_csv(
        sio,
        skiprows=range(0, 8),
        index_col="Direction",
        delimiter=" *, *",
        engine="python",
    )
    assert df.index.values[0] == "355-004"
    assert len(df.columns) == 4
    assert abs(df.sum(axis=0).sum() - 100.0) < 0.1
Exemple #7
0
def test_windrose_hour_limiter():
    """Test that we can filter by hour."""
    valid, sknt, drct = faux_data()
    fig = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        hours=list(range(6, 16)),
        nogenerated=True,
    )
    return fig
Exemple #8
0
    def test_windrose(self):
        """Exercise the windrose code"""
        pgconn = psycopg2.connect(database='asos', host="iemdb")
        cursor = pgconn.cursor()
        v = datetime.datetime(2015, 1, 1, 6)
        for s in range(100):
            v += datetime.timedelta(hours=1)
            cursor.execute("""INSERT into t2015(station, valid, sknt, drct)
            VALUES (%s, %s, %s, %s)""", ('AMW2', v, s, s))
        fig = windrose('AMW2', cursor=cursor, sname='Ames')
        self.assertTrue(fig is not None)
        fig = windrose('AMW2',
                       cursor=cursor, sts=datetime.datetime(2001, 1, 1),
                       ets=datetime.datetime(2016, 1, 1))
        # fig.savefig('/tmp/test_plot_windrose.png')
        self.assertTrue(fig is not None)

        res = windrose('AMW2',
                       cursor=cursor, sts=datetime.datetime(2015, 1, 1),
                       ets=datetime.datetime(2015, 10, 2), justdata=True)
        assert isinstance(res, str)
Exemple #9
0
def test_windrose_month_limiter():
    """Test that we can filter by month."""
    valid, sknt, drct = faux_data()
    fig = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        nogenerated=True,
    )
    return fig
Exemple #10
0
def test_windrose_upperair():
    """Test the magic that happens when level= is set."""
    valid, sknt, drct = faux_data()
    fig = windrose(
        "_XXX",
        sknt=sknt,
        drct=drct,
        valid=valid,
        level=500,
        nogenerated=True,
        tzname="UTC",
    )
    return fig
Exemple #11
0
def test_windrose_with_units():
    """Ensure that we can deal with provided bins."""
    valid, sknt, drct = faux_data()
    res = windrose(
        "AMW2",
        sknt=sknt,
        drct=drct,
        valid=valid,
        months=[4, 5, 6],
        bins=[0.0001, 20, 40] * units("mph"),
        justdata=True,
    )
    assert res
Exemple #12
0
def test_windrose_upperair_text():
    """Test the magic that happens when level= is set."""
    valid, sknt, drct = faux_data()
    res = windrose(
        "_XXX",
        sknt=sknt,
        drct=drct,
        valid=valid,
        level=500,
        nogenerated=True,
        justdata=True,
        tzname="UTC",
    )
    assert res
Exemple #13
0
database = 'asos'
if form["network"].value in ('KCCI', 'KELO', 'KIMT'):
    database = 'snet'
elif form["network"].value in ('IA_RWIS'):
    database = 'rwis'
elif form["network"].value in ('ISUSM'):
    database = 'isuag'

try:
    nsector = int(form['nsector'].value)
except:
    nsector = 36

rmax = None
if "staticrange" in form and form["staticrange"].value == "1":
    rmax = 100

nt = NetworkTable(form['network'].value)
res = windrose(form["station"].value, database=database, sts=sts, ets=ets,
               months=months, hours=hours, units=units, nsector=nsector,
               justdata=("justdata" in form), rmax=rmax,
               sname=nt.sts[form['station'].value]['name'])
if 'justdata' in form:
    # We want text
    sys.stdout.write("Content-type: text/plain\n\n")
    sys.stdout.write(res)
else:
    sys.stdout.write("Content-type: image/png\n\n")
    res.savefig(sys.stdout, format='png')
Exemple #14
0
def application(environ, start_response):
    """ Query out the CGI variables"""
    form = parse_formvars(environ)
    try:
        sts, ets = get_times(form)
    except Exception:
        return [
            send_error(
                form,
                "Invalid Times Selected, please try again",
                start_response,
            )
        ]

    if "hour1" in form and "hourlimit" in form:
        hours = numpy.array((int(form["hour1"]), ))
    elif "hour1" in form and "hour2" in form and "hourrangelimit" in form:
        if sts.hour > ets.hour:  # over midnight
            hours = numpy.arange(sts.hour, 24)
            hours = numpy.append(hours, numpy.arange(0, ets.hour))
        else:
            if sts.hour == ets.hour:
                ets += datetime.timedelta(hours=1)
            hours = numpy.arange(sts.hour, ets.hour)
    else:
        hours = numpy.arange(0, 24)

    if "units" in form and form["units"] in ["mph", "kts", "mps", "kph"]:
        units = form["units"]
    else:
        units = "mph"

    if "month1" in form and "monthlimit" in form:
        months = numpy.array((int(form["month1"]), ))
    else:
        months = numpy.arange(1, 13)

    database = "asos"
    if form["network"] in ("KCCI", "KELO", "KIMT"):
        database = "snet"
    elif form["network"] in ("IA_RWIS", ):
        database = "rwis"
    elif form["network"] in ("ISUSM", ):
        database = "isuag"
    elif form["network"] in ("RAOB", ):
        database = "postgis"
    elif form["network"].find("_DCP") > 0:
        database = "hads"

    try:
        nsector = int(form["nsector"])
    except Exception:
        nsector = 36

    rmax = None
    if "staticrange" in form and form["staticrange"] == "1":
        rmax = 100

    nt = NetworkTable(form["network"], only_online=False)
    bins = []
    if "bins" in form:
        bins = [float(v) for v in form.get("bins").split(",")]
        bins.insert(0, 0)
    res = windrose(
        form["station"],
        database=database,
        sts=sts,
        ets=ets,
        months=months,
        hours=hours,
        units=units,
        nsector=nsector,
        justdata=("justdata" in form),
        rmax=rmax,
        sname=nt.sts[form["station"]]["name"],
        level=form.get("level", None),
        bins=bins,
    )
    if "justdata" in form:
        # We want text
        start_response("200 OK", [("Content-type", "text/plain")])
        return [res.encode("ascii")]
    fmt = form.get("fmt", "png")
    if fmt == "png":
        ct = "image/png"
    elif fmt == "pdf":
        ct = "application/pdf"
    elif fmt == "svg":
        ct = "image/svg+xml"
    else:
        return [send_error(form, "Invalid fmt set", start_response)]
    start_response("200 OK", [("Content-type", ct)])
    bio = BytesIO()
    res.savefig(bio, format=fmt, dpi=int(form.get("dpi", 100)))
    return [bio.getvalue()]
Exemple #15
0
def main():
    """ Query out the CGI variables"""
    form = cgi.FieldStorage()
    try:
        sts, ets = get_times(form)
    except Exception:
        send_error(form, "Invalid Times Selected, please try again")
        return

    if "hour1" in form and "hourlimit" in form:
        hours = numpy.array((int(form["hour1"].value),))
    elif "hour1" in form and "hour2" in form and "hourrangelimit" in form:
        if sts.hour > ets.hour:  # over midnight
            hours = numpy.arange(sts.hour, 24)
            hours = numpy.append(hours, numpy.arange(0, ets.hour))
        else:
            if sts.hour == ets.hour:
                ets += datetime.timedelta(hours=1)
            hours = numpy.arange(sts.hour, ets.hour)
    else:
        hours = numpy.arange(0, 24)

    if "units" in form and form["units"].value in ["mph", "kts", "mps", "kph"]:
        units = form["units"].value
    else:
        units = "mph"

    if "month1" in form and "monthlimit" in form:
        months = numpy.array((int(form["month1"].value),))
    else:
        months = numpy.arange(1, 13)

    database = "asos"
    if form["network"].value in ("KCCI", "KELO", "KIMT"):
        database = "snet"
    elif form["network"].value in ("IA_RWIS",):
        database = "rwis"
    elif form["network"].value in ("ISUSM",):
        database = "isuag"
    elif form["network"].value in ("RAOB",):
        database = "postgis"
    elif form["network"].value.find("_DCP") > 0:
        database = "hads"

    try:
        nsector = int(form["nsector"].value)
    except Exception:
        nsector = 36

    rmax = None
    if "staticrange" in form and form["staticrange"].value == "1":
        rmax = 100

    nt = NetworkTable(form["network"].value, only_online=False)
    bins = []
    if "bins" in form:
        bins = [float(v) for v in form.getfirst("bins").split(",")]
        bins.insert(0, 0)
    res = windrose(
        form["station"].value,
        database=database,
        sts=sts,
        ets=ets,
        months=months,
        hours=hours,
        units=units,
        nsector=nsector,
        justdata=("justdata" in form),
        rmax=rmax,
        sname=nt.sts[form["station"].value]["name"],
        level=form.getfirst("level", None),
        bins=bins,
    )
    if "justdata" in form:
        # We want text
        ssw("Content-type: text/plain\n\n")
        ssw(res)
    else:
        fmt = form.getfirst("fmt", "png")
        if fmt == "png":
            ct = "image/png"
        elif fmt == "pdf":
            ct = "application/pdf"
        elif fmt == "svg":
            ct = "image/svg+xml"
        else:
            ssw("Content-type: text/plain\n\n")
            ssw("Invalid fmt set")
            sys.exit(0)
        ssw("Content-type: %s\n\n" % (ct,))
        res.savefig(
            getattr(sys.stdout, "buffer", sys.stdout),
            format=fmt,
            dpi=int(form.getfirst("dpi", 100)),
        )
Exemple #16
0
def main():
    """ Query out the CGI variables"""
    form = cgi.FieldStorage()
    try:
        sts, ets = get_times(form)
    except Exception as _:
        send_error(form, "Invalid Times Selected, please try again")
        return

    if "hour1" in form and "hourlimit" in form:
        hours = numpy.array((int(form["hour1"].value), ))
    elif "hour1" in form and "hour2" in form and "hourrangelimit" in form:
        if sts.hour > ets.hour:  # over midnight
            hours = numpy.arange(sts.hour, 24)
            hours = numpy.append(hours, numpy.arange(0, ets.hour))
        else:
            if sts.hour == ets.hour:
                ets += datetime.timedelta(hours=1)
            hours = numpy.arange(sts.hour, ets.hour)
    else:
        hours = numpy.arange(0, 24)

    if "units" in form and form["units"].value in ['mph', 'kts', 'mps', 'kph']:
        units = form["units"].value
    else:
        units = "mph"

    if "month1" in form and "monthlimit" in form:
        months = numpy.array((int(form["month1"].value), ))
    else:
        months = numpy.arange(1, 13)

    database = 'asos'
    if form["network"].value in ('KCCI', 'KELO', 'KIMT'):
        database = 'snet'
    elif form["network"].value in ('IA_RWIS', ):
        database = 'rwis'
    elif form["network"].value in ('ISUSM', ):
        database = 'isuag'
    elif form["network"].value in ('RAOB', ):
        database = 'postgis'
    elif form["network"].value.find("_DCP") > 0:
        database = 'hads'

    try:
        nsector = int(form['nsector'].value)
    except Exception as _:
        nsector = 36

    rmax = None
    if "staticrange" in form and form["staticrange"].value == "1":
        rmax = 100

    nt = NetworkTable(form['network'].value)
    bins = []
    if 'bins' in form:
        bins = [float(v) for v in form.getfirst('bins').split(",")]
        bins.insert(0, 0)
    res = windrose(form["station"].value,
                   database=database,
                   sts=sts,
                   ets=ets,
                   months=months,
                   hours=hours,
                   units=units,
                   nsector=nsector,
                   justdata=("justdata" in form),
                   rmax=rmax,
                   sname=nt.sts[form['station'].value]['name'],
                   level=form.getfirst('level', None),
                   bins=bins)
    if 'justdata' in form:
        # We want text
        sys.stdout.write("Content-type: text/plain\n\n")
        sys.stdout.write(res)
    else:
        fmt = form.getfirst('fmt', 'png')
        if fmt == 'png':
            ct = "image/png"
        elif fmt == 'pdf':
            ct = "application/pdf"
        elif fmt == 'svg':
            ct = "image/svg+xml"
        else:
            sys.stdout.write("Content-type: text/plain\n\n")
            sys.stdout.write("Invalid fmt set")
            sys.exit(0)
        sys.stdout.write("Content-type: %s\n\n" % (ct, ))
        res.savefig(sys.stdout, format=fmt)
Exemple #17
0
import datetime
import sys
net = sys.argv[1]
nt = NetworkTable(net)
sid = sys.argv[2]

database = 'asos'
if net in ('KCCI', 'KELO', 'KIMT'):
    database = 'snet'
elif net in ('IA_RWIS'):
    database = 'rwis'
elif net in ('ISUSM'):
    database = 'isuag'


fn = "/mesonet/share/windrose/climate/yearly/%s_yearly.png" % (sid,)
print "%4s %-20.20s -- YR" % (sid, nt.sts[sid]['name']),
res = windrose(sid, database=database, sname=nt.sts[sid]['name'])
res.savefig(fn)
plt.close()
for m in range(1, 13):
    fn = ("/mesonet/share/windrose/climate/monthly/%02i/%s_%s.png"
          ) % (m, sid, datetime.datetime(2000, m, 1).strftime("%b").lower())
    print "%s" % (m,),
    res = windrose(sid, months=(m,), database=database,
                   sname=nt.sts[sid]['name'])
    res.savefig(fn)
    plt.close()

print
Exemple #18
0
try:
    nsector = int(form['nsector'].value)
except:
    nsector = 36

rmax = None
if "staticrange" in form and form["staticrange"].value == "1":
    rmax = 100

nt = NetworkTable(form['network'].value)
res = windrose(form["station"].value,
               database=database,
               sts=sts,
               ets=ets,
               months=months,
               hours=hours,
               units=units,
               nsector=nsector,
               justdata=("justdata" in form),
               rmax=rmax,
               sname=nt.sts[form['station'].value]['name'],
               level=form.getfirst('level', None))
if 'justdata' in form:
    # We want text
    sys.stdout.write("Content-type: text/plain\n\n")
    sys.stdout.write(res)
else:
    fmt = form.getfirst('fmt', 'png')
    if fmt == 'png':
        ct = "image/png"
    elif fmt == 'pdf':
        ct = "application/pdf"
Exemple #19
0
def application(environ, start_response):
    """ Query out the CGI variables"""
    form = parse_formvars(environ)
    try:
        sts, ets = get_times(form)
        dbname, network, station = get_station_info(form)
    except Exception as exp:
        return [send_error(form, str(exp), start_response)]
    if "hour1" in form and "hourlimit" in form:
        hours = numpy.array((int(form["hour1"]), ))
    elif "hour1" in form and "hour2" in form and "hourrangelimit" in form:
        if sts.hour > ets.hour:  # over midnight
            hours = numpy.arange(sts.hour, 24)
            hours = numpy.append(hours, numpy.arange(0, ets.hour))
        else:
            if sts.hour == ets.hour:
                ets += datetime.timedelta(hours=1)
            hours = numpy.arange(sts.hour, ets.hour)
    else:
        hours = numpy.arange(0, 24)

    if "units" in form and form["units"] in ["mph", "kts", "mps", "kph"]:
        units = form["units"]
    else:
        units = "mph"

    if "month1" in form and "monthlimit" in form:
        months = numpy.array((int(form["month1"]), ))
    else:
        months = numpy.arange(1, 13)

    try:
        nsector = int(form["nsector"])
    except Exception:
        nsector = 36

    rmax = None
    if "staticrange" in form and form["staticrange"] == "1":
        rmax = 100

    nt = NetworkTable(network, only_online=False)
    tzname = nt.sts[station]["tzname"]
    if network != "RAOB":
        # Assign the station time zone to the sts and ets
        sts = pytz.timezone(tzname).localize(sts)
        ets = pytz.timezone(tzname).localize(ets)
    else:
        tzname = "UTC"
    bins = []
    if "bins" in form:
        bins = [float(v) for v in form.get("bins").split(",")]
    res = windrose(
        station,
        database=dbname,
        sts=sts,
        ets=ets,
        months=months,
        hours=hours,
        units=units,
        nsector=nsector,
        justdata=("justdata" in form),
        rmax=rmax,
        sname=nt.sts[station]["name"],
        tzname=tzname,
        level=form.get("level", None),
        bins=bins,
    )
    if "justdata" in form:
        # We want text
        start_response("200 OK", [("Content-type", "text/plain")])
        return [res.encode("ascii")]
    fmt = form.get("fmt", "png")
    if fmt == "png":
        ct = "image/png"
    elif fmt == "pdf":
        ct = "application/pdf"
    elif fmt == "svg":
        ct = "image/svg+xml"
    else:
        return [send_error(form, "Invalid fmt set", start_response)]
    start_response("200 OK", [("Content-type", ct)])
    bio = BytesIO()
    res.savefig(bio, format=fmt, dpi=int(form.get("dpi", 100)))
    return [bio.getvalue()]
Exemple #20
0
import sys
net = sys.argv[1]
nt = NetworkTable(net)
sid = sys.argv[2]

database = 'asos'
if net in ('KCCI', 'KELO', 'KIMT'):
    database = 'snet'
elif net in ('IA_RWIS'):
    database = 'rwis'
elif net in ('ISUSM'):
    database = 'isuag'

fn = "/mesonet/share/windrose/climate/yearly/%s_yearly.png" % (sid, )
print "%4s %-20.20s -- YR" % (sid, nt.sts[sid]['name']),
res = windrose(sid, database=database, sname=nt.sts[sid]['name'])
res.savefig(fn)
plt.close()
for m in range(1, 13):
    fn = ("/mesonet/share/windrose/climate/monthly/%02i/%s_%s.png") % (
        m, sid, datetime.datetime(2000, m, 1).strftime("%b").lower())
    print "%s" % (m, ),
    res = windrose(sid,
                   months=(m, ),
                   database=database,
                   sname=nt.sts[sid]['name'])
    res.savefig(fn)
    plt.close()

print
Exemple #21
0
def windrose(*args, **kwargs):
    """Depreciated."""
    warnings.warn("windrose() is depreciated, use pyiem.windrose_utils!")
    import pyiem.windrose_utils as wru
    return wru.windrose(*args, **kwargs)
Exemple #22
0
def test_windrose_upperair_nodata():
    """Test what happens with upperair logic and no data found."""
    fig = windrose("_XXX", level=500)
    return fig
Exemple #23
0
def test_windrose_upperair_nodata_text():
    """Test what happens with upperair logic and no data found."""
    res = windrose("_XXX", level=500, justdata=True, hours=list(range(6, 12)))
    assert res