TauDEMAlgorithm.py

Cecile Kittel, 2017-08-07 01:49 PM

Download (4.92 KB)

 
1
# -*- coding: utf-8 -*-
2

    
3
"""
4
***************************************************************************
5
    TauDEMAlgorithm.py
6
    ---------------------
7
    Date                 : October 2012
8
    Copyright            : (C) 2012 by Alexander Bruy
9
    Email                : alexander dot bruy at gmail dot com
10
***************************************************************************
11
*                                                                         *
12
*   This program is free software; you can redistribute it and/or modify  *
13
*   it under the terms of the GNU General Public License as published by  *
14
*   the Free Software Foundation; either version 2 of the License, or     *
15
*   (at your option) any later version.                                   *
16
*                                                                         *
17
***************************************************************************
18
"""
19

    
20
__author__ = 'Alexander Bruy'
21
__date__ = 'October 2012'
22
__copyright__ = '(C) 2012, Alexander Bruy'
23

    
24
# This will get replaced with a git SHA1 when you do a git archive
25

    
26
__revision__ = '$Format:%H$'
27

    
28
import os
29
from qgis.PyQt.QtCore import QCoreApplication
30
from qgis.PyQt.QtGui import QIcon
31

    
32
from processing.core.GeoAlgorithm import GeoAlgorithm
33
from processing.core.ProcessingLog import ProcessingLog
34
from processing.core.ProcessingConfig import ProcessingConfig
35
from processing.core.GeoAlgorithmExecutionException import \
36
    GeoAlgorithmExecutionException
37

    
38
from processing.core.parameters import ParameterRaster
39
from processing.core.parameters import ParameterVector
40
from processing.core.parameters import ParameterBoolean
41
from processing.core.parameters import ParameterString
42
from processing.core.parameters import ParameterNumber
43
from processing.core.parameters import getParameterFromString
44
from processing.core.outputs import getOutputFromString
45

    
46
from .TauDEMUtils import TauDEMUtils
47

    
48

    
49
class TauDEMAlgorithm(GeoAlgorithm):
50

    
51
    def __init__(self, descriptionfile):
52
        GeoAlgorithm.__init__(self)
53
        self.descriptionFile = descriptionfile
54
        self.defineCharacteristicsFromFile()
55

    
56
    def getCopy(self):
57
        newone = TauDEMAlgorithm(self.descriptionFile)
58
        newone.provider = self.provider
59
        return newone
60

    
61
    def getIcon(self):
62
        return QIcon(os.path.dirname(__file__) + '/../../images/taudem.svg')
63

    
64
    def defineCharacteristicsFromFile(self):
65
        lines = open(self.descriptionFile)
66
        line = lines.readline().strip('\n').strip()
67
        self.name = line
68
        self.i18n_name = QCoreApplication.translate("TAUDEMAlgorithm", line)
69
        line = lines.readline().strip('\n').strip()
70
        self.cmdName = line
71
        line = lines.readline().strip('\n').strip()
72
        self.group = line
73
        self.i18n_group = QCoreApplication.translate("TAUDEMAlgorithm", line)
74

    
75
        line = lines.readline().strip('\n').strip()
76
        while line != '':
77
            try:
78
                line = line.strip('\n').strip()
79
                if line.startswith('Parameter'):
80
                    param = getParameterFromString(line)
81
                    self.addParameter(param)
82
                else:
83
                    self.addOutput(getOutputFromString(line))
84
                line = lines.readline().strip('\n').strip()
85
            except Exception as e:
86
                ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
87
                                       self.tr('Could not load TauDEM algorithm: %s\n%s' % (self.descriptionFile, line)))
88
                raise e
89
        lines.close()
90

    
91
    def processAlgorithm(self, progress):
92
        commands = []
93
        commands.append(os.path.join(TauDEMUtils.mpiexecPath(), 'mpiexec'))
94

    
95
        processNum = int(ProcessingConfig.getSetting(TauDEMUtils.MPI_PROCESSES))
96
        if processNum <= 0:
97
            raise GeoAlgorithmExecutionException(
98
                self.tr('Wrong number of MPI processes used. Please set '
99
                        'correct number before running TauDEM algorithms.'))
100

    
101
        commands.append('-n')
102
        commands.append(unicode(processNum))
103
        commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName))
104

    
105
        for param in self.parameters:
106
            if param.value is None or param.value == '':
107
                continue
108
            if isinstance(param, ParameterNumber):
109
                commands.append(param.name)
110
                commands.append(unicode(param.value))
111
            if isinstance(param, (ParameterRaster, ParameterVector)):
112
                commands.append(param.name)
113
                commands.append(param.value)
114
            elif isinstance(param, ParameterBoolean):
115
                if param.value:
116
                    commands.append(param.name)
117
            elif isinstance(param, ParameterString):
118
                commands.append(param.name)
119
                commands.append(unicode(param.value))
120

    
121
        for out in self.outputs:
122
            commands.append(out.name)
123
            commands.append(out.value)
124

    
125
        TauDEMUtils.executeTauDEM(commands, progress)