Exemple #1
0
def xyz_wf(z, y, x, scale=1.0):
    """
   xyz_wf (z, [y, x] [,scale = 1.0])
      returns a 3-by-ni-by-nj array whose 0th entry is x, 1th entry
      is y, and 2th entry is z. z is ni-by-nj. x and y, if present,
      must be the same shape. If not present, integer ranges will
      be used to create an equally spaced coordinate grid in x and y.
      The function which scales the "topography" of z(x,y) is
      potentially useful apart from plwf.
      For example, the xyz array used by plwf can be converted from
      a quadrilateral mesh plotted using plf to a polygon list plotted
      using plfp like this:
        xyz= xyz_wf(z,y,x,scale=scale);
        ni= shape(z)[1];
        nj= shape(z)[2];
        list = ravel (add.outer (
           ravel(add.outer (adders,zeros(nj-1, int32))) +
           arange((ni-1)*(nj-1), dtype= int32),
           array ( [[0, 1], [nj + 1, nj]])))
        xyz=array([take(ravel(xyz[0]),list),
           take(ravel(xyz[1]),list),
           take(ravel(xyz[2]),list)])
        nxyz= ones((ni-1)*(nj-1)) * 4;
      The resulting array xyz is 3-by-(4*(nj-1)*(ni-1)).
      xyz[0:3,4*i:4*(i+1)] are the clockwise coordinates of the
      vertices of cell number i.
   """

    if len(shape(z)) < 2:
        raise _Xyz_wfError, "impossible dimensions for z array"
    nx = shape(z)[0]
    ny = shape(z)[1]
    if y == None or x == None:
        if x != None or y != None:
            raise _Xyz_wfError, "either give y,x both or neither"
        x = gistfuncs.span(0, ny - 1, ny, nx)
        y = transpose(gistfuncs.span(0, nx - 1, nx, ny))
    elif shape(x) != shape(z) or shape(y) != shape(z):
        raise _Xyz_wfError, "x, y, and z must all have same dimensions"
    xyscl = max(maxelt_(x) - minelt_(x), maxelt_(y) - minelt_(y))
    if scale != None:
        xyscl = xyscl * scale
    dz = maxelt_(z) - minelt_(z)
    zscl = dz + (dz == 0.0)
    if zscl:
        z = z * 0.5 * xyscl / zscl
    xbar = avg_(x)
    ybar = avg_(y)
    zbar = avg_(z)
    xyz = array([x - xbar, y - ybar, z - zbar], float32)
    return (xyz)
Exemple #2
0
def nice_levels(z, n=8):
    """nice_levels(z, n = 8) finds approximately n "nice values"
    between min(z) and max(z) for axis labels. n defaults to eight.
    """
    zmax = max(ravel(z))
    zmin = min(ravel(z))
    finest = abs(zmax - zmin) / float(n)
    # blows up on zmin=zmax
    unit = 10.**floor(log10(finest))
    finest = finest / unit
    if finest > 5.0:
        finest = 10.
    elif finest > 2.:
        finest = 5.
    elif finest > 1.:
        finest = 2.
    unit = unit * finest
    cmin = unit * ceil(zmin / unit)
    if (abs(cmin - zmin) < 0.01 * unit):
        cmin = cmin + unit
    cmax = unit * floor(zmax / unit)
    if (abs(cmax - zmax) < 0.01 * unit):
        cmax = cmax - unit
    n = int(((cmax - cmin) / unit + 0.5) + 1)
    levs = gistfuncs.span(cmin, cmax, n)
    list = numpy.nonzero(less(abs(levs), 0.1 * unit))[0]
    if len(list) > 0:
        gistfuncs.array_set(levs, list, 0.0)
    return levs
Exemple #3
0
def spann(zmin, zmax, n=8, fudge=0, force=0):
    """
   spann (zmin, zmax, n = 8, fudge = 0, force = 0)
      return no more than N equally spaced "nice" numbers between
      ZMIN and ZMAX.
      Note that in general spann may not supply the number of
      values that you asked for. To force it to do so, set
      keyword FORCE to nonzero.
      SEE ALSO: span, spanl, plc, plfc
   """
    dz = (zmax - zmin) / max(float(n), 0.)
    if dz == 0.:
        dz = abs(zmin)
    if (dz != 0.):
        power = floor(log10(dz) + 0.00001)
        base = dz / (10.**power)
        if base > 5.00001:
            base = 1.0
            power = power + 1.0
        elif base > 2.00001:
            base = 5.0
        else:
            base = 2.0
        # Round dz up to nearest "nice" number
        dz = base * 10.0**power
        zmin = ceil(zmin / dz - fudge)
        zmax = floor(zmax / dz + fudge)
        if (force == 0):
            nz = int(zmax - zmin + 1.0)
        else:
            nz = n
        if nz > 1:
            levs = gistfuncs.span(zmin * dz, zmax * dz, nz)
        else:
            if nz < 1:
                if base < 1.5:
                    base = 5.0
                    power = power - 1
                elif base < 2.5:
                    base = 1.0
                else:
                    base = 2.0
                dz = base * 10.0**power
                zmin = ceil(zmin / dz + 0.001)
            levs = array([zmin * dz])
    else:
        levs = array([-1.0, 1.0])
    return (levs)
