MesaData

MesaData is a class that encodes the data in a single history or profile file. It is instantiated by providing a path to the file to be accessed or indirectly via MesaLogDir. Whenever possible, columns from the header or body of these files are directly accessible as attributes and always via the data or header methods.

class mesa_reader.MesaData(file_name='./LOGS/history.data', file_type=None)[source]

Structure containing data from a Mesa output file.

Reads a profile or history output file from mesa. Assumes a file with the following structure:

  • line 1: header names
  • line 2: header data
  • line 3: blank
  • line 4: main data names
  • line 5: main data values

This structure can be altered by using the class methods MesaData.set_header_rows and MesaData.set_data_rows.

Parameters:file_name (str, optional) – File name to be read in. Default is ‘LOGS/history.data’, which works for scripts in a standard work directory with a standard logs directory for accessing the history data.
file_name

str – Path to file from which the data is read.

bulk_data

numpy.ndarray – The main data (line 6 and below) in record array format. Primarily accessed via the data method.

bulk_names

tuple of str – Tuple of all available data column names that are valid inputs for data. Essentially the column names in line 4 of file_name.

header_data

dict – Header data (line 2 of file_name) in dict format

header_names

list of str – List of all available header column names that are valid inputs for header. Essentially the column names in line 1 of file_name.

data(key)[source]

Accesses the data and returns a numpy array with the appropriate data

Accepts a string key, like star_age (for history files) or logRho (for profile files) and returns the corresponding numpy array of data for that data type. Can also just use the shorthand methods that have the same name of the key.

Parameters:key (str) – Name of data. Must match a main data title in the source file. If it is not a data title, will first check for a log_[key] or ln[key] version and return an exponentiated version of that data. If key looks like a log_* or ln_* name, searches for a linear quantity of the appropriate name and returns the expected logarithmic quantity.
Returns:Array of values for data corresponding to key at various time steps (history) or grid points (profile or model).
Return type:numpy.ndarray
Raises:KeyError – If key is an invalid key (i.e. not in self.bulk_names and no fallback logarithmic or linear quantities found)

Examples

You can either call data explicitly with key as an argument, or get the same result by calling it implicitly by treating key as an attribute.

>>> m = MesaData()
>>> x = m.data('star_age')
>>> y = m.star_age
>>> x == y
True

In this case, x and y are the same because the non-existent method MesaData.star_age will direct to to the corresponding MesaData.data call.

Even data categories that are not in the file may still work. Specifically, if a linear quantity is available, but the log is asked for, the linear quantity will be first log-ified and then returned:

>>> m = MesaData()
>>> m.in_data('L')
False
>>> m.in_data('log_L')
True
>>> x = m.L
>>> y = 10**m.log_L
>>> x == y
True

Here, data was called implicitly with an argument of ‘L’ to get x. Since ‘L’ was an invalid data category, it first looked to see if a logarithmic version existed. Indeed, ‘log_L’ was present, so it was retrieved, exponentiated, and returned.

data_at_model_number(key, m_num)[source]

Return main data at a specific model number (for history files).

Finds the index i where MesaData.data(‘model_number’)[i] == m_num. Then returns MesaData.data(key)[i]. Essentially lets you use model numbers to index data.

Parameters:
  • key (str) – Name of data. Must match a main data title in the source file.
  • m_num (int) – Model number where you want to sample the data
Returns:

Value of MesaData.data(key) at the same index where MesaData.data(‘model_number’) == m_num

Return type:

float or int

See also

index_of_model_number()
returns the index for sampling, not the value
header(key)[source]

Accesses the header, returning a scalar the appropriate data

Accepts a string key, like version_number and returns the corresponding datum for that key. Can also just use the shorthand methods that have the same name of the key.

Parameters:key (string) – Name of data. Must match a main data title in the source file.
Returns:Returns whatever value is below the corresponding key in the header lines of the source file.
Return type:int or str or float
Raises:KeyError – If key is an invalid key (i.e. not in self.header_names)

Examples

Can call header explicitly with key as argument or implicitly, treating key as an attribute.

In this case, x and y are the same because the non-existent method MesaData.version_number will first see if it can call MesaData.data(‘version_number’). Then, realizing that this doesn’t make sense, it will instead call MesaData.header(‘version_number’)

>>> m = MesaData()
>>> x = m.data('version_number')
>>> y = m.version_number
>>> x == y
True
in_data(key)[source]

Determine if key is an available main data category.

Checks if string key is a valid argument of MesaData.data. Returns True if it is, otherwise False

Parameters:key (str) – Candidate string for accessing main data. This is what you want to be able to use as an argument of MesaData.data.
Returns:True if key is a valid input to MesaData.data, otherwise False.
Return type:bool

Notes

This is automatically called by MesaData.data, so the average user shouldn’t need to call it.

in_header(key)[source]

Determine if key is an available header data category.

Checks if string key is a valid argument of MesaData.header. Returns True if it is, otherwise False

Parameters:key (str) – Candidate string for accessing header data. This is what you want to be able to use as an argument of MesaData.header.
Returns:True if key is a valid input to MesaData.header, otherwise False.
Return type:bool

Notes

This is automatically called by MesaData.header, so the average user shouldn’t need to call it.

index_of_model_number(m_num)[source]

Return index where MesaData.data(‘model_number’) is m_num.

Returns the index i where MesaData.data(‘model_number’)[i] == m_num.

Parameters:

m_num (int) – Model number where you want to sample data

Returns:

The index where MesaData.data(‘model_number’) == m_num

Return type:

int

Raises:
  • HistoryError – If trying to access a non-history file
  • ModelNumberError – If zero or more than one model numbers matching m_num are found.

See also

data_at_model_number()
returns the datum of a specific key a model no.
is_history()[source]

Determine if the source file is a history file

Checks if ‘model_number’ is a valid key for self.data. If it is, return True. Otherwise return False. This is used in determining whether or not to cleanse the file of backups and restarts in the MesaData.read_data.

Returns:True if file is a history file, otherwise False
Return type:bool
read_data()[source]

Decide if data file is log output or a model, then load the data

Log files and models are structured differently, so different methods will be required to read in each. This method figures out which one should be called and then punts to that method.

Parameters:None
Returns:
Return type:None
read_log_data()[source]

Reads in or update data from the original log (.data or .log) file.

This re-reads the data from the originally-provided file name. Mostly useful if the data file has been changed since it was first read in or if the class methods MesaData.set_header_rows or MesaData.set_data_rows have been used to alter how the data have been read in.

Returns:
Return type:None
read_model_data()[source]

Read in or update data from the original model (.mod) file.

Models are assumed to have the following structure:

  • lines of comments and otherwise [considered] useless information
  • one or more blank line
  • Header information (names and values separated by one or more space, one per line)
  • one or more blank lines
  • ONE line of column names (strings separated by one or more spaces)
  • many lines of bulk data (integer followed by many doubles, separated by one or more spaces)
  • a blank line
  • everything else is ignored
Parameters:None
Returns:
Return type:None
remove_backups(dbg=False)[source]

Cleanses a history file of backups and restarts

If the file is a history file, goes through and ensure that the model_number data are monotonically increasing. It removes rows of data from all categories if there are earlier ones later in the file.

Parameters:dbg (bool, optional) – If True, will output how many lines are cleansed. Default is False
Returns:
Return type:None