Beispiel #1
0
def read_slices(field='uu1',datadir='data/',proc=-1,
                extension='xz',format='native',oldfile=False):
    """
    read 2D slice files and return an array of (nslices,vsize,hsize).
    """
    datadir = os.path.expanduser(datadir)
    if proc < 0:
        filename = datadir+'/slice_'+field+'.'+extension
    else:
        filename = datadir+'/proc'+str(proc)+'/slice_'+field+'.'+extension

    # global dim
    param = read_param(datadir, quiet=True)

    dim = read_dim(datadir,proc) 
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if extension.startswith('xy'):
        hsize = dim.nx
        vsize = dim.ny
    if extension.startswith('xz'):
        hsize = dim.nx
        vsize = dim.nz
    if extension.startswith('yz'):
        hsize = dim.ny
        vsize = dim.nz


    infile = npfile(filename,endian=format)

    ifirst = True
    islice = 0
    t = N.zeros(1,dtype=precision)
    slices = N.zeros(1,dtype=precision)

    while 1:
        try:
            raw_data = infile.fort_read(precision)
        except ValueError:
            break
        except TypeError:
            break
        
        if oldfile:
            t = N.concatenate((t,raw_data[-1:]))
            slices = N.concatenate((slices,raw_data[:-1]))
        else:
            t = N.concatenate((t,raw_data[-2:-1]))
            slices = N.concatenate((slices,raw_data[:-2]))
        islice += 1

    output = slices[1:].reshape(islice,vsize,hsize)

    return output,t[1:]
Beispiel #2
0
def slices(field='uu1',datadir='./data',proc=-1,extension='xz',format='native',oldfile=False):
    """
    read 2D slice files and return an array of (nslices,vsize,hsize).
    """
    datadir = os.path.expanduser(datadir)
    if proc < 0:
        filename = datadir+'/slice_'+field+'.'+extension
    else:
        filename = datadir+'/proc'+str(proc)+'/slice_'+field+'.'+extension

    # global dim
    param = read_param(datadir, quiet=True)

    dim = read_dim(datadir,proc) 
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz


    infile = npfile(filename,endian=format)

    ifirst = True
    islice = 0
    t = N.zeros(1,dtype=precision)
    slices = N.zeros(1,dtype=precision)

    while 1:
        try:
            raw_data = infile.fort_read(precision)
        except ValueError:
            break
        except TypeError:
            break
        
        if oldfile:
            t = N.concatenate((t,raw_data[-1:]))
            slices = N.concatenate((slices,raw_data[:-1]))
        else:
            t = N.concatenate((t,raw_data[-2:-1]))
            slices = N.concatenate((slices,raw_data[:-2]))
        islice += 1

    output = slices[1:].reshape(islice,vsize,hsize)

    return output,t[1:]
