def test_fit_to_mesh_file2domain(self): from anuga.load_mesh.loadASCII import import_mesh_file, \ export_mesh_file import tempfile import os # create a .tsh file, no user outline mesh_dic = {} mesh_dic['vertices'] = [[0.0, 0.0], [0.0, 5.0], [5.0, 0.0]] mesh_dic['triangles'] = [[0, 2, 1]] mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]] mesh_dic['triangle_tags'] = [''] mesh_dic['vertex_attributes'] = [[], [], []] mesh_dic['vertiex_attribute_titles'] = [] mesh_dic['triangle_neighbors'] = [[-1, -1, -1]] mesh_dic['segment_tags'] = ['external', 'external', 'external'] mesh_file = tempfile.mktemp(".tsh") export_mesh_file(mesh_file,mesh_dic) # create a points .csv file point_file = tempfile.mktemp(".csv") fd = open(point_file,'w') fd.write("x,y, elevation, stage \n\ 1.0, 1.0,2.,4 \n\ 1.0, 3.0,4,8 \n\ 3.0,1.0,4.,8 \n") fd.close() mesh_output_file = tempfile.mktemp(".tsh") fit_to_mesh_file(mesh_file, point_file, mesh_output_file, alpha = 0.0) # load in the .tsh file we just wrote mesh_dic = import_mesh_file(mesh_output_file) #print "mesh_dic",mesh_dic ans =[[0.0, 0.0], [5.0, 10.0], [5.0,10.0]] assert num.allclose(mesh_dic['vertex_attributes'],ans) self.assertTrue(mesh_dic['vertex_attribute_titles'] == ['elevation','stage'], 'test_fit_to_mesh_file failed') domain = Domain(mesh_output_file, use_cache=True, verbose=False) answer = [0., 5., 5.] assert num.allclose(domain.quantities['elevation'].vertex_values, answer) #clean up os.remove(mesh_file) os.remove(point_file) os.remove(mesh_output_file)
def test_fit_to_mesh_fileII(self): from anuga.load_mesh.loadASCII import import_mesh_file, \ export_mesh_file import tempfile import os # create a .tsh file, no user outline mesh_dic = {} mesh_dic['vertices'] = [[0.0, 0.0], [0.0, 5.0], [5.0, 0.0]] mesh_dic['triangles'] = [[0, 2, 1]] mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]] mesh_dic['triangle_tags'] = [''] mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]] mesh_dic['vertex_attribute_titles'] = ['density', 'temp'] mesh_dic['triangle_neighbors'] = [[-1, -1, -1]] mesh_dic['segment_tags'] = ['external', 'external', 'external'] mesh_file = tempfile.mktemp(".tsh") export_mesh_file(mesh_file,mesh_dic) # create a points .csv file point_file = tempfile.mktemp(".csv") fd = open(point_file,'w') fd.write("x,y,elevation, stage \n\ 1.0, 1.0,2.,4 \n\ 1.0, 3.0,4,8 \n\ 3.0,1.0,4.,8 \n") fd.close() mesh_output_file = "new_triangle.tsh" fit_to_mesh_file(mesh_file, point_file, mesh_output_file, alpha = 0.0) # load in the .tsh file we just wrote mesh_dic = import_mesh_file(mesh_output_file) assert num.allclose(mesh_dic['vertex_attributes'], [[1.0, 2.0,0.0, 0.0], [1.0, 2.0,5.0, 10.0], [1.0, 2.0,5.0,10.0]]) self.assertTrue(mesh_dic['vertex_attribute_titles'] == ['density', 'temp','elevation','stage'], 'test_fit_to_mesh_file failed') #clean up os.remove(mesh_file) os.remove(mesh_output_file) os.remove(point_file)
def test_fit_to_mesh_file_errorsIII(self): from anuga.load_mesh.loadASCII import import_mesh_file, export_mesh_file import tempfile import os # create a .tsh file, no user outline mesh_dic = {} mesh_dic['vertices'] = [[0.0, 0.0], [0.0, 5.0], [5.0, 0.0]] mesh_dic['triangles'] = [[0, 2, 1]] mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]] mesh_dic['triangle_tags'] = [''] mesh_dic['vertex_attributes'] = [[1, 2], [1, 2], [1, 2]] mesh_dic['vertex_attribute_titles'] = ['density', 'temp'] mesh_dic['triangle_neighbors'] = [[-1, -1, -1]] mesh_dic['segment_tags'] = ['external', 'external', 'external'] mesh_file = tempfile.mktemp(".tsh") export_mesh_file(mesh_file, mesh_dic) # create a points .csv file point_file = tempfile.mktemp(".csv") fd = open(point_file, 'w') fd.write("x,y,elevation, stage \n\ 1.0, 1.0,2.,4 \n\ 1.0, 3.0,4,8 \n\ 3.0,1.0,4.,8 \n") fd.close() #This a deliberately illegal filename to invoke the error. mesh_output_file = ".../\z\z:ya.tsh" try: fit_to_mesh_file(mesh_file, point_file, mesh_output_file, display_errors=False) except IOError: pass else: raise Exception('Bad file did not raise error!') #clean up os.remove(mesh_file) os.remove(point_file)
def test_fit_to_mesh_file_errorsIII(self): from anuga.load_mesh.loadASCII import import_mesh_file, export_mesh_file import tempfile import os # create a .tsh file, no user outline mesh_dic = {} mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]] mesh_dic['triangles'] = [[0, 2, 1]] mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]] mesh_dic['triangle_tags'] = [''] mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]] mesh_dic['vertex_attribute_titles'] = ['density', 'temp'] mesh_dic['triangle_neighbors'] = [[-1, -1, -1]] mesh_dic['segment_tags'] = ['external', 'external','external'] mesh_file = tempfile.mktemp(".tsh") export_mesh_file(mesh_file,mesh_dic) # create a points .csv file point_file = tempfile.mktemp(".csv") fd = open(point_file,'w') fd.write("x,y,elevation, stage \n\ 1.0, 1.0,2.,4 \n\ 1.0, 3.0,4,8 \n\ 3.0,1.0,4.,8 \n") fd.close() #This a deliberately illegal filename to invoke the error. mesh_output_file = ".../\z\z:ya.tsh" try: fit_to_mesh_file(mesh_file, point_file, mesh_output_file, display_errors = False) except IOError: pass else: raise Exception('Bad file did not raise error!') #clean up os.remove(mesh_file) os.remove(point_file)
def test_fit_to_mesh_file_errors(self): from anuga.load_mesh.loadASCII import import_mesh_file, export_mesh_file import tempfile import os # create a .tsh file, no user outline mesh_dic = {} mesh_dic['vertices'] = [[0.0, 0.0], [0.0, 5.0], [5.0, 0.0]] mesh_dic['triangles'] = [[0, 2, 1]] mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]] mesh_dic['triangle_tags'] = [''] mesh_dic['vertex_attributes'] = [[1, 2], [1, 2], [1, 2]] mesh_dic['vertex_attribute_titles'] = ['density', 'temp'] mesh_dic['triangle_neighbors'] = [[-1, -1, -1]] mesh_dic['segment_tags'] = ['external', 'external', 'external'] mesh_file = tempfile.mktemp(".tsh") export_mesh_file(mesh_file, mesh_dic) # create a bad points .csv file point_file = tempfile.mktemp(".csv") fd = open(point_file, 'w') fd.write("x,y,elevation stage \n\ 1.0, 1.0,2.,4 \n\ 1.0, 3.0,4,8 \n\ 3.0,1.0,4.,8 \n") fd.close() mesh_output_file = "new_triangle.tsh" try: fit_to_mesh_file(mesh_file, point_file, mesh_output_file, display_errors=False) except SyntaxError: pass else: #self.assertTrue(0 ==1, 'Bad file did not raise error!') raise Exception('Bad file did not raise error!') #clean up os.remove(mesh_file) os.remove(point_file)
def test_fit_to_mesh_file_errors(self): from anuga.load_mesh.loadASCII import import_mesh_file, export_mesh_file import tempfile import os # create a .tsh file, no user outline mesh_dic = {} mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]] mesh_dic['triangles'] = [[0, 2, 1]] mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]] mesh_dic['triangle_tags'] = [''] mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]] mesh_dic['vertex_attribute_titles'] = ['density', 'temp'] mesh_dic['triangle_neighbors'] = [[-1, -1, -1]] mesh_dic['segment_tags'] = ['external', 'external','external'] mesh_file = tempfile.mktemp(".tsh") export_mesh_file(mesh_file,mesh_dic) # create a bad points .csv file point_file = tempfile.mktemp(".csv") fd = open(point_file,'w') fd.write("x,y,elevation stage \n\ 1.0, 1.0,2.,4 \n\ 1.0, 3.0,4,8 \n\ 3.0,1.0,4.,8 \n") fd.close() mesh_output_file = "new_triangle.tsh" try: fit_to_mesh_file(mesh_file, point_file, mesh_output_file, display_errors = False) except SyntaxError: pass else: #self.assertTrue(0 ==1, 'Bad file did not raise error!') raise Exception('Bad file did not raise error!') #clean up os.remove(mesh_file) os.remove(point_file)
verbose=verbose, data_origin=None, mesh_origin=mesh_origin) if verbose: log.critical("finished fitting to mesh") # convert array to list of lists new_point_attributes = f.tolist() #FIXME have this overwrite attributes with the same title - DSG #Put the newer attributes last if old_title_list != []: old_title_list.extend(title_list) #FIXME can this be done a faster way? - DSG for i in range(len(old_point_attributes)): old_point_attributes[i].extend(new_point_attributes[i]) mesh_dict['vertex_attributes'] = old_point_attributes mesh_dict['vertex_attribute_titles'] = old_title_list else: mesh_dict['vertex_attributes'] = new_point_attributes mesh_dict['vertex_attribute_titles'] = title_list if verbose: log.critical("exporting to file %s" % mesh_output_file) try: export_mesh_file(mesh_output_file, mesh_dict) except IOError, e: if display_errors: log.critical("Could not write file %s", str(e)) raise IOError
#inflate() skew() #fix() fixable = [] inflatable = [] removeable = [] corrupt = [] wrongs = check(mesh) counter = counter + 1 print 'Skewable: ', len(fixable) print 'Inflatable: ', len(inflatable) print 'Removeable: ', len(removeable) print 'Corrupt: ', len(corrupt) print 'Total: ', wrongs if counter > 1: print 'Too many loops (5), quitting...' break #remove all remaining invalid triangles removeable = corrupt + fixable + inflatable + removeable remove(removeable) wrongs = check(mesh) print 'Last check: ', wrongs trianglenr = len(mesh['triangles']) for i in range(trianglenr): a, b, c = getvertices(i) area = calcarea(a, b, c) fo1.write(geojson(a, b, c, area, i)) fo1.write(']}') fo1.close() loadASCII.export_mesh_file('output_skewed.msh', mesh) print 'done'
verbose = verbose, data_origin = None, mesh_origin = mesh_origin) if verbose: log.critical("finished fitting to mesh") # convert array to list of lists new_point_attributes = f.tolist() #FIXME have this overwrite attributes with the same title - DSG #Put the newer attributes last if old_title_list != []: old_title_list.extend(title_list) #FIXME can this be done a faster way? - DSG for i in range(len(old_point_attributes)): old_point_attributes[i].extend(new_point_attributes[i]) mesh_dict['vertex_attributes'] = old_point_attributes mesh_dict['vertex_attribute_titles'] = old_title_list else: mesh_dict['vertex_attributes'] = new_point_attributes mesh_dict['vertex_attribute_titles'] = title_list if verbose: log.critical("exporting to file %s" % mesh_output_file) try: export_mesh_file(mesh_output_file, mesh_dict) except IOError, e: if display_errors: log.critical("Could not write file %s", str(e)) raise IOError
def fit_to_mesh_file(mesh_file, point_file, mesh_output_file, alpha=DEFAULT_ALPHA, verbose=False, expand_search=False, precrop=False, display_errors=True): """ Given a mesh file (tsh) and a point attribute file, fit point attributes to the mesh and write a mesh file with the results. Note: the points file needs titles. If you want anuga to use the tsh file, make sure the title is elevation. NOTE: Throws IOErrors, for a variety of file problems. """ from anuga.load_mesh.loadASCII import import_mesh_file, \ export_mesh_file, concatinate_attributelist try: mesh_dict = import_mesh_file(mesh_file) except IOError as e: if display_errors: log.critical("Could not load bad file: %s" % str(e)) raise IOError # Could not load bad mesh file. vertex_coordinates = mesh_dict['vertices'] triangles = mesh_dict['triangles'] if isinstance(mesh_dict['vertex_attributes'], num.ndarray): old_point_attributes = mesh_dict['vertex_attributes'].tolist() else: old_point_attributes = mesh_dict['vertex_attributes'] if isinstance(mesh_dict['vertex_attribute_titles'], num.ndarray): old_title_list = mesh_dict['vertex_attribute_titles'].tolist() else: old_title_list = mesh_dict['vertex_attribute_titles'] if verbose: log.critical('tsh file %s loaded' % mesh_file) # load in the points file try: geo = Geospatial_data(point_file, verbose=verbose) except IOError as e: if display_errors: log.critical("Could not load bad file: %s" % str(e)) raise IOError # Re-raise exception point_coordinates = geo.get_data_points(absolute=True) title_list, point_attributes = concatinate_attributelist( geo.get_all_attributes()) if 'geo_reference' in mesh_dict and \ not mesh_dict['geo_reference'] is None: mesh_origin = mesh_dict['geo_reference'].get_origin() else: mesh_origin = None if verbose: log.critical("points file loaded") if verbose: log.critical("fitting to mesh") f = fit_to_mesh(point_coordinates, vertex_coordinates, triangles, None, point_attributes, alpha=alpha, verbose=verbose, data_origin=None, mesh_origin=mesh_origin) if verbose: log.critical("finished fitting to mesh") # convert array to list of lists new_point_attributes = f.tolist() # FIXME have this overwrite attributes with the same title - DSG # Put the newer attributes last if old_title_list != []: old_title_list.extend(title_list) # FIXME can this be done a faster way? - DSG for i in range(len(old_point_attributes)): old_point_attributes[i].extend(new_point_attributes[i]) mesh_dict['vertex_attributes'] = old_point_attributes mesh_dict['vertex_attribute_titles'] = old_title_list else: mesh_dict['vertex_attributes'] = new_point_attributes mesh_dict['vertex_attribute_titles'] = title_list if verbose: log.critical("exporting to file %s" % mesh_output_file) try: export_mesh_file(mesh_output_file, mesh_dict) except IOError as e: if display_errors: log.critical("Could not write file %s", str(e)) raise IOError