Changeset 4659
- Timestamp:
- Aug 7, 2007, 10:20:18 AM (17 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/fit_interpolate/test_fit.py
r4590 r4659 660 660 661 661 #Fit surface to mesh 662 interp = Fit(points, triangles, 663 alpha=0.0) #, 664 # mesh_origin = mesh_origin) 665 666 f1 = interp.fit(data_points1,z) #Fitted values at vertices (using same z as before) 662 interp = Fit(points, triangles, alpha=0.0) 663 #Fitted values at vertices (using same z as before) 664 f1 = interp.fit(data_points1,z) 667 665 668 666 assert allclose(f,f1), 'Fit should have been unaltered' … … 752 750 export_mesh_file(mesh_file,mesh_dic) 753 751 754 # create a n .xyafile755 point_file = tempfile.mktemp(". xya")752 # create a points .csv file 753 point_file = tempfile.mktemp(".csv") 756 754 fd = open(point_file,'w') 757 fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n") 755 fd.write("x,y, elevation, stage \n\ 756 1.0, 1.0,2.,4 \n\ 757 1.0, 3.0,4,8 \n\ 758 3.0,1.0,4.,8 \n") 758 759 fd.close() 759 760 … … 808 809 export_mesh_file(mesh_file,mesh_dic) 809 810 810 # create a n .xyafile811 point_file = tempfile.mktemp(". xya")811 # create a points .csv file 812 point_file = tempfile.mktemp(".csv") 812 813 fd = open(point_file,'w') 813 fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n") 814 fd.write("x,y, elevation, stage \n\ 815 1.0, 1.0,2.,4 \n\ 816 1.0, 3.0,4,8 \n\ 817 3.0,1.0,4.,8 \n") 814 818 fd.close() 815 819 816 820 mesh_output_file = tempfile.mktemp(".tsh") 817 821 fit_to_mesh_file(mesh_file, … … 825 829 [5.0, 10.0], 826 830 [5.0,10.0]] 827 assert allclose(mesh_dic['vertex_attributes'],ans)828 829 self.failUnless(mesh_dic['vertex_attribute_titles'] ==830 ['elevation','stage'],831 'test_fit_to_mesh_file failed')832 833 #clean up834 os.remove(mesh_file)835 os.remove(point_file)836 os.remove(mesh_output_file)837 838 def test_fit_to_mesh_file4(self):839 from load_mesh.loadASCII import import_mesh_file, \840 export_mesh_file841 import tempfile842 import os843 844 # create a .tsh file, no user outline845 mesh_dic = {}846 mesh_dic['vertices'] = [[0.76, 0.76],847 [0.76, 5.76],848 [5.76, 0.76]]849 mesh_dic['triangles'] = [[0, 2, 1]]850 mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]851 mesh_dic['triangle_tags'] = ['']852 mesh_dic['vertex_attributes'] = [[], [], []]853 mesh_dic['vertiex_attribute_titles'] = []854 mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]855 mesh_dic['segment_tags'] = ['external',856 'external',857 'external']858 mesh_dic['geo_reference'] = Geo_reference(56,-0.76,-0.76)859 mesh_file = tempfile.mktemp(".tsh")860 export_mesh_file(mesh_file,mesh_dic)861 862 geo_ref = Geo_reference(56,-200,-400)863 # create an .xya file864 point_file = tempfile.mktemp(".xya")865 fd = open(point_file,'w')866 fd.write("elevation, stage \n 201.0, 401.0,2.,4 \n 201.0, 403.0,4,8 \n 203.0, 401.0,4.,8 \n")867 geo_ref.write_ASCII(fd)868 fd.close()869 870 mesh_output_file = tempfile.mktemp(".tsh")871 fit_to_mesh_file(mesh_file,872 point_file,873 mesh_output_file,874 alpha = 0.0)875 # load in the .tsh file we just wrote876 mesh_dic = import_mesh_file(mesh_output_file)877 #print "mesh_dic",mesh_dic878 ans =[[0.0, 0.0],879 [5.0, 10.0],880 [5.0, 10.0]]881 831 assert allclose(mesh_dic['vertex_attributes'],ans) 882 832 … … 913 863 export_mesh_file(mesh_file,mesh_dic) 914 864 915 # create a n .xyafile916 point_file = tempfile.mktemp(". xya")865 # create a points .csv file 866 point_file = tempfile.mktemp(".csv") 917 867 fd = open(point_file,'w') 918 fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n") 868 fd.write("x,y,elevation, stage \n\ 869 1.0, 1.0,2.,4 \n\ 870 1.0, 3.0,4,8 \n\ 871 3.0,1.0,4.,8 \n") 919 872 fd.close() 920 873 … … 959 912 export_mesh_file(mesh_file,mesh_dic) 960 913 961 # create a n .xyafile962 point_file = tempfile.mktemp(". xya")914 # create a bad points .csv file 915 point_file = tempfile.mktemp(".csv") 963 916 fd = open(point_file,'w') 964 fd.write("elevation stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n") 917 fd.write("x,y,elevation stage \n\ 918 1.0, 1.0,2.,4 \n\ 919 1.0, 3.0,4,8 \n\ 920 3.0,1.0,4.,8 \n") 965 921 fd.close() 966 922 … … 969 925 fit_to_mesh_file(mesh_file, point_file, 970 926 mesh_output_file, display_errors = False) 971 except IOError:927 except SyntaxError: 972 928 pass 973 929 else: … … 990 946 fd.close() 991 947 992 # create a n .xyafile993 point_file = tempfile.mktemp(". xya")948 # create a points .csv file 949 point_file = tempfile.mktemp(".csv") 994 950 fd = open(point_file,'w') 995 fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n") 951 fd.write("x,y,elevation, stage \n\ 952 1.0, 1.0,2.,4 \n\ 953 1.0, 3.0,4,8 \n\ 954 3.0,1.0,4.,8 \n") 996 955 fd.close() 997 956 … … 1027 986 export_mesh_file(mesh_file,mesh_dic) 1028 987 1029 # create an .xya file 1030 point_file = tempfile.mktemp(".xya") 988 989 # create a points .csv file 990 point_file = tempfile.mktemp(".csv") 1031 991 fd = open(point_file,'w') 1032 fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n") 992 fd.write("x,y,elevation, stage \n\ 993 1.0, 1.0,2.,4 \n\ 994 1.0, 3.0,4,8 \n\ 995 3.0,1.0,4.,8 \n") 1033 996 fd.close() 1034 997 -
anuga_core/source/anuga/geospatial_data/geospatial_data.py
r4642 r4659 102 102 2.7, 2.4, 5.2, 0.3 103 103 104 The first two columns are always assumed to be x, y104 The first two columns have to be x, y or lat, long 105 105 coordinates. 106 106 107 An issue with the xya format is that the attribute column order108 is not be controlled. The info is stored in a dictionary and it's109 written in an order dependent on the hash order110 107 111 108 The format for a Points dictionary is: … … 177 174 self.set_default_attribute_name(default_attribute_name) 178 175 179 180 #Why?181 #assert self.attributes is None or isinstance(self.attributes, DictType)182 #This is a hassle when blocking, so I've removed it.183 184 185 176 if verbose is True: 186 177 if file_name is not None: … … 515 506 516 507 def import_points_file(self, file_name, delimiter=None, verbose=False): 517 """ load an .txt, .csv or . xya or .pts file518 Note: will throw an IOError if it can't load the file.508 """ load an .txt, .csv or .pts file 509 Note: will throw an IOError/SyntaxError if it can't load the file. 519 510 Catch these! 520 511 … … 527 518 528 519 attributes = {} 529 if file_name[-4:]== ".xya": 530 # Maybe not phase-out, so we can load in geo-ref info 531 #msg = 'Text file format is moving to comma seperated .txt files.' 532 #warn(msg, DeprecationWarning) 533 try: 534 if delimiter == None: 535 try: 536 fd = open(file_name) 537 data_points, attributes, geo_reference =\ 538 _read_xya_file(fd, ',') 539 except TitleError: 540 fd.close() 541 fd = open(file_name) 542 data_points, attributes, geo_reference =\ 543 _read_xya_file(fd, ' ') 544 else: 545 fd = open(file_name) 546 data_points, attributes, geo_reference =\ 547 _read_xya_file(fd, delimiter) 548 fd.close() 549 except (IndexError,ValueError,SyntaxError): 550 fd.close() 551 msg = 'Could not open file %s ' %file_name 552 msg += 'Check the file location.' 553 raise IOError, msg 554 except IOError, e: 555 fd.close() 556 # Catch this to add an error message 557 msg = 'Could not open file or incorrect file format %s:%s'\ 558 %(file_name, e) 559 raise IOError, msg 560 561 elif file_name[-4:]== ".pts": 520 if file_name[-4:]== ".pts": 562 521 try: 563 522 data_points, attributes, geo_reference =\ … … 614 573 """ 615 574 616 if absolute is False and file_name[-4:] == ".xya": 617 msg = 'The text file values must be absolute. ' 618 msg += 'Text file format is moving to comma seperated .txt files.' 619 warn(msg, DeprecationWarning) 620 621 if (file_name[-4:] == ".xya"): 622 msg = '.xya format is deprecated. Please use .txt.' 623 warn(msg, DeprecationWarning) 624 if absolute is True: 625 geo_ref = deepcopy(self.geo_reference) 626 geo_ref.xllcorner = 0 627 geo_ref.yllcorner = 0 628 _write_xya_file(file_name, 629 self.get_data_points(absolute=True), 630 self.get_all_attributes(), 631 geo_ref) 632 else: 633 _write_xya_file(file_name, 634 self.get_data_points(absolute=False), 635 self.get_all_attributes(), 636 self.get_geo_reference()) 637 638 elif (file_name[-4:] == ".pts"): 575 if (file_name[-4:] == ".pts"): 639 576 if absolute is True: 640 577 geo_ref = deepcopy(self.geo_reference) … … 699 636 700 637 def split(self, factor=0.5, verbose=False): 701 """Returns two geospatial_data object, first is the size of the 'factor' 702 smaller the original and the second is the remainder. The two new 703 object are disjoin set of each other. 638 """Returns two 639 geospatial_data object, first is the size of the 'factor' 640 smaller the original and the second is the remainder. The two 641 new object are disjoin set of each other. 704 642 705 643 Points of the two new object have selected RANDOMLY. 706 644 AND if factor is a decimal it will round (2.25 to 2 and 2.5 to 3) 707 645 708 This method create two lists of indices which are passed into get_sample. 709 The lists are created using random numbers, and they are unique sets eg. 710 total_list(1,2,3,4,5,6,7,8,9) random_list(1,3,6,7,9) and remainder_list(0,2,4,5,8) 646 This method create two lists of indices which are passed into 647 get_sample. The lists are created using random numbers, and 648 they are unique sets eg. total_list(1,2,3,4,5,6,7,8,9) 649 random_list(1,3,6,7,9) and remainder_list(0,2,4,5,8) 711 650 712 651 Input - the factor which to split the object, if 0.1 then 10% of the 713 object will be returned652 together object will be returned 714 653 715 654 Output - two geospatial_data objects that are disjoint sets of the … … 747 686 k=1 748 687 remainder_list = total_list[:] 749 #pops array index (random_num) from remainder_list (which starts as the 688 #pops array index (random_num) from remainder_list 689 # (which starts as the 750 690 #total_list and appends to random_list 751 691 random_num_len = len(random_num) … … 765 705 test_total.extend(remainder_list) 766 706 test_total.sort() 767 msg = 'The two random lists made from the original list when added together'\768 ' DO NOT equal the original list'707 msg = 'The two random lists made from the original list when added '\ 708 'together DO NOT equal the original list' 769 709 assert (total_list==test_total),msg 770 710 … … 791 731 self.max_read_lines = MAX_READ_LINES 792 732 793 794 if self.file_name[-4:] == ".xya": 795 # FIXME (Ole): shouldn't the xya format be replaced by txt/csv? 796 # Currently both file formats are used. 797 798 # FIXME (Ole): This has to go - it caused Ted Rigby to waste 799 # time trying to read in his data in xya format with an 800 # inevitable memory error appearing. 801 802 #let's just read it all 803 msg = 'The xya format is deprecated. Use csv or pts.' 804 warn(msg, DeprecationWarning) 805 806 elif self.file_name[-4:] == ".pts": 733 if self.file_name[-4:] == ".pts": 807 734 808 735 # see if the file is there. Throw a QUIET IO error if it isn't … … 846 773 """ read a block, instanciate a new geospatial and return it""" 847 774 848 if self.file_name[-4:]== ".xya" : 849 # FIXME (Ole): shouldn't the xya format be replaced by txt/csv? 850 # Currently both file formats are used. 851 852 if not hasattr(self,'finished_reading') or \ 853 self.finished_reading is False: 854 #let's just read it all 855 geo = Geospatial_data(self.file_name) 856 self.finished_reading = True 857 else: 858 raise StopIteration 859 self.finished_reading = False 860 861 elif self.file_name[-4:] == ".pts": 775 if self.file_name[-4:] == ".pts": 862 776 if self.start_row == self.last_row: 863 777 # read the end of the file last iteration … … 1206 1120 return pointlist, attributes 1207 1121 1208 1209 def _read_xya_file(fd, delimiter):1210 points = []1211 pointattributes = []1212 title = fd.readline()1213 att_names = clean_line(title,delimiter)1214 att_dict = {}1215 line = fd.readline()1216 numbers = clean_line(line,delimiter)1217 1218 while len(numbers) > 1 and line[0] <> '#':1219 if numbers != []:1220 try:1221 x = float(numbers[0])1222 y = float(numbers[1])1223 points.append([x,y])1224 numbers.pop(0)1225 numbers.pop(0)1226 if len(att_names) != len(numbers):1227 fd.close()1228 # It might not be a problem with the title1229 #raise TitleAmountError1230 raise IOError1231 for i,num in enumerate(numbers):1232 num.strip()1233 if num != '\n' and num != '':1234 #attributes.append(float(num))1235 att_dict.setdefault(att_names[i],[]).append(float(num))1236 except ValueError:1237 raise SyntaxError1238 line = fd.readline()1239 numbers = clean_line(line,delimiter)1240 1241 if line == '':1242 geo_reference = None1243 else:1244 geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)1245 1246 1247 pointlist = array(points).astype(Float)1248 for key in att_dict.keys():1249 att_dict[key] = array(att_dict[key]).astype(Float)1250 1251 return pointlist, att_dict, geo_reference1252 1253 1122 def _write_pts_file(file_name, 1254 1123 write_data_points, … … 1303 1172 outfile.close() 1304 1173 1305 1306 1307 def _write_xya_file(file_name,1308 write_data_points,1309 write_attributes=None,1310 write_geo_reference=None,1311 delimiter=','):1312 """1313 export a file, file_name, with the xya format1314 1315 """1316 points = write_data_points1317 pointattributes = write_attributes1318 1319 fd = open(file_name,'w')1320 titlelist = ""1321 if pointattributes is not None:1322 for title in pointattributes.keys():1323 titlelist = titlelist + title + delimiter1324 titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter1325 fd.write(titlelist+"\n")1326 1327 #<vertex #> <x> <y> [attributes]1328 for i, vert in enumerate( points):1329 1330 1331 if pointattributes is not None:1332 attlist = ","1333 for att in pointattributes.keys():1334 attlist = attlist + str(pointattributes[att][i])+ delimiter1335 attlist = attlist[0:-len(delimiter)] # remove the last delimiter1336 attlist.strip()1337 else:1338 attlist = ''1339 1340 fd.write(str(vert[0]) + delimiter +1341 str(vert[1]) + attlist + "\n")1342 1343 if write_geo_reference is not None:1344 write_geo_reference = ensure_geo_reference(write_geo_reference)1345 write_geo_reference.write_ASCII(fd)1346 fd.close()1347 1348 1349 1174 def _write_csv_file(file_name, 1350 1175 write_data_points, -
anuga_core/source/anuga/geospatial_data/test_geospatial_data.py
r4641 r4659 13 13 from anuga.coordinate_transforms.redfearn import degminsec2decimal_degrees 14 14 from anuga.utilities.anuga_exceptions import ANUGAError 15 # Ignore these warnings, since we still want to test .xya code.16 import warnings17 warnings.filterwarnings(action = 'ignore',18 message='.xya format is deprecated. Please use .txt.',19 category=DeprecationWarning)20 21 warnings.filterwarnings(action = 'ignore',22 message='The text file values must be ab',23 category=DeprecationWarning)24 25 warnings.filterwarnings(action = 'ignore',26 message='Text file format is moving to comma se',27 category=DeprecationWarning)28 29 warnings.filterwarnings(action = 'ignore',30 message='Specifying delimiters will be removed.',31 category=DeprecationWarning)32 15 33 16 class Test_Geospatial_data(unittest.TestCase): 34 17 def setUp(self): 35 import warnings 36 warnings.filterwarnings(action = 'ignore', 37 message='.xya format is deprecated. Please use .txt.', 38 category=DeprecationWarning) 39 40 warnings.filterwarnings(action = 'ignore', 41 message='The text file values must be ab', 42 category=DeprecationWarning) 43 44 warnings.filterwarnings(action = 'ignore', 45 message='Text file format is moving to comma se', 46 category=DeprecationWarning) 47 48 warnings.filterwarnings(action = 'ignore', 49 message='Specifying delimiters will be removed.', 50 category=DeprecationWarning) 18 pass 51 19 52 20 def tearDown(self): … … 772 740 773 741 774 def test_create_from_xya_file(self):775 """Check that object can be created from a points file (.pts and .xya)776 """777 778 points = [[1.0, 2.1], [3.0, 5.3], [5.0, 6.1], [6.0, 3.3]]779 attributes = [2, 4, 5, 76]780 '''781 # Use old pointsdict format782 pointsdict = {'pointlist': points,783 'attributelist': {'att1': attributes,784 'att2': array(attributes) + 1}}785 '''786 att_dict = {'att1': attributes,787 'att2': array(attributes) +1}788 789 # Create points as an xya file790 FN = 'test_points.xya'791 G1 = Geospatial_data(points, att_dict)792 G1.export_points_file(FN)793 # G1.export_points_file(ofile)794 795 #Create object from file796 G = Geospatial_data(file_name = FN)797 798 assert allclose(G.get_data_points(), points)799 assert allclose(G.get_attributes('att1'), attributes)800 assert allclose(G.get_attributes('att2'), array(attributes) + 1)801 802 os.remove(FN)803 804 def test_create_from_xya_file1(self):805 """806 Check that object can be created from an Absolute xya file807 """808 809 points = [[1.0, 2.1], [3.0, 5.3], [5.0, 6.1], [6.0, 3.3]]810 attributes = [2, 4, 5, 76]811 812 att_dict = {'att1': attributes,813 'att2': array(attributes) +1}814 815 geo_ref = Geo_reference(56, 10, 5)816 817 # Create points as an xya file818 FN = 'test_points.xya'819 G1 = Geospatial_data(points, att_dict, geo_ref)820 821 G1.export_points_file(FN, absolute=True)822 823 #Create object from file824 G = Geospatial_data(file_name = FN)825 826 assert allclose(G.get_data_points(absolute=True),827 G1.get_data_points(absolute=True))828 assert allclose(G.get_attributes('att1'), attributes)829 assert allclose(G.get_attributes('att2'), array(attributes) + 1)830 831 os.remove(FN)832 833 742 def test_load_csv(self): 834 743 … … 844 753 file.close() 845 754 #print fileName 846 results = Geospatial_data(fileName , delimiter=',')755 results = Geospatial_data(fileName) 847 756 os.remove(fileName) 848 757 # print 'data', results.get_data_points() … … 853 762 assert allclose(results.get_attributes(attribute_name='speed'), 854 763 [0.0, 10.0, 40.0]) 855 856 857 def test_load_xya(self):858 """ test_load_xya(self):859 comma delimited860 """861 fileName = tempfile.mktemp(".xya")862 file = open(fileName,"w")863 file.write("elevation , speed \n\864 1.0, 0.0, 10.0, 0.0\n\865 0.0, 1.0, 0.0, 10.0\n\866 1.0, 0.0, 10.4, 40.0\n")867 file.close()868 results = Geospatial_data(fileName, delimiter=',')869 os.remove(fileName)870 # print 'data', results.get_data_points()871 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])872 assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])873 assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])874 875 def test_loadxya2(self):876 """ test_loadxya2(self):877 space delimited878 """879 import os880 881 fileName = tempfile.mktemp(".xya")882 file = open(fileName,"w")883 file.write(" elevation speed \n\884 1.0 0.0 10.0 0.0\n\885 0.0 1.0 0.0 10.0\n\886 1.0 0.0 10.4 40.0\n")887 file.close()888 889 results = Geospatial_data(fileName, delimiter=' ')890 891 os.remove(fileName)892 893 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])894 assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])895 assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])896 897 def test_loadxya3(self):898 """899 test_loadxya3(self):900 space delimited901 """902 import os903 904 fileName = tempfile.mktemp(".xya")905 file = open(fileName,"w")906 file.write(" elevation speed \n\907 1.0 0.0 10.0 0.0\n\908 0.0 1.0 0.0 10.0\n\909 1.0 0.0 10.4 40.0\n\910 #geocrap\n\911 56\n\912 56.6\n\913 3\n")914 file.close()915 916 results = Geospatial_data(fileName, delimiter=' ')917 918 os.remove(fileName)919 assert allclose(results.get_data_points(absolute=False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])920 assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])921 assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])922 923 def BADtest_loadxya4(self):924 """925 comma delimited926 """927 fileName = tempfile.mktemp(".xya")928 file = open(fileName,"w")929 file.write("elevation , speed \n\930 1.0, 0.0, splat, 0.0\n\931 0.0, 1.0, 0.0, 10.0\n\932 1.0, 0.0, 10.4, 40.0\n")933 file.close()934 results = Geospatial_data(fileName, delimiter=',')935 os.remove(fileName)936 # print 'data', results.get_data_points()937 assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])938 assert allclose(results.get_attributes(attribute_name='elevation'), ["splat", 0.0, 10.4])939 assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])940 941 def test_read_write_points_file_bad2(self):942 att_dict = {}943 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])944 att_dict['elevation'] = array([10.0, 0.0, 10.4])945 att_dict['brightness'] = array([10.0, 0.0, 10.4])946 geo_reference=Geo_reference(56,1.9,1.9)947 948 G = Geospatial_data(pointlist, att_dict, geo_reference)949 950 try:951 G.export_points_file("_???/yeah.xya")952 953 except IOError:954 pass955 else:956 msg = 'bad points file extension did not raise error!'957 raise msg958 # self.failUnless(0 == 1,959 # 'bad points file extension did not raise error!')960 961 def test_loadxy_bad(self):962 import os963 964 fileName = tempfile.mktemp(".xya")965 file = open(fileName,"w")966 file.write(" elevation \n\967 1.0 0.0 10.0 0.0\n\968 0.0 1.0 0.0 10.0\n\969 1.0 0.0 10.4 40.0\n")970 file.close()971 #print fileName972 try:973 results = Geospatial_data(fileName, delimiter=' ')974 except IOError:975 pass976 else:977 msg = 'bad xya file did not raise error!'978 raise msg979 # self.failUnless(0 == 1,980 # 'bad xya file did not raise error!')981 os.remove(fileName)982 983 def test_loadxy_bad2(self):984 import os985 986 fileName = tempfile.mktemp(".xya")987 file = open(fileName,"w")988 file.write("elevation\n\989 1.0 0.0 10.0 \n\990 0.0 1.0\n\991 1.0 \n")992 file.close()993 #print fileName994 try:995 results = Geospatial_data(fileName, delimiter=' ')996 except IOError:997 pass998 else:999 msg = 'bad xya file did not raise error!'1000 raise msg1001 os.remove(fileName)1002 1003 def test_loadxy_bad3(self):1004 """ test_loadxy_bad3(self):1005 specifying wrong delimiter1006 """1007 import os1008 1009 fileName = tempfile.mktemp(".xya")1010 file = open(fileName,"w")1011 file.write(" elevation , speed \n\1012 1.0, 0.0, 10.0, 0.0\n\1013 0.0, 1.0, 0.0, 10.0\n\1014 1.0, 0.0, 10.4, 40.0\n")1015 file.close()1016 try:1017 results = Geospatial_data(fileName, delimiter=' ')1018 except IOError:1019 pass1020 else:1021 msg = 'bad xya file did not raise error!'1022 raise msg1023 os.remove(fileName)1024 1025 def test_loadxy_bad4(self):1026 """ test_loadxy_bad4(self):1027 specifying wrong delimiter1028 """1029 import os1030 fileName = tempfile.mktemp(".xya")1031 file = open(fileName,"w")1032 file.write(" elevation speed \n\1033 1.0 0.0 10.0 0.0\n\1034 0.0 1.0 0.0 10.0\n\1035 1.0 0.0 10.4 40.0\n\1036 #geocrap\n\1037 56\n\1038 56.6\n\1039 3\n"1040 )1041 file.close()1042 try:1043 results = Geospatial_data(fileName, delimiter=',')1044 except IOError:1045 pass1046 else:1047 msg = 'bad xya file did not raise error!'1048 raise msg1049 1050 os.remove(fileName)1051 1052 def test_loadxy_bad5(self):1053 """ test_loadxy_bad5(self):1054 specifying wrong delimiter1055 """1056 import os1057 1058 fileName = tempfile.mktemp(".xya")1059 file = open(fileName,"w")1060 file.write(" elevation speed \n\1061 1.0 0.0 10.0 0.0\n\1062 0.0 1.0 0.0 10.0\n\1063 1.0 0.0 10.4 40.0\n\1064 #geocrap\n\1065 crap")1066 file.close()1067 try:1068 # dict = import_points_file(fileName,delimiter=' ')1069 # results = Geospatial_data()1070 results = Geospatial_data(fileName, delimiter=' ', verbose=False)1071 # results.import_points_file(fileName, delimiter=' ')1072 except IOError:1073 pass1074 else:1075 msg = 'bad xya file did not raise error!'1076 raise msg1077 1078 # self.failUnless(0 ==1,1079 # 'bad xya file did not raise error!')1080 os.remove(fileName)1081 1082 def test_loadxy_bad_no_file_xya(self):1083 import os1084 1085 fileName = tempfile.mktemp(".xya")1086 try:1087 results = Geospatial_data(fileName, delimiter=' ')1088 except IOError:1089 pass1090 else:1091 msg = 'imaginary file did not raise error!'1092 raise msg1093 1094 # except IOError:1095 # pass1096 # else:1097 # self.failUnless(0 == 1,1098 # 'imaginary file did not raise error!')1099 764 1100 765 … … 1404 1069 1405 1070 1406 def test_export_xya_file(self): 1407 # depreciated since it's testing using geo refs in a text file 1408 # dict = {} 1409 att_dict = {} 1410 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1411 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 1412 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 1413 # dict['attributelist'] = att_dict 1414 geo_reference=Geo_reference(56,1.9,1.9) 1415 1416 1417 fileName = tempfile.mktemp(".xya") 1418 G = Geospatial_data(pointlist, att_dict, geo_reference) 1419 G.export_points_file(fileName, False) 1420 1421 # dict2 = import_points_file(fileName) 1422 results = Geospatial_data(file_name = fileName) 1423 #print "fileName",fileName 1424 os.remove(fileName) 1425 1426 assert allclose(results.get_data_points(absolute=False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1427 assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4]) 1428 answer = [10.0, 0.0, 10.4] 1429 assert allclose(results.get_attributes(attribute_name='brightness'), answer) 1430 #print "dict2['geo_reference']",dict2['geo_reference'] 1431 self.failUnless(results.get_geo_reference() == geo_reference, 1432 'test_writepts failed. Test geo_reference') 1433 1434 def test_export_xya_file2(self): 1435 """ test_export_xya_file2(self): 1436 test absolute xya file 1437 """ 1438 att_dict = {} 1439 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1440 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 1441 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 1442 1443 fileName = tempfile.mktemp(".xya") 1444 G = Geospatial_data(pointlist, att_dict) 1445 G.export_points_file(fileName) 1446 results = Geospatial_data(file_name = fileName) 1447 # dict2 = import_points_file(fileName) 1448 os.remove(fileName) 1449 1450 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1451 assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4]) 1452 answer = [10.0, 0.0, 10.4] 1453 assert allclose(results.get_attributes('brightness'), answer) 1454 1455 def test_export_xya_file3(self): 1456 """ test_export_xya_file3(self): 1457 test absolute xya file with geo_ref 1458 """ 1459 att_dict = {} 1460 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1461 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 1462 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 1463 geo_reference=Geo_reference(56,1.9,1.9) 1464 1465 1466 fileName = tempfile.mktemp(".xya") 1467 G = Geospatial_data(pointlist, att_dict, geo_reference) 1468 1469 G.export_points_file(fileName, absolute=True) 1470 1471 results = Geospatial_data(file_name = fileName) 1472 os.remove(fileName) 1473 1474 assert allclose(results.get_data_points(), 1475 [[2.9, 1.9],[1.9, 2.9],[2.9, 1.9]]) 1476 assert allclose(results.get_attributes(attribute_name='elevation'), 1477 [10.0, 0.0, 10.4]) 1478 answer = [10.0, 0.0, 10.4] 1479 assert allclose(results.get_attributes(attribute_name='brightness'), answer) 1480 self.failUnless(results.get_geo_reference() == geo_reference, 1481 'test_writepts failed. Test geo_reference') 1482 1483 1484 1071 1485 1072 def test_new_export_pts_file(self): 1486 1073 att_dict = {} … … 1585 1172 'test_writepts failed. Test geo_reference') 1586 1173 1587 def test_write_ xya_attributes(self):1588 #test_write xya: Test that storage of x,y,attributes works1174 def test_write_csv_attributes(self): 1175 #test_write : Test that storage of x,y,attributes works 1589 1176 1590 1177 att_dict = {} … … 1593 1180 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 1594 1181 geo_reference=Geo_reference(56,0,0) 1595 # Test xya format 1596 fileName = tempfile.mktemp(".xya") 1597 G = Geospatial_data(pointlist, att_dict, geo_reference) 1598 G.export_points_file(fileName) 1599 results = Geospatial_data(file_name=fileName) 1600 os.remove(fileName) 1601 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1602 assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4]) 1603 answer = [10.0, 0.0, 10.4] 1604 assert allclose(results.get_attributes('brightness'), answer) 1605 self.failUnless(geo_reference == geo_reference, 1606 'test_writepts failed. Test geo_reference') 1607 1608 def test_write_csv_attributes(self): 1609 #test_write : Test that storage of x,y,attributes works 1610 1611 att_dict = {} 1612 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1613 att_dict['elevation'] = array([10.0, 0.0, 10.4]) 1614 att_dict['brightness'] = array([10.0, 0.0, 10.4]) 1615 geo_reference=Geo_reference(56,0,0) 1616 # Test xya format 1182 # Test txt format 1617 1183 fileName = tempfile.mktemp(".txt") 1618 1184 G = Geospatial_data(pointlist, att_dict, geo_reference) … … 1666 1232 'test_writepts failed. Test geo_reference') 1667 1233 1668 def test_write_xya_no_attributes(self): 1669 #test_write xya _no_attributes: Test that storage of x,y alone works 1234 1235 def test_write_csv_no_attributes(self): 1236 #test_write txt _no_attributes: Test that storage of x,y alone works 1670 1237 1671 1238 att_dict = {} 1672 1239 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1673 1240 geo_reference=Geo_reference(56,0,0) 1674 # Test xya format 1675 fileName = tempfile.mktemp(".xya") 1676 G = Geospatial_data(pointlist, None, geo_reference) 1677 G.export_points_file(fileName) 1678 results = Geospatial_data(file_name=fileName) 1679 os.remove(fileName) 1680 assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1681 self.failUnless(geo_reference == geo_reference, 1682 'test_writepts failed. Test geo_reference') 1683 1684 1685 def test_write_csv_no_attributes(self): 1686 #test_write xya _no_attributes: Test that storage of x,y alone works 1687 1688 att_dict = {} 1689 pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 1690 geo_reference=Geo_reference(56,0,0) 1691 # Test xya format 1241 # Test format 1692 1242 fileName = tempfile.mktemp(".txt") 1693 1243 G = Geospatial_data(pointlist, None, geo_reference) … … 1812 1362 def test_add_(self): 1813 1363 '''test_add_(self): 1814 adds an xyaand pts files, reads the files and adds them1364 adds an txt and pts files, reads the files and adds them 1815 1365 checking results are correct 1816 1366 ''' … … 1831 1381 G2 = Geospatial_data(pointlist2, att_dict2, geo_reference2) 1832 1382 1833 fileName1 = tempfile.mktemp(". xya")1383 fileName1 = tempfile.mktemp(".txt") 1834 1384 fileName2 = tempfile.mktemp(".pts") 1835 1385 … … 1925 1475 assert allclose(new_points, ab_points) 1926 1476 1927 1928 fileName = tempfile.mktemp(".xya")1929 file = open(fileName,"w")1930 file.write(" elevation speed \n\1931 1.0 0.0 10.0 0.0\n\1932 0.0 1.0 0.0 10.0\n\1933 1.0 0.0 10.4 40.0\n\1934 #geocrap\n\1935 56\n\1936 10\n\1937 20\n")1938 file.close()1939 1940 ab_points = ensure_absolute(fileName)1941 actual = [[11, 20.0],[10.0, 21.0],[11.0, 20.0]]1942 assert allclose(ab_points, actual)1943 os.remove(fileName)1944 1477 1945 1478 … … 2010 1543 import os 2011 1544 2012 fileName = tempfile.mktemp(". xya")1545 fileName = tempfile.mktemp(".csv") 2013 1546 file = open(fileName,"w") 2014 file.write(" elevation speed \n\ 2015 1.0 0.0 10.0 0.0\n\ 2016 0.0 1.0 0.0 10.0\n\ 2017 1.0 0.0 10.4 40.0\n\ 2018 #geocrap\n\ 2019 56\n\ 2020 56.6\n\ 2021 3\n") 1547 file.write("x,y, elevation , speed \n\ 1548 1.0, 0.0, 10.0, 0.0\n\ 1549 0.0, 1.0, 0.0, 10.0\n\ 1550 1.0, 0.0, 10.4, 40.0\n") 2022 1551 file.close() 2023 1552 2024 1553 results = Geospatial_data(fileName) 2025 assert allclose(results.get_data_points(absolute= False), \1554 assert allclose(results.get_data_points(absolute=True), \ 2026 1555 [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]) 2027 1556 assert allclose(results.get_attributes(attribute_name='elevation'), \ … … 2032 1561 os.remove(fileName) 2033 1562 2034 def test_delimiter(self):2035 2036 try:2037 G = Geospatial_data(delimiter=',')2038 # results = Geospatial_data(file_name = fileName)2039 # dict = import_points_file(fileName)2040 except ValueError:2041 pass2042 else:2043 msg = 'Instance with No fileName but has a delimiter\2044 did not raise error!'2045 raise msg2046 1563 2047 1564 def test_no_constructors(self): … … 2057 1574 raise msg 2058 1575 2059 def test_check_geo_reference(self):2060 """2061 checks geo reference details are OK. eg can be called '#geo reference'2062 if not throws a clear error message2063 """2064 import os2065 fileName = tempfile.mktemp(".xya")2066 file = open(fileName,"w")2067 file.write(" elevation \n\2068 1.0 0.0 10.0\n\2069 0.0 1.0 0.0\n\2070 1.0 0.0 10.4\n\2071 #ge oreference\n\2072 56\n\2073 1.1\n\2074 1.0\n")2075 2076 file.close()2077 results = Geospatial_data(fileName)2078 assert allclose(results.get_geo_reference().get_xllcorner(), 1.1)2079 assert allclose(results.get_geo_reference().get_yllcorner(), 1.0)2080 2081 os.remove(fileName)2082 2083 fileName = tempfile.mktemp(".xya")2084 file = open(fileName,"w")2085 file.write(" elevation \n\2086 1.0 0.0 10.0\n\2087 0.0 1.0 0.0\n\2088 1.0 0.0 10.4\n")2089 2090 file.close()2091 results = Geospatial_data(fileName)2092 2093 os.remove(fileName)2094 2095 def test_check_geo_reference1(self):2096 """2097 checks geo reference details are OK. eg can be called '#geo reference'2098 if not throws a clear error message2099 """2100 import os2101 fileName = tempfile.mktemp(".xya")2102 file = open(fileName,"w")2103 file.write(" elevation \n\2104 1.0 0.0 10.0\n\2105 0.0 1.0 0.0\n\2106 1.0 0.0 10.4\n\2107 #geo t t\n\2108 56\n\2109 1.1\n"2110 )2111 file.close()2112 2113 try:2114 results = Geospatial_data(fileName, delimiter = " ")2115 except IOError:2116 pass2117 else:2118 msg = 'Geo reference data format is incorrect'2119 raise msg2120 2121 2122 os.remove(fileName)2123 2124 2125 1576 def test_load_csv_lat_long(self): 2126 1577 """ … … 2134 1585 150.0,-34,459.126000, 10\n") 2135 1586 file.close() 2136 results = Geospatial_data(fileName , delimiter=',')1587 results = Geospatial_data(fileName) 2137 1588 os.remove(fileName) 2138 1589 points = results.get_data_points() … … 2155 1606 -34,150.0,459.126000\n") 2156 1607 file.close() 2157 results = Geospatial_data(fileName , delimiter=',')1608 results = Geospatial_data(fileName) 2158 1609 os.remove(fileName) 2159 1610 points = results.get_data_points() … … 2177 1628 file.close() 2178 1629 try: 2179 results = Geospatial_data(fileName , delimiter=',')1630 results = Geospatial_data(fileName) 2180 1631 except ANUGAError: 2181 1632 pass -
anuga_core/source/anuga/shallow_water/test_data_manager.py
r4631 r4659 600 600 601 601 def test_dem2pts_bounding_box_v2(self): 602 """Test conversion from dem in ascii format to native NetCDF xyaformat602 """Test conversion from dem in ascii format to native NetCDF format 603 603 """ 604 604 … … 714 714 715 715 def test_dem2pts_bounding_box_removeNullvalues_v2(self): 716 """Test conversion from dem in ascii format to native NetCDF xyaformat716 """Test conversion from dem in ascii format to native NetCDF format 717 717 """ 718 718 … … 844 844 845 845 def test_dem2pts_bounding_box_removeNullvalues_v3(self): 846 """Test conversion from dem in ascii format to native NetCDF xyaformat846 """Test conversion from dem in ascii format to native NetCDF format 847 847 Check missing values on clipping boundary 848 848 """ … … 4020 4020 4021 4021 def test_read_asc(self): 4022 """Test conversion from dem in ascii format to native NetCDF xyaformat4022 """Test conversion from dem in ascii format to native NetCDF format 4023 4023 """ 4024 4024 … … 5025 5025 ########## testing nbed class ################## 5026 5026 def test_exposure_csv_loading(self): 5027 file_name = tempfile.mktemp(". xya")5027 file_name = tempfile.mktemp(".csv") 5028 5028 file = open(file_name,"w") 5029 5029 file.write("LATITUDE, LONGITUDE ,sound , speed \n\ … … 5045 5045 5046 5046 5047 file_name = tempfile.mktemp(". xya")5047 file_name = tempfile.mktemp(".txt") 5048 5048 file = open(file_name,"w") 5049 5049 file.write("LATITUDE, LONGITUDE ,sound , speed \n\ … … 5068 5068 if sys.platform == 'win32': 5069 5069 file_name = tempfile.gettempdir() + \ 5070 "test_exposure_csv_loading_title_check_list. xya"5070 "test_exposure_csv_loading_title_check_list.csv" 5071 5071 else: 5072 file_name = tempfile.mktemp(". xya")5072 file_name = tempfile.mktemp(".csv") 5073 5073 file = open(file_name,"w") 5074 5074 file.write("LATITUDE, LONGITUDE ,sound , speed \n\ … … 5088 5088 5089 5089 def test_exposure_csv_cmp(self): 5090 file_name = tempfile.mktemp(". xya")5090 file_name = tempfile.mktemp(".csv") 5091 5091 file = open(file_name,"w") 5092 5092 file.write("LATITUDE, LONGITUDE ,sound , speed \n\ … … 5106 5106 'FAILED!') 5107 5107 5108 file_name = tempfile.mktemp(". xya")5108 file_name = tempfile.mktemp(".csv") 5109 5109 file = open(file_name,"w") 5110 5110 # Note, this has less spaces in the title, … … 5121 5121 'FAILED!') 5122 5122 5123 file_name = tempfile.mktemp(". xya")5123 file_name = tempfile.mktemp(".csv") 5124 5124 file = open(file_name,"w") 5125 5125 # Note, 40 changed to 44 . … … 5136 5136 'FAILED!') 5137 5137 5138 file_name = tempfile.mktemp(". xya")5138 file_name = tempfile.mktemp(".csv") 5139 5139 file = open(file_name,"w") 5140 5140 # Note, the first two columns are swapped. … … 5153 5153 5154 5154 5155 file_name = tempfile.mktemp(". xya")5155 file_name = tempfile.mktemp(".csv") 5156 5156 file = open(file_name,"w") 5157 5157 file.write("LATITUDE, LONGITUDE ,sound , speed \n\ … … 5162 5162 e1 = Exposure_csv(file_name) 5163 5163 5164 file_name2 = tempfile.mktemp(". xya")5164 file_name2 = tempfile.mktemp(".csv") 5165 5165 e1.save(file_name = file_name2) 5166 5166 e2 = Exposure_csv(file_name2) … … 5172 5172 5173 5173 def test_exposure_csv_get_location(self): 5174 file_name = tempfile.mktemp(". xya")5174 file_name = tempfile.mktemp(".csv") 5175 5175 file = open(file_name,"w") 5176 5176 file.write("LONGITUDE , LATITUDE, sound , speed \n\ … … 5194 5194 5195 5195 def test_exposure_csv_set_column_get_column(self): 5196 file_name = tempfile.mktemp(". xya")5196 file_name = tempfile.mktemp(".csv") 5197 5197 file = open(file_name,"w") 5198 5198 file.write("LONGITUDE , LATITUDE, sound , speed \n\ … … 5210 5210 ' Error!') 5211 5211 5212 file_name2 = tempfile.mktemp(". xya")5212 file_name2 = tempfile.mktemp(".csv") 5213 5213 e1.save(file_name = file_name2) 5214 5214 e2 = Exposure_csv(file_name2) … … 5219 5219 5220 5220 def test_exposure_csv_set_column_get_column_error_checking(self): 5221 file_name = tempfile.mktemp(". xya")5221 file_name = tempfile.mktemp(".csv") 5222 5222 file = open(file_name,"w") 5223 5223 file.write("LONGITUDE , LATITUDE, sound , speed \n\ … … 5259 5259 else: 5260 5260 self.failUnless(0 ==1, 'Error not thrown error!') 5261 file_name2 = tempfile.mktemp(". xya")5261 file_name2 = tempfile.mktemp(".csv") 5262 5262 e1.save(file_name = file_name2) 5263 5263 e2 = Exposure_csv(file_name2) … … 5278 5278 5279 5279 5280 file_name = tempfile.mktemp(". xya")5280 file_name = tempfile.mktemp(".csv") 5281 5281 file = open(file_name,"w") 5282 5282 file.write("x, y ,sound , speed \n\
Note: See TracChangeset
for help on using the changeset viewer.