Changeset 2564 in lmdz_wrf


Ignore:
Timestamp:
May 29, 2019, 9:56:33 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `cut_between_xpolygon': Function to cut a polygon between 2 given value of the x-axis
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2563 r2564  
    2323
    2424####### Contents:
    25 # cut_between_[x/y]polygon: Function to cut a polygon between 2 given value of the y-axis
     25# cut_between_[x/y]polygon: Function to cut a polygon between 2 given value of the [x/y]-axis
    2626# cut_[x/y]polygon: Function to cut a polygon from a given value of the [x/y]-axis
    2727# deg_deci: Function to pass from degrees [deg, minute, sec] to decimal angles [rad]
     
    734734            print errormsg
    735735            print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
     736
     737    Npts = icut[1] - icut[0] + Nadd + ecut[0] - ecut[1]
     738    cutpolygon = np.zeros((Npts,2), dtype=np.float)
     739    cutpolygon[0,:] = ipt[0,:]
     740    cutpolygon[1:icut[1]-icut[0]+1,:] = polygon[icut[0]+1:icut[1]+1,:]
     741    iip = icut[1]-icut[0]
     742
     743    # cutting lines
     744    Nadd2 = int(Nadd/2)
     745    cutlines = np.zeros((2,Nadd2,2), dtype=np.float)
     746
     747    for ic in range(2):
     748        dx = (ept[ic,1] - ipt[ic,1])/(Nadd2-2)
     749        dy = (ept[ic,0] - ipt[ic,0])/(Nadd2-2)
     750        cutlines[ic,0,:] = ipt[ic,:]
     751        for ip in range(1,Nadd2-1):
     752            cutlines[ic,ip,:] = ipt[ic,:] + np.array([dy*ip,dx*ip])
     753        cutlines[ic,Nadd2-1,:] = ept[ic,:]
     754
     755    cutpolygon[iip:iip+Nadd2,:] = cutlines[1,:,:]
     756    iip = iip + Nadd2
     757    cutpolygon[iip:iip+(ecut[0]-ecut[1]),:] = polygon[ecut[1]+1:ecut[0]+1,:]
     758    iip = iip + ecut[0]-ecut[1]
     759    cutpolygon[iip:iip+Nadd2,:] = cutlines[0,::-1,:]
     760
     761    cutpolygon = ma.masked_equal(cutpolygon, gen.fillValueF)
     762
     763    return Npts, cutpolygon
     764
     765def cut_between_xpolygon(polygon, xval1, xval2, Nadd=20):
     766    """ Function to cut a polygon between 2 given value of the x-axis
     767      polygon: polygon to cut
     768      xval1: first value to use to cut the polygon
     769      xval2: first value to use to cut the polygon
     770      Nadd: additional points to add to draw the line (20, default)
     771    """
     772    fname = 'cut_betwen_xpolygon'
     773
     774    N = polygon.shape[0]
     775
     776    ipt = None
     777    ept = None
     778
     779    dx = np.zeros((2), dtype=np.float)
     780    dy = np.zeros((2), dtype=np.float)
     781    icut = np.zeros((2), dtype=int)
     782    ecut = np.zeros((2), dtype=int)
     783    ipt = np.zeros((2,2), dtype=np.float)
     784    ept = np.zeros((2,2), dtype=np.float)
     785
     786    if xval1 > xval2:
     787        print errormsg
     788        print '  ' + fname + ': wrong between cut values !!'
     789        print '     it is expected xval1 < xval2'
     790        print '     values provided xval1: (', xval1, ')> xval2 (', xval2, ')'
     791        quit(-1)
     792
     793    xvals = [xval1, xval2]
     794
     795    for ic in range(2):
     796        xval = xvals[ic]
     797        if type(polygon) == type(gen.mamat):
     798            # Assuming clockwise polygons
     799            for ip in range(N-1):
     800                if not polygon.mask[ip,0]:
     801                    eep = ip + 1
     802                    if eep == N: eep = 0
     803     
     804                    if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
     805                        icut[ic] = ip
     806                        dx[ic] = polygon[eep,1] - polygon[ip,1]
     807                        dy[ic] = polygon[eep,0] - polygon[ip,0]
     808                        dd = xval - polygon[ip,1]
     809                        ipt[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
     810
     811                    if polygon[ip,1] >= yval and polygon[eep,1] <= xval:
     812                        ecut[ic] = ip
     813                        dx[ic] = polygon[eep,1] - polygon[ip,1]
     814                        dy[ic] = polygon[eep,0] - polygon[ip,0]
     815                        dd = xval - polygon[ip,1]
     816                        ept[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
     817        else:
     818            # Assuming clockwise polygons
     819            for ip in range(N-1):
     820                eep = ip + 1
     821                if eep == N: eep = 0
     822     
     823                if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
     824                    icut[ic] = ip
     825                    dx[ic] = polygon[eep,1] - polygon[ip,1]
     826                    dy[ic] = polygon[eep,0] - polygon[ip,0]
     827                    dd = xval - polygon[ip,1]
     828                    ipt[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
     829
     830                if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
     831                    ecut[ic] = ip
     832                    dx[ic] = polygon[eep,1] - polygon[ip,1]
     833                    dy[ic] = polygon[eep,0] - polygon[ip,0]
     834                    dd = xval - polygon[ip,1]
     835                    ept[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
     836
     837        if ipt is None or ept is None:
     838            print errormsg
     839            print '  ' + fname + ': no cutting for polygon at x=', xval, '!!'
    736840
    737841    Npts = icut[1] - icut[0] + Nadd + ecut[0] - ecut[1]
Note: See TracChangeset for help on using the changeset viewer.