示例#1
0
文件: p82.py 项目: jamayfieldjr/iem
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn('iem')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    varname = ctx['var']
    sdate = ctx['sdate']
    edate = ctx['edate']

    # Get Climatology
    cdf = read_sql("""
        SELECT to_char(valid, 'mmdd') as sday, high, low,
        (high + low) / 2. as avg,
        precip from ncdc_climate81 WHERE station = %s
    """, get_dbconn('coop'),
                   params=(
                       ctx['_nt'].sts[station]['ncdc81'],), index_col='sday')
    if cdf.empty:
        raise NoDataFound("No Data Found.")

    cursor.execute("""
    SELECT day, max_tmpf, min_tmpf, max_dwpf, min_dwpf,
    (max_tmpf + min_tmpf) / 2. as avg_tmpf,
    pday, coalesce(avg_sknt, 0) as avg_sknt from summary s JOIN stations t
    on (t.iemid = s.iemid) WHERE s.day >= %s and s.day <= %s and
    t.id = %s and t.network = %s ORDER by day ASC
    """, (sdate, edate, station, ctx['network']))
    rows = []
    data = {}
    for row in cursor:
        hd = row['max_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'high']
        ld = row['min_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'low']
        ad = row['avg_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'avg']
        rows.append(dict(day=row['day'], max_tmpf=row['max_tmpf'],
                         avg_smph=speed(row['avg_sknt'], 'KT').value('MPH'),
                         min_dwpf=row['min_dwpf'], max_dwpf=row['max_dwpf'],
                         high_departure=hd, low_departure=ld,
                         avg_departure=ad,
                         min_tmpf=row['min_tmpf'], pday=row['pday']))
        data[row[0]] = {'val': safe(rows[-1], varname)}
        if data[row[0]]['val'] == '0':
            data[row[0]]['color'] = 'k'
        elif varname == 'high_departure':
            data[row[0]]['color'] = 'b' if hd < 0 else 'r'
        elif varname == 'low_departure':
            data[row[0]]['color'] = 'b' if ld < 0 else 'r'
        elif varname == 'avg_departure':
            data[row[0]]['color'] = 'b' if ad < 0 else 'r'
    df = pd.DataFrame(rows)

    title = '[%s] %s Daily %s' % (
        station, ctx['_nt'].sts[station]['name'], PDICT.get(varname))
    subtitle = '%s thru %s' % (
        sdate.strftime("%-d %b %Y"), edate.strftime("%-d %b %Y"))

    fig = calendar_plot(
        sdate, edate, data, title=title, subtitle=subtitle)
    return fig, df
示例#2
0
 def test_calendar(self):
     """See if we can make a calendar plot!"""
     import matplotlib.pyplot as plt
     sts = datetime.date(2015, 5, 1)
     ets = datetime.date(2015, 7, 1)
     data = dict()
     fig = plot.calendar_plot(sts, ets, data)
     self.assertTrue(isinstance(fig, plt.Figure))
示例#3
0
 def test_calendar(self):
     """See if we can make a calendar plot!"""
     import matplotlib.pyplot as plt
     sts = datetime.date(2015, 5, 1)
     ets = datetime.date(2015, 7, 1)
     data = dict()
     fig = plot.calendar_plot(sts, ets, data)
     self.assertTrue(isinstance(fig, plt.Figure))
示例#4
0
def test_issue101():
    """We like June, it is a good month, don't drop it."""
    sts = datetime.date(2017, 5, 29)
    ets = datetime.date(2017, 9, 30)
    data = dict()
    data[datetime.date(2017, 6, 6)] = {'val': "0606"}
    data[datetime.date(2017, 7, 6)] = {'val': "0506"}
    return calendar_plot(
        sts, ets, data, title='Whiz Bang, Wizzardry',
        subtitle='This is officially unofficial and hacky.')
示例#5
0
def test_calendar():
    """See if we can make a calendar plot!"""
    sts = datetime.date(2015, 5, 4)
    ets = datetime.date(2015, 5, 15)
    data = dict()
    data[datetime.date(2015, 5, 16)] = {'val': 300, 'color': '#ff0000'}
    data[datetime.date(2015, 5, 6)] = {'val': 1, 'cellcolor': '#0000ff'}
    return calendar_plot(
        sts, ets, data, title='Whiz Bang, Wizzardry',
        subtitle='This is officially unofficial and hacky.', heatmap=True)
示例#6
0
def test_calendar4():
    """See if we can make a calendar plot!"""
    sts = datetime.date(2015, 5, 4)
    ets = datetime.date(2015, 8, 15)
    data = dict()
    data[datetime.date(2015, 6, 6)] = {'val': "0606"}
    data[datetime.date(2015, 5, 6)] = {'val': "0506"}
    return calendar_plot(
        sts, ets, data, title='Whiz Bang, Wizzardry',
        subtitle='This is officially unofficial and hacky.')
示例#7
0
文件: p82.py 项目: akrherz/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import calendar_plot
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    varname = ctx['var']
    network = ctx['network']
    sdate = ctx['sdate']
    edate = ctx['edate']

    nt = NetworkTable(network)

    # Get Climatology
    cdf = read_sql("""SELECT to_char(valid, 'mmdd') as sday, high, low,
    precip from ncdc_climate81 WHERE station = %s
    """, psycopg2.connect(database='coop', host='iemdb', user='******'),
                   params=(nt.sts[station]['ncdc81'],), index_col='sday')

    cursor.execute("""
    SELECT day, max_tmpf, min_tmpf, max_dwpf, min_dwpf,
    pday, coalesce(avg_sknt, 0) as avg_sknt from summary s JOIN stations t
    on (t.iemid = s.iemid) WHERE s.day >= %s and s.day <= %s and
    t.id = %s and t.network = %s ORDER by day ASC
    """, (sdate, edate, station, network))
    rows = []
    data = {}
    for row in cursor:
        hd = row['max_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'high']
        ld = row['min_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'low']
        rows.append(dict(day=row['day'], max_tmpf=row['max_tmpf'],
                         avg_smph=speed(row['avg_sknt'], 'KT').value('MPH'),
                         min_dwpf=row['min_dwpf'], max_dwpf=row['max_dwpf'],
                         high_departure=hd, low_departure=ld,
                         min_tmpf=row['min_tmpf'], pday=row['pday']))
        data[row[0]] = {'val': safe(rows[-1], varname)}
        if varname == 'high_departure':
            data[row[0]]['color'] = 'b' if hd < 0 else 'r'
        elif varname == 'low_departure':
            data[row[0]]['color'] = 'b' if ld < 0 else 'r'
    df = pd.DataFrame(rows)

    title = ('[%s] %s Daily %s\n%s thru %s'
             ) % (station, nt.sts[station]['name'],
                  PDICT.get(varname), sdate.strftime("%-d %b %Y"),
                  edate.strftime("%-d %b %Y"))

    fig = calendar_plot(sdate, edate, data,
                        title=title)
    return fig, df
示例#8
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import calendar_plot
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    varname = ctx['var']
    network = ctx['network']
    sdate = ctx['sdate']
    edate = ctx['edate']

    nt = NetworkTable(network)

    # Get Climatology
    cdf = read_sql("""SELECT to_char(valid, 'mmdd') as sday, high, low,
    precip from ncdc_climate81 WHERE station = %s
    """, psycopg2.connect(database='coop', host='iemdb', user='******'),
                   params=(nt.sts[station]['ncdc81'],), index_col='sday')

    cursor.execute("""
    SELECT day, max_tmpf, min_tmpf, max_dwpf, min_dwpf,
    pday, coalesce(avg_sknt, 0) as avg_sknt from summary s JOIN stations t
    on (t.iemid = s.iemid) WHERE s.day >= %s and s.day <= %s and
    t.id = %s and t.network = %s ORDER by day ASC
    """, (sdate, edate, station, network))
    rows = []
    data = {}
    for row in cursor:
        hd = row['max_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'high']
        ld = row['min_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'low']
        rows.append(dict(day=row['day'], max_tmpf=row['max_tmpf'],
                         avg_smph=speed(row['avg_sknt'], 'KT').value('MPH'),
                         min_dwpf=row['min_dwpf'], max_dwpf=row['max_dwpf'],
                         high_departure=hd, low_departure=ld,
                         min_tmpf=row['min_tmpf'], pday=row['pday']))
        data[row[0]] = {'val': safe(rows[-1], varname)}
        if varname == 'high_departure':
            data[row[0]]['color'] = 'b' if hd < 0 else 'r'
        elif varname == 'low_departure':
            data[row[0]]['color'] = 'b' if ld < 0 else 'r'
    df = pd.DataFrame(rows)

    title = ('[%s] %s Daily %s\n%s thru %s'
             ) % (station, nt.sts[station]['name'],
                  PDICT.get(varname), sdate.strftime("%-d %b %Y"),
                  edate.strftime("%-d %b %Y"))

    fig = calendar_plot(sdate, edate, data,
                        title=title)
    return fig, df
示例#9
0
文件: p82.py 项目: muthulatha/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import calendar_plot
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    station = fdict.get('station', 'DSM')
    varname = fdict.get('var', 'pday')
    network = fdict.get('network', 'IA_ASOS')
    sdate = datetime.datetime.strptime(fdict.get('sdate', '2015-01-01'),
                                       '%Y-%m-%d')
    edate = datetime.datetime.strptime(fdict.get('edate', '2015-02-01'),
                                       '%Y-%m-%d')
    sdate = sdate.date()
    edate = edate.date()
    if PDICT.get(varname) is None:
        return

    nt = NetworkTable(network)

    # Get Climatology
    cdf = read_sql("""SELECT to_char(valid, 'mmdd') as sday, high, low,
    precip from ncdc_climate81 WHERE station = %s
    """, psycopg2.connect(database='coop', host='iemdb', user='******'),
                   params=(nt.sts[station]['ncdc81'],), index_col='sday')

    cursor.execute("""
    SELECT day, max_tmpf, min_tmpf, pday from summary s JOIN stations t
    on (t.iemid = s.iemid) WHERE s.day >= %s and s.day <= %s and
    t.id = %s and t.network = %s ORDER by day ASC
    """, (sdate, edate, station, network))
    rows = []
    data = {}
    for row in cursor:
        hd = row['max_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'high']
        ld = row['min_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'low']
        rows.append(dict(day=row['day'], max_tmpf=row['max_tmpf'],
                         high_departure=hd, low_departure=ld,
                         min_tmpf=row['min_tmpf'], pday=row['pday']))
        data[row[0]] = {'val': safe(rows[-1], varname)}
        if varname == 'high_departure':
            data[row[0]]['color'] = 'b' if hd < 0 else 'r'
        elif varname == 'low_departure':
            data[row[0]]['color'] = 'b' if ld < 0 else 'r'
    df = pd.DataFrame(rows)

    title = '[%s] %s Daily %s' % (station, nt.sts[station]['name'],
                                  PDICT.get(varname))

    fig = calendar_plot(sdate, edate, data,
                        title=title)
    return fig, df
示例#10
0
def test_issue101():
    """We like June, it is a good month, don't drop it."""
    sts = datetime.date(2017, 5, 29)
    ets = datetime.date(2017, 9, 30)
    data = dict()
    data[datetime.date(2017, 6, 6)] = {'val': "0606"}
    data[datetime.date(2017, 7, 6)] = {'val': "0506"}
    return calendar_plot(sts,
                         ets,
                         data,
                         title='Whiz Bang, Wizzardry',
                         subtitle='This is officially unofficial and hacky.')
示例#11
0
def test_calendar4():
    """See if we can make a calendar plot!"""
    sts = datetime.date(2015, 5, 4)
    ets = datetime.date(2015, 8, 15)
    data = dict()
    data[datetime.date(2015, 6, 6)] = {'val': "0606"}
    data[datetime.date(2015, 5, 6)] = {'val': "0506"}
    return calendar_plot(sts,
                         ets,
                         data,
                         title='Whiz Bang, Wizzardry',
                         subtitle='This is officially unofficial and hacky.')
示例#12
0
def test_calendar():
    """See if we can make a calendar plot!"""
    sts = datetime.date(2015, 5, 4)
    ets = datetime.date(2015, 5, 15)
    data = dict()
    data[datetime.date(2015, 5, 16)] = {'val': 300, 'color': '#ff0000'}
    data[datetime.date(2015, 5, 6)] = {'val': 1, 'cellcolor': '#0000ff'}
    return calendar_plot(sts,
                         ets,
                         data,
                         title='Whiz Bang, Wizzardry',
                         subtitle='This is officially unofficial and hacky.',
                         heatmap=True)
示例#13
0
def test_calendar4():
    """See if we can make a calendar plot!"""
    sts = datetime.date(2015, 5, 4)
    ets = datetime.date(2015, 8, 15)
    data = dict()
    data[datetime.date(2015, 6, 6)] = {"val": "0606"}
    data[datetime.date(2015, 5, 6)] = {"val": "0506"}
    return calendar_plot(
        sts,
        ets,
        data,
        title="Whiz Bang, Wizzardry",
        subtitle="This is officially unofficial and hacky.\nAnd a second line",
    )
示例#14
0
def test_calendar():
    """See if we can make a calendar plot!"""
    sts = datetime.date(2015, 5, 4)
    ets = datetime.date(2015, 5, 15)
    data = dict()
    data[datetime.date(2015, 5, 16)] = {"val": 300, "color": "#ff0000"}
    data[datetime.date(2015, 5, 6)] = {"val": 1, "cellcolor": "#0000ff"}
    return calendar_plot(
        sts,
        ets,
        data,
        title="Whiz Bang, Wizzardry",
        subtitle="This is officially unofficial and hacky.",
        heatmap=True,
    )
示例#15
0
def test_calendar2():
    """See if we can make a calendar plot!"""
    sts = datetime.date(2015, 5, 4)
    ets = datetime.date(2015, 6, 15)
    data = dict()
    data[datetime.date(2015, 6, 6)] = {"val": "0606"}
    data[datetime.date(2015, 5, 6)] = {"val": "0506"}
    return calendar_plot(
        sts,
        ets,
        data,
        title=("Whiz Bang, Wizzardry. This is even more text and we "
               "have even more."),
        subtitle="This is officially unofficial and hacky.",
    )
示例#16
0
def main():
    """Go Main Go"""
    pgconn = get_dbconn('postgis')
    cursor = pgconn.cursor()
    cursor.execute("""
    WITH data as (
    SELECT eventid, min(issue) from warnings_2017 where wfo = 'OUN' and
    phenomena = 'FG' and significance = 'Y' GROUP by eventid
    )

    SELECT date(min), count(*) from data GROUP by date
    """)
    data = {}
    for row in cursor:
        data[row[0]] = {'val': row[1]}
    fig = calendar_plot(datetime.date(2017, 1, 1),
                        datetime.date(2017, 12, 31), data, heatmap=True)
    fig.text(0.5, 0.95, "2017 OUN Daily Severe Thunderstorm Warnings",
             ha='center', fontsize=20)
    fig.savefig('test.png')
示例#17
0
文件: p82.py 项目: bthoover/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import calendar_plot
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    station = fdict.get('station', 'DSM')
    varname = fdict.get('var', 'pday')
    network = fdict.get('network', 'IA_ASOS')
    sdate = datetime.datetime.strptime(fdict.get('sdate', '2015-01-01'),
                                       '%Y-%m-%d')
    edate = datetime.datetime.strptime(fdict.get('edate', '2015-02-01'),
                                       '%Y-%m-%d')
    sdate = sdate.date()
    edate = edate.date()
    if PDICT.get(varname) is None:
        return

    nt = NetworkTable(network)

    cursor.execute("""
    SELECT day, max_tmpf, min_tmpf, pday from summary s JOIN stations t
    on (t.iemid = s.iemid) WHERE s.day >= %s and s.day <= %s and
    t.id = %s and t.network = %s ORDER by day ASC
    """, (sdate, edate, station, network))
    rows = []
    data = {}
    for row in cursor:
        data[row[0]] = {'val': safe(row, varname)}
        rows.append(dict(day=row['day'], max_tmpf=row['max_tmpf'],
                         min_tmpf=row['min_tmpf'], pday=row['pday']))
    df = pd.DataFrame(rows)

    title = '[%s] %s Daily %s' % (station, nt.sts[station]['name'],
                                PDICT.get(varname))

    fig = calendar_plot(sdate, edate, data,
                        title=title)
    return fig, df
示例#18
0
文件: p191.py 项目: smartparrot/iem
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn("postgis")
    ctx = get_autoplot_context(fdict, get_description())
    sts = ctx["sdate"]
    ets = ctx["edate"]
    wfo = ctx["wfo"]
    p1 = ctx["phenomenav1"]
    p2 = ctx["phenomenav2"]
    p3 = ctx["phenomenav3"]
    p4 = ctx["phenomenav4"]
    phenomena = []
    for p in [p1, p2, p3, p4]:
        if p is not None:
            phenomena.append(p[:2])
    s1 = ctx["significancev1"]
    s2 = ctx["significancev2"]
    s3 = ctx["significancev3"]
    s4 = ctx["significancev4"]
    significance = []
    for s in [s1, s2, s3, s4]:
        if s is not None:
            significance.append(s[0])

    pstr = []
    title = ""
    for i, (p, s) in enumerate(zip(phenomena, significance)):
        pstr.append("(phenomena = '%s' and significance = '%s')" % (p, s))
        if i == 2:
            title += "\n"
        title += "%s %s.%s, " % (vtec.get_ps_string(p, s), p, s)
    pstr = " or ".join(pstr)
    pstr = "(%s)" % (pstr, )

    if ctx["w"] == "wfo":
        ctx["_nt"].sts["_ALL"] = {
            "name": "All Offices",
            "tzname": "America/Chicago",
        }
        if wfo not in ctx["_nt"].sts:
            raise NoDataFound("No Data Found.")
        wfo_limiter = (" and wfo = '%s' ") % (wfo
                                              if len(wfo) == 3 else wfo[1:], )
        if wfo == "_ALL":
            wfo_limiter = ""
        tzname = ctx["_nt"].sts[wfo]["tzname"]
    else:
        wfo_limiter = " and substr(ugc, 1, 2) = '%s' " % (ctx["state"], )
        tzname = "America/Chicago"

    df = read_sql(
        """
with events as (
  select wfo, min(issue at time zone %s) as localissue,
  extract(year from issue) as year,
  phenomena, significance, eventid from warnings
  where """ + pstr + """ """ + wfo_limiter + """ and
  issue >= %s and issue < %s GROUP by wfo, year, phenomena, significance,
  eventid
)

SELECT date(localissue), count(*) from events GROUP by date(localissue)
    """,
        pgconn,
        params=(
            tzname,
            sts - datetime.timedelta(days=2),
            ets + datetime.timedelta(days=2),
        ),
        index_col="date",
    )

    data = {}
    now = sts
    while now <= ets:
        data[now] = {"val": 0}
        now += datetime.timedelta(days=1)
    for date, row in df.iterrows():
        data[date] = {"val": row["count"]}
    if ctx["w"] == "wfo":
        title2 = "NWS %s [%s]" % (ctx["_nt"].sts[wfo]["name"], wfo)
        if wfo == "_ALL":
            title2 = "All NWS Offices"
    else:
        title2 = state_names[ctx["state"]]
    fig = calendar_plot(
        sts,
        ets,
        data,
        heatmap=(ctx["heatmap"] == "yes"),
        title=("Number of VTEC Events for %s by Local Calendar Date") %
        (title2, ),
        subtitle="Valid %s - %s for %s" %
        (sts.strftime("%d %b %Y"), ets.strftime("%d %b %Y"), title),
    )
    return fig, df
示例#19
0
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    pgconn = get_dbconn('postgis')
    ctx = get_autoplot_context(fdict, get_description())
    sts = ctx['sdate']
    ets = ctx['edate']
    wfo = ctx['wfo']
    p1 = ctx['phenomenav1']
    p2 = ctx['phenomenav2']
    p3 = ctx['phenomenav3']
    p4 = ctx['phenomenav4']
    phenomena = []
    for p in [p1, p2, p3, p4]:
        if p is not None:
            phenomena.append(p[:2])
    s1 = ctx['significancev1']
    s2 = ctx['significancev2']
    s3 = ctx['significancev3']
    s4 = ctx['significancev4']
    significance = []
    for s in [s1, s2, s3, s4]:
        if s is not None:
            significance.append(s[0])

    pstr = []
    title = ""
    for i, (p, s) in enumerate(zip(phenomena, significance)):
        pstr.append("(phenomena = '%s' and significance = '%s')" % (p, s))
        if i == 2:
            title += "\n"
        title += "%s %s.%s, " % (vtec.get_ps_string(p, s), p, s)
    pstr = " or ".join(pstr)
    pstr = "(%s)" % (pstr, )

    if ctx['w'] == 'wfo':
        nt = NetworkTable("WFO")
        nt.sts['_ALL'] = {'name': 'All Offices', 'tzname': 'America/Chicago'}
        wfo_limiter = (" and wfo = '%s' ") % (wfo
                                              if len(wfo) == 3 else wfo[1:], )
        if wfo == '_ALL':
            wfo_limiter = ''
        tzname = nt.sts[wfo]['tzname']
    else:
        wfo_limiter = " and substr(ugc, 1, 2) = '%s' " % (ctx['state'], )
        tzname = 'America/Chicago'

    df = read_sql("""
with events as (
  select wfo, min(issue at time zone %s) as localissue,
  extract(year from issue) as year,
  phenomena, significance, eventid from warnings
  where """ + pstr + """ """ + wfo_limiter + """ and
  issue >= %s and issue < %s GROUP by wfo, year, phenomena, significance,
  eventid
)

SELECT date(localissue), count(*) from events GROUP by date(localissue)
    """,
                  pgconn,
                  params=(tzname, sts - datetime.timedelta(days=2),
                          ets + datetime.timedelta(days=2)),
                  index_col='date')

    data = {}
    now = sts
    while now <= ets:
        data[now] = {'val': 0}
        now += datetime.timedelta(days=1)
    for date, row in df.iterrows():
        data[date] = {'val': row['count']}
    fig = calendar_plot(sts, ets, data, heatmap=(ctx['heatmap'] == 'yes'))
    if ctx['w'] == 'wfo':
        title2 = "NWS %s [%s]" % (nt.sts[wfo]['name'], wfo)
        if wfo == '_ALL':
            title2 = "All NWS Offices"
    else:
        title2 = state_names[ctx['state']]
    fig.text(
        0.5,
        0.95,
        ("Number of VTEC Events for %s by Local Calendar Date"
         "\nValid %s - %s for %s") %
        (title2, sts.strftime("%d %b %Y"), ets.strftime("%d %b %Y"), title),
        ha='center',
        va='center')

    return fig, df
示例#20
0
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn('postgis')
    ctx = get_autoplot_context(fdict, get_description())
    sts = ctx['sdate']
    ets = ctx['edate']
    wfo = ctx['wfo']
    outlook_type = ctx['outlook_type']
    day = int(ctx['day'])
    ugc = ctx['ugc']

    sqllimiter = ""
    category = "CATEGORICAL"
    if day >= 4 and outlook_type == 'C':
        category = 'ANY SEVERE'
    elif day >= 3 and outlook_type == 'F':
        category = 'CRITICAL FIRE WEATHER AREA'
    elif outlook_type == 'F':
        category = 'FIRE WEATHER CATEGORICAL'
    if ctx['w'] == 'all':
        df = read_sql("""
        with data as (
            select expire, o.threshold from spc_outlooks o
            WHERE category = %s
            and o.day = %s and o.outlook_type = %s and expire > %s
            and expire < %s),
        agg as (
            select date(expire - '1 day'::interval), d.threshold, priority,
            rank() OVER (PARTITION by date(expire - '1 day'::interval)
            ORDER by priority DESC)
            from data d JOIN spc_outlook_thresholds t
            on (d.threshold = t.threshold))

        SELECT distinct date, threshold from agg where rank = 1
        ORDER by date ASC
        """,
                      pgconn,
                      params=(category, day, outlook_type, sts,
                              ets + datetime.timedelta(days=2)),
                      index_col='date')
        title2 = "Continental US"
    else:
        if ctx['w'] == 'wfo':
            table = "cwa"
            abbrcol = "wfo"
            geoval = wfo
            geomcol = "the_geom"
            if wfo not in ctx['_nt'].sts:
                raise NoDataFound("Unknown station metadata.")
            title2 = "NWS %s [%s]" % (ctx['_nt'].sts[wfo]['name'], wfo)
        elif ctx['w'] == 'ugc':
            table = "ugcs"
            abbrcol = "ugc"
            geomcol = "simple_geom"
            geoval = ugc
            sqllimiter = " and t.end_ts is null "
            cursor = pgconn.cursor()
            cursor.execute(
                """
                SELECT name from ugcs where ugc = %s and end_ts is null
            """, (ugc, ))
            name = "Unknown"
            if cursor.rowcount == 1:
                name = cursor.fetchone()[0]
            title2 = "%s [%s] %s" % ("County" if ugc[2] == 'C' else 'Zone',
                                     ugc, name)
        else:
            table = "states"
            geomcol = "the_geom"
            abbrcol = "state_abbr"
            geoval = ctx['mystate']
            title2 = state_names[ctx['mystate']]

        df = read_sql("""
        with data as (
            select expire, o.threshold from spc_outlooks o,
            """ + table + """ t
            WHERE t.""" + abbrcol + """ = %s and category = %s
            and ST_Intersects(st_buffer(o.geom, 0), t.""" + geomcol + """)
            and o.day = %s and o.outlook_type = %s and expire > %s
            and expire < %s """ + sqllimiter + """),
        agg as (
            select date(expire - '1 day'::interval), d.threshold, priority,
            rank() OVER (PARTITION by date(expire - '1 day'::interval)
            ORDER by priority DESC)
            from data d JOIN spc_outlook_thresholds t
            on (d.threshold = t.threshold))

        SELECT distinct date, threshold from agg where rank = 1
        ORDER by date ASC
        """,
                      pgconn,
                      params=(geoval, category, day, outlook_type, sts,
                              ets + datetime.timedelta(days=2)),
                      index_col='date')

    data = {}
    now = sts
    while now <= ets:
        data[now] = {'val': " "}
        now += datetime.timedelta(days=1)
    for date, row in df.iterrows():
        if row['threshold'] == 'TSTM' and ctx.get('g', 'yes') == 'no':
            continue
        data[date] = {
            'val': row['threshold'],
            'cellcolor': COLORS.get(row['threshold'], '#EEEEEE')
        }
    fig = calendar_plot(sts,
                        ets,
                        data,
                        title="Highest SPC Day %s %s Outlook for %s" %
                        (day, PDICT[outlook_type], title2),
                        subtitle="Valid %s - %s" %
                        (sts.strftime("%d %b %Y"), ets.strftime("%d %b %Y")))
    return fig, df
示例#21
0
文件: p201.py 项目: stormchas4/iem
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn("postgis")
    ctx = get_autoplot_context(fdict, get_description())
    sts = ctx["sdate"]
    ets = ctx["edate"]
    wfo = ctx["wfo"]
    outlook_type = ctx["outlook_type"]
    day = int(ctx["day"])
    ugc = ctx["ugc"]

    sqllimiter = ""
    category = "CATEGORICAL"
    if day >= 4 and outlook_type == "C":
        category = "ANY SEVERE"
    elif day >= 3 and outlook_type == "F":
        category = "CRITICAL FIRE WEATHER AREA"
    elif outlook_type == "F":
        category = "FIRE WEATHER CATEGORICAL"
    if ctx["w"] == "all":
        df = read_sql(
            """
        with data as (
            select expire, o.threshold from spc_outlooks o
            WHERE category = %s
            and o.day = %s and o.outlook_type = %s and expire > %s
            and expire < %s),
        agg as (
            select date(expire - '1 day'::interval), d.threshold, priority,
            rank() OVER (PARTITION by date(expire - '1 day'::interval)
            ORDER by priority DESC)
            from data d JOIN spc_outlook_thresholds t
            on (d.threshold = t.threshold))

        SELECT distinct date, threshold from agg where rank = 1
        ORDER by date ASC
        """,
            pgconn,
            params=(
                category,
                day,
                outlook_type,
                sts,
                ets + datetime.timedelta(days=2),
            ),
            index_col="date",
        )
        title2 = "Continental US"
    else:
        if ctx["w"] == "wfo":
            table = "cwa"
            abbrcol = "wfo"
            geoval = wfo
            geomcol = "the_geom"
            if wfo not in ctx["_nt"].sts:
                raise NoDataFound("Unknown station metadata.")
            title2 = "NWS %s [%s]" % (ctx["_nt"].sts[wfo]["name"], wfo)
        elif ctx["w"] == "ugc":
            table = "ugcs"
            abbrcol = "ugc"
            geomcol = "simple_geom"
            geoval = ugc
            sqllimiter = " and t.end_ts is null "
            cursor = pgconn.cursor()
            cursor.execute(
                """
                SELECT name from ugcs where ugc = %s and end_ts is null
            """,
                (ugc, ),
            )
            name = "Unknown"
            if cursor.rowcount == 1:
                name = cursor.fetchone()[0]
            title2 = "%s [%s] %s" % (
                "County" if ugc[2] == "C" else "Zone",
                ugc,
                name,
            )
        else:
            table = "states"
            geomcol = "the_geom"
            abbrcol = "state_abbr"
            geoval = ctx["mystate"]
            title2 = state_names[ctx["mystate"]]

        df = read_sql(
            """
        with data as (
            select expire, o.threshold from spc_outlooks o,
            """ + table + """ t
            WHERE t.""" + abbrcol + """ = %s and category = %s
            and ST_Intersects(st_buffer(o.geom, 0), t.""" + geomcol + """)
            and o.day = %s and o.outlook_type = %s and expire > %s
            and expire < %s """ + sqllimiter + """),
        agg as (
            select date(expire - '1 day'::interval), d.threshold, priority,
            rank() OVER (PARTITION by date(expire - '1 day'::interval)
            ORDER by priority DESC)
            from data d JOIN spc_outlook_thresholds t
            on (d.threshold = t.threshold))

        SELECT distinct date, threshold from agg where rank = 1
        ORDER by date ASC
        """,
            pgconn,
            params=(
                geoval,
                category,
                day,
                outlook_type,
                sts,
                ets + datetime.timedelta(days=2),
            ),
            index_col="date",
        )

    data = {}
    now = sts
    while now <= ets:
        data[now] = {"val": " "}
        now += datetime.timedelta(days=1)
    for date, row in df.iterrows():
        if row["threshold"] == "TSTM" and ctx.get("g", "yes") == "no":
            continue
        data[date] = {
            "val": row["threshold"],
            "cellcolor": COLORS.get(row["threshold"], "#EEEEEE"),
        }
    fig = calendar_plot(
        sts,
        ets,
        data,
        title="Highest SPC Day %s %s Outlook for %s" %
        (day, PDICT[outlook_type], title2),
        subtitle="Valid %s - %s" %
        (sts.strftime("%d %b %Y"), ets.strftime("%d %b %Y")),
    )
    return fig, df
示例#22
0
文件: p82.py 项目: nbackas/iem
def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import calendar_plot
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='******')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    station = fdict.get('station', 'DSM')
    varname = fdict.get('var', 'pday')
    network = fdict.get('network', 'IA_ASOS')
    sdate = datetime.datetime.strptime(fdict.get('sdate', '2015-01-01'),
                                       '%Y-%m-%d')
    edate = datetime.datetime.strptime(fdict.get('edate', '2015-02-01'),
                                       '%Y-%m-%d')
    sdate = sdate.date()
    edate = edate.date()
    if PDICT.get(varname) is None:
        return

    nt = NetworkTable(network)

    # Get Climatology
    cdf = read_sql("""SELECT to_char(valid, 'mmdd') as sday, high, low,
    precip from ncdc_climate81 WHERE station = %s
    """,
                   psycopg2.connect(database='coop',
                                    host='iemdb',
                                    user='******'),
                   params=(nt.sts[station]['ncdc81'], ),
                   index_col='sday')

    cursor.execute(
        """
    SELECT day, max_tmpf, min_tmpf, pday from summary s JOIN stations t
    on (t.iemid = s.iemid) WHERE s.day >= %s and s.day <= %s and
    t.id = %s and t.network = %s ORDER by day ASC
    """, (sdate, edate, station, network))
    rows = []
    data = {}
    for row in cursor:
        hd = row['max_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'high']
        ld = row['min_tmpf'] - cdf.at[row[0].strftime("%m%d"), 'low']
        rows.append(
            dict(day=row['day'],
                 max_tmpf=row['max_tmpf'],
                 high_departure=hd,
                 low_departure=ld,
                 min_tmpf=row['min_tmpf'],
                 pday=row['pday']))
        data[row[0]] = {'val': safe(rows[-1], varname)}
        if varname == 'high_departure':
            data[row[0]]['color'] = 'b' if hd < 0 else 'r'
        elif varname == 'low_departure':
            data[row[0]]['color'] = 'b' if ld < 0 else 'r'
    df = pd.DataFrame(rows)

    title = '[%s] %s Daily %s' % (station, nt.sts[station]['name'],
                                  PDICT.get(varname))

    fig = calendar_plot(sdate, edate, data, title=title)
    return fig, df
示例#23
0
文件: p82.py 项目: smartparrot/iem
def plotter(fdict):
    """ Go """
    pgconn = get_dbconn("iem")
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    ctx = get_autoplot_context(fdict, get_description())
    station = ctx["station"]
    varname = ctx["var"]
    sdate = ctx["sdate"]
    edate = ctx["edate"]

    # Get Climatology
    cdf = read_sql(
        """
        SELECT to_char(valid, 'mmdd') as sday, high, low,
        (high + low) / 2. as avg,
        precip from ncdc_climate81 WHERE station = %s
    """,
        get_dbconn("coop"),
        params=(ctx["_nt"].sts[station]["ncdc81"], ),
        index_col="sday",
    )
    if cdf.empty:
        raise NoDataFound("No Data Found.")

    cursor.execute(
        """
        SELECT day, max_tmpf, min_tmpf, max_dwpf, min_dwpf,
        (max_tmpf + min_tmpf) / 2. as avg_tmpf,
        pday, avg_sknt from summary s JOIN stations t
        on (t.iemid = s.iemid) WHERE s.day >= %s and s.day <= %s and
        t.id = %s and t.network = %s ORDER by day ASC
    """,
        (sdate, edate, station, ctx["network"]),
    )
    rows = []
    data = {}
    for row in cursor:
        hd = row["max_tmpf"] - cdf.at[row[0].strftime("%m%d"), "high"]
        ld = row["min_tmpf"] - cdf.at[row[0].strftime("%m%d"), "low"]
        ad = row["avg_tmpf"] - cdf.at[row[0].strftime("%m%d"), "avg"]
        avg_sknt = row["avg_sknt"]
        if avg_sknt is None:
            if varname == "avg_smph":
                continue
            avg_sknt = 0
        rows.append(
            dict(
                day=row["day"],
                max_tmpf=row["max_tmpf"],
                avg_smph=(avg_sknt * units("knot")).to(units("mile / hour")).m,
                min_dwpf=row["min_dwpf"],
                max_dwpf=row["max_dwpf"],
                high_departure=hd,
                low_departure=ld,
                avg_departure=ad,
                min_tmpf=row["min_tmpf"],
                pday=row["pday"],
            ))
        data[row[0]] = {"val": safe(rows[-1], varname)}
        if data[row[0]]["val"] == "0":
            data[row[0]]["color"] = "k"
        elif varname == "high_departure":
            data[row[0]]["color"] = "b" if hd < 0 else "r"
        elif varname == "low_departure":
            data[row[0]]["color"] = "b" if ld < 0 else "r"
        elif varname == "avg_departure":
            data[row[0]]["color"] = "b" if ad < 0 else "r"
    df = pd.DataFrame(rows)

    title = "[%s] %s Daily %s" % (
        station,
        ctx["_nt"].sts[station]["name"],
        PDICT.get(varname),
    )
    subtitle = "%s thru %s" % (
        sdate.strftime("%-d %b %Y"),
        edate.strftime("%-d %b %Y"),
    )

    fig = calendar_plot(sdate, edate, data, title=title, subtitle=subtitle)
    return fig, df