示例#1
0
def _weather(depart_time, r):
    """
    :type depart_time: datetime.datetime
    :type r: pyticas.ttypes.Route
    :return:
    :rtype:
    """
    from_time = depart_time - datetime.timedelta(hours=1)
    prd = period.Period(from_time, depart_time, interval=300)
    stations = r.get_stations()
    n_stations = len(stations)
    center_station = stations[int(n_stations / 2)]
    sites = rwis.find_nearby_sites(center_station.lat, center_station.lon)
    nearby_site = sites[0]
    getLogger(__name__).debug(
        '  - RWIS station : site_id=%d, lat=%f, lon=%f, distance=%f' %
        (nearby_site.site_id, nearby_site.lat, nearby_site.lon,
         nearby_site.distance_to_target))

    wd = rwis.get_weather(nearby_site, prd)

    surface_status = wd.get_surface_statuses()

    if wd.is_rain(0.5) or (surface_status and wd._contains(
        ['rain', 'wet'], surface_status[-1], ['freezing'])):
        return WC_RAIN
    elif wd.is_snow(0.5) or (surface_status and wd._contains(
        ['snow', 'slush', 'ice', 'chemical wet', 'freezing'],
            surface_status[-1], ['freezing'])):
        return WC_SNOW
    else:
        return WC_NORMAL
示例#2
0
def _daily_period(date):
    """

    :type date: datetime.date
    :rtype:
    """
    sdt = _set_time_to_date(date, '00:00:00')
    edt = _set_time_to_date(date, '23:59:00')
    return period.Period(sdt, edt, cfg.TT_DATA_INTERVAL)
示例#3
0
def _get_period_for_a_day(dt):
    """
    :type dt: datetime.datetime
    :rtype:pyticas.ttypes.Period
    """
    date = dt.date()
    stime = datetime.time(0, 0, 0)
    etime = datetime.time(23, 59, 59)
    return period.Period(datetime.datetime.combine(date, stime),
                         datetime.datetime.combine(date, etime),
                         cfg.TT_DATA_INTERVAL)
def _retrieve_data_from_db(route_id, operating_conditions, sdate, edate, start_time, end_time, target_days, remove_holiday, **kwargs):
    """
    :type route_id: int
    :type operating_conditions: list[pyticas_tetres.rengine.filter.ExtFilterGroup]
    :type sdate: datetime.datetime
    :type edate: datetime.datetime
    :type start_time: datetime.time
    :type end_time: datetime.time
    :type target_days: list[int]
    :type remove_holiday: bool
    """
    # logger = getLogger(__name__)
    prd = period.Period(sdate, edate, cfg.TT_DATA_INTERVAL)
    # proc_start_time = time.time()
    # logger.debug('>>>> retrieving data for %s' % prd.get_date_string())

    year = sdate.year

    da_tt = tt.TravelTimeDataAccess(year)

    # generator
    traveltimes = da_tt.list_by_period(route_id, prd, start_time=start_time, end_time=end_time, weekdays=target_days,
                                       as_model=True)
    """:type: list[pyticas_tetres.db.model.TravelTime] """

    for ttm in traveltimes:

        dt = str2datetime(ttm.time)

        if remove_holiday and period.is_holiday(dt.date()):
            continue

        _tt_weathers = list(ttm._tt_weathers)
        _tt_incidents = list(ttm._tt_incidents)
        _tt_workzones = list(ttm._tt_workzones)
        _tt_specialevents = list(ttm._tt_specialevents)
        _tt_snowmanagements = list(ttm._tt_snowmanagements)

        if not _tt_weathers:
            getLogger(__name__).warning('No weather data for route(%d) at %s' % (route_id, dt.strftime('%Y-%m-%d %H:%M')))
            continue

        extdata = ExtData(ttm, _tt_weathers[0], _tt_incidents, _tt_workzones, _tt_specialevents, _tt_snowmanagements)

        for fidx, ef in enumerate(operating_conditions):
            try:
                ef.check(extdata)
            except Exception as ex:
                tb.traceback(ex)
                continue
示例#5
0
def _find_last_date(current_year):
    for y in range(current_year, cfg.DATA_ARCHIVE_START_YEAR - 1, -1):
        tt_da = TravelTimeDataAccess(y)
        year_prd = period.Period(datetime.datetime(y, 1, 1, 0, 0, 0),
                                 datetime.datetime(y, 12, 31, 23, 59, 59),
                                 cfg.TT_DATA_INTERVAL)

        items = tt_da.list_by_period(None,
                                     year_prd,
                                     limit=1,
                                     order_by=('time', 'desc'))
        if not items:
            continue
        return datetime.datetime.strptime(items[0].time, '%Y-%m-%d %H:%M:%S')

    return None
def _get_yearly_periods():
    stime = datetime.time(0, 0, 0)
    etime = datetime.time(23, 59, 59)
    periods = []
    for y in _all_years():
        first_day = datetime.date(y, 1, 1)
        last_day = datetime.date(y, 12, 31)
        prd = period.Period(datetime.datetime.combine(first_day, stime),
                            datetime.datetime.combine(last_day, etime),
                            cfg.TT_DATA_INTERVAL)
        periods.append(prd)

    last_day = datetime.date.today() - datetime.timedelta(days=cfg.DAILY_JOB_OFFSET_DAYS)
    last_prd = periods[-1]
    if last_prd.end_date.date() > last_day:
        last_prd.end_date = datetime.datetime.combine(last_day, etime)

    return periods
