How to use the pynbody.util.open_ function in pynbody

To help you get started, we’ve selected a few pynbody examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pynbody / pynbody / pynbody / halo.py View on Github external
def _get_file_positions(self,filename):
        """Get the starting positions of each halo's particle information within the
        AHF_particles file for faster access later"""
        if os.path.exists(self._ahfBasename + 'fpos'):
            f = util.open_(self._ahfBasename + 'fpos')
            for i in range(self._nhalos):
                self._halos[i+1].properties['fstart'] = int(f.readline())
            f.close()
        else:
            f = util.open_(filename)
            for h in xrange(self._nhalos):
                if len((f.readline().split())) == 1:
                    f.readline()
                self._halos[h+1].properties['fstart'] = f.tell()
                for i in xrange(self._halos[h+1].properties['npart']):
                    f.readline()
            f.close()
github pynbody / pynbody / pynbody / halo.py View on Github external
def _load_ahf_halos(self, filename):
        f = util.open_(filename,"rt")
        # get all the property names from the first, commented line
        # remove (#)
        keys = [re.sub('\([0-9]*\)', '', field)
                for field in f.readline().split()]
        # provide translations
        for i, key in enumerate(keys):
            if self.isnew:
                if(key == '#npart'):
                    keys[i] = 'npart'
            else:
                if(key == '#'):
                    keys[i] = 'dumb'
            if(key == 'a'):
                keys[i] = 'a_axis'
            if(key == 'b'):
                keys[i] = 'b_axis'
github pynbody / pynbody / pynbody / snapshot / tipsy.py View on Github external
if "." in l or "e" in l or l[:-1] == "inf":
                    dtype = float
                else:
                    dtype = int

                # Restart at head of file
                f.seek(0)
                f.readline()

            loadblock = lambda count: np.fromfile(
                f, dtype=dtype, sep="\n", count=count)
            # data = np.fromfile(f, dtype=tp, sep="\n")
        except ValueError:
            # this is probably a binary file
            binary = True
            f = util.open_(filename, 'rb')

            # Read header and check endianness
            if self._byteswap:
                l = struct.unpack(">i", f.read(4))[0]
            else:
                l = struct.unpack("i", f.read(4))[0]

            if l != self._load_control.disk_num_particles:
                raise IOError, "Incorrect file format"

            if dtype is None:
                # Set data format to be read (float or int) based on config
                int_arrays = map(
                    str.strip, config_parser.get('tipsy', 'binary-int-arrays').split(","))
                if array_name in int_arrays:
                    dtype = 'i'
github pynbody / pynbody / pynbody / snapshot / tipsy.py View on Github external
if isinstance(arrays, str):
            arrays = [arrays]

        # check if arrays includes a 3D array
        if 'pos' in arrays:
            arrays.remove('pos')
            for arr in ['x', 'y', 'z']:
                arrays.append(arr)
        if 'vel' in arrays:
            arrays.remove('vel')
            for arr in ['vx', 'vy', 'vz']:
                arrays.append(arr)

        with self.lazy_off:
            fin = util.open_(self.filename, "rb")
            fout = util.open_(self.filename + ".tmp", "wb")

            if self._byteswap:
                t, n, ndim, ng, nd, ns = struct.unpack(">diiiii", fin.read(28))
                fout.write(struct.pack(">diiiiii", t, n, ndim, ng, nd, ns, 0))
            else:
                t, n, ndim, ng, nd, ns = struct.unpack("diiiii", fin.read(28))
                fout.write(struct.pack("diiiiii", t, n, ndim, ng, nd, ns, 0))

            fin.read(4)

            if family.gas in fam_out:
                assert(ng == len(self[family.gas]))
            if family.dm in fam_out:
                assert(nd == len(self[family.dm]))
            if family.star in fam_out:
                assert(ns == len(self[family.star]))
github pynbody / pynbody / pynbody / snapshot / tipsy.py View on Github external
def is_readable_array(x):
            try:
                f = util.open_(x, 'r')
                return int(f.readline()) == len(self)
            except ValueError:
                # could be a binary file
                f = util.open_(x, 'rb')

                header = f.read(4)
                if len(header)!=4:
                    return False

                if self._byteswap:
                    buflen = struct.unpack(">i", header)[0]
                else:
                    buflen = struct.unpack("i", header)[0]

                ourlen_1 = (self._load_control.disk_num_particles)& 0xffffffffL
                ourlen_3 = (self._load_control.disk_num_particles*3)& 0xffffffffL
github pynbody / pynbody / pynbody / snapshot / tipsy.py View on Github external
if self._filename[-3:] == '.gz':
                    filename = self._filename[:-3] + "." + array_name + ".gz"
                else:
                    filename = self._filename + "." + array_name

            if binary:
                fhand = util.open_(filename, 'wb')

                if byteswap:
                    fhand.write(struct.pack(">i", len(self)))

                else:
                    fhand.write(struct.pack("i", len(self)))

            else:
                fhand = util.open_(filename, 'wb')
                fhand.write((str(len(self)) + '\n').encode('utf-8'))

            if contents is None:
                if array_name in self.family_keys():
                    for f in [family.gas, family.dm, family.star]:
                        try:
                            dtype = self[f][array_name].dtype
                            ar = self[f][array_name]
                            units_out = ar.units

                        except KeyError:
                            ar = np.zeros(len(self[f]), dtype=int)

                        TipsySnap.__write_block(fhand, ar, binary, byteswap)

                else:
github pynbody / pynbody / pynbody / halo / rockstar.py View on Github external
def _get_dummy_for_halo(self, n):
        if n=self._halo_max:
            raise KeyError, "No such halo"

        with util.open_(self._rsFilename, 'rb') as f:
            f.seek(self._haloprops_offset+(n-self._halo_min)*self.halo_type.itemsize)
            halo_data = np.fromfile(f, dtype=self.halo_type, count=1)

        hn = DummyHalo()
        # TODO: properties are in Msun / h, Mpc / h
        hn.properties = dict(zip(halo_data.dtype.names,halo_data[0]))
        return hn
github pynbody / pynbody / pynbody / halo.py View on Github external
nparr = np.array([self._halos[i+1].properties['npart'] for i in range(self._nhalos)])
            osort = np.argsort(nparr)[::-1]
            self._sorted_indices = osort + 1
            hcnt = self._sorted_indices

        else:
            hcnt = np.arange(len(self._sorted_indices)) + 1

        if top_level is False:
            hord = self._sorted_indices
        else:
            hord = self._sorted_indices[::-1]
            hcnt = hcnt[::-1]

        if self._all_parts is None:
            f = util.open_(self._ahfBasename+'particles')

        cnt = 0
        ar = np.empty(len(target),dtype=np.int32)
        ar[:]=-1
        for i in hord:
            halo = self._halos[i]
            if self._all_parts is not None:
                ids = halo.get_index_list(self.base)
            else:
                f.seek(halo.properties['fstart'],0)
                ids = self._load_ahf_particle_block(f,halo.properties['npart'])
            if family is None:
                ar[ids] = hcnt[cnt]
            else:
                if famslice:
                    t_mask = (ids >= famslice.start) & (ids < famslice.stop)