Changeset 2580 in lmdz_wrf


Ignore:
Timestamp:
Jun 2, 2019, 5:47:28 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `val_between': Function to provide if a given value is between two consecutive ones
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2579 r2580  
    4343# spheric_line: Function to transform a series of locations in lon, lat coordinates
    4444#   to x,y,z over an 3D spaceFunction to provide coordinates of a line  on a 3D space
     45# val_between: Function to provide if a given value is between two consecutive ones
    4546# write_join_poly: Function to write an ASCII file with the combination of polygons
    4647
     
    435436    return polys
    436437
     438def val_between(valA, valB, val):
     439    """ Function to provide if a given value is between two consecutive ones
     440      valA: first value
     441      valB: second value
     442      val: value to determine if it is between
     443      >>> val_between(0.5,1.5,0.8)
     444      True
     445      >>> val_between(0.5,1.5.,-0.8)
     446      False
     447      >>> val_between(0.5,1.5,0.5)
     448      True
     449    """
     450    fname = 'val_between'
     451
     452    btw = False
     453    if (valA <= val and valB > val) or (valA < val and valB >= val): btw =True
     454
     455    return btw
     456
    437457def cut_ypolygon(polygon, yval, keep='bottom', Nadd=20):
    438458    """ Function to cut a polygon from a given value of the y-axis
     
    465485    ept = []
    466486
    467     if type(polygon) == type(gen.mamat):
     487    if type(polygon) == type(gen.mamat) and type(polygon.mask) !=                    \
     488      type(gen.mamat.mask[1]):
    468489        # Assuming clockwise polygons
    469490        for ip in range(N-1):
     
    472493                if eep == N: eep = 0
    473494     
    474                 if polygon[ip,0] <= yval and polygon[eep,0] >= yval:
     495                if val_between(polygon[ip,0], polygon[eep,0], yval):
    475496                    icut.append(ip)
    476497                    dx = polygon[eep,1] - polygon[ip,1]
     
    485506                    dd = yval - polygon[ip,0]
    486507                    ept.append([yval, polygon[ip,1]+dx*dd/dy])
    487                     Ncuts = Nctus + 1
     508                    Ncuts = Ncuts + 1
    488509    else:
    489510        # Assuming clockwise polygons
     
    505526                dd = yval - polygon[ip,0]
    506527                ept.append([yval, polygon[ip,1]+dx*dd/dy])
    507 
    508     if ipt is None or ept is None:
     528                Ncuts = Ncuts + 1
     529
     530    if ipt is None or ept is None or Ncuts == 0:
    509531        print errormsg
    510532        print '  ' + fname + ': no cutting for polygon at y=', yval, '!!'
     533    else:
     534        print '  ' + fname + ': found ', Ncuts, ' Ncuts'
     535        print '    yval=', yval, 'cut, ip; ipt ep; ept ________'
     536        for ic in range(Ncuts):
     537            print ic, icut[ic], ';', ipt[ic], ecut[ic], ';', ept[ic]
    511538
    512539    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))
     540    if Ncuts > 1:
     541        Naddc = Nadd/(Ncuts-1)
     542        for ic in range(Ncuts-1):
     543            Nadds.append(Naddc)
     544
     545        Nadds.append(N-Naddc*(Ncuts-1))
     546    else:
     547        Nadds.append(Nadd)
    518548
    519549    iip = 0
     
    550580
    551581    rmpolygon = []
     582    Npts = cutpolygon.shape[0]
    552583    if keep == 'bottom':
    553584        for ip in range(Npts):
Note: See TracChangeset for help on using the changeset viewer.