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 plot_ll_voronoi_polygon():
    """
    LatlonGridRemap.get_voronoi(): Plot with cube_basemap.py
    """
    from time import sleep
    from cube_remap import LatlonGridRemap
    from util.convert_coord.cs_ll import xyp2latlon, xyz2latlon
    from util.plot.cube_basemap import PlotSphere, draw_points, draw_polygon

    nlat, nlon = 90, 180
    ll_obj = LatlonGridRemap(nlat, nlon)
    print "nlat=%d, nlon=%d" % (nlat, nlon)

    # plot
    ps = PlotSphere(-90, 0, figsize=(15, 15), interact=True, draw_map=True)

    for idx in xrange(nlon, 2 * nlon + 1):
        lat0, lon0 = ll_obj.latlons[idx]
        # ll_vts = ll_obj.get_voronoi(idx)
        ll_vts = [xyz2latlon(*xyz) for xyz in ll_obj.get_voronoi(idx)]

        draw_points(ps.bmap, ll_vts)
        poly = draw_polygon(ps.bmap, ll_vts)
        poly.update(dict(fc="r"))

    ps.show(True)
def test_ll_voronoi_area():
    """
    LatlonGridRemap.get_voronoi(): check the sphere area
    """
    from cube_remap import LatlonGridRemap
    from util.geometry.sphere import area_polygon
    from math import fsum, pi
    from util.convert_coord.cart_ll import latlon2xyz

    nlat, nlon = 90, 180
    # nlat, nlon = 180, 360
    # nlat, nlon = 360, 720
    # nlat, nlon = 720, 1440
    ll_obj = LatlonGridRemap(nlat, nlon)

    area_list = list()
    for idx in xrange(ll_obj.nsize):
        # latlons = ll_obj.get_voronoi(idx)
        # xyzs = [latlon2xyz(*latlon) for latlon in latlons]
        xyzs = ll_obj.get_voronoi(idx)
        area_list.append(area_polygon(xyzs))

    aa_equal(fsum(area_list), 4 * pi, 12)

    """
示例#4
0
def test_get_voronoi_latlon():
    '''
    LatlonGridRemap.get_voronoi(): nlat=180, nlon=360 (regular)
    '''
    from cube_remap import LatlonGridRemap

    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)]
    aa_equal(expect, ret, 10)

    ret = ll.get_voronoi(nlon)
    expect = [(-1.5447610285607269,-0.00872664625997164), \
              (-1.5447610285607269, 0.008726646259971647), \
              (-1.5274041630712807, 0.008726646259971647), \
              (-1.5274041630712807,-0.00872664625997164)]
    aa_equal(expect, ret, 10)
示例#5
0
def test_get_neighbors_latlon():
    '''
    LatlonGridRemap.get_neighbors(): nlat=180, nlon=360 (regular)
    '''
    from cube_remap import LatlonGridRemap

    nlat, nlon = 180, 360
    ll = LatlonGridRemap(nlat, nlon, 'regular')

    ret = ll.get_neighbors(nlon+1)
    expect = (0, 1, 2, nlon, nlon+2, 2*nlon, 2*nlon+1, 2*nlon+2)
    a_equal(expect, ret)

    ret = ll.get_neighbors(nlon)
    expect = (nlon-1, 0, 1, 2*nlon-1, nlon+1, 3*nlon-1, 2*nlon, 2*nlon+1)
    a_equal(expect, ret)

    ret = ll.get_neighbors(1)
    expect = (0, 2, nlon, nlon+1, nlon+2)
    a_equal(expect, ret)

    ret = ll.get_neighbors(0)
    expect = (nlon-1, 1, 2*nlon-1, nlon, nlon+1)
    a_equal(expect, ret)

    ret = ll.get_neighbors((nlat-1)*nlon)
    expect = ((nlat-1)*nlon-1, (nlat-2)*nlon, (nlat-2)*nlon+1, nlat*nlon-1, (nlat-1)*nlon+1)
    a_equal(expect, ret)
示例#6
0
def test_get_surround_idxs():
    '''
    LatlonGridRemap.get_surround_idxs(): nlat=180, nlon=360 (regular)
    '''
    from cube_remap import LatlonGridRemap


    nlat, nlon = 180, 360
    tx = 1e-3
    ll = LatlonGridRemap(nlat, nlon, 'regular')

    # Near south pole
    lat0 = ll.tmp_lats[1]
    lon0 = ll.tmp_lons[7]
    ret_idxs = ll.get_surround_idxs(lat0-tx,lon0+tx)
    equal(ret_idxs, (7,-1,-1,-1))

    # Near north pole
    lat0 = ll.tmp_lats[-2]
    lon0 = ll.tmp_lons[-2]
    ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx)
    equal(ret_idxs, (nlat*nlon-1,-1,-1,-1))

    # First box
    lat0 = ll.tmp_lats[1]
    lon0 = ll.tmp_lons[0]
    ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx)
    a_equal(ret_idxs, [0,1,nlon,nlon+1])

    # Last box
    lat0 = ll.tmp_lats[-2]
    lon0 = ll.tmp_lons[-2]
    ret_idxs = ll.get_surround_idxs(lat0-tx,lon0+tx)
    a_equal(ret_idxs, [(nlat-1)*nlon-1, (nlat-2)*nlon, nlat*nlon-1, (nlat-1)*nlon])

    # Near Meridian
    lat0 = ll.tmp_lats[1]
    lon0 = ll.tmp_lons[-2]
    ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx)
    a_equal(ret_idxs, [nlon-1,0,nlon*2-1,nlon])

    # Error cases
    lat, lon = -0.785398163397, 6.28318530718
    ret_idxs = ll.get_surround_idxs(lat,lon)
    a_equal(ret_idxs, [16199, 15840, 16559, 16200])
示例#7
0
def test_get_surround_idxs_2():
    '''
    LatlonGridRemap.get_surround_idxs(): nlat=192, nlon=384 (gaussian)
    '''
    from cube_remap import LatlonGridRemap


    nlat, nlon = 192, 384
    tx = 1e-5
    ll = LatlonGridRemap(nlat, nlon, 'gaussian')

    # Near south pole
    lat0 = ll.tmp_lats[1]
    lon0 = ll.tmp_lons[7]
    ret_idxs = ll.get_surround_idxs(lat0-tx,lon0+tx)
    equal(ret_idxs, (7,-1,-1,-1))

    # Near north pole
    lat0 = ll.tmp_lats[-2]
    lon0 = ll.tmp_lons[-2]
    ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx)
    equal(ret_idxs, (nlat*nlon-1,-1,-1,-1))

    # First box
    lat0 = ll.tmp_lats[1]
    lon0 = ll.tmp_lons[0]
    ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx)
    a_equal(ret_idxs, [0,1,nlon,nlon+1])

    # Last box
    lat0 = ll.tmp_lats[-2]
    lon0 = ll.tmp_lons[-2]
    ret_idxs = ll.get_surround_idxs(lat0-tx,lon0+tx)
    a_equal(ret_idxs, [(nlat-1)*nlon-1, (nlat-2)*nlon, nlat*nlon-1, (nlat-1)*nlon])

    # Near Meridian
    lat0 = ll.tmp_lats[1]
    lon0 = ll.tmp_lons[-2]
    ret_idxs = ll.get_surround_idxs(lat0+tx,lon0+tx)
    a_equal(ret_idxs, [nlon-1,0,nlon*2-1,nlon])