Utility functions

c2raytools.helper_functions.determine_filetype(filename)

Try to figure out what type of data is in filename.

Parameters:
  • filename (string): the filename. May include the full

    path.

Returns:
A string with the data type. Possible values are: ‘xfrac’, ‘density’, ‘velocity’, ‘cbin’, ‘unknown’
c2raytools.helper_functions.determine_redshift_from_filename(filename)

Try to find the redshift hidden in the filename. If there are many sequences of numbers in the filename this method will guess that the longest sequence is the redshift.

Parameters:
  • filename (string): the filename to analyze
Returns:
  • redshift (float)

If no redshift could be found, return -1

c2raytools.helper_functions.find_idx(ar, values)

Find the (fractional) indices of values in an array. If values contains values outside of the range of ar, these will be clamped to the bounds of ar.

Parameters:
  • ar (numpy array): the array to search through
  • values (numpy array or float): the value(s) to look for
Returns:
The indices of the values. Can be in-between integer indices
c2raytools.helper_functions.flt_comp(x, y, epsilon=0.0001)

Compare two floats, return true of difference is < epsilon

c2raytools.helper_functions.get_data_and_type(indata, cbin_bits=32, cbin_order='c', raw_density=False)

Extract the actual data from an object (which may be a file object or a filename to be read), and determine what type of data it is.

Parameters:
  • indata (XfracFile, DensityFile, string or numpy array): the data

  • cbin_bits (integer): the number of bits to use if indata is a cbin file

  • cbin_order (string): the order of the data in indata if it’s a cbin file

  • raw_density (bool): if this is true, and the data is a

    density file, the raw (simulation units) density will be returned instead of the density in cgs units

Returns:
  • A tuple with (outdata, type), where outdata is a numpy array

containing the actual data and type is a string with the type of data. Possible values for type are ‘xfrac’, ‘density’, ‘cbin’ and ‘unknown’

c2raytools.helper_functions.get_dens_redshifts(dens_dir, z_low=None, z_high=None, bracket=False)

Make a list of the redshifts of all the density files in a directory.

Parameters:
  • dens_dir (string): the directory to look in

  • z_low = None (float): the minimum redshift to include (if given)

  • z_high = None (float): the maximum redshift to include (if given)

  • bracket = False (bool): if true, also include the redshifts on the

    lower side of z_low and the higher side of z_high

Returns:
The redhifts of the files (numpy array of floats)
c2raytools.helper_functions.get_eval()

Evaluate an expression using numexpr if available. For internal use.

c2raytools.helper_functions.get_interpolated_array(in_array, new_len, kind='nearest')

Get a higher-res version of an array.

Parameters:
  • in_array (numpy array): the array to upscale
  • new_len (integer): the new length of the array
  • kind = ‘nearest’ (string): the type of interpolation to use
Returns:
The upscaled array.
c2raytools.helper_functions.get_mesh_size(filename)

Read only the first three integers that specify the mesh size of a file.

Parameters:
  • filename (string): the file to read from. can be an xfrac file,

    a density file or a cbin file.

Returns:
(mx,my,mz) tuple
c2raytools.helper_functions.get_xfrac_redshifts(xfrac_dir, z_low=None, z_high=None, bracket=False)

Make a list of the redshifts of all the xfrac files in a directory.

Parameters:
  • xfrac_dir (string): the directory to look in

  • z_low = None (float): the minimum redshift to include (if given)

  • z_high = None (float): the maximum redshift to include (if given)

  • bracket = False (bool): if true, also include the redshifts on the

    lower side of z_low and the higher side of z_high

Returns:
The redhifts of the files (numpy array of floats)
c2raytools.helper_functions.outputify(output)

If given a list with only one element, return the element If given a standard python list or tuple, make it into a numpy array.

Parameters:
output (any scalar or list-like): the output to process
Returns:
The output in the correct format.
c2raytools.helper_functions.print_msg(message, print_time=True)

Print a message if verbose is true

c2raytools.helper_functions.read_cbin(filename, bits=32, order='C', dimensions=3)

Read a binary file with three inital integers (a cbin file).

Parameters:
  • filename (string): the filename to read from

  • bits = 32 (integer): the number of bits in the file

  • order = ‘C’ (string): the ordering of the data. Can be ‘C’

    for C style ordering, or ‘F’ for fortran style.

  • dimensions (int): the number of dimensions of the data (default:3)

Returns:
The data as a three dimensional numpy array.
c2raytools.helper_functions.read_fits(filename)

Read a fits file and return the data as a numpy array

Parameters:
  • filename (string): the fits file to read
Returns:
numpy array containing the data
c2raytools.helper_functions.read_raw_binary(filename, bits=64, order='C')

Read a raw binary file with no mesh info. The mesh is assumed to be cubic.

Parameters:
  • filename (string): the filename to read from

  • bits = 64 (integer): the number of bits in the file

  • order = ‘C’ (string): the ordering of the data. Can be ‘C’

    for C style ordering, or ‘F’ for fortran style.

Returns:
The data as a three dimensional numpy array.
c2raytools.helper_functions.save_cbin(filename, data, bits=32, order='C')

Save a binary file with three inital integers (a cbin file).

Parameters:
  • filename (string): the filename to save to

  • data (numpy array): the data to save

  • bits = 32 (integer): the number of bits in the file

  • order = ‘C’ (string): the ordering of the data. Can be ‘C’

    for C style ordering, or ‘F’ for fortran style.

Returns:
Nothing
c2raytools.helper_functions.save_fits(data, filename)

Save data as a fits file. The data can be a file object, a file to read or a pure data array.

Parameters:
  • indata (XfracFile, DensityFile, string or numpy array): the data to save
  • filename (string): the file to save to
Returns:
Nothing
c2raytools.helper_functions.save_raw_binary(filename, data, bits=64, order='C')

Save a raw binary file with no mesh info.

Parameters:
  • filename (string): the filename to read from

  • data (numpy array): the data to save

  • bits = 64 (integer): the number of bits in the file

  • order = ‘C’ (string): the ordering of the data. Can be ‘C’

    for C style ordering, or ‘F’ for fortran style.

Returns:
The data as a three dimensional numpy array.
c2raytools.helper_functions.set_verbose(_verbose)

Turn on or off verbose mode.

Parameters:
  • verb (bool): whether or not to be verbose
Returns:
Nothing

Previous topic

Constants and conversion factors

This Page