Changeset 2545 in lmdz_wrf for trunk/tools/geometry_tools.py


Ignore:
Timestamp:
May 19, 2019, 3:23:05 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `cut_ypolygon': Function to cut a polygon from a given value of the y-axis
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2544 r2545  
    1717import generic_tools as gen
    1818import numpy.ma as ma
     19import module_ForSci as sci
    1920
    2021errormsg = 'ERROR -- error -- ERROR -- error'
     
    2223
    2324####### Contents:
     25# cut_ypolygon: Function to cut a polygon from a given value of the y-axis
    2426# deg_deci: Function to pass from degrees [deg, minute, sec] to decimal angles [rad]
    2527# dist_points: Function to provide the distance between two points
     
    420422
    421423    return polys
     424
     425def cut_ypolygon(polygon, yval, keep='bottom', Nadd=20):
     426    """ Function to cut a polygon from a given value of the y-axis
     427      polygon: polygon to cut
     428      yval: value to use to cut the polygon
     429      keep: part to keep from the height ('bottom', default)
     430      Nadd: additional points to add to draw the line (20, default)
     431    """
     432    fname = 'cut_ypolygon'
     433
     434    N = polygon.shape[0]
     435
     436    ipt = None
     437    ept = None
     438
     439    if type(polygon) == type(gen.mamat):
     440        # Assuming clockwise polygons
     441        for ip in range(N):
     442            if not polygon.mask[ip,0]:
     443                eep = ip + 1
     444                if eep == N: eep = 0
     445     
     446                if polygon[ip,0] <= yval and polygon[eep,0] >= yval:
     447                    icut = ip
     448                    dx = polygon[eep,1] - polygon[ip,1]
     449                    dy = polygon[eep,0] - polygon[ip,0]
     450                    dd = yval - polygon[ip,0]
     451                    ipt = [yval, polygon[ip,1]+dx*dd/dy]
     452
     453                if polygon[ip,0] >= yval and polygon[eep,0] <= yval:
     454                    ecut = ip
     455                    dx = polygon[eep,1] - polygon[ip,1]
     456                    dy = polygon[eep,0] - polygon[ip,0]
     457                    dd = yval - polygon[eep,0]
     458                    ept = [yval, polygon[eep,1]+dx*dd/dy]
     459    else:
     460        # Assuming clockwise polygons
     461        for ip in range(N):
     462            eep = ip + 1
     463            if eep == N: eep = 0
     464     
     465            if polygon[ip,0] <= yval and polygon[eep,0] >= yval:
     466                icut = ip
     467                dx = polygon[eep,1] - polygon[ip,1]
     468                dy = polygon[eep,0] - polygon[ip,0]
     469                dd = yval - polygon[ip,0]
     470                ipt = [yval, polygon[ip,1]+dx*dd/dy]
     471                print ip, 'Lluis pi:', polygon[ip,:], 'pi+1', polygon[eep,:]
     472                print '    yval', yval, 'dx', dx, 'dy', dy
     473                print '      ipt', ipt
     474
     475            if polygon[ip,0] >= yval and polygon[eep,0] <= yval:
     476                ecut = ip
     477                dx = polygon[eep,1] - polygon[ip,1]
     478                dy = polygon[eep,0] - polygon[ip,0]
     479                dd = yval - polygon[ip,0]
     480                ept = [yval, polygon[ip,1]+dx*dd/dy]
     481                print ip, 'Lluis pi:', polygon[ip,:], 'pi+1', polygon[eep,:]
     482                print '    yval', yval, 'dx', dx, 'dy', dy
     483                print '      ept', ept
     484
     485    if ipt is None or ept is None:
     486        print errormsg
     487        print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
     488        irmv = 0
     489        rmpolygon = []
     490        for ip in range(N):
     491            if polygon[ip,0] > yval:
     492                rmpolygon.append([gen.fillValueF, gen.fillValueF])
     493            else:
     494                rmpolygon.append(polygon[ip,:])
     495        Npts = len(rmpolygon)
     496        cutpolygon = np.array(rmpolygon)
     497    else:
     498        Npts = icut + (N-ecut) + Nadd
     499
     500        cutpolygon = np.zeros((Npts,2), dtype=np.float)
     501        if type(polygon) == type(gen.mamat):
     502            cutpolygon[0:icut+1,:] = polygon[0:icut+1,:].filled(gen.fillValueF)
     503        else:
     504            cutpolygon[0:icut+1,:] = polygon[0:icut+1,:]
     505
     506        # cutting line
     507        cutline = np.zeros((Nadd,2), dtype=np.float)
     508        dx = (ept[1] - ipt[1])/(Nadd-2)
     509        dy = (ept[0] - ipt[0])/(Nadd-2)
     510        cutline[0,:] = ipt
     511        for ip in range(1,Nadd-1):
     512            cutline[ip,:] = ipt + np.array([dy*ip,dx*ip])
     513        cutline[Nadd-1,:] = ept
     514        cutpolygon[icut+1:icut+1+Nadd,:] = cutline
     515        cutpolygon[icut+1+Nadd:Npts,:] = polygon[ecut+1:N,:]
     516
     517    cutpolygon = ma.masked_equal(cutpolygon, gen.fillValueF)
     518
     519    return Npts, cutpolygon
    422520
    423521####### ###### ##### #### ### ## #
Note: See TracChangeset for help on using the changeset viewer.