gridding algorithm