Beispiel #1
0
    def test_tomso_open(self):
        with common.tomso_open('data/test.txt', 'rb') as f:
            lines = f.read().decode('utf-8').split('\n')

        with common.tomso_open('data/test.txt.gz', 'rb') as f:
            lines_gz = f.read().decode('utf-8').split('\n')

        for line, line_gz in zip(lines, lines_gz):
            self.assertEqual(line, line_gz)
Beispiel #2
0
def load_astero_results(filename):
    """Reads a set of MESA results from one of the optimization routines
    in the `astero` module.

    Parameters
    ----------
    filename: str
        Filename of the file containing the results.

    Returns
    -------
    data: structured array
        Array with all the results.
    """
    with tomso_open(filename, 'rb') as f:
        lines = [line.replace(b'D', b'E') for line in f.readlines()]

    # the last column results for `search_type = simplex` fits have a
    # nameless column that says what kind of simplex step was taken.
    # we have to give it a name ourselves
    names = [name.decode('utf-8') for name in lines[1].split()]
    N_columns = len(lines[2].split())
    if len(names) == N_columns - 1:
        names.append('step_type')

    data = np.genfromtxt(lines[2:-4], dtype=None, names=names)

    return data
Beispiel #3
0
def load_profile(filename):
    """Reads a MESA profile and returns the global data and profile
    data in two structured arrays.  Uses builtin `gzip` module to read
    files ending with `.gz`.

    Parameters
    ----------
    filename: str
        Filename of the MESA profile to load.

    Returns
    -------
    header: structured array
        Global data for the stellar model. e.g. total mass, luminosity.
        The keys for the array are the MESA variable names as in
        `profile.columns`.

    data: structured array
        Profile data for the stellar model. e.g. radius, pressure.
        The keys for the array are the MESA variable names as in
        `profile.columns`.
    """

    with tomso_open(filename, 'rb') as f:
        lines = f.readlines()

    header = np.genfromtxt(lines[1:3], names=True)
    data = np.genfromtxt(lines[5:], names=True)

    return header, data
Beispiel #4
0
def load_history(filename):
    """Reads a MESA history file and returns the global data and history
    data in two structured arrays.  Uses builtin `gzip` module to read
    files ending with `.gz`.

    Parameters
    ----------
    filename: str
        Filename of the MESA history file to load.

    Returns
    -------
    header: structured array
        Global data for the evolutionary run. e.g. initial parameters.
        The keys for the array are the MESA variable names as in
        `history.columns`.

    data: structured array
        History data for the run. e.g. age, effective temperature.
        The keys for the array are the MESA variable names as in
        `history.columns`.
    """

    with tomso_open(filename, 'rb') as f:
        lines = f.readlines()

    header = np.genfromtxt(lines[1:3], names=True)
    data = np.genfromtxt(lines[5:], names=True)

    return header, data
Beispiel #5
0
def load_mode(filename):
    """Reads a GYRE mode file and returns the global data and mode profile
    data in two structured arrays.  Uses builtin `gzip` module to read
    files ending with `.gz`.

    Parameters
    ----------
    filename: str
        Filename of the GYRE mode file to load.

    Returns
    -------
    header: structured array
        Global data for the frequency calculation. e.g. initial parameters.
        The keys for the array are the GYRE variable names as in
        the ``&output`` namelist in the GYRE input file.

    data: structured array
        Mode data for the frequency calculation. e.g. mode frequencies.
        The keys for the array are the GYRE variable names as in
        the ``&output`` namelist in the GYRE input file.

    """
    with tomso_open(filename, 'rb') as f:
        lines = f.readlines()

    # catch case of no global data
    if lines[1] == '\n':
        header = []
    else:
        header = np.genfromtxt(lines[2:4], names=True)

    data = np.genfromtxt(lines[5:], names=True)

    return header, data
