def nearest_node(point, node_set): ''' nearest_node() - Find the nearest node to a point from the node array ** INCOMPLETE ** TODO: NEED TO UPDATE TO READ WELL FILE Parameters ---------- point : tuple (x,y) point node_set : list list of node numbers with x and y of each Returns ------- nearest : int number of nearest node ''' import iwfm as iwfm dist, nearest = 9.9e30, -1 for j in range(0, len(node_set)): line = node_set[j] pt = [line[1], line[2]] # Compute distance between each pair of the two collections of inputs. new_dist = iwfm.distance(point, pt) if dist > new_dist: dist, nearest = new_dist, line[0] return nearest
def nearest(d_nodes, x, y): ''' nearest() - Find the nearest node to a point from a node dictionary Parameters ---------- d_nodes : dictionary key = model node, value = x and y locations x : float x location of point y : float y location of point Returns ------- nearest : int node ID of node closest to (x,y) ''' import iwfm as iwfm point = [x, y] dist = 9.9e30 nearest = -1 for key in d_nodes: pt = d_nodes[key] new_dist = iwfm.distance(point, pt) if dist > new_dist: dist = new_dist nearest = key return nearest
def iwfm_nearest_nodes(filename, node_set): ''' iwfm_nearest_nodes() - Read a point file, calculate the nearest node and distance for each point, and write the results to the output file Parameters ---------- filename : str output file name base node_set : list node IDs and x,y locations Returns ------- number of points processed ''' import iwfm as iwfm output_filename = filename[0:filename.find('.')] + '_nodes.out' with open(output_filename, 'w') as output_file: with open(filename, 'r') as input_file: lines = input_file.read().splitlines() # open and read input file output_file.write(f'{lines[0]}\tNdNear\tNdDist\n') for line_index in range(1, len(lines)): # skip header line temp = lines[line_index].split() nearest = iwfm.nearest_node( [float(temp[1]), float(temp[2])], node_set) dist = iwfm.distance( [float(temp[1]), float(temp[2])], [node_set[nearest][1], node_set[nearest][2]], ) # write out output_file.write(f'{lines[line_index]}\t{nearest}\t{dist}\n') return len(lines) - 1