Beispiel #3
0
    def __init__(self,
                 datadir='data/',
                 proc=-1,
                 ivar=-1,
                 quiet=False,
                 trim=False,
                 format='native',
                 param=None):
        """
        Read grid from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load grid from specified processor.
        """
        datadir = os.path.expanduser(datadir)
        if param is None:
            param = read_param(datadir, quiet=quiet)
        dim = read_dim(datadir, proc)
        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if proc < 0:
            procdirs = filter(lambda s: s.startswith('proc'),
                              os.listdir(datadir))
        else:
            procdirs = ['proc' + str(proc)]

        #global array
        x = N.zeros(dim.mx, dtype=precision)
        y = N.zeros(dim.my, dtype=precision)
        z = N.zeros(dim.mz, dtype=precision)
        dx_1 = N.zeros(dim.mx, dtype=precision)
        dy_1 = N.zeros(dim.my, dtype=precision)
        dz_1 = N.zeros(dim.mz, dtype=precision)
        dx_tilde = N.zeros(dim.mx, dtype=precision)
        dy_tilde = N.zeros(dim.my, dtype=precision)
        dz_tilde = N.zeros(dim.mz, dtype=precision)

        for directory in procdirs:
            proc = int(directory[4:])
            procdim = read_dim(datadir, proc)
            if not quiet:
                print "reading data from processor %i of %i ..." \
                      % (proc, len(procdirs))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            filename = os.path.join(datadir, directory, 'grid.dat')
            infile = npfile(filename, endian=format)
            grid_raw = infile.fort_read(precision)
            dx, dy, dz = tuple(infile.fort_read(precision))
            Lx, Ly, Lz = tuple(infile.fort_read(precision))
            dx_1_raw = infile.fort_read(precision)
            dx_tilde_raw = infile.fort_read(precision)
            infile.close()

            #reshape
            t = grid_raw[0]
            x_loc = grid_raw[1:mxloc + 1]
            y_loc = grid_raw[mxloc + 1:mxloc + myloc + 1]
            z_loc = grid_raw[mxloc + myloc + 1:mxloc + myloc + mzloc + 1]
            dx_1_loc = dx_1_raw[0:mxloc]
            dy_1_loc = dx_1_raw[mxloc:mxloc + myloc]
            dz_1_loc = dx_1_raw[mxloc + myloc:mxloc + myloc + mzloc]
            dx_tilde_loc = dx_tilde_raw[0:mxloc]
            dy_tilde_loc = dx_tilde_raw[mxloc:mxloc + myloc]
            dz_tilde_loc = dx_tilde_raw[mxloc + myloc:mxloc + myloc + mzloc]

            if len(procdirs) > 1:
                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x + procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx * procdim.nx + procdim.nghostx
                    i1x = i0x + procdim.mx - procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y + procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy * procdim.ny + procdim.nghosty
                    i1y = i0y + procdim.my - procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z + procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz * procdim.nz + procdim.nghostz
                    i1z = i0z + procdim.mz - procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]
                dx_1[i0x:i1x] = dx_1_loc[i0xloc:i1xloc]
                dy_1[i0y:i1y] = dy_1_loc[i0yloc:i1yloc]
                dz_1[i0z:i1z] = dz_1_loc[i0zloc:i1zloc]
                dx_tilde[i0x:i1x] = dx_tilde_loc[i0xloc:i1xloc]
                dy_tilde[i0y:i1y] = dy_tilde_loc[i0yloc:i1yloc]
                dz_tilde[i0z:i1z] = dz_tilde_loc[i0zloc:i1zloc]

            else:
                x = x_loc
                y = y_loc
                z = z_loc
                dx_1 = dx_1_loc
                dy_1 = dy_1_loc
                dz_1 = dz_1_loc
                dx_tilde = dx_tilde_loc
                dy_tilde = dy_tilde_loc
                dz_tilde = dz_tilde_loc
            #endif MPI run

        # end directories loop
        if trim:
            self.x = x[dim.l1:dim.l2 + 1]
            self.y = y[dim.m1:dim.m2 + 1]
            self.z = z[dim.n1:dim.n2 + 1]
            self.dx_1 = dx_1[dim.l1:dim.l2 + 1]
            self.dy_1 = dy_1[dim.m1:dim.m2 + 1]
            self.dx_1 = dz_1[dim.n1:dim.n2 + 1]
            self.dx_tilde = dx_tilde[dim.l1:dim.l2 + 1]
            self.dy_tilde = dy_tilde[dim.m1:dim.m2 + 1]
            self.dx_tilde = dz_tilde[dim.n1:dim.n2 + 1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.dx_1 = dx_1
            self.dy_1 = dy_1
            self.dx_1 = dz_1
            self.dx_tilde = dx_tilde
            self.dy_tilde = dy_tilde
            self.dx_tilde = dz_tilde

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        self.Lx = Lx
        self.Ly = Ly
        self.Lz = Lz
Beispiel #4
0
    def __init__(self,
                 varfile='',
                 datadir='data/',
                 proc=-1,
                 ivar=-1,
                 quiet=False,
                 trimall=False,
                 format='native',
                 param=None,
                 dim=None,
                 index=None,
                 run2D=False,
                 magic=None,
                 setup=None):
        """
        Description:
        -----------
        Read VAR files from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load VAR file from specified processor.

        format -- one of (['native', 'n'], ['ieee-le', 'l'],
        ['ieee-be', 'B']) for byte-ordering

        Params:
        ------
            varfile=''
            datadir='data/'
            proc=-1
            ivar=-1
            quiet=False
            trimall=False
            format='native'
            param=None
            dim=None
            index=None
            run2D=False
        """
        if (setup is not None):
            datadir = os.path.expanduser(setup.datadir)
            dim = setup.dim
            param = setup.param
            index = setup.index
            run2D = setup.run2D
        else:
            datadir = os.path.expanduser(datadir)
            if dim is None:
                dim = read_dim(datadir, proc)
            if param is None:
                param = read_param(datadir=datadir, quiet=quiet)
            if index is None:
                index = read_index(datadir=datadir)

        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if param.lwrite_aux:
            totalvars = dim.mvar + dim.maux
        else:
            totalvars = dim.mvar

        # read index.pro to get positions and "names"
        # of variables in f(mx,my,mz,nvar)
        # Thomas: seems useless now ?
        #exec(index) # this loads the indicies.

        if (not varfile):
            if ivar < 0:
                varfile = 'var.dat'
            else:
                varfile = 'VAR' + str(ivar)

        if proc < 0:
            procdirs = natural_sort(
                filter(lambda s: s.startswith('proc'), os.listdir(datadir)))
        else:
            procdirs = ['proc' + str(proc)]

        #global array
        if (not run2D):
            f = N.zeros((totalvars, dim.mz, dim.my, dim.mx), dtype=precision)
        else:
            if dim.ny == 1:
                f = N.zeros((totalvars, dim.mz, dim.mx), dtype=precision)
            else:
                f = N.zeros((totalvars, dim.my, dim.mx), dtype=precision)
        x = N.zeros(dim.mx, dtype=precision)
        y = N.zeros(dim.my, dtype=precision)
        z = N.zeros(dim.mz, dtype=precision)
        for directory in procdirs:

            proc = int(directory[4:])
            procdim = read_dim(datadir, proc)
            if (not quiet):
                print "reading data from processor %i of %i ..." \
                      % (proc, len(procdirs))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            filename = os.path.join(datadir, directory, varfile)
            infile = npfile(filename, endian=format)
            if (not run2D):
                f_loc = infile.fort_read(precision,
                                         shape=(-1, mzloc, myloc, mxloc))
            else:
                if dim.ny == 1:
                    f_loc = infile.fort_read(precision,
                                             shape=(-1, mzloc, mxloc))
                else:
                    f_loc = infile.fort_read(precision,
                                             shape=(-1, myloc, mxloc))
            raw_etc = infile.fort_read(precision)
            infile.close()

            t = raw_etc[0]
            x_loc = raw_etc[1:mxloc + 1]
            y_loc = raw_etc[mxloc + 1:mxloc + myloc + 1]
            z_loc = raw_etc[mxloc + myloc + 1:mxloc + myloc + mzloc + 1]
            if (param.lshear):
                shear_offset = 1
                deltay = raw_etc[-1]
            else:
                shear_offset = 0

            dx = raw_etc[-3 - shear_offset]
            dy = raw_etc[-2 - shear_offset]
            dz = raw_etc[-1 - shear_offset]

            if len(procdirs) > 1:
                # calculate where the local processor will go in
                # the global array

                #
                #  Don't overwrite ghost zones of processor to the left (and
                #  accordingly in y and z direction--makes a difference on the
                #  diagonals)
                #
                # recall that in NumPy, slicing is NON-INCLUSIVE on the right end
                # ie, x[0:4] will slice all of a 4-digit array, not produce
                # an error like in idl.

                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x + procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx * procdim.nx + procdim.nghostx
                    i1x = i0x + procdim.mx - procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y + procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy * procdim.ny + procdim.nghosty
                    i1y = i0y + procdim.my - procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z + procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz * procdim.nz + procdim.nghostz
                    i1z = i0z + procdim.mz - procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]

                if (not run2D):
                    f[:, i0z:i1z, i0y:i1y, i0x:i1x] = \
                        f_loc[:, i0zloc:i1zloc, i0yloc:i1yloc, i0xloc:i1xloc]
                else:
                    if dim.ny == 1:
                        f[:, i0z:i1z, i0x:i1x] = \
                              f_loc[:, i0zloc:i1zloc, i0xloc:i1xloc]
                    else:
                        f[:, i0y:i1y, i0x:i1x] = \
                              f_loc[:, i0yloc:i1yloc, i0xloc:i1xloc]
            else:
                f = f_loc
                x = x_loc
                y = y_loc
                z = z_loc
            #endif MPI run
        #endfor directories loop

        if (magic is not None):
            if ('bb' in magic):
                # compute the magnetic field before doing trimall
                aa = f[index['ax'] - 1:index['az'], ...]
                self.bb = curl(aa, dx, dy, dz, run2D=param.lwrite_2d)
                if (trimall):
                    self.bb = self.bb[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1,
                                      dim.l1:dim.l2 + 1]
            if ('jj' in magic):
                # compute the electric current field before doing trimall
                aa = f[index['ax'] - 1:index['az'], ...]
                self.jj = curl2(aa, dx, dy, dz)
                if (trimall):
                    self.jj = self.jj[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1,
                                      dim.l1:dim.l2 + 1]
            if ('vort' in magic):
                # compute the vorticity field before doing trimall
                uu = f[index['ux'] - 1:index['uz'], ...]
                self.vort = curl(uu, dx, dy, dz, run2D=param.lwrite_2d)
                if (trimall):
                    if (param.lwrite_2d):
                        if (dim.nz == 1):
                            self.vort = self.vort[:, dim.m1:dim.m2 + 1,
                                                  dim.l1:dim.l2 + 1]
                        else:
                            self.vort = self.vort[:, dim.n1:dim.n2 + 1,
                                                  dim.l1:dim.l2 + 1]
                    else:
                        self.vort = self.vort[:, dim.n1:dim.n2 + 1,
                                              dim.m1:dim.m2 + 1,
                                              dim.l1:dim.l2 + 1]

        # trim the ghost zones of the global f-array if asked
        if trimall:
            self.x = x[dim.l1:dim.l2 + 1]
            self.y = y[dim.m1:dim.m2 + 1]
            self.z = z[dim.n1:dim.n2 + 1]
            if (not run2D):
                self.f = f[:, dim.n1:dim.n2 + 1, dim.m1:dim.m2 + 1,
                           dim.l1:dim.l2 + 1]
            else:
                if dim.ny == 1:
                    self.f = f[:, dim.n1:dim.n2 + 1, dim.l1:dim.l2 + 1]
                else:
                    self.f = f[:, dim.m1:dim.m2 + 1, dim.l1:dim.l2 + 1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.f = f
            self.l1 = dim.l1
            self.l2 = dim.l2 + 1
            self.m1 = dim.m1
            self.m2 = dim.m2 + 1
            self.n1 = dim.n1
            self.n2 = dim.n2 + 1

        # Assign an attribute to self for each variable defined in
        # 'data/index.pro' so that e.g. self.ux is the x-velocity
        for key, value in index.items():
            #          print key,value
            if key != 'global_gg':
                setattr(self, key, self.f[value - 1, ...])
        # Special treatment for vector quantities
        if index.has_key('uu'):
            self.uu = self.f[index['ux'] - 1:index['uz'], ...]
        if index.has_key('aa'):
            self.aa = self.f[index['ax'] - 1:index['az'], ...]

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        if param.lshear:
            self.deltay = deltay

        # do the rest of magic after the trimall (i.e. no additional curl...)
        self.magic = magic
        if self.magic is not None:
            self.magicAttributes(param)
Beispiel #5
0
def animate_slices(field='uu1',datadir='data/',proc=-1,extension='xz',format='native',
                tmin=0.,tmax=1.e38,wait=0.,amin=0.,amax=1.,transform='',oldfile=False):
    """
    read 2D slice files and assemble an animation.

    Options:

     field --- which variable to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to modify the slice
     wait --- pause in seconds between animation slices
    """
    
    datadir = os.path.expanduser(datadir)
    if proc < 0:
        filename = datadir+'/slice_'+field+'.'+extension
    else:
        filename = datadir+'/proc'+str(proc)+'/slice_'+field+'.'+extension

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir,proc) 
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane = N.zeros((vsize,hsize),dtype=precision)

    infile = npfile(filename,endian=format)

    ax = P.axes()
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_ylim

    image = P.imshow(plane,vmin=amin,vmax=amax)

    # for real-time image display
    manager = P.get_current_fig_manager()
    manager.show()

    ifirst = True
    islice = 0
    while 1:
        try:
            raw_data = infile.fort_read(precision)
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[-1]
            plane = raw_data[:-1].reshape(vsize,hsize)
        else:
            slice_z2pos = raw_data[-1]
            t = raw_data[-2]
            plane = raw_data[:-2].reshape(vsize,hsize)
        
        if transform:
            exec('plane = plane'+transform)

        if (t > tmin and t < tmax):
            title = 't = %11.3e' % t
            ax.set_title(title)
            image.set_data(plane)
            manager.canvas.draw()
            
            if ifirst:
                print "----islice----------t---------min-------max-------delta"
            print "%10i %10.3e %10.3e %10.3e %10.3e" % (islice,t,plane.min(),plane.max(),plane.max()-plane.min())
                
            ifirst = False
            islice += 1

            sleep(wait)

    infile.close()
