from hec.heclib.dss import *
from hec.script import *
from hec.dataTable import *
import java
#import time
#from datetime import date
#from datetime import timedelta
#----------------------------------------------------------------------+
# 
#----------------------------------------------------------------------+
def read_export_flow(river_ml,flow_ml,dt_start,dt_end):
    try:
        dssfile = HecDss.open("/home/sindh/www-htdocs/pakistan_dss/DATA/ML/MODELS/Kot_Alam_Model/Sindh_Complete.dss",dt_start,dt_end)
        flow = dssfile.get("/"+river_ml+ "/"+flow_ml+"/FLOW//1DAY/SINDH_COMPLETE_PLAN/")
    except java.lang.Exception, e :
        # Take care of any missing data or errors
        MessageBox.showError(e.getMessage(), "Error reading data")
    # Add Data
    datasets = java.util.Vector()
    datasets.add(flow)
    # For this code, jython sees a List before a Vector
    list = []
    list.append(datasets)
    table = HecDataTableToExcel.newTable()
    # CREATE AND SAVE FILE
    table.createExcelFile(list, "/home/sindh/www-htdocs/pakistan_dss/DATA/ML/SCRIPTS/Kot_Alam_Model/DB_xs_output/flow/"+flow_ml+".xls")
#----------------------------------------------------------------------+
#
#----------------------------------------------------------------------+
def read_export_stage(river_ml,stage_ml,dt_start,dt_end):
    try:
        dssfile = HecDss.open("/home/sindh/www-htdocs/pakistan_dss/DATA/ML/MODELS/Kot_Alam_Model/Sindh_Complete.dss",dt_start,dt_end)
        stage = dssfile.get("/"+river_ml+"/"+stage_ml+"/STAGE//1DAY/SINDH_COMPLETE_PLAN/")
    except java.lang.Exception, e :
        # Take care of any missing data or errors
        MessageBox.showError(e.getMessage(), "Error reading data")
    # Add Data
    datasets = java.util.Vector()
    datasets.add(stage)
    # For this code, jython sees a List before a Vector
    list = []
    list.append(datasets)
    table = HecDataTableToExcel.newTable()
    # CREATE AND SAVE FILE
    table.createExcelFile(list, "/home/sindh/www-htdocs/pakistan_dss/DATA/ML/SCRIPTS/Kot_Alam_Model/DB_xs_output/stage/"+stage_ml+".xls")
#----------------------------------------------------------------------+
#
#----------------------------------------------------------------------+
def read_export_flow_barrage(river_ml,flow_ml,dt_start,dt_end,barrage_name):
    try:
        dssfile = HecDss.open("/home/sindh/www-htdocs/pakistan_dss/DATA/ML/MODELS/Kot_Alam_Model/Sindh_Complete.dss",dt_start,dt_end)
        flow = dssfile.get("/"+river_ml+ "/"+flow_ml+"/FLOW//1DAY/SINDH_COMPLETE_PLAN/")
    except java.lang.Exception, e :
        # Take care of any missing data or errors
        MessageBox.showError(e.getMessage(), "Error reading data")
    # Add Data
    datasets = java.util.Vector()
    datasets.add(flow)
    # For this code, jython sees a List before a Vector
    list = []
    list.append(datasets)
    table = HecDataTableToExcel.newTable()
    # CREATE AND SAVE FILE
    table.createExcelFile(list, "/home/sindh/www-htdocs/pakistan_dss/DATA/ML/SCRIPTS/Kot_Alam_Model/DB_xs_output/flow/"+barrage_name+".xls")
#----------------------------------------------------------------------+
# 
#----------------------------------------------------------------------+
def read_export_stage_barrage(river_ml,stage_ml,dt_start,dt_end,barrage_name):
    try:
        dssfile = HecDss.open("/home/sindh/www-htdocs/pakistan_dss/DATA/ML/MODELS/Kot_Alam_Model/Sindh_Complete.dss",dt_start,dt_end)
        stage = dssfile.get("/"+river_ml+"/"+stage_ml+"/STAGE//1DAY/SINDH_COMPLETE_PLAN/")
    except java.lang.Exception, e :
        # Take care of any missing data or errors
        MessageBox.showError(e.getMessage(), "Error reading data")
    # Add Data
    datasets = java.util.Vector()
    datasets.add(stage)
    # For this code, jython sees a List before a Vector
    list = []
    list.append(datasets)
    table = HecDataTableToExcel.newTable()
    # CREATE AND SAVE FILE
    table.createExcelFile(list, "/home/sindh/www-htdocs/pakistan_dss/DATA/ML/SCRIPTS/Kot_Alam_Model/DB_xs_output/stage/"+barrage_name+".xls")
#----------------------------------------------------------------------+
#
#----------------------------------------------------------------------+
# START/END DATE
#----------------------------------------------------------------------+
file = open('/home/sindh/www-htdocs/pakistan_dss/uploads/user/DB/Kot_Alam_Model/chenab.csv','r')
first_line = file.readline()
for last_line in file:
    pass
file.close()
data_first = first_line.strip()
data_arr_first = data_first.split(',')
start_dt = data_arr_first[0]
print(start_dt)
data_last = last_line.strip()
data_arr_last = data_last.split(',')
end_dt = data_arr_last[0]
print(end_dt)
#----------------------------------------------------------------------+
start_ml_dt = start_dt+" 2400"
end_ml_dt = end_dt+" 2400"
cross_section_upper_indus = ['1220698','1220165','1219064','1218713','1218016','1217008','1215987','1214992','1214087','1212668','1211536','1209193','1207009','1204989','1203249','1201209','1199804','1195710','1192688','1189457','1186742','1183635','1181801','1179469','1173629','1171419','1169968','1168237','1167233','1166705','1166027','1165778','1165340','1164184','1162497','1160860','1159837','1158408','1156528','1154694','1152892','1151131','1149794','1147954','1146096','1144367','1142258','1140337','1138928','1137286','1135717','1133771','1131423','1127713','1124061','1121158','1117157','1112287','1107702','1104448','1101920','1099162','1095711','1092800','1089907','1086841','1083888','1081723','1079469','1077015','1074097','1070377','1067958','1066145','1063546','1061063','1058008']
cross_section_lower_indus = ['1050584','1049224','1043919','1036598','1032268','1027668','1022432','1015596','1010611','1003221','997484','990491','985692','980464','973797','965743','961050','956022','951925','948198','944043','939963','936374','933406','929691','925970','922089','918888','915455','912566','909242','907343','906852','906189','905454','904730','903618','901048','899145','897068','894433','891577','888921','885765','881780','879257','876210','873704','868943','864026','859687','856875','853530','851329','848466','845446','842459','837936','835164','833529','833038','832151','828481','827306','826585','825687','824250','820482','818636','817569','816315','814826','813652','812321','811042','810073','809120','808044','806833','801523','799880','796759','793046','791031','789260','787855','784963','779686','761809','759226','756421','753564','751302','749364','747751','745699','743921','742504','741499','740544','739231','738029','737187','736043','735252','734484','733260','732560','731468','730592','729523','728612','727589','726919','726199','725378','723747','721501','720321','718948','716603','714736','713165','711987','708454','704005','702155','700116','698862','697727','696256','693163','689682','679070','675456','667378','664339','661585','658870','653769','650558','642264','638271','634686','632871','630178','628487','627733','626766','625822','624518','623228','621907','620497','609958','608191','604124','601544','598285','596412','594410','591443','589004','584365','579580','574111','570225','567471','562120','554108','551809','549704','546957','543775','541895','539132','537725','535303','529999','527348','524923','522010','514686','513562','506884','504539','502775','500565','497911','495921','493084','490364','488270','485953','484700','482341','479423','477601','473092','470766','468451','465070','462555','458305','456746','454590','452402','450640','448812','446584','444514','440792','439772','435550','430588','426763','416845','409150','406359','400615','398253','395874','393588','390717','382229','378034','374730','369801','366252','361428','357779','353038','346049','341144','337872','329116','320428','310190','306802','301615','297430','293925','286716','284554','281642','279486','277144','275395','274797','274180','272609','271837','270689','269931','268490','266766','265695','264099','261897','260254','258024','255410','253684','251126','247733','242585','234797','230847','221486','218815','216421','211790','209974','207050','203254','200056','192559','186457','182451','179355','177976','173885','165929','163416','161127','155954','146342','142681','135539','132777','128073','125300','117218','111490','107770','103850','97004','91287','87252','82954','79058','75922','71837','67355','63827','56051','42776','38937','36046','33969']
cross_section_chenab= ['49955','49107','46897','45583','43100','40785','36411','34234','31161','28600','25582','22576','20641','18648','15639','11773','7658','5048','3094','1298']
#----------------------------------------------------------------------+
# FOR RIVER 1 UPPER INDUS 
#----------------------------------------------------------------------+
for i in range(len(cross_section_upper_indus)):
    # FLOW
    read_export_flow('RIVER 1 UPPER INDUS',cross_section_upper_indus[i],start_ml_dt,end_ml_dt)
    # STAGE
    read_export_stage('RIVER 1 UPPER INDUS',cross_section_upper_indus[i],start_ml_dt,end_ml_dt)
#----------------------------------------------------------------------+
# FOR RIVER 1 LOWER INDUS
#----------------------------------------------------------------------+
for i in range(len(cross_section_lower_indus)):
    # FLOW
    read_export_flow('RIVER 1 LOWER INDUS',cross_section_lower_indus[i],start_ml_dt,end_ml_dt)
    # STAGE
    read_export_stage('RIVER 1 LOWER INDUS',cross_section_lower_indus[i],start_ml_dt,end_ml_dt)
#----------------------------------------------------------------------+
# FOR RIVER 2 CHENAB
#----------------------------------------------------------------------+
for i in range(len(cross_section_chenab)):
    # FLOW
    read_export_flow('RIVER 2 CHENAB RIVER',cross_section_chenab[i],start_ml_dt,end_ml_dt)
    # STAGE
    read_export_stage('RIVER 2 CHENAB RIVER',cross_section_chenab[i],start_ml_dt,end_ml_dt)
#----------------------------------------------------------------------+
barrage_upper = ['1220698']
upper_name = ['Taunsa']
barrage_lower = ['904730','733260','274180']
lower_name = ['Guddu','Sukkur','Kotri']
barrage_chenab = ['49955']
chenab_name = ['Punjnad']
#----------------------------------------------------------------------+
for i in range(len(barrage_upper)):
    # FLOW
    read_export_flow_barrage('RIVER 1 UPPER INDUS',barrage_upper[i],start_ml_dt,end_ml_dt,upper_name[i])
    # STAGE
    read_export_stage_barrage('RIVER 1 UPPER INDUS',barrage_upper[i],start_ml_dt,end_ml_dt,upper_name[i])
#----------------------------------------------------------------------+
for i in range(len(barrage_lower)):
    # FLOW
    read_export_flow_barrage('RIVER 1 LOWER INDUS',barrage_lower[i],start_ml_dt,end_ml_dt,lower_name[i])
    # STAGE
    read_export_stage_barrage('RIVER 1 LOWER INDUS',barrage_lower[i],start_ml_dt,end_ml_dt,lower_name[i])
#----------------------------------------------------------------------+
for i in range(len(barrage_chenab)):
    # FLOW
    read_export_flow_barrage('RIVER 2 CHENAB RIVER',barrage_chenab[i],start_ml_dt,end_ml_dt,chenab_name[i])
    # STAGE
    read_export_stage_barrage('RIVER 2 CHENAB RIVER',barrage_chenab[i],start_ml_dt,end_ml_dt,chenab_name[i])
#----------------------------------------------------------------------+
