Ejemplo n.º 1
0
    def __init__(self, d):

        self.NickName = fe.remove_norwegian_letters(d["NickName"])
        self.ObserverGroupDescription = fe.remove_norwegian_letters(d["ObserverGroupDescription"])
        self.ObserverGroupID = int(d["ObserverGroupID"])
        self.ObserverGroupName = fe.remove_norwegian_letters(d["ObserverGroupName"])
        self.ObserverID = int(d["ObserverID"])
Ejemplo n.º 2
0
    def __init__(self, region_id_inn, region_name_inn, date_inn, order_inn, cause_name_inn, source_inn):
        '''
        The AvalancheProblem object is useful since there are 3 different tables to get regObs-data from and 2 tables
        from forecasts. Thus avalanche problems are saved in 5 ways. The structure of this object is based on the
        "latest" model and the other/older ways to save avalanche problems on may be mapped to these.

        Parameters part of the constructor:
        :param region_id_inn:       [int]       Region ID is as given in ForecastRegionKDV.
        :param region_name_inn:     [String]    Region name as given in ForecastRegionKDV.
        :param date_inn:            [Date]      Date for avalanche problem.
        :param order_inn:           [int]       The index* of the avalanche problem.
        :param cause_name_inn:      [String]    Avalanche cause. For newer problems this as given in AvalCauseKDV.
        :param source_inn:          [String]    The source of the data. Normally 'Observasjon' og 'Varsel'.

        * Order should be indexed from 0 (main problem) and up, but I see that they often start on higher indexes
          and don't always have a step of +1. A rule is that the higher the index the higher the priority.

        Other variables:
        regid               [int]
        municipal_name      [String]
        cause_tid           [int]           Is only used in avalanche problems from dec 2014 and newer
        main_cause          [String]        Avalanche causes are grouped in main causes
        aval_type           [String]
        aval_size           [String]
        aval_trigger        [String]
        aval_probability    [String]
        aval_distribution   [String]        Named AvalPropagation in regObs
        problem_combined    [String]
        regobs_view         [String]
        url = None          [String]
        metadata = []       [list with dictionaries [{},{},..] ]
        '''

        self.metadata = {}              # dictionary {key:value, key:value, ..}
        self.region_id = region_id_inn
        self.region_name = fe.remove_norwegian_letters(region_name_inn)
        self.date = None
        self.set_date(date_inn)
        self.order = order_inn
        self.cause_tid = None           # [int]     Avalanche cause ID (in regObs TID)
        self.set_cause_name(cause_name_inn)
        self.source = fe.remove_norwegian_letters(source_inn)

        self.regid = None               # int
        self.municipal_name = None      # [String]
        self.main_cause = None          # [String]
        self.aval_type = None           # [String]
        self.aval_size = None           # [String]
        self.aval_trigger = None        # [String]
        self.aval_probability = None    # [String]
        self.aval_distribution = None   # [String]
        self.problem_combined = None    # [String]
        self.regobs_view = None         # [String]
        self.url = None                 # [String]
        self.nick_name = None           # [String]

        self.danger_level = None        # Int
        self.danger_level_name = None   # String
Ejemplo n.º 3
0
    def __init__(self, d):

        self.LocationName = fe.remove_norwegian_letters(d['LocationName'])
        self.UTMZone = int(d['UTMZone'])
        self.UTMEast = int(d['UTMEast'])
        self.UTMNorth = int(d['UTMNorth'])
        self.ForecastRegionName = fe.remove_norwegian_letters(d['ForecastRegionName'])

        # In AllRegistrationsV MunicipalName is named Kommunenavn
        if 'MunicipalName' in d:
            self.MunicipalName = fe.remove_norwegian_letters(d['MunicipalName'])
        elif 'Kommunenavn' in d:
            self.MunicipalName = fe.remove_norwegian_letters(d['Kommunenavn'])
        else:
            self.MunicipalName = None
