-
Notifications
You must be signed in to change notification settings - Fork 4
/
resample.py
executable file
·269 lines (231 loc) · 9.51 KB
/
resample.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from pyresample import geometry, kd_tree
import numpy as np
def resample_to_grid_only_valid_return(
input_data, src_lon, src_lat, target_lon, target_lat,
methods='nn', weight_funcs=None, min_neighbours=1, search_rad=18000,
neighbours=8, fill_values=None):
"""
resamples data from dictionary of numpy arrays using pyresample
to given grid.
Searches for the neighbours and then resamples the data to the grid given
in to grid if at least min_neighbours neighbours are found
Parameters
----------
input_data : dict of numpy.arrays
Data to resample
src_lon : numpy.array
longitudes of the input data
src_lat : numpy.array
src_latitudes of the input data
target_lon : numpy.array
longitudes of the output data
target_src_lat : numpy.array
src_latitudes of the output data
methods : string or dict, optional
method of spatial averaging. this is given to pyresample
and can be
'nn' : nearest neighbour
'custom' : custom weight function has to be supplied in weight_funcs
see pyresample documentation for more details
can also be a dictionary with a method for each array in input data dict
weight_funcs : function or dict of functions, optional
if method is 'custom' a function like func(distance) has to be given
can also be a dictionary with a function for each array in input data dict
min_neighbours: int, optional
if given then only points with at least this number of neighbours will be
resampled
Default : 1
search_rad : float, optional
search radius in meters of neighbour search
Default : 18000
neighbours : int, optional
maximum number of neighbours to look for for each input grid point
Default : 8
fill_values : number or dict, optional
if given the output array will be filled with this value if no valid
resampled value could be computed, if not a masked array will be returned
can also be a dict with a fill value for each variable
Returns
-------
data : dict of numpy.arrays
resampled data on part of the target grid over which data was found
mask: numpy.ndarray
boolean mask into target grid that specifies where data was resampled
Raises
------
ValueError :
if empty dataset is resampled
"""
output_data = {}
if target_lon.ndim == 2:
target_lat = target_lat.ravel()
target_lon = target_lon.ravel()
input_swath = geometry.SwathDefinition(src_lon, src_lat)
output_swath = geometry.SwathDefinition(target_lon, target_lat)
(valid_input_index,
valid_output_index,
index_array,
distance_array) = kd_tree.get_neighbour_info(input_swath,
output_swath,
search_rad,
neighbours=neighbours)
# throw away points with less than min_neighbours neighbours
# find points with valid neighbours
# get number of found neighbours for each grid point/row
if neighbours > 1:
nr_neighbours = np.isfinite(distance_array).sum(1)
neigh_condition = nr_neighbours >= min_neighbours
mask = np.invert(neigh_condition)
enough_neighbours = np.nonzero(neigh_condition)[0]
if neighbours == 1:
nr_neighbours = np.isfinite(distance_array)
neigh_condition = nr_neighbours >= min_neighbours
mask = np.invert(neigh_condition)
enough_neighbours = np.nonzero(neigh_condition)[0]
distance_array = np.reshape(
distance_array, (distance_array.shape[0], 1))
index_array = np.reshape(index_array, (index_array.shape[0], 1))
if enough_neighbours.size == 0:
raise ValueError(
"No points with at least %d neighbours found" % min_neighbours)
# remove neighbourhood info of input grid points that have no neighbours
# to not have to resample to whole output grid for small input grid file
distance_array = distance_array[enough_neighbours, :]
index_array = index_array[enough_neighbours, :]
valid_output_index = valid_output_index[enough_neighbours]
for param in input_data:
data = input_data[param]
if type(methods) == dict:
method = methods[param]
else:
method = methods
if method != 'nn':
if type(weight_funcs) == dict:
weight_func = weight_funcs[param]
else:
weight_func = weight_funcs
else:
weight_func = None
neigh_slice = slice(None, None, None)
# check if method is nn, if so only use first row of index_array and
# distance_array
if method == 'nn':
neigh_slice = (slice(None, None, None), 0)
if type(fill_values) == dict:
fill_value = fill_values[param]
else:
fill_value = fill_values
output_array = kd_tree.get_sample_from_neighbour_info(
method,
enough_neighbours.shape,
data,
valid_input_index,
valid_output_index,
index_array[neigh_slice],
distance_array[neigh_slice],
weight_funcs=weight_func,
fill_value=fill_value)
output_data[param] = output_array
return output_data, mask
def resample_to_grid(input_data, src_lon, src_lat, target_lon, target_lat,
methods='nn', weight_funcs=None,
min_neighbours=1, search_rad=18000, neighbours=8,
fill_values=None):
"""
resamples data from dictionary of numpy arrays using pyresample
to given grid.
Searches for the neighbours and then resamples the data
to the grid given in togrid if at least
min_neighbours neighbours are found
Parameters
----------
input_data : dict of numpy.arrays
src_lon : numpy.array
longitudes of the input data
src_lat : numpy.array
src_latitudes of the input data
target_lon : numpy.array
longitudes of the output data
target_src_lat : numpy.array
src_latitudes of the output data
methods : string or dict, optional
method of spatial averaging. this is given to pyresample
and can be
'nn' : nearest neighbour
'custom' : custom weight function has to be supplied in weight_funcs
see pyresample documentation for more details
can also be a dictionary with a method for each array in input data dict
weight_funcs : function or dict of functions, optional
if method is 'custom' a function like func(distance) has to be given
can also be a dictionary with a function for each array in input data dict
min_neighbours: int, optional
if given then only points with at least this number of neighbours will be
resampled
Default : 1
search_rad : float, optional
search radius in meters of neighbour search
Default : 18000
neighbours : int, optional
maximum number of neighbours to look for for each input grid point
Default : 8
fill_values : number or dict, optional
if given the output array will be filled with this value if no valid
resampled value could be computed, if not a masked array will be returned
can also be a dict with a fill value for each variable
Returns
-------
data : dict of numpy.arrays
resampled data on given grid
Raises
------
ValueError :
if empty dataset is resampled
"""
output_data = {}
output_shape = target_lat.shape
if target_lon.ndim == 2:
target_lat = target_lat.ravel()
target_lon = target_lon.ravel()
resampled_data, mask = resample_to_grid_only_valid_return(input_data,
src_lon, src_lat,
target_lon, target_lat,
methods=methods,
weight_funcs=weight_funcs,
min_neighbours=min_neighbours,
search_rad=search_rad,
neighbours=neighbours)
not_masked = ~mask
for param in input_data:
data = resampled_data[param]
orig_data = input_data[param]
if type(fill_values) == dict:
fill_value = fill_values[param]
else:
fill_value = fill_values
# construct arrays in output grid form
if fill_value is not None:
output_array = np.zeros(
target_lat.shape, dtype=orig_data.dtype) + fill_value
else:
output_array = np.zeros(target_lat.shape, dtype=orig_data.dtype)
output_array = np.ma.array(output_array, mask=mask)
output_array[not_masked] = data
output_data[param] = output_array.reshape(output_shape)
return output_data
def hamming_window(radius, distances):
"""
Hamming window filter.
Parameters
----------
radius : float32
Radius of the window.
distances : numpy.ndarray
Array with distances.
Returns
-------
weights : numpy.ndarray
Distance weights.
"""
alpha = 0.54
weights = alpha + (1 - alpha) * np.cos(np.pi / radius * distances)
return weights