示例#1
0
def update_station_description(pool,
                               latitude,
                               longitude,
                               station_type,
                               description,
                               append_description=True):

    lat = '%.6f' % float(latitude)
    lon = '%.6f' % float(longitude)

    try:

        if station_type and station_type in (CURW_WATER_LEVEL_STATION,
                                             CURW_WEATHER_STATION,
                                             IRRIGATION_DEPARTMENT,
                                             CURW_CROSS_SECTION):
            station_type = StationEnum.getType(station_type)
        else:
            print("Station type cannot be recognized")
            exit(1)

        station_id = get_station_id(pool=pool,
                                    latitude=lat,
                                    longitude=lon,
                                    station_type=station_type)

        if append_description:
            update_description(pool=pool,
                               id_=station_id,
                               description=description,
                               append=True)
        else:
            update_description(pool=pool,
                               id_=station_id,
                               description=description,
                               append=False)

    except Exception as e:
        traceback.print_exc()
        print(
            "Exception occurred while updating description for station id {}.".
            format(station_id))
def generate_curw_obs_hash_id(pool,
                              variable,
                              unit,
                              unit_type,
                              latitude,
                              longitude,
                              run_name,
                              station_type=None,
                              station_name=None,
                              description=None,
                              append_description=True,
                              update_run_name=True,
                              start_date=None):
    """
    Generate corresponding curw_obs hash id for a given curw observational station
    :param pool: databse connection pool
    :param variable: str: e.g. "Precipitation"
    :param unit: str: e.g. "mm"
    :param unit_type: str: e.g. "Accumulative"
    :param latitude: float: e.g. 6.865576
    :param longitude: float: e.g. 79.958181
    :param run_name: str: e.g "A&T Labs"
    :param station_type: str: enum:  'CUrW_WeatherStation' | 'CUrW_WaterLevelGauge'
    :param station_name: str: "Urumewella"
    :param description: str: "A&T Communication Box, Texas Standard Rain Gauge"
    :param append_description: bool:
    :param update_run_name: bool:
    :param start_date: str: e.g."2019-07-01 00:00:00" ; the timestamp of the very first entry of the timeseries

    :return: new curw_obs hash id
    """
    if run_name not in ('A&T Labs', 'Leecom', 'CUrW IoT'):
        print(
            "This function is dedicated for generating curw_obs hash ids only for 'A&T Labs', 'Leecom', 'CUrW IoT' "
            "weather stations")
        exit(1)

    try:

        meta_data = {
            'run_name': run_name,
            'variable': variable,
            'unit': unit,
            'unit_type': unit_type,
            'latitude': latitude,
            'longitude': longitude
        }

        # run_name = run_name
        # station_name = station_name
        # latitude = latitude
        # longitude = longitude
        # description = description
        # variable = variable
        # unit = unit
        # unit_type = unit_type

        if variable == "Waterlevel":
            variable = "WaterLevel"

        if station_type and station_type in (CURW_WATER_LEVEL_STATION,
                                             CURW_WEATHER_STATION):
            station_type = StationEnum.getType(station_type)
        else:
            if variable == "WaterLevel":
                station_type = StationEnum.CUrW_WaterLevelGauge
            else:
                station_type = StationEnum.CUrW_WeatherStation

        meta_data['station_type'] = StationEnum.getTypeString(station_type)

        unit_id = get_unit_id(pool=pool,
                              unit=unit,
                              unit_type=UnitType.getType(unit_type))

        if unit_id is None:
            add_unit(pool=pool,
                     unit=unit,
                     unit_type=UnitType.getType(unit_type))
            unit_id = get_unit_id(pool=pool,
                                  unit=unit,
                                  unit_type=UnitType.getType(unit_type))

        variable_id = get_variable_id(pool=pool, variable=variable)

        if variable_id is None:
            add_variable(pool=pool, variable=variable)
            variable_id = get_variable_id(pool=pool, variable=variable)

        station_id = get_station_id(pool=pool,
                                    latitude=latitude,
                                    longitude=longitude,
                                    station_type=station_type)

        if station_id is None:
            add_station(pool=pool,
                        name=station_name,
                        latitude=latitude,
                        longitude=longitude,
                        station_type=station_type)
            station_id = get_station_id(pool=pool,
                                        latitude=latitude,
                                        longitude=longitude,
                                        station_type=station_type)
            if description:
                update_description(pool=pool,
                                   id_=station_id,
                                   description=description,
                                   append=False)

        elif append_description:
            if description:
                update_description(pool=pool,
                                   id_=station_id,
                                   description=description,
                                   append=True)

        TS = Timeseries(pool=pool)

        tms_id = TS.get_timeseries_id_if_exists(meta_data=meta_data)

        meta_data['station_id'] = station_id
        meta_data['variable_id'] = variable_id
        meta_data['unit_id'] = unit_id

        if tms_id is None:
            tms_id = TS.generate_timeseries_id(meta_data=meta_data)
            meta_data['tms_id'] = tms_id
            TS.insert_run(run_meta=meta_data)
            if start_date:
                TS.update_start_date(id_=tms_id, start_date=start_date)

        if update_run_name:
            TS.update_run_name(id_=tms_id, run_name=run_name)

        return tms_id

    except Exception:
        traceback.print_exc()
        print(
            "Exception occurred while inserting run entries to curw_obs run table and making hash mapping"
        )