• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python util.get_autoplot_context函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pyiem.util.get_autoplot_context函数的典型用法代码示例。如果您正苦于以下问题:Python get_autoplot_context函数的具体用法?Python get_autoplot_context怎么用?Python get_autoplot_context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了get_autoplot_context函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_get_autoplot_context

def test_get_autoplot_context():
    """See that we can do things."""
    form = dict(station='AMW', network='IA_ASOS', type2='bogus',
                t=15, type3=['max-high', 'bogus', 'min-high'])
    form['type'] = 'max-low'
    pdict = OrderedDict([
                            ('max-high', 'Maximum High'),
                            ('avg-high', 'Average High'),
                            ('min-high', 'Minimum High'),
                            ('max-low', 'Maximum Low')])
    cfg = dict(arguments=[
        dict(type='station', name='station', default='IA0000'),
        dict(type='select', name='type', default='max-high',
             options=pdict),
        dict(type='select', name='type2', default='max-high',
             options=pdict),
        dict(type='select', name='type3', default='max-high',
             options=pdict, multiple=True),
        dict(type='select', name='type4', default='max-high',
             options=pdict, multiple=True, optional=True),
        dict(type='select', name='type5', default='max-high',
             options=pdict),
        dict(type='int', name='threshold', default=-99),
        dict(type='int', name='t', default=9, min=0, max=10),
        dict(type='date', name='d', default='2011/11/12'),
        dict(type='datetime', name='d2', default='2011/11/12 0000',
             max='2017/12/12 1212', min='2011/01/01 0000'),
        dict(type='year', name='year', default='2011', optional=True),
        dict(type='float', name='f', default=1.10)])
    ctx = util.get_autoplot_context(form, cfg)
    assert ctx['station'] == 'AMW'
    assert ctx['network'] == 'IA_ASOS'
    assert isinstance(ctx['threshold'], int)
    assert ctx['type'] == 'max-low'
    assert ctx['type2'] == 'max-high'
    assert isinstance(ctx['f'], float)
    assert ctx['t'] == 9
    assert ctx['d'] == datetime.date(2011, 11, 12)
    assert ctx['d2'] == datetime.datetime(2011, 11, 12)
    assert 'year' not in ctx
    assert 'bogus' not in ctx['type3']
    assert 'type4' not in ctx

    form = dict(zstation='DSM')
    cfg = dict(arguments=[
        dict(type='zstation', name='station', default='DSM',
             network='IA_ASOS')])
    ctx = util.get_autoplot_context(form, cfg)
    assert ctx['network'] == 'IA_ASOS'
开发者ID:akrherz,项目名称:pyIEM,代码行数:49,代码来源:test_util.py


示例2: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    ctx = get_autoplot_context(fdict, get_description())
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    station = ctx['station']
    nt = NetworkTable("CSCAP")
    clstation = nt.sts[station]['climate_site']
    (model, scenario) = ctx['model'].split("=")

    (fig, ax) = plt.subplots(1, 1)

    cursor.execute("""
    SELECT extract(year from day) as yr, sum(case when precip > 0
    THEN 1 else 0 end) from hayhoe_daily WHERE precip is not null and
    station = %s and model = %s and scenario = %s
    GROUP by yr ORDER by yr ASC
    """, (clstation, model, scenario))
    years = []
    precip = []
    for row in cursor:
        years.append(row[0])
        precip.append(row[1])

    ax.bar(years, precip, ec='b', fc='b')
    ax.grid(True)
    ax.set_ylabel("Days Per Year")
    ax.set_title("%s %s\n%s %s :: Days per Year with Measurable Precip" % (
            station, nt.sts[station]['name'], model,
            scenario))
    return fig
开发者ID:akrherz,项目名称:iem,代码行数:35,代码来源:p50.py


示例3: test_vtecps

