#!/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/hecras_model/Tori_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/hecras_model/Tori_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/hecras_model/Tori_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/hecras_model/Tori_Model/chenab.csv'
    fc_data = [dt,data]
    with open(file_pwd,'a') as csvfile:
        csvfileWriter = csv.writer(csvfile)
        csvfileWriter.writerow(fc_data)
    csvfile.close()
#-------------------------------------------------------------------------------------------------------------+
create_csv_file_indus()
create_csv_file_chenab()
row_num = 0
fname = sys.argv[1]
file_name = '/home/sindh/www-htdocs/pakistan_dss/uploads/hecras_model/Tori_Model/'+fname
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')
            indus_q = row[1].strip()
            chenab_q = row[2].strip()
            # CREATE INDUS FILE FOR INPUT
            write_csv_indus_file(final_dt,round((float(indus_q)),2)) 
            # CREATE CHENAB FILE FOR INPUT
            #write_csv_chenab_file(final_dt,float(chenab_q))
            if (float(chenab_q) < 2069.44):
            	write_csv_chenab_file(final_dt,round((float(2069.44)),2)) 
            else:
            	write_csv_chenab_file(final_dt,round((float(chenab_q)),2)) 
        row_num = row_num + 1
csvfile.close()
