def prepare_mike_dis_input(start, end, tide_id): try: pool = get_Pool(host=con_params.CURW_SIM_HOST, port=con_params.CURW_SIM_PORT, user=con_params.CURW_SIM_USERNAME, password=con_params.CURW_SIM_PASSWORD, db=con_params.CURW_SIM_DATABASE) TS = Timeseries(pool) ts = TS.get_timeseries(id_=tide_id, start_date=start, end_date=end) ts.insert(0, ['time', 'value']) ts_df = list_of_lists_to_df_first_row_as_columns(ts) ts_df['value'] = ts_df['value'].astype('float64') tide_ts_df = pd.DataFrame() tide_ts_df['time'] = pd.date_range(start=start, end=end, freq='15min') tide_ts_df = pd.merge(tide_ts_df, ts_df, how="left", on='time') tide_ts_df.set_index('time', inplace=True) processed_tide_ts_df = replace_negative_99999_with_nan(tide_ts_df) if np.isnan(processed_tide_ts_df.iloc[-1, 0]): processed_tide_ts_df.iloc[-1, 0] = 0 processed_tide_ts_df = processed_tide_ts_df.dropna() return processed_tide_ts_df except Exception: traceback.print_exc() finally: destroy_Pool(pool)
def prepare_outflow_250(outflow_file_path, start, end, tide_id): try: curw_sim_pool = get_Pool(host=CURW_SIM_HOST, user=CURW_SIM_USERNAME, password=CURW_SIM_PASSWORD, port=CURW_SIM_PORT, db=CURW_SIM_DATABASE) TS = TideTS(pool=curw_sim_pool) tide_ts = TS.get_timeseries(id_=tide_id, start_date=start, end_date=end) tide_data = [] timeseries = tide_ts for i in range(len(timeseries)): time_col = (str( '%.3f' % (((timeseries[i][0] - timeseries[0][0]).total_seconds()) / 3600))).rjust(16) value_col = (str('%.3f' % (timeseries[i][1]))).rjust(16) tide_data.append('S' + time_col + value_col) outflow = [] outflow.append('K 91') outflow.append('K 171') outflow.append('K 214') outflow.append('K 491') outflow.append('N 134 1') outflow.extend(tide_data) outflow.append('N 220 1') outflow.extend(tide_data) outflow.append('N 261 1') outflow.extend(tide_data) outflow.append('N 558 1') outflow.extend(tide_data) write_to_file(outflow_file_path, data=outflow) tail_file_path = os.path.join(os.getcwd(), 'outflow', 'tail_150.txt') tail_file = open(tail_file_path, "r") tail = tail_file.read() tail_file.close() append_file_to_file(outflow_file_path, file_content=tail) except Exception as e: print(traceback.print_exc()) finally: destroy_Pool(curw_sim_pool) print("Outflow generated")
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)
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': extract_stations_dict.get(station_name)[2], '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) start_date = (datetime.now() - timedelta(days=10)).strftime('%Y-%m-%d %H:00:00') end_date = (datetime.now() + timedelta( hours=5, minutes=30)).strftime('%Y-%m-%d %H:00:00') 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) else: obs_end = TS.get_obs_end(tms_id) if obs_end is not None:
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': extract_stations_dict.get(station_name)[2], '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) obs_end = TS.get_obs_end(id_=tms_id) ts_end = TS.get_timeseries_end(id_=tms_id) processed_tide_ts = [] if method in ('TSF'):