def test_vtecps():
    """Can we properly handle the vtecps form type"""
    cfg = dict(arguments=[
        dict(type='vtec_ps', name='v1', default='TO.W',
             label='VTEC Phenomena and Significance 1'),
        dict(type='vtec_ps', name='v2', default='TO.A', optional=True,
             label='VTEC Phenomena and Significance 2'),
        dict(type='vtec_ps', name='v3', default=None, optional=True,
             label='VTEC Phenomena and Significance 3'),
        dict(type='vtec_ps', name='v4', default='FL.Y', optional=True,
             label='VTEC Phenomena and Significance 4'),
        dict(type='vtec_ps', name='v5', default='UNUSED', optional=True,
             label='VTEC Phenomena and Significance 5')])
    form = dict(phenomenav1='SV', significancev1='A',
                phenomenav4='TO', significancev4='W')
    ctx = util.get_autoplot_context(form, cfg)
    # For v1, we were explicitly provided by from the form
    assert ctx['phenomenav1'] == 'SV'
    assert ctx['significancev1'] == 'A'
    # For v2, optional is on, so our values should be None
    assert ctx.get('phenomenav2') is None
    # For v3, should be None as well
    assert ctx.get('phenomenav3') is None
    # For v4, we provided a value via form
    assert ctx['significancev4'] == 'W'
    # For v5, we have a bad default set
    assert ctx.get('phenomenav5') is None
开发者ID:akrherz,项目名称:pyIEM,代码行数:27,代码来源:test_util.py


示例4: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    days = ctx['days']
    nt = NetworkTable("CSCAP")
    clstation = nt.sts[station]['climate_site']
    (model, scenario) = ctx['model'].split("=")

    (fig, ax) = plt.subplots(1, 1)

    df = read_sql("""
    WITH data as (
    SELECT day, sum(precip) OVER (ORDER by day ASC ROWS BETWEEN %s preceding
    and current row) from hayhoe_daily WHERE precip is not null and
    station = %s and model = %s and scenario = %s
    )

    SELECT extract(year from day) as yr, sum(case when
     sum < 0.01 then 1 else 0 end) as precip
     from data WHERE extract(month from day) in
     (3,4,5,6,7,8) GROUP by yr ORDER by yr ASC
    """, pgconn, params=(days - 1, clstation, model, scenario), index_col='yr')

    ax.bar(df.index.values, df['precip'].values, ec='b', fc='b')
    ax.grid(True)
    ax.set_ylabel("Days Per Year")
    ax.set_title(("%s %s\n%s %s :: Spring/Summer with No Precip over %s days"
                  ) % (station, nt.sts[station]['name'], model,
                       scenario, days))
    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:35,代码来源:p51.py


示例5: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    ctx = get_autoplot_context(fdict, get_description())

    station = ctx['station']
    t1 = ctx['t1']
    t2 = ctx['t2']

    table = "alldata_%s" % (station[:2],)
    nt = NetworkTable("%sCLIMATE" % (station[:2],))

    df = read_sql("""
        SELECT year,
        min(low) as min_low,
        min(case when low < %s then extract(doy from day)
            else 999 end) as t1_doy,
        min(case when low < %s then extract(doy from day)
            else 999 end) as t2_doy
        from """+table+""" where station = %s and month > 6
        GROUP by year ORDER by year ASC
    """, pgconn, params=(t1, t2, station), index_col='year')
    df = df[df['t2_doy'] < 400]

    doy = np.array(df['t1_doy'], 'i')
    doy2 = np.array(df['t2_doy'], 'i')

    sts = datetime.datetime(2000, 1, 1)
    xticks = []
    xticklabels = []
    for i in range(min(doy), max(doy2)+1):
        ts = sts + datetime.timedelta(days=i)
        if ts.day in [1, 8, 15, 22]:
            xticks.append(i)
            fmt = "%b %-d" if ts.day == 1 else "%-d"
            xticklabels.append(ts.strftime(fmt))

    (fig, ax) = plt.subplots(1, 1)
    ax.set_xticks(xticks)
    ax.set_xticklabels(xticklabels)
    ax.scatter(doy, doy2-doy)

    for x in xticks:
        ax.plot((x-100, x), (100, 0), ':', c=('#000000'))

    ax.set_ylim(-1, max(doy2-doy)+4)
    ax.set_xlim(min(doy)-4, max(doy)+4)

    ax.set_title("[%s] %s\nFirst Fall Temperature Occurences" % (
                                            station, nt.sts[station]['name']))
    ax.set_ylabel("Days until first sub %s$^{\circ}\mathrm{F}$" % (t2,))
    ax.set_xlabel("First day of sub %s$^{\circ}\mathrm{F}$" % (t1,))

    ax.grid(True)

    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:59,代码来源:p27.py


