Changeset 2583 in lmdz_wrf


Ignore:
Timestamp:
Jun 2, 2019, 6:23:46 PM (6 years ago)
Author:
lfita
Message:

Adding multiple cuts in `cut_xpolygon'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2582 r2583  
    622622    ept = None
    623623
     624    icut = []
     625    ecut = []
     626    ipt = []
     627    ept = []
     628    Ncuts = 0
    624629    if type(polygon) == type(gen.mamat) and type(polygon.mask) !=                    \
    625630      type(gen.mamat.mask[1]):
     
    630635                if eep == N: eep = 0
    631636     
    632                 if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
    633                     icut = ip
     637                if val_between(polygon[ip,1], polygon[eep,1], xval):
     638                    icut.append(ip)
    634639                    dx = polygon[eep,1] - polygon[ip,1]
    635640                    dy = polygon[eep,0] - polygon[ip,0]
    636641                    dd = xval - polygon[ip,1]
    637                     ipt = [polygon[ip,0]+dy*dd/dx, xval]
    638 
    639                 if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
    640                     ecut = ip
     642                    ipt.append([polygon[ip,0]+dy*dd/dx, xval])
     643
     644                if val_between(polygon[ip,1], polygon[eep,1], xval):
     645                    ecut.append(ip)
    641646                    dx = polygon[eep,1] - polygon[ip,1]
    642647                    dy = polygon[eep,0] - polygon[ip,0]
    643648                    dd = xval - polygon[ip,1]
    644                     ept = [polygon[ip,0]+dy*dd/dx, xval]
     649                    ept.append([polygon[ip,0]+dy*dd/dx, xval])
     650                    Npts = Npts + 1
    645651    else:
    646652        # Assuming clockwise polygons
     
    649655            if eep == N: eep = 0
    650656     
    651             if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
    652                 icut = ip
     657            if val_between(polygon[ip,1], polygon[eep,1], xval):
     658                icut.append(ip)
    653659                dx = polygon[eep,1] - polygon[ip,1]
    654660                dy = polygon[eep,0] - polygon[ip,0]
    655661                dd = xval - polygon[ip,1]
    656                 ipt = [polygon[ip,0]+dy*dd/dx, xval]
    657 
    658             if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
    659                 ecut = ip
     662                ipt.append([polygon[ip,0]+dy*dd/dx, xval])
     663
     664            if val_between(polygon[ip,1], polygon[eep,1], xval):
     665                ecut.append(ip)
    660666                dx = polygon[eep,1] - polygon[ip,1]
    661667                dy = polygon[eep,0] - polygon[ip,0]
    662668                dd = xval - polygon[ip,1]
    663                 ept = [polygon[ip,0]+dy*dd/dx, xval]
    664 
    665     if ipt is None or ept is None:
     669                ept.append([polygon[ip,0]+dy*dd/dx, xval])
     670
     671    if ipt is None or ept is None or Ncuts == 0:
    666672        print errormsg
    667673        print '  ' + fname + ': no cutting for polygon at x=', xval, '!!'
    668 
    669     if keep == 'left':
    670         Npts = icut + (N-ecut) + Nadd + 1
    671         cutpolygon = np.zeros((Npts,2), dtype=np.float)
    672         cutpolygon[0,:] = ept
    673         cutpolygon[1:N-ecut,:] = polygon[ecut+1:N,:]
    674         iip = N-ecut
    675         cutpolygon[iip:iip+icut+1,:] = polygon[0:icut+1,:]
    676         iip = iip + icut+1
    677674    else:
    678         Npts = ecut - icut + Nadd-1
    679         cutpolygon = np.zeros((Npts,2), dtype=np.float)
    680         cutpolygon[0,:] = ipt
    681         cutpolygon[1:ecut-icut,:] = polygon[icut+1:ecut,:]
    682         iip = ecut-icut-1
    683 
    684     # cutting line
    685     cutline = np.zeros((Nadd,2), dtype=np.float)
    686     dx = (ept[1] - ipt[1])/(Nadd-2)
    687     dy = (ept[0] - ipt[0])/(Nadd-2)
    688     cutline[0,:] = ipt
    689     for ip in range(1,Nadd-1):
    690         cutline[ip,:] = ipt + np.array([dy*ip,dx*ip])
    691     cutline[Nadd-1,:] = ept
    692     if keep == 'left':
    693         cutpolygon[iip:iip+Nadd,:] = cutline
    694 #        cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     675        print '  ' + fname + ': found ', Ncuts, ' Ncuts'
     676        print '    yval=', xval, 'cut, ip; ipt ep; ept ________'
     677        for ic in range(Ncuts):
     678            print '      ', ic, icut[ic], ';', ipt[ic], ecut[ic], ';', ept[ic]
     679
     680    # Length of joining lines
     681    Nadds = []   
     682    if Ncuts > 1:
     683        Naddc = (Nadd-Ncuts)/(Ncuts-1)
     684        if Naddc < 3:
     685            print errormsg
     686            print '  ' + fname + ': too few points for jioning lines !!'
     687            print '    increase Nadd at least to:', Ncuts*3+Ncuts
     688            quit(-1)
     689        for ic in range(Ncuts-1):
     690            Nadds.append(Naddc)
     691
     692        Nadds.append(N-Naddc*(Ncuts-1))
    695693    else:
    696         cutpolygon[iip:iip+Nadd,:] = cutline[::-1,:]
     694        Nadds.append(Nadd)
     695
     696   # Total points cut polygon
     697    Ntotpts = 0
     698    Ncpts = []
     699    for ic in range(Ncuts):
     700        ip = ipt[ic]
     701        if ic == 0:
     702            dpts = icut[ic] + ecut[ic] + Nadds[ic]
     703        else:
     704            dpts = ecut[ic] - icut[ic] + Nadds[ic] - 1
     705
     706        # Adding end of the polygon in 'left' keeps
     707        if keep == 'left' and ic == Ncuts - 1: dpts = dpts + N-ecut[ic]
     708        Ncpts.append(dpts)
     709        Ntotpts = Ntotpts + dpts
     710
     711    cutpolygon = np.ones((Ntotpts,2), dtype=np.float)*gen.fillValue
     712
     713    iip = 0
     714    iipc = 0
     715    for ic in range(Ncuts):
     716        Npts = Ncpts[ic]
     717        if keep == 'left':
     718            if ic == 0:
     719                cutpolygon[0:icut[ic]] = polygon[0:icut[ic],:]
     720                iip = icut[ic]
     721                iipc = icut[ic]
     722            dcpt = Ncpts[ic]-Nadds[ic]
     723            cutpolygon[iipc+1:iipc+dcpt,:] = polygon[icut[ic]-1:ecut[ic]+1,:]
     724            iipc = iipc + dcpt
     725        else:
     726            cutpolygon[iipc,:] = ipt[ic]
     727            cutpolygon[iipc+1:iipc+ecut[ic]-icut[ic],:]=polygon[icut[ic]+1:ecut[ic],:]
     728            iipc = iipc+ecut[ic]-icut[ic]-1
     729
     730        # cutting line
     731        cutline = np.zeros((Nadds[ic],2), dtype=np.float)
     732        dx = (ept[ic][1] - ipt[ic][1])/(Nadds[ic]-2)
     733        dy = (ept[ic][0] - ipt[ic][0])/(Nadds[ic]-2)
     734        cutline[0,:] = ipt[ic]
     735        for ip in range(1,Nadds[ic]-1):
     736            cutline[ip,:] = ipt[ic] + np.array([dy*ip,dx*ip])
     737        cutline[Nadd-1,:] = ept[ic]
     738        if keep == 'left':
     739            cutpolygon[iipc:iipc+Nadds[ic],:] = cutline
     740#            cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     741        else:
     742            cutpolygon[iipc:iipc+Nadds[ic],:] = cutline[::-1,:]
    697743
    698744    rmpolygon = []
Note: See TracChangeset for help on using the changeset viewer.