appspice.localize_mk

Localize the MK (meta-kernel) file.

Background: The MK file, ending with ‘.tm’, contains the actual kernels to be loaded for a specific project. It frequently contains a “hard-coded” path, “PATH_VALUES”, that points to the parent path (‘..’), while this refers to the parent path of the current directory. Therefore, the PATH_VALUES should be modified manually to load the actual kernels to load.

There are several work-arounds to handle the MK file. In this module, a given MK file is loaded, change PATH_VALUES to the specific path, and write to the stdout or the given file.

The simplest way to localize the mkfile, output to stdout, is as follows:

% irfpy-spice-localize-mk    /path/to/spice/kernels/mk/bc_ops.tm

… PATH_VALUES = ( ‘path/to/spice/kernels’ ) …

“-o” option will provide opportunity to specify the output file (instead of stdout).

% irfpy-spice-localize-mk  -o ./bc_ops_local.tm   /path/to/spice/kernels/mk/bc_ops.tm

The actual PATH_VALUES value is obtained from the localtion of the MK file: For the above examples, the value becomes /path/to/spice/kernels. You can specify the PATH_VALUES value of your preference with “-k”

% irfpy-spice-localize-mk  -k /path/to/spice/kernels/   original_bc_plan.tm
...
PATH_VALUES     = ( 'path/to/spice/kernels'  )
...
""" Localize the MK (meta-kernel) file.

Background: The MK file, ending with '.tm', contains the actual kernels
to be loaded for a specific project.
It frequently contains a "hard-coded" path, "PATH_VALUES", that points to
the parent path ('..'), while this refers to the parent path of the current directory.
Therefore, the PATH_VALUES should be modified manually to load the actual kernels to load.

There are several work-arounds to handle the MK file.
In this module, a given MK file is loaded, change PATH_VALUES to the specific path, and
write to the stdout or the given file.

The simplest way to localize the mkfile, output to stdout, is as follows:

    ::

    % irfpy-spice-localize-mk    /path/to/spice/kernels/mk/bc_ops.tm
    ...
    PATH_VALUES     = ( 'path/to/spice/kernels'  )
    ...

"-o" option will provide opportunity to specify the output file (instead of stdout).

::

    % irfpy-spice-localize-mk  -o ./bc_ops_local.tm   /path/to/spice/kernels/mk/bc_ops.tm

The actual ``PATH_VALUES`` value is obtained from the localtion of the MK file: For the above examples,
the value becomes ``/path/to/spice/kernels``.
You can specify the ``PATH_VALUES`` value of your preference with "-k"

::

    % irfpy-spice-localize-mk  -k /path/to/spice/kernels/   original_bc_plan.tm
    ...
    PATH_VALUES     = ( 'path/to/spice/kernels'  )
    ...
"""

import os
import sys
import argparse
import logging
import datetime
import re

from irfpy.spice import __version__

_logger = logging.getLogger(__name__)
_logger_irfpy = logging.getLogger('irfpy.spice')

from irfpy.spice.localize import localize_mkfile

def main():
    parser = argparse.ArgumentParser(description="Loalize mk file")

    parser.add_argument('metakernel', help='Original .tm file name')
    parser.add_argument('-l', '--local', help="Create the file in the same directory as the metakernel, with _local added. (The ESA way). If given, -o is ignored.", default=False, action='store_true')
    parser.add_argument('--output', '-o', help='Output .tm file name. Default: stdout')
    parser.add_argument('--kernel-path', '-k', help='Path that contain the kernel. Default: Parent path of the original mk file.')
    parser.add_argument('--verbose', '-v', help='Verbose mode', default=False, action='store_true')

    params = parser.parse_args()

    logging.basicConfig()

    if params.verbose:
        _logger.setLevel('DEBUG')
        _logger_irfpy.setLevel('DEBUG')
        _logger.debug('Debug mode.')
    else:
        _logger.setLevel('WARNING')
        _logger_irfpy.setLevel('WARNING')

    _logger.debug(params)

    localize_mkfile(params.metakernel, local_dir=params.local, output=params.output, kernel_path=params.kernel_path)


if __name__ == "__main__":
    logging.basicConfig()
    main()