-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path_conservative.pyx
More file actions
128 lines (107 loc) · 4.89 KB
/
Copy path_conservative.pyx
File metadata and controls
128 lines (107 loc) · 4.89 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import numpy as np
cimport numpy as np
# Must be called to use the C-API with Numpy.
np.import_array()
cdef calculate_weights(np.ndarray[np.float64_t, ndim=2] src_point,
np.ndarray[np.float64_t, ndim=2] tgt_point):
"""
Calculate weights for a given point.
The following visually illustrates the calculation::
src_min src_max
|----------| : Source
tgt_min tgt_max
|------------| : Target
|----------| : Delta (src_max - src_min)
|----| : Overlap (between src & tgt)
weight = overlap / delta
Parameters
----------
src_point (2d double array) - Source point (at a specific location).
tgt_point (2d double array) - Target point (at a specific location).
Returns
-------
2d double array - Weights corresponding to shape [src_point.shape[0],
tgt_point.shape[0]].
"""
cdef Py_ssize_t src_ind, tgt_ind
cdef np.float64_t delta, weight
cdef np.ndarray[np.float64_t, ndim=2] weights
cdef np.ndarray[np.float64_t, ndim=1] src_cell, tgt_cell
weights = np.zeros([src_point.shape[0], tgt_point.shape[0]],
dtype=np.float64)
for src_ind, src_cell in enumerate(src_point):
delta = src_cell.max() - src_cell.min()
for tgt_ind, tgt_cell in enumerate(tgt_point):
weight = (min(src_cell.max(), tgt_cell.max()) -
max(src_cell.min(), tgt_cell.min())) / float(delta)
if weight > 0:
weights[src_ind, tgt_ind] = weight
return weights
cdef apply_weights(np.ndarray[np.float64_t, ndim=3] src_point,
np.ndarray[np.float64_t, ndim=3] tgt_point,
np.ndarray[np.float64_t, ndim=3] src_data):
"""
Perform conservative interpolation.
Conservation interpolation of a dataset between a provided source
coordinate and a target coordinate. Where no source cells contribute to a
target cell, a np.nan value is returned.
Parameters
----------
src_points (3d double array) - Source coordinate, taking the form
[axis_interpolation, z_varying, 2].
tgt_points (3d double array) - Target coordinate, taking the form
[axis_interpolation, z_varying, 2].
src_data (3d double array) - The source data, the phenomenon data to be
interpolated from ``src_points`` to ``tgt_points``. Taking the form
[broadcasting_dims, axis_interpolation, z_varying].
Returns
-------
3d double array - Interpolated result over target levels (``tgt_points``).
Taking the form [broadcasting_dims, axis_interpolation, z_varying].
"""
cdef Py_ssize_t ind
cdef np.ndarray[np.float64_t, ndim=3] results, weighted_contrib
cdef np.ndarray[np.float64_t, ndim=2] weights
results = np.zeros(
[src_data.shape[0], tgt_point.shape[0], src_data.shape[2]],
dtype='float64')
# Calculate and apply weights
for ind in range(src_data.shape[2]):
weights = calculate_weights(src_point[:, ind], tgt_point[:, ind])
if not (weights.sum(axis=1) == 1).all():
msg = ('Weights calculation yields a less than conservative '
'result. Aborting.')
raise ValueError(msg)
weighted_contrib = weights * src_data[..., ind][..., None]
results[..., ind] = (
np.nansum(weighted_contrib, axis=1))
# Return nan values for those target cells, where there is any
# contribution of nan data from the source data.
results[..., ind][
((weights > 0) * np.isnan(weighted_contrib)).any(axis=1)] = np.nan
# Return np.nan for those target cells where no source contributes.
results[:, weights.sum(axis=0) == 0, ind] = np.nan
return results
def conservative_interpolation(src_points, tgt_points, src_data):
"""
Perform conservative interpolation.
Conservation interpolation of a dataset between a provided source
coordinate and a target coordinate. All inputs are recast to 64-bit float
arrays before calculation.
Parameters
----------
src_points (3d array) - Source coordinate, taking the form
[axis_interpolation, z_varying, 2].
tgt_points (3d array) - Target coordinate, taking the form
[axis_interpolation, z_varying, 2].
src_data (3d array) - The source data, the phenonenon data to be
interpolated from ``src_points`` to ``tgt_points``. Taking the form
[broadcasting_dims, axis_interpolation, z_varying].
Returns
-------
3d double array - Interpolated result over target levels (``tgt_points``).
Taking the form [broadcasting_dims, axis_interpolation, z_varying].
"""
return apply_weights(src_points.astype('float64'),
tgt_points.astype('float64'),
src_data.astype('float64'))