Exemple #4
0
def run(which=None, time_limit=60):
    """Exhibit quadrilateral mesh plots in 3 movies of a drumhead.
     The drumhead is initially stationary, but has a bump near one
     edge.  Yorick is solving a 2D wave equation to compute the
     evolution of this bump.

     The first movie is a filled mesh plot with color "proportional"
     to the height of the surface of the drum.  A few well chosen
     contour levels (here 3) add a lot to a filled mesh plot.

     The second movie is a "3D" perspective plot of the height of the
     drumhead.  In this movie, the mesh lines are drawn, which is
     slightly confusing since the cells are not all the same shape.

     The second movie is a "3D" shaded plot of the height of the
     drumhead.  Yorick computes surface shading based on the angle
     of each cell from a light source.

     As you watch this, you might reflect on the two dimensionality
     of your retina.  What Yorick lacks by way of 3D graphics is
     really just fancy hidden surface algorithms; the simple
     painter's algorithm used here and in plwf.py is easy to
     implement.

     There are two optional arguments to demo2: the first is the
     number of the movie (1, 2, or 3) you want to watch; the second
     is a time limit on the duration of each movie in seconds (default
     is 60 seconds each)."""
    import movie
    global f, fdot, dt, x, y, level

    # generate a 30-by-30 cell mesh on the [-1,1] square
    x = gistfuncs.span(-1, 1, 31, 31)
    y = transpose(x)
    # map the square mesh into a mesh on the unit circle
    # this mesh has more nearly equal area cells than a polar
    # coordinate circle
    scale = maximum(abs(y), abs(x)) / (hypot(y, x) + 1.e-30)
    x = x * scale
    y = y * scale

    f = exp(-8. * hypot(y + .67, x + .25)**2) * (1. - hypot(y, x)**2)
    f0 = numpy.array(f)  # get an independent copy
    fdot = 0.0 * f[1:-1, 1:-1]

    lf = laplacian(f, y, x)
    xdz = x[1:, 1:] + x[:-1, 1:] - x[1:, :-1] - x[:-1, :-1]
    xzd = x[1:, 1:] - x[:-1, 1:] + x[1:, :-1] - x[:-1, :-1]
    ydz = y[1:, 1:] + y[:-1, 1:] - y[1:, :-1] - y[:-1, :-1]
    yzd = y[1:, 1:] - y[:-1, 1:] + y[1:, :-1] - y[:-1, :-1]
    dt = 0.1875 * numpy.sqrt(
        numpy.min(numpy.min(numpy.abs(xdz * yzd - xzd * ydz))))

    window(0, wait=1, style="nobox.gs")
    palette("heat.gp")
    limits(-1, 1, -1, 1)

    # roll the filled mesh movie
    if which == None or which == 1:
        fc = (f[1:, 1:] + f[:-1, 1:] + f[1:, :-1] + f[:-1, :-1]) / 4.
        cmin = cmax = max([max(abs(row)) for row in fc])
        cmin = -cmin
        level = cmax / 4.
        display_plf(0)
        fixedlimits = limits()
        movie.movie(display_plf, time_limit, lims=fixedlimits, timing=1)
        # Note; movie_timing is a global variable in movie.py
        print movie.movie_timing[
            3], "frames of filled mesh drumhead completed in",
        print movie.movie_timing[2], "sec"
        print "Rate for filled mesh is",
        print movie.movie_timing[3] / (movie.movie_timing[0] -
                                       movie.movie_timing[4] + 1.0e-4),
        print "frames/(CPU sec),",
        print movie.movie_timing[3] / (movie.movie_timing[2] -
                                       movie.movie_timing[4] + 1.0e-4),
        print "frames(wall sec)"

    # roll the perspective movie */
    if which == None or which == 2:
        f[:, :] = f0
        limits(-1, 1, -1, 1)
        display_plm(0)
        fixedlimits = limits()
        movie.movie(display_plm, time_limit, lims=fixedlimits, timing=1)
        print movie.movie_timing[
            3], "frames of wireframe surface drumhead completed in",
        print movie.movie_timing[2], "sec"
        print "Rate for filled mesh is",
        print movie.movie_timing[3] / (movie.movie_timing[0] -
                                       movie.movie_timing[4] + 1.0e-4),
        print "frames/(CPU sec),",
        print movie.movie_timing[3] / (movie.movie_timing[2] -
                                       movie.movie_timing[4] + 1.0e-4),
        print "frames(wall sec)"

    # roll the shaded movie
    if which == None or which == 3:
        f[:, :] = f0
        limits(-1, 1, -1, 1)
        display_pl3(0)
        fixedlimits = limits()
        movie.movie(display_pl3, time_limit, lims=fixedlimits, timing=1)
        print movie.movie_timing[
            3], "frames of filled surface drumhead completed in",
        print movie.movie_timing[2], "sec"
        print "Rate for filled mesh is",
        print movie.movie_timing[3] / (movie.movie_timing[0] -
                                       movie.movie_timing[4] + 1.0e-4),
        print "frames/(CPU sec),",
        print movie.movie_timing[3] / (movie.movie_timing[2] -
                                       movie.movie_timing[4] + 1.0e-4),
        print "frames(wall sec)"

        fma()
        limits()
