Hi

I have four-dimensional data stored in a netcdf file and want to create
an isosurface visualization for each time step in the file (>100 times)
and save the resulting image as a PNG-file.

The problem is that after a few iterations I run out of memory. How do I
correctly destroy the tvtk and mayavi objects which I have created
before entering into the next iteration?

I'm using scene.children.remove() to remove data sources and using del
to delete all the objects which I have created. What am I missing?

Thanks,
Juerg
#!/usr/bin/env mayavi2

"""This script demonstrates how to create a numpy array data and
visualize it as image data using a few modules.

"""
# The following *optional* two lines allow a user to call this script
# as either `python script.py` or `mayavi2 script.py`.  These two
# lines must be placed before any other mayavi imports.
from enthought.mayavi.scripts import mayavi2
mayavi2.standalone(globals())

# Standard library imports
import enthought.util.scipyx as scipy
import Nio
import numpy
from numpy import newaxis, gradient

# Enthought library imports
from enthought.mayavi.sources.array_source import ArraySource
from enthought.mayavi.sources.vtk_data_source import VTKDataSource
from enthought.mayavi.modules.outline import Outline
from enthought.mayavi.modules.axes import Axes
from enthought.mayavi.modules.grid_plane import GridPlane
from enthought.mayavi.modules.contour_grid_plane import ContourGridPlane
from enthought.mayavi.modules.scalar_cut_plane import ScalarCutPlane
from enthought.mayavi.modules.image_plane_widget import ImagePlaneWidget
from enthought.mayavi.modules.iso_surface import IsoSurface
from enthought.tvtk.api import tvtk


def get_points(file):
    
    f = Nio.open_file(file)
    ix = slice(200); iy = slice(None); iz = slice(40)
    z = f.variables['ZP'][iz,iy,ix].astype('f')/500.
    (nz,ny,nx) = z.shape
    x = numpy.zeros(z.shape)
    y = numpy.zeros(z.shape)
    xc = f.variables['xc'][ix].astype('f')/1000.
    yc = f.variables['yc'][iy].astype('f')/1000.
    x[:,:,:] = xc[newaxis,newaxis,:]
    y[:,:,:] = yc[newaxis,:,newaxis]
    f.close()
    z.shape = (-1,)
    y.shape = (-1,)
    x.shape = (-1,)
    pts = numpy.array([x,y,z]).T
    return pts    


def get_data(file, varname, itime, pts):
    f = Nio.open_file(file)
    ix = slice(200); iy = slice(None); iz = slice(40)
    if varname == 'DIV':
        u = f.variables['U'][itime,iz,iy,ix].astype('f')
        v = f.variables['V'][itime,iz,iy,ix].astype('f')
        fld = gradient(u)[2]+gradient(v)[1]
    else:
        fld = f.variables['W'][itime,iz,iy,ix].astype('f')
    f.close()
    (nz,ny,nx) = fld.shape
    fld.shape = (-1,)

    sg = tvtk.StructuredGrid(dimensions=(nx,ny,nz))
    sg.point_data.scalars = fld
    sg.point_data.scalars.name = varname
    sg.points = pts
    return sg


def make_fig(file, itime, figname, pts):
    # visualize DIV
    sg = get_data(file, 'DIV', itime, pts)
    src = VTKDataSource(data=sg)
    mayavi.add_source(src)

    c1 = ContourGridPlane()
    mayavi.add_module(c1)
    c1.grid_plane.axis='z'
    c1.contour.filled_contours = True
    c1.contour.auto_contours = True
    c1.contour.minimum_contour = -1.5
    c1.contour.maximum_contour = 1.5
    c1.contour.number_of_contours = 10
    c1.module_manager.scalar_lut_manager.lut_mode = 'hsv'

    # visualize W
    sg2 = get_data(file, 'W', itime, pts)
    src2 = VTKDataSource(data=sg2)
    mayavi.add_source(src2)

    c2 = ContourGridPlane()
    mayavi.add_module(c2)
    c2.grid_plane.axis='y'
    c2.contour.filled_contours = True
    c2.contour.auto_contours = True
    c2.contour.minimum_contour = -2.0
    c2.contour.maximum_contour = 2.0
    c2.contour.number_of_contours = 5
    c2.module_manager.scalar_lut_manager.lut_mode = 'black-white'

    c3 = ContourGridPlane()
    mayavi.add_module(c3)
    c3.grid_plane.axis='x'
    c3.contour.filled_contours = True
    c3.contour.minimum_contour = -2.0
    c3.contour.maximum_contour = 2.0
    c3.contour.number_of_contours = 5
    c3.module_manager.scalar_lut_manager.lut_mode = 'black-white'

    i1 = IsoSurface()
    mayavi.add_module(i1)
    i1.module_manager.scalar_lut_manager.lut_mode = 'black-white'
    i1.contour.contours[0] = 1.0
    i1.scene.isometric_view()

    figname = figname + str(itime).zfill(6) + '.png'
    i1.scene.save(figname, size=(900,750))

    s = mayavi.engine.current_scene
    s.children.remove(src)
    s.children.remove(src2)
    del(src)
    del(src2)
    del(c1); del(c2); del(c3); del(i1)
    del(sg); del(sg2)


if __name__ == '__main__':
    mayavi.new_scene()
    file = 'dat/arps/bv_vles_tcst02/ncdf/arps_vles_tcst02_30s.nc'
    pts = get_points(file)
    for itime in range(121):
        make_fig(file, itime, 'fig/isosrf_vles_tcst02_t', pts)
    
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
MayaVi-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mayavi-users

Reply via email to