Ignore:
Timestamp:
Jan 12, 2007, 3:54:31 PM (18 years ago)
Author:
duncan
Message:

working on fit_to_mesh when .pts and .xya files are passed in.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/fit_interpolate/test_fit.py

    r4165 r4174  
    66from math import sqrt
    77import tempfile
    8 
     8import os
    99from Numeric import zeros, take, compress, Float, Int, dot, concatenate, \
    1010     ArrayType, allclose, array
     
    239239        #print "answer\n",answer
    240240        assert allclose(f, answer)
     241        os.remove(fileName)
    241242
    242243    def test_fit_to_mesh(self):
     
    277278        #print "answer\n",answer
    278279        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\
     3000.0, 2.0 , 2.\n\
     3011.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\
     3430.0, 2.0 , 2.\n\
     3441.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)
    279364       
    280365    def test_fit_to_mesh_2_atts(self):
     
    316401        #print "answer\n",answer
    317402        assert allclose(f, answer)
     403        os.remove(fileName)
    318404       
    319405    def test_fit_and_interpolation(self):
     
    605691
    606692
    607     def obsolete_test_fit_to_mesh_file(self):
    608         from load_mesh.loadASCII import import_mesh_file, \
    609              export_mesh_file
    610         import tempfile
    611         import os
    612 
    613         # create a .tsh file, no user outline
    614         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 file
    631         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 wrote
    642         mesh_dic = import_mesh_file(mesh_output_file)
    643         #print "mesh_dic",mesh_dic
    644         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 up
    654         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_file
    662         import tempfile
    663         import os
    664 
    665         # create a .tsh file, no user outline
    666         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 file
    684         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 wrote
    695         mesh_dic = import_mesh_file(mesh_output_file)
    696         #print "mesh_dic",mesh_dic
    697         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 up
    707         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_file
    714         import tempfile
    715         import os
    716 
    717         # create a .tsh file, no user outline
    718         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 file
    737         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 wrote
    749         mesh_dic = import_mesh_file(mesh_output_file)
    750         #print "mesh_dic",mesh_dic
    751         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 up
    761         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_file
    768         import tempfile
    769         import os
    770 
    771         # create a .tsh file, no user outline
    772         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 file
    789         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 wrote
    800         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 up
    812         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_file
    818         import tempfile
    819         import os
    820 
    821         # create a .tsh file, no user outline
    822         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 file
    835         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             pass
    846         else:
    847             #self.failUnless(0 ==1,  'Bad file did not raise error!')
    848             raise 'Bad file did not raise error!'
    849            
    850         #clean up
    851         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_file
    856         import tempfile
    857         import os
    858 
    859         # create a .tsh file, no user outline
    860         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 file
    866         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             pass
    877         else:
    878             raise 'Bad file did not raise error!'
    879            
    880         #clean up
    881         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_file
    886         import tempfile
    887         import os
    888 
    889         # create a .tsh file, no user outline
    890         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 file
    903         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             pass
    916         else:
    917             raise 'Bad file did not raise error!'
    918        
    919         #clean up
    920         os.remove(mesh_file)
    921         os.remove(point_file)
    922 
    923 
    924693    def Not_yet_test_smooth_att_to_mesh_with_excess_verts(self):
    925694
Note: See TracChangeset for help on using the changeset viewer.