Exemple #5
0
def run(*itest):
    """run() or run(i)
     Run examples of use of pl3d.i, plwf.i, and slice3.i.  With
     argument I = 1, 2, or 3, run that particular demonstration.
     Read the source code to understand the details of how the
     various effects are obtained.

     run (1) demonstrates the various effects which can be obtained
     with the plwf (plot wire frame) function.
     run (2) demonstrates shading effects controlled by the light3
     function
     run (3) demonstrates the slice3, slice2, and pl3tree functions,
     as well as changing the orientation of the 3D object
   """
    global making_movie

    if len(itest) == 0 or itest[0] == 1:
        set_draw3_(0)
        x = gistfuncs.span(-1, 1, 64, 64)
        y = transpose(x)
        z = (x + y) * exp(-6. * (x * x + y * y))
        limits_(square=1)
        print "Plot wire frame: plwf (z, y, x)"
        orient3()
        light3()
        plwf(z, y, x)
        [xmin, xmax, ymin, ymax] = draw3(1)  # not necessary interactively
        limits(xmin, xmax, ymin, ymax)
        plt("opaque wire mesh", .30, .42)
        paws()

        print 'plwf(z,y,x,shade=1,ecolor="red")'
        plwf(z, y, x, shade=1, ecolor="red")
        [xmin, xmax, ymin, ymax] = draw3(1)  # not necessary interactively
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "plwf(z,y,x,shade=1,edges=0)"
        plwf(z, y, x, shade=1, edges=0)
        [xmin, xmax, ymin, ymax] = draw3(1)  # not necessary interactively
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "light3 ( diffuse=.1, specular=1., sdir=array([0,0,-1]))"
        light3(diffuse=.1, specular=1., sdir=array([0, 0, -1]))
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "light3 ( diffuse=.5, specular=1., sdir=array([1,.5,1]))"
        light3(diffuse=.5, specular=1., sdir=array([1, .5, 1]))
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "light3 ( ambient=.1,diffuse=.1,specular=1.,"
        print "   sdir=array([[0,0,-1],[1,.5,1]]),spower=array([4,2]))"
        light3(ambient=.1,
               diffuse=.1,
               specular=1.,
               sdir=array([[0, 0, -1], [1, .5, 1]]),
               spower=array([4, 2]))
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

    if len(itest) == 0 or itest[0] == 2:

        set_draw3_(0)
        x = gistfuncs.span(-1, 1, 64, 64)
        y = transpose(x)
        z = (x + y) * exp(-6. * (x * x + y * y))
        print "Default lighting:  light3()"
        orient3()
        light3()
        plwf(z, y, x, shade=1, edges=0)
        [xmin, xmax, ymin, ymax] = draw3(1)  # not necessary interactively
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "light3(diffuse=.2,specular=1)"
        light3(diffuse=.2, specular=1)
        limits_(square=1)
        [xmin, xmax, ymin, ymax] = draw3(1)  # not necessary interactively
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "movie(demo5_light, lims = [xmin, xmax, ymin, ymax])"
        print "demo5_light calls:"
        print "light3 (sdir = array ( [cos(theta), .25, sin(theta)], float32))"
        making_movie = 1
        movie(demo5_light, lims=[xmin, xmax, ymin, ymax])
        making_movie = 0
        fma()
        demo5_light(1)
        paws()
        light3()

    if len(itest) == 0 or itest[0] == 3:

        nx = demo5_n[0]
        ny = demo5_n[1]
        nz = demo5_n[2]
        xyz = zeros((3, nx, ny, nz), float32)
        xyz[0] = multiply.outer(gistfuncs.span(-1, 1, nx),
                                ones((ny, nz), float32))
        xyz[1] = multiply.outer(
            ones(nx, float32),
            multiply.outer(gistfuncs.span(-1, 1, ny), ones(nz, float32)))
        xyz[2] = multiply.outer(ones((nx, ny), float32),
                                gistfuncs.span(-1, 1, nz))
        r = sqrt(xyz[0]**2 + xyz[1]**2 + xyz[2]**2)
        theta = arccos(xyz[2] / r)
        phi = arctan2(xyz[1], xyz[0] + logical_not(r))
        y32 = sin(theta)**2 * cos(theta) * cos(2 * phi)
        m3 = mesh3(xyz, funcs=[r * (1. + y32)])
        del r, theta, phi, xyz, y32

        print "   test uses " + ` (nx - 1) * (ny - 1) * (nz - 1) ` + " cells"
        elapsed = [0., 0., 0.]
        elapsed = timer_(elapsed)
        elapsed0 = elapsed

        [nv, xyzv, dum] = slice3(m3, 1, None, None, value=.50)
        # (inner isosurface)
        [nw, xyzw, dum] = slice3(m3, 1, None, None, value=1.)
        # (outer isosurface)
        pxy = plane3(array([0, 0, 1], float32), zeros(3, float32))
        pyz = plane3(array([1, 0, 0], float32), zeros(3, float32))
        [np, xyzp, vp] = slice3(m3, pyz, None, None, 1)
        # (pseudo-colored slice)
        [np, xyzp, vp] = slice2(pxy, np, xyzp, vp)
        # (cut slice in half)
        [nv, xyzv, d1, nvb, xyzvb, d2] = \
            slice2x (pxy, nv, xyzv, None)
        [nv, xyzv, d1] = \
            slice2 (- pyz, nv, xyzv, None)
        # (...halve one of those halves)
        [nw, xyzw, d1, nwb, xyzwb, d2] = \
            slice2x ( pxy , nw, xyzw, None)
        # (split outer in halves)
        [nw, xyzw, d1] = \
            slice2 (- pyz, nw, xyzw, None)

        elapsed = timer_(elapsed)
        timer_print("slicing time", elapsed - elapsed0)

        fma()
        print 'Generate palette for pl3tree:  split_palette ("earth.gp")'
        split_palette("earth.gp")
        print "gnomon -- turn on gnomon"
        gnomon(1)

        print "pl3tree with 1 slicing plane, 2 isosurfaces"
        clear3()
        # Make sure we don't draw till ready
        set_draw3_(0)
        pl3tree(np, xyzp, vp, pyz)
        pl3tree(nvb, xyzvb)
        pl3tree(nwb, xyzwb)
        pl3tree(nv, xyzv)
        pl3tree(nw, xyzw)
        orient3()
        light3(diffuse=.2, specular=1)
        limits()
        limits(square=1)
        demo5_light(1)
        paws()
        hcp()

        print "spin3 animated rotation, use rot3 or orient3 for one frame"
        # don't want limits to autoscale during animation
        lims = limits()
        spin3()
        limits()  # back to autoscaling
        demo5_light(1)
        paws()

        light3()
        gnomon(0)
        limits(square=1)
        palette("gray.gp")

