Skip to content

Commit e2d9d01

Browse files
committed
modify api functions
1 parent ca370ac commit e2d9d01

File tree

6 files changed

+134
-11
lines changed

6 files changed

+134
-11
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
venv/
21
.idea/
32
template/
43
output/
4+
venv/

config.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515

1616
BACK_DAYS = 2
1717

18-
# MYSQL_USER = 'curw'
19-
# MYSQL_PASSWORD = 'curw'
20-
# MYSQL_HOST = '124.43.13.195'
21-
# MYSQL_PORT = '5036'
22-
# MYSQL_DB = 'curw'
2318
RAIN_FALL_FILE_NAME = '/home/hasitha/PycharmProjects/hechms_distributed/DailyRain-{}.csv'
19+
DISCHARGE_FILE_NAME = '/home/hasitha/PycharmProjects/hechms_distributed/DailyDischarge-{}.csv'
2420

2521
GAGE_MANAGER_TEMPLATE = 'Gage Manager: {MODEL_NAME}\n Version: 4.2.1\n Filepath Separator: \ \nEnd:'
2622

@@ -55,3 +51,21 @@
5551
'End:'
5652

5753
RUN_FILE_NAME = '/home/hasitha/PycharmProjects/hechms_distributed/{MODEL_NAME}.run'
54+
55+
HEC_HMS_HOME = '/home/curw/distributed_hec/hec-hms-421'
56+
HEC_HMS_SH = 'hec-hms.sh'
57+
HEC_DSSVUE_HOME = '/home/curw/distributed_hec/hec-dssvue201'
58+
HEC_DSSVUE_SH = 'hec-dssvue.sh'
59+
HEC_HMS_MODEL_DIR = '/home/curw/distributed_hec/distributed_model'
60+
BASIN_STATES_DIR = 'basinStates'
61+
HEC_EVENT = 'hec_event'
62+
PRE_PROCESSING_SCRIPT = 'CSVTODSS.py'
63+
POST_PROCESSING_SCRIPT = 'DSSTOCSV.py'
64+
HEC_INPUT_DSS = '{MODEL_NAME}_input.dss'
65+
HEC_OUTPUT_DSS = '{MODEL_NAME}_run.dss'
66+
CONTROL_FILE_NAME = '{MODEL_NAME}.control'
67+
GAGE_FILE_NAME = '{MODEL_NAME}.gage'
68+
RUN_FILE_NAME = '{MODEL_NAME}.run'
69+
STATE_INDEX_NAME = HEC_EVENT + '.stateIndex'
70+
HEC_EVENT_SCRIPT = '{MODEL_NAME}.script'
71+

controller.py

+71-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
from flask_json import FlaskJSON, JsonError, json_response
33
from flask_uploads import UploadSet, configure_uploads
44
from os import path
5-
from config import UPLOADS_DEFAULT_DEST, HECHMS_LIBS_DIR, DISTRIBUTED_MODEL_TEMPLATE_DIR, INIT_DATE_TIME_FORMAT, RAIN_FALL_FILE_NAME
6-
from input.shape_util.polygon_util import get_sub_ratios, get_timeseris, get_sub_catchment_rain_files, get_rain_files
5+
from datetime import datetime,timedelta
6+
from pathlib import Path
7+
8+
from config import UPLOADS_DEFAULT_DEST, INIT_DATE_TIME_FORMAT, RAIN_FALL_FILE_NAME, HEC_HMS_MODEL_DIR
9+
from input.shape_util.polygon_util import get_rain_files
710
from input.gage.model_gage import create_gage_file, create_gage_file_by_rain_file
811
from input.control.model_control import create_control_file, create_control_file_by_rain_file
912
from input.run.model_run import create_run_file
10-
from datetime import datetime,timedelta
11-
from pathlib import Path
13+
from model.model_execute import execute_pre_dssvue, execute_post_dssvue, execute_hechms
1214

1315

1416
app = Flask(__name__)
@@ -74,6 +76,70 @@ def init_run():
7476
return json_response(status_=200, run_id=run_id, description='Successfully saved files.')
7577

