Changeset 4165
- Timestamp:
- Jan 10, 2007, 4:50:53 PM (18 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/alpha_shape/alpha_shape.py
r4144 r4165 21 21 import exceptions 22 22 from Numeric import array, Float, divide_safe, sqrt, product 23 from load_mesh.loadASCII import import_points_file, export_boundary_file24 23 import random 24 25 from load_mesh.loadASCII import export_boundary_file 26 from anuga.geospatial_data.geospatial_data import Geospatial_data 25 27 26 28 class AlphaError(exceptions.Exception):pass … … 42 44 the optimum alpha value will be used. 43 45 """ 44 point_dict = import_points_file(point_file)45 points = point_dict['pointlist']46 geospatial = Geospatial_data(point_file) 47 points = geospatial.get_data_points(absolute=False) 46 48 47 49 AS = Alpha_Shape(points, alpha) -
anuga_core/source/anuga/fit_interpolate/fit.py
r4138 r4165 500 500 501 501 502 def fit_to_mesh_file(mesh_file, point_file, mesh_output_file,502 def obsolete_fit_to_mesh_file(mesh_file, point_file, mesh_output_file, 503 503 alpha=DEFAULT_ALPHA, verbose= False, 504 504 display_errors = True): … … 516 516 517 517 """ 518 #OBSOLETE 519 #Problems with using blocking and knowing the attribute title.. 520 518 521 519 522 # Question … … 553 556 # load in the .pts file 554 557 try: 555 point_dict = import_points_file(point_file, verbose=verbose) 558 #point_dict = import_points_file(point_file, verbose=verbose) 559 560 geospatial = Geospatial_data(point_file) 561 point_coordinates = geospatial.get_data_points(absolute=False) 556 562 except IOError,e: 557 563 if display_errors: … … 559 565 raise IOError #Re-raise exception 560 566 561 point_coordinates = point_dict['pointlist'] 562 title_list,point_attributes = concatinate_attributelist(point_dict['attributelist']) 563 564 if point_dict.has_key('geo_reference') and not point_dict['geo_reference'] is None: 565 data_origin = point_dict['geo_reference'].get_origin() 566 else: 567 data_origin = (56, 0, 0) #FIXME(DSG-DSG) 567 #point_coordinates = point_dict['pointlist'] 568 #get_all_attributes 569 #title_list,point_attributes = concatinate_attributelist(point_dict['attributelist']) 570 title_list,point_attributes = concatinate_attributelist( \ 571 geospatial.get_all_attributes()) 572 568 573 569 574 if mesh_dict.has_key('geo_reference') and not mesh_dict['geo_reference'] is None: … … 576 581 f = fit_to_mesh(vertex_coordinates, 577 582 triangles, 578 point_coordinates, 579 point_attributes, 583 point_file, 580 584 alpha = alpha, 581 585 verbose = verbose, -
anuga_core/source/anuga/fit_interpolate/test_fit.py
r4144 r4165 271 271 272 272 f = fit_to_mesh(vertices, triangles,fileName, 273 alpha=0.0, max_read_lines=2)#, verbose=True) 273 alpha=0.0, max_read_lines=2) 274 #use_cache=True, verbose=True) 274 275 answer = linear_function(vertices) 275 276 #print "f\n",f … … 604 605 605 606 606 def test_fit_to_mesh_file(self):607 def obsolete_test_fit_to_mesh_file(self): 607 608 from load_mesh.loadASCII import import_mesh_file, \ 608 609 export_mesh_file … … 656 657 657 658 658 def test_fit_to_mesh_file3(self):659 def obsolete_test_fit_to_mesh_file3(self): 659 660 from load_mesh.loadASCII import import_mesh_file, \ 660 661 export_mesh_file … … 708 709 os.remove(mesh_output_file) 709 710 710 def test_fit_to_mesh_file4(self):711 def obsolete_test_fit_to_mesh_file4(self): 711 712 from load_mesh.loadASCII import import_mesh_file, \ 712 713 export_mesh_file … … 762 763 os.remove(mesh_output_file) 763 764 764 def test_fit_to_mesh_fileII(self):765 def obsolete_test_fit_to_mesh_fileII(self): 765 766 from load_mesh.loadASCII import import_mesh_file, \ 766 767 export_mesh_file … … 813 814 os.remove(point_file) 814 815 815 def test_fit_to_mesh_file_errors(self):816 def obsolete_test_fit_to_mesh_file_errors(self): 816 817 from load_mesh.loadASCII import import_mesh_file, export_mesh_file 817 818 import tempfile … … 851 852 os.remove(point_file) 852 853 853 def test_fit_to_mesh_file_errorsII(self):854 def obsolete_test_fit_to_mesh_file_errorsII(self): 854 855 from load_mesh.loadASCII import import_mesh_file, export_mesh_file 855 856 import tempfile … … 881 882 os.remove(point_file) 882 883 883 def test_fit_to_mesh_file_errorsIII(self):884 def obsolete_test_fit_to_mesh_file_errorsIII(self): 884 885 from load_mesh.loadASCII import import_mesh_file, export_mesh_file 885 886 import tempfile -
anuga_core/source/anuga/geospatial_data/geospatial_data.py
r4154 r4165 173 173 else: 174 174 self.data_points = ensure_numeric(data_points) 175 assert len(self.data_points.shape) == 2 176 assert self.data_points.shape[1] == 2 175 #print "self.data_points.shape",self.data_points.shape 176 if not (0,) == self.data_points.shape: 177 assert len(self.data_points.shape) == 2 178 assert self.data_points.shape[1] == 2 177 179 178 180 def set_attributes(self, attributes): … … 584 586 msg += 'Text file format is moving to comma seperated .txt files.' 585 587 warn(msg, DeprecationWarning) 588 error(msg, DeprecationWarning) 586 589 587 590 if (file_name[-4:] == ".xya"): 591 msg = '.xya format is deprecated. Please use .txt.' 592 warn(msg, DeprecationWarning) 593 #import sys; sys.exit() 588 594 if absolute is True: 589 595 _write_xya_file(file_name, … … 607 613 self.get_geo_reference()) 608 614 609 elif (file_name[-4:] == ".txt"):615 elif file_name[-4:] == ".txt" or file_name[-4:] == ".csv": 610 616 msg = "ERROR: trying to write a .txt file with relative data." 611 617 assert absolute, msg … … 792 798 file_pointer, 793 799 header, 794 max_read_lines=MAX_READ_LINES) #FIXME: how hacky is that!800 max_read_lines=MAX_READ_LINES) #FIXME: must be highest int 795 801 except StopIteration: 796 802 break -
anuga_core/source/anuga/geospatial_data/test_geospatial_data.py
r4154 r4165 11 11 from anuga.coordinate_transforms.geo_reference import Geo_reference, TitleError 12 12 from anuga.coordinate_transforms.redfearn import degminsec2decimal_degrees 13 14 # Ignore these warnings, since we still want to test .xya code. 15 import warnings 16 warnings.filterwarnings(action = 'ignore', 17 message='.xya format is deprecated. Please use .txt.', 18 category=DeprecationWarning) 13 19 14 20 … … 639 645 640 646 os.remove(FN) 647 648 def test_load_csv(self): 649 # To test the mesh side of loading xya files. 650 # Not the loading of xya files 651 652 import os 653 import tempfile 654 655 fileName = tempfile.mktemp(".csv") 656 file = open(fileName,"w") 657 file.write("x,y,elevation speed \n\ 658 1.0 0.0 10.0 0.0\n\ 659 0.0 1.0 0.0 10.0\n\ 660 1.0 0.0 10.4 40.0\n") 661 file.close() 662 #print fileName 663 results = Geospatial_data(fileName, delimiter=',') 664 os.remove(fileName) 665 # print 'data', results.get_data_points() 666 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0], 667 [1.0, 0.0]]) 668 assert allclose(results.get_attributes(attribute_name='elevation'), 669 [10.0, 0.0, 10.4]) 670 assert allclose(results.get_attributes(attribute_name='speed'), 671 [0.0, 10.0, 40.0]) 641 672 642 673 def not_test_loadcsv(self): 643 """ 674 """ not_test_loadcsv(self): 644 675 comma delimited 645 676 """ … … 654 685 os.remove(fileName) 655 686 # print 'data', results.get_data_points() 656 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 657 assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4]) 658 assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0]) 659 660 def test_loadxya(self): 661 """ 687 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0], 688 [1.0, 0.0]]) 689 assert allclose(results.get_attributes(attribute_name='elevation'), 690 [10.0, 0.0, 10.4]) 691 assert allclose(results.get_attributes(attribute_name='speed'), 692 [0.0, 10.0, 40.0]) 693 694 def test_load_xya(self): 695 """ test_load_xya(self): 662 696 comma delimited 663 697 """ … … 677 711 678 712 def test_loadxya2(self): 679 """ 713 """ test_loadxya2(self): 680 714 space delimited 681 715 """ … … 700 734 def test_loadxya3(self): 701 735 """ 736 test_loadxya3(self): 702 737 space delimited 703 738 """ … … 804 839 805 840 def test_loadxy_bad3(self): 806 """ 841 """ test_loadxy_bad3(self): 807 842 specifying wrong delimiter 808 843 """ … … 826 861 827 862 def test_loadxy_bad4(self): 828 """ 863 """ test_loadxy_bad4(self): 829 864 specifying wrong delimiter 830 865 """ … … 853 888 854 889 def test_loadxy_bad5(self): 855 """ 890 """ test_loadxy_bad5(self): 856 891 specifying wrong delimiter 857 892 """ … … 904 939 905 940 def test_load_csv(self): 906 """ 941 """ test_load_csv(self): 907 942 space delimited 908 943 """ … … 942 977 943 978 def test_load_csv_bad(self): 944 """ 979 """ test_load_csv_bad(self): 945 980 space delimited 946 981 """ … … 998 1033 999 1034 def test_export_xya_file2(self): 1000 """test absolute xya file 1035 """ test_export_xya_file2(self): 1036 test absolute xya file 1001 1037 """ 1002 1038 att_dict = {} … … 1018 1054 1019 1055 def test_export_xya_file3(self): 1020 """test absolute xya file with geo_ref 1056 """ test_export_xya_file3(self): 1057 test absolute xya file with geo_ref 1021 1058 """ 1022 1059 att_dict = {} … … 1354 1391 1355 1392 def test_add_(self): 1356 '''adds an xya and pts files, reads the files and adds them 1393 '''test_add_(self): 1394 adds an xya and pts files, reads the files and adds them 1357 1395 checking results are correct 1358 1396 ''' -
anuga_core/source/anuga/load_mesh/loadASCII.py
r4099 r4165 75 75 ##FIXME (DSG-DSG) Is the dict format mentioned above a list of a numeric array? 76 76 # Needs to be defined 77 ##FIXME (DSG-DSG) if the ascii file isn't .xya give a better error message.78 79 # FIXME (Ole): Has this stuff been superseded by geospatial data?80 # Much of this code is also there81 77 82 78 … … 973 969 ### 974 970 975 #FIXME (DSG): These should be obsolete. Use geospatial objects.976 def export_points_file(ofile, point_dict):977 """978 write a points file, ofile, as a text (.xya) or binary (.pts) file979 980 ofile is the file name, including the extension981 982 The point_dict is defined at the top of this file.983 """984 #this was done for all keys in the mesh file.985 #if not mesh_dict.has_key('points'):986 # mesh_dict['points'] = []987 if (ofile[-4:] == ".xya"):988 _write_xya_file(ofile, point_dict)989 elif (ofile[-4:] == ".pts"):990 _write_pts_file(ofile, point_dict)991 else:992 msg = 'Unknown file type %s ' %ofile993 raise IOError, msg994 995 def import_points_file(ofile, delimiter = None, verbose = False):996 """997 load an .xya or .pts file998 999 Note: will throw an IOError if it can't load the file.1000 Catch these!1001 """1002 1003 if ofile[-4:]== ".xya":1004 try:1005 if delimiter == None:1006 try:1007 fd = open(ofile)1008 points_dict = _read_xya_file(fd, ',')1009 except TitleError: # this is catching the error thrown by geo_ref1010 fd.close()1011 fd = open(ofile)1012 points_dict = _read_xya_file(fd, ' ')1013 else:1014 fd = open(ofile)1015 points_dict = _read_xya_file(fd, delimiter)1016 fd.close()1017 except (IndexError,ValueError,SyntaxError):1018 fd.close()1019 msg = 'Could not open file %s ' %ofile1020 raise IOError, msg1021 except TitleError:1022 print "reclassifying title error"1023 fd.close()1024 msg = 'Could not open file %s ' %ofile1025 raise IOError, msg1026 except IOError:1027 # Catch this to add an error message1028 msg = 'Could not open file %s ' %ofile1029 raise IOError, msg1030 1031 elif ofile[-4:]== ".pts":1032 try:1033 points_dict = _read_pts_file(ofile, verbose)1034 except IOError, e:1035 msg = 'Could not open file %s ' %ofile1036 raise IOError, msg1037 else:1038 msg = 'Extension %s is unknown' %ofile[-4:]1039 raise IOError, msg1040 return points_dict1041 1042 971 def extent_point_atts(point_atts): 1043 972 """ … … 1121 1050 return dic.keys(), point_attributes 1122 1051 1123 def _read_pts_file(file_name, verbose = False):1124 """Read .pts NetCDF file1125 1126 Return a dic of array of points, and dic of array of attribute1127 1128 eg1129 dic['points'] = [[1.0,2.0],[3.0,5.0]]1130 dic['attributelist']['elevation'] = [[7.0,5.0]1131 """1132 #FIXME: (DSG)This format has issues.1133 # There can't be an attribute called points1134 # consider format change1135 1136 1137 if verbose: print 'Reading ', file_name1138 1139 1140 # see if the file is there. Throw a QUIET IO error if it isn't1141 fd = open(file_name,'r')1142 fd.close()1143 1144 #throws prints to screen if file not present1145 fid = NetCDFFile(file_name, 'r')1146 1147 point_atts = {}1148 # Get the variables1149 point_atts['pointlist'] = array(fid.variables['points'])1150 keys = fid.variables.keys()1151 if verbose: print 'Got %d variables: %s' %(len(keys), keys)1152 try:1153 keys.remove('points')1154 except IOError, e:1155 fid.close()1156 msg = 'Expected keyword "points" but could not find it'1157 raise IOError, msg1158 1159 attributes = {}1160 for key in keys:1161 if verbose: print "reading attribute '%s'" %key1162 1163 attributes[key] = array(fid.variables[key])1164 1165 point_atts['attributelist'] = attributes1166 1167 try:1168 geo_reference = Geo_reference(NetCDFObject=fid)1169 point_atts['geo_reference'] = geo_reference1170 except AttributeError, e:1171 #geo_ref not compulsory1172 point_atts['geo_reference'] = None1173 1174 1175 fid.close()1176 1177 #print "point_atts",point_atts1178 return point_atts1179 1180 def _write_pts_file(file_name, point_atts):1181 """Write .pts NetCDF file1182 1183 WARNING: This function mangles the point_atts data structure1184 """1185 #FIXME: (DSG)This format has issues.1186 # There can't be an attribute called points1187 # consider format change1188 1189 1190 legal_keys = ['pointlist', 'attributelist', 'geo_reference']1191 for key in point_atts.keys():1192 msg = 'Key %s is illegal. Valid keys are %s' %(key, legal_keys)1193 assert key in legal_keys, msg1194 1195 point_atts2array(point_atts)1196 # NetCDF file definition1197 outfile = NetCDFFile(file_name, 'w')1198 1199 #Create new file1200 outfile.institution = 'Geoscience Australia'1201 outfile.description = 'NetCDF format for compact and portable storage ' +\1202 'of spatial point data'1203 1204 # dimension definitions1205 shape = point_atts['pointlist'].shape[0]1206 outfile.createDimension('number_of_points', shape)1207 outfile.createDimension('number_of_dimensions', 2) #This is 2d data1208 1209 # variable definition1210 outfile.createVariable('points', Float, ('number_of_points',1211 'number_of_dimensions'))1212 1213 #create variables1214 outfile.variables['points'][:] = point_atts['pointlist'] #.astype(Float32)1215 for key in point_atts['attributelist'].keys():1216 outfile.createVariable(key, Float, ('number_of_points',))1217 outfile.variables[key][:] = point_atts['attributelist'][key] #.astype(Float32)1218 1219 if point_atts.has_key('geo_reference') and not point_atts['geo_reference'] == None:1220 point_atts['geo_reference'].write_NetCDF(outfile)1221 1222 outfile.close()1223 1224 def _read_xya_file(fd, delimiter):1225 #lines = fd.readlines()1226 points = []1227 pointattributes = []1228 #if len(lines) <= 1:1229 # raise SyntaxError1230 title = fd.readline()1231 #title = lines.pop(0) # the first (title) line1232 att_names = clean_line(title,delimiter)1233 1234 att_dict = {}1235 line = fd.readline()1236 numbers = clean_line(line,delimiter)1237 #for line in lines:1238 while len(numbers) > 1:1239 #print "line >%s" %line1240 #numbers = clean_line(line,delimiter)1241 #print "numbers >%s<" %numbers1242 #if len(numbers) < 2 and numbers != []:1243 1244 # A line without two numbers1245 # or a bad delimiter1246 #FIXME dsg-dsg change error that is raised.1247 #raise SyntaxError1248 if numbers != []:1249 try:1250 x = float(numbers[0])1251 y = float(numbers[1])1252 points.append([x,y])1253 numbers.pop(0)1254 numbers.pop(0)1255 #attributes = []1256 #print "att_names",att_names1257 #print "numbers",numbers1258 if len(att_names) != len(numbers):1259 fd.close()1260 # It might not be a problem with the title1261 #raise TitleAmountError1262 raise IOError1263 for i,num in enumerate(numbers):1264 num.strip()1265 if num != '\n' and num != '':1266 #attributes.append(float(num))1267 att_dict.setdefault(att_names[i],[]).append(float(num))1268 1269 except ValueError:1270 raise SyntaxError1271 line = fd.readline()1272 numbers = clean_line(line,delimiter)1273 1274 if line == '':1275 # end of file1276 geo_reference = None1277 else:1278 geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)1279 1280 xya_dict = {}1281 xya_dict['pointlist'] = array(points).astype(Float)1282 1283 for key in att_dict.keys():1284 att_dict[key] = array(att_dict[key]).astype(Float)1285 xya_dict['attributelist'] = att_dict1286 xya_dict['geo_reference'] = geo_reference1287 #print "xya_dict",xya_dict1288 return xya_dict1289 1290 1052 #FIXME(dsg), turn this dict plus methods into a class? 1291 1053 def take_points(dict,indices_to_keep): … … 1315 1077 combined['geo_reference'] = dict1['geo_reference'] 1316 1078 return combined 1317 1318 def _write_xya_file( file_name, xya_dict, delimiter = ','):1319 """1320 export a file, ofile, with the xya format1321 1322 """1323 points = xya_dict['pointlist']1324 pointattributes = xya_dict['attributelist']1325 1326 fd = open(file_name,'w')1327 1328 titlelist = ""1329 for title in pointattributes.keys():1330 titlelist = titlelist + title + delimiter1331 titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter1332 fd.write(titlelist+"\n")1333 #<vertex #> <x> <y> [attributes]1334 for i,vert in enumerate( points):1335 1336 attlist = ","1337 for att in pointattributes.keys():1338 attlist = attlist + str(pointattributes[att][i])+ delimiter1339 attlist = attlist[0:-len(delimiter)] # remove the last delimiter1340 attlist.strip()1341 fd.write( str(vert[0]) + delimiter1342 + str(vert[1])1343 + attlist + "\n")1344 1345 # geo_reference info1346 if xya_dict.has_key('geo_reference') and \1347 not xya_dict['geo_reference'] is None:1348 xya_dict['geo_reference'].write_ASCII(fd)1349 fd.close()1350 1079 1351 1080 if __name__ == "__main__": -
anuga_core/source/anuga/load_mesh/test_loadASCII.py
r3514 r4165 12 12 from Numeric import array, allclose 13 13 14 from loadASCII import *14 from anuga.load_mesh.loadASCII import * 15 15 from anuga.coordinate_transforms.geo_reference import Geo_reference 16 16 import loadASCII … … 320 320 #print fileName 321 321 try: 322 dict = import_ points_file(fileName,delimiter = ' ')322 dict = import_mesh_file(fileName) 323 323 except IOError: 324 324 pass … … 341 341 #print fileName 342 342 try: 343 dict = import_ points_file(fileName,delimiter = ' ')343 dict = import_mesh_file(fileName) 344 344 except IOError: 345 345 pass … … 519 519 'imaginary file did not raise error!') 520 520 521 def t est_import_msh_bad(self):521 def throws_error_2_screen_test_import_mesh_bad(self): 522 522 import os 523 523 import tempfile … … 533 533 #print fileName 534 534 try: 535 dict = import_ points_file(fileName,delimiter = ' ')535 dict = import_mesh_file(fileName) 536 536 except IOError: 537 537 pass … … 540 540 'bad msh file did not raise error!') 541 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("_???/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 ########## 867 868 def test_loadpts(self): 869 870 from Scientific.IO.NetCDF import NetCDFFile 871 872 fileName = tempfile.mktemp(".pts") 873 # NetCDF file definition 874 outfile = NetCDFFile(fileName, 'w') 875 876 # dimension definitions 877 outfile.createDimension('number_of_points', 3) 878 outfile.createDimension('number_of_dimensions', 2) #This is 2d data 879 880 # variable definitions 881 outfile.createVariable('points', Float, ('number_of_points', 882 'number_of_dimensions')) 883 outfile.createVariable('elevation', Float, ('number_of_points',)) 884 885 # Get handles to the variables 886 points = outfile.variables['points'] 887 elevation = outfile.variables['elevation'] 888 889 points[0, :] = [1.0,0.0] 890 elevation[0] = 10.0 891 points[1, :] = [0.0,1.0] 892 elevation[1] = 0.0 893 points[2, :] = [1.0,0.0] 894 elevation[2] = 10.4 895 896 outfile.close() 897 898 dict = import_points_file(fileName) 899 os.remove(fileName) 900 answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]] 901 #print "dict['pointlist']",dict['pointlist'] 902 #print "answer",answer 903 assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 904 assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4]) 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 952 def test_concatinate_attributelist(self): 953 dic = {} 954 dic['one'] = array([1,2]) 955 dic['2'] = array([2,7]) 956 dic['three'] = array([3,79]) 957 dic['4'] = array([4,47]) 958 dic['five'] = array([5,17]) 959 titles, block = concatinate_attributelist(dic) 960 #print "titles", titles 961 #print "array", block 962 self.failUnless(titles == ['4', '2', 'five', 'three', 'one'], 963 'test_concatinate_attributelist failed.') 964 assert allclose(block, [[4,2,5,3,1],[47,7,17,79,2]]) 965 966 def test_half_pts(self): 967 dict = {} 968 att_dict = {} 969 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 970 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 971 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 972 dict['attributelist'] = att_dict 973 out_dict = half_pts(dict) 974 assert allclose(out_dict['pointlist'],[[1.0, 0.0],[1.0, 0.0]]) 975 assert allclose(out_dict['attributelist']['elevation'], [10.0, 10.4]) 976 assert allclose(out_dict['attributelist']['brightness'], [10.0, 10.4]) 977 978 def test_extent_point_atts(self): 979 980 dict = {} 981 att_dict = {} 982 dict['pointlist'] = array([[1.0, 10.0],[0.0, 1.0],[10.0, -10.0]]) 983 att_dict['elevation'] = array([30.0, 0.0, 10.4]) 984 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 985 dict['attributelist'] = att_dict 986 out_dict = extent_point_atts(dict) 987 988 #print "out_dict['pointlist']",out_dict #['pointlist'] 989 assert allclose(out_dict['pointlist'],[[0.0, -10.0],[10.0, -10.0], 990 [10.0,10.0],[0.0, 10.0]]) 991 992 self.failUnless(dict['attributelist'] == {}, 993 'test_extent_point_atts failed. Test 1') 994 995 def test_reduce_pts(self): 996 dict = {} 997 att_dict = {} 998 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 999 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 1000 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 1001 dict['attributelist'] = att_dict 1002 1003 inFileName = tempfile.mktemp(".pts") 1004 export_points_file(inFileName, dict) 1005 1006 outFileName = tempfile.mktemp(".pts") 1007 1008 dict2 = reduce_pts(inFileName,outFileName, 1 ) 1009 os.remove(inFileName) 1010 1011 dict2 = import_points_file(outFileName) 1012 os.remove(outFileName) 1013 #print "dict2",dict2 1014 1015 assert allclose(dict2['pointlist'],[[1.0, 0.0]]) 1016 assert allclose(dict2['attributelist']['elevation'], [10.0]) 1017 assert allclose(dict2['attributelist']['brightness'], [10.0]) 1018 1019 def test_produce_half_point_files(self): 1020 dict = {} 1021 att_dict = {} 1022 dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1023 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 1024 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 1025 dict['attributelist'] = att_dict 1026 1027 inFileName = tempfile.mktemp(".pts") 1028 export_points_file(inFileName, dict) 1029 1030 outFileName = tempfile.mktemp(".pts") 1031 delimiter = '_p' 1032 outfiles = produce_half_point_files(inFileName, 1, 1033 delimiter, verbose = False ) 1034 os.remove(inFileName) 1035 root, ext = splitext(inFileName) 1036 outFileName = root + delimiter + ext 1037 #print "outFileName",outfiles 1038 dict2 = import_points_file(outfiles[1]) 1039 for file in outfiles: 1040 #print "del file",file 1041 os.remove(file) 1042 1043 assert allclose(dict2['pointlist'],[[1.0, 0.0]]) 1044 assert allclose(dict2['attributelist']['elevation'], [10.0]) 1045 assert allclose(dict2['attributelist']['brightness'], [10.0]) 1046 542 1047 543 #------------------------------------------------------------- 1048 544 if __name__ == "__main__": … … 1050 546 suite = unittest.makeSuite(loadASCIITestCase,'test') 1051 547 #suite = unittest.makeSuite(loadASCIITestCase,'test_writepts') 1052 runner = unittest.TextTestRunner( verbosity=0)548 runner = unittest.TextTestRunner() #verbosity=0) 1053 549 runner.run(suite) 1054 550 -
anuga_core/source/anuga/mesh_engine/test_generate_mesh.py
r4155 r4165 523 523 #suite = unittest.makeSuite(triangTestCase,'test_lone_verts4') 524 524 #suite = unittest.makeSuite(triangTestCase,'testrectangleIIb') 525 runner = unittest.TextTestRunner( verbosity=2) #verbosity=2)525 runner = unittest.TextTestRunner() #verbosity=2) 526 526 runner.run(suite) -
anuga_core/source/anuga/pmesh/mesh.py
r4144 r4165 25 25 26 26 27 #import load_mesh 27 28 import load_mesh 28 29 from anuga.coordinate_transforms.geo_reference import Geo_reference,DEFAULT_ZONE 29 30 from anuga.utilities.polygon import point_in_polygon 30 import load_mesh.loadASCII31 import anuga.load_mesh.loadASCII 31 32 import anuga.alpha_shape.alpha_shape 32 from anuga.geospatial_data.geospatial_data import Geospatial_data, ensure_geospatial, ensure_absolute, ensure_numeric 33 from anuga.geospatial_data.geospatial_data import Geospatial_data, \ 34 ensure_geospatial, ensure_absolute, ensure_numeric 33 35 from anuga.mesh_engine.mesh_engine import generate_mesh 36 34 37 35 38 #import anuga.mesh_engine_b.mesh_engine as triang … … 2018 2021 2019 2022 mesh_dict = self.Mesh2IODict() 2020 point_dict = {}2021 point_dict['attributelist'] = {} #this will need to be expanded..2023 #point_dict = {} 2024 #point_dict['attributelist'] = {} #this will need to be expanded.. 2022 2025 # if attributes are brought back in. 2023 point_dict['geo_reference'] = self.geo_reference2026 #point_dict['geo_reference'] = self.geo_reference 2024 2027 if mesh_dict['vertices'] == []: 2025 point_dict['pointlist'] = mesh_dict['points'] 2028 #point_dict['pointlist'] = mesh_dict['points'] 2029 geo = Geospatial_data(mesh_dict['points'], 2030 geo_reference=self.geo_reference) 2026 2031 else: 2027 point_dict['pointlist'] = mesh_dict['vertices'] 2028 2029 load_mesh.loadASCII.export_points_file(ofile,point_dict) 2032 #point_dict['pointlist'] = mesh_dict['vertices'] 2033 geo = Geospatial_data(mesh_dict['vertices'], 2034 geo_reference=self.geo_reference) 2035 2036 geo.export_points_file(ofile, absolute=True) 2037 2030 2038 2031 2039 … … 2995 3003 """ 2996 3004 newmesh = None 2997 if (ofile[-4:]== ".xya" or ofile[-4:]== ".pts"): 2998 dict = load_mesh.loadASCII.import_points_file(ofile) 2999 dict['points'] = dict['pointlist'] 3005 if (ofile[-4:]== ".xya" or ofile[-4:]== ".pts" or ofile[-4:]== ".txt" or \ 3006 ofile[-4:]== ".csv"): 3007 #dict = load_mesh.loadASCII.import_points_file(ofile) 3008 geospatial = Geospatial_data(ofile) 3009 dict = {} 3010 dict['points'] = geospatial.get_data_points(absolute=False) 3000 3011 dict['outline_segments'] = [] 3001 3012 dict['outline_segment_tags'] = [] … … 3004 3015 dict['region_max_areas'] = [] 3005 3016 dict['holes'] = [] 3006 newmesh= Mesh(geo_reference = dict['geo_reference'])3017 newmesh= Mesh(geo_reference = geospatial.geo_reference) 3007 3018 newmesh.IOOutline2Mesh(dict) 3008 3019 counter = newmesh.removeDuplicatedUserVertices() -
anuga_core/source/anuga/pmesh/test_mesh.py
r4144 r4165 8 8 #except ImportError: 9 9 # from mesh import * 10 10 11 11 12 from load_mesh.loadASCII import * 12 13 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 986 987 import tempfile 987 988 988 fileName = tempfile.mktemp(". xya")989 fileName = tempfile.mktemp(".csv") 989 990 file = open(fileName,"w") 990 file.write(" elevationspeed \n\991 1.0 0.0 10.00.0\n\992 0.0 1.0 0.010.0\n\993 1.0 0.0 10.440.0\n")991 file.write("x,y,elevation, speed \n\ 992 1.0, 0.0, 10.0, 0.0\n\ 993 0.0, 1.0, 0.0, 10.0\n\ 994 1.0, 0.0, 10.4, 40.0\n") 994 995 file.close() 995 996 #print fileName … … 1009 1010 # 'loadxy, test 5 failed') 1010 1011 1011 def test_exportPointsFile(self):1012 def exportPointsFile(self): 1012 1013 a = Vertex (0,0) 1013 1014 b = Vertex (0,3) … … 1029 1030 holes = [h1]) 1030 1031 1031 fileName = tempfile.mktemp(". xya")1032 fileName = tempfile.mktemp(".txt") 1032 1033 #fileName = 't.xya' 1033 1034 #os.remove(fileName) … … 1038 1039 1039 1040 os.remove(fileName) 1040 self.failUnless(lFile[0] == " " and1041 self.failUnless(lFile[0] == "x,y" and 1041 1042 lFile[1] == "0,0" and 1042 1043 lFile[2] == "0,3" and … … 1069 1070 'exported Ascii xya file is wrong') 1070 1071 1071 def t est_lone_vert_in_mesh_gen_c_layer(self):1072 def to_be_test_lone_vert_in_mesh_gen_c_layer(self): 1072 1073 # currently just a copy of the above test 1073 1074 a = Vertex (0,0) … … 1090 1091 holes = [h1]) 1091 1092 1092 fileName = tempfile.mktemp(". xya")1093 fileName = tempfile.mktemp(".csv") 1093 1094 #fileName = 't.xya' 1094 1095 #os.remove(fileName) … … 1099 1100 1100 1101 os.remove(fileName) 1101 self.failUnless(lFile[0] == " " and1102 self.failUnless(lFile[0] == "x,y" and 1102 1103 lFile[1] == "0,0" and 1103 1104 lFile[2] == "0,3" and … … 1113 1114 # it is a loner and it is removed. 1114 1115 m.generateMesh("Q", maxArea = 2.1) 1115 fileName = tempfile.mktemp(". xya")1116 fileName = tempfile.mktemp(".csv") 1116 1117 #fileName = 't.xya' 1117 1118 #m.export_mesh_file('m.tsh') … … 1122 1123 os.remove(fileName) 1123 1124 1124 self.failUnless(lFile[0] == " " and1125 self.failUnless(lFile[0] == "x,y" and 1125 1126 lFile[1] == "0.0,0.0" and 1126 1127 lFile[2] == "0.0,3.0" and … … 1130 1131 'exported Ascii xya file is wrong') 1131 1132 1132 def test_exportPointsFilefile2(self): 1133 def NOT_test_exportPointsFilefile2(self): 1134 #geospatial needs at least one point 1133 1135 m = Mesh() 1134 1136 1135 fileName = tempfile.mktemp(". xya")1137 fileName = tempfile.mktemp(".csv") 1136 1138 m.exportPointsFile(fileName) 1137 1139 file = open(fileName)
Note: See TracChangeset
for help on using the changeset viewer.