Ejemplo n.º 4
0
    def __init__(self, region_regobs_id_inn, region_name_inn, data_table_inn, date_inn, danger_level_inn, danger_level_name_inn):
        '''
        AvalancheDanger object since there are 3 different tables to get regObs-data from and one from the forecast

        :param region_regobs_id_inn:
        :param region_name_inn:
        :param data_table_inn:
        :param date_inn:
        :param danger_level_inn:
        :param danger_level_name_inn:
        :return:

        Other variables:
        main_message_no
        main_message_en
        nick                            [string]    Nickname of the observer/forecaster who issued the avalanche danger
        metadata:                       [{key:value}, ..]
        avalanche_problems:             [AvalancheProblem, ..]
        '''

        if region_regobs_id_inn < 100: # makes sure that it is regObs ID used in this program
            region_regobs_id_inn = region_regobs_id_inn + 100

        self.metadata = {}                                                          # [dictionary] {key:value, key:value, ..}
        self.region_regobs_id = region_regobs_id_inn                                # [int]
        self.region_name = fe.remove_norwegian_letters(region_name_inn)             # [String]
        self.data_table = fe.remove_norwegian_letters(data_table_inn)               # [String]

        if isinstance(date_inn, dt.datetime): # makes sure we only have the date
            self.add_metadata("Original datetime", date_inn)
            date_inn = date_inn.date()
        self.date = date_inn                                                        # [date]

        self.danger_level = danger_level_inn                                        # [Int]
        self.danger_level_name = fe.remove_norwegian_letters(danger_level_name_inn) # [String]

        #forecast stuff
        self.main_message_no = None                                                 # [String]
        self.main_message_en = None
        self.nick = None
        self.avalanche_problems = []
        self.source = None
        self.avalanche_forecast = None
        self.avalanche_nowcast = None

        #obs eval 3 stuff
        self.forecast_correct = None
        self.forecast_correct_id = None
Ejemplo n.º 5
0
    def __init__(self, d):

        Registration.__init__(self, d)
        Location.__init__(self, d)
        Observer.__init__(self, d)

        self.ForecastRegionTID = int(d['ForecastRegionTID'])
        self.ObserverId = int(d['ObserverId'])

        self.RowNumber = int(d['RadNummer'])
        self.GeoHazardName = fe.remove_norwegian_letters(d['GeoHazardName'])
        self.RegistrationName = fe.remove_norwegian_letters(d['RegistrationName'])
        self.TypicalValue1 = fe.remove_norwegian_letters(d['TypicalValue1'])
        self.TypicalValue2 = fe.remove_norwegian_letters(d['TypicalValue2'])
        self.LangKey = int(d['LangKey'])
        self.Picture = d['Picture']
Ejemplo n.º 6
0
def get_registration(from_date, to_date, output='List', geohazard_tid=None, ApplicationID=None, include_deleted=False):

    import setvariables as setv

    odata_filter = ""
    if geohazard_tid is not None:
        odata_filter += "GeoHazardTID eq {0} and ".format(geohazard_tid)
    odata_filter += "DtRegTime gt datetime'{0}' and DtRegTime lt datetime'{1}'".format(from_date, to_date)
    if "Web and app" in ApplicationID:
        odata_filter += " and (ApplicationId eq guid'{0}' or ApplicationId eq guid'{1}')".format(setv.web, setv.app)

    url = 'http://api.nve.no/hydrology/regobs/{0}/Odata.svc/{1}/?$filter={2}&$format=json'.format(env.api_version, "Registration",odata_filter)
    print "getmisc.py -> get_registration: ..to {0}".format(fe.remove_norwegian_letters(url))

    result = requests.get(url).json()
    data = result['d']['results']

    # if more than 1000 elements are requested, odata truncates data to 1000. We do more requests
    if len(data) == 1000:
        time_delta = to_date - from_date
        date_in_middle = from_date + time_delta/2
        data = get_registration(from_date, date_in_middle, output='Raw', geohazard_tid=geohazard_tid, ApplicationID=ApplicationID) \
                 + get_registration(date_in_middle, to_date, output='Raw', geohazard_tid=geohazard_tid, ApplicationID=ApplicationID)

    if output=='Raw':
        return data
    elif output=='List':
        data_out = [Registration(d) for d in data]
        observer_nicks = get_observer_v()
        for d in data_out:
            d.NickName = [observer_nicks[d.ObserverID]]
        if include_deleted == False:
            data_out = [d for d in data_out if d.DeletedDate is None]
        return data_out
Ejemplo n.º 7
0
 def set_municipal(self, municipal_inn):
     try:
         self.municipal_name = fe.remove_norwegian_letters(municipal_inn)
     except ValueError:
         print "Got ValueError on setting municipal name on {0} for {1}."\
             .format(self.date, self.region_name)
     except:
         print "Got un expected error on setting municipal name on {0} for {1}."\
             .format(self.date, self.region_name)