示例6: get_context

def get_context(fdict):
    pgconn = psycopg2.connect(database='hads', host='iemdb-hads',
                              user='nobody')
    cursor = pgconn.cursor()
    ctx = get_autoplot_context(fdict, get_description())

    ctx['station'] = ctx['station'].upper()
    station = ctx['station']
    dt = ctx['dt']

    # Attempt to get station information
    cursor.execute("""
    SELECT name from stations where id = %s and network ~* 'DCP'
    """, (station,))
    ctx['name'] = ""
    if cursor.rowcount > 0:
        row = cursor.fetchone()
        ctx['name'] = row[0]

    ctx['df'] = read_sql("""with fx as (
        select id, issued, primaryname, primaryunits, secondaryname,
        secondaryunits from hml_forecast where station = %s
        and generationtime between %s and %s)
    SELECT f.id, f.issued, d.valid, d.primary_value, f.primaryname,
    f.primaryunits, d.secondary_value, f.secondaryname,
    f.secondaryunits from
    hml_forecast_data_""" + str(dt.year) + """ d JOIN fx f
    on (d.hml_forecast_id = f.id) ORDER by f.id ASC, d.valid ASC
    """, pgconn, params=(station, dt - datetime.timedelta(days=3),
                         dt + datetime.timedelta(days=1)), index_col=None)
    if len(ctx['df'].index) > 0:
        ctx['primary'] = "%s[%s]" % (ctx['df'].iloc[0]['primaryname'],
                                     ctx['df'].iloc[0]['primaryunits'])
        ctx['secondary'] = "%s[%s]" % (ctx['df'].iloc[0]['secondaryname'],
                                       ctx['df'].iloc[0]['secondaryunits'])

        # get obs
        mints = ctx['df']['valid'].min()
        maxts = ctx['df']['valid'].max()
    else:
        mints = dt - datetime.timedelta(days=3)
        maxts = dt + datetime.timedelta(days=3)
    df = read_sql("""
    SELECT valid, h.label, value
    from hml_observed_data_""" + str(dt.year) + """ d JOIN hml_observed_keys h
    on (d.key = h.id)
    WHERE station = %s and valid between %s and %s ORDER by valid
    """, pgconn, params=(station, mints, maxts), index_col=None)
    ctx['odf'] = df.pivot('valid', 'label', 'value')
    if len(ctx['df'].index) > 0:
        ctx['df'] = pd.merge(ctx['df'], ctx['odf'], left_on='valid',
                             right_index=True, how='left', sort=False)
    ctx['title'] = "[%s] %s" % (ctx['station'], ctx['name'])
    ctx['subtitle'] = ctx['dt'].strftime("%d %b %Y %H:%M UTC")
    if len(ctx['df'].index) == 0 and len(ctx['odf'].index) > 0:
        ctx['primary'] = ctx['odf'].columns[0]
        ctx['secondary'] = ctx['odf'].columns[1]
    return ctx
开发者ID:akrherz,项目名称:iem,代码行数:58,代码来源:p160.py


示例7: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='asos', host='iemdb', user='nobody')
    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
开发者ID:akrherz,项目名称:iem,代码行数:57,代码来源:p138.py


