nugridpy.ascii_table

Ascii_table.py: read and write simple ascii tables

By Daniel Alexander Bertolino Conti Fall 2010 If the user find any bugs or errors, please email fherwig@uvic.ca.

Assumptions for ascii Files:

Headers are always at the beginning of the file and start with a capital H (default, can be reset). The next line after the header lines, is a line of column attribute names. The column attribute names are separated by ‘ ‘ by default or whatever the user dictates to the class. Data columns are seperated by spaces and each data attribute contains no spaces. All the data columns are of equal length. Any file name that has ‘trajectory’ or ‘Trajectory’ in it, is assumed to be a trajectory type file.

Assumptions for Trajectory Files:

The first three lines start with a ‘#’. The first of these contains ‘time’, ‘T’ and ‘rho’, each seperated by a space. The second is ‘# YRS/SEC; T8K/T9K; CGS/LOG’ which are the choices for the header attributes AGEUNIT, TUNIT, and RHOUNIT can be. The third is something like ‘FORMAT:’ The four lines afther those three are the header attributes. Each line has one header attribute, followd by a = and then followed after by the value. After these seven lines comes three columns of data, the first is is associated with ‘time’, the second with ‘T’ and the third with rho.

Functions

write(filename, headers, dcols, data[, …]) Method for writeing Ascii files.
writeGCE_table(filename, headers, data[, …]) Method for writeing data in GCE format in Ascii files.
writeTraj([filename, data, ageunit, tunit, …]) Method for writeing Trajectory type ascii files files.

Classes

ascii_table(filename[, sldir, sep, …]) Data structure to read simple data tables and trajectory data tables.
class nugridpy.ascii_table.ascii_table(filename, sldir='.', sep=' ', datatype='normal', headers=[], dcols=[], data=[], header_char='H', read=True, headerlines=[])

Data structure to read simple data tables and trajectory data tables.

Init method that reads in ascii type files and trajectory type files. By default this method reads ascii type files. If the user wants a trajectory file read, either the file must have ‘trajectory’ in the filename or the user must set the datatype=’trajectory’.

Parameters:
  • filename (string) – The name of the file we are looking at, or writeng to.
  • sldir (string, optional) – Standard directory of filename. The default is ‘.’.
  • sep (string, optional) – The seperator that seperates column attributes in filename. The defaults is ‘ ‘.
  • datatype (string, optional) – What type of ascii table you are reading and or writeing. The only two options currently are ‘normal’ and ‘trajectory’. The default is ‘normal’.
  • Headers (list, optional) – A list of Header strings or if the file being writen is of type trajectory, this is a dictionary of header attributes and their associated values. The default is [].
  • dcols (list, optional) – A list of data attributes. The default is [].
  • data (list, optional) – A list of lists (or of numpy arrays) of columns of data. The default is [].
  • header_char (caracter, optional) – The character that indicates a header lines. The default is ‘H’.
  • read (boolean, optional) – Boolean of weather this is reading or writing a file. The default is True.
  • headerlines (list, optional) – Additional list of strings of header data, only used in trajectory data Types. The default is [].

Examples

>>> from ascii_table import *
>>> p=ascii_table('c12cg.dat')
>>> p.hattrs
['1 12  6  1  1  1  0  0  0  1 13  7  0', '55   1.943', 'c12pg']
>>> a.dcols
['upper', 'lower', 'T9', 'ado(gs)', 'ado/CA88', 'tt/gs', 'ado/fit', 'CA88', 'fitted']
>>> a.get('upper')
[1.3400000000000001e-24, ... 1590.0]
>>> a.plot('T9', 'ado/CA88') #plots data

Also there is a global method called write that will allow a user to write ascii Files and trajectory files.

One Can call this method like

>>> write('file.txt',dcols,data,headers)

Where Dcols is a list of data attributes, data is a list of lists of data and headers is a list of strings that are each a header attribute.

See also

write

get(attri)

Method that dynamically determines the type of attribute that is passed into this method. Also it then returns that attribute’s associated data.

Parameters:attri (string) – The attribute we are looking for.
getColData(attri)

Method that returns column data

Parameters:attri (string) – The attribute we are looking for.
nugridpy.ascii_table.write(filename, headers, dcols, data, headerlines=[], header_char='H', sldir='.', sep=' ', trajectory=False, download=False)

Method for writeing Ascii files.

Note the attribute name at position i in dcols will be associated with the column data at index i in data. Also the number of data columns(in data) must equal the number of data attributes (in dcols) Also all the lengths of that columns must all be the same.

Parameters:
  • filename (string) – The file where this data will be written.
  • Headers (list) – A list of Header strings or if the file being written is of type trajectory, this is a List of strings that contain header attributes and their associated values which are seperated by a ‘=’.
  • dcols (list) – A list of data attributes.
  • data (list) – A list of lists (or of numpy arrays).
  • headerlines (list, optional) – Additional list of strings of header data, only used in trajectory data Types. The default is [].
  • header_char (character, optional) – The character that indicates a header lines. The default is ‘H’.
  • sldir (string, optional) – Where this fill will be written. The default is ‘.’.
  • sep (string, optional) – What seperates the data column attributes. The default is ‘ ‘.
  • trajectory (boolean, optional) – Boolean of if we are writeing a trajectory type file. The default is False.
  • download (boolean, optional) – If using iPython notebook, do you want a download link for the file you write? The default is False.
nugridpy.ascii_table.writeGCE_table(filename, headers, data, dcols=['Isotopes', 'Yields', 'Z', 'A'], header_char='H', sldir='.', sep='&')

Method for writeing data in GCE format in Ascii files. Reads either elements or isotopes dcols[0] needs to contain either isotopes or elements

Note the attribute name at position i in dcols will be associated with the column data at index i in data. Also the number of data columns(in data) must equal the number of data attributes (in dcols) Also all the lengths of that columns must all be the same. Input: filename: The file where this data will be written. Headers: A list of Header strings or if the file being written

is of type trajectory, this is a List of strings that contain header attributes and their associated values which are seperated by a ‘=’.

dcols: A list of data attributes data: A list of lists (or of numpy arrays). header_char the character that indicates a header lines sldir: Where this fill will be written. sep: What seperatesa the data column attributes trajectory: Boolean of if we are writeing a trajectory type file

nugridpy.ascii_table.writeTraj(filename='trajectory.input', data=[], ageunit=0, tunit=0, rhounit=0, idNum=0)

Method for writeing Trajectory type ascii files files.

Parameters:
  • filename (string) – The file where this data will be written.
  • data (list) – A list of 1D data vectors with time, T and rho.
  • ageunit (integer, optional) – If 1 ageunit = SEC, If 0 ageunit = YRS. If 2 agunit = logtimerev in yrs. The default is 0. logtimerev is log of time until end
  • tunit (integer, optional) – If 1 TUNIT = T9K, if 0 TUNIT = T8K. The default is 0.
  • rhounit (integer, optional) – If 1 RHOUNIT = LOG, if 0 RHOUNIT = CGS. The default is 0.
  • idNum (optional) – An optional id argument