Ignore:
Timestamp:
Sep 22, 2004, 6:04:29 PM (21 years ago)
Author:
duncan
Message:

added 1st iteration of loading a .tsh and .xya file and returning a .tsh with fitted attribute values

Location:
inundation/ga/storm_surge/pyvolution
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/least_squares.py

    r339 r342  
    331331   
    332332    import os, sys
    333     from load_mesh.loadASCII import mesh_file_to_mesh_dictionary
    334     usage = "usage: %s pmesh_file_name" %         os.path.basename(sys.argv[0])
    335 
    336     if len(sys.argv) < 2:
     333    from load_mesh.loadASCII import mesh_file_to_mesh_dictionary, \
     334                 load_xya_file, export_trianglulation_file
     335    usage = "usage: %s mesh_input.tsh point.xya  mesh_output.tsh" %         os.path.basename(sys.argv[0])
     336
     337    if len(sys.argv) < 4:
    337338        print usage
    338339    else:
    339         mesh_file_name = sys.argv[1]
    340 
    341         meshdic = mesh_file_to_mesh_dictionary(mesh_file_name)
    342         vertex_coordinates = meshdic['generatedpointlist']
    343         triangles = meshdic['generatedtrianglelist']
    344        
    345         d1 = [1.0, 1.0]
    346         d2 = [1.0, 3.0]
    347         d3 = [3.0,1.0]
    348         z1 = 2
    349         z2 = 4
    350         z3 = 4
    351         data_coords = [ d1, d2, d3]
    352         z = [z1, z2, z3]
     340        mesh_file = sys.argv[1]
     341        point_file = sys.argv[2]
     342        mesh_output_file = sys.argv[3]
     343
     344        # load in the .tsh file
     345        mesh_dic = mesh_file_to_mesh_dictionary(mesh_file)
     346        vertex_coordinates = mesh_dic['generatedpointlist']
     347        triangles = mesh_dic['generatedtrianglelist']
     348
     349        # load in the .xya file
     350        point_dict = load_xya_file(point_file)
     351        point_coordinates = point_dict['pointlist']
     352        point_attributes = point_dict['pointattributelist']
     353       
    353354       
    354355        f = fit_to_mesh(vertex_coordinates,
    355356                                      triangles,
    356                                       data_coords,
    357                                       z)
    358         print f
    359        
    360 
    361 
    362 
    363 
    364 
    365 
    366 
    367 
    368 
    369 
    370 
     357                                      point_coordinates,
     358                                      point_attributes)
     359        # convert array to list of lists
     360        mesh_dic['generatedpointattributelist'] = f.tolist()
     361        export_trianglulation_file(mesh_output_file, mesh_dic)
     362        #FIXME  do unit test
     363       
     364       
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
  • inundation/ga/storm_surge/pyvolution/load_mesh/loadASCII.py

    r333 r342  
    258258    return meshDict
    259259
     260def clean_line(line,delimiter):     
     261    """Remove whitespace
     262    """
     263    #print ">%s" %line
     264    line = line.strip()
     265    #print "stripped>%s" %line
     266    numbers = line.split(delimiter)
     267    i = len(numbers) - 1
     268    while i >= 0:
     269        if numbers[i] == '':
     270            numbers.pop(i)
     271        i += -1
     272    #for num in numbers:
     273    #    print "num>%s<" %num
     274    return numbers
     275
     276def write_ASCII_trianglulation(fd,
     277                               gen_dict):
     278    vertices = gen_dict['generatedpointlist']
     279    vertices_attributes = gen_dict['generatedpointattributelist']
     280    triangles = gen_dict['generatedtrianglelist']
     281    triangles_attributes = gen_dict['generatedtriangleattributelist']
     282    triangle_neighbors = gen_dict['generatedtriangleneighborlist']
     283       
     284    segments = gen_dict['generatedsegmentlist']
     285    segment_markers = gen_dict['generatedsegmentmarkerlist']
     286     
     287    numVert = str(len(vertices))
     288    if (numVert == "0"):
     289        numVertAttrib = "0"
     290    else:
     291        numVertAttrib = str(len(vertices_attributes[0]))
     292    fd.write(numVert + " " + numVertAttrib + " # <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." + "\n")
     293
     294    #<vertex #> <x> <y> [attributes]   
     295    index = 0
     296    for vert in vertices:
     297        attlist = ""
     298        for att in vertices_attributes[index]:
     299            attlist = attlist + str(att)+" "
     300        attlist.strip()
     301        fd.write(str(index) + " "
     302                 + str(vert[0]) + " "
     303                 + str(vert[1]) + " "
     304                 + attlist + "\n")
     305        index += 1
     306       
     307    #<# of triangles>
     308    n = len(triangles)
     309    fd.write(str(n) + " # <triangle #> [<vertex #>] [<neigbouring triangle #>] [attribute of region] ...Triangulation Triangles..." + "\n")
     310       
     311    # <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
     312    for index in range(n):
     313        neighbors = ""
     314        tri = triangles[index]
     315        for neighbor in triangle_neighbors[index]:
     316            if neighbor:
     317                neighbors += str(neighbor) + " "
     318            else:
     319                if neighbor == 0:
     320                    neighbors +=  "0 "
     321                else:
     322                    neighbors +=  "-1 "
     323               
     324        fd.write(str(index) + " "
     325                 + str(tri[0]) + " "
     326                 + str(tri[1]) + " "
     327                 + str(tri[2]) + " "
     328                 + neighbors + " "
     329                 + str(triangles_attributes[index]) + "\n")
     330           
     331    #One line:  <# of segments>
     332    fd.write(str(len(segments)) +
     333             " # <segment #> <vertex #>  <vertex #> [boundary marker] ...Triangulation Segments..." + "\n")
     334       
     335    #Following lines:  <segment #> <vertex #>  <vertex #> [boundary marker]
     336    for i in range(len(segments)):
     337        seg = segments[i]
     338        fd.write(str(i) + " "
     339                 + str(seg[0]) + " "
     340                 + str(seg[1]) + " "
     341                 + str(segment_markers[i]) + "\n")
     342
     343
     344def export_trianglulation_file(ofile,gen_dict):
     345    """
     346    write a file, ofile, with the format
     347   
     348    First line:  <# of vertices> <# of attributes>
     349    Following lines:  <vertex #> <x> <y> [attributes]
     350    One line:  <# of triangles>
     351    Following lines:  <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
     352    One line:  <# of segments>
     353    Following lines:  <segment #> <vertex #>  <vertex #> [boundary marker]
     354    """
     355    try:
     356        fd = open(ofile,'w')
     357        write_ASCII_trianglulation(fd,gen_dict)
     358        fd.close()
     359    except IOError, e:       
     360        msg = 'Could not write file %s ' %fileName
     361        raise IOError, msg
     362
    260363###
    261364#  LOADING XYA FILES
     
    263366 
    264367           
    265 def load_xya_file(ofile,delimiter):
     368def load_xya_file(ofile,delimiter = ','):
    266369    """
    267370    load a file, ofile, with the format
     
    326429    return xya_dict
    327430
    328 def clean_line(line,delimiter):     
    329     """Remove whitespace
    330     """
    331     #print ">%s" %line
    332     line = line.strip()
    333     #print "stripped>%s" %line
    334     numbers = line.split(delimiter)
    335     i = len(numbers) - 1
    336     while i >= 0:
    337         if numbers[i] == '':
    338             numbers.pop(i)
    339         i += -1
    340     #for num in numbers:
    341     #    print "num>%s<" %num
    342     return numbers
    343 
    344431if __name__ == "__main__":
    345432    m = import_mesh("tee.txt")
Note: See TracChangeset for help on using the changeset viewer.