"""Simple water flow example using ANUGA
Water flowing down a channel
"""
#------------------------------------------------------------------------------
# Import necessary modules
#------------------------------------------------------------------------------
import anuga
import subprocess
import csv
import os
import time
from anuga.abstract_2d_finite_volumes.util import add_directories
from anuga.utilities.log_analyser import analyse_log

#--------------------------------------------------------------------------------------
# Set up variables for the correct directories to store the output
#--------------------------------------------------------------------------------------
host = os.getenv('HOST')
home = os.getenv('INUNDATIONHOME')
scenariodir = add_directories(home, ["data","mem_time_test", "parallel1","cairns"])
meta_path = os.path.join(scenariodir, 'metalog.csv')
final_path = os.path.join(scenariodir, 'final.csv')

# main loop that runs macpus - 1 times
maxcpus = 4

for n in range(1,maxcpus,1):
    #the necessary ways to run this script in parallel on the different hosts we use
    if (host == 'cyclone.agso.gov.au'):
        subprocess.call(['mpirun', '-np', str(n), '-hostfile' ,'~/machinefiles/test.machines_cyclone', '-x','PYTHONPATH','-x','INUNDATIONHOME','python', 'runcairns.py']) 
    elif (host == 'tornado.agso.gov.au'):
        subprocess.call(['mpirun', '-np', str(n), '-hostfile' ,'~/machinefiles/test.machines_tornado', '-x','PYTHONPATH','-x','INUNDATIONHOME','python', 'runcairns.py'])
    elif (host == 'rhe-compute1.ga.gov.au'):
        subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x','INUNDATIONHOME','python2.6', 'runcairns.py']) 
    else:
        subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x','INUNDATIONHOME','python', 'runcairns.py'])
     
print 'DONE'

#get the important data for the experiments from the anuga experiments
analyse_log(scenariodir, os.path.join(scenariodir,'metalog.csv')) 

#open files to read from and write to
metalog = csv.reader(open(meta_path,'rb'))
final = csv.writer(open(final_path,'wb'))

#list stores the index of the values requird
indexlist = []

#read in the first row
firstrow = metalog.next()

#get the indices of the values we want, so that the data can be condensed
indexlist.append(firstrow.index("beforetime"))
indexlist.append(firstrow.index("aftertime"))
indexlist.append(firstrow.index("aftermeshtime"))
indexlist.append(firstrow.index("beforesimulationmemory"))
indexlist.append(firstrow.index("aftermeshmemory"))
indexlist.append(firstrow.index("afterinitialconditionsmemory"))
indexlist.append(firstrow.index("afterboundarymemory"))
indexlist.append(firstrow.index("aftersimulationmemory"))
indexlist.append(firstrow.index("numberofcpus"))
indexlist.append(firstrow.index("myid"))


#write the header for the final csv
final.writerow(["TimeTaken(s)","Time For Mesh (s)",
                firstrow[(indexlist[3])],firstrow[(indexlist[4])],
                firstrow[(indexlist[5])],firstrow[(indexlist[6])],
                firstrow[(indexlist[7])],firstrow[(indexlist[8])],
                firstrow[(indexlist[9])]])

#write the data for each column in the final csv
for row in metalog:

    #manipulate the beginning and end time to get the time taken
    begin = time.strptime(row[(indexlist[0])],'%Y%m%d_%H%M%S')
    end = time.strptime(row[(indexlist[1])],'%Y%m%d_%H%M%S')
    mesh = time.strptime(row[(indexlist[2])],'%Y%m%d_%H%M%S')
    taken = time.mktime(end) - time.mktime(begin)
    meshtime = time.mktime(mesh) - time.mktime(begin)

    #write to file
    final.writerow([str(taken),str(meshtime),
                    row[(indexlist[3])],row[(indexlist[4])],
                    row[(indexlist[5])],row[(indexlist[6])],
                    row[(indexlist[7])],row[(indexlist[8])],
                    row[(indexlist[9])]])