Beispiel #6
0
def make_movie(field='uu1',datadir='data/',proc=-1,extension='xz',format='native',
                tmin=0.,tmax=1.e38,amin=0.,amax=1.,transform='',oldfile=False):
    """
    read 2D slice files and assemble an animation in a mpg movie.

    Quickly written from the example at http://matplotlib.sourceforge.net/faq/howto_faq.html

    Options:

     field --- which variable to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to modify the slice
    """
    
    datadir = os.path.expanduser(datadir)
    if proc < 0:
        filename = datadir+'/slice_'+field+'.'+extension
    else:
        filename = datadir+'/proc'+str(proc)+'/slice_'+field+'.'+extension

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir,proc) 
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane = N.zeros((vsize,hsize),dtype=precision)

    infile = npfile(filename,endian=format)

    files = []
    fig = P.figure(figsize=(5,10))
    ax = fig.add_subplot(111)

    ifirst = True
    islice = 0
    while 1:
        try:
            raw_data = infile.fort_read(precision)
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[-1]
            plane = raw_data[:-1].reshape(vsize,hsize)
        else:
            slice_z2pos = raw_data[-1]
            t = raw_data[-2]
            plane = raw_data[:-2].reshape(vsize,hsize)
        
        if transform:
            exec('plane = plane'+transform)

        if (t > tmin and t < tmax):
            ax.cla()
            ax.imshow(plane,vmin=amin,vmax=amax)
            fname = '_tmp%03d.png'%islice
            print 'Saving frame', fname
            fig.savefig(fname)
            files.append(fname)
            
            if ifirst:
                print "----islice----------t---------min-------max-------delta"
            print "%10i %10.3e %10.3e %10.3e %10.3e" % (islice,t,plane.min(),plane.max(),plane.max()-plane.min())
                
            ifirst = False
            islice += 1

    print 'Making movie animation.mpg - this make take a while'
    os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=24 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o animation.mpg")
    os.system("rm _tmp*.png")
    infile.close()
