13
13
14
14
class linear_interpolant (object ):
15
15
"""
16
- Returns a linear interpolation over the full GB data set.
16
+ Returns a linear interpolation and optionally its gradient over the full
17
+ GB data set.
17
18
18
- The returned function takes two arguments ``x`` and ``y`` (both in metres)
19
- and returns an interpolated height ``z`` (in meters).
19
+ When ``grad`` is set to ``False`` (by default), the returned function
20
+ takes two arguments ``x`` and ``y`` (both in metres) and returns an
21
+ interpolated height ``z`` (in meters).
22
+
23
+ When ``grad`` is set to ``True``, the returned function takes two
24
+ arguments ``x`` and ``y`` (both in metres) and returns a tuple ``(z, g)``,
25
+ where ``z`` is an interpolated height (in meters) and ``g`` is a tuple
26
+ ``(dz/dx, dz/dy)``.
20
27
21
28
The height for each grid point ``(i, j)`` is assumed to be in the center of
22
29
the square from ``(i, j)`` to ``(i + 1, j + 1)``.
@@ -26,13 +33,18 @@ class linear_interpolant(object):
26
33
f = linear_interpolation()
27
34
print(f(1000, 500))
28
35
36
+ f_grad = linear_interpolation(grad=True)
37
+ z, (gx, gy) = f_grad(1000, 500)
38
+ print(f"Height: {z:.2f} m, gradient: ({gx:.2f} m/m, {gy:.2f} m/m)")
39
+
29
40
"""
30
41
# Note: This is technically a class, but used as a function here so
31
42
# following the underscore naming convention.
32
43
33
- def __init__ (self ):
44
+ def __init__ (self , grad = False ):
34
45
self ._heights = nevis .gb ()
35
46
self ._resolution = nevis .spacing ()
47
+ self .grad = grad
36
48
37
49
def __call__ (self , x , y ):
38
50
ny , nx = self ._heights .shape
@@ -65,7 +77,13 @@ def __call__(self, x, y):
65
77
f2 = np .where (h12 == h22 , h12 , (x2 - x ) * h12 + (x - x1 ) * h22 )
66
78
67
79
# Final result
68
- return np .where (f1 == f2 , f1 , (y2 - y ) * f1 + (y - y1 ) * f2 )
80
+ f = float (np .where (f1 == f2 , f1 , (y2 - y ) * f1 + (y - y1 ) * f2 ))
81
+ if self .grad :
82
+ # Gradient of the interpolant
83
+ g = (h21 - h11 ) / self ._resolution , (h12 - h11 ) / self ._resolution
84
+ return f , g
85
+ else :
86
+ return f
69
87
70
88
71
89
def spline (verbose = False ):
0 commit comments