Changeset 6074
- Timestamp:
- Dec 12, 2008, 1:22:18 PM (15 years ago)
- Location:
- anuga_core/source/anuga/load_mesh
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/load_mesh/loadASCII.py
r4903 r6074 1 1 """ 2 3 2 The format for a Points dictionary is: 4 3 … … 12 11 dic['attributelist']['elevation'] = [[7.0,5.0] 13 12 14 13 15 14 The dict format for IO with mesh files is; 16 15 (the triangulation) … … 18 17 vertex_attributes: [[a11,a12,...],[a21,a22],...] (lists of doubles) 19 18 vertex_attribute_titles:[A1Title, A2Title ...] (A list of strings) 20 segments: [[v1,v2],[v3,v4],...] (lists of integers) 19 segments: [[v1,v2],[v3,v4],...] (lists of integers) 21 20 segment_tags : [tag,tag,...] list of strings 22 21 triangles : [(v1,v2,v3), (v4,v5,v6),....] lists of points 23 triangle_tags: [s1,s2,...] A list of strings 22 triangle_tags: [s1,s2,...] A list of strings 24 23 triangle_neighbors: [[t1,t2,t3], [t4,t5,t6],..] lists of triangles 25 26 (the outline) 24 25 (the outline) 27 26 points: [[x1,y1],[x2,y2],...] (lists of doubles) 28 27 point_attributes: [[a11,a12,...],[a21,a22],...] (lists of doubles) 29 outline_segments: [[point1,point2],[p3,p4],...] (lists of integers) 28 outline_segments: [[point1,point2],[p3,p4],...] (lists of integers) 30 29 outline_segment_tags : [tag1,tag2,...] list of strings 31 30 holes : [[x1,y1],...](List of doubles, one inside each hole region) … … 44 43 45 44 The format for a .tsh file is (FIXME update this) 46 45 47 46 First line: <# of vertices> <# of attributes> 48 47 Following lines: <vertex #> <x> <y> [attributes] 49 One line: <# of triangles> 50 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] 51 One line: <# of segments> 48 One line: <# of triangles> 49 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> 50 <neigbouring triangle #> <neigbouring triangle #> 51 <neigbouring triangle #> [attribute of region] 52 One line: <# of segments> 52 53 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 53 54 """ … … 57 58 58 59 from string import find, rfind 59 from Numeric import array, Float, Int16, Int32, Character,reshape, concatenate, take 60 from Numeric import array, Float, Int16, Int32, Character,reshape, \ 61 concatenate, take 60 62 from os.path import splitext 61 63 62 from anuga.coordinate_transforms.geo_reference import Geo_reference,TITLE, TitleError 64 from anuga.coordinate_transforms.geo_reference import Geo_reference, TITLE, \ 65 TitleError 63 66 64 67 from Scientific.IO.NetCDF import NetCDFFile 65 68 66 69 import exceptions 67 70 class TitleAmountError(exceptions.Exception): pass 68 71 72 69 73 NOMAXAREA=-999 70 74 71 # This is used by pmesh 75 76 ## 77 # @brief Read a mesh file (.tsh or .msh) and return a dictionary. 78 # @param ofile Path to the file to read. 79 # @return A dictionary of data from the input file. 80 # @note Throws IOError if can't read file. 81 # @note This is used by pmesh. 72 82 def import_mesh_file(ofile): 73 83 """ 74 84 read a mesh file, either .tsh or .msh 75 85 76 86 Note: will throw an IOError if it can't load the file. 77 87 Catch these! … … 82 92 elif ofile[-4:]== ".msh": 83 93 dict = _read_msh_file(ofile) 84 else: 85 msg = 'Extension .%s is unknown' % ofile[-4:]94 else: 95 msg = 'Extension .%s is unknown' % ofile[-4:] 86 96 raise IOError, msg 87 except (TitleError,SyntaxError,IndexError, ValueError): #FIXME No test for ValueError 97 #FIXME No test for ValueError 98 except (TitleError, SyntaxError, IndexError, ValueError): 88 99 msg = 'File could not be opened' 89 raise IOError, msg 100 raise IOError, msg 90 101 return dict 91 102 92 103 93 def export_mesh_file(ofile,mesh_dict): 104 ## 105 # @brief Write a mesh file from a dictionary. 106 # @param ofile The path of the mesh file to write. 107 # @param mesh_dict Dictionary containing data to write to output mesh file. 108 # @note Raises IOError if file extension is unrecognized. 109 def export_mesh_file(ofile, mesh_dict): 94 110 """ 95 111 write a file, ofile, with the format 96 97 First line: <# of vertices> <# of attributes>112 113 First line: <# of vertices> <# of attributes> 98 114 Following lines: <vertex #> <x> <y> [attributes] 99 One line: <# of triangles> 100 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] 101 One line: <# of segments> 115 One line: <# of triangles> 116 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> 117 <neigbouring triangle #> <neigbouring triangle #> 118 <neigbouring triangle #> [attribute of region] 119 One line: <# of segments> 102 120 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 103 121 """ 104 #FIXME DSG-anyone: automate 105 if not mesh_dict.has_key('points'): 106 mesh_dict['points'] = [] 107 if not mesh_dict.has_key('point_attributes'): 108 mesh_dict['point_attributes'] = [] 109 if not mesh_dict.has_key('outline_segments'): 110 mesh_dict['outline_segments'] = [] 111 if not mesh_dict.has_key('outline_segment_tags'): 112 mesh_dict['outline_segment_tags'] = [] 113 if not mesh_dict.has_key('holes'): 114 mesh_dict['holes'] = [] 115 if not mesh_dict.has_key('regions'): 116 mesh_dict['regions'] = [] 117 118 if not mesh_dict.has_key('region_tags'): 119 mesh_dict['region_tags'] = [] 120 if not mesh_dict.has_key('region_max_areas'): 121 mesh_dict['region_max_areas'] = [] 122 if not mesh_dict.has_key('vertices'): 123 mesh_dict['vertices'] = [] 124 if not mesh_dict.has_key('vertex_attributes'): 125 mesh_dict['vertex_attributes'] = [] 126 if not mesh_dict.has_key('vertex_attribute_titles'): 127 mesh_dict['vertex_attribute_titles'] = [] 128 if not mesh_dict.has_key('segments'): 129 mesh_dict['segments'] = [] 130 if not mesh_dict.has_key('segment_tags'): 131 mesh_dict['segment_tags'] = [] 132 if not mesh_dict.has_key('triangles'): 133 mesh_dict['triangles'] = [] 134 if not mesh_dict.has_key('triangle_tags'): 135 mesh_dict['triangle_tags'] = [] 136 if not mesh_dict.has_key('triangle_neighbors'): 137 mesh_dict['triangle_neighbors'] = [] 138 #print "DSG************" 139 #print "mesh_dict",mesh_dict 140 #print "DSG************" 141 122 123 # Ensure that if required key isn't present, we add it with [] value. 124 # .setdefault() for dictionaries was added in python 2.0. 125 # The only other neat way to do this is to override the dictionary 126 # .__getitem__() method to return [] if key not found. 127 reqd_keys = ['points', 'point_attributes', 'outline_segments', 128 'outline_segment_tags', 'holes', 'regions', 'region_tags', 129 'region_max_areas', 'vertices', 'vertex_attributes', 130 'vertex_attribute_titles', 'segments', 'segment_tags', 131 'triangles', 'triangle_tags', 'triangle_neighbors'] 132 133 for k in reqd_keys: 134 mesh_dict.setdefault(k, []) 135 136 # hand-off to appropriate function 142 137 if (ofile[-4:] == ".tsh"): 143 138 _write_tsh_file(ofile,mesh_dict) … … 146 141 else: 147 142 msg = 'Unknown file type %s ' %ofile 148 raise IOError, msg 149 143 raise IOError, msg 144 145 146 ## 147 # @brief Read .tsh file into a dictionary. 148 # @param ofile Path of the file to read. 149 # @return Data dictionary from the .tsh file. 150 150 def _read_tsh_file(ofile): 151 151 """ 152 152 Read the text file format for meshes 153 153 """ 154 fd = open(ofile,'r') 154 155 fd = open(ofile, 'r') 155 156 dict = _read_triangulation(fd) 156 157 dict_mesh = _read_outline(fd) … … 158 159 dict[element] = dict_mesh[element] 159 160 fd.close() 161 160 162 return dict 161 163 162 164 165 ## 166 # @brief ?? 167 # @param fd An open descriptor of the file to read. 168 # @return A dictionary ... 163 169 def _read_triangulation(fd): 164 170 """ 165 Read the generated triangulation, NOT the outline. 166 """ 171 Read the generated triangulation, NOT the outline. 172 """ 173 167 174 delimiter = " " 168 ######### loading the point info 175 176 # loading the point info 169 177 line = fd.readline() 170 #print line171 178 fragments = line.split() 172 #for fragment in fragments:173 #print fragment174 179 if fragments ==[]: 175 180 NumOfVertices = 0 … … 180 185 points = [] 181 186 pointattributes = [] 182 for index in range(int(NumOfVertices)): 183 #print index 187 for index in range(int(NumOfVertices)): 184 188 fragments = fd.readline().split() 185 #print fragments186 189 fragments.pop(0) #pop off the index 187 188 # pop the x y off so we're left with a list of attributes 189 vert = [float(fragments.pop(0)), float(fragments.pop(0))]190 191 # pop the x y off so we're left with a list of attributes 192 vert = [float(fragments.pop(0)), float(fragments.pop(0))] 190 193 points.append(vert) 191 194 apointattributes = [] 192 #print fragments193 195 for fragment in fragments: 194 196 apointattributes.append(float(fragment)) 195 197 if apointattributes != []: 196 198 pointattributes.append(apointattributes) 197 198 # ########loading the point title info199 200 # loading the point title info 199 201 line = fd.readline() 200 #print "point title comments",line201 202 vertTitle = [] 202 for index in range(int(NumOfVertAttributes)): 203 #print index 204 fragments = fd.readline().strip() 203 for index in range(int(NumOfVertAttributes)): 204 fragments = fd.readline().strip() 205 205 vertTitle.append(fragments) 206 #print vertTitle 207 208 ######### loading the triangle info 206 207 # loading the triangle info 209 208 line = fd.readline() 210 #print "triangle comments",line211 209 fragments = line.split() 212 #for fragment in fragments:213 # print fragment214 210 NumOfTriangles = fragments[0] 215 211 triangles = [] 216 212 triangleattributes = [] 217 213 triangleneighbors = [] 218 for index in range(int(NumOfTriangles)): 219 #print index 214 for index in range(int(NumOfTriangles)): 220 215 line = fd.readline() 221 216 line.strip() # so we can get the region string 222 217 fragments = line.split() 223 #print "triangle info", fragments224 218 fragments.pop(0) #pop off the index 225 226 tri = [int(fragments[0]), int(fragments[1]),int(fragments[2])]219 220 tri = [int(fragments[0]), int(fragments[1]), int(fragments[2])] 227 221 triangles.append(tri) 228 neighbors = [int(fragments[3]), int(fragments[4]),int(fragments[5])]222 neighbors = [int(fragments[3]), int(fragments[4]), int(fragments[5])] 229 223 triangleneighbors.append(neighbors) 230 224 for x in range(7): # remove index [<vertex #>] [<neigbouring tri #>] 231 line = line[find(line, delimiter):] # remove index225 line = line[find(line, delimiter):] # remove index 232 226 line = line.lstrip() 233 227 stringtag = line.strip() 234 228 triangleattributes.append(stringtag) 235 236 # ########loading the segment info229 230 # loading the segment info 237 231 line = fd.readline() 238 #print "seg comment line",line239 232 fragments = line.split() 240 #for fragment in fragments:241 # print fragment242 233 NumOfSegments = fragments[0] 243 234 segments = [] 244 235 segmenttags = [] 245 for index in range(int(NumOfSegments)): 246 #print index 236 for index in range(int(NumOfSegments)): 247 237 line = fd.readline() 248 238 line.strip() # to get the segment string 249 239 fragments = line.split() 250 #print fragments251 240 fragments.pop(0) #pop off the index 252 seg = [int(fragments[0]), int(fragments[1])]241 seg = [int(fragments[0]), int(fragments[1])] 253 242 segments.append(seg) 254 line = line[find(line, delimiter):] # remove index243 line = line[find(line, delimiter):] # remove index 255 244 line = line.lstrip() 256 line = line[find(line, delimiter):] # remove x245 line = line[find(line, delimiter):] # remove x 257 246 line = line.lstrip() 258 line = line[find(line, delimiter):] # remove y247 line = line[find(line, delimiter):] # remove y 259 248 stringtag = line.strip() 260 249 segmenttags.append(stringtag) 261 250 262 263 251 meshDict = {} 264 252 meshDict['vertices'] = points … … 269 257 meshDict['triangles'] = triangles 270 258 meshDict['triangle_tags'] = triangleattributes 271 meshDict['triangle_neighbors'] = triangleneighbors 272 meshDict['segments'] = segments 259 meshDict['triangle_neighbors'] = triangleneighbors 260 meshDict['segments'] = segments 273 261 meshDict['segment_tags'] = segmenttags 274 262 meshDict['vertex_attribute_titles'] = vertTitle 275 263 276 264 return meshDict 277 265 266 267 ## 268 # @brief 269 # @param fd An open descriptor of the file to read. 270 # @return A dictionary ... 271 # @note If file has no mesh info, an empty dict will be returned. 278 272 def _read_outline(fd): 279 273 """ … … 281 275 returned will be 'empty'. 282 276 """ 277 283 278 delimiter = " " # warning: split() calls are using default whitespace 284 285 # ########loading the point info279 280 # loading the point info 286 281 line = fd.readline() 287 #print 'read_outline - line',line288 282 fragments = line.split() 289 #for fragment in fragments:290 283 if fragments ==[]: 291 284 NumOfVertices = 0 … … 296 289 points = [] 297 290 pointattributes = [] 298 for index in range(int(NumOfVertices)): 299 #print index 300 fragments = fd.readline().split() 301 #print fragments 291 for index in range(int(NumOfVertices)): 292 fragments = fd.readline().split() 302 293 fragments.pop(0) #pop off the index 303 294 # pop the x y off so we're left with a list of attributes 304 vert = [float(fragments.pop(0)), float(fragments.pop(0))]295 vert = [float(fragments.pop(0)), float(fragments.pop(0))] 305 296 points.append(vert) 306 apointattributes = [] 307 #print fragments 297 apointattributes = [] 308 298 for fragment in fragments: 309 299 apointattributes.append(float(fragment)) 310 300 pointattributes.append(apointattributes) 311 312 313 ######### loading the segment info 301 302 # loading the segment info 314 303 line = fd.readline() 315 304 fragments = line.split() 316 #for fragment in fragments:317 # print fragment318 305 if fragments ==[]: 319 306 NumOfSegments = 0 … … 322 309 segments = [] 323 310 segmenttags = [] 324 for index in range(int(NumOfSegments)): 325 #print index 311 for index in range(int(NumOfSegments)): 326 312 line = fd.readline() 327 313 fragments = line.split() 328 #print fragments329 314 fragments.pop(0) #pop off the index 330 331 seg = [int(fragments[0]),int(fragments[1])] 315 seg = [int(fragments[0]), int(fragments[1])] 332 316 segments.append(seg) 333 line = line[find(line, delimiter):] # remove index317 line = line[find(line, delimiter):] # remove index 334 318 line = line.lstrip() 335 line = line[find(line, delimiter):] # remove x319 line = line[find(line, delimiter):] # remove x 336 320 line = line.lstrip() 337 line = line[find(line, delimiter):] # remove y321 line = line[find(line, delimiter):] # remove y 338 322 stringtag = line.strip() 339 segmenttags.append(stringtag) 340 341 # ########loading the hole info323 segmenttags.append(stringtag) 324 325 # loading the hole info 342 326 line = fd.readline() 343 #print line344 327 fragments = line.split() 345 #for fragment in fragments: 346 # print fragment 347 if fragments ==[]: 328 if fragments == []: 348 329 numOfHoles = 0 349 330 else: 350 331 numOfHoles = fragments[0] 351 332 holes = [] 352 for index in range(int(numOfHoles)): 353 #print index 333 for index in range(int(numOfHoles)): 354 334 fragments = fd.readline().split() 355 #print fragments356 335 fragments.pop(0) #pop off the index 357 hole = [float(fragments[0]), float(fragments[1])]336 hole = [float(fragments[0]), float(fragments[1])] 358 337 holes.append(hole) 359 338 360 361 ######### loading the region info 339 # loading the region info 362 340 line = fd.readline() 363 #print line364 341 fragments = line.split() 365 #for fragment in fragments: 366 # print fragment 367 if fragments ==[]: 342 if fragments == []: 368 343 numOfRegions = 0 369 344 else: … … 374 349 line = fd.readline() 375 350 fragments = line.split() 376 #print fragments377 351 fragments.pop(0) #pop off the index 378 region = [float(fragments[0]), float(fragments[1])]352 region = [float(fragments[0]), float(fragments[1])] 379 353 regions.append(region) 380 354 381 line = line[find(line, delimiter):] # remove index355 line = line[find(line, delimiter):] # remove index 382 356 line = line.lstrip() 383 line = line[find(line, delimiter):] # remove x357 line = line[find(line, delimiter):] # remove x 384 358 line = line.lstrip() 385 line = line[find(line, delimiter):] # remove y359 line = line[find(line, delimiter):] # remove y 386 360 stringtag = line.strip() 387 361 regionattributes.append(stringtag) 362 388 363 regionmaxareas = [] 389 364 line = fd.readline() … … 391 366 line = fd.readline() 392 367 fragments = line.split() 393 #print fragments394 368 # The try is here for format compatibility 395 369 try: … … 405 379 geo_reference = Geo_reference(ASCIIFile=fd) 406 380 except: 407 #geo_ref not compulsory 381 #geo_ref not compulsory 408 382 geo_reference = None 409 383 410 384 meshDict = {} 411 385 meshDict['points'] = points 412 386 meshDict['point_attributes'] = pointattributes 413 meshDict['outline_segments'] = segments 387 meshDict['outline_segments'] = segments 414 388 meshDict['outline_segment_tags'] = segmenttags 415 389 meshDict['holes'] = holes … … 418 392 meshDict['region_max_areas'] = regionmaxareas 419 393 meshDict['geo_reference'] = geo_reference 420 421 #print "meshDict",meshDict 394 422 395 return meshDict 423 396 424 def clean_line(line,delimiter): 397 398 ## 399 # @brief Clean up a line with delimited fields. 400 # @param line The string to be cleaned. 401 # @param delimiter String used a delimiter when splitting 'line'. 402 # @return A list of delimited fields with white-space removed from ends. 403 # @note Will also remove any field that was originally ''. 404 def clean_line(line, delimiter): 425 405 """Remove whitespace 426 406 """ 427 #print ">%s" %line 407 428 408 line = line.strip() 429 #print "stripped>%s" %line430 409 numbers = line.split(delimiter) 431 410 i = len(numbers) - 1 … … 435 414 else: 436 415 numbers[i] = numbers[i].strip() 437 416 438 417 i += -1 439 #for num in numbers: 440 # print "num>%s<" %num 418 441 419 return numbers 442 420 443 def _write_ASCII_triangulation(fd, 444 gen_dict): 421 422 ## 423 # @brief Write an ASCII triangulation file. 424 # @param fd An open descriptor to the file to write. 425 # @param gen_dict Dictionary containing data to write. 426 def _write_ASCII_triangulation(fd, gen_dict): 445 427 vertices = gen_dict['vertices'] 446 428 vertices_attributes = gen_dict['vertex_attributes'] 447 429 try: 448 vertices_attribute_titles = gen_dict['vertex_attribute_titles'] 430 vertices_attribute_titles = gen_dict['vertex_attribute_titles'] 449 431 except KeyError, e: 450 #FIXME is this the best way? 432 #FIXME is this the best way? 451 433 if vertices_attributes == [] or vertices_attributes[0] == []: 452 434 vertices_attribute_titles = [] 453 435 else: 454 raise KeyError, e 455 436 raise KeyError, e 437 456 438 triangles = gen_dict['triangles'] 457 439 triangles_attributes = gen_dict['triangle_tags'] 458 440 triangle_neighbors = gen_dict['triangle_neighbors'] 459 441 460 442 segments = gen_dict['segments'] 461 443 segment_tags = gen_dict['segment_tags'] 462 444 463 445 numVert = str(len(vertices)) 464 #print "load vertices_attributes", vertices_attributes465 446 # Don't understand why we have to do vertices_attributes[0] is None, 466 447 # but it doesn't work otherwise... 467 if vertices_attributes == None or\468 (numVert == "0") or\469 448 if vertices_attributes == None \ 449 or (numVert == "0") \ 450 or len(vertices_attributes) == 0: 470 451 numVertAttrib = "0" 471 452 else: 472 453 numVertAttrib = str(len(vertices_attributes[0])) 473 fd.write(numVert + " " + numVertAttrib + " # <# of verts> <# of vert attributes>, next lines <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." + "\n") 474 475 #<vertex #> <x> <y> [attributes] 454 455 fd.write(numVert + " " + numVertAttrib + 456 " # <# of verts> <# of vert attributes>, next lines <vertex #> " 457 "<x> <y> [attributes] ...Triangulation Vertices...\n") 458 459 #<vertex #> <x> <y> [attributes] 476 460 index = 0 477 461 for vert in vertices: 478 462 attlist = "" 479 480 if vertices_attributes == None or \ 481 vertices_attributes == []: 463 464 if vertices_attributes == None or vertices_attributes == []: 482 465 attlist = "" 483 466 else: 484 467 for att in vertices_attributes[index]: 485 attlist = attlist + str(att) +" "468 attlist = attlist + str(att) + " " 486 469 attlist.strip() 487 fd.write(str(index) + " " 488 + str(vert[0]) + " " 489 + str(vert[1]) + " " 490 + attlist + "\n") 470 fd.write(str(index) + " " + str(vert[0]) + " " + str(vert[1]) 471 + " " + attlist + "\n") 491 472 index += 1 492 473 493 # write comments for title 494 fd.write("# attribute column titles ...Triangulation Vertex Titles... " + "\n")474 # write comments for title 475 fd.write("# attribute column titles ...Triangulation Vertex Titles...\n") 495 476 for title in vertices_attribute_titles: 496 477 fd.write(title + "\n") 497 478 498 479 #<# of triangles> 499 480 n = len(triangles) 500 fd.write(str(n) + " # <# of triangles>, next lines <triangle #> [<vertex #>] [<neigbouring triangle #>] [attribute of region] ...Triangulation Triangles..." + "\n") 501 502 # <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] 481 fd.write(str(n) 482 + " # <# of triangles>, next lines <triangle #> [<vertex #>] " 483 "[<neigbouring triangle #>] [attribute of region] ..." 484 "Triangulation Triangles...\n") 485 486 # <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> 487 # <neigbouring triangle #> <neigbouring triangle #> [attribute of region] 503 488 for index in range(n): 504 489 neighbors = "" … … 521 506 # with triangle, and it seems to have the option of returning 522 507 # more than one value for triangle attributex 523 if triangles_attributes == None or\524 triangles_attributes == [] or\525 508 if triangles_attributes == None \ 509 or triangles_attributes == [] \ 510 or triangles_attributes[index] == ['']: 526 511 att = "" 527 512 else: 528 513 att = str(triangles_attributes[index]) 529 514 fd.write(str(index) + " " 530 + str(tri[0]) + " " 531 + str(tri[1]) + " " 532 + str(tri[2]) + " " 515 + str(tri[0]) + " " 516 + str(tri[1]) + " " 517 + str(tri[2]) + " " 533 518 + neighbors + " " 534 519 + att + "\n") 535 536 #One line: <# of segments> 537 fd.write(str(len(segments)) + 538 " # <# of segments>, next lines <segment #> <vertex #> <vertex #> [boundary tag] ...Triangulation Segments..." + "\n") 539 520 521 #One line: <# of segments> 522 fd.write(str(len(segments)) + 523 " # <# of segments>, next lines <segment #> <vertex #> " 524 "<vertex #> [boundary tag] ...Triangulation Segments...\n") 525 540 526 #Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 541 527 for i in range(len(segments)): 542 528 seg = segments[i] 543 529 fd.write(str(i) + " " 544 + str(seg[0]) + " " 545 + str(seg[1]) + " " 530 + str(seg[0]) + " " 531 + str(seg[1]) + " " 546 532 + str(segment_tags[i]) + "\n") 547 533 548 534 549 def _write_tsh_file(ofile,mesh_dict): 550 fd = open(ofile,'w') 551 _write_ASCII_triangulation(fd,mesh_dict) 552 _write_ASCII_outline(fd,mesh_dict) 553 fd.close() 554 555 def _write_ASCII_outline(fd, 556 dict): 535 ## 536 # @brief Write triangulation and outline to a .tsh file. 537 # @param ofile Path of the file to write. 538 # @mesh_dict Dictionary of data to write. 539 def _write_tsh_file(ofile, mesh_dict): 540 fd = open(ofile, 'w') 541 _write_ASCII_triangulation(fd, mesh_dict) 542 _write_ASCII_outline(fd, mesh_dict) 543 fd.close() 544 545 546 ## 547 # @brief Write an ASCII outline to a file. 548 # @param fd An open descriptor of the file to write. 549 # @param dict Dictionary holding data to write. 550 def _write_ASCII_outline(fd, dict): 557 551 points = dict['points'] 558 552 point_attributes = dict['point_attributes'] … … 563 557 region_tags = dict['region_tags'] 564 558 region_max_areas = dict['region_max_areas'] 565 559 566 560 num_points = str(len(points)) 567 561 if (num_points == "0"): … … 569 563 else: 570 564 num_point_atts = str(len(point_attributes[0])) 571 fd.write(num_points + " " + num_point_atts + " # <# of verts> <# of vert attributes>, next lines <vertex #> <x> <y> [attributes] ...Mesh Vertices..." + "\n") 572 565 566 fd.write(num_points + " " + num_point_atts + 567 " # <# of verts> <# of vert attributes>, next lines <vertex #> " 568 "<x> <y> [attributes] ...Mesh Vertices...\n") 569 573 570 # <x> <y> [attributes] 574 571 for i,point in enumerate(points): 575 572 attlist = "" 576 573 for att in point_attributes[i]: 577 attlist = attlist + str(att) +" "574 attlist = attlist + str(att) + " " 578 575 attlist.strip() 579 576 fd.write(str(i) + " " 580 + str(point[0]) + " "577 + str(point[0]) + " " 581 578 + str(point[1]) + " " 582 579 + attlist + "\n") 583 584 #One line: <# of segments> 585 fd.write(str(len(segments)) + 586 " # <# of segments>, next lines <segment #> <vertex #> <vertex #> [boundary tag] ...Mesh Segments..." + "\n") 587 580 581 #One line: <# of segments> 582 fd.write(str(len(segments)) + 583 " # <# of segments>, next lines <segment #> <vertex #> " 584 "<vertex #> [boundary tag] ...Mesh Segments...\n") 585 588 586 #Following lines: <vertex #> <vertex #> [boundary tag] 589 587 for i,seg in enumerate(segments): 590 588 fd.write(str(i) + " " 591 + str(seg[0]) + " " 592 + str(seg[1]) + " " 589 + str(seg[0]) + " " 590 + str(seg[1]) + " " 593 591 + str(segment_tags[i]) + "\n") 594 592 595 #One line: <# of holes> 596 fd.write(str(len(holes)) + 597 " # <# of holes>, next lines <Hole #> <x> <y> ...Mesh Holes... " + "\n")593 #One line: <# of holes> 594 fd.write(str(len(holes)) + 595 " # <# of holes>, next lines <Hole #> <x> <y> ...Mesh Holes...\n") 598 596 # <x> <y> 599 597 for i,h in enumerate(holes): … … 601 599 + str(h[0]) + " " 602 600 + str(h[1]) + "\n") 603 604 #One line: <# of regions> 605 fd.write(str(len(regions)) + 606 " # <# of regions>, next lines <Region #> <x> <y> <tag>...Mesh Regions..." + "\n") 601 602 #One line: <# of regions> 603 fd.write(str(len(regions)) + 604 " # <# of regions>, next lines <Region #> <x> <y> <tag>" 605 "...Mesh Regions...\n") 606 607 607 # <index> <x> <y> <tag> 608 #print "regions",regions609 608 for i,r in enumerate(regions): 610 #print "r",r 611 fd.write(str(i) + " " 609 fd.write(str(i) + " " 612 610 + str(r[0]) + " " 613 611 + str(r[1])+ " " 614 612 + str(region_tags[i]) + "\n") 615 613 616 614 # <index> [<MaxArea>|""] 617 618 #One line: <# of regions> 619 fd.write(str(len(regions)) + 620 " # <# of regions>, next lines <Region #> [Max Area] ...Mesh Regions..." + "\n") 615 616 #One line: <# of regions> 617 fd.write(str(len(regions)) + 618 " # <# of regions>, next lines <Region #> [Max Area] " 619 "...Mesh Regions...\n") 621 620 for i,r in enumerate(regions): 622 621 area = str(region_max_areas[i]) 623 622 624 623 fd.write(str(i) + " " + area + "\n") 625 624 … … 628 627 dict['geo_reference'].write_ASCII(fd) 629 628 629 630 ## 631 # @brief Write a .msh file. 632 # @param file Path to the file to write. 633 # @param mesh Dictionary holding data to write. 630 634 def _write_msh_file(file_name, mesh): 631 """Write .msh NetCDF file 635 """Write .msh NetCDF file 632 636 633 637 WARNING: This function mangles the mesh data structure 634 638 """ 635 639 636 640 # FIXME(Ole and John): We ran into a problem on Bogong (64 bit) 637 641 # where integers appeared as arrays. This may be similar to … … 643 647 # created with the type Int64. Need to look at the NetCDF library 644 648 # in more detail. 645 649 646 650 IntType = Int32 647 651 #IntType = Int 648 649 # 652 650 653 #the triangulation 651 654 mesh['vertices'] = array(mesh['vertices']).astype(Float) 652 655 if mesh['vertex_attributes'] != None: 653 656 mesh['vertex_attributes'] = \ 654 array(mesh['vertex_attributes']).astype(Float) 655 mesh['vertex_attribute_titles'] = array(mesh['vertex_attribute_titles']).astype(Character) 657 array(mesh['vertex_attributes']).astype(Float) 658 mesh['vertex_attribute_titles'] = \ 659 array(mesh['vertex_attribute_titles']).astype(Character) 656 660 mesh['segments'] = array(mesh['segments']).astype(IntType) 657 661 mesh['segment_tags'] = array(mesh['segment_tags']).astype(Character) 658 662 mesh['triangles'] = array(mesh['triangles']).astype(IntType) 659 663 mesh['triangle_tags'] = array(mesh['triangle_tags']) #.astype(Character) 660 mesh['triangle_neighbors'] = array(mesh['triangle_neighbors']).astype(IntType) 661 662 #the outline 664 mesh['triangle_neighbors'] = \ 665 array(mesh['triangle_neighbors']).astype(IntType) 666 667 #the outline 663 668 mesh['points'] = array(mesh['points']).astype(Float) 664 669 mesh['point_attributes'] = array(mesh['point_attributes']).astype(Float) 665 670 mesh['outline_segments'] = array(mesh['outline_segments']).astype(IntType) 666 mesh['outline_segment_tags'] = array(mesh['outline_segment_tags']).astype(Character) 671 mesh['outline_segment_tags'] = \ 672 array(mesh['outline_segment_tags']).astype(Character) 667 673 mesh['holes'] = array(mesh['holes']).astype(Float) 668 674 mesh['regions'] = array(mesh['regions']).astype(Float) 669 675 mesh['region_tags'] = array(mesh['region_tags']).astype(Character) 670 676 mesh['region_max_areas'] = array(mesh['region_max_areas']).astype(Float) 671 677 672 678 #mesh = mesh_dict2array(mesh) 673 679 #print "new_mesh",new_mesh 674 #print "mesh",mesh 675 680 #print "mesh",mesh 681 676 682 # NetCDF file definition 677 683 try: 678 684 outfile = NetCDFFile(file_name, 'w') 679 685 except IOError: 680 msg = 'File %s could not be created' % file_name686 msg = 'File %s could not be created' % file_name 681 687 raise msg 682 688 683 689 #Create new file 684 690 outfile.institution = 'Geoscience Australia' 685 691 outfile.description = 'NetCDF format for compact and portable storage ' +\ 686 'of spatial point data'687 692 'of spatial point data' 693 688 694 # dimension definitions - fixed 689 695 outfile.createDimension('num_of_dimensions', 2) #This is 2d data … … 694 700 695 701 # Create dimensions, variables and set the variables 696 702 697 703 # trianglulation 698 704 # vertices … … 702 708 'num_of_dimensions')) 703 709 outfile.variables['vertices'][:] = mesh['vertices'] 704 if mesh['vertex_attributes'] != None and \ 705 (mesh['vertex_attributes'].shape[0] > 0 and mesh['vertex_attributes'].shape[1] > 0): 710 if mesh['vertex_attributes'] != None \ 711 and (mesh['vertex_attributes'].shape[0] > 0 \ 712 and mesh['vertex_attributes'].shape[1] > 0): 706 713 outfile.createDimension('num_of_vertex_attributes', 707 714 mesh['vertex_attributes'].shape[1]) … … 711 718 Float, 712 719 ('num_of_vertices', 713 'num_of_vertex_attributes')) 720 'num_of_vertex_attributes')) 714 721 outfile.createVariable('vertex_attribute_titles', 715 722 Character, 716 ( 717 'num_of_vertex_attribute_title_chars'))723 ('num_of_vertex_attributes', 724 'num_of_vertex_attribute_title_chars')) 718 725 outfile.variables['vertex_attributes'][:] = \ 719 726 mesh['vertex_attributes'] 720 727 outfile.variables['vertex_attribute_titles'][:] = \ 721 728 mesh['vertex_attribute_titles'] 729 722 730 # segments 723 if (mesh['segments'].shape[0] > 0): 724 outfile.createDimension('num_of_segments', 725 mesh['segments'].shape[0]) 726 outfile.createVariable('segments', 727 IntType, 731 if (mesh['segments'].shape[0] > 0): 732 outfile.createDimension('num_of_segments', mesh['segments'].shape[0]) 733 outfile.createVariable('segments', IntType, 728 734 ('num_of_segments', 'num_of_segment_ends')) 729 735 outfile.variables['segments'][:] = mesh['segments'] … … 736 742 'num_of_segment_tag_chars')) 737 743 outfile.variables['segment_tags'][:] = mesh['segment_tags'] 738 # triangles 744 745 # triangles 739 746 if (mesh['triangles'].shape[0] > 0): 740 outfile.createDimension('num_of_triangles', 741 mesh['triangles'].shape[0]) 747 outfile.createDimension('num_of_triangles', mesh['triangles'].shape[0]) 742 748 outfile.createVariable('triangles', 743 749 IntType, 744 ('num_of_triangles', 745 'num_of_triangle_vertices')) 750 ('num_of_triangles', 'num_of_triangle_vertices')) 746 751 outfile.createVariable('triangle_neighbors', 747 752 IntType, 748 ('num_of_triangles', 749 'num_of_triangle_faces')) 753 ('num_of_triangles', 'num_of_triangle_faces')) 750 754 outfile.variables['triangles'][:] = mesh['triangles'] 751 755 outfile.variables['triangle_neighbors'][:] = mesh['triangle_neighbors'] 752 if mesh['triangle_tags'] != None and\753 756 if mesh['triangle_tags'] != None \ 757 and (mesh['triangle_tags'].shape[1] > 0): 754 758 outfile.createDimension('num_of_triangle_tag_chars', 755 mesh['triangle_tags'].shape[1]) 759 mesh['triangle_tags'].shape[1]) 756 760 outfile.createVariable('triangle_tags', 757 761 Character, … … 759 763 'num_of_triangle_tag_chars')) 760 764 outfile.variables['triangle_tags'][:] = mesh['triangle_tags'] 761 762 765 763 766 # outline … … 767 770 outfile.createVariable('points', Float, ('num_of_points', 768 771 'num_of_dimensions')) 769 outfile.variables['points'][:] = mesh['points'] 770 if (mesh['point_attributes'].shape[0] > 0 and mesh['point_attributes'].shape[1] > 0): 772 outfile.variables['points'][:] = mesh['points'] 773 if mesh['point_attributes'].shape[0] > 0 \ 774 and mesh['point_attributes'].shape[1] > 0: 771 775 outfile.createDimension('num_of_point_attributes', 772 776 mesh['point_attributes'].shape[1]) 773 777 outfile.createVariable('point_attributes', 774 778 Float, 775 ('num_of_points', 776 'num_of_point_attributes')) 779 ('num_of_points', 'num_of_point_attributes')) 777 780 outfile.variables['point_attributes'][:] = mesh['point_attributes'] 778 # outline_segments 779 if (mesh['outline_segments'].shape[0] > 0): 781 782 # outline_segments 783 if mesh['outline_segments'].shape[0] > 0: 780 784 outfile.createDimension('num_of_outline_segments', 781 785 mesh['outline_segments'].shape[0]) … … 792 796 ('num_of_outline_segments', 793 797 'num_of_outline_segment_tag_chars')) 794 outfile.variables['outline_segment_tags'][:] = mesh['outline_segment_tags'] 798 outfile.variables['outline_segment_tags'][:] = \ 799 mesh['outline_segment_tags'] 800 795 801 # holes 796 802 if (mesh['holes'].shape[0] > 0): … … 799 805 'num_of_dimensions')) 800 806 outfile.variables['holes'][:] = mesh['holes'] 807 801 808 # regions 802 809 if (mesh['regions'].shape[0] > 0): … … 804 811 outfile.createVariable('regions', Float, ('num_of_regions', 805 812 'num_of_dimensions')) 806 outfile.createVariable('region_max_areas', 807 Float, 808 ('num_of_regions',)) 813 outfile.createVariable('region_max_areas', Float, ('num_of_regions',)) 809 814 outfile.variables['regions'][:] = mesh['regions'] 810 815 outfile.variables['region_max_areas'][:] = mesh['region_max_areas'] … … 812 817 outfile.createDimension('num_of_region_tag_chars', 813 818 mesh['region_tags'].shape[1]) 814 outfile.createVariable('region_tags', 815 Character, 819 outfile.createVariable('region_tags', Character, 816 820 ('num_of_regions', 817 821 'num_of_region_tag_chars')) … … 821 825 if mesh.has_key('geo_reference') and not mesh['geo_reference'] == None: 822 826 mesh['geo_reference'].write_NetCDF(outfile) 823 827 824 828 outfile.close() 825 829 826 830 827 831 ## 832 # @brief Read a mesh file. 833 # @param file_name Path to the file to read. 834 # @return A dictionary containing the .msh file data. 828 835 def _read_msh_file(file_name): 829 """ 830 Read in an msh file. 831 832 """ 833 834 836 """ Read in an msh file. 837 """ 838 835 839 #Check contents 836 840 #Get NetCDF 837 838 841 # see if the file is there. Throw a QUIET IO error if it isn't 839 fd = open(file_name, 'r')842 fd = open(file_name, 'r') 840 843 fd.close() 841 844 842 845 #throws prints to screen if file not present 843 fid = NetCDFFile(file_name, 'r') 846 fid = NetCDFFile(file_name, 'r') 844 847 mesh = {} 848 845 849 # Get the variables 846 850 # the triangulation … … 849 853 except KeyError: 850 854 mesh['vertices'] = array([]) 855 851 856 try: 852 857 mesh['vertex_attributes'] = fid.variables['vertex_attributes'][:] … … 855 860 #for ob in mesh['vertices']: 856 861 #mesh['vertex_attributes'].append([]) 857 862 858 863 mesh['vertex_attribute_titles'] = [] 859 864 try: … … 862 867 mesh['vertex_attribute_titles'].append(titles[i].tostring().strip()) 863 868 except KeyError: 864 pass 869 pass 870 865 871 try: 866 872 mesh['segments'] = fid.variables['segments'][:] 867 873 except KeyError: 868 874 mesh['segments'] = array([]) 875 869 876 mesh['segment_tags'] =[] 870 877 try: … … 875 882 for ob in mesh['segments']: 876 883 mesh['segment_tags'].append('') 884 877 885 try: 878 886 mesh['triangles'] = fid.variables['triangles'][:] … … 881 889 mesh['triangles'] = array([]) 882 890 mesh['triangle_neighbors'] = array([]) 891 883 892 mesh['triangle_tags'] =[] 884 893 try: … … 889 898 for ob in mesh['triangles']: 890 899 mesh['triangle_tags'].append('') 891 892 #the outline 900 901 #the outline 893 902 try: 894 903 mesh['points'] = fid.variables['points'][:] 895 904 except KeyError: 896 905 mesh['points'] = [] 906 897 907 try: 898 908 mesh['point_attributes'] = fid.variables['point_attributes'][:] … … 901 911 for point in mesh['points']: 902 912 mesh['point_attributes'].append([]) 913 903 914 try: 904 915 mesh['outline_segments'] = fid.variables['outline_segments'][:] 905 916 except KeyError: 906 917 mesh['outline_segments'] = array([]) 918 907 919 mesh['outline_segment_tags'] =[] 908 920 try: … … 913 925 for ob in mesh['outline_segments']: 914 926 mesh['outline_segment_tags'].append('') 927 915 928 try: 916 929 mesh['holes'] = fid.variables['holes'][:] 917 930 except KeyError: 918 931 mesh['holes'] = array([]) 932 919 933 try: 920 934 mesh['regions'] = fid.variables['regions'][:] 921 935 except KeyError: 922 936 mesh['regions'] = array([]) 937 923 938 mesh['region_tags'] =[] 924 939 try: … … 929 944 for ob in mesh['regions']: 930 945 mesh['region_tags'].append('') 946 931 947 try: 932 948 mesh['region_max_areas'] = fid.variables['region_max_areas'][:] … … 934 950 mesh['region_max_areas'] = array([]) 935 951 #mesh[''] = fid.variables[''][:] 936 952 937 953 try: 938 954 geo_reference = Geo_reference(NetCDFObject=fid) 939 955 mesh['geo_reference'] = geo_reference 940 956 except AttributeError, e: 941 #geo_ref not compulsory 957 #geo_ref not compulsory 942 958 mesh['geo_reference'] = None 943 959 944 960 fid.close() 945 961 946 962 return mesh 947 948 949 ## used by alpha shapes 950 951 def export_boundary_file( file_name, points, title, delimiter = ','): 963 964 965 ## 966 # @brief Export a boundary file. 967 # @param file_name Path to the file to write. 968 # @param points List of point index pairs. 969 # @paran title Title string to write into file (first line). 970 # @param delimiter Delimiter string to write between points. 971 # @note Used by alpha shapes 972 def export_boundary_file(file_name, points, title, delimiter=','): 952 973 """ 953 974 export a file, ofile, with the format 954 975 955 976 First line: Title variable 956 Following lines: [point index][delimiter][point index] 977 Following lines: [point index][delimiter][point index] 957 978 958 979 file_name - the name of the new file 959 points - List of point index pairs [[p1, p2],[p3, p4]..] 980 points - List of point index pairs [[p1, p2],[p3, p4]..] 960 981 title - info to write in the first line 961 982 """ 962 963 fd = open(file_name,'w') 964 965 fd.write(title+"\n") 966 #[point index][delimiter][point index] 983 984 fd = open(file_name, 'w') 985 986 fd.write(title + "\n") 987 988 #[point index][delimiter][point index] 967 989 for point in points: 968 fd.write( str(point[0]) + delimiter969 + str(point[1]) + "\n") 990 fd.write(str(point[0]) + delimiter + str(point[1]) + "\n") 991 970 992 fd.close() 971 993 … … 975 997 ### 976 998 999 ## 1000 # @brief 1001 # @param point_atts 977 1002 def extent_point_atts(point_atts): 978 1003 """ … … 982 1007 point_atts['pointlist'] = extent(point_atts['pointlist']) 983 1008 point_atts['attributelist'] = {} 1009 984 1010 return point_atts 985 1011 1012 1013 ## 1014 # @brief Get an extent array for a set of points. 1015 # @param points A set of points. 1016 # @return An extent array of form [[min_x, min_y], [max_x, min_y], 1017 # [max_x, max_y], [min_x, max_y]] 986 1018 def extent(points): 987 1019 points = array(points).astype(Float) 1020 988 1021 max_x = min_x = points[0][0] 989 1022 max_y = min_y = points[0][1] 1023 990 1024 for point in points[1:]: 991 x = point[0] 1025 x = point[0] 992 1026 if x > max_x: max_x = x 993 if x < min_x: min_x = x 994 y = point[1] 1027 if x < min_x: min_x = x 1028 1029 y = point[1] 995 1030 if y > max_y: max_y = y 996 1031 if y < min_y: min_y = y 997 extent = array([[min_x,min_y],[max_x,min_y],[max_x,max_y],[min_x,max_y]]) 998 #print "extent",extent 1032 1033 extent = array([[min_x, min_y], 1034 [max_x, min_y], 1035 [max_x, max_y], 1036 [min_x, max_y]]) 1037 999 1038 return extent 1000 1039 1040 1041 ## 1042 # @brief Reduce a points file until number of points is less than limit. 1043 # @param infile Path to input file to thin. 1044 # @param outfile Path to output thinned file. 1045 # @param max_points Max number of points in output file. 1046 # @param verbose True if this function is to be verbose. 1001 1047 def reduce_pts(infile, outfile, max_points, verbose = False): 1002 1048 """ 1003 reduces a points file by remov ong every second point until the # of points1049 reduces a points file by removing every second point until the # of points 1004 1050 is less than max_points. 1005 1051 """ 1052 1006 1053 # check out pts2rectangular in least squares, and the use of reduction. 1007 1054 # Maybe it does the same sort of thing? 1008 1055 point_atts = _read_pts_file(infile) 1056 1009 1057 while point_atts['pointlist'].shape[0] > max_points: 1010 1058 if verbose: print "point_atts['pointlist'].shape[0]" 1011 1059 point_atts = half_pts(point_atts) 1060 1012 1061 export_points_file(outfile, point_atts) 1013 1014 def produce_half_point_files(infile, max_points, delimiter, verbose = False): 1062 1063 1064 ## 1065 # @brief 1066 # @param infile 1067 # @param max_points 1068 # @param delimiter 1069 # @param verbose True if this function is to be verbose. 1070 # @return A list of 1071 def produce_half_point_files(infile, max_points, delimiter, verbose=False): 1015 1072 point_atts = _read_pts_file(infile) 1016 1073 root, ext = splitext(infile) 1017 1074 outfiles = [] 1075 1018 1076 if verbose: print "# of points", point_atts['pointlist'].shape[0] 1077 1019 1078 while point_atts['pointlist'].shape[0] > max_points: 1020 1079 point_atts = half_pts(point_atts) 1080 1021 1081 if verbose: print "# of points", point_atts['pointlist'].shape[0] 1082 1022 1083 outfile = root + delimiter + str(point_atts['pointlist'].shape[0]) + ext 1023 1084 outfiles.append(outfile) 1024 export_points_file(outfile, 1025 point_atts) 1085 export_points_file(outfile, point_atts) 1086 1026 1087 return outfiles 1027 1088 1089 1090 ## 1091 # @brief 1092 # @param point_atts Object with attribute of 'points list'. 1093 # @return 1028 1094 def point_atts2array(point_atts): 1095 # convert attribute list to array of floats 1029 1096 point_atts['pointlist'] = array(point_atts['pointlist']).astype(Float) 1030 1097 1031 1098 for key in point_atts['attributelist'].keys(): 1032 point_atts['attributelist'][key]= array(point_atts['attributelist'][key]).astype(Float) 1099 point_atts['attributelist'][key] = \ 1100 array(point_atts['attributelist'][key]).astype(Float) 1101 1033 1102 return point_atts 1034 1103 1104 1105 ## 1106 # @brief ?? 1107 # @param point_atts ?? 1108 # @return ?? 1035 1109 def half_pts(point_atts): 1036 1110 point_atts2array(point_atts) 1037 1111 point_atts['pointlist'] = point_atts['pointlist'][::2] 1038 1112 1039 1113 for key in point_atts['attributelist'].keys(): 1040 point_atts['attributelist'][key]= point_atts['attributelist'][key] [::2] 1114 point_atts['attributelist'][key] = point_atts['attributelist'][key][::2] 1115 1041 1116 return point_atts 1042 1117 1118 ## 1119 # @brief ?? 1120 # @param dic ?? 1121 # @return ?? 1043 1122 def concatinate_attributelist(dic): 1044 1123 """ 1045 1124 giving a dic[attribute title] = attribute 1046 return list of attribute titles, array of attributes 1047 """ 1125 return list of attribute titles, array of attributes 1126 """ 1127 1048 1128 point_attributes = array([]).astype(Float) 1049 1129 keys = dic.keys() … … 1054 1134 reshaped = reshape(dic[key],(dic[key].shape[0],1)) 1055 1135 point_attributes = concatenate([point_attributes, reshaped], axis=1) 1136 1056 1137 return dic.keys(), point_attributes 1057 1138 1139 1140 ## 1141 # @brief 1142 # @param dict 1143 # @param indices_to_keep 1144 # @return 1058 1145 #FIXME(dsg), turn this dict plus methods into a class? 1059 def take_points(dict, indices_to_keep):1146 def take_points(dict, indices_to_keep): 1060 1147 dict = point_atts2array(dict) 1061 1148 #FIXME maybe the points data structure should become a class? … … 1065 1152 dict['attributelist'][key]= take(dict['attributelist'][key], 1066 1153 indices_to_keep) 1154 1067 1155 return dict 1068 1156 1157 ## 1158 # @brief ?? 1159 # @param dict1 ?? 1160 # @param dict2 ?? 1161 # @return ?? 1069 1162 def add_point_dictionaries (dict1, dict2): 1070 1163 """ 1071 1164 """ 1165 1072 1166 dict1 = point_atts2array(dict1) 1073 1167 dict2 = point_atts2array(dict2) 1074 1075 combined = {} 1168 1169 combined = {} 1076 1170 combined['pointlist'] = concatenate((dict2['pointlist'], 1077 dict1['pointlist']),axis=0) 1078 atts = {} 1171 dict1['pointlist']),axis=0) 1172 1173 atts = {} 1079 1174 for key in dict2['attributelist'].keys(): 1080 1175 atts[key]= concatenate((dict2['attributelist'][key], … … 1082 1177 combined['attributelist']=atts 1083 1178 combined['geo_reference'] = dict1['geo_reference'] 1179 1084 1180 return combined 1085 1181 1182 1086 1183 if __name__ == "__main__": 1087 1184 pass -
anuga_core/source/anuga/load_mesh/test_loadASCII.py
r5875 r6074 543 543 'bad msh file did not raise error!') 544 544 os.remove(fileName) 545 546 ###### 547 # Test the clean_line() utility function. 548 ###### 549 550 def test_clean_line_01(self): 551 test_line = 'abc, ,,xyz,123' 552 result = clean_line(test_line, ',') 553 self.failUnless(result == ['abc', '', 'xyz', '123']) 554 555 def test_clean_line_02(self): 556 test_line = ' abc , ,, xyz , 123 ' 557 result = clean_line(test_line, ',') 558 self.failUnless(result == ['abc', '', 'xyz', '123']) 559 560 def test_clean_line_03(self): 561 test_line = '1||||2' 562 result = clean_line(test_line, '|') 563 self.failUnless(result == ['1', '2']) 545 564 546 565 #-------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.