Beispiel #7
0
def time_slices(field=['uu1'],datadir='data/',proc=-1,extension='xz',format='native',
                tmin=0.,tmax=1.e38,amin=0.,amax=1.,transform='plane[0]',dtstep=1,
                deltat=0,oldfile=False,outfile=""):
    """
    read a list of 1D slice files, combine them, and plot the slice in one dimension, and time in the other one.

    Options:

     field --- list of variables to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to combine the slices
     dtstep --- only plot every dt step
     deltat --- if set to nonzero, plot at fixed time interval rather than step
     outfile --- if set, write the slice values in the text file
    """
    
    datadir = os.path.expanduser(datadir)
    if outfile != "":
        outslice=file(outfile,"w")
    filename=[]
    if proc < 0:
        for i in field:
            filename += [datadir+'/slice_'+i+'.'+extension]
    else:
        for i in field:
            filename += [datadir+'/proc'+str(proc)+'/slice_'+i+'.'+extension]

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir,proc) 
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane=[]
    infile=[]
    for i in filename:
        plane += [N.zeros((vsize,hsize),dtype=precision)]
        
        infile += [npfile(i,endian=format)]

    ifirst = True
    islice = 0
    plotplane=[]
    dt=0
    nextt=tmin
    while 1:
        try:
            raw_data=[]
            for i in infile:
                raw_data += [i.fort_read(precision)]
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[0][-1]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-1].reshape(vsize,hsize)
        else:
            slice_z2pos = raw_data[0][-1]
            t = raw_data[0][-2]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-2].reshape(vsize,hsize)
        
        exec('tempplane ='+transform)

        if (t > tmin and t < tmax):
            if dt==0:
                plotplane += tempplane.tolist()
            
                if ifirst:
                    print "----islice----------t---------min-------max-------delta"
                print "%10i %10.3e %10.3e %10.3e %10.3e" % (islice,t,tempplane.min(),tempplane.max(),tempplane.max()-tempplane.min())
                if outfile != "":
                    outslice.write("%10i %10.3e %10.3e %10.3e %10.3e" % (islice,t,tempplane.min(),tempplane.max(),tempplane.max()-tempplane.min()))
                    outslice.write("\n")

                ifirst = False
                islice += 1
                nextt=t+deltat
            if deltat==0:
                dt=(dt+1)%dtstep
            elif t >= nextt:
                dt=0
                nextt=t+deltat
            else:
                dt=1

    ax = P.axes()
    ax.set_xlabel('t')
    ax.set_ylabel('y')
    ax.set_ylim
    image = P.imshow(N.array(plotplane).reshape(islice,vsize).transpose(),vmin=amin,vmax=amax)
    manager = P.get_current_fig_manager()
    manager.show()

    for i in infile:
        i.close()
    if outfile != "":
        outslice.close()
Beispiel #8
0
def animate_multislices(field=['uu1'],datadir='data/',proc=-1,extension='xz',format='native',
                tmin=0.,tmax=1.e38,amin=0.,amax=1.,transform='plane[0]',
                oldfile=False,outfile=""):
    """
    read a list of 2D slice files, combine them, and assemble an animation.

    Options:

     field --- list of variables to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to combine the slices
     outfile --- if set, write the slice values in the text file
    """
    
    datadir = os.path.expanduser(datadir)
    if outfile != "":
        outslice=file(outfile,"w")
    filename=[]
    if proc < 0:
        for i in field:
            filename += [datadir+'/slice_'+i+'.'+extension]
    else:
        for i in field:
            filename += [datadir+'/proc'+str(proc)+'/slice_'+i+'.'+extension]

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir,proc) 
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane=[]
    infile=[]
    for i in filename:
        plane += [N.zeros((vsize,hsize),dtype=precision)]
        
        infile += [npfile(i,endian=format)]

    ax = P.axes()
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_ylim

    exec('plotplane ='+transform)
    image = P.imshow(plotplane,vmin=amin,vmax=amax)

    # for real-time image display
    manager = P.get_current_fig_manager()
    manager.show()

    ifirst = True
    islice = 0
    while 1:
        try:
            raw_data=[]
            for i in infile:
                raw_data += [i.fort_read(precision)]
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[0][-1]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-1].reshape(vsize,hsize)
        else:
            slice_z2pos = raw_data[0][-1]
            t = raw_data[0][-2]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-2].reshape(vsize,hsize)
        
        exec('plotplane ='+transform)

        if (t > tmin and t < tmax):
            title = 't = %11.3e' % t
            ax.set_title(title)
            image.set_data(plotplane)
            manager.canvas.draw()
            
            if ifirst:
                print "----islice----------t---------min-------max-------delta"
            print "%10i %10.3e %10.3e %10.3e %10.3e" % (islice,t,plotplane.min(),plotplane.max(),plotplane.max()-plotplane.min())
            if outfile != "":
                outslice.write("%10i %10.3e %10.3e %10.3e %10.3e" % (islice,t,plotplane.min(),plotplane.max(),plotplane.max()-plotplane.min()))
                outslice.write("\n")
               
            ifirst = False
            islice += 1

    for i in infile:
        i.close()
    if outfile != "":
        outslice.close()
