def extend_to_global(var, src_grid, global_grid, arctic_filler=None):
"""
Use nearest neighbour to extend obs/source over the whole globe, including land.
"""
# Create masked array of the correct shape.
tmp = np.zeros((global_grid.num_levels, global_grid.num_lat_points,
global_grid.num_lon_points))
new_data = np.ma.array(tmp, mask=global_grid.mask, copy=True)
# Mask everything by default. The code below fills masked values with
# nearest neighbour.
new_data.mask[:] = True
# Drop obs data into new grid at correct location
lat_min_idx = find_nearest_index(global_grid.y_t[:, 0], np.min(src_grid.y_t[:]))
if np.max(global_grid.y_t[:]) <= np.max(src_grid.y_t[:]):
new_data[:, lat_min_idx:, :] = var[:]
else:
lat_max_idx = find_nearest_index(global_grid.y_t[:, 0], np.max(src_grid.y_t[:]))
new_data[:, lat_min_idx:lat_max_idx+1, :] = var[:]
# Fill in missing values on each level with nearest neighbour
for l in range(var.shape[0]):
ind = nd.distance_transform_edt(new_data[l, :, :].mask,
return_distances=False,
return_indices=True)
tmp = new_data[l, :, :]
tmp = tmp[tuple(ind)]
new_data[l, :, :] = tmp[:, :]
return new_data
评论列表
文章目录