def test_cs_get_voronoi(): ''' CubeGridRemap.get_voronoi(): ne=30, some points ''' from cube_remap import CubeGridRemap from scipy.spatial import voronoi_plot_2d import matplotlib.pyplot as plt ne, ngq = 30, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) uid = 0 xy_vertices, vor = cube.get_voronoi(uid) expect = [( 4.54850726e-03, 3.80070853e-05), \ ( 2.30716873e-03, 3.92011929e-03), \ (-2.30716873e-03, 3.92011929e-03), \ (-4.54850726e-03, 3.80070853e-05), \ (-2.24133853e-03,-3.95812638e-03), \ ( 2.24133853e-03,-3.95812638e-03)] aa_equal(expect, xy_vertices, 10) uid = 1 xy_vertices, vor = cube.get_voronoi(uid) expect = [( 5.98890285e-03,-2.13793346e-03), \ ( 5.01646021e-03, 3.93916864e-03), \ (-2.27448976e-03, 3.93916864e-03), \ (-4.54802687e-03,-7.61894981e-05), \ (-7.97052613e-04,-6.32824066e-03), \ ( 4.91440620e-03,-4.03563236e-03)] aa_equal(expect, xy_vertices, 10)
def test_cs_voronoi_area(): """ CubeGridRemap.get_voronoi(): check the sphere area, ne=3 """ from cube_remap import CubeGridRemap from util.geometry.sphere import area_polygon from math import fsum, pi from util.convert_coord.cart_cs import xyp2xyz ne, ngq = 3, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) area_list = list() for uid in xrange(cube.up_size): # xy_vts, vor_obj = cube.get_voronoi_scipy(uid) # xyzs = [xyp2xyz(x,y,1) for x,y in xy_vts] # area_list.append( area_polygon(xyzs) ) xyzs = cube.get_voronoi(uid) area_list.append(area_polygon(xyzs)) aa_equal(fsum(area_list), 4 * pi, 15) """
def test_get_surround_4_uids(): ''' CubeGridRemap.get_surround_4_uids(): ne=30 ''' from cube_remap import CubeGridRemap from pkg.convert_coord.cs_ll import abp2latlon ne, ngq = 30, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) td = (np.pi/2)/ne/3/2 # tiny delta ij = (1,2,1,1,1) gid = cube.ij2gid[ij] alpha, beta = cube.alpha_betas[gid] lat, lon = abp2latlon(alpha+td, beta+td, ij[0]) ret_uids = cube.get_surround_4_uids(lat, lon) a_equal(ret_uids, [3,16,7,19]) ij = (1,2,1,2,3) gid = cube.ij2gid[ij] alpha, beta = cube.alpha_betas[gid] lat, lon = abp2latlon(alpha+td, beta+td, ij[0]) ret_uids = cube.get_surround_4_uids(lat, lon) a_equal(ret_uids, [22,23,25,26]) '''
def plot_cs_voronoi(): ''' CubeGridRemap.get_voronoi(): Plot ''' from cube_remap import CubeGridRemap from scipy.spatial import voronoi_plot_2d import matplotlib.pyplot as plt #ne, ngq = 30, 4 ne, ngq = 3, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) print 'ne=%d, ngq=%d'%(ne, ngq) for uid in xrange(20): xy_vertices, vor = cube.get_voronoi(uid) print '' print 'uid', uid print 'vor.vertices\n', vor.vertices print 'vor.ridge_points\n', vor.ridge_points print 'vor.ridge_vertices\n', vor.ridge_vertices print '' print 'xy_vertices\n', xy_vertices voronoi_plot_2d(vor) #plt.savefig('voronoi_%d.png'%uid) plt.show()
def plot_cs_voronoi_scipy(): """ CubeGridRemap.get_voronoi(): Plot with voronoi_plot_2d """ from cube_remap import CubeGridRemap from scipy.spatial import voronoi_plot_2d import matplotlib.pyplot as plt # ne, ngq = 30, 4 ne, ngq = 3, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) print "ne=%d, ngq=%d" % (ne, ngq) for uid in xrange(20): xy_vertices, vor = cube.get_voronoi(uid) print "" print "uid", uid print "vor.vertices\n", vor.vertices print "vor.ridge_points\n", vor.ridge_points print "vor.ridge_vertices\n", vor.ridge_vertices print "" print "xy_vertices\n", xy_vertices voronoi_plot_2d(vor) # plt.savefig('voronoi_%d.png'%uid) plt.show()
def test_get_surround_4_gids(): """ CubeGridRemap.get_surround_4_gids(): ne=30 """ from cube_remap import CubeGridRemap ne, ngq = 30, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) lat, lon = -0.78973737977, 0.0 abp, ret_gids = cube.get_surround_4_gids(lat, lon) a_equal(ret_gids, [71754, 71755, 71758, 71759])
def test_get_surround_elem_rotated(): """ CubeGridRemap.get_surround_elem() rotated: ne=30 """ from cube_remap import CubeGridRemap from util.convert_coord.cs_ll import abp2latlon ne, ngq = 30, 4 rotated = True cube = CubeGridRemap(ne, ngq, rotated) lat, lon = np.deg2rad(38), np.deg2rad(127) (a, b), (panel, ei, ej) = cube.get_surround_elem(lat, lon) aa_equal((a, b), (0, 0), 15) a_equal((panel, ei, ej), (1, 15, 16))
def plot_cs_voronoi_polygon(): """ CubeGridRemap.get_voronoi(): Plot with cube_basemap.py """ from time import sleep from cube_remap import CubeGridRemap from util.convert_coord.cs_ll import xyp2latlon, xyz2latlon from util.plot.cube_basemap import PlotSphere, draw_points, draw_polygon ne, ngq = 3, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) print "ne=%d, ngq=%d" % (ne, ngq) # plot ps = PlotSphere(0, 0, figsize=(15, 15), interact=True, draw_map=True) ps.draw_cube_panel(1) ps.draw_cube_panel(4) ps.draw_cube_panel(5) ps.draw_cube_elements(ne, 1) polys = list() for uid in xrange(1): lat0, lon0 = cube.latlons[uid] # xy_vts, vor_obj = cube.get_voronoi_scipy(uid) # ll_vts = [xyp2latlon(x,y,1,lat0,lon0) for x,y in xy_vts] xyz_vts = cube.get_voronoi(uid) ll_vts = [xyz2latlon(*xyz) for xyz in xyz_vts] print ll_vts draw_points(ps.bmap, ll_vts) poly = draw_polygon(ps.bmap, ll_vts) poly.update(dict(fc="r")) polys.append(poly) # ps.draw() # polys[uid].set_facecolor('w') ps.show(True)
def test_get_surround_elem_gids(): """ CubeGridRemap.get_surround_elem_gids(): ne=30 """ from cube_remap import CubeGridRemap from util.convert_coord.cs_ll import abp2latlon ne, ngq = 30, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) td = (np.pi / 2) / ne / 3 / 2 # tiny delta ij = (1, 2, 1, 1, 1) gid = cube.ij2gid[ij] alpha, beta = cube.alpha_betas[gid] lat, lon = abp2latlon(alpha + td, beta + td, ij[0]) ret_gids = cube.get_surround_elem_gids(lat, lon) a_equal(ret_gids, [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]) ret_uids = [cube.uids[gid] for gid in ret_gids] a_equal(ret_uids, [3, 16, 17, 18, 7, 19, 20, 21, 11, 22, 23, 24, 15, 25, 26, 27])
def test_cs_get_voronoi_area(): ''' CubeGridRemap.get_voronoi(): check the sphere area, ne=3 ''' from cube_remap import CubeGridRemap from pkg.convert_coord.cart_cs import xyp2xyz from pkg.convert_coord.cart_rotate import xyz_rotate_reverse from area_sphere import area_polygon_sphere from math import fsum, pi ne, ngq = 3, 4 rotated = False cube = CubeGridRemap(ne, ngq, rotated) area_list = list() for uid in xrange(cube.up_size): xy_list, vor = cube.get_voronoi(uid) xyzs = [xyp2xyz(x,y,1) for x,y in xy_list] area_list.append( area_polygon_sphere(xyzs) ) aa_equal(fsum(area_list), 4*pi, 15)