# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Volume Analysis.py
# Created on: 2013-05-10 08:29:26.00000
# Author : Shakir Ahmed, GISP,SFWMD.GOV
# Description: Calculate volume analysis from TIN
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
import os
import string
import sys
import shutil
import math
import time
# Check out any necessary licenses
arcpy.CheckOutExtension("3D")

# All input variables.
My_surface= sys.argv[1]
my_file = sys.argv[2]
surface = sys.argv[3]
n= sys.argv[4]
t= sys.argv[5]
increment = sys.argv[6]

n = int(n)
t = int(t)
t=t+1
increment = float(increment)
####### Local variables:
#My_surface = "T:\\applications\\tst\\py\\RettThompson\\Oak Creek\\Grove Resrv N\\thetin"

txt_file = my_file[:-4]

f = open('%s_new.txt'%(txt_file),'w')
m = 1
# Process: Surface Volume
i = 1
while t >= n:
      
    print n
    #print y
   
    arcpy.SurfaceVolume_3d(My_surface, my_file, surface, n, 1, "0")
    print 'ok at this point'
    
    inFile = '%s_%s.txt'%(txt_file,i)
    shutil.copy(my_file, inFile)
    os.remove(my_file)
    
    startline = 1
    linesCounter = 1
    inFile = file("%s_%s.txt"%(txt_file,i))
    f = open('%s_new.txt'%(txt_file),'a')
    
    for line in inFile.readlines():
        print line
        if m == 1:
            f.write(line)
        else:
            if linesCounter >1:
                f.write(line)
            else:
                print 'not a valid line'

        linesCounter +=1
    
    inFile.close()
    os.remove("%s_%s.txt"%(txt_file,i))
    print i
    i = i+1
    n = n+increment
     
    m = m+1
    print 'moving %s'%n
else:
    print 'the loop is over'
print my_file
shutil.copy('%s_new.txt'%(txt_file), my_file)
f.close()
os.remove('%s_new.txt'%(txt_file))
print 'All Done Successfully'
