From e0010485301b863ee33a1bba18305b2500524f3d Mon Sep 17 00:00:00 2001 From: Corson-Dosch Date: Fri, 20 Aug 2021 11:38:53 -0500 Subject: [PATCH] feat(structuredgrid): add helper function to to get ij from point(s) in real world coords. Same as the function of the same name in the now depreciated SpatialReference class. --- flopy/discretization/structuredgrid.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/flopy/discretization/structuredgrid.py b/flopy/discretization/structuredgrid.py index 14ca47a9c9..9980034a29 100644 --- a/flopy/discretization/structuredgrid.py +++ b/flopy/discretization/structuredgrid.py @@ -904,6 +904,30 @@ def get_node(self, lrc_list): v.append(node) return v + def get_ij(self, x, y): + """Return the row and column of a point or sequence of points + in real-world coordinates. + + Parameters + ---------- + x : scalar or sequence of x coordinates + y : scalar or sequence of y coordinates + + Returns + ------- + i : row or sequence of rows (zero-based) + j : column or sequence of columns (zero-based) + """ + if np.isscalar(x): + c = (np.abs(self.xcellcenters[0] - x)).argmin() + r = (np.abs(self.ycellcenters[:, 0] - y)).argmin() + else: + xcp = np.array([self.xcellcenters[0]] * (len(x))) + ycp = np.array([self.ycellcenters[:, 0]] * (len(x))) + c = (np.abs(xcp.transpose() - x)).argmin(axis=0) + r = (np.abs(ycp.transpose() - y)).argmin(axis=0) + return r, c + def plot(self, **kwargs): """ Plot the grid lines.