Skip to content

Commit 6b765c7

Browse files
authored
Merge pull request #677 from rjleveque/center_points
utility to center gauges or other points in computational cells
2 parents 93293b2 + bc257f0 commit 6b765c7

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
The function adjust_xy will center a point in a finite volume grid cell of
3+
a given resolution on a given domain.
4+
5+
This can be used in particular to take adjust an approximate point
6+
(x_desired, y_desired) where you want a computational gauge, to obtain
7+
a point that lies exactly in the center of a grid cell at a given resolution.
8+
This eliminates the need to interpolate between cell values in GeoClaw output,
9+
which has issues for cells near the shoreline as described at
10+
https://www.clawpack.org/nearshore_interp.html
11+
12+
Functions:
13+
14+
- adjust: utility function to adjust in one dimension (x or y)
15+
- adjust_xy: the function to call to center a point in both x and y.
16+
- test: a simple example
17+
18+
"""
19+
20+
import numpy as np
21+
22+
def adjust(z_desired, z_edge, dz, verbose=False):
23+
"""
24+
Given a desired location z (either x or y) for a gauge or other point
25+
of interest, adjust the location is it is offset (integer + 1/2)*dz
26+
from z_edge, an arbitrary edge location (e.g. an edge of the
27+
computational domain or any point offset integer*dz from the domain edge).
28+
This will put the new location in the center of a finite volume cell
29+
provided this point is in a patch with resolution dz.
30+
"""
31+
# z represents either x or y
32+
i = np.round((z_desired-z_edge - 0.5*dz)/dz)
33+
z_centered = z_edge + (i+0.5)*dz
34+
if verbose:
35+
zshift = z_centered - z_desired
36+
zfrac = zshift / dz
37+
print("adjusted from %15.9f" % z_desired)
38+
print(" to %15.9f" % z_centered)
39+
print(" shifted by %15.9f = %.3f*dz" % (zshift,zfrac))
40+
return z_centered
41+
42+
def adjust_xy(x_desired, y_desired, x_edge, y_edge, dx, dy, verbose=False):
43+
"""
44+
Given a desired location (or array of locations) in 2D, center
45+
so that the point(s) are at the centers of cells of size dx by dy,
46+
with cell edges that are integer multiples of dx,dy away from
47+
from x_edge,y_edge (e.g. the edges of the computational domain or
48+
any other points offset by integer multiples of dx,dy from the edges.
49+
50+
This will put the new location in the center of a finite volume cell
51+
provided this point is in a grid patch with resolution dx,dy.
52+
53+
:Input:
54+
55+
- x_desired, y_desired: single floats or arrays of floats, the desired
56+
locations
57+
- x_edge, y_edge (float) the edges of the computational domain
58+
(or any other points offset by integer multiples of dx,dy from the edges)
59+
- dx, dy (float): the grid resolution on which the point(s) should be
60+
centered
61+
62+
:Output:
63+
64+
- xc,yc: centered points that lie within dx/2, dy/2 of the desired
65+
location(s) and with (xc - x_edge)/dx and (yc - y_edge)/dy
66+
equal to an integer + 0.5,
67+
"""
68+
69+
# convert single values or lists/tuples to numpy arrays
70+
x_desired = np.array(x_desired, ndmin=1)
71+
y_desired = np.array(y_desired, ndmin=1)
72+
73+
assert len(x_desired) == len(y_desired), \
74+
'*** lengths of x_desired, y_desired do not match'
75+
76+
x_centered = []
77+
y_centered = []
78+
for i in range(len(x_desired)):
79+
x = adjust(x_desired[i], x_edge, dx, verbose=verbose)
80+
y = adjust(y_desired[i], y_edge, dy, verbose=verbose)
81+
x_centered.append(x)
82+
y_centered.append(y)
83+
84+
if len(x_centered) == 1:
85+
return float(x_centered[0]), float(y_centered[0])
86+
else:
87+
return np.array(x_centered), np.array(y_centered)
88+
89+
def test():
90+
91+
x_desired = [-122.01, -122.02]
92+
y_desired = [47.001, 47.002]
93+
94+
# grid resolution on which to center point:
95+
dx = 1/(3*3600.)
96+
97+
# lower left edge of computational domain:
98+
x_edge = -123.
99+
y_edge = 45.
100+
101+
print('Desired x = ',x_desired)
102+
print('Desired y = ',y_desired)
103+
104+
xc,yc = adjust_xy(x_desired,y_desired,x_edge,y_edge,dx,dx,verbose=True)
105+
106+
print('Centered x = ',xc)
107+
print('Offsets in x in units of 1/3 arcsec: ', (xc-x_edge)*3*3600)
108+
print('Centered y = ',yc)
109+
print('Offsets in y in units of 1/3 arcsec: ', (yc-y_edge)*3*3600)
110+
111+
if __name__ == '__main__':
112+
test()

0 commit comments

Comments
 (0)