Exemplo n.º 1
0
def test_area_polygon():
    '''
    area_polygon(): 1/8, 1/16, 1/24, 1/48
    '''

    from sphere import area_polygon
    from convert_coord.cart_ll import latlon2xyz
    from convert_coord.cs_ll import abp2latlon


    # 1/8 sphere area
    latlons = [(0,0), (0,pi/2), (pi/2,0)]
    xyzs = [latlon2xyz(*latlon) for latlon in latlons]
    ret = area_polygon(xyzs)
    aa_equal(ret, 4*pi/8, 15)


    # 1/16 sphere area
    latlons = [(0,0), (0,pi/4), (pi/2,0)]
    xyzs = [latlon2xyz(*latlon) for latlon in latlons]
    ret = area_polygon(xyzs)
    aa_equal(ret, 4*pi/16, 15)


    # 1/24 sphere area
    latlon1 = (0,0)
    latlon2 =  abp2latlon(-pi/4,0,1)
    latlon3 =  abp2latlon(-pi/4,-pi/4,1)
    latlon4 =  abp2latlon(0,-pi/4,1) 
    latlons = [latlon1, latlon2, latlon3, latlon4]
    xyzs = [latlon2xyz(*latlon) for latlon in latlons]
    ret = area_polygon(xyzs)
    aa_equal(ret, 4*pi/24, 15)


    # 1/48 sphere area
    latlons = [latlon1, latlon3, latlon4]
    xyzs = [latlon2xyz(*latlon) for latlon in latlons]
    ret = area_polygon(xyzs)
    aa_equal(ret, 4*pi/48, 15)