7678

79+
@app.route('/HECHMS/distributed/init', methods=['GET', 'POST'])
80+
@app.route('/HECHMS/distributed/init/<string:run_datetime>', methods=['GET', 'POST'])
81+
@app.route('/HECHMS/distributed/init/<string:run_datetime>/<int:back_days>/<int:forward_days>', methods=['GET', 'POST'])
82+
def prepare_input_files(run_datetime=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), back_days=2, forward_days=3):
83+
print('prepare_input_files.')
84+
print('run_datetime : ', run_datetime)
85+
print('back_days : ', back_days)
86+
print('forward_days : ', forward_days)
87+
run_datetime = datetime.strptime(run_datetime, '%Y-%m-%d %H:%M:%S')
88+
to_date = run_datetime + timedelta(days=forward_days)
89+
from_date = run_datetime - timedelta(days=back_days)
90+
file_date = run_datetime.strftime('%Y-%m-%d')
91+
from_date = from_date.strftime('%Y-%m-%d %H:%M:%S')
92+
to_date = to_date.strftime('%Y-%m-%d %H:%M:%S')
93+
file_name = RAIN_FALL_FILE_NAME.format(file_date)
94+
print('file_name : ', file_name)
95+
print('{from_date, to_date} : ', {from_date, to_date})
96+
get_rain_files(file_name, run_datetime.strftime('%Y-%m-%d %H:%M:%S'), forward_days, back_days)
97+
rain_fall_file = Path(file_name)
98+
if rain_fall_file.is_file():
99+
create_gage_file_by_rain_file('distributed_model', file_name)
100+
create_control_file_by_rain_file('distributed_model', file_name)
101+
else:
102+
create_gage_file('distributed_model', from_date, to_date)
103+
create_control_file('distributed_model', from_date, to_date)
104+
create_run_file('distributed_model', run_datetime.strftime('%Y-%m-%d %H:%M:%S'))
105+
106+
107+
@app.route('/HECHMS/distributed/pre-process', methods=['GET', 'POST'])
108+
@app.route('/HECHMS/distributed/pre-process/<string:run_datetime>', methods=['GET', 'POST'])
109+
def pre_processing(run_datetime=datetime.now().strftime('%Y-%m-%d %H:%M:%S')):
110+
print('pre_processing.')
111+
print('run_datetime : ', run_datetime)
112+
run_datetime = datetime.strptime(run_datetime, '%Y-%m-%d %H:%M:%S')
113+
ret_code = execute_pre_dssvue('distributed_model', run_datetime.strftime('%Y-%m-%d %H:%M:%S'))
114+
if ret_code == 0:
115+
return jsonify({'Result': 'Success'})
116+
else:
117+
return jsonify({'Result': 'Fail'})
118+
119+
120+
@app.route('/HECHMS/distributed/run', methods=['GET', 'POST'])
121+
def run_hec_hms_model():
122+
print('run_hec_hms_model.')
123+
ret_code = execute_hechms('distributed_model', HEC_HMS_MODEL_DIR)
124+
if ret_code == 0:
125+
return jsonify({'Result': 'Success'})
126+
else:
127+
return jsonify({'Result': 'Fail'})
128+
129+
130+
@app.route('/HECHMS/distributed/post-process', methods=['GET', 'POST'])
131+
@app.route('/HECHMS/distributed/post-process/<string:run_datetime>', methods=['GET', 'POST'])
132+
def post_processing(run_datetime=datetime.now().strftime('%Y-%m-%d %H:%M:%S')):
133+
print('post_processing.')
134+
print('run_datetime : ', run_datetime)
135+
run_datetime = datetime.strptime(run_datetime, '%Y-%m-%d %H:%M:%S')
136+
ret_code = execute_post_dssvue('distributed_model', run_datetime.strftime('%Y-%m-%d %H:%M:%S'))
137+
if ret_code == 0:
138+
return jsonify({'Result': 'Success'})
139+
else:
140+
return jsonify({'Result': 'Fail'})
141+
142+
77143
@app.route('/HECHMS/distributed/rain-fall', methods=['GET', 'POST'])
78144
@app.route('/HECHMS/distributed/rain-fall/<string:run_datetime>', methods=['GET', 'POST'])
79145
@app.route('/HECHMS/distributed/rain-fall/<string:run_datetime>/<int:back_days>/<int:forward_days>', methods=['GET', 'POST'])
@@ -111,7 +177,7 @@ def get_gage_file(run_datetime=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), bac
111177
file_date = run_datetime.strftime('%Y-%m-%d')
112178
from_date = from_date.strftime('%Y-%m-%d %H:%M:%S')
113179
to_date = to_date.strftime('%Y-%m-%d %H:%M:%S')
114-
file_name = 'output/DailyRain-{}.csv'.format(file_date)
180+
file_name = RAIN_FALL_FILE_NAME.format(file_date)
115181
rain_fall_file = Path(file_name)
116182
if rain_fall_file.is_file():
117183
create_gage_file_by_rain_file('distributed_model', file_name)