示例8: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='asos', host='iemdb', user='nobody')

    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['zstation']
    network = ctx['network']
    month = int(ctx['month'])
    thres = ctx['t']
    mydir = ctx['dir']

    nt = NetworkTable(network)
    tzname = nt.sts[station]['tzname']

    df = read_sql("""
    WITH data as (
        SELECT valid at time zone %s  + '10 minutes'::interval as v, tmpf
        from alldata where station = %s and tmpf > -90 and tmpf < 150
        and extract(month from valid) = %s and report_type = 2)

    SELECT extract(hour from v) as hour,
    sum(case when tmpf::int < %s THEN 1 ELSE 0 END) as below,
    sum(case when tmpf::int >= %s THEN 1 ELSE 0 END) as above,
    count(*) from data
    GROUP by hour ORDER by hour ASC
    """, pgconn, params=(tzname, station, month, thres, thres),
                  index_col='hour')

    df['below_freq'] = df['below'].values.astype('f') / df['count'] * 100.
    df['above_freq'] = df['above'].values.astype('f') / df['count'] * 100.

    freq = df[mydir+"_freq"].values
    hours = df.index.values

    (fig, ax) = plt.subplots(1, 1)
    bars = ax.bar(hours-0.4, freq, fc='blue')
    for i, bar in enumerate(bars):
        ax.text(i, bar.get_height()+3, "%.0f" % (bar.get_height(),),
                ha='center', fontsize=10)
    ax.set_xticks(range(0, 25, 3))
    ax.set_xticklabels(['Mid', '3 AM', '6 AM', '9 AM', 'Noon', '3 PM',
                        '6 PM', '9 PM'])
    ax.grid(True)
    ax.set_ylim(0, 100)
    ax.set_yticks([0, 25, 50, 75, 100])
    ax.set_ylabel("Frequency [%]")
    ax.set_xlabel("Hour Timezone: %s" % (tzname,))
    ax.set_xlim(-0.5, 23.5)
    ax.set_title(("%s [%s]\nFrequency of %s Hour, %s: %s$^\circ$F"
                  ) % (nt.sts[station]['name'], station,
                       calendar.month_name[month], PDICT[mydir],
                       thres))

    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:57,代码来源:p85.py


示例9: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    network = ctx['network']
    varname = ctx['var']
    thedate = ctx['thedate']
    date = ctx['date']

    nt = NetworkTable(network)
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')

    table = "alldata_%s" % (station[:2], )
    if date == 'exact':
        df = read_sql("""
        SELECT year, high, day, precip from """ + table + """
        WHERE station = %s
        and sday = %s ORDER by year ASC
        """, pgconn, params=(station, thedate.strftime("%m%d")),
                      index_col='year')
        subtitle = thedate.strftime("%B %-d")
    else:
        if date == 'memorial':
            days = memorial_days()
        elif date == 'thanksgiving':
            days = thanksgiving()
        else:
            days = labor_days()

        df = read_sql("""
        SELECT year, high, day, precip from """ + table + """
        WHERE station = %s
        and day in %s ORDER by year ASC
        """, pgconn, params=(station, tuple(days)),
                      index_col='year')
        subtitle = PDICT[date]

    (fig, ax) = plt.subplots(1, 1)

    ax.bar(df.index.values, df[varname], fc='r', ec='r', align='center')
    mean = df[varname].mean()
    ax.axhline(mean)
    ax.text(df.index.values[-1] + 1, mean, '%.2f' % (mean,), ha='left',
            va='center')
    ax.grid(True)
    ax.set_title(("%s [%s] Daily %s\non %s"
                  ) % (nt.sts[station]['name'], station, PDICT2[varname],
                       subtitle))
    ax.set_xlim(df.index.values.min() - 1,
                df.index.values.max() + 1)
    ax.set_ylabel(PDICT2[varname])
    if varname != 'precip':
        ax.set_ylim(df[varname].min() - 5, df[varname].max() + 5)
    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:57,代码来源:p148.py


