cdftools.py 2.2 KB
Newer Older
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
from earthdiagnostics.utils import Utils
from autosubmit.config.log import Log
class CDFTools(object):
    """
    Class to run CDFTools executables
    """
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
    # noinspection PyShadowingBuiltins
    def run(self, command, input, output=None, options=None, log_level=Log.INFO):
        """
        Runs one of the CDFTools

        :param command: executable to run
        :type command: str
        :param input: input file
        :type input: str
        :param output: output file. Not all tools support this parameter
        :type options: str
        :param options: options for the tool.
        :type options: str | list
        :param log_level: log level at which the output of the cdftool command will be added
        :type log_level: int
        line = [os.path.join(self.path, command)]
        if not os.path.exists(line[0]):
            raise Exception('Error executing {0}\n Command does not exist in {1}', command, self.path)

Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            if isinstance(input, basestring):
                line.append(input)
                if not os.path.exists(input):
                    raise Exception('Error executing {0}\n Input file {1} file does not exist', command, input)
Javier Vegas-Regidor's avatar
Javier Vegas-Regidor committed
            else:
                for element in input:
                    line.append(element)
                    if not os.path.exists(element):
                        raise Exception('Error executing {0}\n Input file {1} file does not exist', command, element)
            if isinstance(options, basestring):
                options = options.split()
                line.append(str(option))
            if input == output:
                raise Exception('Input and output file can not be the same on CDFTools')
        Log.debug('Executing {0}', ' '.join(line))
        shell_output = Utils.execute_shell_command(line, log_level)

        if output:
            if not os.path.exists(output):
                raise Exception('Error executing {0}\n Output file not created', ' '.join(line))
        return shell_output