dags/__init__.py

Whitespace-only changes.

dags/hechms_distributed_dag.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import airflow
2+
from airflow import DAG
3+
from airflow.operators.python_operator import PythonOperator
4+

model/model_execute.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import subprocess
2+
from os import path
3+
4+
from config import HEC_HMS_HOME, HEC_HMS_SH, HEC_DSSVUE_HOME, HEC_DSSVUE_SH, HEC_EVENT_SCRIPT,\
5+
PRE_PROCESSING_SCRIPT,POST_PROCESSING_SCRIPT, RAIN_FALL_FILE_NAME,DISCHARGE_FILE_NAME, \
6+
HEC_INPUT_DSS, HEC_OUTPUT_DSS
7+
8+
9+
def execute_pre_dssvue(model_name, run_date_time):
10+
python_script_fp = PRE_PROCESSING_SCRIPT
11+
rain_csv_fp = RAIN_FALL_FILE_NAME.replace('{MODEL_NAME}', model_name).format(run_date_time)
12+
input_dss_fp = HEC_INPUT_DSS
13+
return _execute_hec_dssvue(python_script_fp, rain_csv_fp, input_dss_fp)
14+
15+
16+
def execute_post_dssvue(model_name, run_date_time):
17+
python_script_fp = POST_PROCESSING_SCRIPT
18+
discharge_csv_fp = DISCHARGE_FILE_NAME.replace('{MODEL_NAME}', model_name).format(run_date_time)
19+
output_dss_fp = HEC_OUTPUT_DSS
20+
return _execute_hec_dssvue(python_script_fp, discharge_csv_fp, output_dss_fp)
21+
22+
23+
def _execute_hec_dssvue(python_script_fp, csv_fp, dss_fp):
24+
dssvue_sh = path.join(HEC_DSSVUE_HOME, HEC_DSSVUE_SH)
25+
bash_command = '{dssvue_sh} {python_script} --csvfp {csv_fp} --dssfp {dss_fp}' \
26+
.format(dssvue_sh=dssvue_sh, python_script=python_script_fp, csv_fp=csv_fp, dss_fp=dss_fp)
27+
print('execute_hec_dssvue|bash_command : ', bash_command)
28+
ret_code = subprocess.call(bash_command, shell=True)
29+
return ret_code
30+
31+
32+
def execute_hechms(model_name, run_path):
33+
hec_hms_sh_fp = path.join(HEC_HMS_HOME, HEC_HMS_SH)
34+
model_event_script_fp = path.join(run_path, HEC_EVENT_SCRIPT.replace('{MODEL_NAME}', model_name))
35+
bash_command = "{hec_hms_sh} -s {hec_event_script}" \
36+
.format(hec_hms_sh=hec_hms_sh_fp, hec_event_script=model_event_script_fp)
37+
print('execute_hechms|bash_command : ', bash_command)
38+
ret_code = subprocess.call(bash_command, shell=True)
39+
return ret_code

0 commit comments

Comments
 (0)