#  .. PR is not available.  Comment out this section.
#   if len (itest) == 0 or itest [0] == 4 :
#      from PR import *
#      f = PR ('./bills_plot')
#      n_nodes = f.NumNodes
#      n_z = f.NodesOnZones
#      x = f.XNodeCoords
#      y = f.YNodeCoords
#      z = f.ZNodeCoords
#      c = f.ZNodeVelocity
#      n_zones = f.NumZones
#      # Put vertices in right order for Gist
#      n_z = transpose (
#         take (transpose (n_z), array ( [0, 4, 3, 7, 1, 5, 2, 6])))
#      m3 = mesh3 (x, y, z, funcs = [c], verts = n_z ) # [0:10])
#      [nv, xyzv, cv] = slice3 (m3, 1, None, None, 1, value = .9 * max (c) )
#      pyz = plane3 ( array ([1, 0, 0], float32 ), zeros (3, float32))
#      pxz = plane3 ( array ([0, 1, 0], float32 ), zeros (3, float32))
#
#      # draw a colored plane first
#      fma ()
#      clear3 ()
#      # Make sure we don't draw till ready
#      set_draw3_ (0)
#      [np, xyzp, vp] = slice3 (m3, pyz, None, None, 1)
#      pl3tree (np, xyzp, vp, pyz, split = 0)
#      palette ("rainbow.gp")
#      orient3 ()
#      demo5_light (1)
#      paws ()
#
#
#     [nv, xyzv, d1] = \
#         slice2 (- pyz, nv, xyzv, None)
#      [nw, xyzw, cw] = slice3 (m3, 1, None, None, 1, value = .9 * min (c) )
#     [nw, xyzw, d1] = \
#         slice2 (- pyz, nw, xyzw, None)
#      [nvi, xyzvi, cvi] = slice3 (m3, 1, None, None, 1, value = .5 * min (c) )
#      [nvi, xyzvi, cvi] = \
#          slice2 (- pyz, nvi, xyzvi, cvi)
#      [nvj, xyzvj, cvj] = slice3 (m3, 1, None, None, 1, value = .5 * max (c) )
#      [nvj, xyzvj, cvj] = \
#          slice2 (- pyz, nvj, xyzvj, cvj)
#
#      fma ()
#      print "gnomon -- turn on gnomon"
#      gnomon (1)
#      clear3 ()
#      # Make sure we don't draw till ready
#      set_draw3_ (0)
#      pl3tree (nv, xyzv) # , cv)
#      pl3tree (nw, xyzw) # , cw)
#      pl3tree (nvi, xyzvi) # , cvi)
#      pl3tree (nvj, xyzvj) # , cvi)
#      orient3 ()
#      light3 (ambient = 0, diffuse = .5, specular = 1, sdir = [0, 0, -1])
#      limits (square=1)
#      palette ("gray.gp")
#      demo5_light (1)
#      paws ()
#
#      print "spin3 animated rotation, use rot3 or orient3 for one frame"
#      # don't want limits to autoscale during animation
#      spin3 ()
#      limits ( ) # back to autoscaling
#      demo5_light (1)
#      paws ()
#
#      light3 ()
#      gnomon (0)
#      palette ("gray.gp")
#
#      draw3 ( 1 )
#      paws ()
#      clear3 ()
#      del nv, xyzv, cv, nw, xyzw, cw, nvi, xyzvi, cvi, nvj, xyzvj, cvj
#      # Make sure we don't draw till ready
#      set_draw3_ (0)
#      for i in range (8) :
#         [nv, xyzv, cv] = slice3 (m3, 1, None, None, 1, value = .9 * min (c) +
#             i * (.9 * max (c) - .9 * min (c)) / 8.)
#         [nv, xyzv, d1] = \
#             slice2 (pxz, nv, xyzv, None)
#         pl3tree (nv, xyzv)
#      orient3 ()
#      light3 (ambient = 0, diffuse = .5, specular = 1, sdir = [0, 0, -1])
#      limits (square=1)
#      palette ("heat.gp")
#      demo5_light (1)
#      paws ()
#      spin3 ()
#      limits ( ) # back to autoscaling
#      demo5_light (1)
#      paws ()
#      demo5_light (1)
#      paws ()
#
#   if len (itest) == 0 or itest [0] == 5 :
#      # Try bert's data
#      from PR import PR
#      f = PR ('./berts_plot')
#      nums = array ( [63, 63, 49], int32)
#      dxs = array ( [2.5, 2.5, 10.], float32 )
#      x0s = array ( [-80., -80., 0.0], float32 )
#      c = f.c
#
#      m3 = mesh3 (nums, dxs, x0s, funcs = [transpose (c)])
#      [nv, xyzv, dum] = slice3 (m3, 1, None, None, value = 6.5)
#      fma ()
#      clear3 ()
#      print "gnomon -- turn on gnomon"
#      gnomon (1)
#      # Make sure we don't draw till ready
#      set_draw3_ (0)
#      palette ("rainbow.gp")
#      pl3tree (nv, xyzv)
#      orient3 ()
#      light3 (diffuse = .2, specular = 1)
#      limits (square=1)
#      demo5_light (1)
#      paws ()
#      spin3 ()
#      demo5_light (1)
#      paws ()
#   if len (itest) == 0 or itest [0] == 6 :
#      # Try Bill's irregular mesh
#      from PR import PR
#      f = PR ("ball.s0001")
#      ZLss = f.ZLstruct_shapesize
#      ZLsc = f.ZLstruct_shapecnt
#      ZLsn = f.ZLstruct_nodelist
#      x = f.sap_mesh_coord0
#      y = f.sap_mesh_coord1
#      z = f.sap_mesh_coord2
#      c = f.W_vel_data
#      # Now we need to convert this information to avs-style data
#      istart = 0 # beginning index into ZLstruct_nodelist
#      NodeError = "NodeError"
#      ntet = 0
#      nhex = 0
#      npyr = 0
#      nprism = 0
#      nz_tet = []
#      nz_hex = []
#      nz_pyr = []
#      nz_prism = []
#      for i in range (4) :
#         if ZLss [i] == 4 : # TETRAHEDRON
#            nz_tet = reshape (ZLsn [istart: istart + ZLss [i] * ZLsc [i]],
#                     (ZLsc [i], ZLss [i]))
#            ntet = ZLsc [i]
#            istart = istart + ZLss [i] * ZLsc [i]
#         elif ZLss[i] == 5 : # PYRAMID
#            nz_pyr = reshape (ZLsn [istart: istart + ZLss [i] * ZLsc [i]],
#                     (ZLsc [i], ZLss [i]))
#            npyr = ZLsc [i]
#            # Now reorder the points (bill has the apex last instead of first)
#            nz_pyr = transpose (
#               take (transpose (nz_pyr), array ( [4, 0, 1, 2, 3])))
#            istart = istart + ZLss [i] * ZLsc [i]
#         elif ZLss[i] == 6 : # PRISM
#            nz_prism = reshape (ZLsn [istart: istart + ZLss [i] * ZLsc [i]],
#                     (ZLsc [i], ZLss [i]))
#            nprism = ZLsc [i]
#            # now reorder the points (bill goes around a square face
#            # instead of traversing the opposite sides in the same direction.
#            nz_prism = transpose (
#               take (transpose (nz_prism), array ( [0, 1, 3, 2, 4, 5])))
#            istart = istart + ZLss [i] * ZLsc [i]
#         elif ZLss[i] == 8 : # HEXAHEDRON
#            nz_hex = reshape (ZLsn [istart: istart + ZLss [i] * ZLsc [i]],
#                     (ZLsc [i], ZLss [i]))
#            # now reorder the points (bill goes around a square face
#            # instead of traversing the opposite sides in the same direction.
#            nz_hex = transpose (
#               take (transpose (nz_hex), array ( [0, 1, 3, 2, 4, 5, 7, 6])))
#            nhex = ZLsc [i]
#            istart = istart + ZLss [i] * ZLsc [i]
#         else :
#            raise NodeError, `ZLss[i]` + "is an incorrect number of nodes."

