Changeset 2579 in lmdz_wrf


Ignore:
Timestamp:
Jun 2, 2019, 5:32:08 PM (5 years ago)
Author:
lfita
Message:

Adding to `cut_xpolygon' once polygon has no mask (all masked values as False, which gives a scalar)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2578 r2579  
    458458    ept = None
    459459
     460    # There might be more than 1 cut...
     461    Ncuts = 0
     462    icut = []
     463    ecut = []
     464    ipt = []
     465    ept = []
     466
    460467    if type(polygon) == type(gen.mamat):
    461468        # Assuming clockwise polygons
     
    466473     
    467474                if polygon[ip,0] <= yval and polygon[eep,0] >= yval:
    468                     icut = ip
     475                    icut.append(ip)
    469476                    dx = polygon[eep,1] - polygon[ip,1]
    470477                    dy = polygon[eep,0] - polygon[ip,0]
    471478                    dd = yval - polygon[ip,0]
    472                     ipt = [yval, polygon[ip,1]+dx*dd/dy]
     479                    ipt.append([yval, polygon[ip,1]+dx*dd/dy])
    473480
    474481                if polygon[ip,0] >= yval and polygon[eep,0] <= yval:
    475                     ecut = ip
     482                    ecut.append(ip)
    476483                    dx = polygon[eep,1] - polygon[ip,1]
    477484                    dy = polygon[eep,0] - polygon[ip,0]
    478485                    dd = yval - polygon[ip,0]
    479                     ept = [yval, polygon[ip,1]+dx*dd/dy]
     486                    ept.append([yval, polygon[ip,1]+dx*dd/dy])
     487                    Ncuts = Nctus + 1
    480488    else:
    481489        # Assuming clockwise polygons
     
    485493     
    486494            if polygon[ip,0] <= yval and polygon[eep,0] >= yval:
    487                 icut = ip
     495                icut.append(ip)
    488496                dx = polygon[eep,1] - polygon[ip,1]
    489497                dy = polygon[eep,0] - polygon[ip,0]
    490498                dd = yval - polygon[ip,0]
    491                 ipt = [yval, polygon[ip,1]+dx*dd/dy]
     499                ipt.append([yval, polygon[ip,1]+dx*dd/dy])
    492500
    493501            if polygon[ip,0] >= yval and polygon[eep,0] <= yval:
    494                 ecut = ip
     502                ecut.append(ip)
    495503                dx = polygon[eep,1] - polygon[ip,1]
    496504                dy = polygon[eep,0] - polygon[ip,0]
    497505                dd = yval - polygon[ip,0]
    498                 ept = [yval, polygon[ip,1]+dx*dd/dy]
     506                ept.append([yval, polygon[ip,1]+dx*dd/dy])
    499507
    500508    if ipt is None or ept is None:
     
    502510        print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
    503511
    504     if keep == 'bottom':
    505         Npts = icut + (N-ecut) + Nadd
    506         cutpolygon = np.zeros((Npts,2), dtype=np.float)
    507         if type(polygon) == type(gen.mamat):
    508             cutpolygon[0:icut+1,:] = polygon[0:icut+1,:]
     512    Nadds = []
     513    Naddc = Nadd/(Ncuts-1)
     514    for ic in range(Ncuts-1):
     515        Nadds.append(Naddc)
     516
     517    Nadds.append(N-Naddc*(Ncuts-1))
     518
     519    iip = 0
     520    iipc = 0
     521    for ic in range(Ncuts):
     522        if keep == 'bottom':
     523            Npts = icut[ic] + (N-ecut[ic]) + Nadds[ic]
     524            cutpolygon = np.zeros((Npts,2), dtype=np.float)
     525            if type(polygon) == type(gen.mamat):
     526                cutpolygon[iipc:icut[ic]+1,:] = polygon[iip:icut[ic]+1,:]
     527            else:
     528                cutpolygon[iipc:icut[ic]+1,:] = polygon[iip:icut[ic]+1,:]
     529            iip = icut[ic]+1
    509530        else:
    510             cutpolygon[0:icut+1,:] = polygon[0:icut+1,:]
    511         iip = icut+1
    512     else:
    513         Npts = ecut - icut + Nadd-1
    514         cutpolygon = np.zeros((Npts,2), dtype=np.float)
    515         cutpolygon[0,:] = ipt
    516         cutpolygon[1:ecut-icut,:] = polygon[icut+1:ecut,:]
    517         iip = ecut-icut-1
    518 
    519     # cutting line
    520     cutline = np.zeros((Nadd,2), dtype=np.float)
    521     dx = (ept[1] - ipt[1])/(Nadd-2)
    522     dy = (ept[0] - ipt[0])/(Nadd-2)
    523     cutline[0,:] = ipt
    524     for ip in range(1,Nadd-1):
    525         cutline[ip,:] = ipt + np.array([dy*ip,dx*ip])
    526     cutline[Nadd-1,:] = ept
    527     if keep == 'bottom':
    528         cutpolygon[iip:iip+Nadd,:] = cutline
    529         if type(polygon) == type(gen.mamat):
    530             cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
     531            Npts = ecut[ec] - icut[ic] + Nadds[ic]-1
     532            cutpolygon = np.zeros((Npts,2), dtype=np.float)
     533            cutpolygon[iipc,:] = ipt[ic]
     534            cutpolygon[iipc+1:ecut[ic]-icut[ic],:] = polygon[icut[ic]+1:ecut[ic],:]
     535            iip = ecut[ic]-icut[ic]-1
     536
     537        # cutting line
     538        cutline = np.zeros((Nadds[ic],2), dtype=np.float)
     539        dx = (ept[ic][1] - ipt[ic][1])/(Nadds[ic]-2)
     540        dy = (ept[ic][0] - ipt[ic][0])/(Nadds[ic]-2)
     541        cutline[0,:] = ipt[ic]
     542        for ip in range(1,Nadds[ic]-1):
     543            cutline[ip,:] = ipt[ic] + np.array([dy*ip,dx*ip])
     544        cutline[Nadds[ic]-1,:] = ept[ic]
     545        if keep == 'bottom':
     546            cutpolygon[iip:iip+Nadds[ic],:] = cutline
     547            cutpolygon[iip+Nadds[ic]:Npts,:] = polygon[ecut[ic]+1:N,:]
    531548        else:
    532             cutpolygon[iip+Nadd:Npts,:] = polygon[ecut+1:N,:]
    533     else:
    534         cutpolygon[iip:iip+Nadd,:] = cutline[::-1,:]
     549            cutpolygon[iip:iip+Nadds[ic],:] = cutline[::-1,:]
    535550
    536551    rmpolygon = []
     
    577592    ept = None
    578593
    579     if type(polygon) == type(gen.mamat):
     594    if type(polygon) == type(gen.mamat) and type(polygon.mask) !=                    \
     595      type(gen.mamat.mask[1]):
    580596        # Assuming clockwise polygons
    581597        for ip in range(N-1):
    582             if not polygon.mask[ip,0]:
     598            if not polygon.mask[ip,1]:
    583599                eep = ip + 1
    584600                if eep == N: eep = 0
     
    619635    if ipt is None or ept is None:
    620636        print errormsg
    621         print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
     637        print '  ' + fname + ': no cutting for polygon at x=', xval, '!!'
    622638
    623639    if keep == 'left':
Note: See TracChangeset for help on using the changeset viewer.