diskpy.utils package

Module contents

Created on Thu Jul 16 11:18:26 2015

@author: ibackus

diskpy.utils.configparser(fname, ftype=’auto’)[source]
parameters = configparser(fname,ftype=’auto’)

Tries to parse ChaNGa configuration files

ftype can be ‘auto’, ‘param’, or ‘director’. If auto, config parser will try to determine the filetype on its own.

returns:
dictionary ‘parameters’. The keys are the names of the parameters and the values are the values defined in the file fname
diskpy.utils.configsave(param, filename, ftype=’auto’)[source]

Saves parameters defined by param (see configparser) to filename. Possible ftypes are ‘director’ and ‘param’. If set to auto, configsave tries to guess file type from the extension.

diskpy.utils.units_from_param(param)[source]
diskpy.utils.strip_units(x)[source]

Removes the units from a SimArray and returns as a numpy array. Note that x is copied so that it is not destroyed

x can be a single SimArray or a tuple or list of SimArrays

If any of the inputs are single number, they are returned as a number

USAGE:

array = strip_units(SimArray)

array1, array2, … = strip_units([SimArray1, SimArray2, …])

diskpy.utils.set_units(x, units)[source]

Sets the units of x to units. If x has units, they are ignored. Does not destroy/alter x

USAGE:

SimArray = set_units(x, units)

SimArray1, SimArray2, … = set_units([x1, x2, …], units)

SimArray1, SimArray2, … = set_units([x1, x2, …], [units1, units2, …])

diskpy.utils.match_units(x, y)[source]

Matches the units of x to y and returns x and y in the same units.

IF x and y don’t have units, they are unchanged

IF one of x or y has units, the unitless quantity is returned as a SimArray (see pb.array.SimArray) with the units of the other quantity.

IF both have units, then an attempt is made to convert x into the units of y. If this is not possible, an error is raised, for example if x is in units of ‘au’ and y is in units of ‘Msol’

x, y can be: scalar, array, SimArray, pynbody unit (eg pb.units.G),
or a unit string (eg ‘Msol a**-2’)

* RETURNS *

x, y are returned as a tuple

diskpy.utils.findfiles(filefilter=’*’, basedir=’.’)[source]

Recursively find files according to filefilter

** ARGUMENTS **

filefilter : str
Filter for finding files. ie, ‘*.jpg’ or ‘file.txt’
basedir : str
Base directory to search. Default is the current directory

** RETURNS **

files : list
A list of the full path to all files matching filefilter
diskpy.utils.pbverbosity(cmd=None)[source]

Changes and returns pynbody verbosity. Works for different versions of pynbody.

ARGUMENTS

cmd
-If None (default) current verbosity level is returned, nothing is done -If ‘off’, pynbody is silenced -If ‘on’, pynbody verbosity is set on -If something else, cmd is assumed to be a verbosity level

RETURNS

current_verbosity
pynbody verbosity level before any changes were made

EXAMPLES

Toggle pynbody verbosity

current_verbosity = pbverbosity(‘off’) … do stuff … pbverbosity(current_verbosity)
diskpy.utils.str2num(string)[source]

Tries to see if ‘string’ is a number

If ‘string’ is a string, returns:
int(string) for integers float(string) for floats ‘string’ otherwise
If ‘string’ is a float or an integer, returns:
string

If none of the above, treats it like a list or tuple and returns for each entry of ‘string’ a float,int, or str as required. Returns as a list ————————————————–

diskpy.utils.get_module_names(fname)[source]

An import utility that returns the module names in the directory of file. Ignores filenames beginning with an underscore.

Parameters:

fname : str

Filename

Returns:

modulenames : list

A list of the modules

diskpy.utils.logparser(fname, verbose=False)[source]

Parses a ChaNGa log file to find run-time parameters. Also returns the header under the key ‘header’

Parameters:

fname : str

Filename of the log file to open

verbose : bool

(optional) If True, prints all the parameters

Returns:

param : dict

Dictionary of the parameters

See also

configparser

diskpy.utils.which(cmd)[source]

Pythonic equivalent of UNIX which

Parameters:

cmd : str

Command to find

Returns:

path : str -or- None

Full path to command which would be executed in current shell by cmd If the command cannot be found, None is returned

diskpy.utils.deepreload(module)[source]

Convenience package for reloading diskpy modules when working.

Parameters:

module : str

Module to reload

Returns:

command : str

Command to exec which will reload the module and its parents

Examples

>>> command = deepreload('diskpy.disk._spirals')
>>> exec(command)

Or, equivalently:

>>> exec(deepreload('diskpy.disk._spirals'))

This is equivalent to:

>>> reload(diskpy.disk._spirals)
>>> reload(diskpy.disk)
>>> reload(diskpy)
diskpy.utils.get_units(x)[source]
Retrieves the units of x if:
  • x has a unit
  • x is a unit
  • x is a unit string (see pynbody)

Else, returns None

class diskpy.utils.logPrinter(verbose=True, filename=None, overwrite=True)[source]

A simple class to print both to screen and to a log file

logPrinter(verbose=True, filename=None, overwrite=True)

Parameters:

verbose : bool

print to screen

filename : str

File to save to (if None, don’t save to file)

overwrite : bool

Overwrite file if it exists

printing can be used via the printer method or by calling the logprinter

close()[source]

close the log file

diskpy.utils.as_simarray(x, assumed_units=‘1’)[source]

Return x as a SimArray. x can be a number, a numpy array, a SimArray, an interpretable string (e.g. ‘1 mm’ or ‘2e3 au’).

Parameters:

x : object

object to make a simarray from. If already a simarray, x is returned

assumed_units : unit-like

The units used if x has no units. Can be a string that pynbody units takes (e.g. ‘au’ or ‘Msol yr**-1’) or a pynbody unit. If None, no units will be assumed.

Returns:

xsimarray : SimArray

x as a SimArray