File: TauDEMAlgorithmProvider.py

package info (click to toggle)
qgis 2.18.28%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,007,948 kB
  • sloc: cpp: 671,774; python: 158,539; xml: 35,690; ansic: 8,346; sh: 1,766; perl: 1,669; sql: 999; yacc: 836; lex: 461; makefile: 292
file content (164 lines) | stat: -rw-r--r-- 7,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-

"""
***************************************************************************
    TauDEMAlgorithmProvider.py
    ---------------------
    Date                 : October 2012
    Copyright            : (C) 2012 by Alexander Bruy
    Email                : alexander dot bruy at gmail dot com
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""

__author__ = 'Alexander Bruy'
__date__ = 'October 2012'
__copyright__ = '(C) 2012, Alexander Bruy'

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

__revision__ = '$Format:%H$'

import os

from qgis.PyQt.QtGui import QIcon

from processing.core.AlgorithmProvider import AlgorithmProvider
from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.core.ProcessingLog import ProcessingLog

from .TauDEMAlgorithm import TauDEMAlgorithm
from .TauDEMMultifileAlgorithm import TauDEMMultifileAlgorithm
from .TauDEMUtils import TauDEMUtils

from .peukerdouglas import PeukerDouglas
from .slopearea import SlopeArea
from .lengtharea import LengthArea
from .dropanalysis import DropAnalysis
from .dinfdistdown import DinfDistDown
from .dinfdistup import DinfDistUp
from .gridnet import GridNet
from .dinftranslimaccum import DinfTransLimAccum
from .dinftranslimaccum2 import DinfTransLimAccum2

from .peukerdouglas_multi import PeukerDouglasMulti
from .slopearea_multi import SlopeAreaMulti
from .lengtharea_multi import LengthAreaMulti
from .dropanalysis_multi import DropAnalysisMulti
from .dinfdistdown_multi import DinfDistDownMulti
from .dinfdistup_multi import DinfDistUpMulti
from .gridnet_multi import GridNetMulti
from .dinftranslimaccum_multi import DinfTransLimAccumMulti
from .dinftranslimaccum2_multi import DinfTransLimAccum2Multi


class TauDEMAlgorithmProvider(AlgorithmProvider):

    def __init__(self):
        AlgorithmProvider.__init__(self)
        self.activate = False

    def getDescription(self):
        return self.tr('TauDEM (hydrologic analysis)')

    def getName(self):
        return 'taudem'

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

    def initializeSettings(self):
        AlgorithmProvider.initializeSettings(self)

        ProcessingConfig.addSetting(Setting(self.getDescription(),
                                            TauDEMUtils.TAUDEM_FOLDER,
                                            self.tr('TauDEM command line tools folder'),
                                            TauDEMUtils.taudemPath(), valuetype=Setting.FOLDER))
        ProcessingConfig.addSetting(Setting(self.getDescription(),
                                            TauDEMUtils.TAUDEM_MULTIFILE_FOLDER,
                                            self.tr('TauDEM multifile command line tools folder'),
                                            TauDEMUtils.taudemMultifilePath(), valuetype=Setting.FOLDER))
        ProcessingConfig.addSetting(Setting(self.getDescription(),
                                            TauDEMUtils.TAUDEM_USE_SINGLEFILE,
                                            self.tr('Enable singlefile TauDEM tools'), True))
        ProcessingConfig.addSetting(Setting(self.getDescription(),
                                            TauDEMUtils.TAUDEM_USE_MULTIFILE,
                                            self.tr('Enable multifile TauDEM tools'), False))
        ProcessingConfig.addSetting(Setting(self.getDescription(),
                                            TauDEMUtils.MPIEXEC_FOLDER,
                                            self.tr('MPICH2/OpenMPI bin directory'),
                                            TauDEMUtils.mpiexecPath(), valuetype=Setting.FOLDER))
        ProcessingConfig.addSetting(Setting(self.getDescription(),
                                            TauDEMUtils.MPI_PROCESSES,
                                            self.tr('Number of MPI parallel processes to use'), 2))

    def unload(self):
        AlgorithmProvider.unload(self)

        ProcessingConfig.removeSetting(TauDEMUtils.TAUDEM_FOLDER)
        ProcessingConfig.removeSetting(TauDEMUtils.TAUDEM_MULTIFILE_FOLDER)
        ProcessingConfig.removeSetting(TauDEMUtils.TAUDEM_USE_SINGLEFILE)
        ProcessingConfig.removeSetting(TauDEMUtils.TAUDEM_USE_MULTIFILE)
        ProcessingConfig.removeSetting(TauDEMUtils.MPIEXEC_FOLDER)
        ProcessingConfig.removeSetting(TauDEMUtils.MPI_PROCESSES)

    def _loadAlgorithms(self):
        self.algs = []
        basePath = TauDEMUtils.taudemDescriptionPath()

        if ProcessingConfig.getSetting(TauDEMUtils.TAUDEM_USE_SINGLEFILE):
            folder = os.path.join(basePath, 'single')

            for descriptionFile in os.listdir(folder):
                if descriptionFile.endswith('txt'):
                    descriptionFile = os.path.join(folder, descriptionFile)
                    self._algFromDescription(descriptionFile)

            self.algs.append(PeukerDouglas())
            self.algs.append(SlopeArea())
            self.algs.append(LengthArea())
            self.algs.append(DropAnalysis())
            self.algs.append(DinfDistDown())
            self.algs.append(DinfDistUp())
            self.algs.append(GridNet())
            self.algs.append(DinfTransLimAccum())
            self.algs.append(DinfTransLimAccum2())

        if ProcessingConfig.getSetting(TauDEMUtils.TAUDEM_USE_MULTIFILE):
            folder = os.path.join(basePath, 'multi')

            for descriptionFile in os.listdir(folder):
                if descriptionFile.endswith('txt'):
                    descriptionFile = os.path.join(folder, descriptionFile)
                    self._algFromDescription(descriptionFile, True)

            self.algs.append(PeukerDouglasMulti())
            self.algs.append(SlopeAreaMulti())
            self.algs.append(LengthAreaMulti())
            self.algs.append(DropAnalysisMulti())
            self.algs.append(DinfDistDownMulti())
            self.algs.append(DinfDistUpMulti())
            self.algs.append(GridNetMulti())
            self.algs.append(DinfTransLimAccumMulti())
            self.algs.append(DinfTransLimAccum2Multi())

    def _algFromDescription(self, descriptionFile, multifile=False):
        try:
            if multifile:
                alg = TauDEMMultifileAlgorithm(descriptionFile)
            else:
                alg = TauDEMAlgorithm(descriptionFile)
            if alg.name.strip() != '':
                self.algs.append(alg)
            else:
                ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
                                       self.tr('Could not open TauDEM algorithm: %s' % descriptionFile))
        except Exception as e:
            ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
                                   self.tr('Could not open TauDEM algorithm %s:\n%s' % (descriptionFile, unicode(e))))