cdftools.py 3.38 KB
Newer Older
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
# coding=utf-8
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
"""CDFTOOLS interface"""
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
import six
from bscearth.utils.log import Log

from earthdiagnostics.utils import Utils
class CDFTools(object):
    """
    Class to run CDFTools executables
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed

    :param path: path to CDFTOOLS binaries
    :type path: str
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    # noinspection PyShadowingBuiltins
    def run(self, command, input_file, output_file=None, options=None, log_level=Log.INFO, input_option=None):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        Run one of the CDFTools

        :param command: executable to run
        :type command: str | iterable
        :param input_file: input file
        :type input_file: str
        :param output_file: output file. Not all tools support this parameter
        :type options: str
        :param options: options for the tool.
        :type options: str | [str] | Tuple[str] | None
        :param log_level: log level at which the output of the cdftool command will be added
        :type log_level: int
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        :param input_option: option to add before input file
        :type input_option: str
        line = [os.path.join(self.path, command)]
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        self._check_command_existence(line[0])
        if input_option:
            line.append(input_option)
        self._check_input(command, input_file, line)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            if isinstance(options, six.string_types):
                options = options.split()
                line.append(str(option))
        if output_file:
            if input_file == output_file:
                raise ValueError('Input and output file can not be the same on CDFTools')
            line.append(output_file)
        Log.debug('Executing {0}', ' '.join(line))
        shell_output = Utils.execute_shell_command(line, log_level)
        self._check_output_was_created(line, output_file)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        return shell_output

    @staticmethod
    def _check_output_was_created(line, output):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            if not os.path.isfile(output):
                raise Exception('Error executing {0}\n Output file not created', ' '.join(line))
    # noinspection PyShadowingBuiltins
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    @staticmethod
    def _check_input(command, input_file, line):
        if input_file:
            if isinstance(input_file, six.string_types):
                line.append(input_file)
                if not os.path.isfile(input_file):
                    raise ValueError('Error executing {0}\n Input file {1} file does not exist', command, input_file)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            else:
                for element in input_file:
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
                    line.append(element)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
                    if not os.path.isfile(element):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
                        raise ValueError('Error executing {0}\n Input file {1} file does not exist', command, element)

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    @staticmethod
    def _is_exe(fpath):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    def _check_command_existence(self, command):
        if self.path:
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            if self._is_exe(os.path.join(self.path, command)):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
                return
        else:
            for path in os.environ["PATH"].split(os.pathsep):
                path = path.strip('"')
                exe_file = os.path.join(path, command)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
                if self._is_exe(exe_file):
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
                    return
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
        raise ValueError('Error executing {0}\n Command does not exist in {1}'.format(command, self.path))