Ejemplo n.º 8
0
def select_messages_with_more(pickle_warnings_file_name):
    """
    Method selects unique messages and adds to them the english name (english name that appeared on the first
    occurrence), adds the danger levels, main causes, causes, and avalanche types that are used to this main
    message. There is also a count of how many times the main text has occurred.

    :param pickle_warnings_file_name    filename to where the picle file with the warnings are located
    :return main_messages               list of MainMessage objects ordered by most occurrences first
    """

    warnings = mp.unpickle_anything(pickle_warnings_file_name)
    main_messages = []

    for w in warnings:

        message_no = fe.remove_norwegian_letters(w.main_message_no)
        message_no = more_text_cleanup(message_no)

        # if no content
        if message_no == "Ikke vurdert":
            continue

        if message_no == "":
            continue

        # if message exists in list append changes to it
        if message_no_is_in_list(message_no, main_messages):
            m = get_main_message_object(message_no, main_messages)
            m.add_occurrence()
            m.add_to_danger_levels(w.danger_level)

            for p in w.avalanche_problems:
                m.add_to_main_causes(p.main_cause)
                m.add_to_cause_names(p.cause_name)
                m.add_to_aval_types(p.aval_type)

        # if not append a new one
        else:
            new_m = MainMessage(message_no)
            new_m.add_main_message_en(w.main_message_en)
            new_m.add_to_danger_levels(w.danger_level)

            for p in w.avalanche_problems:
                new_m.add_to_main_causes(p.main_cause)
                new_m.add_to_cause_names(p.cause_name)
                new_m.add_to_aval_types(p.aval_type)

            main_messages.append(new_m)

    # sort on main_message_no
    main_messages.sort(key=lambda m: m.main_message_no, reverse=False)

    # sort on occurrences
    # main_messages.sort(key=lambda m: m.occurrences, reverse=True)

    return main_messages
Ejemplo n.º 9
0
    def __init__(self, d):

        Registration.__init__(self, d)
        Location.__init__(self, d)
        Observer.__init__(self, d)

        self.AvalancheName = fe.remove_norwegian_letters(d['AvalancheName'])
        self.AvalancheTriggerName = fe.remove_norwegian_letters(d['AvalancheTriggerName'])
        self.Comment = fe.remove_norwegian_letters(d['Comment'])
        self.DestructiveSizeName = fe.remove_norwegian_letters(d['DestructiveSizeName'])
        self.DtAvalancheTime = _unix_time_2_normal(int(d['DtAvalancheTime'][6:-2]))
        self.HeightStartZone = d['HeigthStartZone']
        self.HeightStopZone = d['HeigthStopZone']
        self.LangKey = d['LangKey']
        self.SnowLine = d['SnowLine']
        self.TerrainStartZoneName = fe.remove_norwegian_letters(d['TerrainStartZoneName'])
        self.UTMEastStop = d['UTMEastStop']
        self.UTMNorthStop = d['UTMNorthStop']
        self.UTMZoneStop = d['UTMZoneStop']
Ejemplo n.º 10
0
def _make_data_request(view, from_date, to_date, region_ids=None, observer_ids=None, geohazard_tid=None):
    """Common part of all data requests.

    :param view:            [string] Name of view in regObs
    :param from_date:       [date] A query returns [from_date, to_date>
    :param to_date:         [date] A query returns [from_date, to_date>
    :param region_ids:      [int or list of ints] If region_ids = None, all regions are selected
    :param observer_ids:    [int or list of ints] If observer_ids = None, all observers are selected
    :param geohazard_tid:   [int] 10 is snow, 20,30,40 are dirt, 60 is water and 70 is ice

    :return:                untreated request result
    """

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

    if not isinstance(observer_ids, types.ListType):
        observer_ids = [observer_ids]

    data_out = []
    for region_id in region_ids:
        for observer_id in observer_ids:

            odata_query = _make_odata_filter(from_date, to_date, region_id, observer_id, geohazard_tid)

            url = "http://api.nve.no/hydrology/regobs/{0}/Odata.svc/{1}/?$filter={2}&$format=json"\
                .decode('utf8').format(env.api_version, view, odata_query)

            print "getobservations.py -> _make_data_request: ..to {0}".format(fe.remove_norwegian_letters(url))

            try:
                result = requests.get(url).json()
                result = result['d']['results']

            except:
                # Need to improve on exception handling:
                # http://docs.python-requests.org/en/latest/user/quickstart/#errors-and-exceptions
                print "getobservations.py -> _make_data_request: EXCEPTION DURING REQUEST. DATA LOST."
                result = []

            # if more than 1000 elements are requested, odata truncates data to 1000. We do more requests
            if len(result) == 1000:
                time_delta = to_date - from_date
                date_in_middle = from_date + time_delta/2
                result = _make_data_request(view, from_date, date_in_middle, region_id, observer_id, geohazard_tid) \
                         + _make_data_request(view, date_in_middle, to_date, region_id, observer_id, geohazard_tid)

            data_out += result

    env.log.append("{2} to {3}: {1} has {0} items".format(len(data_out), view, from_date, to_date))

    return data_out
