Changeset 1376
- Timestamp:
- May 12, 2005, 12:11:44 PM (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
r1375 r1376 50 50 Note: point att's have no titles - that's currently ok, since least 51 51 squares adds the point info to vertices, and they have titles 52 53 The format for a .tsh file is (FIXME update this) 54 55 First line: <# of vertices> <# of attributes> 56 Following lines: <vertex #> <x> <y> [attributes] 57 One line: <# of triangles> 58 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] 59 One line: <# of segments> 60 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 52 61 """ 53 62 ##FIXME (DSG-DSG) Is the dict format mentioned above a list of a numeric array? … … 84 93 def import_mesh_file(ofile): 85 94 """ 86 reading triangulation and meshoutline info. 87 import a file, ofile, with the format 88 89 First line: <# of vertices> <# of attributes> 90 Following lines: <vertex #> <x> <y> [attributes] 91 One line: <# of triangles> 92 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] 93 One line: <# of segments> 94 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 95 read a mesh file, either .tsh or .msh 96 95 97 Note: This might throw a can't load file error 96 98 """ … … 105 107 return dict 106 108 109 110 def export_mesh_file(ofile,mesh_dict): 111 """ 112 write a file, ofile, with the format 113 114 First line: <# of vertices> <# of attributes> 115 Following lines: <vertex #> <x> <y> [attributes] 116 One line: <# of triangles> 117 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] 118 One line: <# of segments> 119 Following lines: <segment #> <vertex #> <vertex #> [boundary tag] 120 """ 121 #FIXME DSG-DSG: automate 122 if not mesh_dict.has_key('points'): 123 mesh_dict['points'] = [] 124 if not mesh_dict.has_key('point_attributes'): 125 mesh_dict['point_attributes'] = [] 126 if not mesh_dict.has_key('outline_segments'): 127 mesh_dict['outline_segments'] = [] 128 if not mesh_dict.has_key('outline_segment_tags'): 129 mesh_dict['outline_segment_tags'] = [] 130 if not mesh_dict.has_key('holes'): 131 mesh_dict['holes'] = [] 132 if not mesh_dict.has_key('regions'): 133 mesh_dict['regions'] = [] 134 135 if not mesh_dict.has_key('region_tags'): 136 mesh_dict['region_tags'] = [] 137 if not mesh_dict.has_key('region_max_areas'): 138 mesh_dict['region_max_areas'] = [] 139 if not mesh_dict.has_key('vertices'): 140 mesh_dict['vertices'] = [] 141 if not mesh_dict.has_key('vertex_attributes'): 142 mesh_dict['vertex_attributes'] = [] 143 if not mesh_dict.has_key('vertex_attribute_titles'): 144 mesh_dict['vertex_attribute_titles'] = [] 145 if not mesh_dict.has_key('segments'): 146 mesh_dict['segments'] = [] 147 if not mesh_dict.has_key('segment_tags'): 148 mesh_dict['segment_tags'] = [] 149 if not mesh_dict.has_key('triangles'): 150 mesh_dict['triangles'] = [] 151 if not mesh_dict.has_key('triangle_tags'): 152 mesh_dict['triangle_tags'] = [] 153 if not mesh_dict.has_key('triangle_neighbors'): 154 mesh_dict['triangle_neighbors'] = [] 155 #print "DSG************" 156 #print "mesh_dict",mesh_dict 157 #print "DSG************" 158 159 try: 160 if (ofile[-4:] == ".tsh"): 161 _write_tsh_file(ofile,mesh_dict) 162 elif (ofile[-4:] == ".msh"): 163 _write_msh_file(ofile, mesh_dict) 164 except IOError, e: 165 msg = 'Could not write file %s ' %fileName 166 raise IOError, msg 167 168 107 169 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 170 """ 171 Read the text file format for meshes 172 """ 173 fd = open(ofile,'r') 174 dict = _read_triangulation(fd) 175 dict_mesh = _read_outline(fd) 176 for element in dict_mesh.keys(): 177 dict[element] = dict_mesh[element] 178 fd.close() 179 return dict 115 180 116 181 … … 374 439 def clean_line(line,delimiter): 375 440 """Remove whitespace 376 FIXME (Ole): ... or rather: Remove empty fields?377 I believe this could also be done by simply allowing delimiter to None.378 (DSG) - I just added stripping of whitespaces if the delimiter is other379 than splace. simply allowing delimiter to None would not handle this380 situation.381 441 """ 382 442 #print ">%s" %line … … 950 1010 951 1011 ### 952 # LOADINGXYA FILES1012 # IMPORT/EXPORT XYA FILES 953 1013 ### 1014 1015 def export_points_file(ofile,point_dict): 1016 """ 1017 write a points file, ofile, as a binary (.msh) or text (.tsh) file 1018 """ 1019 #this was done for all keys in the mesh file. 1020 #if not mesh_dict.has_key('points'): 1021 # mesh_dict['points'] = [] 1022 try: 1023 if (ofile[-4:] == ".xya"): 1024 _write_xya_file(ofile, point_dict) 1025 elif (ofile[-4:] == ".pts"): 1026 _write_pts_file(ofile, point_dict) 1027 except IOError, e: 1028 msg = 'Could not write file %s ' %fileName 1029 raise IOError, msg 1030 1031 def import_points_file(ofile, delimiter = None, verbose = False): 1032 """ 1033 load an .xya or .pts file 1034 """ 1035 1036 if ofile[-4:]== ".xya": 1037 try: 1038 if delimiter == None: 1039 try: 1040 fd = open(ofile) 1041 points_dict = _read_xya_file(fd, ',') 1042 except SyntaxError: 1043 fd.close() 1044 fd = open(ofile) 1045 points_dict = _read_xya_file(fd, ' ') 1046 else: 1047 fd = open(ofile) 1048 points_dict = _read_xya_file(fd, delimiter) 1049 fd.close() 1050 points_dict['geo_reference'] = None 1051 except IOError, e: 1052 msg = 'Could not open file %s ' %ofile 1053 raise IOError, msg 1054 1055 elif ofile[-4:]== ".pts": 1056 try: 1057 points_dict = _read_pts_file(ofile, verbose) 1058 except IOError, e: 1059 msg = 'Could not open file %s ' %ofile 1060 raise IOError, msg 1061 else: 1062 msg = 'Extension %s is unknown' %ofile[-4:] 1063 raise IOError, msg 1064 return points_dict 954 1065 955 1066 def extent_point_atts(point_atts): … … 978 1089 979 1090 def reduce_pts(infile, outfile, max_points, verbose = False): 980 point_atts = read_pts(infile)1091 point_atts = _read_pts_file(infile) 981 1092 while point_atts['pointlist'].shape[0] > max_points: 982 1093 if verbose: print "point_atts['pointlist'].shape[0]" 983 1094 point_atts = half_pts(point_atts) 984 write_pts(outfile, point_atts)1095 export_points_file(outfile, point_atts) 985 1096 986 1097 def produce_half_point_files(infile, max_points, delimiter, verbose = False): 987 point_atts = read_pts(infile)1098 point_atts = _read_pts_file(infile) 988 1099 root, ext = splitext(infile) 989 1100 outfiles = [] … … 994 1105 outfile = root + delimiter + str(point_atts['pointlist'].shape[0]) + ext 995 1106 outfiles.append(outfile) 996 write_pts(outfile,1107 export_points_file(outfile, 997 1108 point_atts) 998 1109 return outfiles … … 1028 1139 return dic.keys(), point_attributes 1029 1140 1030 def read_pts(filename, verbose = False):1141 def _read_pts_file(filename, verbose = False): 1031 1142 """Read .pts NetCDF file 1032 1143 … … 1056 1167 fid.close() 1057 1168 msg = 'Expected keyword "points" but could not find it' 1058 #FIXME (Ole): Also the original error e,1059 #could be added and raised again1060 # (DSG) The original error e is useful for fixing bugs1061 # This is an issue with a file that has been caught,1062 # not a bug.1063 #(Ole): I see, this is true. Let's remove this conversation:-)1064 1169 raise IOError, msg 1065 1170 … … 1085 1190 return point_atts 1086 1191 1087 def write_pts(filename, point_atts):1192 def _write_pts_file(filename, point_atts): 1088 1193 """Write .pts NetCDF file 1089 1194 … … 1123 1228 1124 1229 outfile.close() 1125 1126 def load_points_file(ofile, delimiter = ',', verbose = False): 1127 """ 1128 load an .xya or .pts file 1129 """ 1130 1131 if ofile[-4:]== ".xya": 1132 try: 1133 fd = open(ofile) 1134 points_dict = read_xya_file(fd, delimiter) 1135 fd.close() 1136 except IOError, e: 1137 msg = 'Could not open file %s ' %ofile 1138 raise IOError, msg 1139 1140 elif ofile[-4:]== ".pts": 1141 try: 1142 points_dict = read_pts(ofile, verbose) 1143 except IOError, e: 1144 msg = 'Could not open file %s ' %ofile 1145 raise IOError, msg 1146 else: 1147 msg = 'Extension %s is unknown' %ofile[-4:] 1148 raise IOError, msg 1149 return points_dict 1150 1151 def read_xya_file(fd, delimiter): 1230 1231 def _read_xya_file(fd, delimiter): 1152 1232 lines = fd.readlines() 1153 1233 points = [] -
inundation/ga/storm_surge/pmesh/load_mesh/test_loadASCII.py
r1375 r1376 434 434 outfile.close() 435 435 436 dict = load_points_file(fileName)436 dict = import_points_file(fileName) 437 437 os.remove(fileName) 438 438 answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]] … … 469 469 470 470 fileName = tempfile.mktemp(".pts") 471 write_pts(fileName, dict)472 dict2 = load_points_file(fileName)471 export_points_file(fileName, dict) 472 dict2 = import_points_file(fileName) 473 473 #print "fileName",fileName 474 474 os.remove(fileName) … … 524 524 525 525 inFileName = tempfile.mktemp(".pts") 526 write_pts(inFileName, dict)526 export_points_file(inFileName, dict) 527 527 528 528 outFileName = tempfile.mktemp(".pts") … … 531 531 os.remove(inFileName) 532 532 533 dict2 = load_points_file(outFileName)533 dict2 = import_points_file(outFileName) 534 534 os.remove(outFileName) 535 535 #print "dict2",dict2 … … 548 548 549 549 inFileName = tempfile.mktemp(".pts") 550 write_pts(inFileName, dict)550 export_points_file(inFileName, dict) 551 551 552 552 outFileName = tempfile.mktemp(".pts") … … 558 558 outFileName = root + delimiter + ext 559 559 #print "outFileName",outfiles 560 dict2 = load_points_file(outfiles[1])560 dict2 = import_points_file(outfiles[1]) 561 561 for file in outfiles: 562 562 #print "del file",file … … 577 577 file.close() 578 578 #print fileName 579 dict = load_points_file(fileName,delimiter = ',')579 dict = import_points_file(fileName,delimiter = ',') 580 580 os.remove(fileName) 581 581 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) … … 595 595 file.close() 596 596 #print fileName 597 dict = load_points_file(fileName,delimiter = ' ')597 dict = import_points_file(fileName,delimiter = ' ') 598 598 os.remove(fileName) 599 599 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) … … 614 614 #print fileName 615 615 try: 616 dict = load_points_file(fileName,delimiter = ' ')616 dict = import_points_file(fileName,delimiter = ' ') 617 617 except TitleAmountError: 618 618 pass
Note: See TracChangeset
for help on using the changeset viewer.