Changeset 1375
- Timestamp:
- May 12, 2005, 9:50:04 AM (20 years ago)
- Location:
- inundation/ga/storm_surge/pmesh/load_mesh
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pmesh/load_mesh/loadASCII.py
r1219 r1375 69 69 #FIXME (DSG-DSG) this is used by least_squares - 70 70 # is this the best function for it to use? 71 # (DSG-DSG) put an exception handler in import_triangulation/import_mesh_file 72 # Think about exceptions before doing this! 71 73 def mesh_file_to_mesh_dictionary(fileName): 72 74 """Load a pmesh file. Returning the mesh dictionary. 73 75 """ 74 76 try: 75 meshdic = import_ triangulation(fileName)77 meshdic = import_mesh_file(fileName) 76 78 except IOError, e: 77 79 msg = 'Could not open file %s ' %fileName … … 79 81 return meshdic 80 82 81 def import_triangulation(ofile): 83 # This is used by pmesh 84 def import_mesh_file(ofile): 82 85 """ 83 86 reading triangulation and meshoutline info. … … 94 97 95 98 if ofile[-4:]== ".tsh": 96 fd = open(ofile,'r') 97 dict = read_triangulation(fd) 98 dict_mesh = read_outline(fd) 99 for element in dict_mesh.keys(): 100 dict[element] = dict_mesh[element] 101 fd.close() 99 dict = _read_tsh_file(ofile) 102 100 elif ofile[-4:]== ".msh": 103 dict = read_msh_file(ofile)101 dict = _read_msh_file(ofile) 104 102 else: 105 103 msg = 'Extension %s is unknown' %ofile[-4:] … … 107 105 return dict 108 106 109 110 def read_triangulation(fd): 107 def _read_tsh_file(ofile): 108 fd = open(ofile,'r') 109 dict = _read_triangulation(fd) 110 dict_mesh = _read_outline(fd) 111 for element in dict_mesh.keys(): 112 dict[element] = dict_mesh[element] 113 fd.close() 114 return dict 115 116 117 def _read_triangulation(fd): 111 118 """ 112 119 Read the generated triangulation, NOT the outline. … … 219 226 return meshDict 220 227 221 def import_mesh(ofile): 222 """ 223 import a file, ofile, with the format 224 225 First line: <# of vertices> <# of attributes> 226 Following lines: <x> <y> [attributes] 227 One line: <# of segments> 228 Following lines: <vertex #> <vertex #> [boundary tag] 229 Note: This might throw a can't load file error 230 """ 231 fd = open(ofile,'r') 232 meshDict = read_outline(fd) 233 fd.close() 234 return meshDict 235 236 def read_outline(fd): 228 def _read_outline(fd): 237 229 """ 238 230 Note, if a file has no mesh info, it can still be read - the meshdic … … 404 396 return numbers 405 397 406 #FIXME change this name 407 def write_ASCII_triangulation(fd, 398 def _write_ASCII_triangulation(fd, 408 399 gen_dict): 409 400 vertices = gen_dict['vertices'] … … 507 498 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 508 499 """ 509 #FIXME (DSG-DSG) Do this to the triangulation keys as well510 500 if not mesh_dict.has_key('points'): 511 501 mesh_dict['points'] = [] … … 525 515 if not mesh_dict.has_key('region_max_areas'): 526 516 mesh_dict['region_max_areas'] = [] 527 528 529 517 if not mesh_dict.has_key('vertices'): 530 518 mesh_dict['vertices'] = [] … … 549 537 try: 550 538 if (ofile[-4:] == ".tsh"): 551 fd = open(ofile,'w') 552 write_ASCII_triangulation(fd,mesh_dict) 553 write_ASCII_outline(fd,mesh_dict) 554 fd.close() 539 _write_tsh_file(ofile,mesh_dict) 555 540 elif (ofile[-4:] == ".msh"): 556 #print "loadascii.mesh mesh_dict",mesh_dict 557 write_msh_file(ofile, mesh_dict) 541 _write_msh_file(ofile, mesh_dict) 558 542 except IOError, e: 559 543 msg = 'Could not write file %s ' %fileName 560 544 raise IOError, msg 561 545 562 563 def write_ASCII_outline(fd, 546 def _write_tsh_file(ofile,mesh_dict): 547 fd = open(ofile,'w') 548 _write_ASCII_triangulation(fd,mesh_dict) 549 _write_ASCII_outline(fd,mesh_dict) 550 fd.close() 551 552 def _write_ASCII_outline(fd, 564 553 dict): 565 554 points = dict['points'] … … 639 628 dict['geo_reference'].write_ASCII(fd) 640 629 641 def write_msh_file(filename, mesh):630 def _write_msh_file(filename, mesh): 642 631 """Write .msh NetCDF file 643 632 … … 820 809 821 810 822 def read_msh_file(file_name):811 def _read_msh_file(file_name): 823 812 """ 824 813 Read in an msh file. -
inundation/ga/storm_surge/pmesh/load_mesh/test_loadASCII.py
r1183 r1375 1 1 #!/usr/bin/env python 2 2 # 3 4 #FIXME (Ole): I think we need a unit test of write_ASCII_triangulation5 # in order to quash bug in merimbula data6 # - to quash the bug need a raised error in pyvolution for reading xya files7 # when the title attribute information is not consistent with the data in the8 # file - DSG9 # also test_export_mesh_file tests write_ASCII_triangulation - DSG10 3 11 4 import tempfile … … 21 14 from loadASCII import * 22 15 from coordinate_transforms.geo_reference import Geo_reference 16 import loadASCII 23 17 24 18 class loadASCIITestCase(unittest.TestCase): … … 170 164 171 165 172 def test_import_mesh(self):166 def FUNCTION_REMOVEDtest_import_mesh(self): 173 167 174 168 dict = self.dict 175 169 fileName = tempfile.mktemp(".txt") 176 170 fd = open(fileName,'w') 177 write_ASCII_outline(fd,dict)171 loadASCII._write_ASCII_outline(fd,dict) 178 172 fd.close() 179 173 loaded_dict = import_mesh(fileName) #FIXME function names are wacky … … 221 215 fileName = tempfile.mktemp(".tsh") 222 216 export_mesh_file(fileName, meshDict) 223 loadedDict = import_ triangulation(fileName)217 loadedDict = import_mesh_file(fileName) 224 218 225 219 #print "*(*( meshDict" … … 262 256 def test_read_write_msh_file(self): 263 257 dict = self.dict.copy() 264 fileName = tempfile.mktemp(". txt")265 write_msh_file(fileName,dict)266 loaded_dict = read_msh_file(fileName)258 fileName = tempfile.mktemp(".msh") 259 export_mesh_file(fileName,dict) 260 loaded_dict = loadASCII._read_msh_file(fileName) 267 261 os.remove(fileName) 268 262 dict = self.dict … … 276 270 def test_read_write_msh_fileII(self): 277 271 dict = self.sparse_dict.copy() 278 fileName = tempfile.mktemp(". txt")279 write_msh_file(fileName,dict)280 loaded_dict = read_msh_file(fileName)272 fileName = tempfile.mktemp(".msh") 273 export_mesh_file(fileName,dict) 274 loaded_dict = loadASCII._read_msh_file(fileName) 281 275 os.remove(fileName) 282 276 dict = self.sparse_dict … … 290 284 def test_read_write_msh_fileIII(self): 291 285 dict = self.blank_dict.copy() 292 fileName = tempfile.mktemp(". txt")293 write_msh_file(fileName,dict)294 loaded_dict = read_msh_file(fileName)286 fileName = tempfile.mktemp(".msh") 287 export_mesh_file(fileName,dict) 288 loaded_dict = loadASCII._read_msh_file(fileName) 295 289 os.remove(fileName) 296 290 dict = self.blank_dict … … 304 298 def test_read_write_msh_file4(self): 305 299 dict = self.seg_dict.copy() 306 fileName = tempfile.mktemp(". txt")307 write_msh_file(fileName,dict)308 loaded_dict = read_msh_file(fileName)300 fileName = tempfile.mktemp(".msh") 301 export_mesh_file(fileName,dict) 302 loaded_dict = loadASCII._read_msh_file(fileName) 309 303 os.remove(fileName) 310 304 dict = self.seg_dict … … 318 312 def test_read_write_msh_file5(self): 319 313 dict = self.triangle_tags_dict.copy() 320 fileName = tempfile.mktemp(". txt")321 write_msh_file(fileName,dict)322 loaded_dict = read_msh_file(fileName)314 fileName = tempfile.mktemp(".msh") 315 export_mesh_file(fileName,dict) 316 loaded_dict = loadASCII._read_msh_file(fileName) 323 317 os.remove(fileName) 324 318 dict = self.seg_dict … … 333 327 def test_read_write_msh_file5(self): 334 328 dict = self.tri_dict.copy() 335 fileName = tempfile.mktemp(". txt")336 write_msh_file(fileName,dict)337 loaded_dict = read_msh_file(fileName)329 fileName = tempfile.mktemp(".msh") 330 export_mesh_file(fileName,dict) 331 loaded_dict = loadASCII._read_msh_file(fileName) 338 332 os.remove(fileName) 339 333 dict = self.tri_dict
Note: See TracChangeset
for help on using the changeset viewer.