Changeset 2547 in lmdz_wrf


Ignore:
Timestamp:
May 19, 2019, 4:46:20 PM (6 years ago)
Author:
lfita
Message:

Fixing `cut_ypolygon'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2546 r2547  
    2323
    2424####### Contents:
    25 # cut_ypolygon: Function to cut a polygon from a given value of the y-axis
     25# cut_[x/y]polygon: Function to cut a polygon from a given value of the [x/y]-axis
    2626# deg_deci: Function to pass from degrees [deg, minute, sec] to decimal angles [rad]
    2727# dist_points: Function to provide the distance between two points
     
    448448    if type(polygon) == type(gen.mamat):
    449449        # Assuming clockwise polygons
    450         for ip in range(N):
     450        for ip in range(N-1):
    451451            if not polygon.mask[ip,0]:
    452452                eep = ip + 1
     
    464464                    dx = polygon[eep,1] - polygon[ip,1]
    465465                    dy = polygon[eep,0] - polygon[ip,0]
    466                     dd = yval - polygon[eep,0]
    467                     ept = [yval, polygon[eep,1]+dx*dd/dy]
     466                    dd = yval - polygon[ip,0]
     467                    print polygon[ip,:], ':', polygon[eep,:]
     468                    print 'dx', dx, 'dy', dy, 'dd', dd
     469                    ept = [yval, polygon[ip,1]+dx*dd/dy]
    468470    else:
    469471        # Assuming clockwise polygons
    470         for ip in range(N):
     472        for ip in range(N-1):
    471473            eep = ip + 1
    472474            if eep == N: eep = 0
     
    484486                dy = polygon[eep,0] - polygon[ip,0]
    485487                dd = yval - polygon[ip,0]
     488                print polygon[ip,:], ':', polygon[eep,:]
     489                print 'dx', dx, 'dy', dy, 'dd', dd
    486490                ept = [yval, polygon[ip,1]+dx*dd/dy]
     491
     492    print 'Lluis ipt:', ipt, 'ept:', ept
    487493
    488494    if ipt is None or ept is None:
    489495        print errormsg
    490496        print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
     497
     498    if keep == 'bottom':
     499        Npts = icut + (N-ecut) + Nadd
     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        iip = icut+1
     506    else:
     507        Npts = ecut - icut + Nadd-1
     508        cutpolygon = np.zeros((Npts,2), dtype=np.float)
     509        if type(polygon) == type(gen.mamat):
     510            cutpolygon[0:ecut-icut-1,:] = polygon[icut+1:ecut,:].filled(gen.fillValueF)
     511        else:
     512            cutpolygon[0:ecut-icut-1,:] = polygon[icut+1:ecut,:]
     513        iip = ecut-icut-1
     514
     515    # cutting line
     516    cutline = np.zeros((Nadd,2), dtype=np.float)
     517    dx = (ept[1] - ipt[1])/(Nadd-2)
     518    dy = (ept[0] - ipt[0])/(Nadd-2)
     519    cutline[0,:] = ipt
     520    for ip in range(1,Nadd-1):
     521        cutline[ip,:] = ipt + np.array([dy*ip,dx*ip])
     522    cutline[Nadd-1,:] = ept
     523    cutpolygon[iip:iip+Nadd,:] = cutline
     524    if keep == 'bottom':
     525        if type(polygon) == type(gen.mamat):
     526            cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:].filled(gen.fillValueF)
     527        else:
     528            cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     529
     530    rmpolygon = []
     531    if keep == 'bottom':
     532        for ip in range(N):
     533            if polygon[ip,0] > yval:
     534                rmpolygon.append([gen.fillValueF, gen.fillValueF])
     535            else:
     536                rmpolygon.append(polygon[ip,:])
     537    else:
     538        for ip in range(N):
     539            if polygon[ip,0] < yval:
     540                rmpolygon.append([gen.fillValueF, gen.fillValueF])
     541            else:
     542                rmpolygon.append(polygon[ip,:])
     543    Npts = len(rmpolygon)
     544    cutpolygon = np.array(rmpolygon)
     545
     546    cutpolygon = ma.masked_equal(cutpolygon, gen.fillValueF)
     547
     548    return Npts, cutpolygon
     549
     550def cut_xpolygon(polygon, xval, keep='left', Nadd=20):
     551    """ Function to cut a polygon from a given value of the x-axis
     552      polygon: polygon to cut
     553      xval: value to use to cut the polygon
     554      keep: part to keep from the cut ('left', default)
     555         'left': left from the cut
     556         'right': right from the cut
     557      Nadd: additional points to add to draw the line (20, default)
     558    """
     559    fname = 'cut_xpolygon'
     560
     561    N = polygon.shape[0]
     562    availkeeps = ['left', 'right']
     563
     564    if not gen.searchInlist(availkeeps, keep):
     565        print errormsg
     566        print '  ' + fname + ": wring keep '" + keep + "' value !!"
     567        print '    available ones:', availkeeps
     568        quit(-1)
     569
     570    ipt = None
     571    ept = None
     572
     573    if type(polygon) == type(gen.mamat):
     574        # Assuming clockwise polygons
     575        for ip in range(N):
     576            if not polygon.mask[ip,0]:
     577                eep = ip + 1
     578                if eep == N: eep = 0
     579     
     580                if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
     581                    icut = ip
     582                    dx = polygon[eep,1] - polygon[ip,1]
     583                    dy = polygon[eep,0] - polygon[ip,0]
     584                    dd = xval - polygon[ip,1]
     585                    ipt = [ polygon[ip,0]+dy*dd/dx, xval]
     586
     587                if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
     588                    ecut = ip
     589                    dx = polygon[eep,1] - polygon[ip,1]
     590                    dy = polygon[eep,0] - polygon[ip,0]
     591                    dd = xval - polygon[eep,1]
     592                    ept = [polygon[eep,0]+dy*dd/dx, xval]
     593    else:
     594        # Assuming clockwise polygons
     595        for ip in range(N):
     596            eep = ip + 1
     597            if eep == N: eep = 0
     598     
     599            if polygon[ip,1] <= xval and polygon[eep,1] >= xval:
     600                icut = ip
     601                dx = polygon[eep,1] - polygon[ip,1]
     602                dy = polygon[eep,0] - polygon[ip,0]
     603                dd = xval - polygon[ip,1]
     604                ipt = [polygon[ip,0]+dy*dd/dx, xval]
     605
     606            if polygon[ip,1] >= xval and polygon[eep,1] <= xval:
     607                ecut = ip
     608                dx = polygon[eep,1] - polygon[ip,1]
     609                dy = polygon[eep,0] - polygon[ip,0]
     610                dd = xval - polygon[ip,1]
     611                ept = [polygon[ip,0]+dy*dd/dx, xval]
     612
     613    if ipt is None or ept is None:
     614        print errormsg
     615        print '  ' + fname + ': no cutting for polygon at y=', xval, '!!'
    491616        irmv = 0
    492617        rmpolygon = []
    493         if keep == 'bottom':
     618        if keep == 'left':
    494619            for ip in range(N):
    495                 if polygon[ip,0] > yval:
     620                if polygon[ip,1] > xval:
    496621                    rmpolygon.append([gen.fillValueF, gen.fillValueF])
    497622                else:
     
    499624        else:
    500625            for ip in range(N):
    501                 if polygon[ip,0] < yval:
     626                if polygon[ip,0] < xval:
    502627                    rmpolygon.append([gen.fillValueF, gen.fillValueF])
    503628                else:
     
    507632        cutpolygon = np.array(rmpolygon)
    508633    else:
    509         if keep == 'bottom':
     634        if keep == 'left':
    510635            Npts = icut + (N-ecut) + Nadd
    511636
     
    535660        cutline[Nadd-1,:] = ept
    536661        cutpolygon[iip:iip+Nadd,:] = cutline
    537         if keep == 'bottom':
     662        if keep == 'left':
    538663            cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
    539664
     
    15671692      'sign2': [buoy[N2+N32+2:N,:],'-','k',1.5]}
    15681693
     1694    # painting it
     1695    Height = height + 1.8*bradii
     1696#    print 'Lluis Height', Height
     1697#    Ncut, halfdown = cut_ypolygon(buoy, yval=Height/2., keep='bottom')
     1698#    Ncut, halfup = cut_ypolygon(buoy, yval=Height/2., keep='above')
     1699
     1700#    buoysecs.append('halfk')
     1701#    buoydic['halfk'] = [halfup, '-', 'k', 1.]
     1702#    buoysecs.append('halfy')
     1703#    buoydic['halfy'] = [halfdown, '-', '#FFFF00', 1.]
     1704
    15691705    return buoy, buoysecs, buoydic
    15701706
Note: See TracChangeset for help on using the changeset viewer.