Beispiel #9
0
    def __init__(self, varfile='', datadir='data/', proc=-1, ivar=-1,
                 quiet=False, trimall=False, format='native',
                 param=None, dim=None, index=None, run2D=False,
                 magic=None, setup=None):
        """
        Description:
        -----------
        Read VAR files from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load VAR file from specified processor.

        format -- one of (['native', 'n'], ['ieee-le', 'l'],
        ['ieee-be', 'B']) for byte-ordering

        Params:
        ------
            varfile=''
            datadir='data/'
            proc=-1
            ivar=-1
            quiet=False
            trimall=False
            format='native'
            param=None
            dim=None
            index=None
            run2D=False
        """
        if (setup is not None):
            datadir = os.path.expanduser(setup.datadir)
            dim = setup.dim
            param = setup.param
            index = setup.index
            run2D = setup.run2D
        else:
            datadir = os.path.expanduser(datadir)
            if dim is None:
                dim = read_dim(datadir,proc)
            if param is None:
                param = read_param(datadir=datadir, quiet=quiet)
            if index is None:
                index = read_index(datadir=datadir)

        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if param.lwrite_aux:
            totalvars = dim.mvar+dim.maux
        else:
            totalvars = dim.mvar

        # Read index.pro to get positions and "names"
        # of variables in f(mx,my,mz,nvar).
        # Thomas: seems useless now ?
        #exec(index) # this loads the indicies.

        if (not varfile):
            if ivar < 0:
                varfile = 'var.dat'
            else:
                varfile = 'VAR'+str(ivar)

        if proc < 0:
            procdirs = natural_sort(filter(lambda s:s.startswith('proc'),
                                    os.listdir(datadir)))
        else:
            procdirs = ['proc'+str(proc)]

        #global array
        if (not run2D):
            f = np.zeros((totalvars, dim.mz, dim.my, dim.mx),
                         dtype=precision)
        else:
            if dim.ny == 1:
                f = np.zeros((totalvars, dim.mz, dim.mx), dtype=precision)
            else:
                f = np.zeros((totalvars, dim.my, dim.mx), dtype=precision)
        x = np.zeros(dim.mx, dtype=precision)
        y = np.zeros(dim.my, dtype=precision)
        z = np.zeros(dim.mz, dtype=precision)
        for directory in procdirs:

            proc = int(directory[4:])
            procdim = read_dim(datadir, proc)
            if (not quiet):
                print "reading data from processor %i of %i ..." \
                      % (proc, len(procdirs))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            filename = os.path.join(datadir,directory,varfile)
            infile = npfile(filename, endian=format)
            if (not run2D):
                f_loc = infile.fort_read(precision,
                                         shape=(-1, mzloc, myloc, mxloc))
            else:
                if dim.ny == 1:
                    f_loc = infile.fort_read(precision, shape=(-1, mzloc, mxloc))
                else:
                    f_loc = infile.fort_read(precision, shape=(-1, myloc, mxloc))
            raw_etc = infile.fort_read(precision)
            infile.close()

            t = raw_etc[0]
            x_loc = raw_etc[1:mxloc+1]
            y_loc = raw_etc[mxloc+1:mxloc+myloc+1]
            z_loc = raw_etc[mxloc+myloc+1:mxloc+myloc+mzloc+1]
            if (param.lshear):
                shear_offset = 1
                deltay = raw_etc[-1]
            else:
                shear_offset = 0

            dx = raw_etc[-3-shear_offset]
            dy = raw_etc[-2-shear_offset]
            dz = raw_etc[-1-shear_offset]

            if len(procdirs) > 1:
                # Calculate where the local processor will go in
                # the global array.

                # Don't overwrite ghost zones of processor to the left (and
                # accordingly in y and z direction--makes a difference on the
                # diagonals).
                #
                # Recall that in NumPy, slicing is NON-INCLUSIVE on the right end
                # ie, x[0:4] will slice all of a 4-digit array, not produce
                # an error like in idl.

                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x+procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx*procdim.nx+procdim.nghostx
                    i1x = i0x+procdim.mx-procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y+procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy*procdim.ny+procdim.nghosty
                    i1y = i0y+procdim.my-procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z+procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz*procdim.nz+procdim.nghostz
                    i1z = i0z+procdim.mz-procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]

                if (not run2D):
                    f[:, i0z:i1z, i0y:i1y, i0x:i1x] = \
                        f_loc[:, i0zloc:i1zloc, i0yloc:i1yloc, i0xloc:i1xloc]
                else:
                    if dim.ny == 1:
                        f[:, i0z:i1z, i0x:i1x] = \
                              f_loc[:, i0zloc:i1zloc, i0xloc:i1xloc]
                    else:
                        f[:, i0y:i1y, i0x:i1x] = \
                              f_loc[:, i0yloc:i1yloc, i0xloc:i1xloc]
            else:
                f = f_loc
                x = x_loc
                y = y_loc
                z = z_loc
            #endif MPI run
        #endfor directories loop

        if (magic is not None):
            if ('bb' in magic):
                # Compute the magnetic field before doing trimall.
                aa = f[index['ax']-1:index['az'],...]
                self.bb = curl(aa,dx,dy,dz,run2D=param.lwrite_2d)
                if (trimall): self.bb=self.bb[:, dim.n1:dim.n2+1,
                dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if ('jj' in magic):
                # Compute the electric current field before doing trimall.
                aa = f[index['ax']-1:index['az'],...]
                self.jj = curl2(aa,dx,dy,dz)
                if (trimall): self.jj=self.jj[:, dim.n1:dim.n2+1,
                dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if ('vort' in magic):
                # Compute the vorticity field before doing trimall.
                uu = f[index['ux']-1:index['uz'],...]
                self.vort = curl(uu,dx,dy,dz,run2D=param.lwrite_2d)
                if (trimall):
                    if (param.lwrite_2d):
                        if (dim.nz == 1):
                            self.vort=self.vort[:, dim.m1:dim.m2+1,
                            dim.l1:dim.l2+1]
                        else:
                            self.vort=self.vort[:, dim.n1:dim.n2+1,
                            dim.l1:dim.l2+1]
                    else:
                        self.vort=self.vort[:, dim.n1:dim.n2+1,
                        dim.m1:dim.m2+1, dim.l1:dim.l2+1]

        # Trim the ghost zones of the global f-array if asked.
        if trimall:
            self.x = x[dim.l1:dim.l2+1]
            self.y = y[dim.m1:dim.m2+1]
            self.z = z[dim.n1:dim.n2+1]
            if (not run2D):
                self.f = f[:, dim.n1:dim.n2+1, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            else:
               if dim.ny == 1:
                   self.f = f[:, dim.n1:dim.n2+1, dim.l1:dim.l2+1]
               else:
                   self.f = f[:, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.f = f
            self.l1 = dim.l1
            self.l2 = dim.l2+1
            self.m1 = dim.m1
            self.m2 = dim.m2+1
            self.n1 = dim.n1
            self.n2 = dim.n2+1

        # Assign an attribute to self for each variable defined in
        # 'data/index.pro' so that e.g. self.ux is the x-velocity.
        for key,value in index.items():
            # print key,value.
            if key != 'global_gg':
                setattr(self,key,self.f[value-1,...])
        # special treatment for vector quantities
        if index.has_key('uu'):
            self.uu = self.f[index['ux']-1:index['uz'],...]
        if index.has_key('aa'):
            self.aa = self.f[index['ax']-1:index['az'],...]
        # Also treat Fcr (from cosmicrayflux) as a vector.
        if index.has_key('fcr'):  
            self.fcr = self.f[index['fcr']-1:index['fcr']+2,...]
            self.fcrx = self.fcr[0]
            self.fcry = self.fcr[1]
            self.fcrz = self.fcr[2]

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        if param.lshear:
            self.deltay = deltay

        # Do the rest of magic after the trimall (i.e. no additional curl...).
        self.magic = magic
        if self.magic is not None:
            self.__magicAttributes(param)
Beispiel #10
0
    def __init__(self, datadir='data/', proc=-1, ivar=-1, quiet=False,
                 trim=False, format='native', param=None):
        """
        Read grid from pencil code. if proc < 0, then load all data
        and assemble. otherwise, load grid from specified processor.
        """
        datadir = os.path.expanduser(datadir)
        if param is None:
            param = read_param(datadir, quiet=quiet)
        dim = read_dim(datadir, proc)
        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if proc < 0:
            procdirs = filter(lambda s:s.startswith('proc'),
                              os.listdir(datadir))
        else:
            procdirs = ['proc'+str(proc)]

        #global array
        x = N.zeros(dim.mx, dtype=precision)
        y = N.zeros(dim.my, dtype=precision)
        z = N.zeros(dim.mz, dtype=precision)
        dx_1 = N.zeros(dim.mx, dtype=precision)
        dy_1 = N.zeros(dim.my, dtype=precision)
        dz_1 = N.zeros(dim.mz, dtype=precision)
        dx_tilde = N.zeros(dim.mx, dtype=precision)
        dy_tilde = N.zeros(dim.my, dtype=precision)
        dz_tilde = N.zeros(dim.mz, dtype=precision)

        for directory in procdirs:
            proc = int(directory[4:])
            procdim = read_dim(datadir, proc)
            if not quiet:
                print "reading data from processor %i of %i ..." \
                      % (proc, len(procdirs))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            #read data
            filename = os.path.join(datadir, directory, 'grid.dat')
            infile = npfile(filename, endian=format)
            grid_raw = infile.fort_read(precision)
            dx, dy, dz = tuple(infile.fort_read(precision))
            Lx, Ly, Lz = tuple(infile.fort_read(precision))
            dx_1_raw = infile.fort_read(precision)
            dx_tilde_raw = infile.fort_read(precision)
            infile.close()

            #reshape
            t = grid_raw[0]
            x_loc = grid_raw[1:mxloc+1]
            y_loc = grid_raw[mxloc+1:mxloc+myloc+1]
            z_loc = grid_raw[mxloc+myloc+1:mxloc+myloc+mzloc+1]
            dx_1_loc = dx_1_raw[0:mxloc]
            dy_1_loc = dx_1_raw[mxloc:mxloc+myloc]
            dz_1_loc = dx_1_raw[mxloc+myloc:mxloc+myloc+mzloc]
            dx_tilde_loc = dx_tilde_raw[0:mxloc]
            dy_tilde_loc = dx_tilde_raw[mxloc:mxloc+myloc]
            dz_tilde_loc = dx_tilde_raw[mxloc+myloc:mxloc+myloc+mzloc]

            if len(procdirs) >1:
                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x+procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx*procdim.nx+procdim.nghostx
                    i1x = i0x+procdim.mx-procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y+procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy*procdim.ny+procdim.nghosty
                    i1y = i0y+procdim.my-procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z+procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz*procdim.nz+procdim.nghostz
                    i1z = i0z+procdim.mz-procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]
                dx_1[i0x:i1x] = dx_1_loc[i0xloc:i1xloc]
                dy_1[i0y:i1y] = dy_1_loc[i0yloc:i1yloc]
                dz_1[i0z:i1z] = dz_1_loc[i0zloc:i1zloc]
                dx_tilde[i0x:i1x] = dx_tilde_loc[i0xloc:i1xloc]
                dy_tilde[i0y:i1y] = dy_tilde_loc[i0yloc:i1yloc]
                dz_tilde[i0z:i1z] = dz_tilde_loc[i0zloc:i1zloc]

            else:
                x = x_loc
                y = y_loc
                z = z_loc
                dx_1 = dx_1_loc
                dy_1 = dy_1_loc
                dz_1 = dz_1_loc
                dx_tilde = dx_tilde_loc
                dy_tilde = dy_tilde_loc
                dz_tilde = dz_tilde_loc
            #endif MPI run

        # end directories loop
        if trim:
            self.x = x[dim.l1:dim.l2+1]
            self.y = y[dim.m1:dim.m2+1]
            self.z = z[dim.n1:dim.n2+1]
            self.dx_1 = dx_1[dim.l1:dim.l2+1]
            self.dy_1 = dy_1[dim.m1:dim.m2+1]
            self.dx_1 = dz_1[dim.n1:dim.n2+1]
            self.dx_tilde = dx_tilde[dim.l1:dim.l2+1]
            self.dy_tilde = dy_tilde[dim.m1:dim.m2+1]
            self.dx_tilde = dz_tilde[dim.n1:dim.n2+1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.dx_1 = dx_1
            self.dy_1 = dy_1
            self.dx_1 = dz_1
            self.dx_tilde = dx_tilde
            self.dy_tilde = dy_tilde
            self.dx_tilde = dz_tilde

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        self.Lx = Lx
        self.Ly = Ly
        self.Lz = Lz
Beispiel #11
0
def animate_slices(field='uu1',
                   datadir='data/',
                   proc=-1,
                   extension='xz',
                   format='native',
                   tmin=0.,
                   tmax=1.e38,
                   wait=0.,
                   amin=0.,
                   amax=1.,
                   transform='',
                   oldfile=False):
    """
    read 2D slice files and assemble an animation.

    Options:

     field --- which variable to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to modify the slice
     wait --- pause in seconds between animation slices
    """

    datadir = os.path.expanduser(datadir)
    if proc < 0:
        filename = datadir + '/slice_' + field + '.' + extension
    else:
        filename = datadir + '/proc' + str(
            proc) + '/slice_' + field + '.' + extension

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir, proc)
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane = N.zeros((vsize, hsize), dtype=precision)

    infile = npfile(filename, endian=format)

    ax = P.axes()
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_ylim

    image = P.imshow(plane, vmin=amin, vmax=amax)

    # for real-time image display
    manager = P.get_current_fig_manager()
    manager.show()

    ifirst = True
    islice = 0
    while 1:
        try:
            raw_data = infile.fort_read(precision)
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[-1]
            plane = raw_data[:-1].reshape(vsize, hsize)
        else:
            slice_z2pos = raw_data[-1]
            t = raw_data[-2]
            plane = raw_data[:-2].reshape(vsize, hsize)

        if transform:
            exec('plane = plane' + transform)

        if (t > tmin and t < tmax):
            title = 't = %11.3e' % t
            ax.set_title(title)
            image.set_data(plane)
            manager.canvas.draw()

            if ifirst:
                print "----islice----------t---------min-------max-------delta"
            print "%10i %10.3e %10.3e %10.3e %10.3e" % (
                islice, t, plane.min(), plane.max(), plane.max() - plane.min())

            ifirst = False
            islice += 1

            sleep(wait)

    infile.close()
Beispiel #12
0
def make_movie(field='uu1',
               datadir='data/',
               proc=-1,
               extension='xz',
               format='native',
               tmin=0.,
               tmax=1.e38,
               amin=0.,
               amax=1.,
               transform='',
               oldfile=False):
    """
    read 2D slice files and assemble an animation in a mpg movie.

    Quickly written from the example at http://matplotlib.sourceforge.net/faq/howto_faq.html

    Options:

     field --- which variable to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to modify the slice
    """

    datadir = os.path.expanduser(datadir)
    if proc < 0:
        filename = datadir + '/slice_' + field + '.' + extension
    else:
        filename = datadir + '/proc' + str(
            proc) + '/slice_' + field + '.' + extension

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir, proc)
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane = N.zeros((vsize, hsize), dtype=precision)

    infile = npfile(filename, endian=format)

    files = []
    fig = P.figure(figsize=(5, 10))
    ax = fig.add_subplot(111)

    ifirst = True
    islice = 0
    while 1:
        try:
            raw_data = infile.fort_read(precision)
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[-1]
            plane = raw_data[:-1].reshape(vsize, hsize)
        else:
            slice_z2pos = raw_data[-1]
            t = raw_data[-2]
            plane = raw_data[:-2].reshape(vsize, hsize)

        if transform:
            exec('plane = plane' + transform)

        if (t > tmin and t < tmax):
            ax.cla()
            ax.imshow(plane, vmin=amin, vmax=amax)
            fname = '_tmp%03d.png' % islice
            print 'Saving frame', fname
            fig.savefig(fname)
            files.append(fname)

            if ifirst:
                print "----islice----------t---------min-------max-------delta"
            print "%10i %10.3e %10.3e %10.3e %10.3e" % (
                islice, t, plane.min(), plane.max(), plane.max() - plane.min())

            ifirst = False
            islice += 1

    print 'Making movie animation.mpg - this make take a while'
    os.system(
        "mencoder 'mf://_tmp*.png' -mf type=png:fps=24 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o animation.mpg"
    )
    os.system("rm _tmp*.png")
    infile.close()
Beispiel #13
0
def time_slices(field=['uu1'],
                datadir='data/',
                proc=-1,
                extension='xz',
                format='native',
                tmin=0.,
                tmax=1.e38,
                amin=0.,
                amax=1.,
                transform='plane[0]',
                dtstep=1,
                deltat=0,
                oldfile=False,
                outfile=""):
    """
    read a list of 1D slice files, combine them, and plot the slice in one dimension, and time in the other one.

    Options:

     field --- list of variables to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to combine the slices
     dtstep --- only plot every dt step
     deltat --- if set to nonzero, plot at fixed time interval rather than step
     outfile --- if set, write the slice values in the text file
    """

    datadir = os.path.expanduser(datadir)
    if outfile != "":
        outslice = file(outfile, "w")
    filename = []
    if proc < 0:
        for i in field:
            filename += [datadir + '/slice_' + i + '.' + extension]
    else:
        for i in field:
            filename += [
                datadir + '/proc' + str(proc) + '/slice_' + i + '.' + extension
            ]

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir, proc)
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane = []
    infile = []
    for i in filename:
        plane += [N.zeros((vsize, hsize), dtype=precision)]

        infile += [npfile(i, endian=format)]

    ifirst = True
    islice = 0
    plotplane = []
    dt = 0
    nextt = tmin
    while 1:
        try:
            raw_data = []
            for i in infile:
                raw_data += [i.fort_read(precision)]
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[0][-1]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-1].reshape(vsize, hsize)
        else:
            slice_z2pos = raw_data[0][-1]
            t = raw_data[0][-2]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-2].reshape(vsize, hsize)

        exec('tempplane =' + transform)

        if (t > tmin and t < tmax):
            if dt == 0:
                plotplane += tempplane.tolist()

                if ifirst:
                    print "----islice----------t---------min-------max-------delta"
                print "%10i %10.3e %10.3e %10.3e %10.3e" % (
                    islice, t, tempplane.min(), tempplane.max(),
                    tempplane.max() - tempplane.min())
                if outfile != "":
                    outslice.write(
                        "%10i %10.3e %10.3e %10.3e %10.3e" %
                        (islice, t, tempplane.min(), tempplane.max(),
                         tempplane.max() - tempplane.min()))
                    outslice.write("\n")

                ifirst = False
                islice += 1
                nextt = t + deltat
            if deltat == 0:
                dt = (dt + 1) % dtstep
            elif t >= nextt:
                dt = 0
                nextt = t + deltat
            else:
                dt = 1

    ax = P.axes()
    ax.set_xlabel('t')
    ax.set_ylabel('y')
    ax.set_ylim
    image = P.imshow(N.array(plotplane).reshape(islice, vsize).transpose(),
                     vmin=amin,
                     vmax=amax)
    manager = P.get_current_fig_manager()
    manager.show()

    for i in infile:
        i.close()
    if outfile != "":
        outslice.close()
