Changeset 2560 in lmdz_wrf


Ignore:
Timestamp:
May 28, 2019, 2:21:06 AM (6 years ago)
Author:
lfita
Message:

Starting to add `cut_xpolygon'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2559 r2560  
    533533        for ip in range(Npts):
    534534            if cutpolygon[ip,0] < yval:
     535                rmpolygon.append([gen.fillValueF, gen.fillValueF])
     536            else:
     537                rmpolygon.append(cutpolygon[ip,:])
     538    Npts = len(rmpolygon)
     539    cutpolygon = np.array(rmpolygon)
     540
     541    cutpolygon = ma.masked_equal(cutpolygon, gen.fillValueF)
     542
     543    return Npts, cutpolygon
     544
     545def cut_xpolygon(polygon, xval, keep='left', Nadd=20):
     546    """ Function to cut a polygon from a given value of the x-axis
     547      polygon: polygon to cut
     548      yval: value to use to cut the polygon
     549      keep: part to keep from the value ('left', default)
     550         'left': left of the value
     551         'right': right of the value
     552      Nadd: additional points to add to draw the line (20, default)
     553    """
     554    fname = 'cut_xpolygon'
     555
     556    N = polygon.shape[0]
     557    availkeeps = ['left', 'right']
     558
     559    if not gen.searchInlist(availkeeps, keep):
     560        print errormsg
     561        print '  ' + fname + ": wring keep '" + keep + "' value !!"
     562        print '    available ones:', availkeeps
     563        quit(-1)
     564
     565    ipt = None
     566    ept = None
     567
     568    if type(polygon) == type(gen.mamat):
     569        # Assuming clockwise polygons
     570        for ip in range(N-1):
     571            if not polygon.mask[ip,0]:
     572                eep = ip + 1
     573                if eep == N: eep = 0
     574     
     575                if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
     576                    icut = ip
     577                    dx = polygon[eep,1] - polygon[ip,1]
     578                    dy = polygon[eep,0] - polygon[ip,0]
     579                    dd = xval - polygon[ip,1]
     580                    ipt = [polygon[ip,0]+dy*dd/dx, xval]
     581
     582                if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
     583                    ecut = ip
     584                    dx = polygon[eep,1] - polygon[ip,1]
     585                    dy = polygon[eep,0] - polygon[ip,0]
     586                    dd = xval - polygon[ip,1]
     587                    ept = [polygon[ip,0]+dy*dd/dx, xval]
     588    else:
     589        # Assuming clockwise polygons
     590        for ip in range(N-1):
     591            eep = ip + 1
     592            if eep == N: eep = 0
     593     
     594            if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
     595                icut = ip
     596                dx = polygon[eep,1] - polygon[ip,1]
     597                dy = polygon[eep,0] - polygon[ip,0]
     598                dd = xval - polygon[ip,1]
     599                ipt = [polygon[ip,0]+dy*dd/dx, xval]
     600
     601            if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
     602                ecut = ip
     603                dx = polygon[eep,1] - polygon[ip,1]
     604                dy = polygon[eep,0] - polygon[ip,0]
     605                dd = xval - polygon[ip,1]
     606                ept = [polygon[ip,0]+dy*dd/dx, xval]
     607
     608    if ipt is None or ept is None:
     609        print errormsg
     610        print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
     611
     612    print 'Lluis icut', icut, 'ipt:', ipt, 'ecut', ecut, 'ept:', ept, 'N', N
     613    print 'Lluis 00', polygon[0:7,:]
     614    print 'Lluis NN', polygon[ecut+1:N,:]
     615
     616    if keep == 'left':
     617        Npts = icut + (N-ecut) + Nadd + 1
     618        cutpolygon = np.zeros((Npts,2), dtype=np.float)
     619        cutpolygon[0,:] = ept
     620        cutpolygon[1:N-ecut,:] = polygon[ecut+1:N,:]
     621        iip = N-ecut
     622        cutpolygon[iip:iip+icut+1,:] = polygon[0:icut+1,:]
     623        iip = iip + icut+1
     624    else:
     625        Npts = ecut - icut + Nadd-1
     626        cutpolygon = np.zeros((Npts,2), dtype=np.float)
     627        cutpolygon[0,:] = ipt
     628        cutpolygon[1:ecut-icut,:] = polygon[icut+1:ecut,:]
     629        iip = ecut-icut-1
     630
     631    # cutting line
     632    cutline = np.zeros((Nadd,2), dtype=np.float)
     633    dx = (ept[1] - ipt[1])/(Nadd-2)
     634    dy = (ept[0] - ipt[0])/(Nadd-2)
     635    cutline[0,:] = ipt
     636    for ip in range(1,Nadd-1):
     637        cutline[ip,:] = ipt + np.array([dy*ip,dx*ip])
     638    cutline[Nadd-1,:] = ept
     639    if keep == 'left':
     640        cutpolygon[iip:iip+Nadd,:] = cutline
     641#        cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     642    else:
     643        cutpolygon[iip:iip+Nadd,:] = cutline[::-1,:]
     644
     645    rmpolygon = []
     646    if keep == 'left':
     647        for ip in range(Npts):
     648            if cutpolygon[ip,1] > xval:
     649                rmpolygon.append([gen.fillValueF, gen.fillValueF])
     650            else:
     651                rmpolygon.append(cutpolygon[ip,:])
     652    else:
     653        for ip in range(Npts):
     654            if cutpolygon[ip,1] < xval:
    535655                rmpolygon.append([gen.fillValueF, gen.fillValueF])
    536656            else:
Note: See TracChangeset for help on using the changeset viewer.