Ejemplo n.º 11
0
    def __init__(self, d):

        Registration.__init__(self, d)
        Location.__init__(self, d)
        Observer.__init__(self, d)

        self.Comment = fe.remove_norwegian_letters(d['Comment'])
        self.DangerObsID = d['DangerObsID']
        self.DangerSignName = d['DangerSignName']
        self.DangerSignTID = d['DangerSignTID']
        self.GeoHazardName = d['GeoHazardName']
        self.GeoHazardTID = d['GeoHazardTID']
        self.LangKey = d['LangKey']
Ejemplo n.º 12
0
def get_forecast_region_name(region_id):
    """
    Method takes in the region id (same as ForecastRegionTID in regObs). It looks up the name in ForecastRegionKDV
    and returns the region name.

    :param region_id:    Region ID is an int as given i ForecastRegionKDV
    :return:             Region Name string is returned
    """

    forecast_region_kdv = gkdv.get_kdv("ForecastRegionKDV")
    forecast_region_kdvelement = forecast_region_kdv[region_id]
    forecast_region_name = fe.remove_norwegian_letters(forecast_region_kdvelement.Name)

    return forecast_region_name
Ejemplo n.º 13
0
def get_observer_v():
    """Selects all data from the ObserverV view.

    :return: a dictionary of key/ObserverID : value/NickName
    """

    url = 'http://api.nve.no/hydrology/regobs/v1.0.0/Odata.svc/ObserverV?$format=json'
    result = requests.get(url).json()
    data = result['d']['results']

    observer_nicks = {}
    for d in data:
        key = d['ObserverId']
        val = fe.remove_norwegian_letters(d['NickName'])
        observer_nicks[key] = val

    return observer_nicks
Ejemplo n.º 14
0
def get_observer_v():
    """Selects all data from the ObserverV view. Returns all observers. Not only the topp 1000 for some reason.

    :return: a dictionary of key/ObserverID : value/NickName
    """

    url = 'http://api.nve.no/hydrology/regobs/{0}/Odata.svc/ObserverV?$format=json'.format(env.api_version)
    result = requests.get(url).json()
    data = result['d']['results']

    observer_nicks = {}
    for d in data:
        key = d['ObserverId']
        val = fe.remove_norwegian_letters(d['NickName'])
        observer_nicks[key] = val

    return observer_nicks
Ejemplo n.º 15
0
def get_registration(from_date, to_date, output='List', geohazard_tid=None, ApplicationID='', include_deleted=False):
    '''Gets data from the Registration table. Adds observer nickname if list is requested and if not otherwize
    specified deleted registrations are taken out.

    :param from_date:
    :param to_date:
    :param output:
    :param geohazard_tid:
    :param ApplicationID:
    :param include_deleted:
    :return:
    '''

    odata_filter = ""
    if geohazard_tid is not None:
        odata_filter += "GeoHazardTID eq {0} and ".format(geohazard_tid)
    odata_filter += "DtRegTime gt datetime'{0}' and DtRegTime lt datetime'{1}'".format(from_date, to_date)
    if "Web and app" in ApplicationID: # does not work..
        odata_filter += " and (ApplicationId eq guid'{0}' or ApplicationId eq guid'{1}')".format('','')

    url = 'http://api.nve.no/hydrology/regobs/{0}/Odata.svc/{1}/?$filter={2}&$format=json'.format(env.api_version, "Registration", odata_filter)
    print "getmisc.py -> get_registration: ..to {0}".format(fe.remove_norwegian_letters(url))

    result = requests.get(url).json()
    data = result['d']['results']

    # if more than 1000 elements are requested, odata truncates data to 1000. We do more requests
    if len(data) == 1000:
        time_delta = to_date - from_date
        date_in_middle = from_date + time_delta/2
        data = get_registration(from_date, date_in_middle, output='Raw', geohazard_tid=geohazard_tid, ApplicationID=ApplicationID) \
                 + get_registration(date_in_middle, to_date, output='Raw', geohazard_tid=geohazard_tid, ApplicationID=ApplicationID)

    if output=='Raw':
        return data
    elif output=='List':
        data_out = [Registration(d) for d in data]
        observer_nicks = get_observer_v()
        # NickName is not originally in the Registration table
        for d in data_out:
            d.NickName = observer_nicks[d.ObserverID]
            # why list??? d.NickName = [observer_nicks[d.ObserverID]]
        if include_deleted == False:
            data_out = [d for d in data_out if d.DeletedDate is None]
        return data_out
