示例#1
0
def run_arts(controlfile=None,
             arts=None,
             writetxt=False,
             ignore_error=False,
             **kwargs):
    """Start an ARTS Simulation.

    Parameters:
        controlfile (str): Path to the ARTS controlfile.
        arts (str): Path to the arts executable.
        writetxt (bool): Write stdout and stderr to ASCII files.
        ignore_error (bool): If set to True, erros during the ARTS run do not
            result in an exception (default is False).
        **kwargs: Additional command line arguments passed as keyword.
            See `arts --help` for more details.

    Returns:
        Named tuple containing the fields stdout, stderr and retcode.

    Examples:
        Run a simple ARTS job and set the output directory
        and the report level:

        >>> run_arts('foo.arts', outdir='bar', reporting='020')

        If a keyword is set to True it is added as flag.
        Show the ARTS help message:

        >>> run_arts(help=True)

    """
    # If path to the ARTS exectuable is not passed explicitly, construct it
    # from the ARTS_BUILD_PATH. Its actual existence is checked later.
    if arts is None and environ.get('ARTS_BUILD_PATH') is not None:
        arts = os.path.join(environ['ARTS_BUILD_PATH'], 'src', 'arts')
    # Try 'arts' as a fallback, maybe it is in the user's PATH.
    elif arts is None:
        arts = 'arts'

    # Append ARTS_INCLUDE_PATH and ARTS_DATA_PATH to the user's environment.
    if environ.get('ARTS_INCLUDE_PATH') is not None:
        path_append(environ.get('ARTS_INCLUDE_PATH'), path='ARTS_INCLUDE_PATH')

    if environ.get('ARTS_DATA_PATH') is not None:
        path_append(environ.get('ARTS_DATA_PATH'), path='ARTS_DATA_PATH')

    if not shutil.which(arts):
        raise Exception('ARTS executable not found at: {}'.format(arts))

    if controlfile is None:
        controlfile = ''
    elif not os.path.exists(controlfile):
        err_msg = 'Controlfile not found at: {}'.format(controlfile)
        raise FileNotFoundError(err_msg)

    opts = []
    for kw, arg in kwargs.items():
        if isinstance(arg, bool) and arg is True:
            if len(kw) == 1:
                opts.append('-{}'.format(kw))
            else:
                opts.append('--{}'.format(kw))
        elif len(kw) == 1:
            opts.append('-{}{}'.format(kw, arg))
        else:
            opts.append('--{}={}'.format(kw, arg))

    # Run ARTS job and redirect output.
    p = subprocess.run([arts, *opts, controlfile],
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE,
                       universal_newlines=True)

    # Write ARTS output and error to ASCII file.
    if writetxt:
        if controlfile.endswith('.arts'):
            outfile = controlfile.replace('.arts', '.out')
            errfile = controlfile.replace('.arts', '.err')
        else:
            outfile = 'arts.out'
            errfile = 'arts.err'

        for key in ['outdir', 'o']:
            if key in kwargs:
                outfile = os.path.join(kwargs[key], outfile)
                errfile = os.path.join(kwargs[key], errfile)

        with open(outfile, 'w') as out, open(errfile, 'w') as err:
            out.write(p.stdout)
            err.write(p.stderr)

    # Throw exception if ARTS run failed.
    if p.returncode != 0 and ignore_error is not True:
        raise Exception('ARTS run failed:\n{}'.format(p.stderr))

    # Store ARTS output in namedtuple.
    arts_out = collections.namedtuple('ARTS_output',
                                      ['stdout', 'stderr', 'retcode'])

    return arts_out(stdout=p.stdout, stderr=p.stderr, retcode=p.returncode)
示例#2
0
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
modindex_common_prefix = ['windtunnel.']

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True

# Exclude ARTS Workspace documentation when `ARTS_BUILD_PATH` is not set.
# Else, the build fails because typhon.arts.workspace can not be imported.
if environ.get('ARTS_BUILD_PATH') is None:
    exclude_patterns.append('typhon.arts.workspace.rst')

# Allows to automatically number figures, tables and code-blocks:
numfig = True

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#html_theme = 'alabaster'
html_theme = 'sphinx_rtd_theme'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
示例#3
0
logger = logging.getLogger(__name__)

################################################################################
# Version Requirements
################################################################################

arts_minimum_major = 2
arts_minimum_minor = 3
arts_minimum_revision = 1167

################################################################################
# Load ARTS C API
################################################################################

if environ.get("ARTS_BUILD_PATH") is None:
    raise EnvironmentError("ARTS_BUILD_PATH environment variable required to" +
                           " locate ARTS API.")

try:
    lib_path = os.path.join(environ.get("ARTS_BUILD_PATH"), "src",
                            "libarts_api.so")
    logger.info("Loading ARTS API from: " + lib_path)
    arts_api = c.cdll.LoadLibrary(lib_path)
except:
    raise EnvironmentError("Could not find ARTS API in your ARTS build path. "
                           "Did you install it?" + os.linesep +
                           "Typhon requires at least ARTS version "
                           f"{arts_minimum_major}.{arts_minimum_minor}."
                           f"{arts_minimum_revision}")