How to use the pynbody.config_parser 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 / snapshot / __init__.py View on Github external
new_units.append(units.Unit(x))


        self._file_units_system = new_units

        # set new units for all known arrays
        for arr_name in self.keys():
            arr = self[arr_name]
            # if the array has units, then use the current units, else
            # check if a default dimension for this array exists in
            # the configuration
            if arr.units != units.NoUnit():
                ref_unit = arr.units
            else:
                try:
                    ref_unit = config_parser.get(
                        'default-array-dimensions', arr_name)
                except ConfigParser.NoOptionError:
                    # give up -- no applicable dimension found
                    continue

            arr.set_units_like(ref_unit)
github pynbody / pynbody / pynbody / halo / ahf.py View on Github external
def _run_ahf(self, sim):
        # if (sim is pynbody.tipsy.TipsySnap) :
        typecode = 90
        # elif (sim is pynbody.gadget.GadgetSnap):
        #   typecode = '60' or '61'
        import pynbody.units as units
        # find AHFstep

        groupfinder = config_parser.get('AHFCatalogue', 'Path')

        if groupfinder == 'None':
            for directory in os.environ["PATH"].split(os.pathsep):
                ahfs = glob.glob(os.path.join(directory, "AHF*"))
                for iahf, ahf in enumerate(ahfs):
                    # if there are more AHF*'s than 1, it's not the last one, and
                    # it's AHFstep, then continue, otherwise it's OK.
                    if ((len(ahfs) > 1) & (iahf != len(ahfs) - 1) &
                            (os.path.basename(ahf) == 'AHFstep')):
                        continue
                    else:
                        groupfinder = ahf
                        break

        if not os.path.exists(groupfinder):
            raise RuntimeError("Path to AHF (%s) is invalid" % groupfinder)
github pynbody / pynbody / pynbody / snapshot / gadgethdf.py View on Github external
@GadgetHDFSnap.decorator
def do_units(sim):

    cosmo = 'HubbleParam' in sim._get_hdf_header_attrs().keys()

    try:
        atr = sim._get_hdf_unit_attrs()
    except KeyError:
        # Use default values, from default_config.ini if necessary
        vel_unit = config_parser.get('gadget-units', 'vel')
        dist_unit = config_parser.get('gadget-units', 'pos')
        mass_unit = config_parser.get('gadget-units', 'mass')
        warnings.warn(
            "No unit information found: using gadget-units.", RuntimeWarning)
        sim._file_units_system = [units.Unit(x) for x in [
                vel_unit, dist_unit, mass_unit, "K"]]
        return

    vel_unit = atr['UnitVelocity_in_cm_per_s']*units.cm/units.s
    dist_unit = atr['UnitLength_in_cm']*units.cm
    mass_unit = atr['UnitMass_in_g']*units.g

    if cosmo:
        for fac in GadgetHDFSnap._get_cosmo_factors(sim._hdf_files[0],'Coordinates') : dist_unit *= fac
        for fac in GadgetHDFSnap._get_cosmo_factors(sim._hdf_files[0],'Velocity') : vel_unit *= fac
        for fac in GadgetHDFSnap._get_cosmo_factors(sim._hdf_files[0],'Mass') : mass_unit *= fac
github pynbody / pynbody / pynbody / family.py View on Github external
def __lt__(self, other):
        return str(self)=3 :
    Family = functools.total_ordering(Family)


# Instantiate the default families as specified
# by the configuration file

g = globals()
for f in config_parser.options('families'):
    aliases = config_parser.get('families', f)
    g[f] = Family(f, map(str.strip, aliases.split(",")))
github pynbody / pynbody / pynbody / snapshot / gadget.py View on Github external
import numpy as np
import struct
import sys
import copy
import os.path as path
import warnings
import errno
import itertools

# This is set here and not in a config file because too many things break
# if it is not 6
N_TYPE = 6

_type_map = backcompat.OrderedDict({})

for name, gtypes in config_parser.items('gadget-type-mapping'):
    try:
        gtypes = np.array([int(q) for q in gtypes.split(",")])
        if (gtypes >= N_TYPE).any() or (gtypes < 0).any():
            raise ValueError(
                "Type specified for family " + name + " is out of bounds (" + gtypes + ").")
        _type_map[family.get_family(name)] = gtypes
    except ConfigParser.NoOptionError:
        pass