Ejemplo n.º 16
0
    def __init__(self, d):

        self.TripID = int(d["TripID"])
        self.ObserverID = int(d["ObserverID"])
        self.ObsLocationID = int(d["ObsLocationID"])
        self.GeoHazardTID = int(d["GeoHazardTID"])
        self.TripTypeTID = int(d["TripTypeTID"])
        self.ObservationExpectedTime = fe.unix_time_2_normal( int(d["ObservationExpectedTime"][6:-2]) )
        self.Comment = fe.remove_norwegian_letters(d["Comment"])
        self.IsFinished = bool(d["IsFinished"])
        self.TripRegistrationTime = fe.unix_time_2_normal( int(d["TripRegistrationTime"][6:-2]) )
        if d["TripFinishedTime"] is not None:
            self.TripFinishedTime = fe.unix_time_2_normal( int(d["TripFinishedTime"][6:-2]) )
        else:
            self.TripFinishedTime = None
        self.DeviceID = d["DeviceID"]

        self.TripTypeName = kdv.get_name("TripTypeKDV", self.TripTypeTID)
Ejemplo n.º 17
0
def get_observer_group_member(group_id=None, output='List'):

    if group_id is None:
        url = 'http://api.nve.no/hydrology/regobs/v0.9.9/Odata.svc/ObserverGroupMemberV/?$format=json'
    else:
        url = 'http://api.nve.no/hydrology/regobs/v0.9.9/Odata.svc/ObserverGroupMemberV/?$filter=ObserverGroupID%20eq%20{0}&$format=json'.format(group_id)
    print "getmisc.py -> get_trip: ..to {0}".format(fe.remove_norwegian_letters(url))

    result = requests.get(url).json()
    data = result['d']['results']
    data_out = [ObserverGroupMember(d) for d in data]

    if output=='List':
        return data_out
    elif output=='Dict':
        observer_dict = {}
        for o in data_out:
            observer_dict[o.ObserverID] = o.NickName
        return observer_dict
Ejemplo n.º 18
0
    def __init__(self, d):

        Registration.__init__(self, d)
        Location.__init__(self, d)
        Observer.__init__(self, d)

        self.AvalancheActivityCode = d['AvalancheActivityCode']
        self.EstimatedNumName = fe.remove_norwegian_letters(d['EstimatedNumName'])
        self.DestructiveSizeName = fe.remove_norwegian_letters(d['DestructiveSizeName'])
        self.Aspect = fe.remove_norwegian_letters(d['Aspect'])
        self.HeightStartingZone = d['HeightStartingZone']
        self.AvalancheName = fe.remove_norwegian_letters(d['AvalancheName'])
        self.AvalancheTriggerName = fe.remove_norwegian_letters(d['AvalancheTriggerName'])
        self.TerrainStartingZone = fe.remove_norwegian_letters(d['TerrainStartingZone'])
        self.DtAvalancheTime = _unix_time_2_normal(int(d['DtAvalancheTime'][6:-2]))
        self.Snowline = d['Snowline']
        self.Comment = fe.remove_norwegian_letters(d['Comment'])
        self.LangKey = d['LangKey']
