Changeset 2597 in lmdz_wrf


Ignore:
Timestamp:
Jun 7, 2019, 6:44:13 AM (6 years ago)
Author:
lfita
Message:

Seem to completely work 'cut_[y/x]polygon' for multiple cuts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2592 r2597  
    513513                    ipt.append([yval, polygon[ip,1]+dx*dd/dy])
    514514
    515                 if val_consec_between(polygon[ip,0], polygon[eep,0], yval):
     515                if val_consec_between(polygon[eep,0], polygon[ip,0], yval):
    516516                    ecut.append(ip)
    517517                    dx = polygon[eep,1] - polygon[ip,1]
     
    533533                ipt.append([yval, polygon[ip,1]+dx*dd/dy])
    534534
    535             if val_consec_between(polygon[ip,0], polygon[eep,0], yval):
     535            if val_consec_between(polygon[eep,0], polygon[ip,0], yval):
    536536                ecut.append(ip)
    537537                dx = polygon[eep,1] - polygon[ip,1]
     
    541541                Ncuts = Ncuts + 1
    542542
     543    # Looking for repeated
     544    newicut = icut + []
     545    newecut = ecut + []
     546    newipt = ipt + []
     547    newept = ept + []
     548    newNcuts = Ncuts
     549    for ic in range(newNcuts-1):
     550        for ic2 in range(ic+1,newNcuts):
     551            if newipt[ic] == newipt[ic2]:
     552                Ncuts = Ncuts-1
     553                icut.pop(ic2)
     554                ecut.pop(ic2)
     555                ipt.pop(ic2)
     556                ept.pop(ic2)
     557                newNcuts = Ncuts + 0
     558
    543559    if ipt is None or ept is None or Ncuts == 0:
    544560        print errormsg
     
    546562    else:
    547563        print '  ' + fname + ': found ', Ncuts, ' Ncuts'
    548         print '    yval=', yval, 'cut, ip; ipt ep; ept ________'
    549         for ic in range(Ncuts):
    550             print '      ', ic, icut[ic], ';', ipt[ic], ecut[ic], ';', ept[ic]
    551 
     564        if Ncuts > 1 and keep == 'below':
     565            # Re-shifting cuts by closest distance.
     566            xis = []
     567            xes = []
     568            for ic in range(Ncuts):
     569                xp = ipt[ic]
     570                xis.append(xp[1])
     571                xp = ept[ic]
     572                xes.append(xp[1])
     573            xs = xis + xes
     574            xs.sort()
     575            newicut = icut + []
     576            newecut = ecut + []
     577            newipt = ipt + []
     578            newept = ept + []
     579            icut = []
     580            ecut = []
     581            ipt = []
     582            ept = []
     583            for xv in xs:
     584                ic = xis.count(xv)
     585                if ic != 0:
     586                    icc = xis.index(xv)
     587                    if len(icut) > len(ecut):
     588                        ecut.append(newicut[icc])
     589                        ept.append(newipt[icc])
     590                    else:
     591                        icut.append(newicut[icc])
     592                        ipt.append(newipt[icc])
     593                else:
     594                    icc = xes.index(xv)
     595                    if len(icut) > len(ecut):
     596                        ecut.append(newecut[icc])
     597                        ept.append(newept[icc])
     598                    else:
     599                        icut.append(newecut[icc])
     600                        ipt.append(newept[icc])
     601
     602#            # Re-shifting cuts. 1st icut --> last ecut; 1st ecut as 1st icut;
     603#            #    2nd icut --> last-1 ecut, ....
     604#            newicut = icut + []
     605#            newecut = ecut + []
     606#            newipt = ipt + []
     607#            newept = ept + []
     608#            for ic in range(Ncuts-1):
     609#                ecut[ic] = newecut[Ncuts-ic-1]
     610#                ept[ic] = newept[Ncuts-ic-1]
     611#                icut[ic+1] = newecut[ic]
     612#                ipt[ic+1] = newept[ic]
     613
     614#            ecut[Ncuts-1] = newicut[Ncuts-1]
     615#            ept[Ncuts-1] = newipt[Ncuts-1]
     616
     617##        print '    yval=', yval, 'cut, ip; ipt ep; ept ________'
     618##        for ic in range(Ncuts):
     619##            print '      ', ic, icut[ic], ';', ipt[ic], ecut[ic], ';', ept[ic]
     620
     621    # Length of joining lines
    552622    Nadds = []
    553623    if Ncuts > 1:
    554         Naddc = Nadd/(Ncuts-1)
     624        Naddc = (Nadd-Ncuts)/(Ncuts)
     625        if Naddc < 3:
     626            print errormsg
     627            print '  ' + fname + ': too few points for jioning lines !!'
     628            print '    increase Nadd at least to:', Ncuts*3+Ncuts
     629            quit(-1)
    555630        for ic in range(Ncuts-1):
    556631            Nadds.append(Naddc)
    557632
    558         Nadds.append(N-Naddc*(Ncuts-1))
     633        Nadds.append(Nadd-Naddc*(Ncuts-1))
    559634    else:
    560635        Nadds.append(Nadd)
    561636
    562     iip = 0
     637    # Total points cut polygon
     638    Ntotpts = 0
     639    Ncpts = []
     640    for ic in range(Ncuts):
     641        if keep == 'below':
     642            if ic == 0:
     643                dpts = icut[ic] + Nadds[ic] + (N - ecut[ic])
     644            else:
     645                dpts = ecut[ic] - icut[ic] + Nadds[ic] - 1
     646 
     647            # Adding end of the polygon in 'left' keeps
     648            if ic == Ncuts - 1: dpts = dpts + N-ecut[ic]
     649        else:
     650            dpts = ecut[ic] - icut[ic] + Nadds[ic] - 1
     651
     652        Ntotpts = Ntotpts + dpts
     653        Ncpts.append(ecut[ic] - icut[ic])
     654
     655    cutpolygon = np.ones((Ntotpts+Ncuts,2), dtype=np.float)*gen.fillValue
     656
    563657    iipc = 0
    564658    for ic in range(Ncuts):
     659        dcpt = Ncpts[ic]
    565660        if keep == 'below':
    566             Npts = icut[ic] + (N-ecut[ic]) + Nadds[ic]
    567             cutpolygon = np.zeros((Npts,2), dtype=np.float)
    568             cutpolygon[iipc:iipc+icut[ic]+1,:] = polygon[iip:iip+icut[ic]+1,:]
    569             iip = iip+icut[ic]+1
    570             iipc = iipc+icut[ic]+1
     661            if ic == 0:
     662                cutpolygon[0:icut[ic],:] = polygon[0:icut[ic],:]
     663                iipc = icut[ic]
     664            else:
     665                cutpolygon[iipc:iipc+dcpt-1,:] = polygon[icut[ic]+1:ecut[ic],:]
     666                iipc = iipc + dcpt -1
    571667        else:
    572             Npts = ecut[ec] - icut[ic] + Nadds[ic]-1
    573             cutpolygon = np.zeros((Npts,2), dtype=np.float)
    574668            cutpolygon[iipc,:] = ipt[ic]
    575             cutpolygon[iipc+1:ecut[ic]-icut[ic],:] = polygon[icut[ic]+1:ecut[ic],:]
    576             iip = ecut[ic]-icut[ic]-1
    577             iipc = iipc + ecut[ic]-icut[ic]-1
     669            cutpolygon[iipc:iipc+dcpt-1,:]=polygon[icut[ic]+1:ecut[ic],:]
     670            iipc = iipc+dcpt-1
    578671
    579672        # cutting line
    580673        cutline = np.zeros((Nadds[ic],2), dtype=np.float)
    581         dx = (ept[ic][1] - ipt[ic][1])/(Nadds[ic]-2)
    582         dy = (ept[ic][0] - ipt[ic][0])/(Nadds[ic]-2)
     674        dx = (ept[ic][1] - ipt[ic][1])/(Nadds[ic]-1)
     675        dy = (ept[ic][0] - ipt[ic][0])/(Nadds[ic]-1)
    583676        cutline[0,:] = ipt[ic]
    584677        for ip in range(1,Nadds[ic]-1):
     
    586679        cutline[Nadds[ic]-1,:] = ept[ic]
    587680        if keep == 'below':
    588             cutpolygon[iip:iip+Nadds[ic],:] = cutline
    589             cutpolygon[iip+Nadds[ic]:Npts,:] = polygon[ecut[ic]+1:N,:]
     681            if ic == 0: cutpolygon[iipc:iipc+Nadds[ic],:] = cutline
     682            else: cutpolygon[iipc:iipc+Nadds[ic],:] = cutline[::-1,:]
     683            iipc = iipc+Nadds[ic]
     684            if ic == 0:
     685                cutpolygon[iipc:iipc+N-ecut[ic]-1,:] = polygon[ecut[ic]+1:N,:]
     686                iipc = iipc + N-ecut[ic]-1
     687                cutpolygon[iipc,:] = polygon[0,:]
     688                iipc = iipc + 1
    590689        else:
    591             cutpolygon[iip:iip+Nadds[ic],:] = cutline[::-1,:]
     690            cutpolygon[iipc:iipc+Nadds[ic],:] = cutline[::-1,:]
     691            iipc = iipc+Nadds[ic]
     692        iipc = iipc + 1
    592693
    593694    rmpolygon = []
     
    635736    ept = None
    636737
     738    # There might be more than 1 cut ...
    637739    icut = []
    638740    ecut = []
     
    683785                Ncuts = Ncuts + 1
    684786
     787    # Looking for repeated
     788    newicut = icut + []
     789    newecut = ecut + []
     790    newipt = ipt + []
     791    newept = ept + []
     792    newNcuts = Ncuts
     793    for ic in range(newNcuts-1):
     794        for ic2 in range(ic+1,newNcuts):
     795            if newipt[ic] == newipt[ic2]:
     796                Ncuts = Ncuts-1
     797                icut.pop(ic2)
     798                ecut.pop(ic2)
     799                ipt.pop(ic2)
     800                ept.pop(ic2)
     801                newNcuts = Ncuts + 0
     802
    685803    if ipt is None or ept is None or Ncuts == 0:
    686804        print errormsg
     
    689807        ##print '  ' + fname + ': found ', Ncuts, ' Ncuts'
    690808        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, ....
     809            # Re-shifting cuts by closest heigth.
     810            yis = []
     811            yes = []
     812            for ic in range(Ncuts):
     813                yp = ipt[ic]
     814                yis.append(yp[0])
     815                yp = ept[ic]
     816                yes.append(yp[0])
     817            ys = yis + yes
     818            ys.sort()
    693819            newicut = icut + []
    694820            newecut = ecut + []
    695821            newipt = ipt + []
    696822            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 
     823            icut = []
     824            ecut = []
     825            ipt = []
     826            ept = []
     827            for yv in ys:
     828                ic = yis.count(yv)
     829                if ic != 0:
     830                    icc = yis.index(yv)
     831                    if len(icut) > len(ecut):
     832                        ecut.append(newicut[icc])
     833                        ept.append(newipt[icc])
     834                    else:
     835                        icut.append(newicut[icc])
     836                        ipt.append(newipt[icc])
     837                else:
     838                    icc = yes.index(yv)
     839                    if len(icut) > len(ecut):
     840                        ecut.append(newecut[icc])
     841                        ept.append(newept[icc])
     842                    else:
     843                        icut.append(newecut[icc])
     844                        ipt.append(newept[icc])
    706845        ##print '    xval=', xval, 'cut, ip; ipt ep; ept ________'
    707846        ##for ic in range(Ncuts):
Note: See TracChangeset for help on using the changeset viewer.