Exemplo n.º 1
0
def update_tide_obs(curw_sim_pool, obs_connection, method, timestep, start_time, end_time, flo2d_model="flo2d"):
    # [station_name,latitude,longitude,target]
    extract_stations = read_csv('grids/tide_stations/extract_stations.csv')
    extract_stations_dict = {}  # keys: station_name , value: [latitude, longitude, target_model]

    for obs_index in range(len(extract_stations)):
        extract_stations_dict[extract_stations[obs_index][0]] = [extract_stations[obs_index][1],
                                                                 extract_stations[obs_index][2],
                                                                 extract_stations[obs_index][3]]

    for station_name in extract_stations_dict.keys():

        meta_data = {
            'latitude': float('%.6f' % float(extract_stations_dict.get(station_name)[0])),
            'longitude': float('%.6f' % float(extract_stations_dict.get(station_name)[1])),
            'model': flo2d_model, 'method': method,
            'grid_id': 'tide_{}'.format(station_name)
        }

        TS = Timeseries(pool=curw_sim_pool)

        tms_id = TS.get_timeseries_id_if_exists(meta_data=meta_data)

        if tms_id is None:
            tms_id = TS.generate_timeseries_id(meta_data=meta_data)
            meta_data['id'] = tms_id
            TS.insert_run(meta_data=meta_data)

        processed_tide_ts = prepare_obs_tide_ts(connection=obs_connection, start_date=start_time, end_date=end_time,
                                                extract_station=station_name)

        for i in range(len(processed_tide_ts)):
            if len(processed_tide_ts[i]) < 2:
                processed_tide_ts.remove(processed_tide_ts[i])

        final_tide_ts = fill_ts_missing_entries(start=start_time, end=end_time, timeseries=processed_tide_ts,
                                                interpolation_method='linear', timestep=timestep)

        if final_tide_ts is not None and len(final_tide_ts) > 0:
            TS.insert_data(timeseries=final_tide_ts, tms_id=tms_id, upsert=True)
Exemplo n.º 2
0
                            obs_end -
                            timedelta(days=1)).strftime('%Y-%m-%d %H:00:00')

                processed_tide_ts = prepare_obs_tide_ts(
                    connection=connection,
                    start_date=start_date,
                    end_date=end_date,
                    extract_station=station_name)

                for i in range(len(processed_tide_ts)):
                    if len(processed_tide_ts[i]) < 2:
                        processed_tide_ts.remove(processed_tide_ts[i])

                final_tide_ts = fill_ts_missing_entries(
                    start=start_date,
                    end=end_date,
                    timeseries=processed_tide_ts,
                    interpolation_method='linear',
                    timestep=60)

                if final_tide_ts is not None and len(final_tide_ts) > 0:
                    TS.insert_data(timeseries=final_tide_ts,
                                   tms_id=tms_id,
                                   upsert=True)
                    TS.update_latest_obs(id_=tms_id,
                                         obs_end=final_tide_ts[-1][1])

    except Exception as e:
        traceback.print_exc()
        logger.error("Exception occurred.")
    finally:
        connection.close()
Exemplo n.º 3
0
def update_waterlevel_obs(obs_connection, curw_sim_pool, flo2d_model, method,
                          timestep, start_time, end_time):
    try:

        # [station_name,latitude,longitude,target]
        extract_stations = read_csv(
            'grids/waterlevel_stations/extract_stations.csv')
        extract_stations_dict = {
        }  # keys: target_model , value: [latitude, longitude, station_name]
        # older version ::: keys: station_name , value: [latitude, longitude, target_model]

        for obs_index in range(len(extract_stations)):
            extract_stations_dict[extract_stations[obs_index][3]] = [
                extract_stations[obs_index][1], extract_stations[obs_index][2],
                extract_stations[obs_index][0]
            ]

        station_name = extract_stations_dict.get(flo2d_model)[2]
        meta_data = {
            'latitude':
            float('%.6f' % float(extract_stations_dict.get(flo2d_model)[0])),
            'longitude':
            float('%.6f' % float(extract_stations_dict.get(flo2d_model)[1])),
            'model':
            flo2d_model,
            'method':
            method,
            'grid_id':
            'waterlevel_{}'.format(station_name)
        }

        TS = Timeseries(pool=curw_sim_pool)

        tms_id = TS.get_timeseries_id_if_exists(meta_data=meta_data)

        ranwala_ts = []

        if tms_id is None:
            tms_id = TS.generate_timeseries_id(meta_data=meta_data)
            meta_data['id'] = tms_id
            TS.insert_run(meta_data=meta_data)

        with obs_connection.cursor() as cursor1:
            cursor1.callproc('getWL', (RANWALA_WL_ID, start_time, end_time))
            results = cursor1.fetchall()
            for result in results:
                ranwala_ts.append([result.get('time'), result.get('value')])

        interpolated_ranwala_ts = fill_ts_missing_entries(
            start=start_time,
            end=end_time,
            timeseries=ranwala_ts,
            interpolation_method='linear',
            timestep=60)

        estimated_wl_ts = []

        if station_name == 'hanwella':
            estimated_wl_ts = calculate_hanwella_wl_from_ranwala(
                interpolated_ranwala_ts)
        elif station_name == 'glencourse':
            estimated_wl_ts = calculate_glencourse_wl_from_ranwala(
                interpolated_ranwala_ts)

        if estimated_wl_ts is not None and len(estimated_wl_ts) > 0:
            TS.insert_data(timeseries=estimated_wl_ts,
                           tms_id=tms_id,
                           upsert=True)

    except Exception as e:
        traceback.print_exc()
                obs_end = TS.get_obs_end(id_=tms_id)
                start_date = (obs_end -
                              timedelta(days=1)).strftime("%Y-%m-%d %H:00:00")

            with connection.cursor() as cursor1:
                cursor1.callproc('getWL',
                                 (RANWALA_WL_ID, start_date, end_date))
                results = cursor1.fetchall()
                for result in results:
                    ranwala_ts.append(
                        [result.get('time'),
                         result.get('value')])

            interpolated_ranwala_ts = fill_ts_missing_entries(
                start=start_date,
                end=end_date,
                timeseries=ranwala_ts,
                interpolation_method='linear',
                timestep=60)

            estimated_wl_ts = []

            if station_name == 'hanwella':
                estimated_wl_ts = calculate_hanwella_wl_from_ranwala(
                    interpolated_ranwala_ts)
            elif station_name == 'glencourse':
                estimated_wl_ts = calculate_glencourse_wl_from_ranwala(
                    interpolated_ranwala_ts)

            if estimated_wl_ts is not None and len(estimated_wl_ts) > 0:
                TS.insert_data(timeseries=estimated_wl_ts,
                               tms_id=tms_id,