Ejemplo n.º 1
0
    def __init__(self, filename):
        """ Read the binary GADGET header """
        file = open(filename, mode='rb')

        header = binary_snapshot_io.read_header(file)
        file.close()
        
        self._data = header
        del self._data['buffer']
        
        self.num_particles_file = np.sum(self._data['num_particles'])
        
        if self._data['flag_doubleprecision']:
            self.dtype = _rtype(float64, self._data['swap_endian'])
        else:
            self.dtype = _rtype(float32, self._data['swap_endian'])
        self.dtype_width = np.dtype(self.dtype).itemsize

        #Also find length of id
        f = open(filename, mode='rb')        
        f.seek(256+24 + int(self.num_particles_file) * 6 * int(self.dtype_width))

        id_block_len = np.fromfile(f, _rtype(uint32, self._data['swap_endian']), 1)[0]
        if id_block_len==4*self.num_particles_file:
            self.long_ids = False
            self.id_width = 4
            self.id_type = _rtype(uint32, self._data['swap_endian'])
        elif id_block_len==8*self.num_particles_file:
            self.long_ids = True
            self.id_width = 8
            self.id_type = _rtype(uint64, self._data['swap_endian'])            
        else:
            raise Exception('Number of bytes in id block (%d) should be 4 or 8x\nnumber of particles (%d)!'%(id_block_len, self.num_particles_file))
        
        f.close()            
Ejemplo n.º 2
0
    def __init__(self, filename):
        """ Read the binary GADGET header of a format 2 snapshot """
        file = open(filename, mode='rb')

        file.seek(16) # skip 4 HEAD 264 4
        
        header = binary_snapshot_io.read_header(file)
        
        self._data = header
        del self._data['buffer']
        
        self.num_particles_file = np.sum(self._data['num_particles'])
        
        if self._data['flag_doubleprecision']:
            self.dtype = _rtype(float64, self._data['swap_endian'])
        else:
            self.dtype = _rtype(float32, self._data['swap_endian'])
        self.dtype_width = np.dtype(self.dtype).itemsize

        # Now build the list of labels        
        u32 = _rtype(uint32, self._data['swap_endian'])
        
        file_offset = 8
        self._file_offsets = {'HEAD':file_offset}
        file_offset += 24 + 256

        while True:
            eight = file.read(4)
            if len(eight)==0:
                break
            n = np.fromstring(eight, u32,1)[0]
            assert(n==8)
            name = file.read(4)
            file_offset += 8
            #print 'Found name', name

            self._file_offsets[name] = file_offset

            count = np.fromfile(file, u32,1)[0]
            file.read(4)
            count2 = np.fromfile(file, u32,1)[0]
            file.seek(count2+4, 1)
            file_offset += count2+16

        file.close()

        # Also find length of id block (by differencing file offsets between this block and next)
        ordered_blocks = sorted(self._file_offsets.keys(), key=self._file_offsets.__getitem__)
        next_block = ordered_blocks[ordered_blocks.index('ID  ')+1]
        
        id_block_len = self._file_offsets[next_block] - self._file_offsets['ID  '] - 24
        # print 'id_block_len', id_block_len
        if id_block_len==4*self.num_particles_file:
            self.long_ids = False
            self.id_width = 4
            self.id_type = _rtype(uint32, self._data['swap_endian'])
        elif id_block_len==8*self.num_particles_file:
            self.long_ids = True
            self.id_width = 8
            self.id_type = _rtype(uint64, self._data['swap_endian'])            
        else:
            raise Exception('Number of bytes in id block (%d) should be 4 or 8x\nnumber of particles (%d)!'%(id_block_len, self.num_particles_file))