Ejemplo n.º 19
0
    def __init__(self, d):

        Registration.__init__(self, d)
        Location.__init__(self, d)
        Observer.__init__(self, d)

        self.ActivityInfluencedName = fe.remove_norwegian_letters(d['ActivityInfluencedName'])
        self.DamageExtentTID = d['DamageExtentTID']
        self.DamageExtentName = fe.remove_norwegian_letters(d['DamageExtentName'])
        self.IncidentHeader = fe.remove_norwegian_letters(d['IncidentHeader'])
        self.IncidentIngress = fe.remove_norwegian_letters(d['IncidentIngress'])
        self.IncidentText = fe.remove_norwegian_letters(d['IncidentText'])
        self.GeoHazardName = fe.remove_norwegian_letters(d['GeoHazardName'])
        self.GeoHazardTID = d['GeoHazardTid']
        self.LangKey = d['LangKey']
Ejemplo n.º 20
0
    def set_cause_name(self, cause_name_inn):
        # Map wrong use of IDs in AvalCauseKDV in forecasts 2014-15

        aval_cause_kdv = gkdv.get_kdv('AvalCauseKDV')
        cause_name_inn = fe.remove_norwegian_letters(cause_name_inn)

        #### Cleanup in db during summer of 2015. Need this no more.
        # if (cause_name_inn == aval_cause_kdv[26].Name):
        #     self.add_metadata('Original AvalCauseName', cause_name_inn)
        #     cause_name_inn = aval_cause_kdv[20]
        # if (cause_name_inn == aval_cause_kdv[27].Name):
        #     self.add_metadata('Original AvalCauseName', cause_name_inn)
        #     cause_name_inn = aval_cause_kdv[23]
        # if (cause_name_inn == aval_cause_kdv[28].Name):
        #     self.add_metadata('Original AvalCauseName', cause_name_inn)
        #     cause_name_inn = aval_cause_kdv[24]
        # if (cause_name_inn == aval_cause_kdv[29].Name):
        #     self.add_metadata('Original AvalCauseName', cause_name_inn)
        #     cause_name_inn = aval_cause_kdv[25].Name

        self.cause_name = cause_name_inn
Ejemplo n.º 21
0
def _make_count_request(view, from_date, to_date, region_ids=None, observer_ids=None, geohazard_tid=None):
    """Common part of all count requests.

    Note that some views are shared by all geohazards. In these it is also
    possible to specify geohazard_tid in the filter.

    :param view:
    :param from_date:       [date] A query returns [from_date, to_date>
    :param to_date:         [date] A query returns [from_date, to_date>
    :param region_ids:      [int or list of ints] If region_ids = None, all regions are selected
    :param observer_ids:    [int or list of ints] If observer_ids = None, all observers are selected
    :param geohazard_tid:   [int] 10 is snow, 20,30,40 are dirt, 60 is water and 70 is ice

    :return:                [int] Number of observations.
    """

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

    if not isinstance(observer_ids, types.ListType):
        observer_ids = [observer_ids]

    count = 0
    for region_id in region_ids:
        for observer_id in observer_ids:

            odata_query = _make_odata_filter(from_date, to_date, region_id, observer_id, geohazard_tid)
            url = "http://api.nve.no/hydrology/regobs/{0}/Odata.svc/{1}/$count/?$filter={2}&$format=json"\
                .decode('utf8').format(env.api_version, view, odata_query)

            print "getobservations.py -> _make_count_request: ..to {0}".format(fe.remove_norwegian_letters(url))

            result = requests.get(url).json()
            count += result

    return count
