-
Notifications
You must be signed in to change notification settings - Fork 1
/
SWMMIO.pas
423 lines (380 loc) · 14.3 KB
/
SWMMIO.pas
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
{ ------------------------------------------------------------------- }
{ Unit: SWMMIO.pas }
{ Project: WERF Framework - SWMM Converter }
{ Version: 2.0 }
{ Date: 2/28/2014 }
{ Author: Gesoyntec (D. Pankani) }
{ }
{ Delphi Pascal unit containing various utility functions, global variables }
{ and constants, primarily used for interacting with SWMM5 input / output }
{ files }
{ ------------------------------------------------------------------- }
unit SWMMIO;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ConverterErrors,
StrUtils, Dialogs, jpeg, ExtCtrls, ComCtrls, StdCtrls, Buttons,DateUtils;
type
TMTARecord = record // converted framework timeseries data structure
tsName: string;
tsNodeName: string;
tsType: string; // FLOW or CONCEN
tsUnitsFactor: Double; // default 1.0
constituentSWMMName: string;
constituentFWName: string;
convFactor: Double; // default 1.0
convertedTS: TStringList;
convertedTSFilePath: string;
ModelRunScenarioID: string;
SWMMFilePath: string;
scratchFilePath: string;
mtaFilePath: string;
end;
const
// includes NsubcatchResults,SUBCATCH_RAINFALL,SUBCATCH_SNOWDEPTH,SUBCATCH_LOSSES,SUBCATCH_RUNOFF,SUBCATCH_GW_FLOW,SUBCATCH_GW_ELEV;
NUMSUBCATCHVARS: integer = 7;
MIN_WQ_FLOW: Double = 0.001; // minmun water quality flow from swmm
NODE_INFLOW: integer = 4;
NODE_QUAL: integer = 6;
MAX_NODE_RESULTS = 7;
MAX_SUBCATCH_RESULTS = 7;
MAX_LINK_RESULTS = 6;
MAX_SYS_RESULTS = 14;
opModes: array [0 .. 1] of string = ('SWMM_TO_FW', 'SWMM_FROM_FW');
appTypes: array [0 .. 1] of string = ('SWMM_CONSOLE', 'SWMM_GUI');
{ constituentNames: array [0 .. 7] of string = ('FLOW', 'TSS', 'TP', 'DP',
'DZn', 'TZN', 'DCU', 'TCU'); }
constituentNames: array [0 .. 22] of string = ('Q', 'FC', 'TSS1', 'TSS2',
'TSS3', 'TSS4', 'TSS5', 'TSS', 'TP', 'TDS', 'POO4', 'NO3', 'LDOM', 'RDOM',
'LPOM', 'RPOM', 'BOD1', 'ALGAE', 'DO', 'TIC', 'ALK', 'Gen 1', 'NH4');
// NnodeResults,NODE_DEPTH,NODE_HEAD,NODE_VOLUME,NODE_LATFLOW,NODE_INFLO,NODE_OVERFLOW;
NUMNODEVARS: integer = 7;
// NlinkResults,LINK_FLOW,LINK_DEPTH,LINK_VELOCITY,LINK_FROUDE,LINK_CAPACITY;
NUMLINKVARS: integer = 6;
// input/output file names
fileNameGroupNames = 'groupnames.txt'; //provides FW timespan and hold file paths for batching
fileNameParamsList = 'params.txt'; //number and list of constituents
fileNameScratch = 'scratch'; //fw times series file
fileNameFWControlFile = 'swmmconvertstrings.txt'; //fw times series control metatadata file
fileNameMessages = 'messages.txt'; //communicates successes and errors to framework
var
workingDir: string; // exe folder
SWMMFileStreamPosition: long;
operatingMode: string; // SWMM_TO_FW or SWMM_FROM_FW'
appType: string; // SWMM_CONSOLE or SWMM_GUI
// stores existing swmm TS and Inflow block names in swmm inputfile
TSList, InflowsList: TStringList;
PollList, NodeNameList: TStringList;
frameCtrlFilePath, mtaFilePath: string;
//errorsList: TStringList;
// stores file stream seek position after node and poll names are read
function readInFrameworkTSFile(filePath: string; var Conv: TArray<TMTARecord>)
: TArray<TMTARecord>;
function getSWMMNodeIDsFromTxtInput(SWMMFilePath: string): TArray<TStringList>;
function getSWMMNodeIDsFromBinary(SWMMFilePath: string): TArray<TStringList>;
procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
procedure saveTextFileToDisc(FileContentsList: TStringList; filePath: string;
shdOverwrite: boolean = false);
implementation
function readInFrameworkTSFile(filePath: string; var Conv: TArray<TMTARecord>)
: TArray<TMTARecord>;
var
FileContentsList: TStringList;
strLine: string;
lineNumber: integer;
tempStrList: TStrings;
tempTimeVal: Double;
tempDateTimeStr, tempTimeStr: string;
tempValueStr: string;
i: integer;
j: integer;
begin
FileContentsList := TStringList.Create;
tempStrList := TStringList.Create;
errorsList := TStringList.Create;
{ First check if the file exists. }
if Not(FileExists(filePath)) then
begin
errorsList.Add('Framework time series data file not found at:' + filePath);
exit;
end;
try
for i := Low(Conv) to High(Conv) do
begin
Conv[i].convertedTS := TStringList.Create;
end;
FileContentsList.LoadFromFile(filePath);
lineNumber := 0;
while lineNumber < FileContentsList.Count - 1 do
begin
strLine := FileContentsList[lineNumber];
// ignore comment lines
if (Pos('#', strLine) < 1) and (Length(strLine) > 1) then
begin
tempStrList.Clear();
ExtractStrings([','], [], PChar(strLine), tempStrList);
tempTimeVal := strToFloat(tempStrList[3]);
tempTimeStr := floatToStr(int(tempTimeVal)) + ':' +
FormatFloat('00', frac(tempTimeVal) * 60);
tempDateTimeStr := Format('%.2d/%.2d/%s %5s',
[strToInt(tempStrList[1]), strToInt(tempStrList[2]),
trim(tempStrList[0]), tempTimeStr]);
for i := Low(Conv) to High(Conv) do
begin
j := i + 4;
if (j < tempStrList.Count) then
begin
tempValueStr := tempStrList[j];
Conv[i].convertedTS.Add(tempDateTimeStr + ' ' + tempValueStr);
end;
end;
end;
inc(lineNumber);
end;
finally
FileContentsList.Free;
tempStrList.Free;
end;
result := Conv;
end;
function getSWMMNodeIDsFromTxtInput(SWMMFilePath: string): TArray<TStringList>;
var
FileContentsList: TStringList;
rsltLists: TArray<TStringList>;
SwmmTokens: TStringList;
lineNumber, intTokenLoc, tempInt, i: integer;
strLine, strToken, strObjectID: string;
begin
intTokenLoc := 0;
FileContentsList := TStringList.Create;
SetLength(rsltLists, 4);
for i := Low(rsltLists) to High(rsltLists) do
rsltLists[i] := TStringList.Create();
// 0-NodeIDs list, 1-Pollutants list, 2-Timeseries list, 3-Inflows list
SwmmTokens := TStringList.Create;
// Add supported SWMM Node types to the list of tokens to parse
SwmmTokens.Delimiter := ' '; // Each list item will be blank separated
SwmmTokens.QuoteChar := '|'; // And each item will be quoted with |'s
SwmmTokens.DelimitedText :=
'|[DIVIDERS]| |[JUNCTIONS]| |[OUTFALLS]| |[STORAGE]| |[POLLUTANTS]| |[TIMESERIES]| |[INFLOWS]|';
if FileExists(SWMMFilePath) then
begin
{ If it exists, load the data into the stringlist. }
FileContentsList.LoadFromFile(SWMMFilePath);
lineNumber := 0;
while lineNumber < FileContentsList.Count - 1 do
begin
strLine := LowerCase(FileContentsList[lineNumber]);
for i := 0 to SwmmTokens.Count - 1 do
begin
strToken := LowerCase(SwmmTokens[i]);
intTokenLoc := Pos(strToken, strLine);
// check inputfile line to see if token present
if intTokenLoc > 0 then
break;
end;
// if token found read in node names
if intTokenLoc > 0 then
begin
Repeat
inc(lineNumber);
strLine := FileContentsList[lineNumber];
intTokenLoc := Pos('[', strLine);
if intTokenLoc > 0 then
begin
dec(lineNumber);
break;
end;
// ignore comment lines
if (Pos(';;', strLine) < 1) and (Length(strLine) > 1) then
begin
// extract node name
tempInt := Pos(' ', strLine);
if tempInt > 0 then
begin
strObjectID := Copy(strLine, 1, tempInt - 1);
// 0-NodeIDs list, 1-Pollutants list, 2-Timeseries list, 3-Inflows list
if i = 4 then
// if we are in the [POLLUTANTS] block save names to pollutants list
begin
rsltLists[1].Add(strObjectID);
end
else if i = 5 then
// if we are in the [TIMESERIES] block save names to TIMESERIES list
begin
rsltLists[2].Add(strObjectID);
end
else if i = 6 then
// if we are in the [INFLOWS] block save names to INFLOWS list
begin
rsltLists[3].Add(strLine);
end
else
// everything else is a node so save names to nodes list
rsltLists[0].Add(strObjectID);
end;
end;
until intTokenLoc > 0;
end;
inc(lineNumber);
end;
result := rsltLists;
end
else
begin
errorsList.Add('SWMM input file not found at:' + SWMMFilePath);
exit;
end;
end;
function getSWMMNodeIDsFromBinary(SWMMFilePath: string): TArray<TStringList>;
var
Stream: TFileStream;
Reader: TBinaryReader;
numberOfPeriods, outputStartPos: integer;
numSubCatchs, numLinks, numPolls, numNodes: integer;
reportStartDate, reportTimeInterv: Double;
days: TDateTime;
myYear, myMonth, myDay: Word;
myHour, myMin, mySec, myMilli: Word;
idx: long;
numCharsInID: integer;
tempID: string;
tempIDCharArr: TArray<Char>;
nodeIDList, pollutantIDList: TStringList;
startDateList, endDateList: TStringList;
begin
Stream := TFileStream.Create(SWMMFilePath, fmOpenRead or fmShareDenyWrite);
nodeIDList := TStringList.Create();
pollutantIDList := TStringList.Create();
endDateList := TStringList.Create();
startDateList := TStringList.Create();
try
Reader := TBinaryReader.Create(Stream);
try
// First get number of periods from the end of the file
Stream.Seek(-4 * sizeof(Integer), soEnd);
// the byte position where the Computed Results section of the file begins (4-byte integer)
outputStartPos := Reader.ReadInteger;
// number of periods
numberOfPeriods := Reader.ReadInteger;;
Stream.Seek(0, soBeginning);
Reader.ReadInteger; // Magic number
Reader.ReadInteger; // SWMM Version number
Reader.ReadInteger; // Flow units
numSubCatchs := Reader.ReadInteger; // # subcatchments
numNodes := Reader.ReadInteger; // # nodes
numLinks := Reader.ReadInteger; // # links
numPolls := Reader.ReadInteger; // # pollutants
// Read all subcatchment IDs and discard, skipping this section is not straight forward since catchment
// name lengths vary
for idx := 0 to numSubCatchs - 1 do
begin
numCharsInID := Reader.ReadInteger;
tempIDCharArr := Reader.ReadChars(numCharsInID);
end;
// Read all node IDs and save for use later
for idx := 0 to numNodes - 1 do
begin
numCharsInID := Reader.ReadInteger;
tempIDCharArr := Reader.ReadChars(numCharsInID);
if Length(tempIDCharArr) > 0 then
begin
SetString(tempID, PChar(@tempIDCharArr[0]), Length(tempIDCharArr));
nodeIDList.Add(tempID);
end
end;
// Read all link IDs and discard, skipping this section is not straight forward since catchment
// name lengths vary
for idx := 0 to numLinks - 1 do
begin
numCharsInID := Reader.ReadInteger;
tempIDCharArr := Reader.ReadChars(numCharsInID);
end;
// Read all pollutant IDs and save for use later
for idx := 0 to numPolls - 1 do
begin
numCharsInID := Reader.ReadInteger;
tempIDCharArr := Reader.ReadChars(numCharsInID);
if Length(tempIDCharArr) > 0 then
begin
SetString(tempID, PChar(@tempIDCharArr[0]), Length(tempIDCharArr));
pollutantIDList.Add(tempID);
end
else
end;
// save stream position for later when we extract node results to avoid having to start over
SWMMFileStreamPosition := Stream.Position;
Stream.Seek(outputStartPos - (sizeof(Double) + sizeof(Integer)),
soBeginning);
// Get Start date and reporting timestep
reportStartDate := Reader.ReadDouble;
// StartDateTime = getDateTime(reportStartDate)
reportTimeInterv := Reader.ReadInteger;
// compute timeseries start date
days := reportStartDate;
DecodeDateTime(days, myYear, myMonth, myDay, myHour, myMin,
mySec, myMilli);
startDateList.Add(IntToStr(myYear));
startDateList.Add(IntToStr(myMonth));
startDateList.Add(IntToStr(myMonth));
startDateList.Add(IntToStr(myHour));
// compute timeseries end date
days := reportStartDate + (reportTimeInterv * numberOfPeriods / 86400.0);
DecodeDateTime(days, myYear, myMonth, myDay, myHour, myMin,
mySec, myMilli);
endDateList.Add(IntToStr(myYear));
endDateList.Add(IntToStr(myMonth));
endDateList.Add(IntToStr(myMonth));
endDateList.Add(IntToStr(myHour));
finally
Reader.Free;
end;
finally
Stream.Free;
end;
SetLength(result, 4);
result[0] := nodeIDList;
result[1] := pollutantIDList;
result[2] := startDateList;
result[3] := endDateList;
end;
procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
Assert(Assigned(Strings));
Strings.Clear;
Strings.Delimiter := Delimiter;
Strings.DelimitedText := '"' + StringReplace(Input, Delimiter,
'"' + Delimiter + '"', [rfReplaceAll]) + '"';
end;
procedure saveTextFileToDisc(FileContentsList: TStringList; filePath: string;
shdOverwrite: boolean = false);
var
dirName: string;
begin
// Save a new swmm file back to disc
if (FileContentsList <> nil) then
begin
// check if directory exists
dirName := ExtractFilePath(filePath);
if (DirectoryExists(dirName) = false) then
begin
if CreateDir(dirName) then
// do nothing ShowMessage('New directory added OK')
else
begin
raise Exception.Create
('Fatal Error: Unable to create directory for saving file - error : '
+ IntToStr(GetLastError));
exit;
end;
end;
{ First check if the file exists. }
if (not shdOverwrite) and (FileExists(filePath)) then
{ If it exists, raise an exception. }
raise Exception.Create
('Fatal Error: File already exists. Attemp to overwrite failed.')
else
FileContentsList.SaveToFile(filePath);
end;
end;
end.