def test_points2polygon(self): att_dict = {} pointlist = num.array([[1.0, 0.0],[0.0, 1.0],[0.0, 0.0]]) att_dict['elevation'] = num.array([10.1, 0.0, 10.4]) att_dict['brightness'] = num.array([10.0, 1.0, 10.4]) fileName = tempfile.mktemp(".csv") G = Geospatial_data(pointlist, att_dict) G.export_points_file(fileName) polygon = load_pts_as_polygon(fileName) # This test may fail if the order changes assert (polygon == [[0.0, 0.0],[1.0, 0.0],[0.0, 1.0]])
def test_fit_to_mesh_pts_passing_mesh_in(self): a = [-1.0, 0.0] b = [3.0, 4.0] c = [4.0, 1.0] d = [-3.0, 2.0] #3 e = [-1.0, -2.0] f = [1.0, -2.0] #5 vertices = [a, b, c, d, e, f] triangles = [[0, 1, 3], [1, 0, 2], [0, 4, 5], [0, 5, 2]] #abd bac aef afc fileName = tempfile.mktemp(".txt") file = open(fileName, "w") file.write(" x, y, elevation \n\ -2.0, 2.0, 0.\n\ -1.0, 1.0, 0.\n\ 0.0, 2.0 , 2.\n\ 1.0, 1.0 , 2.\n\ 2.0, 1.0 ,3. \n\ 0.0, 0.0 , 0.\n\ 1.0, 0.0 , 1.\n\ 0.0, -1.0, -1.\n\ -0.2, -0.5, -0.7\n\ -0.9, -1.5, -2.4\n\ 0.5, -1.9, -1.4\n\ 3.0, 1.0 , 4.\n") file.close() geo = Geospatial_data(fileName) fileName_pts = tempfile.mktemp(".pts") geo.export_points_file(fileName_pts) f = fit_to_mesh(fileName_pts, vertices, triangles, alpha=0.0, max_read_lines=2) answer = linear_function(vertices) #print "f\n",f #print "answer\n",answer assert num.allclose(f, answer) os.remove(fileName) os.remove(fileName_pts)
def test_fit_to_mesh_pts_passing_mesh_in(self): a = [-1.0, 0.0] b = [3.0, 4.0] c = [4.0,1.0] d = [-3.0, 2.0] #3 e = [-1.0,-2.0] f = [1.0, -2.0] #5 vertices = [a, b, c, d,e,f] triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc fileName = tempfile.mktemp(".txt") file = open(fileName,"w") file.write(" x, y, elevation \n\ -2.0, 2.0, 0.\n\ -1.0, 1.0, 0.\n\ 0.0, 2.0 , 2.\n\ 1.0, 1.0 , 2.\n\ 2.0, 1.0 ,3. \n\ 0.0, 0.0 , 0.\n\ 1.0, 0.0 , 1.\n\ 0.0, -1.0, -1.\n\ -0.2, -0.5, -0.7\n\ -0.9, -1.5, -2.4\n\ 0.5, -1.9, -1.4\n\ 3.0, 1.0 , 4.\n") file.close() geo = Geospatial_data(fileName) fileName_pts = tempfile.mktemp(".pts") geo.export_points_file(fileName_pts) f = fit_to_mesh(fileName_pts, vertices, triangles, alpha=0.0, max_read_lines=2) answer = linear_function(vertices) #print "f\n",f #print "answer\n",answer assert num.allclose(f, answer) os.remove(fileName) os.remove(fileName_pts)
def trial(self, num_of_points=20000, maxArea=1000, max_points_per_cell=13, is_fit=True, use_file_type=None, blocking_len=500000, segments_in_mesh=True, save=False, verbose=False, run_profile=False, gridded=True, geo_ref=True): ''' num_of_points ''' if geo_ref is True: geo = Geo_reference(xllcorner=2.0, yllcorner=2.0) else: geo = None mesh_dict = self._build_regular_mesh_dict(maxArea=maxArea, is_segments=segments_in_mesh, save=save, geo=geo) points_dict = self._build_points_dict(num_of_points=num_of_points, gridded=gridded, verbose=verbose) if is_fit is True: op = "Fit_" else: op = "Interp_" profile_file = op + "P" + str(num_of_points) + \ "T" + str(len(mesh_dict['triangles'])) + \ "PPC" + str(max_points_per_cell) + \ ".txt" # Apply the geo_ref to the points, so they are relative # Pass in the geo_ref domain = Domain(mesh_dict['vertices'], mesh_dict['triangles'], use_cache=False, verbose=verbose, geo_reference=geo) #Initial time and memory t0 = time.time() #m0 = None on windows m0 = mem_usage() # Apply the geo_ref to the points, so they are relative # Pass in the geo_ref geospatial = Geospatial_data(points_dict['points'], points_dict['point_attributes'], geo_reference=geo) del points_dict if is_fit is True: if use_file_type is None: points = geospatial filename = None else: #FIXME (DSG) check that the type fileName = tempfile.mktemp("." + use_file_type) geospatial.export_points_file(fileName, absolute=True) points = None filename = fileName if run_profile is True: s = """domain.set_quantity('elevation',points,filename=filename,use_cache=False)""" pobject = profile.Profile() presult = pobject.runctx(s, vars(sys.modules[__name__]), vars()) prof_file = tempfile.mktemp(".prof") presult.dump_stats(prof_file) # # Let process these results S = pstats.Stats(prof_file) saveout = sys.stdout pfile = open(profile_file, "w") sys.stdout = pfile s = S.sort_stats('cumulative').print_stats(60) sys.stdout = saveout pfile.close() os.remove(prof_file) else: domain.set_quantity('elevation', points, filename=filename, use_cache=False, verbose=verbose) if not use_file_type is None: os.remove(fileName) else: # run an interploate problem. if run_profile: # pass in the geospatial points # and the mesh origin s = """benchmark_interpolate(mesh_dict['vertices'],mesh_dict['vertex_attributes'],mesh_dict['triangles'],geospatial,max_points_per_cell=max_points_per_cell,mesh_origin=geo)""" pobject = profile.Profile() presult = pobject.runctx(s, vars(sys.modules[__name__]), vars()) prof_file = tempfile.mktemp(".prof") presult.dump_stats(prof_file) # # Let process these results S = pstats.Stats(prof_file) saveout = sys.stdout pfile = open(profile_file, "w") sys.stdout = pfile s = S.sort_stats('cumulative').print_stats(60) sys.stdout = saveout pfile.close() os.remove(prof_file) else: # pass in the geospatial points benchmark_interpolate(mesh_dict['vertices'], mesh_dict['vertex_attributes'], mesh_dict['triangles'], geospatial, mesh_origin=geo, max_points_per_cell=max_points_per_cell, verbose=verbose) time_taken_sec = (time.time() - t0) m1 = mem_usage() if m0 is None or m1 is None: memory_used = None else: memory_used = (m1 - m0) #print 'That took %.2f seconds' %time_taken_sec # return the times spent in first cell searching and # backing up. #search_one_cell_time, search_more_cells_time = search_times() #reset_search_times() #print "bench - build_quadtree_time", get_build_quadtree_time() return time_taken_sec, memory_used, len(mesh_dict['triangles']), \ get_build_quadtree_time()
def trial(self, num_of_points=20000, maxArea=1000, max_points_per_cell=13, is_fit=True, use_file_type=None, blocking_len=500000, segments_in_mesh=True, save=False, verbose=False, run_profile=False, gridded=True, geo_ref=True): ''' num_of_points ''' if geo_ref is True: geo = Geo_reference(xllcorner = 2.0, yllcorner = 2.0) else: geo = None mesh_dict = self._build_regular_mesh_dict(maxArea=maxArea, is_segments=segments_in_mesh, save=save, geo=geo) points_dict = self._build_points_dict(num_of_points=num_of_points, gridded=gridded, verbose=verbose) if is_fit is True: op = "Fit_" else: op = "Interp_" profile_file = op + "P" + str(num_of_points) + \ "T" + str(len(mesh_dict['triangles'])) + \ "PPC" + str(max_points_per_cell) + \ ".txt" # Apply the geo_ref to the points, so they are relative # Pass in the geo_ref domain = Domain(mesh_dict['vertices'], mesh_dict['triangles'], use_cache=False, verbose=verbose, geo_reference=geo) #Initial time and memory t0 = time.time() #m0 = None on windows m0 = mem_usage() # Apply the geo_ref to the points, so they are relative # Pass in the geo_ref geospatial = Geospatial_data(points_dict['points'], points_dict['point_attributes'], geo_reference=geo) del points_dict if is_fit is True: if use_file_type == None: points = geospatial filename = None else: #FIXME (DSG) check that the type fileName = tempfile.mktemp("." + use_file_type) geospatial.export_points_file(fileName, absolute=True) points = None filename = fileName if run_profile is True: s = """domain.set_quantity('elevation',points,filename=filename,use_cache=False)""" pobject = profile.Profile() presult = pobject.runctx(s, vars(sys.modules[__name__]), vars()) prof_file = tempfile.mktemp(".prof") presult.dump_stats(prof_file) # # Let process these results S = pstats.Stats(prof_file) saveout = sys.stdout pfile = open(profile_file, "w") sys.stdout = pfile s = S.sort_stats('cumulative').print_stats(60) sys.stdout = saveout pfile.close() os.remove(prof_file) else: domain.set_quantity('elevation',points,filename=filename, use_cache=False, verbose=verbose) if not use_file_type == None: os.remove(fileName) else: # run an interploate problem. if run_profile: # pass in the geospatial points # and the mesh origin s="""benchmark_interpolate(mesh_dict['vertices'],mesh_dict['vertex_attributes'],mesh_dict['triangles'],geospatial,max_points_per_cell=max_points_per_cell,mesh_origin=geo)""" pobject = profile.Profile() presult = pobject.runctx(s, vars(sys.modules[__name__]), vars()) prof_file = tempfile.mktemp(".prof") presult.dump_stats(prof_file) # # Let process these results S = pstats.Stats(prof_file) saveout = sys.stdout pfile = open(profile_file, "w") sys.stdout = pfile s = S.sort_stats('cumulative').print_stats(60) sys.stdout = saveout pfile.close() os.remove(prof_file) else: # pass in the geospatial points benchmark_interpolate(mesh_dict['vertices'], mesh_dict['vertex_attributes'], mesh_dict['triangles'], geospatial, mesh_origin=geo, max_points_per_cell=max_points_per_cell, verbose=verbose) time_taken_sec = (time.time()-t0) m1 = mem_usage() if m0 is None or m1 is None: memory_used = None else: memory_used = (m1 - m0) #print 'That took %.2f seconds' %time_taken_sec # return the times spent in first cell searching and # backing up. #search_one_cell_time, search_more_cells_time = search_times() #reset_search_times() #print "bench - build_quadtree_time", get_build_quadtree_time() return time_taken_sec, memory_used, len(mesh_dict['triangles']), \ get_build_quadtree_time()
assert len(vertex_points.shape) == 2 # Interpolate from anuga.fit_interpolate.interpolate import Interpolate interp = Interpolate(vertex_points, volumes, verbose=verbose) # Interpolate using quantity values if verbose: log.critical('Interpolating') interpolated_values = interp.interpolate(q, data_points).flatten() if verbose: log.critical( 'Interpolated values are in [%f, %f]' % (num.min(interpolated_values), num.max(interpolated_values))) # Assign NODATA_value to all points outside bounding polygon # (from interpolation mesh) P = interp.mesh.get_boundary_polygon() outside_indices = outside_polygon(data_points, P, closed=True) for i in outside_indices: interpolated_values[i] = NODATA_value # Store results G = Geospatial_data(data_points=data_points, attributes=interpolated_values) G.export_points_file(name_out, absolute=True) fid.close()
assert len(vertex_points.shape) == 2 # Interpolate from anuga.fit_interpolate.interpolate import Interpolate interp = Interpolate(vertex_points, volumes, verbose=verbose) # Interpolate using quantity values if verbose: log.critical('Interpolating') interpolated_values = interp.interpolate(q, data_points).flatten() if verbose: log.critical('Interpolated values are in [%f, %f]' % (num.min(interpolated_values), num.max(interpolated_values))) # Assign NODATA_value to all points outside bounding polygon # (from interpolation mesh) P = interp.mesh.get_boundary_polygon() outside_indices = outside_polygon(data_points, P, closed=True) for i in outside_indices: interpolated_values[i] = NODATA_value # Store results G = Geospatial_data(data_points=data_points, attributes=interpolated_values) G.export_points_file(name_out, absolute = True) fid.close()
def sww2pts(name_in, name_out=None, data_points=None, quantity=None, timestep=None, reduction=None, NODATA_value=-9999, verbose=False, origin=None): """Read SWW file and convert to interpolated values at selected points The parameter 'quantity' must be the name of an existing quantity or an expression involving existing quantities. The default is 'elevation'. if timestep (an index) is given, output quantity at that timestep. if reduction is given use that to reduce quantity over all timesteps. data_points (Nx2 array) give locations of points where quantity is to be computed. """ import sys from anuga.geometry.polygon import inside_polygon, outside_polygon from anuga.abstract_2d_finite_volumes.util import \ apply_expression_to_dictionary from anuga.geospatial_data.geospatial_data import Geospatial_data if quantity is None: quantity = 'elevation' if reduction is None: reduction = max basename_in, in_ext = os.path.splitext(name_in) if name_out != None: basename_out, out_ext = os.path.splitext(name_out) else: basename_out = basename_in + '_%s' % quantity out_ext = '.pts' name_out = basename_out + out_ext if in_ext != '.sww': raise IOError('Input format for %s must be .sww' % name_in) if out_ext != '.pts': raise IOError('Output format for %s must be .pts' % name_out) # Read sww file if verbose: log.critical('Reading from %s' % name_in) from anuga.file.netcdf import NetCDFFile fid = NetCDFFile(name_in) # Get extent and reference x = fid.variables['x'][:] y = fid.variables['y'][:] volumes = fid.variables['volumes'][:] try: # works with netcdf4 number_of_timesteps = len(fid.dimensions['number_of_timesteps']) number_of_points = len(fid.dimensions['number_of_points']) except: #works with scientific.io.netcdf number_of_timesteps = fid.dimensions['number_of_timesteps'] number_of_points = fid.dimensions['number_of_points'] if origin is None: # Get geo_reference # sww files don't have to have a geo_ref try: geo_reference = Geo_reference(NetCDFObject=fid) except AttributeError as e: geo_reference = Geo_reference() # Default georef object xllcorner = geo_reference.get_xllcorner() yllcorner = geo_reference.get_yllcorner() zone = geo_reference.get_zone() else: zone = origin[0] xllcorner = origin[1] yllcorner = origin[2] # FIXME: Refactor using code from file_function.statistics # Something like print swwstats(swwname) if verbose: x = fid.variables['x'][:] y = fid.variables['y'][:] times = fid.variables['time'][:] log.critical('------------------------------------------------') log.critical('Statistics of SWW file:') log.critical(' Name: %s' % swwfile) log.critical(' Reference:') log.critical(' Lower left corner: [%f, %f]' % (xllcorner, yllcorner)) log.critical(' Start time: %f' % fid.starttime[0]) log.critical(' Extent:') log.critical(' x [m] in [%f, %f], len(x) == %d' % (num.min(x), num.max(x), len(x.flat))) log.critical(' y [m] in [%f, %f], len(y) == %d' % (num.min(y), num.max(y), len(y.flat))) log.critical(' t [s] in [%f, %f], len(t) == %d' % (min(times), max(times), len(times))) log.critical(' Quantities [SI units]:') for name in ['stage', 'xmomentum', 'ymomentum', 'elevation']: q = fid.variables[name][:].flat log.critical(' %s in [%f, %f]' % (name, min(q), max(q))) # Get quantity and reduce if applicable if verbose: log.critical('Processing quantity %s' % quantity) # Turn NetCDF objects into numeric arrays quantity_dict = {} for name in list(fid.variables.keys()): quantity_dict[name] = fid.variables[name][:] # Convert quantity expression to quantities found in sww file q = apply_expression_to_dictionary(quantity, quantity_dict) if len(q.shape) == 2: # q has a time component and needs to be reduced along # the temporal dimension if verbose: log.critical('Reducing quantity %s' % quantity) q_reduced = num.zeros(number_of_points, num.float) for k in range(number_of_points): q_reduced[k] = reduction(q[:, k]) q = q_reduced # Post condition: Now q has dimension: number_of_points assert len(q.shape) == 1 assert q.shape[0] == number_of_points if verbose: log.critical('Processed values for %s are in [%f, %f]' % (quantity, min(q), max(q))) # Create grid and update xll/yll corner and x,y vertex_points = num.concatenate((x[:, num.newaxis], y[:, num.newaxis]), axis=1) assert len(vertex_points.shape) == 2 # Interpolate from anuga.fit_interpolate.interpolate import Interpolate interp = Interpolate(vertex_points, volumes, verbose=verbose) # Interpolate using quantity values if verbose: log.critical('Interpolating') interpolated_values = interp.interpolate(q, data_points).flatten() if verbose: log.critical( 'Interpolated values are in [%f, %f]' % (num.min(interpolated_values), num.max(interpolated_values))) # Assign NODATA_value to all points outside bounding polygon # (from interpolation mesh) P = interp.mesh.get_boundary_polygon() outside_indices = outside_polygon(data_points, P, closed=True) for i in outside_indices: interpolated_values[i] = NODATA_value # Store results G = Geospatial_data(data_points=data_points, attributes=interpolated_values) G.export_points_file(name_out, absolute=True) fid.close()
def sdf2pts(name_in, name_out=None, verbose=False): """ Read HEC-RAS Elevation datal from the following ASCII format (.sdf) basename_in Sterm of input filename. basename_out Sterm of output filename. verbose True if this function is to be verbose. Example: # RAS export file created on Mon 15Aug2005 11:42 # by HEC-RAS Version 3.1.1 BEGIN HEADER: UNITS: METRIC DTM TYPE: TIN DTM: v:\\1\\cit\\perth_topo\\river_tin STREAM LAYER: c:\\local\\hecras\\21_02_03\\up_canning_cent3d.shp CROSS-SECTION LAYER: c:\\local\\hecras\\21_02_03\\up_can_xs3d.shp MAP PROJECTION: UTM PROJECTION ZONE: 50 DATUM: AGD66 VERTICAL DATUM: NUMBER OF REACHES: 19 NUMBER OF CROSS-SECTIONS: 14206 END HEADER: Only the SURFACE LINE data of the following form will be utilised CROSS-SECTION: STREAM ID:Southern-Wungong REACH ID:Southern-Wungong STATION:19040.* CUT LINE: 405548.671603161 , 6438142.7594925 405734.536092045 , 6438326.10404912 405745.130459356 , 6438331.48627354 405813.89633823 , 6438368.6272789 SURFACE LINE: 405548.67, 6438142.76, 35.37 405552.24, 6438146.28, 35.41 405554.78, 6438148.78, 35.44 405555.80, 6438149.79, 35.44 405559.37, 6438153.31, 35.45 405560.88, 6438154.81, 35.44 405562.93, 6438156.83, 35.42 405566.50, 6438160.35, 35.38 405566.99, 6438160.83, 35.37 ... END CROSS-SECTION Convert to NetCDF pts format which is points: (Nx2) float array elevation: N float array """ import os from anuga.file.netcdf import NetCDFFile if name_in[-4:] != '.sdf': raise IOError('Input file %s should be of type .sdf.' % name_in) if name_out is None: name_out = name_in[:-4] + '.pts' elif name_out[-4:] != '.pts': raise IOError('Input file %s should be of type .pts.' % name_out) # Get ASCII file infile = open(name_in, 'r') if verbose: log.critical('Reading DEM from %s' % (root + '.sdf')) lines = infile.readlines() infile.close() if verbose: log.critical('Converting to pts format') # Scan through the header, picking up stuff we need. i = 0 while lines[i].strip() == '' or lines[i].strip().startswith('#'): i += 1 assert lines[i].strip().upper() == 'BEGIN HEADER:' i += 1 assert lines[i].strip().upper().startswith('UNITS:') units = lines[i].strip().split()[1] i += 1 assert lines[i].strip().upper().startswith('DTM TYPE:') i += 1 assert lines[i].strip().upper().startswith('DTM:') i += 1 assert lines[i].strip().upper().startswith('STREAM') i += 1 assert lines[i].strip().upper().startswith('CROSS') i += 1 assert lines[i].strip().upper().startswith('MAP PROJECTION:') projection = lines[i].strip().split(':')[1] i += 1 assert lines[i].strip().upper().startswith('PROJECTION ZONE:') zone = int(lines[i].strip().split(':')[1]) i += 1 assert lines[i].strip().upper().startswith('DATUM:') datum = lines[i].strip().split(':')[1] i += 1 assert lines[i].strip().upper().startswith('VERTICAL DATUM:') i += 1 assert lines[i].strip().upper().startswith('NUMBER OF REACHES:') i += 1 assert lines[i].strip().upper().startswith('NUMBER OF CROSS-SECTIONS:') number_of_cross_sections = int(lines[i].strip().split(':')[1]) i += 1 # Now read all points points = [] elevation = [] for j, entries in enumerate(_read_hecras_cross_sections(lines[i:])): for k, entry in enumerate(entries): points.append(entry[:2]) elevation.append(entry[2]) msg = 'Actual #number_of_cross_sections == %d, Reported as %d'\ %(j+1, number_of_cross_sections) assert j + 1 == number_of_cross_sections, msg # Get output file, write PTS data if name_out is None: ptsname = name_in[:-4] + '.pts' else: ptsname = name_out geo_ref = Geo_reference(zone, 0, 0, datum, projection, units) geo = Geospatial_data(points, {"elevation": elevation}, verbose=verbose, geo_reference=geo_ref) geo.export_points_file(ptsname)