forked from mlt/sextante-taudem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TauDEMAlgorithm.py
134 lines (114 loc) · 5.29 KB
/
TauDEMAlgorithm.py
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
# -*- coding: utf-8 -*-
#******************************************************************************
#
# TauDEM SEXTANTE Provider
# ---------------------------------------------------------
# A suite of Digital Elevation Model (DEM) tools for the extraction and
# analysis of hydrologic information from topography as represented by
# a DEM of vector layer.
#
# Copyright (C) 2012 Alexander Bruy (alexander.bruy@gmail.com)
#
# This source 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.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/licenses/>. You can also obtain it by writing
# to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
# MA 02110-1335 USA.
#
#******************************************************************************
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.SextanteLog import SextanteLog
from sextante.core.SextanteUtils import SextanteUtils
from sextante.core.SextanteConfig import SextanteConfig
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.parameters.ParameterFactory import ParameterFactory
from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.parameters.ParameterString import ParameterString
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.outputs.OutputFactory import OutputFactory
from sextante.outputs.OutputRaster import OutputRaster
from sextante.outputs.OutputVector import OutputVector
from sextante.outputs.OutputFile import OutputFile
from sextante_taudem.TauDEMUtils import TauDEMUtils
class TauDEMAlgorithm(GeoAlgorithm):
def __init__(self, descriptionfile):
GeoAlgorithm.__init__(self)
self.descriptionFile = descriptionfile
self.defineCharacteristicsFromFile()
def getCopy(self):
newone = TauDEMAlgorithm(self.descriptionFile)
newone.provider = self.provider
return newone
def getIcon(self):
return QIcon(os.path.dirname(__file__) + "/icons/taudem.png")
def defineCharacteristicsFromFile(self):
lines = open(self.descriptionFile)
line = lines.readline().strip("\n").strip()
self.name = line
line = lines.readline().strip("\n").strip()
self.cmdName = line
line = lines.readline().strip("\n").strip()
self.group = line
while line != "":
try:
line = line.strip("\n").strip()
if line.startswith("Parameter"):
param = ParameterFactory.getFromString(line)
self.addParameter(param)
else:
self.addOutput(OutputFactory.getFromString(line))
line = lines.readline().strip("\n").strip()
except Exception, e:
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not load TauDEM algorithm: " + self.descriptionFile + "\n" + line)
raise e
lines.close()
def processAlgorithm(self, progress):
commands = []
commands.append(os.path.join(TauDEMUtils.mpiexecPath(), "mpiexec"))
processNum = SextanteConfig.getSetting(TauDEMUtils.MPI_PROCESSES)
if processNum <= 0:
raise GeoAlgorithmExecutionException("Wrong number of MPI processes used.\nPlease set correct number before running TauDEM algorithms.")
commands.append("-n")
commands.append(str(processNum))
commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName))
for param in self.parameters:
if param.value == None or param.value == "":
continue
if isinstance(param, ParameterNumber):
commands.append(param.name)
commands.append(str(param.value))
if isinstance(param, (ParameterRaster, ParameterVector)):
commands.append(param.name)
commands.append(param.value)
elif isinstance(param, ParameterBoolean):
if param.value and str(param.value).lower() == "false":
commands.append(param.name)
elif isinstance(param, ParameterString):
commands.append(param.name)
commands.append(str(param.value))
for out in self.outputs:
commands.append(out.name)
commands.append(out.value)
loglines = []
loglines.append("TauDEM execution command")
for line in commands:
loglines.append(line)
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
TauDEMUtils.executeTauDEM(commands, progress)
def helpFile(self):
return os.path.join(os.path.dirname(__file__), "help", self.cmdName + ".html")