def update_discharge_from_hechms(curw_sim_pool, curw_fcst_pool, flo2d_model, method, start_time, end_time, sim_tag): try: TS = DTimeseries(pool=curw_sim_pool) # [station_name,latitude,longitude,target,model,version,sim_tag,station] extract_stations = read_csv( os.path.join(ROOT_DIR, 'grids/discharge_stations/flo2d_stations.csv')) for i in range(len(extract_stations)): station_name = extract_stations[i][0] latitude = extract_stations[i][1] longitude = extract_stations[i][2] target_model = extract_stations[i][3] if target_model == flo2d_model: if station_name in ("glencourse"): meta_data = { 'latitude': float('%.6f' % float(latitude)), 'longitude': float('%.6f' % float(longitude)), 'model': target_model, 'method': method, 'grid_id': 'discharge_{}'.format(station_name) } 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_discharge_ts = process_fcst_ts_from_hechms_outputs( curw_fcst_pool=curw_fcst_pool, extract_stations=extract_stations, i=i, start=start_time, end=end_time, sim_tag=sim_tag) if processed_discharge_ts is not None and len( processed_discharge_ts) > 0: TS.insert_data(timeseries=processed_discharge_ts, tms_id=tms_id, upsert=True) else: continue # skip the current iteration except Exception as e: traceback.print_exc()
def update_discharge_obs(curw_sim_pool, flo2d_model, method, timestep, start_time, end_time): try: discharge_TS = DTimeseries(pool=curw_sim_pool) waterlevel_TS = WLTimeseries(pool=curw_sim_pool) # [station_name,latitude,longitude,target] extract_stations = read_csv( 'grids/discharge_stations/flo2d_stations.csv') extract_stations_dict = { } # keys: target_model , value: [latitude, longitude, station_name] # 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': 'discharge_{}'.format(station_name) } wl_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) } tms_id = discharge_TS.get_timeseries_id_if_exists(meta_data=meta_data) wl_tms_id = waterlevel_TS.get_timeseries_id_if_exists( meta_data=wl_meta_data) if wl_tms_id is None: print("Warning!!! {} waterlevel timeseries doesn't exist.".format( station_name)) exit(1) timeseries = [] if tms_id is None: tms_id = discharge_TS.generate_timeseries_id(meta_data=meta_data) meta_data['id'] = tms_id discharge_TS.insert_run(meta_data=meta_data) wl_timeseries = waterlevel_TS.get_timeseries(id_=wl_tms_id, start_date=start_time, end_date=end_time) estimated_discharge_ts = [] if station_name == 'hanwella': estimated_discharge_ts = calculate_hanwella_discharge( wl_timeseries) elif station_name == 'glencourse': estimated_discharge_ts = calculate_glencourse_discharge( wl_timeseries) if estimated_discharge_ts is not None and len( estimated_discharge_ts) > 0: discharge_TS.insert_data(timeseries=estimated_discharge_ts, tms_id=tms_id, upsert=True) except Exception as e: traceback.print_exc()
target_model = extract_stations[i][3] method = extract_stations[i][4] meta_data = { 'latitude': float('%.6f' % float(latitude)), 'longitude': float('%.6f' % float(longitude)), 'model': target_model, 'method': method, 'grid_id': 'discharge_{}'.format(station_name) } 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) existing_ts_end = TS.get_obs_end(id_=tms_id) processed_discharge_ts = [] if existing_ts_end is None: fcst_start = datetime.now() - timedelta(days=10) else: fcst_start = existing_ts_end + timedelta(hours=1) if method in ('SF'): # process fcst ts from statistical forecasts try: timeseries = read_csv('{}/{}.csv'.format(INPUT_DIR, station_name)) processed_discharge_ts = process_fcsts_from_csv(timeseries=timeseries, fcst_start=fcst_start) except FileNotFoundError as fe:
if wl_tms_id is None: print("Warning!!! {} waterlevel timeseries doesn't exist.". format(station_name)) continue end_time = (datetime.now() + timedelta( hours=5, minutes=30)).strftime(COMMON_DATE_TIME_FORMAT) timeseries = [] if tms_id is None: tms_id = discharge_TS.generate_timeseries_id( meta_data=meta_data) meta_data['id'] = tms_id discharge_TS.insert_run(meta_data=meta_data) start = (datetime.now() - timedelta(days=10)).strftime(COMMON_DATE_TIME_FORMAT) else: obs_end = discharge_TS.get_obs_end(id_=tms_id) if obs_end is None: start = ( datetime.now() - timedelta(days=10)).strftime(COMMON_DATE_TIME_FORMAT) else: start = ( obs_end - timedelta(days=1)).strftime(COMMON_DATE_TIME_FORMAT) wl_timeseries = waterlevel_TS.get_timeseries(id_=wl_tms_id, start_date=start,