예제 #1
0
def merge(paths, newpath):
    '''merge neutron files to one neutron file

    it is assumed that the neutrons in the input files were
    normalized correctly so that we can merge them directly.
    '''

    from mcni.neutron_storage import storage

    assert len(paths) > 0

    out = storage(newpath, 'w')

    for path in paths:

        info.log(' * Working on %r' % path)

        s = storage(path, 'r')
        neutrons = s.read()

        if neutrons:
            out.write(neutrons)
        continue

    return
예제 #2
0
def merge(paths, newpath):
    '''merge neutron files to one neutron file

    it is assumed that the neutrons in the input files were
    normalized correctly so that we can merge them directly.
    '''
    
    from mcni.neutron_storage import storage
    
    assert len(paths)>0
    
    out = storage(newpath, 'w')

    for path in paths:
        
        info.log( ' * Working on %r' % path )
        
        s = storage(path, 'r')
        neutrons = s.read()

        if neutrons:
            out.write(neutrons)
        continue
    
    return
예제 #3
0
 def test1(self):
     'neutron_storage: mix npy implementation and non-npy one'
     import mcni
     neutrons = mcni.neutron_buffer(7)
     out = 'tmp-nst-test1.ns'
     if os.path.exists(out): os.remove(out)
     storage = mns.storage(out, mode='w')
     storage.write(neutrons)
     neutrons1 = mns.load('neutrons.dat')
     return
예제 #4
0
 def test1(self):
     'neutron_storage: mix npy implementation and non-npy one'
     import mcni
     neutrons = mcni.neutron_buffer( 7 )
     out = 'tmp-nst-test1.ns'
     if os.path.exists(out): os.remove(out)
     storage = mns.storage(out, mode='w')
     storage.write(neutrons)
     neutrons1 = mns.load( 'neutrons.dat' )
     return
예제 #5
0
    def test(self):
        'neutron_storage.merge'

        oldstorage_path = 'test-merge-old'
        newstorage_path = 'test-merge-new'
        newpacketsize = 10

        for path in [ oldstorage_path, newstorage_path ]:
            if os.path.exists( path ):
                os.remove( path )
            continue
        
        from mcni.neutron_storage import storage, merge

        #open storage for writing
        s = storage( oldstorage_path, 'w' )

        #create neutrons
        import mcni
        neutrons = mcni.neutron_buffer( 7 )
        neutrons[5] = mcni.neutron( v = (8,9,10) )

        #write neutrons
        s.write( neutrons )
        s.write( neutrons )
        s.write( neutrons )
        #important!!! flush the storage
        del s

        #merge
        merge([oldstorage_path], newstorage_path)

        #open the merged storage for reading
        sr = storage( newstorage_path, 'r')
        neutrons = sr.read()
        self.assertEqual( len(neutrons), 7*3 )

        self.assertAlmostEqual( neutrons[5].state.velocity[0] , 8 )
        return
예제 #6
0
    def __init__(self, name, path, append=False):

        AbstractComponent.__init__(self, name)

        if not append and os.path.exists(path):
            raise RuntimeError, "Neutron storage %r already exists. To append neutrons to this storage, please use keyword 'append=1'" % path

        if append: mode = 'a'
        else: mode = 'w'

        from mcni.neutron_storage import storage
        self._storage = storage(path, mode=mode)
        return
예제 #7
0
    def __init__(self, name, path, append = False):
        
        AbstractComponent.__init__(self, name)

        if not append and os.path.exists( path ):
            raise RuntimeError, "Neutron storage %r already exists. To append neutrons to this storage, please use keyword 'append=1'" % path
        
        if append: mode='a'
        else: mode = 'w'
        
        from mcni.neutron_storage import storage
        self._storage = storage( path, mode = mode) 
        return
예제 #8
0
    def __init__(self, name, path):
        AbstractComponent.__init__(self, name)
        
        path = self.path = os.path.abspath( path )
        
        if not os.path.exists( path ):
            raise IOError , "path %r does not exist" % path
        
        if os.path.isdir( path ):
            raise IOError , "path %r is a directory" % path

        from mcni.neutron_storage import storage
        self._storage = storage( path, 'r' )
        if self.parallel:
            # master node keeps the cursor
            if self.mpiRank == 0:
                self._cursor = 0
        return
예제 #9
0
    def __init__(self, name, path):
        AbstractComponent.__init__(self, name)

        path = self.path = os.path.abspath(path)

        if not os.path.exists(path):
            raise IOError, "path %r does not exist" % path

        if os.path.isdir(path):
            raise IOError, "path %r is a directory" % path

        from mcni.neutron_storage import storage
        self._storage = storage(path, 'r')
        if self.parallel:
            # master node keeps the cursor
            if self.mpiRank == 0:
                self._cursor = 0
        return
#!/usr/bin/env python

import numpy

arr = numpy.arange(1000.)
arr.shape = -1, 10

from mcni.neutron_storage import neutrons_from_npyarr, storage
neutrons = neutrons_from_npyarr(arr)

s = storage('neutron-storage-for-NeutronFromStorage_TestCase', 'w')
s.write(neutrons)