Ejemplo n.º 22
0
    def __init__(self, d):

        Registration.__init__(self, d)
        Location.__init__(self, d)
        Observer.__init__(self, d)

        self.EstimatedNumTID = d['EstimatedNumTID']
        self.EstimatedNumName = fe.remove_norwegian_letters(d['EstimatedNumName'])

        #timedelta between regobs.no and database
        time_delta = dt.timedelta(hours=-2)

        if d['DtStart'] is not None:
            self.DtStart = _unix_time_2_normal(int(d['DtStart'][6:-2])) + time_delta
        else:
            # Really not sure I should assume DtStart = DtObsTime when None
            self.DtStart = (_unix_time_2_normal(int(d['DtObsTime'][6:-2]))).replace(hour=00, minute=00)

        if d['DtEnd'] is not None:
            self.DtEnd = _unix_time_2_normal(int(d['DtEnd'][6:-2])) + time_delta
        else:
            # Really not sure I should assume DtEnd = DtObsTime when None
            self.DtEnd = (_unix_time_2_normal(int(d['DtObsTime'][6:-2]))).replace(hour=23, minute=59)

        self.ValidExposition = d['ValidExposition']     # int of eight char. First char is N, second is NE etc.
        self.ExposedHeight1 = None
        if d['ExposedHeight1'] is not None: self.ExposedHeight1 = d['ExposedHeight1']      # upper height
        self.ExposedHeight2 = None
        if d['ExposedHeight2'] is not None: self.ExposedHeight2 = d['ExposedHeight2']      # lower height
        self.ExposedHeightComboTID = d['ExposedHeightComboTID']

        self.AvalancheExtName = fe.remove_norwegian_letters(d['AvalancheExtName'])
        self.AvalCauseName = fe.remove_norwegian_letters(d['AvalCauseName'])
        self.AvalTriggerSimpleName = fe.remove_norwegian_letters(d['AvalTriggerSimpleName'])
        self.DestructiveSizeName = fe.remove_norwegian_letters(d['DestructiveSizeName'])
        self.AvalPropagationName = fe.remove_norwegian_letters(d['AvalPropagationName'])
        self.Comment = fe.remove_norwegian_letters(d['Comment'])
        self.LangKey = d['LangKey']
Ejemplo n.º 23
0
 def set_forecast_correct(self, forecast_correct_inn, forecast_correct_id_inn):
     self.forecast_correct = fe.remove_norwegian_letters(forecast_correct_inn)
     self.forecast_correct_id = int(forecast_correct_id_inn)
Ejemplo n.º 24
0
 def set_nick(self, nick_inn):
     self.nick = fe.remove_norwegian_letters(nick_inn)
Ejemplo n.º 25
0
 def set_main_message_en(self, main_message_en_inn):
     self.main_message_en = fe.remove_norwegian_letters(main_message_en_inn)
Ejemplo n.º 26
0
 def set_main_message_no(self, main_message_no_inn):
     self.main_message_no = fe.remove_norwegian_letters(main_message_no_inn)
     self.add_metadata('Original norwegian main message', main_message_no_inn)
Ejemplo n.º 27
0
        mp.pickle_anything([all_danger_levels, all_danger_signs], pickle_file_name_1)
    else:
        # load data from pickle
        all_danger_levels, all_danger_signs = mp.unpickle_anything(pickle_file_name_1)

    if make_new:
        # for counting days with danger levels
        level_count = []
        data = {1:[], 2:[], 3:[], 4:[], 5:[]}
        for dl in all_danger_levels:
            if dl.source == 'Varsel' and dl.danger_level is not 0:
                level_count.append(dl.danger_level)
                for ds in all_danger_signs:
                    if dl.date == ds.DtObsTime.date() and dl.region_name in ds.ForecastRegionName:
                        print '{0}'.format(dl.date)
                        data[dl.danger_level].append(fe.remove_norwegian_letters(ds.DangerSignName))
        mp.pickle_anything([data, level_count], pickle_file_name_2)
    else:
        data, level_count = mp.unpickle_anything(pickle_file_name_2)

    # pick out a list of danger signs
    DangerSignKDV = gkdv.get_kdv('DangerSignKDV')
    danger_signs = []
    for k, v in DangerSignKDV.iteritems():
        if 0 < k < 100 and v.IsActive is True:
            danger_signs.append(v.Name)

    # change order in danger signs
    a = danger_signs[8]
    danger_signs.insert(5, a)
    b = danger_signs[9]
Ejemplo n.º 28
0
 def set_problem_combined(self, problem_inn):
     self.problem_combined = fe.remove_norwegian_letters(problem_inn)
Ejemplo n.º 29
0
 def set_aval_size(self, aval_size_inn):
     aval_size = fe.remove_norwegian_letters(aval_size_inn)
     if aval_size != "Ikke gitt":
         self.aval_size = aval_size