Exemplo n.º 2
0
    def __init__(self, ne, ngq, rotated=False, is_print=False):
        self.ne = ne
        self.ngq = ngq
        self.rotated = rotated
        self.is_print = is_print

        # EP(Entire Point), UP(Unique Point)
        ep_size = ngq*ngq*ne*ne*6
        up_size = 4*(ngq-1)*ne*((ngq-1)*ne + 1) + \
                   2*((ngq-1)*(ne-1) + (ngq-2))**2


        #-----------------------------------------------------
        # allocations
        #-----------------------------------------------------
        # Gauss-qudrature point index (panel,ei,ej,gi,gj)
        gq_indices = np.zeros((ep_size,5), 'i4')
        ij2seq_dict = dict()    # {(panel,ei,ej,gi,gj):seq,...}

        # sequential index
        mvps = np.ones((ep_size,4), 'i4')*(-1) # overlapped points (gid or -1)
        is_uvps = np.zeros(ep_size, 'i2')      # unique-point (True/False)
        uids = np.ones(ep_size, 'i4')*(-1)     # unique index (seq)
        gids = np.ones(up_size, 'i4')*(-1)     # global index
        nbrs = np.ones((up_size,8), 'i4')*(-1) # neighbors, anti-clockwise (gid)

        # coordinates
        alpha_betas = np.zeros((ep_size,2), 'f8')   # different at each panel
        latlons = np.zeros((up_size,2), 'f8')
        xyzs = np.zeros((up_size,3), 'f8')


        #-----------------------------------------------------
        # (gi, gj, ei, ej, panel)
        #-----------------------------------------------------
        seq = 0
        for panel in range(1,7):
            for ej in range(1,ne+1):
                for ei in range(1,ne+1):
                    for gj in range(1,ngq+1):
                        for gi in range(1,ngq+1):
                            ij = (panel,ei,ej,gi,gj)
                            gq_indices[seq,:] = ij
                            ij2seq_dict[ij] = seq       # start from 0
                            seq += 1

        
        #-----------------------------------------------------
        # mvps (overlapped index)
        if is_print: print("Generate mvps (multi-valued points)")
        #-----------------------------------------------------
        abcd2ij_dict = {'A':(1,1,1,1), 'B':(ne,1,ngq,1), \
                        'C':(ne,ne,ngq,ngq), 'D':(1,ne,1,ngq)}

        for seq in range(ep_size):
            panel, ei, ej, gi, gj = gq_indices[seq]

            mvps[seq,0] = seq       # self index, start from 0

            #-----------------------------------
            # At the panel corner (3 points)
            #-----------------------------------
            if (ei,ej,gi,gj) in abcd2ij_dict.values():
                if   (ei,ej,gi,gj) == abcd2ij_dict['A']: tag1 = "{}A".format(panel)
                elif (ei,ej,gi,gj) == abcd2ij_dict['B']: tag1 = "{}B".format(panel)
                elif (ei,ej,gi,gj) == abcd2ij_dict['C']: tag1 = "{}C".format(panel)
                elif (ei,ej,gi,gj) == abcd2ij_dict['D']: tag1 = "{}D".format(panel)

                tag2 = panel_corner_tags[tag1]
                tag3 = panel_corner_tags[tag2]

                for k, tag in enumerate([tag2,tag3]):
                    p, abcd = int(tag[0]), tag[1]
                    ij = tuple( [p] + list(abcd2ij_dict[abcd]) )
                    mvps[seq,k+1] = ij2seq_dict[ij]

                #print(seq, mvps[seq,:])


            #-----------------------------------
            # At the panel side
            #-----------------------------------
            elif (ei,gi) in [(1,1),(ne,ngq)] or \
                 (ej,gj) in [(1,1),(ne,ngq)]:

                if   (ei,gi) == (1,1):    tag1 = "{}W".format(panel)
                elif (ei,gi) == (ne,ngq): tag1 = "{}E".format(panel)
                elif (ej,gj) == (1,1):    tag1 = "{}S".format(panel)
                elif (ej,gj) == (ne,ngq): tag1 = "{}N".format(panel)

                tag2 = panel_side_tags[tag1]
                p1, ewsn1 = int(tag1[0]), tag1[1]
                p2, ewsn2 = int(tag2[0]), tag2[1]

                ij0 = (p1,ei,ej,gi,gj)
                ij_across = get_across_ij_panel_side( \
                        ne, ngq, tag1, tag2, ei, ej, gi, gj)

                #-------------------------------------
                # 4 points
                #-------------------------------------
                if (gi,gj) in [(1,1),(ngq,1),(ngq,ngq),(1,ngq)]:
                    fb_next, ij_next = get_next_ij_panel_side(ewsn1, ngq, *ij0)
                    ij_a_next = get_next_ij_panel_side(ewsn2, ngq, *ij_across)[-1]

                    if fb_next == 'forward':
                        ijs = [ij_next, ij_a_next, ij_across]
                    else:
                        ijs = [ij_across, ij_a_next, ij_next]

                    for k, ij in enumerate(ijs):
                        mvps[seq,k+1] = ij2seq_dict[ij]


                #-------------------------------------
                # 2 points
                #-------------------------------------
                else:
                    mvps[seq,1] = ij2seq_dict[ij_across]


                #print(seq, mvps[seq,:])


            #-----------------------------------
            # 4 points inside the panel
            #-----------------------------------
            # corner 
            elif (gi,gj) in [(1,1),(ngq,1),(ngq,ngq),(1,ngq)]:
                if (gi,gj) == (1,1):
                    ij1 = (panel,ei-1,ej  ,ngq,  1)
                    ij2 = (panel,ei-1,ej-1,ngq,ngq)
                    ij3 = (panel,ei  ,ej-1,  1,ngq)

                elif (gi,gj) == (ngq,1):
                    ij1 = (panel,ei  ,ej-1,ngq,ngq)
                    ij2 = (panel,ei+1,ej-1,  1,ngq)
                    ij3 = (panel,ei+1,ej  ,  1,  1)

                elif (gi,gj) == (ngq,ngq):
                    ij1 = (panel,ei+1,ej  ,  1,ngq)
                    ij2 = (panel,ei+1,ej+1,  1,  1)
                    ij3 = (panel,ei  ,ej+1,ngq,  1)

                elif (gi,gj) == (1,ngq):
                    ij1 = (panel,ei  ,ej+1,  1,  1)
                    ij2 = (panel,ei-1,ej+1,ngq,  1)
                    ij3 = (panel,ei-1,ej  ,ngq,ngq)

                #print("(panel,ei,ej,gi,gj) = {}".format((panel,ei,ej,gi,gj)))
                mvps[seq,1] = ij2seq_dict[ij1]
                mvps[seq,2] = ij2seq_dict[ij2]
                mvps[seq,3] = ij2seq_dict[ij3]


            #-----------------------------------
            # 2 points inside the panel
            #-----------------------------------
            #  side
            elif gi in (1,ngq) or gj in (1,ngq):
                if   gj == 1:   ij1 = (panel,ei,ej-1,gi,ngq)
                elif gj == ngq: ij1 = (panel,ei,ej+1,gi,  1)
                elif gi == 1:   ij1 = (panel,ei-1,ej,ngq,gj)
                elif gi == ngq: ij1 = (panel,ei+1,ej,  1,gj)

                mvps[seq,1] = ij2seq_dict[ij1]


        #-----------------------------------------------------
        # is_uvps (unique-point, True/False)
        # uids (unique index), ep_size
        # gids (global index), up_size
        if is_print: print("Generate is_uvps, uids and gids")
        #-----------------------------------------------------
        u_seq = 0

        for seq in range(ep_size):
            valid_mvp = [k for k in mvps[seq] if k != -1]

            #print(seq, valid_mvp)
            if min(valid_mvp) == seq:
                is_uvps[seq] = True
                gids[u_seq] = seq

                for k in valid_mvp:
                    uids[k] = u_seq

                u_seq += 1

        is_up_size = np.count_nonzero(is_uvps)
        assert up_size == is_up_size, "Error: up_size={}, np.count_nonzero(is_uvp)={}".format(up_size, is_up_size)
        assert up_size == u_seq, "Error: up_size={}, u_seq={}".foramt(up_size, u_seq)
        assert -1 not in uids, "Error: -1 in uids"
        assert -1 not in gids, "Error: -1 in gids"



        #-----------------------------------------------------
        # nbrs (neighbors)
        if is_print: print("Generate nbrs (neighbors, anti-clockwise)")
        #-----------------------------------------------------
        for u_seq in range(up_size):
            gid = gids[u_seq]
            panel, ei, ej, gi, gj = gq_indices[gid]
            valid_mvp = [k for k in mvps[gid] if k != -1]

            if (gi,gj) in [(1,1),(ngq,1),(ngq,ngq),(1,ngq)]:
                if len(valid_mvp) == 3:     # panel corner
                    ij0, ij1, ij2 = [gq_indices[m] for m in valid_mvp]
                    nbrs[u_seq,:3] = get_neighbors(ngq, ij2seq_dict, *ij0)
                    nbrs[u_seq,3:5] = get_neighbors(ngq, ij2seq_dict, *ij1)[1:]
                    nbrs[u_seq,5] = get_neighbors(ngq, ij2seq_dict, *ij2)[1]

                elif len(valid_mvp) == 4:   # corner
                    ij0, ij1, ij2, ij3 = [gq_indices[m] for m in valid_mvp]
                    nbrs[u_seq,:3] = get_neighbors(ngq, ij2seq_dict, *ij0)
                    nbrs[u_seq,3:5] = get_neighbors(ngq, ij2seq_dict, *ij1)[1:]
                    nbrs[u_seq,5:7] = get_neighbors(ngq, ij2seq_dict, *ij2)[1:]
                    nbrs[u_seq,7] = get_neighbors(ngq, ij2seq_dict, *ij3)[1]


            elif (gi in [1,ngq]) or (gj in [1,ngq]):
                ij0, ij1 = [gq_indices[m] for m in valid_mvp]
                nbrs[u_seq,:5] = get_neighbors(ngq, ij2seq_dict, *ij0)
                nbrs[u_seq,5:] = get_neighbors(ngq, ij2seq_dict, *ij1)[1:-1]


            else:
                ij0 = gq_indices[valid_mvp[0]]
                nbrs[u_seq,:] = get_neighbors(ngq, ij2seq_dict, *ij0)

            #print(u_seq, gid, nbrs[u_seq])



        #-----------------------------------------------------
        # coordinates  (alpha,beta), (lat,lon), (x,y,z)
        if is_print: print("Generate coordinates (alpha,beta), (lat,lon), (x,y,z)")
        #-----------------------------------------------------
        for seq in range(ep_size):
            panel, ei, ej, gi, gj = gq_indices[seq]
            alpha, beta = ij2ab(ne, ngq, panel, ei, ej, gi, gj)
            alpha_betas[seq,:] = (alpha, beta)


        for u_seq in range(up_size):
            seq = gids[u_seq]
            panel, ei, ej, gi, gj = gq_indices[seq]

            alpha, beta = alpha_betas[seq]

            if rotated:
                rlat, rlon = np.deg2rad(38), np.deg2rad(127)  #korea centered
            else:
                rlat, rlon = 0, 0
            lat, lon = abp2latlon(alpha, beta, panel, rlat, rlon)
            latlons[u_seq,:] = (lat,lon)

            x, y, z = latlon2xyz(lat, lon)
            xyzs[u_seq,:] = (x, y, z)


        #-----------------------------------------------------
        # global variables
        #-----------------------------------------------------
        self.ep_size = ep_size
        self.up_size = up_size
        self.ij2seq_dict = ij2seq_dict
        self.gq_indices = gq_indices
        self.mvps = mvps
        self.is_uvps = is_uvps
        self.uids = uids
        self.gids = gids
        self.nbrs = nbrs
        self.alpha_betas = alpha_betas
        self.latlons = latlons
        self.xyzs = xyzs
        self.rlat = rlat
        self.rlon = rlon