Changeset 1416
- Timestamp:
- May 18, 2005, 9:55:42 AM (20 years ago)
- Location:
- inundation/ga/storm_surge/pmesh
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pmesh/load_mesh/loadASCII.py
r1401 r1416 13 13 Use the same delimiter for the attribute names and the data 14 14 15 An xya file can optionally end with 16 #geo reference 17 56 18 466600.0 19 8644444.0 20 21 When the 1st # is the zone, 22 2nd # the xllcorner and 23 3rd # the yllcorner 15 24 The format for a Points dictionary is: 16 25 … … 69 78 from os.path import splitext 70 79 71 from coordinate_transforms.geo_reference import Geo_reference 80 from coordinate_transforms.geo_reference import Geo_reference,TITLE 72 81 73 82 import exceptions … … 80 89 # (DSG-DSG) put an exception handler in import_triangulation/import_mesh_file 81 90 # Think about exceptions before doing this! 82 def mesh_file_to_mesh_dictionary(file Name):91 def mesh_file_to_mesh_dictionary(file_name): 83 92 """Load a pmesh file. Returning the mesh dictionary. 84 93 """ 85 94 try: 86 meshdic = import_mesh_file(file Name)95 meshdic = import_mesh_file(file_name) 87 96 except IOError, e: 88 msg = 'Could not open file %s ' %file Name97 msg = 'Could not open file %s ' %file_name 89 98 raise IOError, msg 90 99 return meshdic … … 95 104 read a mesh file, either .tsh or .msh 96 105 97 Note: This might throw a can't load file error 106 Note: will throw an IOError if it can't load the file. 107 Catch these! 98 108 """ 99 109 try: … … 126 136 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 127 137 """ 128 #FIXME DSG- DSG: automate138 #FIXME DSG-anyone: automate 129 139 if not mesh_dict.has_key('points'): 130 140 mesh_dict['points'] = [] … … 164 174 #print "DSG************" 165 175 166 try: 167 if (ofile[-4:] == ".tsh"): 168 _write_tsh_file(ofile,mesh_dict) 169 elif (ofile[-4:] == ".msh"): 170 _write_msh_file(ofile, mesh_dict) 171 except IOError, e: 172 msg = 'Could not write file %s ' %fileName 176 if (ofile[-4:] == ".tsh"): 177 _write_tsh_file(ofile,mesh_dict) 178 elif (ofile[-4:] == ".msh"): 179 _write_msh_file(ofile, mesh_dict) 180 else: 181 msg = 'Unknown file type %s ' %ofile 173 182 raise IOError, msg 174 175 183 176 184 def _read_tsh_file(ofile): … … 554 562 555 563 556 def export_mesh_file(ofile,mesh_dict):557 """558 write a file, ofile, with the format559 560 First line: <# of vertices> <# of attributes>561 Following lines: <vertex #> <x> <y> [attributes]562 One line: <# of triangles>563 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]564 One line: <# of segments>565 Following lines: <segment #> <vertex #> <vertex #> [boundary tag]566 """567 if not mesh_dict.has_key('points'):568 mesh_dict['points'] = []569 if not mesh_dict.has_key('point_attributes'):570 mesh_dict['point_attributes'] = []571 if not mesh_dict.has_key('outline_segments'):572 mesh_dict['outline_segments'] = []573 if not mesh_dict.has_key('outline_segment_tags'):574 mesh_dict['outline_segment_tags'] = []575 if not mesh_dict.has_key('holes'):576 mesh_dict['holes'] = []577 if not mesh_dict.has_key('regions'):578 mesh_dict['regions'] = []579 580 if not mesh_dict.has_key('region_tags'):581 mesh_dict['region_tags'] = []582 if not mesh_dict.has_key('region_max_areas'):583 mesh_dict['region_max_areas'] = []584 if not mesh_dict.has_key('vertices'):585 mesh_dict['vertices'] = []586 if not mesh_dict.has_key('vertex_attributes'):587 mesh_dict['vertex_attributes'] = []588 if not mesh_dict.has_key('vertex_attribute_titles'):589 mesh_dict['vertex_attribute_titles'] = []590 if not mesh_dict.has_key('segments'):591 mesh_dict['segments'] = []592 if not mesh_dict.has_key('segment_tags'):593 mesh_dict['segment_tags'] = []594 if not mesh_dict.has_key('triangles'):595 mesh_dict['triangles'] = []596 if not mesh_dict.has_key('triangle_tags'):597 mesh_dict['triangle_tags'] = []598 if not mesh_dict.has_key('triangle_neighbors'):599 mesh_dict['triangle_neighbors'] = []600 #print "DSG************"601 #print "mesh_dict",mesh_dict602 #print "DSG************"603 604 try:605 if (ofile[-4:] == ".tsh"):606 _write_tsh_file(ofile,mesh_dict)607 elif (ofile[-4:] == ".msh"):608 _write_msh_file(ofile, mesh_dict)609 except IOError, e:610 msg = 'Could not write file %s ' %fileName611 raise IOError, msg612 613 564 def _write_tsh_file(ofile,mesh_dict): 614 565 fd = open(ofile,'w') … … 684 635 " # <# of regions>, next lines <Region #> [Max Area] ...Mesh Regions..." + "\n") 685 636 for i,r in enumerate(regions): 686 if region_max_areas[i] <= 0 : 687 area = "" 688 else: 689 area = str(region_max_areas[i]) 637 area = str(region_max_areas[i]) 690 638 691 639 fd.write(str(i) + " " + area + "\n") … … 695 643 dict['geo_reference'].write_ASCII(fd) 696 644 697 def _write_msh_file(file name, mesh):645 def _write_msh_file(file_name, mesh): 698 646 """Write .msh NetCDF file 699 647 … … 731 679 732 680 # NetCDF file definition 733 outfile = NetCDFFile(file name, 'w')681 outfile = NetCDFFile(file_name, 'w') 734 682 735 683 #Create new file … … 886 834 #Check contents 887 835 #Get NetCDF 836 837 # see if the file is there. Throw a QUIET IO error if it isn't 838 fd = open(file_name,'r') 839 fd.close() 840 841 #throws prints to screen if file not present 888 842 fid = NetCDFFile(file_name, 'r') 889 843 … … 1027 981 #if not mesh_dict.has_key('points'): 1028 982 # mesh_dict['points'] = [] 1029 try: 1030 if (ofile[-4:] == ".xya"): 1031 _write_xya_file(ofile, point_dict) 1032 elif (ofile[-4:] == ".pts"): 1033 _write_pts_file(ofile, point_dict) 1034 except IOError, e: 1035 msg = 'Could not write file %s ' %fileName 1036 raise IOError, msg 983 if (ofile[-4:] == ".xya"): 984 _write_xya_file(ofile, point_dict) 985 elif (ofile[-4:] == ".pts"): 986 _write_pts_file(ofile, point_dict) 987 else: 988 msg = 'Unknown file type %s ' %ofile 989 raise IOError, msg 1037 990 1038 991 def import_points_file(ofile, delimiter = None, verbose = False): 1039 992 """ 1040 load an .xya or .pts file 993 load an .xya or .pts file 994 995 Note: will throw an IOError if it can't load the file. 996 Catch these! 1041 997 """ 1042 998 … … 1055 1011 points_dict = _read_xya_file(fd, delimiter) 1056 1012 fd.close() 1057 points_dict['geo_reference'] = None1058 except IOError, e:1059 msg = 'Could not open file %s ' %ofile1060 raise IOError, msg1061 1013 except SyntaxError: 1014 fd.close() 1062 1015 msg = 'File could not be opened' 1063 1016 raise IOError, msg 1064 1017 except IndexError: 1018 fd.close() 1019 msg = 'File could not be opened' 1020 raise IOError, msg 1021 except ValueError: # thrown by geo_ref, read ASCII 1022 fd.close() 1065 1023 msg = 'File could not be opened' 1066 1024 raise IOError, msg … … 1069 1027 try: 1070 1028 points_dict = _read_pts_file(ofile, verbose) 1071 except IOError, e: 1029 except IOError, e: 1072 1030 msg = 'Could not open file %s ' %ofile 1073 1031 raise IOError, msg … … 1152 1110 return dic.keys(), point_attributes 1153 1111 1154 def _read_pts_file(file name, verbose = False):1112 def _read_pts_file(file_name, verbose = False): 1155 1113 """Read .pts NetCDF file 1156 1114 … … 1167 1125 from Scientific.IO.NetCDF import NetCDFFile 1168 1126 1169 if verbose: print 'Reading ', filename 1170 fid = NetCDFFile(filename, 'r') 1127 if verbose: print 'Reading ', file_name 1128 1129 1130 # see if the file is there. Throw a QUIET IO error if it isn't 1131 fd = open(file_name,'r') 1132 fd.close() 1133 1134 #throws prints to screen if file not present 1135 fid = NetCDFFile(file_name, 'r') 1171 1136 1172 1137 point_atts = {} … … 1203 1168 return point_atts 1204 1169 1205 def _write_pts_file(file name, point_atts):1170 def _write_pts_file(file_name, point_atts): 1206 1171 """Write .pts NetCDF file 1207 1172 … … 1215 1180 point_atts2array(point_atts) 1216 1181 # NetCDF file definition 1217 outfile = NetCDFFile(file name, 'w')1182 outfile = NetCDFFile(file_name, 'w') 1218 1183 1219 1184 #Create new file … … 1243 1208 1244 1209 def _read_xya_file(fd, delimiter): 1245 lines = fd.readlines()1210 #lines = fd.readlines() 1246 1211 points = [] 1247 1212 pointattributes = [] 1248 if len(lines) <= 1: 1249 raise SyntaxError 1250 title = lines.pop(0) # the first (title) line 1213 #if len(lines) <= 1: 1214 # raise SyntaxError 1215 title = fd.readline() 1216 #title = lines.pop(0) # the first (title) line 1251 1217 att_names = clean_line(title,delimiter) 1252 1218 1253 1219 att_dict = {} 1254 for line in lines: 1220 line = fd.readline() 1221 numbers = clean_line(line,delimiter) 1222 #for line in lines: 1223 while len(numbers) > 1: 1255 1224 #print "line >%s" %line 1256 numbers = clean_line(line,delimiter)1225 #numbers = clean_line(line,delimiter) 1257 1226 #print "numbers >%s<" %numbers 1258 if len(numbers) < 2 and numbers != []: 1227 #if len(numbers) < 2 and numbers != []: 1228 1259 1229 # A line without two numbers 1260 1230 # or a bad delimiter 1261 1231 #FIXME dsg-dsg change error that is raised. 1262 raise SyntaxError1232 #raise SyntaxError 1263 1233 if numbers != []: 1264 1234 try: … … 1281 1251 #attributes.append(float(num)) 1282 1252 att_dict.setdefault(att_names[i],[]).append(float(num)) 1283 1253 1284 1254 except ValueError: 1285 1255 raise SyntaxError 1286 #pointattributes.append(attributes) 1256 line = fd.readline() 1257 numbers = clean_line(line,delimiter) 1258 1259 if line == '': 1260 # end of file 1261 geo_reference = None 1262 else: 1263 geo_reference = Geo_reference(ASCIIFile=fd,read_title=line) 1264 1287 1265 xya_dict = {} 1288 1266 xya_dict['pointlist'] = array(points).astype(Float) … … 1291 1269 att_dict[key] = array(att_dict[key]).astype(Float) 1292 1270 xya_dict['attributelist'] = att_dict 1293 1294 1295 ##print "xya_dict",xya_dict 1271 xya_dict['geo_reference'] = geo_reference 1272 #print "xya_dict",xya_dict 1296 1273 return xya_dict 1297 1274 … … 1323 1300 combined['geo_reference'] = dict1['geo_reference'] 1324 1301 return combined 1325 1326 #FIXME (DSG) need an export_points_file method.. 1302 1327 1303 def _write_xya_file( file_name, xya_dict, delimiter = ','): 1328 1304 """ 1329 export a file, ofile, with the format 1330 1331 First line: Title variable 1332 Following lines: <vertex #> <x> <y> [attributes] 1333 1334 file_name - the name of the new file 1335 xya_dict - point and point attribute info in a dictionary 1336 title - info to write in the first line 1337 """ 1338 #FIXME, move the test for this from meshharness to loadasciiharness 1305 export a file, ofile, with the xya format 1306 1307 """ 1339 1308 points = xya_dict['pointlist'] 1340 1309 pointattributes = xya_dict['attributelist'] … … 1358 1327 + str(vert[1]) 1359 1328 + attlist + "\n") 1329 1330 # geo_reference info 1331 if xya_dict.has_key('geo_reference') and \ 1332 not xya_dict['geo_reference'] is None: 1333 xya_dict['geo_reference'].write_ASCII(fd) 1360 1334 fd.close() 1361 1335 -
inundation/ga/storm_surge/pmesh/load_mesh/test_loadASCII.py
r1394 r1416 163 163 pass 164 164 165 165 ############### .TSH ########## 166 166 def test_export_mesh_file(self): 167 167 import os … … 210 210 os.remove(fileName) 211 211 212 def test_read_write_tsh_file(self): 213 dict = self.dict.copy() 214 fileName = tempfile.mktemp(".tsh") 215 export_mesh_file(fileName,dict) 216 loaded_dict = import_mesh_file(fileName) 217 os.remove(fileName) 218 dict = self.dict 219 #print "*********************" 220 #print dict 221 #print "**loaded_dict*******************" 222 #print loaded_dict 223 #print "*********************" 224 self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file') 225 226 def test_read_write_tsh_fileII(self): 227 dict = self.sparse_dict.copy() 228 fileName = tempfile.mktemp(".tsh") 229 export_mesh_file(fileName,dict) 230 loaded_dict = import_mesh_file(fileName) 231 dict = self.sparse_dict 232 self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file') 233 os.remove(fileName) 234 235 def test_read_write_tsh_fileIII(self): 236 dict = self.blank_dict.copy() 237 fileName = tempfile.mktemp(".tsh") 238 export_mesh_file(fileName,dict) 239 loaded_dict = import_mesh_file(fileName) 240 os.remove(fileName) 241 dict = self.blank_dict 242 #print "*********************" 243 #print dict 244 #print "**loaded_dict*******************" 245 #print loaded_dict 246 #print "*********************" 247 self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_fileIII') 248 249 def test_read_write_tsh_file4(self): 250 dict = self.seg_dict.copy() 251 fileName = tempfile.mktemp(".tsh") 252 export_mesh_file(fileName,dict) 253 loaded_dict = import_mesh_file(fileName) 254 os.remove(fileName) 255 dict = self.seg_dict 256 self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file4') 257 258 def test_read_write_tsh_file5(self): 259 dict = self.triangle_tags_dict.copy() 260 fileName = tempfile.mktemp(".tsh") 261 export_mesh_file(fileName,dict) 262 loaded_dict = import_mesh_file(fileName) 263 dict = self.triangle_tags_dict 264 #print "*********************" 265 #print dict 266 #print "**loaded_dict*******************" 267 #print loaded_dict 268 #print "*********************" 269 self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file5') 270 os.remove(fileName) 271 272 def test_read_write_tsh_file6(self): 273 dict = self.tri_dict.copy() 274 fileName = tempfile.mktemp(".tsh") 275 export_mesh_file(fileName,dict) 276 loaded_dict = import_mesh_file(fileName) 277 dict = self.tri_dict 278 self.check_mesh_dicts(loaded_dict,dict, 'test_read_write_msh_file6') 279 os.remove(fileName) 280 281 ########################## BAD .TSH ########################## 282 283 def test_load_bad_no_file_tsh(self): 284 import os 285 import tempfile 286 287 fileName = tempfile.mktemp(".tsh") 288 #print fileName 289 try: 290 dict = import_mesh_file(fileName) 291 except IOError: 292 pass 293 else: 294 self.failUnless(0 ==1, 295 'imaginary file did not raise error!') 296 297 def test_read_write_tsh_file_bad(self): 298 dict = self.tri_dict.copy() 299 fileName = tempfile.mktemp(".xxx") 300 try: 301 export_mesh_file(fileName,dict) 302 except IOError: 303 pass 304 else: 305 self.failUnless(0 ==1, 306 'bad tsh file did not raise error!') 307 308 def test_import_tsh_bad(self): 309 import os 310 import tempfile 311 312 fileName = tempfile.mktemp(".tsh") 313 file = open(fileName,"w") 314 # this is a bad tsh file 315 file.write("elevn\n\ 316 1.0 what \n\ 317 0.0 the \n\ 318 1.0 !!! \n") 319 file.close() 320 #print fileName 321 try: 322 dict = import_points_file(fileName,delimiter = ' ') 323 except IOError: 324 pass 325 else: 326 self.failUnless(0 ==1, 327 'bad tsh file did not raise error!') 328 os.remove(fileName) 329 330 def test_import_tsh3(self): 331 import os 332 import tempfile 333 334 fileName = tempfile.mktemp(".tsh") 335 file = open(fileName,"w") 336 file.write("1.0 \n\ 337 showme1.0 0.0 10.0 \n\ 338 0.0 1.0\n\ 339 13.0 \n") 340 file.close() 341 #print fileName 342 try: 343 dict = import_points_file(fileName,delimiter = ' ') 344 except IOError: 345 pass 346 else: 347 self.failUnless(0 ==1, 348 'bad tsh file did not raise error!') 349 350 os.remove(fileName) 351 352 353 ############### .MSH ########## 354 212 355 def test_read_write_msh_file(self): 213 356 dict = self.dict.copy() … … 272 415 loaded_dict = loadASCII._read_msh_file(fileName) 273 416 os.remove(fileName) 274 dict = self. seg_dict275 #print " *********************"417 dict = self.triangle_tags_dict 418 #print "msh_file5*********************" 276 419 #print dict 277 420 #print "**loaded_dict*******************" … … 281 424 282 425 283 def test_read_write_msh_file 5(self):426 def test_read_write_msh_file6(self): 284 427 dict = self.tri_dict.copy() 285 428 fileName = tempfile.mktemp(".msh") … … 359 502 loaded_dict['geo_reference'] == None) , 360 503 fail_string + ' failed!! Test geo_reference') 504 505 ########################## BAD .MSH ########################## 506 507 def test_load_bad_no_file_msh(self): 508 import os 509 import tempfile 510 511 fileName = tempfile.mktemp(".msh") 512 #print fileName 513 try: 514 dict = import_mesh_file(fileName) 515 except IOError: 516 pass 517 else: 518 self.failUnless(0 ==1, 519 'imaginary file did not raise error!') 520 521 def test_import_msh_bad(self): 522 import os 523 import tempfile 524 525 fileName = tempfile.mktemp(".msh") 526 file = open(fileName,"w") 527 # this is a bad tsh file 528 file.write("elevn\n\ 529 1.0 what \n\ 530 0.0 the \n\ 531 1.0 !!! \n") 532 file.close() 533 #print fileName 534 try: 535 dict = import_points_file(fileName,delimiter = ' ') 536 except IOError: 537 pass 538 else: 539 self.failUnless(0 ==1, 540 'bad msh file did not raise error!') 541 os.remove(fileName) 542 543 ###################### .XYA ############################## 544 545 def test_export_xya_file(self): 546 dict = {} 547 att_dict = {} 548 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 549 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 550 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 551 dict['attributelist'] = att_dict 552 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 553 554 555 fileName = tempfile.mktemp(".xya") 556 export_points_file(fileName, dict) 557 dict2 = import_points_file(fileName) 558 #print "fileName",fileName 559 os.remove(fileName) 560 #print "dict2",dict2 561 562 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 563 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 564 answer = [10.0, 0.0, 10.4] 565 assert allclose(dict2['attributelist']['brightness'], answer) 566 #print "dict2['geo_reference']",dict2['geo_reference'] 567 self.failUnless(dict['geo_reference'] == dict2['geo_reference'], 568 'test_writepts failed. Test geo_reference') 569 570 def test_export_xya_file2(self): 571 dict = {} 572 att_dict = {} 573 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 574 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 575 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 576 dict['attributelist'] = att_dict 577 578 579 fileName = tempfile.mktemp(".xya") 580 export_points_file(fileName, dict) 581 dict2 = import_points_file(fileName) 582 #print "fileName",fileName 583 os.remove(fileName) 584 #print "dict2",dict2 585 586 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 587 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 588 answer = [10.0, 0.0, 10.4] 589 assert allclose(dict2['attributelist']['brightness'], answer) 590 591 592 def test_loadxya(self): 593 """ 594 comma delimited 595 """ 596 597 fileName = tempfile.mktemp(".xya") 598 file = open(fileName,"w") 599 file.write("elevation , speed \n\ 600 1.0, 0.0, 10.0, 0.0\n\ 601 0.0, 1.0, 0.0, 10.0\n\ 602 1.0, 0.0, 10.4, 40.0\n") 603 file.close() 604 #print fileName 605 dict = import_points_file(fileName,delimiter = ',') 606 os.remove(fileName) 607 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 608 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 609 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 610 611 #FIXME - redundant test? 612 def test_loadxy(self): 613 """ 614 To test the mesh side of loading xya files. 615 Not the loading of xya files 616 """ 617 import os 618 import tempfile 619 620 fileName = tempfile.mktemp(".xya") 621 file = open(fileName,"w") 622 file.write("elevation speed \n\ 623 1.0 0.0 10.0 0.0\n\ 624 0.0 1.0 0.0 10.0\n\ 625 1.0 0.0 10.4 40.0\n") 626 file.close() 627 #print fileName 628 dict = import_points_file(fileName) 629 os.remove(fileName) 630 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 631 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 632 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 633 634 635 def test_loadxya2(self): 636 """ 637 space delimited 638 """ 639 import os 640 import tempfile 641 642 fileName = tempfile.mktemp(".xya") 643 file = open(fileName,"w") 644 file.write(" elevation speed \n\ 645 1.0 0.0 10.0 0.0\n\ 646 0.0 1.0 0.0 10.0\n\ 647 1.0 0.0 10.4 40.0\n") 648 file.close() 649 #print fileName 650 dict = import_points_file(fileName,delimiter = ' ') 651 os.remove(fileName) 652 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 653 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 654 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 655 656 def test_loadxya3(self): 657 """ 658 space delimited 659 """ 660 import os 661 import tempfile 662 663 fileName = tempfile.mktemp(".xya") 664 file = open(fileName,"w") 665 file.write(" elevation speed \n\ 666 1.0 0.0 10.0 0.0\n\ 667 0.0 1.0 0.0 10.0\n\ 668 1.0 0.0 10.4 40.0\n\ 669 #geocrap\n\ 670 56\n\ 671 56.6\n\ 672 3\n") 673 file.close() 674 #print fileName 675 dict = import_points_file(fileName,delimiter = ' ') 676 os.remove(fileName) 677 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 678 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 679 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 680 681 geo_reference = Geo_reference(56, 56.6, 3.0) 682 683 self.failUnless(geo_reference == dict['geo_reference'], 684 'test_writepts failed. Test geo_reference') 685 686 ########################## BAD .XYA ########################## 687 688 def test_loadxy_bad_no_file_xya(self): 689 import os 690 import tempfile 691 692 fileName = tempfile.mktemp(".xya") 693 #print fileName 694 try: 695 dict = import_points_file(fileName,delimiter = ' ') 696 except IOError: 697 pass 698 else: 699 self.failUnless(0 ==1, 700 'imaginary file did not raise error!') 701 702 def test_read_write_points_file_bad(self): 703 dict = self.tri_dict.copy() 704 fileName = tempfile.mktemp(".xxx") 705 try: 706 export_points_file(fileName,dict) 707 except IOError: 708 pass 709 else: 710 self.failUnless(0 ==1, 711 'bad points file extension did not raise error!') 712 713 def test_read_write_points_file_bad2(self): 714 dict = {} 715 att_dict = {} 716 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 717 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 718 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 719 dict['attributelist'] = att_dict 720 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 721 try: 722 export_points_file("e:yeah.xya",dict) 723 except IOError: 724 pass 725 else: 726 self.failUnless(0 ==1, 727 'bad points file extension did not raise error!') 728 729 def test_loadxy_bad(self): 730 import os 731 import tempfile 732 733 fileName = tempfile.mktemp(".xya") 734 file = open(fileName,"w") 735 file.write(" elevation \n\ 736 1.0 0.0 10.0 0.0\n\ 737 0.0 1.0 0.0 10.0\n\ 738 1.0 0.0 10.4 40.0\n") 739 file.close() 740 #print fileName 741 try: 742 dict = import_points_file(fileName,delimiter = ' ') 743 except IOError: 744 pass 745 else: 746 self.failUnless(0 ==1, 747 'bad xya file did not raise error!') 748 os.remove(fileName) 749 750 def test_loadxy_bad2(self): 751 import os 752 import tempfile 753 754 fileName = tempfile.mktemp(".xya") 755 file = open(fileName,"w") 756 file.write("elevation\n\ 757 1.0 0.0 10.0 \n\ 758 0.0 1.0\n\ 759 1.0 \n") 760 file.close() 761 #print fileName 762 try: 763 dict = import_points_file(fileName,delimiter = ' ') 764 except IOError: 765 pass 766 else: 767 self.failUnless(0 ==1, 768 'bad xya file did not raise error!') 769 os.remove(fileName) 770 771 def test_loadxy_bad3(self): 772 """ 773 specifying wrong delimiter 774 """ 775 import os 776 import tempfile 777 778 fileName = tempfile.mktemp(".xya") 779 file = open(fileName,"w") 780 file.write(" elevation , speed \n\ 781 1.0, 0.0, 10.0, 0.0\n\ 782 0.0, 1.0, 0.0, 10.0\n\ 783 1.0, 0.0, 10.4, 40.0\n") 784 file.close() 785 try: 786 dict = import_points_file(fileName,delimiter = ' ') 787 except IOError: 788 pass 789 else: 790 self.failUnless(0 ==1, 791 'bad xya file did not raise error!') 792 os.remove(fileName) 793 794 def test_loadxy_bad4(self): 795 """ 796 specifying wrong delimiter 797 """ 798 import os 799 import tempfile 800 801 fileName = tempfile.mktemp(".xya") 802 file = open(fileName,"w") 803 file.write(" elevation speed \n\ 804 1.0 0.0 10.0 0.0\n\ 805 0.0 1.0 0.0 10.0\n\ 806 1.0 0.0 10.4 40.0\n\ 807 yeah") 808 file.close() 809 try: 810 dict = import_points_file(fileName,delimiter = ' ') 811 except IOError: 812 pass 813 else: 814 self.failUnless(0 ==1, 815 'bad xya file did not raise error!') 816 os.remove(fileName) 817 818 def test_loadxy_bad4(self): 819 """ 820 specifying wrong delimiter 821 """ 822 import os 823 import tempfile 824 825 fileName = tempfile.mktemp(".xya") 826 file = open(fileName,"w") 827 file.write(" elevation speed \n\ 828 1.0 0.0 10.0 0.0\n\ 829 0.0 1.0 0.0 10.0\n\ 830 1.0 0.0 10.4 40.0\n\ 831 #geocrap") 832 file.close() 833 try: 834 dict = import_points_file(fileName,delimiter = ' ') 835 except IOError: 836 pass 837 else: 838 self.failUnless(0 ==1, 839 'bad xya file did not raise error!') 840 os.remove(fileName) 841 842 def test_loadxy_bad5(self): 843 """ 844 specifying wrong delimiter 845 """ 846 import os 847 import tempfile 848 849 fileName = tempfile.mktemp(".xya") 850 file = open(fileName,"w") 851 file.write(" elevation speed \n\ 852 1.0 0.0 10.0 0.0\n\ 853 0.0 1.0 0.0 10.0\n\ 854 1.0 0.0 10.4 40.0\n\ 855 #geocrap\n\ 856 crap") 857 file.close() 858 try: 859 dict = import_points_file(fileName,delimiter = ' ') 860 except IOError: 861 pass 862 else: 863 self.failUnless(0 ==1, 864 'bad xya file did not raise error!') 865 os.remove(fileName) 866 ############### .PTS ########## 361 867 362 868 def test_loadpts(self): … … 397 903 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 398 904 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 399 400 905 906 def test_writepts(self): 907 dict = {} 908 att_dict = {} 909 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 910 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 911 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 912 dict['attributelist'] = att_dict 913 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 914 915 916 fileName = tempfile.mktemp(".pts") 917 export_points_file(fileName, dict) 918 dict2 = import_points_file(fileName) 919 #print "fileName",fileName 920 os.remove(fileName) 921 #print "dict2",dict2 922 923 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 924 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 925 answer = [10.0, 0.0, 10.4] 926 assert allclose(dict2['attributelist']['brightness'], answer) 927 928 #print "dict['geo_reference'] ",dict['geo_reference'] 929 #print "dict2['geo_reference']",dict2['geo_reference'] 930 931 self.failUnless(dict['geo_reference'] == dict2['geo_reference'], 932 'test_writepts failed. Test geo_reference') 933 934 ########################## BAD .PTS ########################## 935 936 def test_load_bad_no_file_pts(self): 937 import os 938 import tempfile 939 940 fileName = tempfile.mktemp(".pts") 941 #print fileName 942 try: 943 dict = import_points_file(fileName) 944 except IOError: 945 pass 946 else: 947 self.failUnless(0 ==1, 948 'imaginary file did not raise error!') 949 950 ############### .PTS OTHER ########## 951 401 952 def test_concatinate_attributelist(self): 402 953 dic = {} … … 412 963 'test_concatinate_attributelist failed.') 413 964 assert allclose(block, [[4,2,5,3,1],[47,7,17,79,2]]) 414 415 416 def test_writepts(self): 417 dict = {} 418 att_dict = {} 419 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 420 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 421 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 422 dict['attributelist'] = att_dict 423 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 424 425 426 fileName = tempfile.mktemp(".pts") 427 export_points_file(fileName, dict) 428 dict2 = import_points_file(fileName) 429 #print "fileName",fileName 430 os.remove(fileName) 431 #print "dict2",dict2 432 433 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 434 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 435 answer = [10.0, 0.0, 10.4] 436 assert allclose(dict2['attributelist']['brightness'], answer) 437 438 #print "dict['geo_reference'] ",dict['geo_reference'] 439 #print "dict2['geo_reference']",dict2['geo_reference'] 440 441 self.failUnless(dict['geo_reference'] == dict2['geo_reference'], 442 'test_writepts failed. Test geo_reference') 443 444 def test_write_xya(self): 445 dict = {} 446 att_dict = {} 447 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 448 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 449 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 450 dict['attributelist'] = att_dict 451 dict['geo_reference'] = Geo_reference(56,1.9,1.9) 452 453 454 fileName = tempfile.mktemp(".xya") 455 export_points_file(fileName, dict) 456 dict2 = import_points_file(fileName) 457 #print "fileName",fileName 458 os.remove(fileName) 459 #print "dict2",dict2 460 461 assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 462 assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4]) 463 answer = [10.0, 0.0, 10.4] 464 assert allclose(dict2['attributelist']['brightness'], answer) 465 466 965 467 966 def test_half_pts(self): 468 967 dict = {} … … 488 987 489 988 #print "out_dict['pointlist']",out_dict #['pointlist'] 490 assert allclose(out_dict['pointlist'],[[0.0, -10.0],[10.0, -10.0],[10.0,10.0],[0.0, 10.0]]) 989 assert allclose(out_dict['pointlist'],[[0.0, -10.0],[10.0, -10.0], 990 [10.0,10.0],[0.0, 10.0]]) 491 991 492 992 self.failUnless(dict['attributelist'] == {}, … … 544 1044 assert allclose(dict2['attributelist']['elevation'], [10.0]) 545 1045 assert allclose(dict2['attributelist']['brightness'], [10.0]) 546 547 def test_loadxy3(self): 548 549 fileName = tempfile.mktemp(".xya") 550 file = open(fileName,"w") 551 file.write(" elevation , speed \n\ 552 1.0, 0.0, 10.0, 0.0\n\ 553 0.0, 1.0, 0.0, 10.0\n\ 554 1.0, 0.0, 10.4, 40.0\n") 555 file.close() 556 #print fileName 557 dict = import_points_file(fileName,delimiter = ',') 558 os.remove(fileName) 559 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 560 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 561 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 562 563 def test_loadxy4(self): 564 import os 565 import tempfile 566 567 fileName = tempfile.mktemp(".xya") 568 file = open(fileName,"w") 569 file.write(" elevation speed \n\ 570 1.0 0.0 10.0 0.0\n\ 571 0.0 1.0 0.0 10.0\n\ 572 1.0 0.0 10.4 40.0\n") 573 file.close() 574 #print fileName 575 dict = import_points_file(fileName,delimiter = ' ') 576 os.remove(fileName) 577 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 578 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 579 assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0]) 580 581 def test_loadxy5(self): 582 import os 583 import tempfile 584 585 fileName = tempfile.mktemp(".xya") 586 file = open(fileName,"w") 587 file.write(" elevation \n\ 588 1.0 0.0 10.0 0.0\n\ 589 0.0 1.0 0.0 10.0\n\ 590 1.0 0.0 10.4 40.0\n") 591 file.close() 592 #print fileName 593 try: 594 dict = import_points_file(fileName,delimiter = ' ') 595 except IOError: 596 pass 597 else: 598 self.failUnless(0 ==1, 599 'bad xya file did not raise error!') 600 601 os.remove(fileName) 602 603 def test_loadxy6(self): 604 import os 605 import tempfile 606 607 fileName = tempfile.mktemp(".xya") 608 file = open(fileName,"w") 609 file.write("elevation\n\ 610 1.0 0.0 10.0 \n\ 611 0.0 1.0\n\ 612 1.0 \n") 613 file.close() 614 #print fileName 615 try: 616 dict = import_points_file(fileName,delimiter = ' ') 617 except IOError: 618 pass 619 else: 620 self.failUnless(0 ==1, 621 'bad xya file did not raise error!') 622 623 os.remove(fileName) 624 625 626 def test_import_tsh(self): 627 import os 628 import tempfile 629 630 fileName = tempfile.mktemp(".tsh") 631 file = open(fileName,"w") 632 file.write("elevation\n\ 633 1.0 0.0 10.0 \n\ 634 0.0 1.0\n\ 635 1.0 \n") 636 file.close() 637 #print fileName 638 try: 639 dict = import_points_file(fileName,delimiter = ' ') 640 except IOError: 641 pass 642 else: 643 self.failUnless(0 ==1, 644 'bad tsh file did not raise error!') 645 646 os.remove(fileName) 647 648 def test_import_tsh2(self): 649 import os 650 import tempfile 651 652 fileName = tempfile.mktemp(".msh") 653 file = open(fileName,"w") 654 file.write("1.0 \n\ 655 1.0 0.0 10.0 \n\ 656 0.0 1.0\n\ 657 13.0 \n") 658 file.close() 659 #print fileName 660 try: 661 dict = import_points_file(fileName,delimiter = ' ') 662 except IOError: 663 pass 664 else: 665 self.failUnless(0 ==1, 666 'bad tsh file did not raise error!') 667 668 os.remove(fileName) 669 670 def test_import_tsh3(self): 671 import os 672 import tempfile 673 674 fileName = tempfile.mktemp(".tsh") 675 file = open(fileName,"w") 676 file.write("1.0 \n\ 677 showme1.0 0.0 10.0 \n\ 678 0.0 1.0\n\ 679 13.0 \n") 680 file.close() 681 #print fileName 682 try: 683 dict = import_points_file(fileName,delimiter = ' ') 684 except IOError: 685 pass 686 else: 687 self.failUnless(0 ==1, 688 'bad tsh file did not raise error!') 689 690 os.remove(fileName) 691 1046 692 1047 #------------------------------------------------------------- 693 1048 if __name__ == "__main__": -
inundation/ga/storm_surge/pmesh/test_mesh.py
r1401 r1416 863 863 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') 864 864 865 def test_loadxy2(self):866 """867 To test the mesh side of loading xya files.868 Not the loading of xya files869 """870 import os871 import tempfile872 873 fileName = tempfile.mktemp(".xya")874 file = open(fileName,"w")875 file.write("elevation, speed \n\876 1.0, 0.0, 10.0, 0.0\n\877 0.0, 1.0, 0.0, 10.0\n\878 1.0, 0.0, 10.4, 40.0\n")879 file.close()880 #print fileName881 m = importMeshFromFile(fileName)882 os.remove(fileName)883 884 865 def test_loadxy(self): 885 866 """
Note: See TracChangeset
for help on using the changeset viewer.