Exemple #1
0
def pickle_warnings(regions, date_from, date_to, pickle_file_name):
    '''All forecasted warnings and problems are selected from regObs or the avalanche api.
    Dangers and problems are connected and neatly pickel'd for later use.

    :param regions:            list [int] RegionID as given in regObs [101-199]
    :param date_from:          string as 'yyyy-mm-dd'
    :param date_to:            string as 'yyyy-mm-dd'
    :param pickle_file_name:   filename including directory as string
    :return:
    '''

    warnings = []

    for r in regions:

        # get all warning and problems for this region and then loop though them joining them where dates match.
        region_warnings = gfa.get_warnings(r, date_from, date_to)
        name = gro.get_forecast_region_name(r)
        problems = gro.get_problems_from_AvalancheWarnProblemV(r, date_from, date_to)

        print 'runmatrix.py -> pickle_warnings: {0} problems found for {1}'.format(len(problems), name)

        for i in range(0, len(region_warnings), 1):
            for j in range(0, len(problems), 1):
                if region_warnings[i].date == problems[j].date:
                    region_warnings[i].add_problem(problems[j])

        warnings += region_warnings

    # make sure all problems are ordered from lowest id (main problem) to largest.
    for w in warnings:
        w.avalanche_problems = sorted(w.avalanche_problems, key=lambda AvalancheProblem: AvalancheProblem.order)

    mp.pickle_anything(warnings, pickle_file_name)
Exemple #2
0
def get_all_problems(region_ids, start_date, end_date, add_danger_level=True):
    '''Method returns all avalanche problems on views AvalancheProblemV, AvalancheEvalProblemV,
    AvalancheEvalProblem2V, AvalancheWarningV and AvalancheWarnProblemV. Method takes the
    region ID as used in ForecastRegionKDV.

    Note: the queries in Odata goes from a date (but not including) this date. Mathematically <from, to].

    :param region_id:           [int] ForecastRegionTID
    :param start_date:          [date or string as "YYYY-MM-DD"]
    :param end_date:            [date or string as "YYYY-MM-DD"]
    :param add_danger_level:    [bool] If false danger level will not be added to the problem

    :return all_problems:   [list]  List of AvalancheProblem objects
    '''

    # If input isn't a list, make it so
    if not isinstance(region_ids, types.ListType):
        region_ids = [region_ids]

    problem_v = []
    eval_problem_v = []
    eval_problem2_v = []
    warning_v = []
    warn_problem_v = []

    for region_id in region_ids:
        problem_v += gro.get_problems_from_AvalancheProblemV(region_id, start_date, end_date)
        eval_problem_v += gro.get_problems_from_AvalancheEvalProblemV(region_id, start_date, end_date)
        eval_problem2_v += gro.get_problems_from_AvalancheEvalProblem2V(region_id, start_date, end_date)
        warning_v += gro.get_problems_from_AvalancheWarningV(region_id, start_date, end_date)
        warn_problem_v += gro.get_problems_from_AvalancheWarnProblemV(region_id, start_date, end_date)

    all_problems = problem_v + eval_problem_v + eval_problem2_v + warning_v + warn_problem_v
    all_problems.sort(key=lambda AvalancheProblem: AvalancheProblem.date)

    # Will add all forecasted danger levels to the problems
    if add_danger_level:

        all_warnings = gfa.get_warnings(region_ids, start_date, end_date)
        all_non_zero_warnings = [w for w in all_warnings if w.danger_level != 0]
        all_non_zero_warnings.sort(key=lambda AvalancheDanger: AvalancheDanger.date)

        # Add forecasted dangerlevel to all problems. Lots of looping. Be smart. Keep track of last index since
        # both lista are ordered the same. Break the for loop if dates dont match anymore.
        last_i = 0
        for p in all_problems:
            print 'getproblems.py -> get_all_problems: looping through {0} on {1}'.format(p.date, p.regobs_view)
            for i in range(last_i, len(all_non_zero_warnings), 1):
                if all_non_zero_warnings[i].date > p.date:
                    last_i = max(0, i-1-len(region_ids))
                    break
                elif p.date == all_non_zero_warnings[i].date and p.region_id == all_non_zero_warnings[i].region_regobs_id:
                    p.set_danger_level(all_non_zero_warnings[i].danger_level)

    return all_problems
def pickle_warnings(regions, date_from, date_to, pickle_file_name):
    """
    All warnings and problems are selected from regObs or the avalanche api and neatly pickel'd for later use.
    This method also gets all warnings in english for the english main message.

    :param regions:            list []
    :param date_from:          string as 'yyyy-mm-dd'
    :param date_to:            string as 'yyyy-mm-dd'
    :param pickle_file_name:   filename including directory as string

    :return:
    """

    warnings = []

    # get all warning and problems for this region and then loop though them joining them on date
    for r in regions:
        warnings_no = gfa.get_warnings(r, date_from, date_to)
        warnings_en = gfa.get_warnings(r, date_from, date_to, lang_key=2)
        name = gro.get_forecast_region_name(r)
        problems = gro.get_problems_from_AvalancheWarnProblemV(name, r, date_from, date_to)

        print "{0} problems found for {1}".format(len(problems), name)

        # loop trough all the norwegian forecasts
        for i in range(0, len(warnings_no), 1):

            # add problems with same dates
            for j in range(0, len(problems), 1):

                if warnings_no[i].date == problems[j].date:
                    warnings_no[i].add_problem(problems[j])

            # add english main message with same dates
            for k in range(0, len(warnings_en), 1):

                if warnings_no[i].date == warnings_en[k].date:
                    warnings_no[i].set_main_message_en(warnings_en[k].main_message_en)
                    continue

        warnings = warnings + warnings_no

    mp.pickle_anything(warnings, pickle_file_name)
Exemple #4
0
def get_forecasted_dangers(region_id, start_date, end_date, include_problems=False, include_ikke_vurdert=False):
    '''Gets forecasted dangers for ONE region. If specified, the avalanche problems are included.

    :param region_id:               [int] only one region. ID as given in regObs
    :param start_date:              [date or string as yyyy-mm-dd] gets dates [from, to>
    :param end_date:                [date or string as yyyy-mm-dd] gets dates [from, to>
    :param include_problems:        [bool] if true, avalanche probelms are included. This takes more time ans additional requests.
    :param include_ikke_vurdert:    [bool] if true, it includes frocasts where danger_level = 0

    :return:
    '''

    # get all warning and problems for this region and then loop though them joining them where dates match.
    region_warnings = gfa.get_warnings(region_id, start_date, end_date)

    if include_problems:
        problems = gro.get_problems_from_AvalancheWarnProblemV(region_id, start_date, end_date)

        for i in range(0, len(region_warnings), 1):
            for j in range(0, len(problems), 1):
                if region_warnings[i].date == problems[j].date:
                    region_warnings[i].add_problem(problems[j])

        # make sure all problems are ordered from lowest id (main problem) to largest.
        for w in region_warnings:
            w.avalanche_problems = sorted(w.avalanche_problems, key=lambda AvalancheProblem: AvalancheProblem.order)

    if not include_ikke_vurdert:
        all_non_zero_warnings = []

        for w in region_warnings:
            if w.danger_level != 0:
                all_non_zero_warnings.append(w)

        region_warnings = all_non_zero_warnings

    return region_warnings