# Purpose: Read the input file, take the square root of a column
#          and write the results to another file.
#          A simple file read/write example
# Usage: No arguments required.  Input file hard-coded.
# Output: Modified text file RDUout2.txt in the scratch directory.
import math, os
inputFile = "C:/gispy/data/ch19/RDUforest.txt"
outputFile = "C:/gispy/scratch/RDUout2.txt"

if os.path.exists(inputFile):
    inf = open(inputFile, 'r')
    outf = open(outputFile, 'w')

    header = inf.readline() # Read header.
    outf.write(header)      # Write header.

    for line in inf:
        # Modify the line.
        line = line.split()
        dbh = float(line[3])
        dbh = math.sqrt(dbh)
        
        # Write the modified line.
        line[3] = str(dbh)
        modLine = '\t'.join(line)
        outf.write(modLine + '\n')

    inf.close()
    outf.close()

    print("{0} created.".format(outputFile))

