Exemplo n.º 1
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument("-f", dest='format', default='genic', required=False)
  parser.add_argument("filename")
  # n is bits per chanel
  parser.add_argument("-n", dest='bitsperaxis', default=6, required=False, type=int)
  parser.add_argument("-o", dest='output', default=None, required=True)
  args = parser.parse_args()
  
  bits = args.bitsperaxis * 3

  if '%d' in args.filename:
    snap0 = Snapshot(args.filename % 0, args.format)
    Nfile = snap0.C['Nfiles']
  else:
    snap0 = Snapshot(args.filename, args.format)
    Nfile = 1

  boxsize = snap0.C['boxsize']
  C = []
  F = []
  with sharedmem.Pool(use_threads=True) as pool:
    def work(fid):
      if '%d' in args.filename:
        snap = Snapshot(args.filename % fid, args.format)
      else:
        snap = Snapshot(args.filename, args.format)
      N = snap.C['N'].sum()
      x,y,z = snap[None, 'pos'].T
      del snap
      scale = fillingcurve.scale(0, boxsize)
      zkey = fillingcurve.encode(x, y, z, scale=scale)
      del x, y, z
      dig = numpy.uint32(zkey >> (fillingcurve.bits * 3 - bits))
  
      bincount = numpy.bincount(dig)
      
      return (fid, bincount.nonzero()[0])

    def reduce(res):
      fid, active = res
      F.append(fid)
      C.append(list(active))
      print fid, len(active)
    pool.map(work, range(Nfile), callback=reduce)

  F = numpy.array(F, dtype='u4')
  l = [len(a) for a in C]
  C = numpy.concatenate(C)
  fid = numpy.repeat(F, l)
  arg = C.argsort()
  fid = fid[arg]
  count = numpy.bincount(C, minlength=1<<bits)
  
  index = packarray(fid, count)
  map = MeshIndex(0, boxsize, args.bitsperaxis, index)
  map.tofile(args.output)
Exemplo n.º 2
0
  def use(self, snapname, format, periodic=False, origin=[0,0,0.], boxsize=None, mapfile=None, **kwargs):
    """ kwargs will be passed to reader,
        only particles within origin and boxsize are loaded.
    """
    self.snapname = snapname
    if format == 'hdf5':
        self.format = 'hdf5'
    elif format == 'bigfile':
        self.format = 'bigfile'
    else:
        self.format = Reader(format, **kwargs)
    try:
      snapname = self.snapname % 0
    except TypeError:
      snapname = self.snapname

    snap = Snapshot(snapname, self.format)
   
    self.C = snap.C
    self._template = snap
    self._schema = snap.schema

    self.origin[...] = numpy.ones(3) * origin

    if boxsize is not None:
      self.need_cut = True
    else:
      self.need_cut = False

    if mapfile is not None:
      self.map = MeshIndex.fromfile(mapfile)
    else:
      self.map = None

    if boxsize is None and 'boxsize' in self.C:
      boxsize = numpy.ones(3) * self.C['boxsize']

    if boxsize is not None:
      self.boxsize[...] = boxsize
    else:
      self.boxsize[...] = 1.0 

    self.periodic = periodic

    self.cosmology = create_cosmology(self.C)
    try:
      self.redshift = self.C['redshift']
    except:
      self.redshift = 0.0

    self.schema('gas', 0, ['sml', 'mass', 'id', 'ye', 'ie', 'entropy'])
    self.schema('bh', 5, ['bhmass', 'bhmdot', 'id'])
    self.schema('dark', 1, ['mass', 'id'])
    self.schema('star', 4, ['mass', 'sft', 'id'])
Exemplo n.º 3
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument("-f", dest='format', required=True)
  parser.add_argument("filename")
  parser.add_argument("-N", dest='N', default=64, required=False, type=int)
  parser.add_argument("-o", dest='output', default=None, required=True)
  args = parser.parse_args()
  
  if '%d' in args.filename:
    snap0 = Snapshot(args.filename % 0, args.format)
    Nfile = snap0.C['Nfiles']
  else:
    snap0 = Snapshot(args.filename, args.format)
    Nfile = 1

  boxsize = snap0.C['boxsize']
  m = MeshIndex(N=args.N, Nd=3, boxsize=boxsize)

  with sharedmem.Pool(use_threads=False) as pool:
    def work(fid):
      if '%d' in args.filename:
        snap = Snapshot(args.filename % fid, args.format)
      else:
        snap = Snapshot(args.filename, args.format)
      print 'start', snap.file
      pos = snap['pos']
      print 'read', snap.file
      return m.set(fid, pos) 
    l = pool.map(work, range(Nfile))

  m.compile(dict(l))
  m.tofile(args.output)
Exemplo n.º 4
0
    def use(self,
            snapname,
            format,
            periodic=False,
            origin=[0, 0, 0.],
            boxsize=None,
            mapfile=None,
            **kwargs):
        """ kwargs will be passed to reader,
        only particles within origin and boxsize are loaded.
    """
        self.snapname = snapname
        if format == 'hdf5':
            self.format = 'hdf5'
        elif format == 'bigfile':
            self.format = 'bigfile'
        else:
            self.format = Reader(format, **kwargs)
        try:
            snapname = self.snapname % 0
        except TypeError:
            snapname = self.snapname

        snap = Snapshot(snapname, self.format)

        self.C = snap.C
        self._template = snap
        self._schema = snap.schema

        self.origin[...] = numpy.ones(3) * origin

        if boxsize is not None:
            self.need_cut = True
        else:
            self.need_cut = False

        if mapfile is not None:
            self.map = MeshIndex.fromfile(mapfile)
        else:
            self.map = None

        if boxsize is None and 'boxsize' in self.C:
            boxsize = numpy.ones(3) * self.C['boxsize']

        if boxsize is not None:
            self.boxsize[...] = boxsize
        else:
            self.boxsize[...] = 1.0

        self.periodic = periodic

        self.cosmology = create_cosmology(self.C)
        try:
            self.redshift = self.C['redshift']
        except:
            self.redshift = 0.0

        self.schema('gas', 0, ['sml', 'mass', 'id', 'ye', 'ie', 'entropy'])
        self.schema('bh', 5, ['bhmass', 'bhmdot', 'id'])
        self.schema('dark', 1, ['mass', 'id'])
        self.schema('star', 4, ['mass', 'sft', 'id'])