示例10: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    ASOS = psycopg2.connect(database='asos', host='iemdb', user='nobody')
    cursor = ASOS.cursor(cursor_factory=psycopg2.extras.DictCursor)
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['zstation']
    network = ctx['network']
    nt = NetworkTable(network)

    cursor.execute("""
    WITH obs as (select valid at time zone %s + '10 minutes'::interval as v,
    tmpf from alldata
    WHERE station = %s and tmpf >= -90 and tmpf < 150),
    s as (SELECT generate_series(0, 23, 1) || ' hours' as series),
    daily as (select s.series, v + s.series::interval as t, tmpf from obs, s),
    sums as (select series, date(t), max(tmpf), min(tmpf) from daily
    GROUP by series, date)

    SELECT series, avg(max), avg(min) from sums GROUP by series
    """, (nt.sts[station]['tzname'], station))

    rows = []
    hrs = range(25)
    highs = [None]*25
    lows = [None]*25
    for row in cursor:
        i = int(row[0].split()[0])
        highs[24-i] = row[1]
        lows[24-i] = row[2]
        rows.append(dict(offset=(24-i), avg_high=row[1], avg_low=row[2]))
    rows.append(dict(offset=0, avg_high=highs[24], avg_low=lows[24]))
    highs[0] = highs[24]
    lows[0] = lows[24]
    df = pd.DataFrame(rows)
    (fig, ax) = plt.subplots(1, 1)
    ax.plot(hrs, np.array(highs) - highs[0], label="High Temp", lw=2,
            color='r')
    ax.plot(hrs, np.array(lows) - lows[0], label="Low Temp", lw=2,
            color='b')
    ax.set_title(("[%s] %s %s-%s\n"
                  "Bias of 24 Hour 'Day' Split for Average High + Low Temp"
                  ) % (station, nt.sts[station]['name'],
                       nt.sts[station]['archive_begin'].year,
                       datetime.date.today().year))
    ax.set_ylabel("Average Temperature Difference $^\circ$F")
    ax.set_xlim(0, 24)
    ax.set_xticks((0, 4, 8, 12, 16, 20, 24))
    ax.set_xticklabels(('Mid', '4 AM', '8 AM', 'Noon', '4 PM', '8 PM', 'Mid'))
    ax.grid(True)
    ax.set_xlabel("Hour Used for 24 Hour Summary")

    ax.legend(loc='best')
    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:56,代码来源:p94.py


示例11: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    ctx = get_autoplot_context(fdict, get_description())

    station = ctx['station']

    table = "alldata_%s" % (station[:2], )
    nt = NetworkTable("%sCLIMATE" % (station[:2], ))
    CATS = np.array([0.01, 0.5, 1., 2., 3., 4.])

    startyear = nt.sts[station]['archive_begin'].year
    # 0.01, 0.5, 1, 2, 3, 4
    df = read_sql("""
        SELECT year, month,
        sum(case when precip >= %s then 1 else 0 end) as cat1,
        sum(case when precip >= %s then 1 else 0 end) as cat2,
        sum(case when precip >= %s then 1 else 0 end) as cat3,
        sum(case when precip >= %s then 1 else 0 end) as cat4,
        sum(case when precip >= %s then 1 else 0 end) as cat5,
        sum(case when precip >= %s then 1 else 0 end) as cat6
        from """ + table + """ WHERE station = %s GROUP by year, month
        ORDER by year, month
    """, pgconn, params=(CATS[0], CATS[1], CATS[2], CATS[3], CATS[4],
                         CATS[5], station), index_col=['year', 'month'])

    res = """\
# IEM Climodat https://mesonet.agron.iastate.edu/climodat/
# Report Generated: %s
# Climate Record: %s -> %s
# Site Information: [%s] %s
# Contact Information: Daryl Herzmann [email protected] 515.294.5978
# Number of days per year with precipitation at or above threshold [inch]
# Partitioned by month of the year, 'ANN' represents the entire year
""" % (datetime.date.today().strftime("%d %b %Y"),
       nt.sts[station]['archive_begin'].date(), datetime.date.today(), station,
       nt.sts[station]['name'])

    for i, cat in enumerate(CATS):
        col = "cat%s" % (i+1,)
        res += ("YEAR %4.2f JAN FEB MAR APR MAY JUN "
                "JUL AUG SEP OCT NOV DEC ANN\n") % (cat,)
        for yr in range(startyear, datetime.date.today().year + 1):
            res += "%s %4.2f " % (yr, cat)
            for mo in range(1, 13):
                if (yr, mo) in df.index:
                    res += "%3.0f " % (df.at[(yr, mo), col], )
                else:
                    res += "%3s " % ('M', )
            res += "%3.0f\n" % (df.loc[(yr, slice(1, 12)), col].sum(), )

    return None, df, res
开发者ID:akrherz,项目名称:iem,代码行数:54,代码来源:p124.py


示例12: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import calendar_plot
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='nobody')
    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='nobody'),
                   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
开发者ID:akrherz,项目名称:iem,代码行数:54,代码来源:p82.py


