Changeset 2592 in lmdz_wrf


Ignore:
Timestamp:
Jun 4, 2019, 4:38:08 AM (6 years ago)
Author:
lfita
Message:

First working version of `cut_xpolygon' for multiple cuts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2587 r2592  
    687687        print '  ' + fname + ': no cutting for polygon at x=', xval, '!!'
    688688    else:
    689         print '  ' + fname + ': found ', Ncuts, ' Ncuts'
    690         print '    xval=', xval, 'cut, ip; ipt ep; ept ________'
    691         for ic in range(Ncuts):
    692             print '      ', ic, icut[ic], ';', ipt[ic], ecut[ic], ';', ept[ic]
     689        ##print '  ' + fname + ': found ', Ncuts, ' Ncuts'
     690        if Ncuts > 1 and keep == 'left':
     691            # Re-shifting cuts. 1st icut --> last ecut; 1st ecut as 1st icut;
     692            #    2nd icut --> last-1 ecut, ....
     693            newicut = icut + []
     694            newecut = ecut + []
     695            newipt = ipt + []
     696            newept = ept + []
     697            for ic in range(Ncuts-1):
     698                ecut[ic] = newecut[Ncuts-ic-1]
     699                ept[ic] = newept[Ncuts-ic-1]
     700                icut[ic+1] = newecut[ic]
     701                ipt[ic+1] = newept[ic]
     702
     703            ecut[Ncuts-1] = newicut[Ncuts-1]
     704            ept[Ncuts-1] = newipt[Ncuts-1]
     705
     706        ##print '    xval=', xval, 'cut, ip; ipt ep; ept ________'
     707        ##for ic in range(Ncuts):
     708        ##    print '      ', ic, icut[ic], ';', ipt[ic], ecut[ic], ';', ept[ic]
    693709
    694710    # Length of joining lines
    695711    Nadds = []   
    696712    if Ncuts > 1:
    697         Naddc = (Nadd-Ncuts)/(Ncuts-1)
     713        Naddc = (Nadd-Ncuts)/(Ncuts)
    698714        if Naddc < 3:
    699715            print errormsg
     
    704720            Nadds.append(Naddc)
    705721
    706         Nadds.append(N-Naddc*(Ncuts-1))
     722        Nadds.append(Nadd-Naddc*(Ncuts-1))
    707723    else:
    708724        Nadds.append(Nadd)
     
    712728    Ncpts = []
    713729    for ic in range(Ncuts):
    714         ip = ipt[ic]
    715         if ic == 0:
    716             dpts = ecut[ic] - icut[ic] + Nadds[ic] + icut[ic]
     730        if keep == 'left':
     731            if ic == 0:
     732                dpts = icut[ic] + Nadds[ic] + (N - ecut[ic])
     733            else:
     734                dpts = ecut[ic] - icut[ic] + Nadds[ic] - 1
     735
     736            # Adding end of the polygon in 'left' keeps
     737            if ic == Ncuts - 1: dpts = dpts + N-ecut[ic]
    717738        else:
    718739            dpts = ecut[ic] - icut[ic] + Nadds[ic] - 1
    719740
    720         # Adding end of the polygon in 'left' keeps
    721         if keep == 'left' and ic == Ncuts - 1: dpts = dpts + N-ecut[ic]
    722         Ncpts.append(dpts)
    723741        Ntotpts = Ntotpts + dpts
    724 
    725     cutpolygon = np.ones((Ntotpts+Ncuts-1,2), dtype=np.float)*gen.fillValue
    726 
    727     iip = 0
     742        Ncpts.append(ecut[ic] - icut[ic])
     743
     744    cutpolygon = np.ones((Ntotpts+Ncuts,2), dtype=np.float)*gen.fillValue
     745
    728746    iipc = 0
    729747    for ic in range(Ncuts):
    730         Npts = Ncpts[ic]
     748        dcpt = Ncpts[ic]
    731749        if keep == 'left':
    732750            if ic == 0:
    733751                cutpolygon[0:icut[ic],:] = polygon[0:icut[ic],:]
    734                 iip = icut[ic]
    735752                iipc = icut[ic]
    736                 dcpt = ecut[ic]-icut[ic]
    737                 print ic, ':', 0, icut[ic], 'iipc:', iipc, 'dcpt', dcpt
    738753            else:
    739                 dcpt = Ncpts[ic]-Nadds[ic]
    740             print ic, 'dcpt:', dcpt, '<Ncpts>', Ncpts[ic], '<--', icut[ic]-1,ecut[ic]+1, '=',  iipc+1, iipc+dcpt+1
    741             cutpolygon[iipc+1:iipc+dcpt+1,:] = polygon[icut[ic]-1:ecut[ic]+1,:]
    742             iipc = iipc + dcpt
     754                cutpolygon[iipc:iipc+dcpt-1,:] = polygon[icut[ic]+1:ecut[ic],:]
     755                iipc = iipc + dcpt -1
    743756        else:
    744757            cutpolygon[iipc,:] = ipt[ic]
    745             cutpolygon[iipc+1:iipc+ecut[ic]-icut[ic],:]=polygon[icut[ic]+1:ecut[ic],:]
    746             iipc = iipc+ecut[ic]-icut[ic]-1
     758            cutpolygon[iipc:iipc+dcpt-1,:]=polygon[icut[ic]+1:ecut[ic],:]
     759            iipc = iipc+dcpt-1
    747760
    748761        # cutting line
    749762        cutline = np.zeros((Nadds[ic],2), dtype=np.float)
    750         dx = (ept[ic][1] - ipt[ic][1])/(Nadds[ic]-2)
    751         dy = (ept[ic][0] - ipt[ic][0])/(Nadds[ic]-2)
     763        dx = (ept[ic][1] - ipt[ic][1])/(Nadds[ic]-1)
     764        dy = (ept[ic][0] - ipt[ic][0])/(Nadds[ic]-1)
    752765        cutline[0,:] = ipt[ic]
    753766        for ip in range(1,Nadds[ic]-1):
    754767            cutline[ip,:] = ipt[ic] + np.array([dy*ip,dx*ip])
    755         cutline[Nadd-1,:] = ept[ic]
     768        cutline[Nadds[ic]-1,:] = ept[ic]
    756769        if keep == 'left':
    757             cutpolygon[iipc:iipc+Nadds[ic],:] = cutline
    758 #            cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     770            if ic == 0: cutpolygon[iipc:iipc+Nadds[ic],:] = cutline
     771            else: cutpolygon[iipc:iipc+Nadds[ic],:] = cutline[::-1]
     772            iipc = iipc+Nadds[ic]
     773            if ic == 0:
     774                cutpolygon[iipc:iipc+N-ecut[ic]-1,:] = polygon[ecut[ic]+1:N,:]
     775                iipc = iipc + N-ecut[ic]-1
     776                cutpolygon[iipc,:] = polygon[0,:]
     777                iipc = iipc + 1
    759778        else:
    760779            cutpolygon[iipc:iipc+Nadds[ic],:] = cutline[::-1,:]
     780            iipc = iipc+Nadds[ic]
     781        iipc = iipc + 1
    761782
    762783    rmpolygon = []
Note: See TracChangeset for help on using the changeset viewer.