def _get_daily_periods_for_a_month(year, month):
    """

    :type year: int
    :type month: int
    :rtype: list[pyticas.ttypes.Period]
    """
    stime = datetime.time(0, 0, 0)
    etime = datetime.time(23, 59, 59)
    periods = []
    (weekday, last_date) = calendar.monthrange(year, month)
    last_day = datetime.date.today() - datetime.timedelta(days=cfg.DAILY_JOB_OFFSET_DAYS)
    for day in range(1, last_date):
        the_day = datetime.date(year, month, day)
        if the_day > last_day:
            break

        prd = period.Period(datetime.datetime.combine(the_day, stime),
                            datetime.datetime.combine(the_day, etime),
                            cfg.TT_DATA_INTERVAL)
        periods.append(prd)

    return periods
def _get_monthly_periods_for_a_year(year):
    """

    :type year: int
    :rtype: list[pyticas.ttypes.Period]
    """
    stime = datetime.time(0, 0, 0)
    etime = datetime.time(23, 59, 59)
    periods = []
    for month in range(1, 13):
        (weekday, last_date) = calendar.monthrange(year, month)
        first_day = datetime.date(year, month, 1)
        last_day = datetime.date(year, month, last_date)
        prd = period.Period(datetime.datetime.combine(first_day, stime),
                            datetime.datetime.combine(last_day, etime),
                            cfg.TT_DATA_INTERVAL)
        periods.append(prd)

    last_day = datetime.date.today() - datetime.timedelta(days=cfg.DAILY_JOB_OFFSET_DAYS)
    last_prd = periods[-1]
    if last_prd.end_date.date() > last_day:
        last_prd.end_date = datetime.datetime.combine(last_day, etime)

    return periods
示例#9
0
def traveltime_info(ttr_id, weather_type, depart_time, dbsession=None):
    """

    :type ttr_id: int
    :type weather_type: int
    :type depart_time: datetime.datetime
    :rtype: list[dict], list[float]
    """
    logger = getLogger(__name__)
    logger.debug(
        '# public travel time information is requested (route_id=%s, weather_type=%s, depart_time=%s)'
        % (ttr_id, weather_type, depart_time))

    ttrda = TTRouteDataAccess(session=dbsession)
    ttri = ttrda.get_by_id(ttr_id)

    if weather_type:
        try:
            weather_type = int(weather_type)
        except:
            weather_type = None

    if not weather_type or weather_type not in [WC_NORMAL, WC_RAIN, WC_SNOW]:
        weather_type = _weather(depart_time, ttri.route)

    regime_type = _regime_type(weather_type, depart_time)

    logger.debug('  > regime type = %d (%s)' %
                 (regime_type, REGIME_STRING[regime_type]))

    da = TODReliabilityDataAccess(session=ttrda.get_session())

    tods = da.list_by_route(ttr_id, regime_type)
    res = []
    dbg_from, dbg_to = 60, len(tods) - 12
    for idx, tod in enumerate(tods):
        tod_res = json.loads(tod.result)
        if not tod_res:
            continue
        if idx >= dbg_from and idx < dbg_to:
            logger.debug(
                '   -  time=%02d:%02d, avg_tt=%s, 95%%p_tt=%s, count=%s' %
                (tod.hour, tod.minute, tod_res['avg_tt'],
                 tod_res['percentile_tts']['95'], tod_res['count']))

        res.append({
            'hour': tod.hour,
            'minute': tod.minute,
            'avg_tt': _roundup(tod_res['avg_tt']),
            'p95_tt': _roundup(tod_res['percentile_tts']['95']),
            'p90_tt': _roundup(tod_res['percentile_tts']['90']),
            'p85_tt': _roundup(tod_res['percentile_tts']['85']),
            'p80_tt': _roundup(tod_res['percentile_tts']['80']),
            'count': tod_res['count']
        })

    today_to = depart_time
    now = datetime.datetime.now()

    if today_to >= now:
        today_to = now

    # 5 minute interval
    delta = (today_to.minute -
             math.floor(today_to.minute / 5) * 5) * 60 + today_to.second
    today_to = today_to - datetime.timedelta(seconds=delta)

    try:
        today_from = datetime.datetime.combine(today_to.date(),
                                               datetime.time(0, 0, 0))
        prd = period.Period(today_from, today_to, cfg.TT_DATA_INTERVAL)
        tts = moe.travel_time(ttri.route, prd)
        tts = moe.imputation(tts, imp_module=time_avg)
        traveltimes = _moving_average(tts[-1].data, 5)

    except Exception as ex:
        getLogger(__name__).warn('error to calculate travel times')
        traveltimes = []

    traveltimes = _roundup(traveltimes)

    ttrda.close_session()

    return res[60:-12], traveltimes[60:]