This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
SwmmAlgorithm.py
423 lines (383 loc) · 20.6 KB
/
SwmmAlgorithm.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# -*- coding: utf-8 -*-
"""
***************************************************************************
SwmmAlgorithm.py
---------------------
Date : December 2013
Copyright : (C) 2013 by Vincent Mora
Email : vincent dot mora dot mtl 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__ = 'Vincent Mora'
__date__ = 'December 2013'
__copyright__ = '(C) 2013, Oslandia'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import re
import datetime
import codecs
import subprocess
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
from processing.core.ProcessingLog import ProcessingLog
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterFile
from processing.core.parameters import Parameter
from processing.core.outputs import OutputVector
from processing.core.outputs import OutputTable
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools import dataobjects
def convert_date(d):
month = {'JAN':'01',
'FEB':'02',
'MAR':'03',
'APR':'04',
'MAY':'05',
'JUN':'06',
'JUL':'07',
'AUG':'08',
'SEP':'09',
'OCT':'10',
'NOV':'11',
'DEC':'12'}
m = re.search('^(\S+)-(\d\d)-(\d\d\d\d)$',d) # for date and time saved as timestamps
if not m : raise RuntimeError
return m.group(3)+'-'+month[m.group(1)]+'-'+m.group(2)
class SwmmAlgorithm(GeoAlgorithm):
TITLE = 'TITLE'
OPTIONS = 'OPTIONS' # analysis options
REPORT = 'REPORT' # output reporting instructions
FILES = 'FILES' # interface file options
RAINGAGES = 'RAINGAGES' # rain gage information
HYDROGRAPHS = 'HYDROGRAPHS' # unit hydrograph data used to construct RDII inflows
EVAPORATION = 'EVAPORATION' # evaporation data
TEMPERATURE = 'TEMPERATURE' # air temperature and snow melt data
SUBCATCHMENTS = 'SUBCATCHMENTS' # basic subcatchment information
SUBAREAS = 'SUBAREAS' # subcatchment impervious/pervious sub-area data
INFILTRATION = 'INFILTRATION' # subcatchment infiltration parameters
LID_CONTROLS = 'LID_CONTROLS' # low impact development control information
LID_USAGE = 'LID_USAGE' # assignment of LID controls to subcatchments
AQUIFERS = 'AQUIFERS' # groundwater aquifer parameters
GROUNDWATER = 'GROUNDWATER' # subcatchment groundwater parameters
SNOWPACKS = 'SNOWPACKS' # subcatchment snow pack parameters
JUNCTIONS = 'JUNCTIONS' # junction node information
OUTFALLS = 'OUTFALLS' # outfall node information
DIVIDERS = 'DIVIDERS' # flow divider node information
STORAGE = 'STORAGE' # storage node information
CONDUITS = 'CONDUITS' # conduit link information
PUMPS = 'PUMPS' # pump link information
ORIFICES = 'ORIFICES' # orifice link information
WEIRS = 'WEIRS' # weir link information
OUTLETS = 'OUTLETS' # outlet link information
XSECTIONS = 'XSECTIONS' # conduit, orifice, and weir cross-section geometry
TRANSECTS = 'TRANSECTS' # transect geometry for conduits with irregular cross-sections
LOSSES = 'LOSSES' # conduit entrance/exit losses and flap valves
CONTROLS = 'CONTROLS' # rules that control pump and regulator operation
POLLUTANTS = 'POLLUTANTS' # pollutant information
LANDUSES = 'LANDUSES' # land use categories
COVERAGES = 'COVERAGES' # assignment of land uses to subcatchments
BUILDUP = 'BUILDUP' # buildup functions for pollutants and land uses
WASHOFF = 'WASHOFF' # washoff functions for pollutants and land uses
TREATMENT = 'TREATMENT' # pollutant removal functions at conveyance system nodes
INFLOWS = 'INFLOWS' # external hydrograph/pollutograph inflow at nodes
DWF = 'DWF' # baseline dry weather sanitary inflow at nodes
PATTERNS = 'PATTERNS' # periodic variation in dry weather inflow
RDII = 'RDII' # rainfall-dependent I/I information at nodes
LOADINGS = 'LOADINGS' # initial pollutant loads on subcatchments
CURVES = 'CURVES' # x-y tabular data referenced in other sections
TIMESERIES = 'TIMESERIES' # time series data referenced in other sections
NODE_OUTPUT = 'NODE_OUTPUT'
LINK_OUTPUT = 'LINK_OUTPUT'
NODE_TABLE_OUTPUT = 'NODE_TABLE_OUTPUT'
def __init__(self):
GeoAlgorithm.__init__(self)
def getIcon(self):
return QIcon(os.path.dirname(__file__) + '/swmm.png')
def helpFile(self):
return None
def commandLineName(self):
return 'swmm:simulation'
def defineCharacteristics(self):
self.name = 'Simulate flow in storm water conveyance systems'
self.group = 'Simulation'
self.addParameter(ParameterString(self.TITLE, 'Title', 'Swmm Simulation'))
self.addParameter(ParameterTable(self.OPTIONS, 'Analysis options', True))
self.addParameter(ParameterTable(self.REPORT, 'Output reporting instructions', True))
self.addParameter(ParameterTable(self.FILES, 'Interface file options', True))
self.addParameter(ParameterTable(self.RAINGAGES, 'Rain gage information', True))
self.addParameter(ParameterTable(self.HYDROGRAPHS, 'Unit hydrograph data used to construct RDII inflows', True))
self.addParameter(ParameterTable(self.EVAPORATION, 'Evaporation data', True))
self.addParameter(ParameterTable(self.TEMPERATURE, 'Air temperature and snow melt data', True))
self.addParameter(ParameterVector(self.SUBCATCHMENTS, 'Basic subcatchment information', [ParameterVector.VECTOR_TYPE_POLYGON], True))
self.addParameter(ParameterTable(self.SUBAREAS, 'Subcatchment impervious/pervious sub-area data', True))
self.addParameter(ParameterTable(self.INFILTRATION, 'Subcatchment infiltration parameters', True))
self.addParameter(ParameterTable(self.LID_CONTROLS, 'Low impact development control information', True))
self.addParameter(ParameterTable(self.LID_USAGE, 'Assignment of LID controls to subcatchments', True))
self.addParameter(ParameterTable(self.AQUIFERS, 'Groundwater aquifer parameters', True))
self.addParameter(ParameterTable(self.GROUNDWATER, 'Subcatchment groundwater parameters', True))
self.addParameter(ParameterTable(self.SNOWPACKS, 'Subcatchment snow pack parameters', True))
self.addParameter(ParameterVector(self.JUNCTIONS, 'Junction node information', [ParameterVector.VECTOR_TYPE_POINT], True))
self.addParameter(ParameterTable(self.OUTFALLS, 'Outfall node information', True))
self.addParameter(ParameterVector(self.DIVIDERS, 'Flow divider node information', [ParameterVector.VECTOR_TYPE_POINT], True))
self.addParameter(ParameterVector(self.STORAGE, 'Storage node information', [ParameterVector.VECTOR_TYPE_POINT], True))
self.addParameter(ParameterVector(self.CONDUITS, 'Conduit link information', [ParameterVector.VECTOR_TYPE_LINE], True))
self.addParameter(ParameterVector(self.PUMPS, 'Pump link information', [ParameterVector.VECTOR_TYPE_POINT], True))
self.addParameter(ParameterVector(self.ORIFICES, 'Orifice link information', [ParameterVector.VECTOR_TYPE_POINT], True))
self.addParameter(ParameterVector(self.WEIRS, 'Weir link information', [ParameterVector.VECTOR_TYPE_POINT], True))
self.addParameter(ParameterVector(self.OUTLETS, 'Outlet link information', [ParameterVector.VECTOR_TYPE_POINT], True))
self.addParameter(ParameterTable(self.XSECTIONS, 'Conduit, orifice, and weir cross-section geometry', True))
self.addParameter(ParameterTable(self.TRANSECTS, 'Transect geometry for conduits with irregular cross-sections', True))
self.addParameter(ParameterTable(self.LOSSES, 'Conduit entrance/exit losses and flap valves', True))
self.addParameter(ParameterTable(self.CONTROLS, 'Rules that control pump and regulator operation', True))
self.addParameter(ParameterTable(self.POLLUTANTS, 'Pollutant information', True))
self.addParameter(ParameterTable(self.LANDUSES, 'Land use categories', True))
self.addParameter(ParameterTable(self.COVERAGES, 'Assignment of land uses to subcatchments', True))
self.addParameter(ParameterTable(self.BUILDUP, 'Buildup functions for pollutants and land uses', True))
self.addParameter(ParameterTable(self.WASHOFF, 'Washoff functions for pollutants and land uses', True))
self.addParameter(ParameterTable(self.TREATMENT, 'Pollutant removal functions at conveyance system nodes', True))
self.addParameter(ParameterTable(self.INFLOWS, 'External hydrograph/pollutograph inflow at nodes', True))
self.addParameter(ParameterTable(self.DWF, 'Baseline dry weather sanitary inflow at nodes', True))
self.addParameter(ParameterTable(self.PATTERNS, 'Periodic variation in dry weather inflow', True))
self.addParameter(ParameterTable(self.RDII, 'Rainfall-dependent I/I information at nodes', True))
self.addParameter(ParameterTable(self.LOADINGS, 'Initial pollutant loads on subcatchments', True))
self.addParameter(ParameterTable(self.CURVES, 'x-y tabular data referenced in other sections', True))
self.addParameter(ParameterTable(self.TIMESERIES, 'Time series data referenced in other sections', True))
self.addOutput(OutputVector(self.NODE_OUTPUT, 'Node output layer'))
self.addOutput(OutputTable(self.NODE_TABLE_OUTPUT, 'Node output table'))
self.addOutput(OutputVector(self.LINK_OUTPUT, 'Link output layer'))
pass
def checkBeforeOpeningParametersDialog(self):
if not ProcessingConfig.getSetting('Swmm_CLI'):
return 'Swmm command line tool is not configured.\n\
Please configure it before running Swmm algorithms.'
layers = dataobjects.getVectorLayers()
for p in self.parameters:
for layer in layers:
if layer.name() == p.name.lower() :
self.setParameterValue(p.name, layer )
return None
def swmmTable(self, table_name):
uri = self.getParameterValue(table_name)
if not uri: return u''
layer = dataobjects.getObjectFromUri(uri)
pkidx = layer.dataProvider().pkAttributeIndexes()
fields = ""
for i,field in enumerate(layer.dataProvider().fields()):
if not i in pkidx: fields+=field.name()+"\t"
tbl =u'['+table_name+']\n'\
';'+fields+'\n'
for feature in layer.getFeatures():
for i,v in enumerate(feature):
if not i in pkidx:
if str(v) != 'NULL':
m = re.search('^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d:\d\d):\d\d',str(v)) # for date and time saved as timestamps
if m:
tbl += '/'.join(m.group(2,3,1))+'\t'+m.group(4)+'\t'
else:
tbl += str(v)+'\t'
else: tbl += '\t'
tbl += '\n'
tbl += '\n'
return tbl;
def swmmKeyVal(self, table_name, simul_title):
uri = self.getParameterValue(table_name)
if not uri: return u''
layer = dataobjects.getObjectFromUri(uri)
fields = []
for i,field in enumerate(layer.dataProvider().fields()):
fields.append(field.name())
tbl =u'['+table_name+']\n'
found = False
for feature in layer.getFeatures():
if str(feature[0]) == simul_title:
for i,v in enumerate(feature):
if i and str(v) != 'NULL': tbl += fields[i].upper()+'\t'+str(v)+'\n'
elif i : tbl += '\t'
found = True
tbl += '\n'
tbl += '\n'
if not found:
raise GeoAlgorithmExecutionException(
"No simulation named '"+simul_title+"' in "+table_name)
return tbl;
def processAlgorithm(self, progress):
swmm_cli = os.path.abspath(ProcessingConfig.getSetting('Swmm_CLI'))
if not swmm_cli:
raise GeoAlgorithmExecutionException(
'Swmm command line toom is not configured.\n\
Please configure it before running Swmm algorithms.')
folder = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER)
filename = os.path.join(folder, 'swmm.inp')
f = codecs.open(filename,'w',encoding='utf-8')
f.write('[TITLE]\n')
f.write(self.getParameterValue(self.TITLE)+'\n\n')
f.write(self.swmmKeyVal(self.OPTIONS, self.getParameterValue(self.TITLE)))
f.write(self.swmmKeyVal(self.REPORT,self.getParameterValue(self.TITLE)))
f.write(self.swmmTable(self.FILES))
f.write(self.swmmTable(self.RAINGAGES))
f.write(self.swmmTable(self.HYDROGRAPHS))
f.write(self.swmmKeyVal(self.EVAPORATION, self.getParameterValue(self.TITLE)))
f.write(self.swmmTable(self.TEMPERATURE))
f.write(self.swmmTable(self.SUBCATCHMENTS))
f.write(self.swmmTable(self.SUBAREAS))
f.write(self.swmmTable(self.INFILTRATION))
f.write(self.swmmTable(self.LID_CONTROLS))
f.write(self.swmmTable(self.LID_USAGE))
f.write(self.swmmTable(self.AQUIFERS))
f.write(self.swmmTable(self.GROUNDWATER))
f.write(self.swmmTable(self.SNOWPACKS))
f.write(self.swmmTable(self.JUNCTIONS))
f.write(self.swmmTable(self.OUTFALLS))
f.write(self.swmmTable(self.DIVIDERS))
f.write(self.swmmTable(self.STORAGE))
f.write(self.swmmTable(self.CONDUITS))
f.write(self.swmmTable(self.PUMPS))
f.write(self.swmmTable(self.ORIFICES))
f.write(self.swmmTable(self.WEIRS))
f.write(self.swmmTable(self.OUTLETS))
f.write(self.swmmTable(self.XSECTIONS))
f.write(self.swmmTable(self.TRANSECTS))
f.write(self.swmmTable(self.LOSSES))
f.write(self.swmmTable(self.CONTROLS))
f.write(self.swmmTable(self.POLLUTANTS))
f.write(self.swmmTable(self.LANDUSES))
f.write(self.swmmTable(self.COVERAGES))
f.write(self.swmmTable(self.BUILDUP))
f.write(self.swmmTable(self.WASHOFF))
f.write(self.swmmTable(self.TREATMENT))
f.write(self.swmmTable(self.INFLOWS))
f.write(self.swmmTable(self.DWF))
f.write(self.swmmTable(self.PATTERNS))
f.write(self.swmmTable(self.RDII))
f.write(self.swmmTable(self.LOADINGS))
f.write(self.swmmTable(self.CURVES))
f.write(self.swmmTable(self.TIMESERIES))
f.close()
outfilename = os.path.join(folder, 'swmm.out')
progress.setText('running simulation')
log=""
proc = subprocess.Popen(
# this doesn't work on linux, but IMHO should
# [swmm_cli, filename, outfilename]
# this works on linux
swmm_cli+' '+filename+' '+outfilename,
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=False,
).stdout
for line in iter(proc.readline, ''):
log+=line
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, log)
if re.search('There are errors', log):
o = open(outfilename,'r')
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, o.read())
o.close()
raise RuntimeError('There were errors, look into logs for details')
progress.setText('postprocessing output')
# put features in a map indexed by the identifier (first column)
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.JUNCTIONS))
node_fields = QgsFields()
node_fields.append(QgsField('Node', QVariant.String))
node_fields.append(QgsField('Time', QVariant.String))
node_fields.append(QgsField('Inflow', QVariant.Double))
node_fields.append(QgsField('Flooding', QVariant.Double))
node_fields.append(QgsField('Depth', QVariant.Double))
node_fields.append(QgsField('Head', QVariant.Double))
node_feat = {}
for feat in layer.getFeatures():
if feat.geometry() and feat.geometry().exportToWkt():
node_feat[feat.attributes()[0]] = feat
node_writer = self.getOutputFromName(
self.NODE_OUTPUT).getVectorWriter(node_fields.toList(),
QGis.WKBPoint,
layer.crs())
node_table_writer = self.getOutputFromName(
self.NODE_TABLE_OUTPUT).getTableWriter(node_fields.toList())
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.CONDUITS))
link_fields = QgsFields()
link_fields.append(QgsField('Link', QVariant.String))
link_fields.append(QgsField('Time', QVariant.String))
link_fields.append(QgsField('Flow', QVariant.Double))
link_fields.append(QgsField('Velocity', QVariant.Double))
link_fields.append(QgsField('Depth', QVariant.Double))
link_fields.append(QgsField('PercentFull', QVariant.Double))
link_feat = {}
for feat in layer.getFeatures():
if feat.geometry() and feat.geometry().exportToWkt():
link_feat[feat.attributes()[0]] = feat
link_writer = self.getOutputFromName(
self.LINK_OUTPUT).getVectorWriter(link_fields.toList(),
QGis.WKBLineString,
layer.crs())
# here we create output layers
# it's a python implementation of a join
# on the identifyer field between the results an the JUNCTIONS or PIPES
# geometries
total_size = os.path.getsize(outfilename)
total_read = 0
o = codecs.open(outfilename,'r',encoding='utf-8')
# get nodes results
link_id = ''
node_id = ''
line = o.readline()
while line:
line = line.rstrip()
if (node_id or link_id) and not line:
link_id = ''
node_id = ''
if re.search('^ <<< Node ', line):
node_id = line[11:-4]
for i in range(5): line = o.readline()
line = line.rstrip()
if re.search('^ <<< Link ', line):
link_id = line[11:-4]
for i in range(5): line = o.readline()
line = line.rstrip()
if node_id:
feature = QgsFeature(node_fields)
tbl = line.split()
if len(tbl) >= 6:
feature['Node'] = node_id
feature['Time'] = convert_date(tbl[0])+' '+tbl[1]
feature['Inflow'] = tbl[2]
feature['Flooding'] = tbl[3]
feature['Depth'] = tbl[4]
feature['Head'] = tbl[5]
feat = node_feat.get(node_id, None)
if feat : feature.setGeometry(feat.geometry())
node_writer.addFeature(feature)
node_table_writer.addRecord([node_id]+tbl)
if link_id:
feature = QgsFeature(link_fields)
tbl = line.split()
if len(tbl) >= 6:
feature['Link'] = link_id
feature['Time'] = convert_date(tbl[0])+' '+tbl[1]
feature['Flow'] = tbl[2]
feature['Velocity'] = tbl[3]
feature['Depth'] = tbl[4]
feature['PercentFull'] = tbl[5]
feat = link_feat.get(link_id, None)
if feat : feature.setGeometry(feat.geometry())
link_writer.addFeature(feature)
line = o.readline()
total_read += len(line)
progress.setPercentage(int(100*total_read/total_size))
o.close()