def evaluate(self, vol_id, edge_id): """Transmissive_n_momentum_zero_t_momentum_set_stage_boundary return the edge momentum values of the volume they serve. """ q = self.domain.get_conserved_quantities(vol_id, edge=edge_id) normal = self.domain.get_normal(vol_id, edge_id) t = self.domain.get_time() if hasattr(self.function, 'time'): # Roll boundary over if time exceeds while t > self.function.time[-1]: msg = 'WARNING: domain time %.2f has exceeded' % t msg += 'time provided in ' msg += 'transmissive_momentum_set_stage_boundary object.\n' msg += 'I will continue, reusing the object from t==0' log.critical(msg) t -= self.function.time[-1] value = self.function(t) try: x = float(value) except: x = float(value[0]) q[0] = x ndotq = (normal[0] * q[1] + normal[1] * q[2]) q[1] = normal[0] * ndotq q[2] = normal[1] * ndotq return q
def safe_acos(x): """Safely compute acos Protect against cases where input argument x is outside the allowed interval [-1.0, 1.0] by no more than machine precision """ error_msg = 'Input to acos is outside allowed domain [-1.0, 1.0].'+\ 'I got %.12f' %x warning_msg = 'Changing argument to acos from %.18f to %.1f' % (x, sign(x)) eps = get_machine_precision() # Machine precision if x < -1.0: if x < -1.0 - eps: raise ValueError, error_msg else: warn(warning_msg) x = -1.0 if x > 1.0: if x > 1.0 + eps: raise ValueError, error_msg else: log.critical('NOTE: changing argument to acos from %.18f to 1.0' % x) x = 1.0 return acos(x)
def inundation_damage(sww_base_name, exposure_files_in, exposure_file_out_marker=None, ground_floor_height=0.3, overwrite=False, verbose=True, use_cache = True): """ This is the main function for calculating tsunami damage due to inundation. It gets the location of structures from the exposure file and gets the inundation of these structures from the sww file. It then calculates the damage loss. Note, structures outside of the sww file get the minimum inundation (-ground_floor_height). These calculations are done over all the sww files with the sww_base_name in the specified directory. exposure_files_in - a file or a list of files to input from exposure_file_out_marker - this string will be added to the input file name to get the output file name """ if isinstance(exposure_files_in, basestring): exposure_files_in = [exposure_files_in] for exposure_file_in in exposure_files_in: csv = Exposure(exposure_file_in, title_check_list=[SHORE_DIST_LABEL,WALL_TYPE_LABEL, STR_VALUE_LABEL,CONT_VALUE_LABEL]) geospatial = csv.get_location() geospatial = ensure_absolute(geospatial) max_depths, max_momentums = calc_max_depth_and_momentum(sww_base_name, geospatial, ground_floor_height=ground_floor_height, verbose=verbose, use_cache=use_cache) edm = EventDamageModel(max_depths, csv.get_column(SHORE_DIST_LABEL), csv.get_column(WALL_TYPE_LABEL), csv.get_column(STR_VALUE_LABEL), csv.get_column(CONT_VALUE_LABEL) ) results_dic = edm.calc_damage_and_costs(verbose_csv=True, verbose=verbose) for title, value in results_dic.iteritems(): csv.set_column(title, value, overwrite=overwrite) # Save info back to csv file if exposure_file_out_marker == None: exposure_file_out = exposure_file_in else: # split off extension, in such a way to deal with more than one '.' in the name of file split_name = exposure_file_in.split('.') exposure_file_out = '.'.join(split_name[:-1]) + exposure_file_out_marker + \ '.' + split_name[-1] csv.save(exposure_file_out) if verbose: log.critical('Augmented building file written to %s' % exposure_file_out)
def get_intersecting_segments(V, N, polyline, verbose=False): """Internal function to find edges intersected by Polyline Input: V: Vertex coordinates as obtained by mesh.get_vertex_coordinates() N: Number of triangles in mesh polyline - list of points forming a segmented line verbose Output: list of instances of class Triangle_intersection This method is used by the public method get_intersecting_segments(self, polyline) which also contains more documentation. """ msg = "Polyline must contain at least two points" assert len(polyline) >= 2, msg # For all segments in polyline triangle_intersections = [] for i, point0 in enumerate(polyline[:-1]): point1 = polyline[i + 1] if verbose: log.critical("Extracting mesh intersections from line:") log.critical("(%.2f, %.2f) - (%.2f, %.2f)" % (point0[0], point0[1], point1[0], point1[1])) line = [point0, point1] triangle_intersections += _get_intersecting_segments(V, N, line, verbose=verbose) msg = "No segments found" assert len(triangle_intersections) > 0, msg return triangle_intersections
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, e: if display_errors: log.critical("Could not load bad file: %s" % str(e)) raise IOError #Could not load bad mesh file.
def evaluate(self, vol_id, edge_id): """Transmissive_n_momentum_zero_t_momentum_set_stage_boundary return the edge momentum values of the volume they serve. """ q = self.domain.get_conserved_quantities(vol_id, edge = edge_id) normal = self.domain.get_normal(vol_id, edge_id) t = self.domain.get_time() if hasattr(self.function, 'time'): # Roll boundary over if time exceeds while t > self.function.time[-1]: msg = 'WARNING: domain time %.2f has exceeded' % t msg += 'time provided in ' msg += 'transmissive_momentum_set_stage_boundary object.\n' msg += 'I will continue, reusing the object from t==0' log.critical(msg) t -= self.function.time[-1] value = self.function(t) try: x = float(value) except: x = float(value[0]) q[0] = x ndotq = (normal[0]*q[1] + normal[1]*q[2]) q[1] = normal[0]*ndotq q[2] = normal[1]*ndotq return q
def evaluate(self, vol_id, edge_id): """Transmissive momentum set stage boundaries return the edge momentum values of the volume they serve. vol_id is volume id edge_id is the edge within the volume """ q = self.domain.get_conserved_quantities(vol_id, edge = edge_id) t = self.domain.get_time() if hasattr(self.function, 'time'): # Roll boundary over if time exceeds while t > self.function.time[-1]: msg = 'WARNING: domain time %.2f has exceeded' % t msg += 'time provided in ' msg += 'transmissive_momentum_set_stage_boundary object.\n' msg += 'I will continue, reusing the object from t==0' log.critical(msg) t -= self.function.time[-1] value = self.function(t) try: x = float(value) except: x = float(value[0]) q[0] = x return q
def get_flow_through_cross_section(filename, polyline, verbose=False): """Obtain flow (m^3/s) perpendicular to specified cross section. filename path to SWW file to read polyline representation of desired cross-section - it may contain multiple sections allowing for complex shapes. Assume absolute UTM coordinates. Format [[x0, y0], [x1, y1], ...] verbose True if this function is to be verbose Return (time, Q) where time is a list of all stored times in SWW file and Q is a hydrograph of total flow across given segments for all stored times. The normal flow is computed for each triangle intersected by the polyline and added up. Multiple segments at different angles are specified the normal flows may partially cancel each other. The typical usage of this function would be to get flow through a channel, and the polyline would then be a cross section perpendicular to the flow. """ quantity_names = ['elevation', 'stage', 'xmomentum', 'ymomentum'] # Get values for quantities at each midpoint of poly line from sww file X = get_interpolated_quantities_at_polyline_midpoints(filename, quantity_names=\ quantity_names, polyline=polyline, verbose=verbose) segments, interpolation_function = X # Get vectors for time and interpolation_points time = interpolation_function.time interpolation_points = interpolation_function.interpolation_points if verbose: log.critical('Computing hydrograph') # Compute hydrograph Q = [] for t in time: total_flow = 0 for i in range(len(interpolation_points)): elevation, stage, uh, vh = interpolation_function(t, point_id=i) normal = segments[i].normal # Inner product of momentum vector with segment normal [m^2/s] normal_momentum = uh * normal[0] + vh * normal[1] # Flow across this segment [m^3/s] segment_flow = normal_momentum * segments[i].length # Accumulate total_flow += segment_flow # Store flow at this timestep Q.append(total_flow) return time, Q
def __call__(self): """ Apply w_uh_vh to those triangles defined in indices indices == [], then don't apply anywhere indices == None, then apply everywhere otherwise apply for the specific indices """ if self.indices is []: return w_uh_vh = self.get_w_uh_vh() if w_uh_vh is None: return if self.verbose is True: log.critical('w_uh_vh of %s at time = %.2f = %f' % (self.quantity_name, domain.get_time(), stage)) if self.indices is None: self.stage_c[:] = w_uh_vh[0] self.xmom_c[:] = w_uh_vh[1] self.ymom_c[:] = w_uh_vh[2] else: self.stage_c[self.indices] = w_uh_vh[0] self.xmom_c[self.indices] = w_uh_vh[1] self.ymom_c[self.indices] = w_uh_vh[2]
def __call__(self): """ Apply w_uh_vh to those triangles defined in indices indices == [], then don't apply anywhere indices is None, then apply everywhere otherwise apply for the specific indices """ if self.indices is []: return w_uh_vh = self.get_w_uh_vh() if w_uh_vh is None: return if self.verbose is True: log.critical('w_uh_vh of %s at time = %.2f = %f' % (self.quantity_name, domain.get_time(), stage)) if self.indices is None: self.stage_c[:] = w_uh_vh[0] self.xmom_c[:] = w_uh_vh[1] self.ymom_c[:] = w_uh_vh[2] else: self.stage_c[self.indices] = w_uh_vh[0] self.xmom_c[self.indices] = w_uh_vh[1] self.ymom_c[self.indices] = w_uh_vh[2]
def safe_acos(x): """Safely compute acos Protect against cases where input argument x is outside the allowed interval [-1.0, 1.0] by no more than machine precision """ error_msg = 'Input to acos is outside allowed domain [-1.0, 1.0].'+\ 'I got %.12f' %x warning_msg = 'Changing argument to acos from %.18f to %.1f' %(x, sign(x)) eps = get_machine_precision() # Machine precision if x < -1.0: if x < -1.0 - eps: raise ValueError, error_msg else: warn(warning_msg) x = -1.0 if x > 1.0: if x > 1.0 + eps: raise ValueError, error_msg else: log.critical('NOTE: changing argument to acos from %.18f to 1.0' %x) x = 1.0 return acos(x)
def evaluate(self, vol_id, edge_id): """Transmissive momentum set stage boundaries return the edge momentum values of the volume they serve. vol_id is volume id edge_id is the edge within the volume """ q = self.domain.get_conserved_quantities(vol_id, edge=edge_id) t = self.domain.get_time() if hasattr(self.function, 'time'): # Roll boundary over if time exceeds while t > self.function.time[-1]: msg = 'WARNING: domain time %.2f has exceeded' % t msg += 'time provided in ' msg += 'transmissive_momentum_set_stage_boundary object.\n' msg += 'I will continue, reusing the object from t==0' log.critical(msg) t -= self.function.time[-1] value = self.function(t) try: x = float(value) except: x = float(value[0]) q[0] = x return q
def get_min_max_values(list=None): """ Returns the min and max of the list it was provided. """ if list == None: log.critical('List must be provided') return min(list), max(list)
def excepthook(type, value, tb): """Exception hook routine.""" msg = '\n' + '='*80 + '\n' msg += 'Uncaught exception:\n' msg += ''.join(traceback.format_exception(type, value, tb)) msg += '='*80 + '\n' log.critical(msg)
def store_parameters(verbose=False,**kwargs): """Temporary Interface to new location""" from anuga.shallow_water.data_manager \ import store_parameters as dm_store_parameters log.critical('store_parameters has moved from util.py.') log.critical('Please use "from anuga.shallow_water.data_manager ' 'import store_parameters"') return dm_store_parameters(verbose=False,**kwargs)
def store_parameters(verbose=False, **kwargs): """Temporary Interface to new location""" from anuga.shallow_water.data_manager \ import store_parameters as dm_store_parameters log.critical('store_parameters has moved from util.py.') log.critical('Please use "from anuga.shallow_water.data_manager ' 'import store_parameters"') return dm_store_parameters(verbose=False, **kwargs)
def create_polygon_function(building_polygons, geo_reference=None): L = [] for i, key in enumerate(building_polygons): if i%100==0: log.critical(i) poly = building_polygons[key] elev = building_heights[key] L.append((poly, elev)) buildings = anuga.Polygon_function(L, default=0.0, geo_reference=geo_reference) return buildings
def show(self, depth=0): """Traverse tree below self, dumping all information. """ if depth == 0: log.critical() print '%s%s' % (' '*depth, self.name), self.extents, ' [', \ self.leaves, ']' if self.children: log.critical() for child in self.children: child.show(depth + 1)
def show(self, depth=0): """Traverse tree below self, dumping all information. """ if depth == 0: log.critical() print '%s%s' % (' '*depth, self.name), self.extents, ' [', \ self.leaves, ']' if self.children: log.critical() for child in self.children: child.show(depth+1)
def get_runup_data_for_locations_from_file(gauge_filename, sww_filename, runup_filename, size=10, verbose=False): """this will read a csv file with the header x,y. Then look in a square 'size'x2 around this position for the 'max_inundaiton_height' in the 'sww_filename' and report the findings in the 'runup_filename'. WARNING: NO TESTS! """ from anuga.shallow_water.data_manager import \ get_maximum_inundation_data file = open(runup_filename, "w") file.write("easting,northing,runup \n ") file.close() #read gauge csv file to dictionary attribute_dic, title_index_dic = load_csv_as_dict(gauge_filename) northing = [float(x) for x in attribute_dic["y"]] easting = [float(x) for x in attribute_dic["x"]] log.critical('Reading %s' % sww_filename) runup_locations = [] for i, x in enumerate(northing): poly = [[int(easting[i] + size), int(northing[i] + size)], [int(easting[i] + size), int(northing[i] - size)], [int(easting[i] - size), int(northing[i] - size)], [int(easting[i] - size), int(northing[i] + size)]] run_up, x_y = get_maximum_inundation_data(filename=sww_filename, polygon=poly, verbose=False) #if no runup will return 0 instead of NONE if run_up == None: run_up = 0 if x_y == None: x_y = [0, 0] if verbose: log.critical('maximum inundation runup near %s is %s meters' % (x_y, run_up)) #writes to file file = open(runup_filename, "a") temp = '%s,%s,%s \n' % (x_y[0], x_y[1], run_up) file.write(temp) file.close()
def get_interpolated_quantities_at_polyline_midpoints(filename, quantity_names=None, polyline=None, verbose=False): """Get values for quantities interpolated to polyline midpoints from SWW. filename path to file to read quantity_names quantity names to get polyline representation of desired cross-section may contain multiple sections allowing complex shapes assume UTM coordinates verbose True if this function is to be verbose Returns (segments, i_func) where segments is a list of Triangle_intersection instances and i_func is an instance of Interpolation_function. Note: For 'polyline' assume absolute UTM coordinates. This function is used by get_flow_through_cross_section and get_energy_through_cross_section. """ from anuga.fit_interpolate.interpolate import Interpolation_function # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file(filename, quantities=quantity_names, verbose=verbose) mesh, quantities, time = X # Find all intersections and associated triangles. segments = mesh.get_intersecting_segments(polyline, verbose=verbose) # Get midpoints interpolation_points = segment_midpoints(segments) # Interpolate if verbose: log.critical( 'Interpolating - total number of interpolation points = %d' % len(interpolation_points)) I = Interpolation_function(time, quantities, quantity_names=quantity_names, vertex_coordinates=mesh.nodes, triangles=mesh.triangles, interpolation_points=interpolation_points, verbose=verbose) return segments, I
def get_interpolated_quantities_at_polyline_midpoints(filename, quantity_names=None, polyline=None, verbose=False): """Get values for quantities interpolated to polyline midpoints from SWW. filename path to file to read quantity_names quantity names to get polyline representation of desired cross-section may contain multiple sections allowing complex shapes assume UTM coordinates verbose True if this function is to be verbose Returns (segments, i_func) where segments is a list of Triangle_intersection instances and i_func is an instance of Interpolation_function. Note: For 'polyline' assume absolute UTM coordinates. This function is used by get_flow_through_cross_section and get_energy_through_cross_section. """ from anuga.fit_interpolate.interpolate import Interpolation_function # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file(filename, quantities=quantity_names, verbose=verbose) mesh, quantities, time = X # Find all intersections and associated triangles. segments = mesh.get_intersecting_segments(polyline, verbose=verbose) # Get midpoints interpolation_points = segment_midpoints(segments) # Interpolate if verbose: log.critical('Interpolating - total number of interpolation points = %d' % len(interpolation_points)) I = Interpolation_function(time, quantities, quantity_names=quantity_names, vertex_coordinates=mesh.nodes, triangles=mesh.triangles, interpolation_points=interpolation_points, verbose=verbose) return segments, I
def check_that_output_is_as_expected(expected_sww, valid_sww, epsilon): '''Check that validation output is as required.''' # get path to expected SWW file log.critical('Checking that simulation results are as expected ...') local_sww = os.path.join(Local_Data_Directory, valid_sww) # get output directory from stdout capture file try: fd = open(RUNMODEL_STDOUT, 'r') except IOError, e: log.critical("Can't open catch file '%s': %s" % (RUNMODEL_STDOUT, str(e))) return 1
def get_runup_data_for_locations_from_file(gauge_filename, sww_filename, runup_filename, size=10, verbose=False): """this will read a csv file with the header x,y. Then look in a square 'size'x2 around this position for the 'max_inundaiton_height' in the 'sww_filename' and report the findings in the 'runup_filename'. WARNING: NO TESTS! """ from anuga.shallow_water.data_manager import \ get_maximum_inundation_data file = open(runup_filename, "w") file.write("easting,northing,runup \n ") file.close() #read gauge csv file to dictionary attribute_dic, title_index_dic = load_csv_as_dict(gauge_filename) northing = [float(x) for x in attribute_dic["y"]] easting = [float(x) for x in attribute_dic["x"]] log.critical('Reading %s' % sww_filename) runup_locations=[] for i, x in enumerate(northing): poly = [[int(easting[i]+size),int(northing[i]+size)], [int(easting[i]+size),int(northing[i]-size)], [int(easting[i]-size),int(northing[i]-size)], [int(easting[i]-size),int(northing[i]+size)]] run_up, x_y = get_maximum_inundation_data(filename=sww_filename, polygon=poly, verbose=False) #if no runup will return 0 instead of NONE if run_up==None: run_up=0 if x_y==None: x_y=[0,0] if verbose: log.critical('maximum inundation runup near %s is %s meters' % (x_y, run_up)) #writes to file file = open(runup_filename, "a") temp = '%s,%s,%s \n' % (x_y[0], x_y[1], run_up) file.write(temp) file.close()
def tsh2sww(infilename, sww_file_name=None, verbose=False): """ This converts a mesh file (.tsh/.msh) to an .sww file. This is usefull to visualise the mesh. Note: This currently just writes the output file in the input file dir. """ if verbose == True: log.critical('Creating domain from %s' % infilename) domain = pmesh_to_domain_instance(infilename, Domain) if verbose == True: log.critical("Number of triangles = %d" % len(domain)) domain.smooth = True domain.format = 'sww' #Native netcdf visualisation format file_path, filename = path.split(infilename) filename, ext = path.splitext(filename) if not (sww_file_name is None): file_path, filename = path.split(sww_file_name) filename, ext = path.splitext(filename) domain.set_name(filename) domain.reduction = mean if verbose == True: log.critical("file_path %s" % file_path) if file_path == "": file_path = "." domain.set_datadir(file_path) if verbose == True: log.critical( "Output written to %s%s%s.%s" % (domain.get_datadir(), sep, domain.get_name(), domain.format)) sww = SWW_file(domain) sww.store_connectivity() sww.store_timestep('stage')
def tsh2sww(infilename, sww_file_name = None, verbose = False): """ This converts a mesh file (.tsh/.msh) to an .sww file. This is usefull to visualise the mesh. Note: This currently just writes the output file in the input file dir. """ if verbose == True: log.critical('Creating domain from %s' % infilename) domain = pmesh_to_domain_instance(infilename, Domain) if verbose == True: log.critical("Number of triangles = %d" % len(domain)) domain.smooth = True domain.format = 'sww' #Native netcdf visualisation format file_path, filename = path.split(infilename) filename, ext = path.splitext(filename) if not (sww_file_name is None): file_path, filename = path.split(sww_file_name) filename, ext = path.splitext(filename) domain.set_name(filename) domain.reduction = mean if verbose == True: log.critical("file_path %s" % file_path) if file_path == "":file_path = "." domain.set_datadir(file_path) if verbose == True: log.critical("Output written to %s%s%s.%s" % (domain.get_datadir(), sep, domain.get_name(), domain.format)) sww = SWW_file(domain) sww.store_connectivity() sww.store_timestep('stage')
def interpolate_sww(sww_file, time, interpolation_points, quantity_names=None, verbose=False): """ obsolete. use file_function in utils """ #open sww file x, y, volumes, time, quantities = read_sww(sww_file) log.critical("x=%s" % str(x)) log.critical("y=%s" % str(y)) log.critical("time=%s" % str(time)) log.critical("quantities=%s" % str(quantities)) #Add the x and y together vertex_coordinates = num.concatenate( (x[:, num.newaxis], y[:, num.newaxis]), axis=1) #Will return the quantity values at the specified times and locations interp = Interpolation_interface(time, quantities, quantity_names=quantity_names, vertex_coordinates=vertex_coordinates, triangles=volumes, interpolation_points=interpolation_points, verbose=verbose)
def interpolate_sww(sww_file, time, interpolation_points, quantity_names=None, verbose=False): """ obsolete. use file_function in utils """ #open sww file x, y, volumes, time, quantities = read_sww(sww_file) log.critical("x=%s" % str(x)) log.critical("y=%s" % str(y)) log.critical("time=%s" % str(time)) log.critical("quantities=%s" % str(quantities)) #Add the x and y together vertex_coordinates = num.concatenate((x[:,num.newaxis], y[:,num.newaxis]), axis=1) #Will return the quantity values at the specified times and locations interp = Interpolation_interface(time, quantities, quantity_names=quantity_names, vertex_coordinates=vertex_coordinates, triangles=volumes, interpolation_points=interpolation_points, verbose=verbose)
def filter_netcdf(filename1, filename2, first=0, last=None, step=1): """Filter data file, selecting timesteps first:step:last. Read netcdf filename1, pick timesteps first:step:last and save to nettcdf file filename2 """ from Scientific.IO.NetCDF import NetCDFFile # Get NetCDF infile = NetCDFFile(filename1, netcdf_mode_r) #Open existing file for read outfile = NetCDFFile(filename2, netcdf_mode_w) #Open new file # Copy dimensions for d in infile.dimensions: outfile.createDimension(d, infile.dimensions[d]) # Copy variable definitions for name in infile.variables: var = infile.variables[name] outfile.createVariable(name, var.dtype.char, var.dimensions) # Copy the static variables for name in infile.variables: if name == 'time' or name == 'stage': pass else: outfile.variables[name][:] = infile.variables[name][:] # Copy selected timesteps time = infile.variables['time'] stage = infile.variables['stage'] newtime = outfile.variables['time'] newstage = outfile.variables['stage'] if last is None: last = len(time) selection = range(first, last, step) for i, j in enumerate(selection): log.critical('Copying timestep %d of %d (%f)' % (j, last-first, time[j])) newtime[i] = time[j] newstage[i,:] = stage[j,:] # Close infile.close() outfile.close()
def urs2nc(basename_in='o', basename_out='urs'): """Convert the 3 urs files to 4 nc files. The name of the urs file names must be; [basename_in]-z-mux [basename_in]-e-mux [basename_in]-n-mux """ files_in = [basename_in + WAVEHEIGHT_MUX_LABEL, basename_in + EAST_VELOCITY_LABEL, basename_in + NORTH_VELOCITY_LABEL] files_out = [basename_out + '_ha.nc', basename_out + '_ua.nc', basename_out + '_va.nc'] quantities = ['HA', 'UA', 'VA'] #if os.access(files_in[0]+'.mux', os.F_OK) == 0 : for i, file_name in enumerate(files_in): if os.access(file_name, os.F_OK) == 0: if os.access(file_name + '.mux', os.F_OK) == 0 : msg = 'File %s does not exist or is not accessible' % file_name raise IOError, msg else: files_in[i] += '.mux' log.critical("file_name %s" % file_name) hashed_elevation = None for file_in, file_out, quantity in map(None, files_in, files_out, quantities): lonlatdep, lon, lat, depth = _binary_c2nc(file_in, file_out, quantity) if hashed_elevation == None: elevation_file = basename_out + '_e.nc' write_elevation_nc(elevation_file, lon, lat, depth) hashed_elevation = myhash(lonlatdep) else: msg = "The elevation information in the mux files is inconsistent" assert hashed_elevation == myhash(lonlatdep), msg files_out.append(elevation_file) return files_out
def urs2nc(basename_in='o', basename_out='urs'): """Convert the 3 urs files to 4 nc files. The name of the urs file names must be; [basename_in]-z-mux [basename_in]-e-mux [basename_in]-n-mux """ files_in = [basename_in + WAVEHEIGHT_MUX_LABEL, basename_in + EAST_VELOCITY_LABEL, basename_in + NORTH_VELOCITY_LABEL] files_out = [basename_out + '_ha.nc', basename_out + '_ua.nc', basename_out + '_va.nc'] quantities = ['HA', 'UA', 'VA'] #if os.access(files_in[0]+'.mux', os.F_OK) == 0 : for i, file_name in enumerate(files_in): if os.access(file_name, os.F_OK) == 0: if os.access(file_name + '.mux', os.F_OK) == 0 : msg = 'File %s does not exist or is not accessible' % file_name raise IOError, msg else: files_in[i] += '.mux' log.critical("file_name %s" % file_name) hashed_elevation = None for file_in, file_out, quantity in map(None, files_in, files_out, quantities): lonlatdep, lon, lat, depth = _binary_c2nc(file_in, file_out, quantity) if hashed_elevation is None: elevation_file = basename_out + '_e.nc' write_elevation_nc(elevation_file, lon, lat, depth) hashed_elevation = myhash(lonlatdep) else: msg = "The elevation information in the mux files is inconsistent" assert hashed_elevation == myhash(lonlatdep), msg files_out.append(elevation_file) return files_out
def reduce_pts(infile, outfile, max_points, verbose = False): """Reduce a points file until less than given size. Reduces a points file by removing every second point until the # of points is less than max_points. """ # check out pts2rectangular in least squares, and the use of reduction. # Maybe it does the same sort of thing? point_atts = _read_pts_file(infile) while point_atts['pointlist'].shape[0] > max_points: if verbose: log.critical("point_atts['pointlist'].shape[0]") point_atts = half_pts(point_atts) export_points_file(outfile, point_atts)
def reduce_pts(infile, outfile, max_points, verbose=False): """Reduce a points file until less than given size. Reduces a points file by removing every second point until the # of points is less than max_points. """ # check out pts2rectangular in least squares, and the use of reduction. # Maybe it does the same sort of thing? point_atts = _read_pts_file(infile) while point_atts['pointlist'].shape[0] > max_points: if verbose: log.critical("point_atts['pointlist'].shape[0]") point_atts = half_pts(point_atts) export_points_file(outfile, point_atts)
def line_intersect(triangles, line, verbose=False): """Determine which of a list of trianglee intersect a line """ line = ensure_numeric(line) triangles = ensure_numeric(triangles) M = triangles.shape[0]/3 # Number of triangles indices = num.zeros(M, num.int) count = _line_intersect(line, triangles, indices) if verbose: log.critical('Found %d triangles (out of %d) that intersect line' % (count, M)) return indices[:count]
def not_line_intersect(triangles, line, verbose=False): """Determine if a polyline and triangle overlap """ line = ensure_numeric(line) triangles = ensure_numeric(triangles) M = triangles.shape[0]/3 # Number of triangles indices = num.zeros(M, num.int) count = _line_intersect(line, triangles, indices) if verbose: log.critical('Found %d triangles (out of %d) that intersect the line' % (count, M)) return indices[count:]
def not_polygon_overlap(triangles, polygon, verbose=False): """Determine if a polygon and triangle overlap """ polygon = ensure_numeric(polygon) triangles = ensure_numeric(triangles) M = triangles.shape[0]/3 # Number of triangles indices = num.zeros(M, num.int) count = _polygon_overlap(polygon, triangles, indices) if verbose: log.critical('Found %d triangles (out of %d) that polygon' % (count, M)) return indices[count:]
def produce_half_point_files(infile, max_points, delimiter, verbose=False): point_atts = _read_pts_file(infile) root, ext = splitext(infile) outfiles = [] if verbose: log.critical("# of points", point_atts['pointlist'].shape[0]) while point_atts['pointlist'].shape[0] > max_points: point_atts = half_pts(point_atts) if verbose: log.critical("# of points = %s" % str(point_atts['pointlist'].shape[0])) outfile = root + delimiter + str(point_atts['pointlist'].shape[0]) + ext outfiles.append(outfile) export_points_file(outfile, point_atts) return outfiles
def not_polygon_overlap(triangles, polygon, verbose=False): """Determine if a polygon and triangle overlap """ polygon = ensure_numeric(polygon) triangles = ensure_numeric(triangles) M = old_div(triangles.shape[0], 3) # Number of triangles indices = num.zeros(M, num.int) count = _polygon_overlap(polygon, triangles, indices) if verbose: log.critical('Found %d triangles (out of %d) that polygon' % (count, M)) return indices[count:]
def line_intersect(triangles, line, verbose=False): """Determine which of a list of trianglee intersect a line """ line = ensure_numeric(line) triangles = ensure_numeric(triangles) M = old_div(triangles.shape[0], 3) # Number of triangles indices = num.zeros(M, num.int) count = _line_intersect(line, triangles, indices) if verbose: log.critical('Found %d triangles (out of %d) that intersect line' % (count, M)) return indices[:count]
def not_line_intersect(triangles, line, verbose=False): """Determine if a polyline and triangle overlap """ line = ensure_numeric(line) triangles = ensure_numeric(triangles) M = old_div(triangles.shape[0], 3) # Number of triangles indices = num.zeros(M, num.int) count = _line_intersect(line, triangles, indices) if verbose: log.critical('Found %d triangles (out of %d) that intersect the line' % (count, M)) return indices[count:]
def run_simulation(vtype, sim_obj): '''Run a simulation. Returns True if all went well, else False. ''' # untar the object tar_path = os.path.join(Local_Data_Directory, sim_obj) log.info('Untarring %s in directory %s ...' % (tar_path, Local_Data_Directory)) untar_file(tar_path, target_dir=Local_Data_Directory) # modify project.py template log.debug("Creating '%s' version of project.py" % vtype) fd = open('project_template.py', 'r') project = fd.readlines() fd.close() new_project = [] for line in project: new_project.append(line.replace('#!SETUP!#', vtype.lower())) fd = open('project.py', 'w') fd.write(''.join(new_project)) fd.close() # import new project.py import project # run the simulation, produce SWW file log.info('Running the simulation ...') cmd = 'python run_model.py > %s' % RUNMODEL_STDOUT log.debug("run_simulation: doing '%s'" % cmd) res = os.system(cmd) log.debug("run_simulation: res=%d" % res) # 'unimport' project.py del project # check result if res != 0: log.critical('Simulation failed, check log') return res == 0
def __call__(self, pts_x, pts_y): """Implement the 'callable' property of Polygon_function. x List of x coordinates of points ot interest. y List of y coordinates of points ot interest. """ pts_x = num.array(pts_x, num.float) pts_y = num.array(pts_y, num.float) # x and y must be one-dimensional and same length assert len(pts_x.shape) == 1 and len(pts_y.shape) == 1 pts_len = pts_x.shape[0] assert pts_y.shape[0] == pts_len, 'x and y must be same length' points = num.ascontiguousarray(num.concatenate((pts_x[:, num.newaxis], pts_y[:, num.newaxis]), axis = 1 )) if callable(self.default): result = self.default(pts_x, pts_y) else: result = num.ones(pts_len, num.float) * self.default for polygon, value in self.regions: indices = inside_polygon(points, polygon) # FIXME: This needs to be vectorised if callable(value): for i in indices: xx = num.array([pts_x[i]]) yy = num.array([pts_y[i]]) result[i] = value(xx, yy)[0] else: for i in indices: result[i] = value if len(result) == 0: msg = ('Warning: points provided to Polygon function did not fall ' 'within its regions in [%.2f, %.2f], y in [%.2f, %.2f]' % (min(pts_x), max(pts_x), min(pts_y), max(pts_y))) log.critical(msg) return result
def __call__(self, pts_x, pts_y): """Implement the 'callable' property of Polygon_function. x List of x coordinates of points ot interest. y List of y coordinates of points ot interest. """ pts_x = num.array(pts_x, num.float) pts_y = num.array(pts_y, num.float) # x and y must be one-dimensional and same length assert len(pts_x.shape) == 1 and len(pts_y.shape) == 1 pts_len = pts_x.shape[0] assert pts_y.shape[0] == pts_len, 'x and y must be same length' points = num.ascontiguousarray( num.concatenate((pts_x[:, num.newaxis], pts_y[:, num.newaxis]), axis=1)) if callable(self.default): result = self.default(pts_x, pts_y) else: result = num.ones(pts_len, num.float) * self.default for polygon, value in self.regions: indices = inside_polygon(points, polygon) # FIXME: This needs to be vectorised if callable(value): for i in indices: xx = num.array([pts_x[i]]) yy = num.array([pts_y[i]]) result[i] = value(xx, yy)[0] else: for i in indices: result[i] = value if len(result) == 0: msg = ('Warning: points provided to Polygon function did not fall ' 'within its regions in [%.2f, %.2f], y in [%.2f, %.2f]' % (min(pts_x), max(pts_x), min(pts_y), max(pts_y))) log.critical(msg) return result
def evaluate(self, vol_id=None, edge_id=None): """Return linearly interpolated values based on domain.time at midpoint of segment defined by vol_id and edge_id. """ # FIXME (Ole): I think this should be get_time(), see ticket:306 t = self.domain.time if vol_id is not None and edge_id is not None: i = self.boundary_indices[ vol_id, edge_id ] try: res = self.F(t, point_id=i) except Modeltime_too_early, e: raise Modeltime_too_early(e) except Modeltime_too_late, e: if self.default_boundary is None: raise Exception(e) # Reraise exception else: # Pass control to default boundary res = self.default_boundary.evaluate(vol_id, edge_id) # Ensure that result cannot be manipulated # This is a real danger in case the # default_boundary is a Dirichlet type # for instance. res = res.copy() if self.default_boundary_invoked is False: # Issue warning the first time if self.verbose: msg = '%s' %str(e) msg += 'Instead I will use the default boundary: %s\n'\ %str(self.default_boundary) msg += 'Note: Further warnings will be supressed' log.critical(msg) # FIXME (Ole): Replace this crude flag with # Python's ability to print warnings only once. # See http://docs.python.org/lib/warning-filter.html self.default_boundary_invoked = True
def get_intersecting_segments(V, N, polyline, verbose=False): """Internal function to find edges intersected by Polyline Input: V: Vertex coordinates as obtained by mesh.get_vertex_coordinates() N: Number of triangles in mesh polyline - list of points forming a segmented line verbose Output: list of instances of class Triangle_intersection This method is used by the public method get_intersecting_segments(self, polyline) which also contains more documentation. """ msg = 'Polyline must contain at least two points' assert len(polyline) >= 2, msg # For all segments in polyline triangle_intersections = [] for i, point0 in enumerate(polyline[:-1]): point1 = polyline[i+1] if verbose: log.critical('Extracting mesh intersections from line:') log.critical('(%.2f, %.2f) - (%.2f, %.2f)' % (point0[0], point0[1], point1[0], point1[1])) line = [point0, point1] triangle_intersections += _get_intersecting_segments(V, N, line, verbose=verbose) msg = 'No segments found' assert len(triangle_intersections) > 0, msg return triangle_intersections
def calc_grid_values(nrows, ncols, cellsize, NODATA_value, x, y, norms, volumes, result, grid_values): grid_points = num.zeros((ncols * nrows, 2), num.float) vertex_points = num.concatenate((x[:, num.newaxis], y[:, num.newaxis]), axis=1) assert len(vertex_points.shape) == 2 for i in xrange(nrows): yg = i * cellsize # if out_ext == '.asc': # yg = i * cellsize # else: # # this will flip the order of the y values for ers # yg = (nrows-i) * cellsize for j in xrange(ncols): xg = j * cellsize k = i * ncols + j grid_points[k, 0] = xg grid_points[k, 1] = yg # Interpolate from anuga.fit_interpolate.interpolate import Interpolate # Remove loners from vertex_points, volumes here vertex_points, volumes = remove_lone_verts(vertex_points, volumes) # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes}) interp = Interpolate(vertex_points, volumes, verbose=verbose) # Interpolate using quantity values if verbose: log.critical('Interpolating') grid_values[:] = interp.interpolate(result, grid_points, NODATA_value=NODATA_value, verbose=verbose).flatten() #print grid_values.shape return
def pmesh_to_domain(file_name=None, mesh_instance=None, use_cache=False, verbose=False): """Convert a pmesh file or a pmesh mesh instance to a bunch of lists that can be used to instanciate a domain object. use_cache: True means that caching is attempted for the computed domain. """ if verbose: log.critical('Pmesh_to_Domain: Initialising') if use_cache is True: from anuga.caching import cache result = cache(_pmesh_to_domain, (file_name, mesh_instance), dependencies=[file_name], verbose=verbose) else: result = apply(_pmesh_to_domain, (file_name, mesh_instance)) if verbose: log.critical('Pmesh_to_Domain: Done') return result
def write_dynamic_quantities(self, outfile, quantities, times, precis = netcdf_float32, verbose = False): """ Write out given quantities to file. """ for q in quantities: outfile.createVariable(q, precis, ('number_of_timesteps', 'number_of_points')) outfile.createVariable(q + Write_sts.RANGE, precis, ('numbers_in_range',)) # Initialise ranges with small and large sentinels. # If this was in pure Python we could have used None sensibly outfile.variables[q+Write_sts.RANGE][0] = max_float # Min outfile.variables[q+Write_sts.RANGE][1] = -max_float # Max # Doing sts_precision instead of Float gives cast errors. outfile.createVariable('time', netcdf_float, ('number_of_timesteps',)) if isinstance(times, (list, num.ndarray)): outfile.variables['time'][:] = times # Store time relative if verbose: log.critical('------------------------------------------------') log.critical('Statistics:') log.critical(' t in [%f, %f], len(t) == %d' % (num.min(times), num.max(times), len(times.flat)))
def write_dynamic_quantities(self, outfile, quantities, times, precis=netcdf_float32, verbose=False): """ Write out given quantities to file. """ for q in quantities: outfile.createVariable(q, precis, ('number_of_timesteps', 'number_of_points')) outfile.createVariable(q + Write_sts.RANGE, precis, ('numbers_in_range', )) # Initialise ranges with small and large sentinels. # If this was in pure Python we could have used None sensibly outfile.variables[q + Write_sts.RANGE][0] = max_float # Min outfile.variables[q + Write_sts.RANGE][1] = -max_float # Max # Doing sts_precision instead of Float gives cast errors. outfile.createVariable('time', netcdf_float, ('number_of_timesteps', )) if isinstance(times, (list, num.ndarray)): outfile.variables['time'][:] = times # Store time relative if verbose: log.critical('------------------------------------------------') log.critical('Statistics:') log.critical(' t in [%f, %f], len(t) == %d' % (num.min(times), num.max(times), len(times.flat)))
def calc_grid_values_old(vertex_points, volumes, result): grid_points = num.zeros ((ncols*nrows, 2), num.float) for i in xrange(nrows): if out_ext == '.asc': yg = i * cellsize else: # this will flip the order of the y values for ers yg = (nrows-i) * cellsize for j in xrange(ncols): xg = j * cellsize k = i*ncols + j grid_points[k, 0] = xg grid_points[k, 1] = yg # Interpolate from anuga.fit_interpolate.interpolate import Interpolate # Remove loners from vertex_points, volumes here vertex_points, volumes = remove_lone_verts(vertex_points, volumes) # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes}) interp = Interpolate(vertex_points, volumes, verbose = verbose) bprint = 0 # Interpolate using quantity values if verbose: log.critical('Interpolating') grid_values = interp.interpolate(bprint, result, grid_points).flatten() outside_indices = interp.get_outside_poly_indices() for i in outside_indices: #print 'change grid_value',NODATA_value grid_values[i] = NODATA_value return grid_values
def calc_grid_values_old(vertex_points, volumes, result): grid_points = num.zeros((ncols * nrows, 2), num.float) for i in range(nrows): if out_ext == '.asc': yg = i * cellsize else: # this will flip the order of the y values for ers yg = (nrows - i) * cellsize for j in range(ncols): xg = j * cellsize k = i * ncols + j grid_points[k, 0] = xg grid_points[k, 1] = yg # Interpolate from anuga.fit_interpolate.interpolate import Interpolate # Remove loners from vertex_points, volumes here vertex_points, volumes = remove_lone_verts(vertex_points, volumes) # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes}) interp = Interpolate(vertex_points, volumes, verbose=verbose) bprint = 0 # Interpolate using quantity values if verbose: log.critical('Interpolating') grid_values = interp.interpolate(bprint, result, grid_points).flatten() outside_indices = interp.get_outside_poly_indices() for i in outside_indices: #print 'change grid_value',NODATA_value grid_values[i] = NODATA_value return grid_values
def get_boundary_values(self, t=None): if t is None: t = self.get_time() try: res = self.function(t) except Modeltime_too_early as e: raise Modeltime_too_early(e) except Modeltime_too_late as e: if self.default_boundary is None: raise Modeltime_too_late(e) # Reraise exception else: # Pass control to default boundary res = self.default_boundary # Ensure that result cannot be manipulated # This is a real danger in case the # default_boundary is a Dirichlet type # for instance. from copy import deepcopy res = deepcopy(res) if self.default_boundary_invoked is False: if self.verbose: # Issue warning the first time msg = '%s' % str(e) msg += 'Instead I will use the default boundary value: %s\n'\ %str(self.default_boundary) msg += 'Note: Further warnings will be suppressed' log.critical(msg) # FIXME (Ole): Replace this crude flag with # Python's ability to print warnings only once. # See http://docs.python.org/lib/warning-filter.html self.default_boundary_invoked = True return res