def test_get_voronoi_latlon(): """ LatlonGridRemap.get_voronoi(): nlat=180, nlon=360 (regular) """ from cube_remap import LatlonGridRemap from util.convert_coord.cart_ll import latlon2xyz nlat, nlon = 180, 360 ll = LatlonGridRemap(nlat, nlon, "regular") ret = ll.get_voronoi(1) expect = [ (-1.5707963267948966, 0), (-1.5447610285607269, 0.026179938779914945), (-1.5447610285607269, 0.008726646259971647), ] expect_xyz = [latlon2xyz(*latlon) for latlon in expect] aa_equal(expect_xyz, ret, 10) ret = ll.get_voronoi(nlon) expect = [ (-1.5447610285607269, -0.00872664625997164), (-1.5447610285607269, 0.008726646259971647), (-1.5274041630712807, 0.008726646259971647), (-1.5274041630712807, -0.00872664625997164), ] expect_xyz = [latlon2xyz(*latlon) for latlon in expect] aa_equal(expect_xyz, ret, 10)
def make_remap_matrix(self, debug=False): dst_obj = self.dst_obj src_obj = self.src_obj dsw_dict = dict() # {dst:[(s,w),(s,w),...],...} if debug: ipoly_dict = dict() # {dst:[(src,ipoly,iarea),...],..} for dst, (lat0,lon0) in enumerate(dst_obj.latlons): dst_poly = dst_obj.get_voronoi(dst) dst_area = area_polygon(dst_poly) inout = pt_in_polygon(dst_poly, latlon2xyz(lat0,lon0)) if inout != 'in': print('dst',dst) print('latlon_in_poly?', inout) idx1, idx2, idx3, idx4 = src_obj.get_surround_idxs(lat0, lon0) candidates = set([idx1, idx2, idx3, idx4]) if -1 in candidates: candidates.remove(-1) checked_srcs = set() dsw_dict[dst] = list() while len(candidates) > 0: #print('\t', checked_srcs) src = candidates.pop() src_poly = src_obj.get_voronoi(src) ipoly = intersect_two_polygons(dst_poly, src_poly) checked_srcs.add(src) if ipoly != None: iarea = area_polygon(ipoly) area_ratio = iarea/dst_area if area_ratio > 1e-10: dsw_dict[dst].append( (src, area_ratio) ) nbrs = set(src_obj.get_neighbors(src)) candidates.update(nbrs - checked_srcs) if debug: ipoly_dict[dst].append( (src,ipoly,iarea) ) if debug: self.save_netcdf_ipoly(ipoly_dict) return self.make_remap_matrix_from_dsw_dict(dsw_dict)
def get_voronoi(self, idx): return [latlon2xyz(np.deg2rad(lat),np.deg2rad(lon)) for lat,lon in self.voronoi_latlons[idx]]