Beispiel #14
0
def animate_multislices(field=['uu1'],
                        datadir='data/',
                        proc=-1,
                        extension='xz',
                        format='native',
                        tmin=0.,
                        tmax=1.e38,
                        amin=0.,
                        amax=1.,
                        transform='plane[0]',
                        oldfile=False,
                        outfile=""):
    """
    read a list of 2D slice files, combine them, and assemble an animation.

    Options:

     field --- list of variables to slice
     datadir --- path to data directory
     proc --- an integer giving the processor to read a slice from
     extension --- which plane of xy,xz,yz,Xz. for 2D this should be overwritten.
     format --- endian. one of little, big, or native (default)
     tmin --- start time
     tmax --- end time
     amin --- minimum value for image scaling
     amax --- maximum value for image scaling
     transform --- insert arbitrary numerical code to combine the slices
     outfile --- if set, write the slice values in the text file
    """

    datadir = os.path.expanduser(datadir)
    if outfile != "":
        outslice = file(outfile, "w")
    filename = []
    if proc < 0:
        for i in field:
            filename += [datadir + '/slice_' + i + '.' + extension]
    else:
        for i in field:
            filename += [
                datadir + '/proc' + str(proc) + '/slice_' + i + '.' + extension
            ]

    # global dim
    param = read_param(datadir)

    dim = read_dim(datadir, proc)
    if dim.precision == 'D':
        precision = 'd'
    else:
        precision = 'f'

    # set up slice plane
    if (extension == 'xy' or extension == 'Xy'):
        hsize = dim.nx
        vsize = dim.ny
    if (extension == 'xz'):
        hsize = dim.nx
        vsize = dim.nz
    if (extension == 'yz'):
        hsize = dim.ny
        vsize = dim.nz
    plane = []
    infile = []
    for i in filename:
        plane += [N.zeros((vsize, hsize), dtype=precision)]

        infile += [npfile(i, endian=format)]

    ax = P.axes()
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_ylim

    exec('plotplane =' + transform)
    image = P.imshow(plotplane, vmin=amin, vmax=amax)

    # for real-time image display
    manager = P.get_current_fig_manager()
    manager.show()

    ifirst = True
    islice = 0
    while 1:
        try:
            raw_data = []
            for i in infile:
                raw_data += [i.fort_read(precision)]
        except ValueError:
            break
        except TypeError:
            break

        if oldfile:
            t = raw_data[0][-1]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-1].reshape(vsize, hsize)
        else:
            slice_z2pos = raw_data[0][-1]
            t = raw_data[0][-2]
            for i in range(len(raw_data)):
                plane[i] = raw_data[i][:-2].reshape(vsize, hsize)

        exec('plotplane =' + transform)

        if (t > tmin and t < tmax):
            title = 't = %11.3e' % t
            ax.set_title(title)
            image.set_data(plotplane)
            manager.canvas.draw()

            if ifirst:
                print "----islice----------t---------min-------max-------delta"
            print "%10i %10.3e %10.3e %10.3e %10.3e" % (
                islice, t, plotplane.min(), plotplane.max(),
                plotplane.max() - plotplane.min())
            if outfile != "":
                outslice.write("%10i %10.3e %10.3e %10.3e %10.3e" %
                               (islice, t, plotplane.min(), plotplane.max(),
                                plotplane.max() - plotplane.min()))
                outslice.write("\n")

            ifirst = False
            islice += 1

    for i in infile:
        i.close()
    if outfile != "":
        outslice.close()