-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpolate2DNumpy.py
More file actions
57 lines (41 loc) · 1.75 KB
/
interpolate2DNumpy.py
File metadata and controls
57 lines (41 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Function to fill gaps(values '-inf') in a 2D Numpy Array
author: Ignace Pelckmans
(University of Antwerp, Belgium)
"""
import numpy as np
from scipy.interpolate import griddata
def interpNumpy(arr, method = 'linear'):
"""
Function fill gaps (values -inf) in a 2D Numpy array
example:
2 2 2 2 3 3 3 2 2 2 2 3 3 3
2 2 2 2 3 3 3 2 2 2 2 3 3 3
-inf -inf -inf -inf -inf -inf -inf ==> 1 1 1 1 1.5 1.5 1.5
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
Args:
arr: (Required) Numpy array with dimensions m x n with gaps indicated with float('-inf')
method: (Optional, defaults to 'linear') String with interpolation method, options: ‘linear’, ‘nearest’, ‘cubic’
Returns:
Numpy array with dimensions m x n with interpolated gaps
"""
# get array dimensions
rows, cols = np.shape(arr)
# get array values as a flat array
Z = arr.flatten()
# meshgrid over the entire rasterized polygon array
x = np.arange(arr.shape[1])
y = np.arange(arr.shape[0])
X, Y = np.meshgrid(x,y)
X, Y = X.flatten(), Y.flatten()
XY = np.zeros([len(Y), 2])
XY[:,0] = X; XY[:,1] = Y
# interpolate over the entire array
Ti = griddata((X[Z > float('-inf')], Y[Z > float('-inf')]), Z[Z > float('-inf')], (X[Z == float('-inf')], Y[Z == float('-inf')]),
method='linear')
# replace -inf in flat array values
Z[Z == float('-inf')] = Ti
# Reshape the flatted array
arr_interp = np.reshape(Z, [rows, cols])
return arr_interp