示例13: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    from matplotlib.ticker import MaxNLocator
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    network = ctx['network']
    days = ctx['days']
    varname = ctx['var']
    nt = NetworkTable(network)
    df = get_data(fdict)
    if len(df.index) == 0:
        return 'Error, no results returned!'

    ax = plt.axes([0.1, 0.3, 0.8, 0.6])
    lax = plt.axes([0.1, 0.1, 0.8, 0.2])
    title = PDICT.get(varname)
    if days == 1:
        title = title.replace("Average ", "")
    ax.set_title(("%s [%s]\n%i Day Period with %s"
                  ) % (nt.sts[station]['name'], station, days, title))
    ax.barh(df.index.values, [days]*len(df.index), left=df['doy'].values,
            edgecolor='tan', facecolor='tan')
    ax.grid(True)
    lax.grid(True)
    xticks = []
    xticklabels = []
    for i in np.arange(df['doy'].min() - 5, df['doy'].max() + 5, 1):
        ts = datetime.datetime(2000, 1, 1) + datetime.timedelta(days=i)
        if ts.day == 1:
            xticks.append(i)
            xticklabels.append(ts.strftime("%-d %b"))
    ax.set_xticks(xticks)
    lax.set_xticks(xticks)
    lax.set_xticklabels(xticklabels)

    counts = np.zeros(366*2)
    for _, row in df.iterrows():
        counts[row['doy']:row['doy']+days] += 1

    lax.bar(np.arange(366*2), counts, edgecolor='blue', facecolor='blue')
    lax.set_ylabel("Years")
    lax.text(0.02, 0.9, "Frequency of Day\nwithin period",
             transform=lax.transAxes, va='top')
    ax.set_ylim(df.index.values.min() - 3, df.index.values.max() + 3)

    ax.set_xlim(df['doy'].min() - 10, df['doy'].max() + 10)
    lax.set_xlim(df['doy'].min() - 10, df['doy'].max() + 10)
    ax.yaxis.set_major_locator(MaxNLocator(prune='lower'))
    return plt.gcf(), df
开发者ID:akrherz,项目名称:iem,代码行数:52,代码来源:p134.py


示例14: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    COOP = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    varname = ctx['var']
    month = ctx['month']
    threshold = float(ctx['thres'])
    if PDICT.get(varname) is None:
        return
    drct = ctx['dir']
    if PDICT2.get(drct) is None:
        return
    operator = ">=" if drct == 'above' else '<'
    table = "alldata_%s" % (station[:2],)
    nt = network.Table("%sCLIMATE" % (station[:2],))

    df = read_sql("""
        SELECT sday,
        sum(case when """+varname+""" """+operator+""" %s then 1 else 0 end)
        as hit,
        count(*) as total
        from """+table+""" WHERE station = %s and month = %s
        GROUP by sday ORDER by sday ASC
        """, COOP, params=(threshold, station, month), index_col='sday')
    df['freq'] = df['hit'] / df['total'] * 100.

    fig, ax = plt.subplots(1, 1)
    bars = ax.bar(np.arange(1, len(df.index)+1)-0.4, df['freq'])
    for i, bar in enumerate(bars):
        ax.text(i+1, bar.get_height() + 0.3, '%s' % (df['hit'][i],),
                ha='center')
    msg = ("[%s] %s %s %s %s during %s (Avg: %.2f days/year)"
           ) % (station, nt.sts[station]['name'], PDICT.get(varname),
                PDICT2.get(drct), threshold, calendar.month_abbr[month],
                df['hit'].sum() / float(df['total'].sum()) * len(df.index))
    tokens = msg.split()
    sz = len(tokens) / 2
    ax.set_title(" ".join(tokens[:sz]) + "\n" + " ".join(tokens[sz:]))
    ax.set_ylabel("Frequency (%)")
    ax.set_xlabel(("Day of %s, years (out of %s) meeting criteria labelled"
                   ) % (calendar.month_name[month], np.max(df['total'],)))
    ax.grid(True)
    ax.set_xlim(0.5, 31.5)
    ax.set_ylim(0, df['freq'].max() + 5)

    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:50,代码来源:p98.py