Beispiel #6
0
def load_sample(filename):
    """Reads a MESA sample file that describes a model from one of the
    optimization routines in the `astero` module.

    Parameters
    ----------
    filename: str
        Filename of the file containing the result.

    Returns
    -------
    d: dict
        A dictionary containing all the results.

    """
    with tomso_open(filename, 'rb') as f:
        # lines = [line.split() for line in f.read().decode('utf-8').split('\n')
        #          if line.strip()]
        lines = [
            line.decode('utf-8').split() for line in f.readlines()
            if line.strip()
        ]

    d = {}
    ell = 0
    for line in lines:
        if line[0][:2] == 'l=':
            ell = int(line[0][-1])
            d['l%i' % ell] = {
                'n': [],
                'chi2': [],
                'mdl': [],
                'cor': [],
                'obs': [],
                'err': [],
                'logE': []
            }
        elif len(line) == 7:
            d['l%i' % ell]['n'].append(int(line[0]))
            d['l%i' % ell]['chi2'].append(float(line[1]))
            d['l%i' % ell]['mdl'].append(float(line[2]))
            d['l%i' % ell]['cor'].append(float(line[3]))
            d['l%i' % ell]['obs'].append(float(line[4]))
            d['l%i' % ell]['err'].append(float(line[5]))
            d['l%i' % ell]['logE'].append(float(line[6]))
        else:
            key = ''.join([word + ' ' for word in line[:-1]])[:-1]
            # if key == 'mass/Msun':
            #     key = 'initial mass'

            value = float(line[-1].replace('D', 'e'))
            d[key] = value

    return d
Beispiel #7
0
def load_gyre(filename):
    """Reads a GYRE stellar model file and returns the global data and
    point-wise data in a pair of NumPy record arrays.  Uses builtin
    `gzip` module to read files ending with `.gz`.

    Parameters
    ----------
    filename: str
        Filename of the GYRE file.

    Returns
    -------
    header: structured array
        Global data for the stellar model. e.g. total mass, luminosity.

    data: structured array
        Profile data for the stellar model. e.g. radius, pressure.

    """
    def replace(s):
        # handles annoying Fortran formatting
        t = s[:]
        t = t.replace(b'D', b'E')
        t = t.replace(b'+', b'E+')
        t = t.replace(b'-', b'E-')
        t = t.replace(b'EE', b'E')
        t = t.replace(b' E-', b' -')
        return t

    with tomso_open(filename, 'rb') as f:
        lines = [replace(line) for line in f.readlines()]

    header_length = len(lines[0].split())
    if header_length == 4:
        version = 1
    elif header_length == 5:
        version = int(lines[0].split()[-1])
    else:
        raise ValueError("header should have 4 or 5 components but "
                         "it appears to have %i" % header_length)

    header = np.loadtxt(lines[:1], dtype=gyre_header_dtypes[version])
    data = np.loadtxt(lines[1:], dtype=gyre_data_dtypes[version])

    return header, data
Beispiel #8
0
def load_summary(filename):
    """Reads a GYRE summary file and returns the global data and mode data
    in two structured arrays.  Uses builtin `gzip` module to read
    files ending with `.gz`.

    Parameters
    ----------
    filename: str
        Filename of the GYRE summary file to load.

    Returns
    -------
    header: structured array
        Global data for the frequency calculation. e.g. initial parameters.
        The keys for the array are the GYRE variable names as in
        the ``&output`` namelist in the GYRE input file.

    data: structured array
        Mode data for the frequency calculation. e.g. mode frequencies.
        The keys for the array are the GYRE variable names as in
        the ``&output`` namelist in the GYRE input file.

    """

    with tomso_open(filename, 'rb') as f:
        lines = f.readlines()

    # catch case of no global data
    # just try to load header and give up on failure
    try:
        header = np.genfromtxt(lines[2:4], names=True)
    except IndexError:
        header = None

    data = np.genfromtxt(lines[5:], names=True)

    return header, data