Index: inundation/ga/storm_surge/pyvolution/least_squares.py
===================================================================
--- inundation/ga/storm_surge/pyvolution/least_squares.py	(revision 341)
+++ inundation/ga/storm_surge/pyvolution/least_squares.py	(revision 342)
@@ -331,40 +331,45 @@
     
     import os, sys
-    from load_mesh.loadASCII import mesh_file_to_mesh_dictionary
-    usage = "usage: %s pmesh_file_name" %         os.path.basename(sys.argv[0])
-
-    if len(sys.argv) < 2:
+    from load_mesh.loadASCII import mesh_file_to_mesh_dictionary, \
+                 load_xya_file, export_trianglulation_file
+    usage = "usage: %s mesh_input.tsh point.xya  mesh_output.tsh" %         os.path.basename(sys.argv[0])
+
+    if len(sys.argv) < 4:
         print usage
     else:
-        mesh_file_name = sys.argv[1]
-
-        meshdic = mesh_file_to_mesh_dictionary(mesh_file_name)
-        vertex_coordinates = meshdic['generatedpointlist']
-        triangles = meshdic['generatedtrianglelist']
-        
-        d1 = [1.0, 1.0]
-        d2 = [1.0, 3.0]
-        d3 = [3.0,1.0]
-        z1 = 2
-        z2 = 4
-        z3 = 4
-        data_coords = [ d1, d2, d3]
-        z = [z1, z2, z3]
+        mesh_file = sys.argv[1]
+        point_file = sys.argv[2]
+        mesh_output_file = sys.argv[3]
+
+        # load in the .tsh file
+        mesh_dic = mesh_file_to_mesh_dictionary(mesh_file)
+        vertex_coordinates = mesh_dic['generatedpointlist']
+        triangles = mesh_dic['generatedtrianglelist']
+
+        # load in the .xya file
+        point_dict = load_xya_file(point_file)
+        point_coordinates = point_dict['pointlist']
+        point_attributes = point_dict['pointattributelist']
+        
         
         f = fit_to_mesh(vertex_coordinates,
                                       triangles,
-                                      data_coords,
-                                      z)
-        print f
-        
-
-
-
-
-
-
-
-
-
-
-
+                                      point_coordinates,
+                                      point_attributes)
+        # convert array to list of lists
+        mesh_dic['generatedpointattributelist'] = f.tolist()
+        export_trianglulation_file(mesh_output_file, mesh_dic)
+        #FIXME  do unit test
+        
+        
+
+
+
+
+
+
+
+
+
+
+
Index: inundation/ga/storm_surge/pyvolution/load_mesh/loadASCII.py
===================================================================
--- inundation/ga/storm_surge/pyvolution/load_mesh/loadASCII.py	(revision 341)
+++ inundation/ga/storm_surge/pyvolution/load_mesh/loadASCII.py	(revision 342)
@@ -258,4 +258,107 @@
     return meshDict
 
+def clean_line(line,delimiter):      
+    """Remove whitespace
+    """
+    #print ">%s" %line
+    line = line.strip()
+    #print "stripped>%s" %line
+    numbers = line.split(delimiter)
+    i = len(numbers) - 1
+    while i >= 0:
+        if numbers[i] == '':
+            numbers.pop(i)
+        i += -1
+    #for num in numbers:
+    #    print "num>%s<" %num
+    return numbers
+
+def write_ASCII_trianglulation(fd,
+                               gen_dict):
+    vertices = gen_dict['generatedpointlist']
+    vertices_attributes = gen_dict['generatedpointattributelist']
+    triangles = gen_dict['generatedtrianglelist']
+    triangles_attributes = gen_dict['generatedtriangleattributelist']
+    triangle_neighbors = gen_dict['generatedtriangleneighborlist']
+        
+    segments = gen_dict['generatedsegmentlist']
+    segment_markers = gen_dict['generatedsegmentmarkerlist']
+     
+    numVert = str(len(vertices))
+    if (numVert == "0"):
+        numVertAttrib = "0"
+    else:
+        numVertAttrib = str(len(vertices_attributes[0]))
+    fd.write(numVert + " " + numVertAttrib + " # <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." + "\n")
+
+    #<vertex #> <x> <y> [attributes]    
+    index = 0 
+    for vert in vertices:
+        attlist = ""
+        for att in vertices_attributes[index]:
+            attlist = attlist + str(att)+" "
+        attlist.strip()
+        fd.write(str(index) + " "
+                 + str(vert[0]) + " "
+                 + str(vert[1]) + " "
+                 + attlist + "\n")
+        index += 1
+        
+    #<# of triangles>
+    n = len(triangles)
+    fd.write(str(n) + " # <triangle #> [<vertex #>] [<neigbouring triangle #>] [attribute of region] ...Triangulation Triangles..." + "\n")
+        
+    # <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
+    for index in range(n):
+        neighbors = ""
+        tri = triangles[index]
+        for neighbor in triangle_neighbors[index]:
+            if neighbor:
+                neighbors += str(neighbor) + " "
+            else:
+                if neighbor == 0:
+                    neighbors +=  "0 "
+                else:
+                    neighbors +=  "-1 "
+                
+        fd.write(str(index) + " "
+                 + str(tri[0]) + " " 
+                 + str(tri[1]) + " " 
+                 + str(tri[2]) + " " 
+                 + neighbors + " "
+                 + str(triangles_attributes[index]) + "\n")
+            
+    #One line:  <# of segments> 
+    fd.write(str(len(segments)) + 
+             " # <segment #> <vertex #>  <vertex #> [boundary marker] ...Triangulation Segments..." + "\n")
+        
+    #Following lines:  <segment #> <vertex #>  <vertex #> [boundary marker]
+    for i in range(len(segments)):
+        seg = segments[i]
+        fd.write(str(i) + " "
+                 + str(seg[0]) + " " 
+                 + str(seg[1]) + " " 
+                 + str(segment_markers[i]) + "\n")
+
+
+def export_trianglulation_file(ofile,gen_dict):
+    """
+    write a file, ofile, with the format
+    
+    First line:  <# of vertices> <# of attributes>
+    Following lines:  <vertex #> <x> <y> [attributes]
+    One line:  <# of triangles> 
+    Following lines:  <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
+    One line:  <# of segments> 
+    Following lines:  <segment #> <vertex #>  <vertex #> [boundary marker]
+    """
+    try:
+        fd = open(ofile,'w')
+        write_ASCII_trianglulation(fd,gen_dict)
+        fd.close()
+    except IOError, e:        
+        msg = 'Could not write file %s ' %fileName
+        raise IOError, msg 
+
 ###
 #  LOADING XYA FILES
@@ -263,5 +366,5 @@
   
             
-def load_xya_file(ofile,delimiter):
+def load_xya_file(ofile,delimiter = ','):
     """
     load a file, ofile, with the format
@@ -326,20 +429,4 @@
     return xya_dict
 
-def clean_line(line,delimiter):      
-    """Remove whitespace
-    """
-    #print ">%s" %line
-    line = line.strip()
-    #print "stripped>%s" %line
-    numbers = line.split(delimiter)
-    i = len(numbers) - 1
-    while i >= 0:
-        if numbers[i] == '':
-            numbers.pop(i)
-        i += -1
-    #for num in numbers:
-    #    print "num>%s<" %num
-    return numbers
-
 if __name__ == "__main__":
     m = import_mesh("tee.txt")