示例15: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    from pyiem.plot import MapPlot
    bins = [0, 1, 14, 31, 91, 182, 273, 365, 730, 1460, 2920, 3800]
    pgconn = psycopg2.connect(database='postgis', host='iemdb', user='nobody')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    ctx = get_autoplot_context(fdict, get_description())
    phenomena = ctx['phenomena']
    significance = ctx['significance']
    edate = ctx.get('edate')
    if edate is None:
        edate = datetime.datetime.utcnow()
    else:
        edate = datetime.datetime(edate.year, edate.month,
                                  edate.day, 0, 0)
    edate = edate.replace(tzinfo=pytz.timezone("UTC"))

    cursor.execute("""
     select wfo,  extract(days from (%s::date - max(issue))) as m
     from warnings where significance = %s and phenomena = %s
     and issue < %s
     GROUP by wfo ORDER by m ASC
    """, (edate, significance, phenomena, edate))
    if cursor.rowcount == 0:
        return ("No Events Found for %s %s (%s.%s)"
                ) % (vtec._phenDict.get(phenomena, phenomena),
                     vtec._sigDict.get(significance, significance),
                     phenomena, significance)
    data = {}
    rows = []
    for row in cursor:
        wfo = row[0] if row[0] != 'JSJ' else 'SJU'
        rows.append(dict(wfo=wfo, days=row[1]))
        data[wfo] = max([row[1], 0])
    df = pd.DataFrame(rows)
    df.set_index('wfo', inplace=True)

    m = MapPlot(sector='nws', axisbg='white', nocaption=True,
                title='Days since Last %s %s by NWS Office' % (
                        vtec._phenDict.get(phenomena, phenomena),
                        vtec._sigDict.get(significance, significance)),
                subtitle='Valid %s' % (edate.strftime("%d %b %Y %H%M UTC"),))
    m.fill_cwas(data, bins=bins, ilabel=True, units='Days',
                lblformat='%.0f')

    return m.fig, df
开发者ID:akrherz,项目名称:iem,代码行数:48,代码来源:p92.py


示例16: get_context

def get_context(fdict):
    pgconn = psycopg2.connect(database='iem', host='iemdb', user='nobody')
    ctx = get_autoplot_context(fdict, get_description())

    station = ctx['zstation']
    network = ctx['network']
    month = ctx['month']
    varname = ctx['var']
    mydir = ctx['dir']
    threshold = ctx['thres']

    ctx['nt'] = NetworkTable(network)

    offset = 'day'
    if month == 'all':
        months = range(1, 13)
    elif month == 'fall':
        months = [9, 10, 11]
    elif month == 'winter':
        months = [12, 1, 2]
        offset = "day + '1 month'::interval"
    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]

    opp = ">=" if mydir == 'aoa' else '<'
    ctx['df'] = read_sql("""
        SELECT extract(year from """ + offset + """)::int as year,
        sum(case when """ + varname + """ """ + opp + """ %s then 1 else 0 end)
        as count
        from summary s JOIN stations t on (s.iemid = t.iemid)
        WHERE t.id = %s and t.network = %s and extract(month from day) in %s
        GROUP by year ORDER by year ASC
        """, pgconn, params=(threshold, station, network,
                             tuple(months)),
                         index_col='year')
    ctx['title'] = "(%s) %s %s %.0f" % (MDICT[ctx['month']],
                                        METRICS[ctx['var']],
                                        DIRS[ctx['dir']],
                                        ctx['thres'])
    ctx['subtitle'] = "%s [%s]" % (ctx['nt'].sts[ctx['zstation']]['name'],
                                   ctx['zstation'])
    return ctx
开发者ID:akrherz,项目名称:iem,代码行数:48,代码来源:p161.py


