def dmsp_map_interpolate_NN_smooth_great_circle(lat_dmsp, lon_dmsp, lat_map, lon_map, Obs_map, k=5, tol=1.5):
    '\n    generic function to spatially interpolate with the SSJ data using nearest neighbors using some arbirtary distance tolerance\n    '
    tol = np.deg2rad(tol)
    dmsp_points = np.deg2rad(np.hstack((lat_dmsp.flatten().reshape((- 1), 1), lon_dmsp.flatten().reshape((- 1), 1))))
    map_points = np.deg2rad(np.hstack((lat_map.flatten().reshape((- 1), 1), lon_map.flatten().reshape((- 1), 1))))
    N_points = dmsp_points.shape[0]
    obs_val = Obs_map.flatten()
    model = sklearn.neighbors.NearestNeighbors(n_neighbors=k, radius=tol, metric='haversine')
    model.fit(map_points)
    neighbors = model.kneighbors(dmsp_points, return_distance=True)
    obs_interp = np.empty(N_points)
    for i in range(N_points):
        distances = neighbors[0][i]
        inds = neighbors[1][i]
        weights = (distances / np.nansum(distances))
        obs_interp[i] = np.nansum((obs_val[inds] * weights))
    return obs_interp
generic function to spatially interpolate with the SSJ data using nearest neighbors using some arbirtary distance tolerance
def dmsp_map_interpolate_NN_smooth_great_circle(lat_dmsp, lon_dmsp, lat_map, lon_map, Obs_map, k=5, tol=1.5):
    '\n    \n    '
    tol = np.deg2rad(tol)
    dmsp_points = np.deg2rad(np.hstack((lat_dmsp.flatten().reshape((- 1), 1), lon_dmsp.flatten().reshape((- 1), 1))))
    map_points = np.deg2rad(np.hstack((lat_map.flatten().reshape((- 1), 1), lon_map.flatten().reshape((- 1), 1))))
    N_points = dmsp_points.shape[0]
    obs_val = Obs_map.flatten()
    model = sklearn.neighbors.NearestNeighbors(n_neighbors=k, radius=tol, metric='haversine')
    model.fit(map_points)
    neighbors = model.kneighbors(dmsp_points, return_distance=True)
    obs_interp = np.empty(N_points)
    for i in range(N_points):
        distances = neighbors[0][i]
        inds = neighbors[1][i]
        weights = (distances / np.nansum(distances))
        obs_interp[i] = np.nansum((obs_val[inds] * weights))
    return obs_interp