Ejemplo n.º 30
0
def get_trip(from_date, to_date, geohazard_tid=None, output='List'):
    """
    :param from_date:       [date] A query returns [from_date, to_date>
    :param to_date:         [date] A query returns [from_date, to_date>
    :param geohazard_tid:   [int] 10 is snow, 20,30,40 are dirt, 60 is water and 70 is ice

    :return:

    <entry>
        <id>http://api.nve.no/hydrology/RegObs/v0.9.9/OData.svc/Trip(1)</id>
        <category term="RegObsModel.Trip" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" title="Trip" href="Trip(1)" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Observer" type="application/atom+xml;type=entry" title="Observer" href="Trip(1)/Observer" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ObsLocation" type="application/atom+xml;type=entry" title="ObsLocation" href="Trip(1)/ObsLocation" />
        <title />
        <updated>2015-12-30T20:09:16Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:TripID m:type="Edm.Int32">1</d:TripID>
                <d:ObserverID m:type="Edm.Int32">1077</d:ObserverID>
                <d:ObsLocationID m:type="Edm.Int32">19063</d:ObsLocationID>
                <d:GeoHazardTID m:type="Edm.Int16">10</d:GeoHazardTID>
                <d:TripTypeTID m:type="Edm.Int32">20</d:TripTypeTID>
                <d:ObservationExpectedTime m:type="Edm.DateTime">2015-01-09T11:00:00</d:ObservationExpectedTime>
                <d:Comment></d:Comment>
                <d:IsFinished m:type="Edm.Boolean">true</d:IsFinished>
                <d:TripRegistrationTime m:type="Edm.DateTime">2015-01-09T09:11:59.263</d:TripRegistrationTime>
                <d:TripFinishedTime m:type="Edm.DateTime">2015-01-09T09:18:36.653</d:TripFinishedTime>
                <d:DeviceID m:type="Edm.Guid">835f5e39-a73a-48d3-2c7f-3c81c0492b87</d:DeviceID>
            </m:properties>
        </content>
    </entry>

    """

    odata_filter = ""

    if geohazard_tid is not None:
        odata_filter += "GeoHazardTID eq {0} and ".format(geohazard_tid)

    odata_filter += "TripRegistrationTime gt datetime'{0}' and TripRegistrationTime lt datetime'{1}'".format(from_date, to_date)


    odata_filter = fe.add_norwegian_letters(odata_filter)

    url = "http://api.nve.no/hydrology/regobs/{0}/Odata.svc/Trip/?$filter={1}&$format=json"\
        .decode('utf8').format(env.api_version, odata_filter)

    print "getmisc.py -> get_trip: ..to {0}".format(fe.remove_norwegian_letters(url))

    result = requests.get(url).json()
    data = result['d']['results']

    # if more than 1000 elements are requested, odata truncates data to 1000. We do more requests
    if len(result) == 1000:
        time_delta = to_date - from_date
        date_in_middle = from_date + time_delta/2
        data_out = get_trip(from_date, date_in_middle, geohazard_tid) + get_trip(date_in_middle, to_date, geohazard_tid)
    else:
        data_out = [Trip(d) for d in data]

    if output == 'List':
        return data_out
    elif output == 'csv':
        with open('{0}trips {1}-{2}.csv'.format(env.output_folder, from_date.strftime('%Y%m%d'), to_date.strftime('%Y%m%d')), 'wb') as f:
            w = csv.DictWriter(f, data_out[0].__dict__.keys(), delimiter=";")
            w.writeheader()
            for t in data_out:
                w.writerow(t.__dict__)
        return
Ejemplo n.º 31
0
        all_danger_levels, all_danger_signs = mp.unpickle_anything(
            pickle_file_name_1)

    if make_new:
        # for counting days with danger levels
        level_count = []
        data = {1: [], 2: [], 3: [], 4: [], 5: []}
        for dl in all_danger_levels:
            if dl.source == 'Varsel' and dl.danger_level is not 0:
                level_count.append(dl.danger_level)
                for ds in all_danger_signs:
                    if dl.date == ds.DtObsTime.date(
                    ) and dl.region_name in ds.ForecastRegionName:
                        print '{0}'.format(dl.date)
                        data[dl.danger_level].append(
                            fe.remove_norwegian_letters(ds.DangerSignName))
        mp.pickle_anything([data, level_count], pickle_file_name_2)
    else:
        data, level_count = mp.unpickle_anything(pickle_file_name_2)

    # pick out a list of danger signs
    DangerSignKDV = gkdv.get_kdv('DangerSignKDV')
    danger_signs = []
    for k, v in DangerSignKDV.iteritems():
        if 0 < k < 100 and v.IsActive is True:
            danger_signs.append(v.Name)

    # change order in danger signs
    a = danger_signs[8]
    danger_signs.insert(5, a)
    b = danger_signs[9]