示例17: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    threshold = float(ctx['threshold'])

    table = "alldata_%s" % (station[:2],)
    nt = NetworkTable("%sCLIMATE" % (station[:2],))

    cursor.execute("""
         WITH monthly as (
         SELECT year, month, max(precip), sum(precip) from """+table+"""
         WHERE station = %s and precip is not null GROUP by year, month)

         SELECT month, sum(case when max > (sum * %s) then 1 else 0 end),
         count(*) from monthly GROUP by month ORDER by month ASC
         """, (station, threshold / 100.))
    df = pd.DataFrame(dict(freq=pd.Series(), events=pd.Series(),
                           month=pd.Series(calendar.month_abbr[1:],
                                           index=range(1, 13))),
                      index=pd.Series(range(1, 13), name='mo'))
    for row in cursor:
        df.at[row[0], 'events'] = row[1]
        df.at[row[0], 'freq'] = row[1] / float(row[2]) * 100.

    (fig, ax) = plt.subplots(1, 1)

    ax.bar(df.index - 0.4, df.freq)
    for i, row in df.iterrows():
        ax.text(i, row['freq']+2, "%.1f%%" % (row['freq'],), ha='center')
    ax.set_title(("[%s] %s\nFreq of One Day Having %.0f%% of That Month's "
                  "Precip Total"
                  ) % (station, nt.sts[station]['name'], threshold))
    ax.grid(True)
    ax.set_xlim(0.5, 12.5)
    ax.set_ylim(0, 100)
    ax.set_ylabel("Percentage of Years")
    ax.set_yticks([0, 10, 25, 50, 75, 90, 100])
    ax.set_xticklabels(calendar.month_abbr[1:])
    ax.set_xticks(range(1, 13))

    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:47,代码来源:p58.py


示例18: plotter

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='coop', host='iemdb', user='nobody')
    ctx = get_autoplot_context(fdict, get_description())
    station = ctx['station']
    month = ctx['month']
    year = ctx['year']

    table = "alldata_%s" % (station[:2],)
    nt = NetworkTable("%sCLIMATE" % (station[:2],))

    # beat month
    df = read_sql("""
    SELECT year, sum(precip) as precip, sum(snow) as snow from """+table+"""
    WHERE station = %s and month = %s and precip >= 0
    and snow >= 0 GROUP by year ORDER by year ASC
    """, pgconn, params=(station, month), index_col='year')

    (fig, ax) = plt.subplots(1, 1)

    ax.scatter(df['precip'], df['snow'], s=40, marker='s', color='b', zorder=2)
    if year in df.index:
        row = df.loc[year]
        ax.scatter(row['precip'], row['snow'], s=60,
                   marker='o', color='r', zorder=3, label=str(year))
    ax.set_title(("[%s] %s\n%s Snowfall vs Precipitation Totals"
                  ) % (station, nt.sts[station]['name'],
                       calendar.month_name[month]))
    ax.grid(True)
    ax.axhline(df['snow'].mean(), lw=2, color='black')
    ax.axvline(df['precip'].mean(), lw=2, color='black')

    ax.set_xlim(left=-0.1)
    ax.set_ylim(bottom=-0.1)
    ylim = ax.get_ylim()
    ax.text(df['precip'].mean(), ylim[1], "%.2f" % (df['precip'].mean(),),
            va='top', ha='center', color='white', bbox=dict(color='black'))
    xlim = ax.get_xlim()
    ax.text(xlim[1], df['snow'].mean(),  "%.1f" % (df['snow'].mean(),),
            va='center', ha='right', color='white', bbox=dict(color='black'))
    ax.set_ylabel("Snowfall Total [inch]")
    ax.set_xlabel("Precipitation Total (liquid + melted) [inch]")
    ax.legend(loc=2, scatterpoints=1)
    return fig, df
开发者ID:akrherz,项目名称:iem,代码行数:47,代码来源:p47.py


示例19: get_context

def get_context(fdict):
    pgconn = psycopg2.connect(database='talltowers',
                              host='talltowers-db.local', user='tt_web')
    ctx = get_autoplot_context(fdict, get_description())
    dt = ctx['dt']
    station = ctx['station']
    minutes = ctx['minutes']
    nt = NetworkTable("TALLTOWERS")
    towerid = nt.sts[station]['remote_id']

    ctx['df'] = read_sql("""
    SELECT * from data_analog where tower = %s and
    valid between %s and %s ORDER by valid ASC
    """, pgconn, params=(towerid, dt,
                         dt + datetime.timedelta(minutes=minutes)),
                         index_col='valid')
    return ctx
开发者ID:akrherz,项目名称:iem,代码行数:17,代码来源:p158.py



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python util.get_dbconn函数代码示例发布时间:2022-05-25
下一篇:
Python plot.MapPlot类代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap