#!/usr/bin/python
import datetime
import csv
import sys
#-------------------------------------------------------------------------------------------------------------+
# CREATE CSV FILE
#-------------------------------------------------------------------------------------------------------------+
def create_csv_file_indus():
    file_name = '/home/sindh/www-htdocs/pakistan_dss/uploads/user/DB/No_Breach_Model/indus.csv'
    with open(file_name,'wb') as csvfile:
        csvfileWriter = csv.writer(csvfile)
    csvfile.close()
#-------------------------------------------------------------------------------------------------------------+
#
#-------------------------------------------------------------------------------------------------------------+
def create_csv_file_chenab():
    file_name = '/home/sindh/www-htdocs/pakistan_dss/uploads/user/DB/No_Breach_Model/chenab.csv'
    with open(file_name,'wb') as csvfile:
        csvfileWriter = csv.writer(csvfile)
    csvfile.close()
#-------------------------------------------------------------------------------------------------------------+
# FUNCTION TO WRITE CSV FILE
#-------------------------------------------------------------------------------------------------------------+
def write_csv_indus_file(dt,data):
    file_pwd = '/home/sindh/www-htdocs/pakistan_dss/uploads/user/DB/No_Breach_Model/indus.csv'
    fc_data = [dt,data]
    with open(file_pwd,'a') as csvfile:
        csvfileWriter = csv.writer(csvfile)
        csvfileWriter.writerow(fc_data)
    csvfile.close()
#-------------------------------------------------------------------------------------------------------------+
#
#-------------------------------------------------------------------------------------------------------------+
def write_csv_chenab_file(dt,data):
    file_pwd = '/home/sindh/www-htdocs/pakistan_dss/uploads/user/DB/No_Breach_Model/chenab.csv'
    fc_data = [dt,data]
    with open(file_pwd,'a') as csvfile:
        csvfileWriter = csv.writer(csvfile)
        csvfileWriter.writerow(fc_data)
    csvfile.close()
#-------------------------------------------------------------------------------------------------------------+
username = sys.argv[1]
create_csv_file_indus()
create_csv_file_chenab()
row_num = 0
#fname = sys.argv[1]
file_name = '/home/sindh/www-htdocs/pakistan_dss/uploads/user/'+username+'/model_run.csv'
with open(file_name,'r') as csvfile:
    csvfileReader = csv.reader(csvfile,delimiter=',')
    for row in csvfileReader:
        if (row_num > 0):
            temp_dt = row[0].strip()
            #temp_dt_cv = datetime.datetime.strptime(temp_dt,'%d-%b-%y')
            # DSS FILE FORMAT DDMMMYYYY
            #final_dt = datetime.datetime.strftime(temp_dt_cv,'%d%b%Y')
            final_dt = temp_dt
            indus_q = row[1].strip()
            chenab_q = row[2].strip()
            # CREATE INDUS FILE FOR INPUT
            #write_csv_indus_file(final_dt,float(indus_q))
            write_csv_indus_file(final_dt,round((float(indus_q)*35.3147),2)) 
            # CREATE CHENAB FILE FOR INPUT
            #write_csv_chenab_file(final_dt,float(chenab_q))
            #write_csv_chenab_file(final_dt,round((float(chenab_q)*35.3147),2)) 
            if (float(chenab_q) < 58.0):
            	write_csv_chenab_file(final_dt,round((float(58.0)*35.3147),2))  # bcoz HecRAS becomes unstable with 0 flow. minimun flow of 2069 cusec given.
            else:
            	write_csv_chenab_file(final_dt,round((float(chenab_q)*35.3147),2)) 
        row_num = row_num + 1
csvfile.close()