#      m3 = mesh3 (x, y, z, funcs = [c], verts = [nz_tet, nz_pyr, nz_prism,
#         nz_hex])
#      [nv, xyzv, cv] = slice3 (m3, 1, None, None, 1, value = .9 * max (c) )
#      pyz = plane3 ( array ([1, 0, 0], float32 ), zeros (3, float32))
#      pxz = plane3 ( array ([0, 1, 0], float32 ), zeros (3, float32))
#
#      # draw a colored plane first
#      fma ()
#      clear3 ()
#      # Make sure we don't draw till ready
#      set_draw3_ (0)
#      [np, xyzp, vp] = slice3 (m3, pyz, None, None, 1)
#      pl3tree (np, xyzp, vp, pyz, split = 0)
#      palette ("rainbow.gp")
#      orient3 ()
#      limits (square=1)
#      demo5_light (1)
#      paws ()
#
#      [nw, xyzw, cw] = slice3 (m3, 1, None, None, 1, value = .9 * min (c) )
#      [nvi, xyzvi, cvi] = slice3 (m3, 1, None, None, 1, value = .1 * min (c) )
#      [nvi, xyzvi, cvi] = \
#          slice2 (- pyz, nvi, xyzvi, cvi)
#      [nvj, xyzvj, cvj] = slice3 (m3, 1, None, None, 1, value = .1 * max (c) )
#      [nvj, xyzvj, cvj] = \
#          slice2 (- pyz, nvj, xyzvj, cvj)
#      [nvii, xyzvii, cvii] = slice3 (m3, 1, None, None, 1,
#         value = 1.e-12 * min (c) )
#      [nvii, xyzvii, cvii] = \
#          slice2 (- pyz, nvii, xyzvii, cvii)
#      [nvjj, xyzvjj, cvjj] = slice3 (m3, 1, None, None, 1,
#         value = 1.e-12 * max (c) )
#      [nvjj, xyzvjj, cvjj] = \
#          slice2 (- pyz, nvjj, xyzvjj, cvjj)
#
#      fma ()
#      print "gnomon -- turn on gnomon"
#      gnomon (1)
#      clear3 ()
#      # Make sure we don't draw till ready
#      set_draw3_ (0)
#      pl3tree (nv, xyzv) # , cv)
#      pl3tree (nw, xyzw) # , cw)
#      pl3tree (nvi, xyzvi) # , cvi)
#      pl3tree (nvj, xyzvj) # , cvj)
#      pl3tree (nvii, xyzvii) # , cvii)
#      pl3tree (nvjj, xyzvjj) # , cvjj)
#      orient3 ()
#      light3 (ambient = 0, diffuse = .5, specular = 1, sdir = [0, 0, -1])
#      limits (square=1)
#      palette ("gray.gp")
#      demo5_light (1)
#      paws ()
#      palette ("heat.gp")
#      paws ()

    if len(itest) == 0 or itest[0] == 7:

        print "Test plwf on the sombrero function"
        # compute sombrero function
        x = arange(-20, 21, dtype=float32)
        y = arange(-20, 21, dtype=float32)
        z = zeros((41, 41), float32)
        r = sqrt(add.outer(x**2, y**2)) + 1e-6
        z = sin(r) / r
        fma()
        clear3()
        gnomon(0)
        # Make sure we don't draw till ready
        set_draw3_(0)
        palette("rainbow.gp")
        limits(square=1)
        orient3()
        light3()
        plwf(z, fill=z, ecolor="black")
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "Try smooth contours, log mode:"
        print 'plzcont (nv, xyzv, contours = 20, scale = "normal")'
        [nv, xyzv, dum] = slice3mesh(x, y, z)
        zmult = max(max(abs(x)), max(abs(y)))
        plzcont(nv, xyzv, contours=20, scale="normal")
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print 'plzcont (nv, xyzv, contours = 20, scale = "lin", edges=1)'
        plzcont(nv, xyzv, contours=20, scale="lin", edges=1)
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print 'plwf (z, fill = z, shade = 1, ecolor = "black")'
        plwf(z, fill=z, shade=1, ecolor="black")
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "plwf (z, fill = z, shade = 1, edges = 0)"
        plwf(z, fill=z, shade=1, edges=0)
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "light3(diffuse=.2,specular=1)"
        print "demo5_light calls:"
        print "light3 (sdir = array ( [cos(theta), .25, sin(theta)], float32))"
        light3(diffuse=.2, specular=1)
        making_movie = 1
        movie(demo5_light, lims=[xmin, xmax, ymin, ymax])
        making_movie = 0
        fma()
        demo5_light(1)
        paws()

        print "plwf (z, fill = None, shade = 1, edges = 0)"
        plwf(z, fill=None, shade=1, edges=0)
        [xmin, xmax, ymin, ymax] = draw3(1)
        palette("gray.gp")
        limits(xmin, xmax, ymin, ymax)
        paws()

    if len(itest) == 0 or itest[0] == 8:

        print "Test pl3surf on the sombrero function"
        # compute sombrero function
        nc1 = 100
        nv1 = nc1 + 1
        br = -(nc1 / 2)
        tr = nc1 / 2 + 1
        x = arange(br, tr, dtype=float32) * 40. / nc1
        y = arange(br, tr, dtype=float32) * 40. / nc1
        z = zeros((nv1, nv1), float32)
        r = sqrt(add.outer(x**2, y**2)) + 1e-6
        z = sin(r) / r
        # In order to use pl3surf, we need to construct a mesh
        # using mesh3. The way I am going to do that is to define
        # a function on the 3d mesh so that the sombrero function
        # is its 0-isosurface.
        z0 = min(ravel(z))
        z0 = z0 - .05 * abs(z0)
        maxz = max(ravel(z))
        maxz = maxz + .05 * abs(maxz)
        zmult = max(max(abs(x)), max(abs(y)))
        dz = (maxz - z0)
        nxnynz = array([nc1, nc1, 1], int32)
        dxdydz = array([1.0, 1.0, zmult * dz], float32)
        x0y0z0 = array([float(br), float(br), z0 * zmult], float32)
        meshf = zeros((nv1, nv1, 2), float32)
        meshf[:, :, 0] = zmult * z - (x0y0z0[2])
        meshf[:, :, 1] = zmult * z - (x0y0z0[2] + dxdydz[2])

        m3 = mesh3(nxnynz, dxdydz, x0y0z0, funcs=[meshf])
        fma()
        # Make sure we don't draw till ready
        set_draw3_(0)
        pldefault(edges=0)
        [nv, xyzv, col] = slice3(m3, 1, None, None, value=0.)
        orient3()
        pl3surf(nv, xyzv)
        lim = draw3(1)
        limits(lim[0], lim[1], 1.5 * lim[2], 1.5 * lim[3])
        palette("gray.gp")
        paws()

        print "Try new slicing function (slice3mesh) to get color graph"
        [nv, xyzv, col] = slice3mesh(nxnynz[0:2],
                                     dxdydz[0:2],
                                     x0y0z0[0:2],
                                     zmult * z,
                                     color=zmult * z)
        pl3surf(nv, xyzv, values=col)
        lim = draw3(1)
        dif = 0.5 * (lim[3] - lim[2])
        limits(lim[0], lim[1], lim[2] - dif, lim[3] + dif)

        palette("rainbow.gp")
        paws()

        print "Try plzcont -- see if smooth mode is possible"
        print "plzcont (nv, xyzv)"
        palette("heat.gp")
        plzcont(nv, xyzv)
        draw3(1)
        paws()

        print "plzcont (nv, xyzv, contours = 20)"
        plzcont(nv, xyzv, contours=20)
        draw3(1)
        paws()

        print 'plzcont (nv, xyzv, contours = 20, scale = "log")'
        plzcont(nv, xyzv, contours=20, scale="log")
        draw3(1)
        paws()

        print 'plzcont (nv, xyzv, contours = 20, scale = "normal")'
        plzcont(nv, xyzv, contours=20, scale="normal")
        draw3(1)
        paws()

    if len(itest) == 0 or itest[0] == 9:

        vsf = 0.
        c = 1
        s = 1000.
        kmax = 25
        lmax = 35

        # The following computations define an interesting 3d surface.

        xr = multiply.outer(arange(1, kmax + 1, dtype=float32),
                            ones(lmax, float32))
        yr = multiply.outer(ones(kmax, float32),
                            arange(1, lmax + 1, dtype=float32))
        zt = 5. + xr + .2 * random_sample(kmax, lmax)  # ranf (xr)
        rt = 100. + yr + .2 * random_sample(kmax, lmax)  # ranf (yr)
        z = s * (rt + zt)
        z = z + .02 * z * random_sample(kmax, lmax)  # ranf (z)
        ut = rt / sqrt(rt**2 + zt**2)
        vt = zt / sqrt(rt**2 + zt**2)
        ireg = multiply.outer(ones(kmax, float32), ones(lmax, float32))
        ireg[0:1, 0:lmax] = 0
        ireg[0:kmax, 0:1] = 0
        ireg[1:15, 7:12] = 2
        ireg[1:15, 12:lmax] = 3
        ireg[3:7, 3:7] = 0
        freg = ireg + .2 * (1. - random_sample(kmax, lmax))  # ranf (ireg))
        freg = array(freg, float32)
        #rt [4:6, 4:6] = -1.e8
        z[3:10, 3:12] = z[3:10, 3:12] * .9
        z[5, 5] = z[5, 5] * .9
        z[17:22, 15:18] = z[17:22, 15:18] * 1.2
        z[16, 16] = z[16, 16] * 1.1
        orient3()
        print "plwf (freg, shade = 1, edges = 0)"
        plwf(freg, shade=1, edges=0)
        [xmin, xmax, ymin, ymax] = draw3(1)
        limits(xmin, xmax, ymin, ymax)
        paws()

        print "two slice3mesh and two pl3tree"
        nxny = array([kmax - 1, lmax - 1])
        x0y0 = array([0., 0.])
        dxdy = array([1., 1.])
        [nv, xyzv, col] = slice3mesh(nxny, dxdy, x0y0, freg)
        [nw, xyzw, col] = slice3mesh(nxny, dxdy, x0y0, freg + ut)
        pl3tree(nv, xyzv)
        pl3tree(nw, xyzw)
        draw3(1)
        limits()
        paws()

        print "light3 (ambient = 0, diffuse = .5, specular = 1, sdir = [0, 0, -1])"
        light3(ambient=0, diffuse=.5, specular=1, sdir=[0, 0, -1])
        demo5_light(1)
        paws()

        print "[nv, xyzv, col] = slice3mesh (nxny, dxdy, x0y0, freg, color = freg)"
        [nv, xyzv, col] = slice3mesh(nxny, dxdy, x0y0, freg, color=freg)
        pl3surf(nv, xyzv, values=col)
        draw3(1)
        palette("rainbow.gp")
        paws()

        print 'palette ("rainbow.gp")'
        [nv, xyzv, col] = slice3mesh(nxny, dxdy, x0y0, freg, color=z)
        pl3surf(nv, xyzv, values=col)
        draw3(1)
        paws()

        print 'palette ("stern.gp")'
        palette("stern.gp")
        paws()

        print "[nv, xyzv, col] = slice3mesh (nxny, dxdy, x0y0, z, color = z)"
        [nv, xyzv, col] = slice3mesh(nxny, dxdy, x0y0, z, color=z)
        pl3surf(nv, xyzv, values=col)
        orient3(phi=0, theta=0)
        draw3(1)
        paws()

        print 'palette ("gray.gp")'
        print "light3 ( diffuse=.1, specular=1., sdir=array([0,0,-1]))"
        set_draw3_(0)
        palette("gray.gp")
        light3(diffuse=.1, specular=1., sdir=array([0, 0, -1]))
        pl3surf(nv, xyzv)
        draw3(1)
        paws()