_name_map, _rev_name_map = util.setup_name_maps(
    'gadget-name-mapping', gadget_blocks=True)
_translate_array_name = util.name_map_function(_name_map, _rev_name_map)


def _to_raw(s):
    if isinstance(s, str) and sys.version_info[0] > 2:
github pynbody / pynbody / pynbody / snapshot / tipsy.py View on Github external
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'
                else:
                    dtype = 'f'

            # Read longest data array possible.
            # Assume byteswap since most will be.
            if self._byteswap:
                loadblock = lambda count: np.fromstring(
                    f.read(count * 4), dtype=dtype, count=count).byteswap()
                # data = np.fromstring(f.read(3*len(self)*4),dtype).byteswap()
            else:
                loadblock = lambda count: np.fromstring(
                    f.read(count * 4), dtype=dtype, count=count)
                # data = np.fromstring(f.read(3*len(self)*4),dtype)
github pynbody / pynbody / pynbody / family.py View on Github external
def __lt__(self, other):
        return str(self)=3 :
    Family = functools.total_ordering(Family)


# Instantiate the default families as specified
# by the configuration file

g = globals()
for f in config_parser.options('families'):
    aliases = config_parser.get('families', f)
    g[f] = Family(f, map(str.strip, aliases.split(",")))
github pynbody / pynbody / pynbody / snapshot / gadget.py View on Github external
@GadgetSnap.decorator
def do_units(sim):
    # cosmo =
    # (sim._hdf['Parameters']['NumericalParameters'].attrs['ComovingIntegrationOn'])!=0

    vel_unit = config_parser.get('gadget-units', 'vel')
    dist_unit = config_parser.get('gadget-units', 'pos')
    mass_unit = config_parser.get('gadget-units', 'mass')

    vel_unit, dist_unit, mass_unit = [
        units.Unit(x) for x in vel_unit, dist_unit, mass_unit]

    if sim.header.HubbleParam == 0.:
        # remove a and h dependences
        vel_unit = units.Unit(
            "km s^-1") * vel_unit.in_units("km s^-1", a=1, h=1)
        mass_unit = units.Unit("Msol") * mass_unit.in_units("Msol", a=1, h=1)
        dist_unit = units.Unit("kpc") * dist_unit.in_units("kpc", a=1, h=1)

    sim._file_units_system = [units.Unit("K"), vel_unit, dist_unit, mass_unit]
github pynbody / pynbody / pynbody / snapshot / ramses.py View on Github external
ramses_grav_header = np.dtype([('ncpu', 'i4'), ('ndim', 'i4'), ('nlevelmax', 'i4'),
                               ('nboundary', 'i4')])

particle_blocks = map(
    str.strip, config_parser.get('ramses', "particle-blocks").split(","))
particle_format = map(
    str.strip, config_parser.get('ramses', "particle-format").split(","))

hydro_blocks = map(
    str.strip, config_parser.get('ramses', "hydro-blocks").split(","))
grav_blocks = map(
    str.strip, config_parser.get('ramses', "gravity-blocks").split(","))

particle_distinguisher = map(
    str.strip, config_parser.get('ramses', 'particle-distinguisher').split(","))


class RamsesSnap(SimSnap):
    reader_pool = None

    def __init__(self, dirname, **kwargs):
        """Initialize a RamsesSnap. Extra kwargs supported:

         *cpus* : a list of the CPU IDs to load. If not set, load all CPU's data.
         *maxlevel* : the maximum refinement level to load. If not set, the deepest level is loaded.
         """

        global config
        super(RamsesSnap, self).__init__()

        if multiprocess:
github pynbody / pynbody / pynbody / halo / ahf.py View on Github external
self._get_file_positions(self._ahfBasename + 'particles')

        if self._dosort is not None:
            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

        if os.path.isfile(self._ahfBasename + 'substructure'):
            logger.info("AHFCatalogue loading substructure")

            self._load_ahf_substructure(self._ahfBasename + 'substructure')
        else:
            self._setup_children()

        if make_grp is None:
            make_grp = config_parser.getboolean('AHFCatalogue', 'AutoGrp')

        if make_grp:
            self.make_grp()

        if config_parser.getboolean('AHFCatalogue', 'AutoPid'):
            sim['pid'] = np.arange(0, len(sim))

        if write_fpos:
            if not os.path.exists(self._ahfBasename + 'fpos'):
                self._write_fpos()

        logger.info("AHFCatalogue loaded")