Changeset 4174 for anuga_core/source
- Timestamp:
- Jan 12, 2007, 3:54:31 PM (18 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/fit_interpolate/fit.py
r4165 r4174 30 30 31 31 from anuga.caching import cache 32 from anuga.geospatial_data.geospatial_data import Geospatial_data, ensure_absolute 32 from anuga.geospatial_data.geospatial_data import Geospatial_data, \ 33 ensure_absolute 33 34 from anuga.fit_interpolate.general_fit_interpolate import FitInterpolate 34 35 from anuga.utilities.sparse import Sparse, Sparse_CSR … … 499 500 return vertex_attributes 500 501 501 502 def obsolete_fit_to_mesh_file(mesh_file, point_file, mesh_output_file,503 alpha=DEFAULT_ALPHA, verbose= False,504 display_errors = True):505 """506 Given a mesh file (tsh) and a point attribute file (xya), fit507 point attributes to the mesh and write a mesh file with the508 results.509 510 511 If data_origin is not None it is assumed to be512 a 3-tuple with geo referenced513 UTM coordinates (zone, easting, northing)514 515 NOTE: Throws IOErrors, for a variety of file problems.516 517 """518 #OBSOLETE519 #Problems with using blocking and knowing the attribute title..520 521 522 # Question523 # should data_origin and mesh_origin be passed in?524 # No they should be in the data structure525 #526 #Should the origin of the mesh be changed using this function?527 # That is overloading this function. Have it as a seperate528 # method, at least initially.529 530 from load_mesh.loadASCII import import_mesh_file, \531 import_points_file, export_mesh_file, \532 concatinate_attributelist533 534 # FIXME: Use geospatial instead of import_points_file535 try:536 mesh_dict = import_mesh_file(mesh_file)537 except IOError,e:538 if display_errors:539 print "Could not load bad file. ", e540 raise IOError #Re-raise exception541 542 vertex_coordinates = mesh_dict['vertices']543 triangles = mesh_dict['triangles']544 if type(mesh_dict['vertex_attributes']) == ArrayType:545 old_point_attributes = mesh_dict['vertex_attributes'].tolist()546 else:547 old_point_attributes = mesh_dict['vertex_attributes']548 549 if type(mesh_dict['vertex_attribute_titles']) == ArrayType:550 old_title_list = mesh_dict['vertex_attribute_titles'].tolist()551 else:552 old_title_list = mesh_dict['vertex_attribute_titles']553 554 if verbose: print 'tsh file %s loaded' %mesh_file555 556 # load in the .pts file557 try: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)562 except IOError,e:563 if display_errors:564 print "Could not load bad file. ", e565 raise IOError #Re-raise exception566 567 #point_coordinates = point_dict['pointlist']568 #get_all_attributes569 #title_list,point_attributes = concatinate_attributelist(point_dict['attributelist'])570 title_list,point_attributes = concatinate_attributelist( \571 geospatial.get_all_attributes())572 573 574 if mesh_dict.has_key('geo_reference') and not mesh_dict['geo_reference'] is None:575 mesh_origin = mesh_dict['geo_reference'].get_origin()576 else:577 mesh_origin = (56, 0, 0) #FIXME(DSG-DSG)578 579 if verbose: print "points file loaded"580 if verbose: print "fitting to mesh"581 f = fit_to_mesh(vertex_coordinates,582 triangles,583 point_file,584 alpha = alpha,585 verbose = verbose,586 data_origin = data_origin,587 mesh_origin = mesh_origin)588 if verbose: print "finished fitting to mesh"589 590 # convert array to list of lists591 new_point_attributes = f.tolist()592 #FIXME have this overwrite attributes with the same title - DSG593 #Put the newer attributes last594 if old_title_list <> []:595 old_title_list.extend(title_list)596 #FIXME can this be done a faster way? - DSG597 for i in range(len(old_point_attributes)):598 old_point_attributes[i].extend(new_point_attributes[i])599 mesh_dict['vertex_attributes'] = old_point_attributes600 mesh_dict['vertex_attribute_titles'] = old_title_list601 else:602 mesh_dict['vertex_attributes'] = new_point_attributes603 mesh_dict['vertex_attribute_titles'] = title_list604 605 if verbose: print "exporting to file ", mesh_output_file606 607 try:608 export_mesh_file(mesh_output_file, mesh_dict)609 except IOError,e:610 if display_errors:611 print "Could not write file. ", e612 raise IOError613 614 615 502 def _fit(*args, **kwargs): 616 503 """Private function for use with caching. Reason is that classes -
anuga_core/source/anuga/fit_interpolate/test_fit.py
r4165 r4174 6 6 from math import sqrt 7 7 import tempfile 8 8 import os 9 9 from Numeric import zeros, take, compress, Float, Int, dot, concatenate, \ 10 10 ArrayType, allclose, array … … 239 239 #print "answer\n",answer 240 240 assert allclose(f, answer) 241 os.remove(fileName) 241 242 242 243 def test_fit_to_mesh(self): … … 277 278 #print "answer\n",answer 278 279 assert allclose(f, answer) 280 281 os.remove(fileName) 282 283 def test_fit_to_mesh_pts(self): 284 a = [-1.0, 0.0] 285 b = [3.0, 4.0] 286 c = [4.0,1.0] 287 d = [-3.0, 2.0] #3 288 e = [-1.0,-2.0] 289 f = [1.0, -2.0] #5 290 291 vertices = [a, b, c, d,e,f] 292 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc 293 294 295 fileName = tempfile.mktemp(".xya") 296 file = open(fileName,"w") 297 file.write(" elevation \n\ 298 -2.0, 2.0, 0.\n\ 299 -1.0, 1.0, 0.\n\ 300 0.0, 2.0 , 2.\n\ 301 1.0, 1.0 , 2.\n\ 302 2.0, 1.0 ,3. \n\ 303 0.0, 0.0 , 0.\n\ 304 1.0, 0.0 , 1.\n\ 305 0.0, -1.0, -1.\n\ 306 -0.2, -0.5, -0.7\n\ 307 -0.9, -1.5, -2.4\n\ 308 0.5, -1.9, -1.4\n\ 309 3.0, 1.0 , 4.\n") 310 file.close() 311 312 geo = Geospatial_data(fileName) 313 fileName_pts = tempfile.mktemp(".pts") 314 geo.export_points_file(fileName_pts) 315 f = fit_to_mesh(vertices, triangles,fileName_pts, 316 alpha=0.0, max_read_lines=2) 317 #use_cache=True, verbose=True) 318 answer = linear_function(vertices) 319 #print "f\n",f 320 #print "answer\n",answer 321 assert allclose(f, answer) 322 os.remove(fileName) 323 os.remove(fileName_pts) 324 325 def test_fit_to_mesh(self): 326 327 a = [-1.0, 0.0] 328 b = [3.0, 4.0] 329 c = [4.0,1.0] 330 d = [-3.0, 2.0] #3 331 e = [-1.0,-2.0] 332 f = [1.0, -2.0] #5 333 334 vertices = [a, b, c, d,e,f] 335 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc 336 337 338 fileName = tempfile.mktemp(".ddd") 339 file = open(fileName,"w") 340 file.write(" x,y, elevation \n\ 341 -2.0, 2.0, 0.\n\ 342 -1.0, 1.0, 0.\n\ 343 0.0, 2.0 , 2.\n\ 344 1.0, 1.0 , 2.\n\ 345 2.0, 1.0 ,3. \n\ 346 0.0, 0.0 , 0.\n\ 347 1.0, 0.0 , 1.\n\ 348 0.0, -1.0, -1.\n\ 349 -0.2, -0.5, -0.7\n\ 350 -0.9, -1.5, -2.4\n\ 351 0.5, -1.9, -1.4\n\ 352 3.0, 1.0 , 4.\n") 353 file.close() 354 355 f = fit_to_mesh(vertices, triangles,fileName, 356 alpha=0.0, max_read_lines=2) 357 #use_cache=True, verbose=True) 358 answer = linear_function(vertices) 359 #print "f\n",f 360 #print "answer\n",answer 361 assert allclose(f, answer) 362 363 os.remove(fileName) 279 364 280 365 def test_fit_to_mesh_2_atts(self): … … 316 401 #print "answer\n",answer 317 402 assert allclose(f, answer) 403 os.remove(fileName) 318 404 319 405 def test_fit_and_interpolation(self): … … 605 691 606 692 607 def obsolete_test_fit_to_mesh_file(self):608 from load_mesh.loadASCII import import_mesh_file, \609 export_mesh_file610 import tempfile611 import os612 613 # create a .tsh file, no user outline614 mesh_dic = {}615 mesh_dic['vertices'] = [[0.0, 0.0],616 [0.0, 5.0],617 [5.0, 0.0]]618 mesh_dic['triangles'] = [[0, 2, 1]]619 mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]620 mesh_dic['triangle_tags'] = ['']621 mesh_dic['vertex_attributes'] = [[], [], []]622 mesh_dic['vertiex_attribute_titles'] = []623 mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]624 mesh_dic['segment_tags'] = ['external',625 'external',626 'external']627 mesh_file = tempfile.mktemp(".tsh")628 export_mesh_file(mesh_file,mesh_dic)629 630 # create an .xya file631 point_file = tempfile.mktemp(".xya")632 fd = open(point_file,'w')633 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")634 fd.close()635 636 mesh_output_file = tempfile.mktemp(".tsh")637 fit_to_mesh_file(mesh_file,638 point_file,639 mesh_output_file,640 alpha = 0.0)641 # load in the .tsh file we just wrote642 mesh_dic = import_mesh_file(mesh_output_file)643 #print "mesh_dic",mesh_dic644 ans =[[0.0, 0.0],645 [5.0, 10.0],646 [5.0,10.0]]647 assert allclose(mesh_dic['vertex_attributes'],ans)648 649 self.failUnless(mesh_dic['vertex_attribute_titles'] ==650 ['elevation','stage'],651 'test_fit_to_mesh_file failed')652 653 #clean up654 os.remove(mesh_file)655 os.remove(point_file)656 os.remove(mesh_output_file)657 658 659 def obsolete_test_fit_to_mesh_file3(self):660 from load_mesh.loadASCII import import_mesh_file, \661 export_mesh_file662 import tempfile663 import os664 665 # create a .tsh file, no user outline666 mesh_dic = {}667 mesh_dic['vertices'] = [[0.76, 0.76],668 [0.76, 5.76],669 [5.76, 0.76]]670 mesh_dic['triangles'] = [[0, 2, 1]]671 mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]672 mesh_dic['triangle_tags'] = ['']673 mesh_dic['vertex_attributes'] = [[], [], []]674 mesh_dic['vertiex_attribute_titles'] = []675 mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]676 mesh_dic['segment_tags'] = ['external',677 'external',678 'external']679 mesh_dic['geo_reference'] = Geo_reference(56,-0.76,-0.76)680 mesh_file = tempfile.mktemp(".tsh")681 export_mesh_file(mesh_file,mesh_dic)682 683 # create an .xya file684 point_file = tempfile.mktemp(".xya")685 fd = open(point_file,'w')686 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")687 fd.close()688 689 mesh_output_file = tempfile.mktemp(".tsh")690 fit_to_mesh_file(mesh_file,691 point_file,692 mesh_output_file,693 alpha = 0.0)694 # load in the .tsh file we just wrote695 mesh_dic = import_mesh_file(mesh_output_file)696 #print "mesh_dic",mesh_dic697 ans =[[0.0, 0.0],698 [5.0, 10.0],699 [5.0,10.0]]700 assert allclose(mesh_dic['vertex_attributes'],ans)701 702 self.failUnless(mesh_dic['vertex_attribute_titles'] ==703 ['elevation','stage'],704 'test_fit_to_mesh_file failed')705 706 #clean up707 os.remove(mesh_file)708 os.remove(point_file)709 os.remove(mesh_output_file)710 711 def obsolete_test_fit_to_mesh_file4(self):712 from load_mesh.loadASCII import import_mesh_file, \713 export_mesh_file714 import tempfile715 import os716 717 # create a .tsh file, no user outline718 mesh_dic = {}719 mesh_dic['vertices'] = [[0.76, 0.76],720 [0.76, 5.76],721 [5.76, 0.76]]722 mesh_dic['triangles'] = [[0, 2, 1]]723 mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]724 mesh_dic['triangle_tags'] = ['']725 mesh_dic['vertex_attributes'] = [[], [], []]726 mesh_dic['vertiex_attribute_titles'] = []727 mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]728 mesh_dic['segment_tags'] = ['external',729 'external',730 'external']731 mesh_dic['geo_reference'] = Geo_reference(56,-0.76,-0.76)732 mesh_file = tempfile.mktemp(".tsh")733 export_mesh_file(mesh_file,mesh_dic)734 735 geo_ref = Geo_reference(56,-200,-400)736 # create an .xya file737 point_file = tempfile.mktemp(".xya")738 fd = open(point_file,'w')739 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")740 geo_ref.write_ASCII(fd)741 fd.close()742 743 mesh_output_file = tempfile.mktemp(".tsh")744 fit_to_mesh_file(mesh_file,745 point_file,746 mesh_output_file,747 alpha = 0.0)748 # load in the .tsh file we just wrote749 mesh_dic = import_mesh_file(mesh_output_file)750 #print "mesh_dic",mesh_dic751 ans =[[0.0, 0.0],752 [5.0, 10.0],753 [5.0, 10.0]]754 assert allclose(mesh_dic['vertex_attributes'],ans)755 756 self.failUnless(mesh_dic['vertex_attribute_titles'] ==757 ['elevation','stage'],758 'test_fit_to_mesh_file failed')759 760 #clean up761 os.remove(mesh_file)762 os.remove(point_file)763 os.remove(mesh_output_file)764 765 def obsolete_test_fit_to_mesh_fileII(self):766 from load_mesh.loadASCII import import_mesh_file, \767 export_mesh_file768 import tempfile769 import os770 771 # create a .tsh file, no user outline772 mesh_dic = {}773 mesh_dic['vertices'] = [[0.0, 0.0],774 [0.0, 5.0],775 [5.0, 0.0]]776 mesh_dic['triangles'] = [[0, 2, 1]]777 mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]778 mesh_dic['triangle_tags'] = ['']779 mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]780 mesh_dic['vertex_attribute_titles'] = ['density', 'temp']781 mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]782 mesh_dic['segment_tags'] = ['external',783 'external',784 'external']785 mesh_file = tempfile.mktemp(".tsh")786 export_mesh_file(mesh_file,mesh_dic)787 788 # create an .xya file789 point_file = tempfile.mktemp(".xya")790 fd = open(point_file,'w')791 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")792 fd.close()793 794 mesh_output_file = "new_triangle.tsh"795 fit_to_mesh_file(mesh_file,796 point_file,797 mesh_output_file,798 alpha = 0.0)799 # load in the .tsh file we just wrote800 mesh_dic = import_mesh_file(mesh_output_file)801 802 assert allclose(mesh_dic['vertex_attributes'],803 [[1.0, 2.0,0.0, 0.0],804 [1.0, 2.0,5.0, 10.0],805 [1.0, 2.0,5.0,10.0]])806 807 self.failUnless(mesh_dic['vertex_attribute_titles'] ==808 ['density', 'temp','elevation','stage'],809 'test_fit_to_mesh_file failed')810 811 #clean up812 os.remove(mesh_file)813 os.remove(mesh_output_file)814 os.remove(point_file)815 816 def obsolete_test_fit_to_mesh_file_errors(self):817 from load_mesh.loadASCII import import_mesh_file, export_mesh_file818 import tempfile819 import os820 821 # create a .tsh file, no user outline822 mesh_dic = {}823 mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]824 mesh_dic['triangles'] = [[0, 2, 1]]825 mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]826 mesh_dic['triangle_tags'] = ['']827 mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]828 mesh_dic['vertex_attribute_titles'] = ['density', 'temp']829 mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]830 mesh_dic['segment_tags'] = ['external', 'external','external']831 mesh_file = tempfile.mktemp(".tsh")832 export_mesh_file(mesh_file,mesh_dic)833 834 # create an .xya file835 point_file = tempfile.mktemp(".xya")836 fd = open(point_file,'w')837 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")838 fd.close()839 840 mesh_output_file = "new_triangle.tsh"841 try:842 fit_to_mesh_file(mesh_file, point_file,843 mesh_output_file, display_errors = False)844 except IOError:845 pass846 else:847 #self.failUnless(0 ==1, 'Bad file did not raise error!')848 raise 'Bad file did not raise error!'849 850 #clean up851 os.remove(mesh_file)852 os.remove(point_file)853 854 def obsolete_test_fit_to_mesh_file_errorsII(self):855 from load_mesh.loadASCII import import_mesh_file, export_mesh_file856 import tempfile857 import os858 859 # create a .tsh file, no user outline860 mesh_file = tempfile.mktemp(".tsh")861 fd = open(mesh_file,'w')862 fd.write("unit testing a bad .tsh file \n")863 fd.close()864 865 # create an .xya file866 point_file = tempfile.mktemp(".xya")867 fd = open(point_file,'w')868 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")869 fd.close()870 871 mesh_output_file = "new_triangle.tsh"872 try:873 fit_to_mesh_file(mesh_file, point_file,874 mesh_output_file, display_errors = False)875 except IOError:876 pass877 else:878 raise 'Bad file did not raise error!'879 880 #clean up881 os.remove(mesh_file)882 os.remove(point_file)883 884 def obsolete_test_fit_to_mesh_file_errorsIII(self):885 from load_mesh.loadASCII import import_mesh_file, export_mesh_file886 import tempfile887 import os888 889 # create a .tsh file, no user outline890 mesh_dic = {}891 mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]892 mesh_dic['triangles'] = [[0, 2, 1]]893 mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]894 mesh_dic['triangle_tags'] = ['']895 mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]896 mesh_dic['vertex_attribute_titles'] = ['density', 'temp']897 mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]898 mesh_dic['segment_tags'] = ['external', 'external','external']899 mesh_file = tempfile.mktemp(".tsh")900 export_mesh_file(mesh_file,mesh_dic)901 902 # create an .xya file903 point_file = tempfile.mktemp(".xya")904 fd = open(point_file,'w')905 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")906 fd.close()907 908 #This a deliberately illegal filename to invoke the error.909 mesh_output_file = ".../\z\z:ya.tsh"910 911 try:912 fit_to_mesh_file(mesh_file, point_file,913 mesh_output_file, display_errors = False)914 except IOError:915 pass916 else:917 raise 'Bad file did not raise error!'918 919 #clean up920 os.remove(mesh_file)921 os.remove(point_file)922 923 924 693 def Not_yet_test_smooth_att_to_mesh_with_excess_verts(self): 925 694 -
anuga_core/source/anuga/geospatial_data/geospatial_data.py
r4167 r4174 7 7 from types import DictType 8 8 from warnings import warn 9 import sys10 9 11 10 from Numeric import concatenate, array, Float, shape, reshape, ravel, take, \ … … 705 704 706 705 #FIXME - give warning if the file format is .xya 707 file_pointer = open(self.file_name) 708 self.header, self.file_pointer = _read_csv_file_header(file_pointer) 709 710 if self.max_read_lines is None: 711 self.max_read_lines = MAX_READ_LINES 706 if self.file_name[-4:]== ".xya" or self.file_name[-4:]== ".pts": 707 #let's just read it all 708 pass 709 else: 710 file_pointer = open(self.file_name) 711 self.header, self.file_pointer = \ 712 _read_csv_file_header(file_pointer) 713 714 if self.max_read_lines is None: 715 self.max_read_lines = MAX_READ_LINES 712 716 return self 713 717 714 718 def next(self): 715 719 # read a block, instanciate a new geospatial and return it 716 try: 717 pointlist, att_dict, self.file_pointer = _read_csv_file_blocking( \ 718 self.file_pointer, 719 self.header[:], 720 max_read_lines=self.max_read_lines) 721 except StopIteration: 722 self.file_pointer.close() 723 raise StopIteration 724 return Geospatial_data(pointlist, att_dict) 720 if self.file_name[-4:]== ".xya" or self.file_name[-4:]== ".pts": 721 if not hasattr(self,'finished_reading') or \ 722 self.finished_reading is False: 723 #let's just read it all 724 geo = Geospatial_data(self.file_name) 725 self.finished_reading = True 726 else: 727 raise StopIteration 728 self.finished_reading = False 729 730 else: 731 try: 732 pointlist, att_dict, self.file_pointer = \ 733 _read_csv_file_blocking( self.file_pointer, 734 self.header[:], 735 max_read_lines=self.max_read_lines) 736 geo = Geospatial_data(pointlist, att_dict) 737 except StopIteration: 738 self.file_pointer.close() 739 raise StopIteration 740 return geo 725 741 726 742 def _read_pts_file(file_name, verbose=False): … … 788 804 """ 789 805 806 #from anuga.shallow_water.data_manager import Exposure_csv 807 #csv =Exposure_csv(file_name) 808 790 809 file_pointer = open(file_name) 791 810 header, file_pointer = _read_csv_file_header(file_pointer) … … 796 815 file_pointer, 797 816 header, 798 max_read_lines= sys.maxint) #This might not be high enough...817 max_read_lines=MAX_READ_LINES) #FIXME: must be highest int 799 818 except StopIteration: 800 819 break
Note: See TracChangeset
for help on using the changeset viewer.