def _build_nn(self, z, distances, indexes): z = mask_it(z, self._mv_source) # TODO probably we don't need to mask but just an empty array result = mask_it(np.empty((len(distances), ) + np.shape(z[0])), self._mv_target, 1) jinterpol = 0 num_cells = result.size back_char, progress_step = progress_step_and_backchar(num_cells) stdout.write('{}Building coeffs: 0/{} [outs: 0] (0%)'.format( back_char, num_cells)) stdout.flush() idxs = empty((len(indexes), ), fill_value=z.size, dtype=np.int64) # wsum will be saved in intertable outs = 0 for dist, ix in izip(distances, indexes): if jinterpol % progress_step == 0: stdout.write( '{}Building coeffs: {}/{} [outs: {}] ({:.2f}%)'.format( back_char, jinterpol, num_cells, outs, jinterpol * 100. / num_cells)) stdout.flush() if dist <= self.min_upper_bound: wz = z[ix] idxs[jinterpol] = ix else: outs += 1 wz = self._mv_target result[jinterpol] = wz jinterpol += 1 stdout.write('{}{:>100}'.format(back_char, ' ')) stdout.write('{}Building coeffs: {}/{} [outs: {}] (100%)\n'.format( back_char, jinterpol, num_cells, outs)) stdout.flush() return result, idxs
def _build_weights(self, z, distances, indexes, nnear): # TODO CHECK: maybe we don't need to mask here z = mask_it(z, self._mv_source) # no intertable found for inverse distance nnear = 8 # TODO CHECK if we need mask here (maybe just need an empty array) result = mask_it(np.empty((len(distances), ) + np.shape(z[0])), self._mv_target, 1) jinterpol = 0 num_cells = result.size back_char, progress_step = progress_step_and_backchar(num_cells) stdout.write('{}Building coeffs: 0/{} [outs: 0] (0%)'.format( back_char, num_cells)) stdout.flush() # weights will be saved in intertable along with indexes weights = empty((len(distances), ) + (nnear, )) idxs = empty((len(indexes), ) + (nnear, ), fill_value=z.size, dtype=int) empty_array = empty(z[0].shape, self._mv_target) outs = 0 for dist, ix in izip(distances, indexes): if jinterpol % progress_step == 0: stdout.write( '{}Building coeffs: {}/{} [outs: {}] ({:.2f}%)'.format( back_char, jinterpol, num_cells, outs, jinterpol * 100. / num_cells)) stdout.flush() if dist[0] <= 1e-10: wz = z[ix[0]] # take exactly the point, weight = 1 idxs[jinterpol] = ix weights[jinterpol] = np.array([1., 0., 0., 0.]) elif dist[0] <= self.min_upper_bound: w = ne.evaluate('1 / dist ** 2') sums = ne.evaluate('sum(w)') ne.evaluate('w/sums', out=w) wz = np.dot(w, z[ix]) # weighted values (result) weights[jinterpol] = w idxs[jinterpol] = ix else: outs += 1 weights[jinterpol] = np.array([1., 0., 0., 0.]) wz = empty_array result[jinterpol] = wz jinterpol += 1 stdout.write('{}{:>100}'.format(back_char, ' ')) stdout.write('{}Building coeffs: {}/{} [outs: {}] (100%)\n'.format( back_char, jinterpol, num_cells, outs)) stdout.flush() return result, weights, idxs
def interpolate_with_table(self, intertable, source_values, target_lons, target_lats): xs, ys, idxs = intertable[0], intertable[1], intertable[2] result = np.empty(target_lons.shape) result.fill(self.target_mv) result = mask_it(result, self.target_mv) result[xs, ys] = source_values[idxs] return result
def interpolate(self, source_values, target_lons, target_lats): result = np.empty(target_lons.shape) result.fill(self.target_mv) result = mask_it(result, self.target_mv) if not self.parallel: xs, ys, idxs = grib_nearest(self.gid, target_lats, target_lons, self.target_mv) else: xs, ys, idxs = grib_nearest_parallel(self.gid, target_lats, target_lons, self.target_mv) intertable = np.asarray([xs, ys, idxs], dtype=np.int32) result[xs, ys] = source_values[idxs] return result, intertable
def interpolate_with_table(self, intertable, source_values, target_lons, target_lats): indexes = intertable[ 'indexes'] # first two arrays of this group are target xs and ys indexes coeffs = intertable['coeffs'] v = source_values result = np.empty(target_lons.shape) result.fill(self.target_mv) result = mask_it(result, self.target_mv) xs, ys, idxs1, idxs2, idxs3, idxs4, coeffs1, coeffs2, coeffs3, coeffs4 = indexes[ 0], indexes[1], indexes[2], indexes[3], indexes[4], indexes[ 5], coeffs[0], coeffs[1], coeffs[2], coeffs[3] result[xs, ys] = v[idxs1] * coeffs1 + v[idxs2] * coeffs2 + v[ idxs3] * coeffs3 + v[idxs4] * coeffs4 return result
def interpolate(self, source_values, target_lons, target_lats): v = source_values result = np.empty(target_lons.shape) result.fill(self.target_mv) result = mask_it(result, self.target_mv) if not self.parallel: xs, ys, idxs1, idxs2, idxs3, idxs4, coeffs1, coeffs2, coeffs3, coeffs4 = grib_invdist( self.gid, target_lats, target_lons, self.target_mv) else: xs, ys, idxs1, idxs2, idxs3, idxs4, coeffs1, coeffs2, coeffs3, coeffs4 = grib_invdist_parallel( self.gid, target_lats, target_lons, self.target_mv) indexes = np.asarray([xs, ys, idxs1, idxs2, idxs3, idxs4], dtype=np.int32) coeffs = np.asarray([ coeffs1, coeffs2, coeffs3, coeffs4, np.zeros(coeffs1.shape), np.zeros(coeffs1.shape) ]) intertable = np.rec.fromarrays((indexes, coeffs), names=('indexes', 'coeffs')) result[xs, ys] = v[idxs1] * coeffs1 + v[idxs2] * coeffs2 + v[ idxs3] * coeffs3 + v[idxs4] * coeffs4 return result, intertable