#     spin3 ()
#     paws ()

    hcp_finish()
Exemple #6
0
def color_bar(minz,
              maxz,
              split=0,
              ncol=None,
              ymax=0.85,
              ymin=0.44,
              xmin0=0.62,
              xmax0=0.64,
              zlabel=None,
              fontsize=16,
              font='helvetica',
              color='black'):
    """
    color_bar (minz, maxz) plots a color bar to the right of the plot square
    labelled by the z values from minz to maxz.

    plf (z, y, x)
    color_bar (z (min, min), z (max, max))

    or
    plf (z, y, x, cmin = MINZ, cmax = MAXZ)
    color_bar (MINZ, MAXZ)

    are typical usage
    """
    if ncol is None:
        ncol = 100 + (1 - split) * 100
    plsys(0)
    if type(minz) == type(maxz) == type(1):  # Do not change!!!
        plotval = reshape(arange(minz, maxz + 1, dtype='B'),
                          (maxz + 1 - minz, 1))
        pli(plotval, xmin0, ymin, xmax0, ymax)  # draw bar
    elif not split:
        pli(reshape(gistfuncs.span(0, 1, ncol), (ncol, 1)), xmin0, ymin, xmax0,
            ymax)  # draw bar
    else:
        pli(
            reshape(
                split_bytscl(gistfuncs.span(0, 1, ncol),
                             0).astype(numpy.uint8), (ncol, 1)), xmin0, ymin,
            xmax0, ymax)  # draw bar
    pldj(array([xmin0, xmin0]),
         array([ymin, ymax]),
         array([xmax0, xmax0]),
         array([ymin, ymax]),
         color=color)
    plsys(1)
    levs = nice_levels(array([minz, maxz]))
    scales = []
    for i in range(len(levs)):
        scales.append("% .5g" % levs[i])
    ys = ymin + (ymax - ymin) * (levs - minz) / (maxz - minz)
    llev = len(levs)
    rllev = range(llev)
    for i in rllev:
        plt(scales[i], xmax0 + 0.005, ys[i], color=color)  # labels
    xmin = zeros(llev, float32)
    xmax = zeros(llev, float32)
    xmin[0:llev] = xmin0
    xmax[0:llev] = xmax0 + 0.005
    plsys(0)
    pldj(xmin, ys, xmax, ys, color=color)  # ticks
    plsys(1)
    # Write the max and min on bar
    xmid = (xmin0 + xmax0) / 2.0
    if max(ys) > (ymax - 0.01):
        plt("% .5g" % maxz, xmid, ymax + 0.020, justify="CA", color=color)
    else:
        plt("% .5g" % maxz, xmid, ymax + 0.005, justify="CA", color=color)
    plt("% .5g" % minz, xmid, ymin - 0.0025, justify="CT", color=color)
    if zlabel is None:
        pass
    elif zlabel != "":
        ymidpt = (ymax + ymin) / 2.0
        x0 = xmax0 + 0.04
        plt(zlabel,
            x0,
            ymidpt,
            color=color,
            font=font,
            justify="CB",
            height=fontsize,
            orient=3)