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


Ignore:
Timestamp:
Apr 19, 2019, 5:06:16 AM (6 years ago)
Author:
lfita
Message:

Adding:

  • `p_circle': Function to get a polygon of a circle
  • `p_square': Function to get a polygon square
  • `read_join_poly': Function to read an ASCII file with the combination of polygons
  • `write_join_poly': Function to write an ASCII file with the combination of polygons
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2438 r2449  
    2626# position_sphere: Function to tranform fom a point in lon, lat deg coordinates to
    2727#   cartesian coordinates over an sphere
     28# p_circle: Function to get a polygon of a circle
     29# p_square: Function to get a polygon square
     30# read_join_poly: Function to read an ASCII file with the combination of polygons
    2831# rotate_2D: Function to rotate a vector by a certain angle in the plain
    2932# rotate_line2D: Function to rotate a line given by 2 pairs of x,y coordinates by a
     
    3336# spheric_line: Function to transform a series of locations in lon, lat coordinates
    3437#   to x,y,z over an 3D spaceFunction to provide coordinates of a line  on a 3D space
     38# write_join_poly: Function to write an ASCII file with the combination of polygons
    3539
    3640## Shapes/objects
    3741# ellipse_polar: Function to determine an ellipse from its center and polar coordinates
    3842# surface_sphere: Function to provide an sphere as matrix of x,y,z coordinates
     43
    3944
    4045## Plotting
     
    384389    return circ_sec
    385390
     391def p_square(face, N=5):
     392    """ Function to get a polygon square
     393      face: length of the face of the square
     394      N: number of points of the polygon
     395    """
     396    fname = 'p_square'
     397
     398    square = np.zeros((N,2), dtype=np.float)
     399
     400    f2 = face/2.
     401    N4 = N/4
     402    df = face/(N4)
     403    # SW-NW
     404    for ip in range(N4):
     405        square[ip,:] = [-f2+ip*df,-f2]
     406    # NW-NE
     407    for ip in range(N4):
     408        square[ip+N4,:] = [f2,-f2+ip*df]
     409    # NE-SE
     410    for ip in range(N4):
     411        square[ip+2*N4,:] = [f2-ip*df,f2]
     412    N42 = N-3*N4-1
     413    df = face/(N42)
     414    # SE-SW
     415    for ip in range(N42):
     416        square[ip+3*N4,:] = [-f2,f2-ip*df]
     417    square[N-1,:] = [-f2,-f2]
     418
     419    return square
     420
     421def p_circle(radii, N=50):
     422    """ Function to get a polygon of a circle
     423      radii: length of the radii of the circle
     424      N: number of points of the polygon
     425    """
     426    fname = 'p_circle'
     427
     428    circle = np.zeros((N,2), dtype=np.float)
     429
     430    dangle = 2.*np.pi/(N-1)
     431
     432    for ia in range(N):
     433        circle[ia,:] = [radii*np.sin(ia*dangle), radii*np.cos(ia*dangle)]
     434
     435    circle[N-1,:] = [0., radii]
     436
     437    return circle
     438
     439# Combined objects
     440##
     441
    386442# FROM: http://www.photographers1.com/Sailing/NauticalTerms&Nomenclature.html
    387443def zsailing_boat(length=10., beam=1., lbeam=0.4, sternbp=0.5):
     
    434490 
    435491    return boat
     492
     493def write_join_poly(polys, flname='join_polygons.dat'):
     494    """ Function to write an ASCII file with the combination of polygons
     495      polys: dictionary with the names of the different polygons
     496      flname: name of the ASCII file
     497    """
     498    fname = 'write_join_poly'
     499
     500    of = open(flname, 'w')
     501
     502    for polyn in polys.keys():
     503        vertices = polys[polyn]
     504        Npts = vertices.shape[0]
     505        for ip in range(Npts):
     506            of.write(polyn+' '+str(vertices[ip,1]) + ' ' + str(vertices[ip,0]) + '\n')
     507
     508    of.close()
     509
     510    return
     511
     512def read_join_poly(flname='join_polygons.dat'):
     513    """ Function to read an ASCII file with the combination of polygons
     514      flname: name of the ASCII file
     515    """
     516    fname = 'read_join_poly'
     517
     518    of = open(flname, 'r')
     519
     520    polys = {}
     521    polyn = ''
     522    poly = []
     523    for line in of:
     524        if len(line) > 1:
     525            linevals = line.replace('\n','').split(' ')
     526            if polyn != linevals[0]:
     527                if len(poly) > 1:
     528                    polys[polyn] = np.array(poly)
     529                polyn = linevals[0]
     530                poly = []
     531                poly.append([np.float(linevals[2]), np.float(linevals[1])])
     532            else:
     533                poly.append([np.float(linevals[2]), np.float(linevals[1])])
     534
     535    of.close()
     536    polys[polyn] = np.array(poly)
     537
     538    return polys
    436539
    437540####### ####### ##### #### ### ## #
     
    599702
    600703    return fig, ax
    601 
Note: See TracChangeset for help on using the changeset viewer.