Package ComboCode :: Package cc :: Package tools :: Package io :: Module DataIO
[hide private]
[frames] | no frames]

Module DataIO

source code

Module for reading and writing data files as well as parsing of information.

Author: R. Lombaert

Functions [hide private]
anything. Or function.
read(func, module=<module 'ComboCode.cc.tools.io.DataIO' from '/Users/robinl/Com..., return_func=0, *args, **kwargs)
Takes any function given as a str (preceding modules separated with a dot '.' ) and passes any additional args and kwargs to the function found in either the DataIO module or the given module.
source code
int
findKey(i, data, key)
Find the index of the line that contains the first occurrence of a keyword.
source code
list[]
getKeyData(incr, filename, keyword, single=1)
Search a data file with data in a single (or multiple) columns, separated by comment lines containing the type of data.
source code
array
readFortranFile(convert_cols, func=<function loadtxt at 0x103cd5050>, *args, **kwargs)
Reads a fortran data file.
source code
list/array
getGastronoomOutput(filename, keyword='RADIUS', begin_index=0, return_array=0, key_index=0)
Search GASTRoNOoM output for relevant envelope information.
source code
list
getInputData(path='/Users/robinl/ComboCode/usr', keyword='STAR_NAME', filename='Star.dat', remove_underscore=0, make_float=1, start_index=1, rindex=None)
Search ComboCode/usr files for parameters.
source code
list[string] or list[list[string]]
readFile(filename, delimiter=None, replace_spaces=1)
Read file, and return content with delimiter of choice.
source code
dict
readDict(filename=None, lines=None, delimiter='=', comment_chars=['#'], convert_lists=0, convert_floats=0, convert_ints=0, multi_keys=[], start_row=0, end_row=None, key_modifier=None)
Read a file as a dictionary.
source code
float or int
convertInt(number)
Convert a float to an integer.
source code
string
convertString(string)
Convert a string to a string, where 'None' is converted to None.
source code
float or string
convertFloat(string, nans=0, convert_int=0)
Convert a string to a float.
source code
string
inputToString(val, make_int=0, exp_not=0)
Convert an input value to a string.
source code
 
printRecArray(recarr, precision=8)
Print a record array in a mannerly fashion.
source code
(list,list)
removeComments(lines, comment_chars=['#', '!', ';'])
Split input from comments and return both as separate lists.
source code
list[list or array] or (list[list or array],list[str])
readCols(filename, delimiter=' ', make_float=1, start_row=0, make_array=1, nans=0, start_from_keyword='', return_comments=0, comment_chars=['#', '!', ';'], end_row=None)
Read columns, remove comments and turn into floats.
source code
list[string] or list[list[string]]
splitLines(lines, delimiter=None, replace_spaces=1)
Split lines based on a delimiter of choice, which can be None.
source code
 
writeFile(filename, input_lines, mode='w', delimiter='\n')
Write file with a list of strings as input.
source code
 
replaceString(filename, old_str, new_str)
Replace a given string with another in requested filename.
source code
 
writeCols(filename, cols, mode='w', delimiter='\t')
Write columns of data.
source code
int
findNumber(index, floats)
Starting from index, find the index of the next number different from zero in the list.
source code
int
findZero(index, floats)
Starting from index, find the index of the next number equal to zero in the list.
source code
int
findFloat(index, vals)
Starting from index, find the index of the next float in the list, zero or non-zero.
source code
int
findString(index, vals)
Starting from index, find the index of the next String in the list.
source code
 
testFolderExistence(filepath)
Checking if requested folder exists and creating it if not.
source code
 
joinPdf(old, new, del_old=1)
Join .pdf files into a single .pdf and remove the separate ones.
source code
 
checkLink(path, ln_path, folder=1)
Check if a link exists between two paths, and if not create it.
source code
str
fillOutSpaces(string, nchars)
Fill out a string to a set number of characters with spaces.
source code
list[tuple(list[string])] or tuple(list[string])
checkEntryInfo(input_list, number_of_keys, info_type)
Specific input keywords for ComboCode (currently: MOLECULE, TRANSITION, R_POINTS_MASS_LOSS) require multiple arguments separated by a space.
source code
recarray
getChemistryAbundances(filename)
Reads in the Chemistry abundance output, works for both fractional abundances and number densities.
source code
recarray
getChemistryPhysPar(filename, keyword)
Reads in the Chemistry physical output
source code
list
getChemistrySpecies(filename, parents=1)
Reads the species and parent species included in the Chemistry code.
source code
Variables [hide private]
  __package__ = 'ComboCode.cc.tools.io'
Function Details [hide private]

read(func, module=<module 'ComboCode.cc.tools.io.DataIO' from '/Users/robinl/Com..., return_func=0, *args, **kwargs)

source code 

Takes any function given as a str (preceding modules separated with a dot '.' ) and passes any additional args and kwargs to the function found in either the DataIO module or the given module.

Note that numpy is important in DataIO as np, and can also be requested this way.

Parameters:
  • func (str/function) - The requested function. Can also be given as a function, in which case no search for the function is done, and this returns that function's results.
  • module (module) - The home module of the function. Default is DataIO, and any module therein can be accessed by passing module.func to the keyword.

    (default: DataIO)

  • return_func (bool) - Return the function itself, rather than calling it.

    (default: 0)

Returns: anything. Or function.
the function's output is returned.

findKey(i, data, key)

source code 

Find the index of the line that contains the first occurrence of a keyword.

The search is case-insensitive.

Parameters:
  • i (int) - The starting index of the search
  • data (list[list]/list[str]) - The data read with readFile. Either split on a delimiter, or the full line as a str.
  • key (str) - The keyword that is searched
Returns: int
The index of the line that contains the key for the first time

getKeyData(incr, filename, keyword, single=1)

source code 

Search a data file with data in a single (or multiple) columns, separated by comment lines containing the type of data.

The data returned follow the line that contains the key, unless incr is set to 0. In this case, the line that contains the data is returned in its entirety, and single is put to 0.

This method is often used for extracting MCMax output. In that case, for radius and theta incr is usually the grid size (NRAD and NTHETA respectively), and for any other quantity incr is NRAD*NTHETA (fi for denstemp.dat). incr==0 can be used to extract inputvalues from log.dat.

Parameters:
  • incr (int) - length of the data after key that is required. Put this keyword to zero if you are extracting a number from one line that contains the keyword itself. In that case single is put to 0 so you can take your information from the whole line.
  • filename (string) - name and path of the file searched
  • keyword (string) - the type of information required, always equal to one of the keywords present in the file
  • single (bool) - return a list of only the first element on every row. Otherwise the entire line is returned. Off by default if incr == 0.

    (default: 1)

Returns: list[]
The requested data

readFortranFile(convert_cols, func=<function loadtxt at 0x103cd5050>, *args, **kwargs)

source code 

Reads a fortran data file.

The method is identical to np.loadtxt, but defines a converters dict for converting double-notation into floats.

Use as np.loadtxt, but leave out the converters argument.

Parameters:
  • convert_cols (list) - The indices of the columns containing double notation
  • func (function) - The read function used. Default is np.loadtxt, alternative is np.genfromtxt. Function requires converters keyword.

    (default: np.loadtxt)

Returns: array
The np.loadtxt output

getGastronoomOutput(filename, keyword='RADIUS', begin_index=0, return_array=0, key_index=0)

source code 

Search GASTRoNOoM output for relevant envelope information.

Parameters:
  • filename (string) - The filename of the relevant output GASTRoNOoM file
  • keyword (string) - the type of information required, always equal to one of the keywords present in the outputfiles of GASTRoNOoM

    (default: 'RADIUS')

  • begin_index (int) - start looking for keyword at row with begin_index

    (default: 0)

  • return_array (bool) - Return a scipy array rather than a python list

    (default: 0)

  • key_index (int) - If 0 it is automatically determined, otherwise this is the column index

    (default: 0)

Returns: list/array
The requested data from the GASTRoNOoM output

getInputData(path='/Users/robinl/ComboCode/usr', keyword='STAR_NAME', filename='Star.dat', remove_underscore=0, make_float=1, start_index=1, rindex=None)

source code 

Search ComboCode/usr files for parameters. (Can be applied to other files as well)

Includes files such as Dust.dat, Star.dat, Indices.dat, Molecule.dat.

Parameters:
  • path (string) - Location of the input file

    (default: cc.path.usr)

  • keyword (string) - the type of information required, always equal to one of the keywords present in the "Data" of ComboCode, and automatically also a Star dict keyword

    (default: STAR_NAME)

  • filename (string) - filename in that includes wanted information

    (default: 'Star.dat')

  • remove_underscore (bool) - remove the underscores from the entries and replace them by spaces.

    (default: 0)

  • make_float (bool) - set to 0, if no floats are desired at all. If 1, all entries will be converted to floats and on failure, the string is returned instead

    (default: 1)

  • start_index (int) - Start search for keyword on the line before this index (ie data is returned from the first noncommented line at or after this index)

    (default: 1)

  • rindex (int) - Only return element with this index. Default if full list is to be returned.

    (default: None)

Returns: list
Requested data from the usr input (either list or single element)

readFile(filename, delimiter=None, replace_spaces=1)

source code 

Read file, and return content with delimiter of choice.

The delimiter allows for lines to be split into a list of substrings.

Parameters:
  • filename (string) - the full filename of to be read file
  • delimiter (string) - The delimiter, default if strings don't have to be split into a list of substrings

    (default: None)

  • replace_spaces (bool) - Replace any number of spaces or tabs by just one space. If delimiter == ' ', then replace_spaces is always active.

    (default: 1)

Returns: list[string] or list[list[string]]
The lines in the file are returned, either as the full line or split into substrings, depending on the delimiter

readDict(filename=None, lines=None, delimiter='=', comment_chars=['#'], convert_lists=0, convert_floats=0, convert_ints=0, multi_keys=[], start_row=0, end_row=None, key_modifier=None)

source code 

Read a file as a dictionary.

Commented lines and lines without the delimiter are ignored.

If given keywords are present more than once, the entries for those keywords are returned as a list in the dictionary with key equal to the keyword. The given keywords are defined by the list multi_keys

Parameters:
  • filename (string) - the filename of the file to be read. In case of default, lines are required. filename takes precedence over lines.

    (default: None)

  • lines (list[str]) - Skip reading a file, and parse these lines instead. If default, a filename is required. Is a list of strings, each interpreted as a line. Ignored if filename is given.

    (default: None)

  • delimiter (string) - the delimiter defining the key/value pairs

    (default: '=')

  • comment_chars (list of strings) - single character strings setting the comment characters

    (default: ['#'])

  • convert_lists (bool) - convert strings that include lists to real lists

    (default: 0)

  • convert_floats (bool) - convert input values to floats if possible

    (default: 0)

  • convert_ints (bool) - convert input values to ints if number%1 is not 0 only works if also convert_float == 1

    (default: 0)

  • multi_keys (list(string)) - Defines the keywords which may be present multiple times in the dictionary. They are included in the dict as a list even if they are present only once.

    (default: [])

  • start_row (int) - Limit the text file to lines starting from this index.

    (default: 0)

  • end_row (int) - Limit the text file to lines ending before this index (ie last line is end_index - 1). Default includes up to the last line of the file.

    (default: None)

  • key_modifier (str) - A function that modifies the key, such as lower(). Any method that works on a string in principle works. Give the function as a string.

    (default: None)

Returns: dict
the dictionary with the info from the file.

convertInt(number)

source code 

Convert a float to an integer.

Is only done if the float%1 != 0. Otherwise, it remains a float.

Parameters:
  • number (float or string) - The float/string to be converted.
Returns: float or int
The converted integer is returned, or the input float if float%1 is 0.

convertString(string)

source code 

Convert a string to a string, where 'None' is converted to None.

Parameters:
  • string (string) - The string to be converted.
Returns: string
The new string (identical to input) or None

convertFloat(string, nans=0, convert_int=0)

source code 

Convert a string to a float.

If the string cannot be converted, the string itself or a nan is returned.

It is possible to pass objects other than strings and numbers as 'string'. In that case a TypeError for the float conversion is raised, and the object is returned untouched. The nan conversion only happens if a ValueError is raised, ie it was indeed a string that does not represent a number.

Parameters:
  • string (string) - The string to be converted.
  • nans (bool) - Convert the string to nan if it cannot be converted to float

    (default: 0)

  • convert_int (bool) - Convert the float to integer straight away if float(string)%1 != 0

    (default: 0)

Returns: float or string
The converted float is returned, or the input string if conversion failed

inputToString(val, make_int=0, exp_not=0)

source code 

Convert an input value to a string.

Parameters:
  • val (str, int, float) - The input value
  • make_int (bool) - Turn the input value into an integer

    (default: 0)

  • exp_not (bool) - Convert to exponential notation in a string

    (default: 0)

Returns: string
The converted input value

printRecArray(recarr, precision=8)

source code 

Print a record array in a mannerly fashion.

Parameters:
  • recarr (recarray) - The record array.
  • precision (int) - The precision of the floats shown when printed

    (default: 8)

removeComments(lines, comment_chars=['#', '!', ';'])

source code 

Split input from comments and return both as separate lists.

Takes a list of strings, such as what readFile returns.

Parameters:
  • lines (list[str]) - The strings in a file
  • comment_chars (list[str]) - single character strings setting the comment characters

    (default: ['#','!'])

Returns: (list,list)
The input and the comment lines in two separate lists

readCols(filename, delimiter=' ', make_float=1, start_row=0, make_array=1, nans=0, start_from_keyword='', return_comments=0, comment_chars=['#', '!', ';'], end_row=None)

source code 

Read columns, remove comments and turn into floats.

Note that number of columns returned is the minimum number of columns from all rows, where the columns are split by the chosen delimiter (space-like by default).

Parameters:
  • filename (string) - The full filename and path of the file
  • delimiter (string) - delimiter between the columns

    (default: ' ')

  • make_float (bool) - turn everything into floats

    (default: 1)

  • start_row (int) - Limit the text file to lines starting from this row. If start_from_keyword is used, start_row counts from the index where the keyword is first found.

    (default: 0)

  • make_array (bool) - return numpy arrays instead of python lists

    (default: 1)

  • nans (bool) - convert any non-float input value to a numpy.NaN. If False, the strings are returned instead.

    (default: 0)

  • start_from_keyword (string) - Start returning data from the line that contains a given keyword. Only used if not default. The start_row parameter counts from this index onward. (not case sensitive)

    (default: '')

  • return_comments (bool) - Return the comments list in addition to the data

    (default: 0)

  • comment_chars (list[str]) - single character strings setting the comment characters

    (default: ['#','!',';'])

  • end_row (int) - Limit the text file to lines ending before this index (ie last line is end_index - 1). Default includes up to the last line of the file. If start_from_keyword is given, end_row counts with respect to the found index.

    (default: None)

Returns: list[list or array] or (list[list or array],list[str])
The columns are returned, with in addition the comments if requested

splitLines(lines, delimiter=None, replace_spaces=1)

source code 

Split lines based on a delimiter of choice, which can be None.

Parameters:
  • lines (list[string]) - The lines to be split up
  • delimiter (string) - The delimiter, default if strings don't have to be split into a list of substrings

    (default: None)

  • replace_spaces (bool) - replace any number of spaces/tabs by just 1 space

    (default: 1)

Returns: list[string] or list[list[string]]
The lines split up in substrings if delimiter is not None. Otherwise, the strings are returned as they are, or with spaces replaced, depending on the replace_spaces keyword. Empty lines are always removed

writeFile(filename, input_lines, mode='w', delimiter='\n')

source code 

Write file with a list of strings as input.

Parameters:
  • filename (string) - filename of file, includes data type extension
  • input_lines (list[string]) - The lines to be written
  • delimiter (string) - The line separator. Typically the new line character, but can be changed if needed (eg empty string in case new line character is already included in the input lines)

    (default: '\n')

  • mode (string) - writing mode ('w' is new file, 'a' appends to existing file)

    (default: 'w')

replaceString(filename, old_str, new_str)

source code 

Replace a given string with another in requested filename.

The filename can contain whatever works for the glob function, such as the wildcard character, to do multiple files in one go.

Parameters:
  • filename (string) - The filename, possibly with a wildcard character
  • old_str (str) - The old string to be replace in all the files
  • new_str (str) - The new string to be inserted in all the files

writeCols(filename, cols, mode='w', delimiter='\t')

source code 

Write columns of data.

Parameters:
  • filename (string) - filename of target file for writing
  • cols (list[list or array]) - columns to be written, every column given separately in list
  • mode (string) - writing mode ('w' is new file, 'a' appends to existing file)

    (default: 'w')

  • delimiter (string) - The delimiter used between columns

    (default: ' ')

findNumber(index, floats)

source code 

Starting from index, find the index of the next number different from zero in the list. Can be at index itself!

Cannot work with non-float or non-convertible-to-float input values.

If at the end of the list, the index is returned as the length of the list.

Parameters:
  • index (int) - The starting index for the search
  • floats (list[floats]) - The floats being searched for a non-zero number
Returns: int
The index of the next number different from zero, can be param index itself.

findZero(index, floats)

source code 

Starting from index, find the index of the next number equal to zero in the list. Can be at index itself!

If at the end of the list, the index is returned as the length of the list.

Parameters:
  • index (int) - The starting index for the search
  • floats (list[floats]) - The floats being searched for a zero number
Returns: int
The index of the next number equal to zero, can be param index itself.

findFloat(index, vals)

source code 

Starting from index, find the index of the next float in the list, zero or non-zero. Can take strings as input! Goal is to browse until an input value convertible to float is found.

In case that no float is found before the end of the file, the length of the list is returned.

If the given index is larger than the length of the list, the length is also returned.

Parameters:
  • index (int) - The starting index in the list
  • vals (list) - the list of strings and floats
Returns: int
the index of the next float in the list

findString(index, vals)

source code 

Starting from index, find the index of the next String in the list.

In case that no float is found before the end of the file, the length of the list is returned.

If the given index is larger than the length of the list, the length is also returned.

Parameters:
  • index (int) - The starting index in the list
  • vals (list) - the list of strings and floats
Returns: int
the index of the next float in the list

testFolderExistence(filepath)

source code 

Checking if requested folder exists and creating it if not.

Parameters:
  • filepath (string) - the folder name

joinPdf(old, new, del_old=1)

source code 

Join .pdf files into a single .pdf and remove the separate ones.

Requires the pdftk software installed on the system.

Parameters:
  • old (list[string]) - The input filenames of the .pdf files to be joined
  • new (string) - The filename of the joined .pdf file
  • del_old (bool) - delete the old filenames

    (default: 1)

checkLink(path, ln_path, folder=1)

source code 

Check if a link exists between two paths, and if not create it.

Parameters:
  • path (string) - The name of the path the link refers to
  • ln_path (string) - The name of the link
  • folder (bool) - if you're linking a folder, instead of a file. If False, a folder check will not be done.

    (default: 1)

fillOutSpaces(string, nchars)

source code 

Fill out a string to a set number of characters with spaces.

Parameters:
  • string (str) - The string to be filled out, if nchars is < than len(string) the string itself is returned, unchanged.
  • nchars (int) - The number of characters requested in final string
Returns: str
The filled out string.

checkEntryInfo(input_list, number_of_keys, info_type)

source code 

Specific input keywords for ComboCode (currently: MOLECULE, TRANSITION, R_POINTS_MASS_LOSS) require multiple arguments separated by a space. This method sorts those arguments and checks if the format is OK.

The method returns a tuple or a list of these arguments depending on multiplicity of input, and the need to create a grid for the parameter.

Parameters:
  • input_list (list[string]) - argument strings for one of the special CC input keys.
  • number_of_keys (int) - the number of arguments expected
  • info_type (string) - MOLECULE, TRANSITION or R_POINTS_MASS_LOSS
Returns: list[tuple(list[string])] or tuple(list[string])
the sorted arguments for the requested info_type. The output is given as a list or a tuple, depending on if this gridded parameter or not, respectively.

getChemistryAbundances(filename)

source code 

Reads in the Chemistry abundance output, works for both fractional abundances and number densities.

Parameters:
  • filename (string) - The filename of the abundance output (csfrac.out or csnum.out)
Returns: recarray
Recursive array containing the abundance per species name.

getChemistryPhysPar(filename, keyword)

source code 

Reads in the Chemistry physical output

Parameters:
  • filename (string) - The filename of the abundance output (csphyspar.out)
  • keyword (string) - The physical parameter in question. Options are: RADIUS, n(H2), TEMP. A_V, RAD. FIELD, CO K(PHOT), VELOCITY
Returns: recarray
Recursive array containing the abundance per species name.

getChemistrySpecies(filename, parents=1)

source code 

Reads the species and parent species included in the Chemistry code.

Parameters:
  • filename (string) - The .specs file
  • parents - Give parent species as output. If parents=0, all species are given. (default: 1)
Returns: list
(Parent) species included in the Chemistry code.