Changeset 2556 in lmdz_wrf


Ignore:
Timestamp:
May 25, 2019, 7:04:18 PM (6 years ago)
Author:
lfita
Message:

Adding:

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

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2548 r2556  
    2323
    2424####### Contents:
     25# cut_between_[x/y]polygon: Function to cut a polygon between 2 given value of the y-axis
    2526# cut_[x/y]polygon: Function to cut a polygon from a given value of the [x/y]-axis
    2627# deg_deci: Function to pass from degrees [deg, minute, sec] to decimal angles [rad]
     
    465466                    dy = polygon[eep,0] - polygon[ip,0]
    466467                    dd = yval - polygon[ip,0]
    467                     print polygon[ip,:], ':', polygon[eep,:]
    468468                    ept = [yval, polygon[ip,1]+dx*dd/dy]
    469469    else:
     
    514514        cutline[ip,:] = ipt + np.array([dy*ip,dx*ip])
    515515    cutline[Nadd-1,:] = ept
    516     cutpolygon[iip:iip+Nadd,:] = cutline
    517516    if keep == 'bottom':
     517        cutpolygon[iip:iip+Nadd,:] = cutline
    518518        if type(polygon) == type(gen.mamat):
    519519            cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
    520520        else:
    521521            cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     522    else:
     523        cutpolygon[iip:iip+Nadd,:] = cutline[::-1,:]
    522524
    523525    rmpolygon = []
     
    541543    return Npts, cutpolygon
    542544
    543 def cut_xpolygon(polygon, xval, keep='left', Nadd=20):
    544     """ Function to cut a polygon from a given value of the x-axis
     545def cut_between_ypolygon(polygon, yval1, yval2, Nadd=20):
     546    """ Function to cut a polygon between 2 given value of the y-axis
    545547      polygon: polygon to cut
    546       xval: value to use to cut the polygon
    547       keep: part to keep from the cut ('left', default)
    548          'left': left from the cut
    549          'right': right from the cut
     548      yval1: first value to use to cut the polygon
     549      yval2: first value to use to cut the polygon
    550550      Nadd: additional points to add to draw the line (20, default)
    551551    """
    552     fname = 'cut_xpolygon'
     552    fname = 'cut_betwen_ypolygon'
    553553
    554554    N = polygon.shape[0]
    555     availkeeps = ['left', 'right']
    556 
    557     if not gen.searchInlist(availkeeps, keep):
    558         print errormsg
    559         print '  ' + fname + ": wring keep '" + keep + "' value !!"
    560         print '    available ones:', availkeeps
    561         quit(-1)
    562555
    563556    ipt = None
    564557    ept = None
    565558
    566     if type(polygon) == type(gen.mamat):
    567         # Assuming clockwise polygons
    568         for ip in range(N):
    569             if not polygon.mask[ip,0]:
     559    dx = np.zeros((2), dtype=np.float)
     560    dy = np.zeros((2), dtype=np.float)
     561    icut = np.zeros((2), dtype=int)
     562    ecut = np.zeros((2), dtype=int)
     563    ipt = np.zeros((2,2), dtype=np.float)
     564    ept = np.zeros((2,2), dtype=np.float)
     565
     566    if yval1 > yval2:
     567        print errormsg
     568        print '  ' + fname + ': wrong between cut values !!'
     569        print '     it is expected yval1 < yval2'
     570        print '     values provided yval1: (', yval1, ')> yval2 (', yval2, ')'
     571        quit(-1)
     572
     573    yvals = [yval1, yval2]
     574
     575    for ic in range(2):
     576        yval = yvals[ic]
     577        if type(polygon) == type(gen.mamat):
     578            # Assuming clockwise polygons
     579            for ip in range(N-1):
     580                if not polygon.mask[ip,0]:
     581                    eep = ip + 1
     582                    if eep == N: eep = 0
     583     
     584                    if polygon[ip,0] <= yval and polygon[eep,0] >= yval:
     585                        icut[ic] = ip
     586                        dx[ic] = polygon[eep,1] - polygon[ip,1]
     587                        dy[ic] = polygon[eep,0] - polygon[ip,0]
     588                        dd = yval - polygon[ip,0]
     589                        ipt[ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]]
     590
     591                    if polygon[ip,0] >= yval and polygon[eep,0] <= yval:
     592                        ecut[ic] = ip
     593                        dx[ic] = polygon[eep,1] - polygon[ip,1]
     594                        dy[ic] = polygon[eep,0] - polygon[ip,0]
     595                        dd = yval - polygon[ip,0]
     596                        ept[ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]]
     597        else:
     598            # Assuming clockwise polygons
     599            for ip in range(N-1):
    570600                eep = ip + 1
    571601                if eep == N: eep = 0
    572602     
    573                 if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
    574                     icut = ip
    575                     dx = polygon[eep,1] - polygon[ip,1]
    576                     dy = polygon[eep,0] - polygon[ip,0]
    577                     dd = xval - polygon[ip,1]
    578                     ipt = [ polygon[ip,0]+dy*dd/dx, xval]
    579 
    580                 if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
    581                     ecut = ip
    582                     dx = polygon[eep,1] - polygon[ip,1]
    583                     dy = polygon[eep,0] - polygon[ip,0]
    584                     dd = xval - polygon[eep,1]
    585                     ept = [polygon[eep,0]+dy*dd/dx, xval]
    586     else:
    587         # Assuming clockwise polygons
    588         for ip in range(N):
    589             eep = ip + 1
    590             if eep == N: eep = 0
    591      
    592             if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
    593                 icut = ip
    594                 dx = polygon[eep,1] - polygon[ip,1]
    595                 dy = polygon[eep,0] - polygon[ip,0]
    596                 dd = xval - polygon[ip,1]
    597                 ipt = [polygon[ip,0]+dy*dd/dx, xval]
    598 
    599             if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
    600                 ecut = ip
    601                 dx = polygon[eep,1] - polygon[ip,1]
    602                 dy = polygon[eep,0] - polygon[ip,0]
    603                 dd = xval - polygon[ip,1]
    604                 ept = [polygon[ip,0]+dy*dd/dx, xval]
    605 
    606     if ipt is None or ept is None:
    607         print errormsg
    608         print '  ' + fname + ': no cutting for polygon at y=', xval, '!!'
    609         irmv = 0
    610         rmpolygon = []
    611         if keep == 'left':
    612             for ip in range(N):
    613                 if polygon[ip,1] > xval:
    614                     rmpolygon.append([gen.fillValueF, gen.fillValueF])
    615                 else:
    616                     rmpolygon.append(polygon[ip,:])
    617         else:
    618             for ip in range(N):
    619                 if polygon[ip,0] < xval:
    620                     rmpolygon.append([gen.fillValueF, gen.fillValueF])
    621                 else:
    622                     rmpolygon.append(polygon[ip,:])
    623 
    624         Npts = len(rmpolygon)
    625         cutpolygon = np.array(rmpolygon)
    626     else:
    627         if keep == 'left':
    628             Npts = icut + (N-ecut) + Nadd
    629 
    630             cutpolygon = np.zeros((Npts,2), dtype=np.float)
    631             if type(polygon) == type(gen.mamat):
    632                 cutpolygon[0:icut+1,:] = polygon[0:icut+1,:].filled(gen.fillValueF)
    633             else:
    634                 cutpolygon[0:icut+1,:] = polygon[0:icut+1,:]
    635             iip = icut+1
    636         else:
    637             Npts = ecut - icut + Nadd-1
    638 
    639             cutpolygon = np.zeros((Npts,2), dtype=np.float)
    640             if type(polygon) == type(gen.mamat):
    641                 cutpolygon[0:ecut-icut-1,:] = polygon[icut+1:ecut,:].filled(gen.fillValueF)
    642             else:
    643                 cutpolygon[0:ecut-icut-1,:] = polygon[icut+1:ecut,:]
    644             iip = ecut-icut-1
    645 
    646         # cutting line
    647         cutline = np.zeros((Nadd,2), dtype=np.float)
    648         dx = (ept[1] - ipt[1])/(Nadd-2)
    649         dy = (ept[0] - ipt[0])/(Nadd-2)
    650         cutline[0,:] = ipt
    651         for ip in range(1,Nadd-1):
    652             cutline[ip,:] = ipt + np.array([dy*ip,dx*ip])
    653         cutline[Nadd-1,:] = ept
    654         cutpolygon[iip:iip+Nadd,:] = cutline
    655         if keep == 'left':
    656             cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     603                if polygon[ip,0] <= yval and polygon[eep,0] >= yval:
     604                    icut[ic] = ip
     605                    dx[ic] = polygon[eep,1] - polygon[ip,1]
     606                    dy[ic] = polygon[eep,0] - polygon[ip,0]
     607                    dd = yval - polygon[ip,0]
     608                    ipt[ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]]
     609
     610                if polygon[ip,0] >= yval and polygon[eep,0] <= yval:
     611                    ecut[ic] = ip
     612                    dx[ic] = polygon[eep,1] - polygon[ip,1]
     613                    dy[ic] = polygon[eep,0] - polygon[ip,0]
     614                    dd = yval - polygon[ip,0]
     615                    ept[ic,:] = [yval, polygon[ip,1]+dx[ic]*dd/dy[ic]]
     616
     617        if ipt is None or ept is None:
     618            print errormsg
     619            print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
     620
     621    Npts = icut[1] - icut[0] + Nadd + ecut[0] - ecut[1]
     622    cutpolygon = np.zeros((Npts,2), dtype=np.float)
     623    cutpolygon[0,:] = ipt[0,:]
     624    cutpolygon[1:icut[1]-icut[0]+1,:] = polygon[icut[0]+1:icut[1]+1,:]
     625    iip = icut[1]-icut[0]
     626
     627    # cutting lines
     628    Nadd2 = int(Nadd/2)
     629    cutlines = np.zeros((2,Nadd2,2), dtype=np.float)
     630
     631    for ic in range(2):
     632        dx = (ept[ic,1] - ipt[ic,1])/(Nadd2-2)
     633        dy = (ept[ic,0] - ipt[ic,0])/(Nadd2-2)
     634        cutlines[ic,0,:] = ipt[ic,:]
     635        for ip in range(1,Nadd2-1):
     636            cutlines[ic,ip,:] = ipt[ic,:] + np.array([dy*ip,dx*ip])
     637        cutlines[ic,Nadd2-1,:] = ept[ic,:]
     638
     639    cutpolygon[iip:iip+Nadd2,:] = cutlines[1,:,:]
     640    iip = iip + Nadd2
     641    cutpolygon[iip:iip+(ecut[0]-ecut[1]),:] = polygon[ecut[1]+1:ecut[0]+1,:]
     642    iip = iip + ecut[0]-ecut[1]
     643    cutpolygon[iip:iip+Nadd2,:] = cutlines[0,::-1,:]
    657644
    658645    cutpolygon = ma.masked_equal(cutpolygon, gen.fillValueF)
     
    16821669    Height = np.max(buoy1v[:,0])
    16831670
    1684     print 'Lluis Height', Height
    1685 
    16861671    Ncut, halfdown = cut_ypolygon(buoy1v, yval=Height/2., keep='bottom')
    16871672    Ncut, halfup = cut_ypolygon(buoy1v, yval=Height/2., keep='above')
Note: See TracChangeset for help on using the changeset viewer.