forked from davidbispo/PySWAT
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyswat.py
1297 lines (1110 loc) · 54.5 KB
/
pyswat.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
PYSWAT
PySWAT is a Command Line Interface(CLI)
for Input/Output manipulation and analysis
of the Soil and Water Assessment Tool(SWAT)
version:0.5
Author: David Bispo Ferreira - Federal University of Parana
Updates by Sarah Jordan
Hydroinformatics - Final Project
Spring 2021
Changes:
*Added scenario for Julian date format
*Can process output.rsv
*expressResults function
"""
class connect:
def __init__(self,TxtInOut):
import os
self.TxtInOut = TxtInOut
os.chdir(self.TxtInOut)
def progressBar(self,value, endvalue, bar_length=20): # Barra de progresso da escrita de arquivos
import sys
percent = float(value) / endvalue
arrow = '-' * int(round(percent * bar_length)-1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write("\rPer cent done: [{0}] {1}%".format(arrow + spaces, int(round(percent * 100))))
sys.stdout.flush()
def open_connection(self,output):
import sqlite3
import os
""" create a database connection to the SQLite database specified by db_file
:param db_file: database file
:return: Connection object or None
"""
print("Opening Connection on SQLITE...")
print("File: %s" % output)
try:
conn = sqlite3.connect(os.path.join(os.getcwd(),output))
print('Connection successful')
return conn
except Error as e:
print(e)
def run(self, swat_version="664_rel_64"):
"""
Runs a connected swat model
:swat_version => Release to run. Accepted values:
'664_rel_32', '664_debug_32',
'664_rel_64','664_debug_64,
'670_rel_32','670_debug_32',
'670_rel_64','670_debug_64'
"""
import os
import subprocess
import shutil
dic_versions = {
'664_rel_32': 'rev664_32rel.exe',
'664_debug_32': 'rev664_32debug.exe',
'664_rel_64': 'rev664_64rel.exe',
'664_debug_64': 'rev664_64debug.exe',
'670_rel_32': 'rev670_32rel.exe',
'670_debug_32': 'rev670_32debug.exe',
'670_rel_64': 'rev670_64rel.exe',
'670_debug_64': 'rev670_64debug.exe',
}
print("PySWAT - Run Cycle")
print("Reading SWAT version...")
try:
swat_filename = dic_versions[swat_version]
print("Ok")
except:
print("""Invalid SWAT Version! Please read the documentation.
Acceptable values are follow the '664_rel_32' (Release +
debug or release + bits of processor architecture""")
exit()
this_filedir = os.path.dirname(os.path.abspath(__file__))
exec_dir = os.path.join(this_filedir,'swat_execs',swat_filename)
shutil.copy2(exec_dir, os.getcwd())
def execute(self,cmd):
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, cmd)
os.chdir(self.TxtInOut)
print("Running Swat..")
for path in execute(self,[swat_filename]):
print(path, end="")
if os.path.exists(os.path.join(self.TxtInOut,swat_filename)):
os.remove(os.path.join(self.TxtInOut,swat_filename))
else:
print("Failed to remove SWAT executable from folder. Please remove it Manually and verify folder permisssions")
print('Run Succesful!')
def expressResults(self, startDate, endDate, id_no, variable, julian=False, fetch_tables='rch', freq='d'):
"""
Creates a dataframe of a given variable and feature ID, or a list of variables/feature IDs
If multiple feature IDs and variables, output is a dictinary of dataframes
with a keys of ID numbers.
Inputs:
*startDate -> start date of simulation (excluding warm up period)
*endDate -> end date of simulation
*id_no -> list of feature IDs of interest
*variable -> find possible values from Chapter 32 of SWAT Users Manual,
capitalization and special characters consistent
*fetch_tables -> What output files to process. Can be a string only.
*Acceptable values -> 'hru', 'sub', 'rch', 'rsv',
*Default value -> '.rch'
"""
import os
import sys
import pandas as pd
sys.path.insert(1,os.path.join(os.path.dirname(os.path.abspath(__file__))))#Add dic_par folder to path list
from assets import dic_par #This method calls the parameter SQLITE types for input
from timeit import default_timer as timer
# start = time.time()
start = timer()
dic_output_files = {
'hru': 'output.hru',
'sub': 'output.sub',
'rch': 'output.rch',
'rsv': 'output.rsv' # add reservoir
}
def variableWarning(variable, variable_ls):
'''
Checks if the variable(s) entered by the user are valid
Displays a list of valid(s) for the given output file
and exits the program if the user has entered an invalid variable
'''
if variable not in variable_ls:
print("Variable not found. Try a variable in the following list:")
print(variable_ls)
sys.exit()
def getVariablesInFile(self,filename,tableName, julian):
"""
Fetches whatever variables in file header. Compares with the known
values and returns a list with them. Prevents NOT-SPACED variables
in the SWAT output. It uses a parser at the make table and insertDataToTable
scripts to fix the queries
"""
instance = dic_par.Parameters(tableName)
variables_in_file = []
with open(filename) as infile:
for line in infile:
if 'GIS ' in line:
variablesInFile = line
break
elif 'RES MON' in line:
variablesInFile = line
break
for item in instance.relParameterDBType:
if item in variablesInFile:
variables_in_file.append(item)
else:
pass
if julian == True:
try:
if tableName == 'hru':
variables_in_file.remove('DA') # DA is in hru file from another
except:
pass
infile.close()
return variables_in_file
def separateAreaMon(ls, index_val):
'''
Separates the MON and AREA information, which are not separated by a space
with the Julian date output.
Required for the hru and subbasin output files if Julian = True.
'''
ls.insert(index_val+1, "." + ls[index_val].split('.')[1]) # area
ls[index_val] = ls[index_val].split('.')[0] # mon
return ls
def createTimeseries(all_vals, variable, dates, i):
'''
converts the extracted information of interest to a Pandas dataframe
'''
df = pd.DataFrame()
ts_dict = {i : [] for i in variable}
for v in variable:
for item in all_vals:
try:
ts_dict[v].append(float(item[i[v]]))
except:
ts_dict[v].append(item[i[v]])
for item in ts_dict:
df[item] = ts_dict[item]
# df = pd.DataFrame({variable:ts}, index=dates)
df.index = dates
return df
#Verifies the validity of list or string argument
if type(fetch_tables) != str:
print("""For express function, you can only enter a single
extension. Please enter just one of the following
as a string: \n .hru \n .sub \n .rch \n .rsv""")
print("Program terminated.")
sys.exit()
elif type(fetch_tables) == str:
if fetch_tables in dic_output_files.keys():
pass
else:
print("""You Have wrong keys on the fetch_table
argument. Correct that to continue.
Wrong Key: %s""" %(fetch_tables))
print("Program Terminated")
sys.exit()
if type(id_no) != list:
id_no = [id_no]
variablesInFile = getVariablesInFile(self, filename = os.path.join(self.TxtInOut,
'output.%s' %(fetch_tables)), tableName=fetch_tables,
julian = julian)
# Turn to list
if type(variable) != list:
variable = [variable]
# Initialize empty array
val_dict = {i : [] for i in id_no}
# Find variables included in output file
vbs = variablesInFile
# Check if variable in array
for v in variable:
variableWarning(v, vbs)
index_dict = {i: vbs.index(i) for i in variable}
# Dates
dates = pd.date_range(startDate, endDate, freq=freq)
# filename
filename = os.path.join(self.TxtInOut, 'output.%s' %(fetch_tables))
# Future work could assign the spaces to read to a dictionary
# This would mean we wouldn't need all these if/else statements
with open(filename, 'r') as f:
for line in f:
# Pull all lines from given ID
if fetch_tables == 'hru':
try:
if int(line[5:9]) in id_no:
# vals.append(line)
val_dict[int(line[5:9])].append(line)
except:
pass
elif fetch_tables == 'rch':
try:
if int(line[9:13]) in id_no:
# vals.append(line)
val_dict[int(line[9:13])].append(line)
except:
pass
elif fetch_tables == 'sub':
try:
if int(line[8:12]) in id_no:
# vals.append(line)
val_dict[int(line[8:12])].append(line)
except:
pass
elif fetch_tables == 'rsv':
try:
if int(line[12:14]) in id_no:
# vals.append(line)
val_dict[int(line[12:14])].append(line)
except:
pass
# Split each line on the white space
# Divide MON and AREA, which are not separated by a space
# splitted = [f.split() for f in vals]
splitted = {}
for key in val_dict:
splitted[key] = [f.split() for f in val_dict[key]]
if splitted[key][0][0] == 'REACH' or splitted[key][0][0] == 'BIGSUB':
splitted[key] = [f[1:] for f in splitted[key]]
elif splitted[key][0][0] == 'RES':
splitted[key] = [f[1:-1] for f in splitted[key]]
if fetch_tables == 'hru':
for item in splitted:
if julian == True:
splitted[item] = [separateAreaMon(f, 5) for f in splitted[item]]
elif fetch_tables == 'sub':
for item in splitted:
if julian == True:
splitted[item] = [separateAreaMon(f, 2) for f in splitted[item]]
# Create a timeseries
df_ls = {}
for item in splitted:
df_ls[item] = createTimeseries(splitted[item], variable, dates, index_dict)
end = timer()
timelength = end - start
print("\nData into dataframe in \n Time: %.3f seconds "%(timelength))
if len(df_ls) == 1:
return df_ls[id_no[0]], timelength
else:
return df_ls, timelength
# add dates and julian
def resultFile_toSQL(self, startDate, endDate, julian=False, output="swat_db.sqlite", fetch_tables=['hru','rch'], freq='d'):
"""
Creates a sqlite table in the same folder as TXTInOut
:output => Output name for the database - Must end with .ssqlite,db3, or other SQLite extensions
*Default -> swat_db.sqlite
:fetch_tables => What tables should be fetched. Can be a string or a list with strings(e.g.: ['hru','rch'])
*Acceptable values -> 'hru', 'sub', 'rch', 'rsv', 'all'
"""
import os
import sys
from sqlite3 import Error
sys.path.insert(1,os.path.join(os.path.dirname(os.path.abspath(__file__))))#Add dic_par folder to path list
from assets import dic_par #This method calls the parameter SQLITE types for input
dic_output_files = {
'hru': 'output.hru',
'sub': 'output.sub',
'rch': 'output.rch',
'mgt': 'output.mgt',
'rsv': 'output.rsv' # add reservoir
}
def countLines(self,filename):
def blocks(files, size=65536):
while True:
b = files.read(size)
if not b: break
yield b
with open(filename, "r") as f:
k = sum(bl.count("\n") for bl in blocks(f))
return k
def createTableFromQuery(self,create_table_sql,tableName):
""" create a table from the create_table_sql statement
:param create_table_sql: a CREATE TABLE statement
:return:
"""
#Replacers for names not accepted by SQLITE -> CREATE***
create_table_sql = create_table_sql.replace('#','_')
create_table_sql = create_table_sql.replace('TOT Nkg','TOT_Nkg')
create_table_sql = create_table_sql.replace('TOT Pkg','TOT_Pkg')
create_table_sql = create_table_sql.replace('WTAB CLIm','WTAB_CLIm')
create_table_sql = create_table_sql.replace('WTAB SOLm','WTAB_SOLm')
create_table_sql = create_table_sql.replace('DOXQ mg/L','DOXQ_mg_L')
create_table_sql = create_table_sql.replace('LAT Q(mm)','LAT_Q_mm')
create_table_sql = create_table_sql.replace('CBODU mg/L','CBODU_mg_L')
create_table_sql = create_table_sql.replace('/','_')
create_table_sql = create_table_sql.replace('-','_')
conn = self.open_connection(os.path.join(os.getcwd(),output))
print("Creating table '%s'..." % tableName)
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
error = str(e)
if 'already exists' in error:
try:
c.execute("DROP TABLE %s" % (tableName))
c.execute(create_table_sql)
except:
print("Error on creating tables. Table already exists and cannot be dropped")
exit()
conn.close()
print("Done!")
def getVariablesInFile(self,filename,tableName, julian):
"""
Fetches whatever variables in file header. Compares with the known
values and returns a list with them. Prevents NOT-SPACED variables
in the SWAT output. It uses a parser at the make table and insertDataToTable
scripts to fix the queries
"""
instance = dic_par.Parameters(tableName)
variables_in_file = []
with open(filename) as infile:
for line in infile:
if 'GIS ' in line:
variablesInFile = line
break
elif 'RES MON' in line:
variablesInFile = line
break
for item in instance.relParameterDBType:
if item in variablesInFile:
variables_in_file.append(item)
else:
pass
#SJ Added
# Can run SWAT either with Julian day printed or Month, Day, Year
if julian == True:
try:
if tableName == 'hru':
variables_in_file.remove('DA') # DA is in hru file from another
variables_in_file.insert(5, "DA")
variables_in_file.insert(6, "YR")
# SJ added
elif tableName == 'sub' or tableName=='rch':
variables_in_file.insert(3, "DA")
variables_in_file.insert(4, "YR")
elif tableName == 'rsv':
variables_in_file.insert(2, "DA")
variables_in_file.insert(3, "YR")
except ValueError:
pass
infile.close()
return variables_in_file
def makeTable(self, variablesInFile,tableName):
"""
Code generates a query for constructing a table
and calls the method to execute the query
:Requires a dictionary with variable type
"""
instance = dic_par.Parameters(tableName)
to_append = {}
column_list = []
for i in variablesInFile:
to_append[i] = instance.returnVariableDBType(i)
for j in to_append.keys():
column_list.append('%s %s NOT NULL,'%(j, to_append[j]))
sql_base = """CREATE TABLE %s (
id integer PRIMARY KEY,"""%(tableName)
for q in column_list:
if q == column_list[0]:
sql = sql_base + q
else:
sql = sql + q
sql = sql[0:-1]
sql = sql + ')'
createTableFromQuery(self,create_table_sql = sql, tableName=tableName)
def parseQuery(self, variablesInFile, parameters):
"""
Little Procedural code for fixing SQL Query Syntax to insert to DB
"""
#Creates list of Parameters with the first being a string, not numeric
string = ""
if variablesInFile[0] == 'LULC':
string = "'" + parameters[0] + "'" + ','
for p in range(1, len(parameters)):
string = string + str(parameters[p]) + ','
string = string[0:-1]
else:
for p in range(0, len(parameters)):
string = string + str(parameters[p]) + ','
string = string[0:-1]
return string
## SJ
def separateAreaMon(ls, index_val):
ls.insert(index_val+1, "." + ls[index_val].split('.')[1]) # area
ls[index_val] = ls[index_val].split('.')[0] # mon
return ls
def insertDataToDB(self, variablesInFile,filename, tableName, startDate, endDate, freq):
import time
import pandas as pd
import numpy as np
"""
Inserts values from text file to the Database
"""
print("Inserting Files to Table %s" % tableName)
print("Counting Lines...")
nlines = countLines(self, os.path.join(self.TxtInOut,'output.%s' % item))
print("File Has " + str(nlines) + " Lines")
start = time. time()
conn = self.open_connection(os.path.join(os.getcwd(),output))
try:
c = conn.cursor()
except Error as e:
print(e)
exit()
with open(r"output." + tableName) as infile:
for _ in range(9):
next(infile)
counter=0
c.execute('BEGIN TRANSACTION')
composed_variablesInFile = ''
for i in variablesInFile:
composed_variablesInFile += i +','
composed_variablesInFile = composed_variablesInFile[0:-1]
print("\nStarting Insert to DB...It may take a long time...")
print("***PLEASE DO NOT EXIT THE PROCESS*** \n \n")
# SJ added dates
dates = pd.date_range(startDate, endDate, freq=freq)
dates = np.repeat(dates, (nlines - 9) / len(dates))
for line,d in zip(infile,dates):
splitted = line.split()
if julian == True:
# SJ updates
if tableName == 'hru':
splitted = separateAreaMon(splitted, 5)
# SJ updates
elif tableName == 'sub':
splitted = separateAreaMon(splitted, 3)
if tableName == 'hru':
splitted[5] = d.month
splitted.insert(6, d.day)
splitted.insert(7, d.year)
# SJ updates
elif (tableName == 'sub') | (tableName == 'rch'):
splitted[3] = d.month
splitted.insert(4, d.day)
splitted.insert(5, d.year)
elif tableName == 'rsv':
splitted[2] = d.month
splitted.insert(3, d.day)
splitted.insert(4, d.year)
if splitted[0] == 'REACH' or splitted[0] == 'BIGSUB':
splitted = splitted[1:]
elif splitted[0] == 'RES':
splitted = splitted[1:-1]
#Replacers for names not accepted by SQLITE -> INSERT***
composed_variablesInFile = composed_variablesInFile.replace('#','_')
composed_variablesInFile = composed_variablesInFile.replace('TOT Nkg','TOT_Nkg')
composed_variablesInFile = composed_variablesInFile.replace('TOT Pkg','TOT_Pkg')
composed_variablesInFile = composed_variablesInFile.replace('WTAB CLIm','WTAB_CLIm')
composed_variablesInFile = composed_variablesInFile.replace('WTAB SOLm','WTAB_SOLm')
composed_variablesInFile = composed_variablesInFile.replace('DOXQ mg/L','DOXQ_mg_L')
composed_variablesInFile = composed_variablesInFile.replace('LAT Q(mm)','LAT_Q_mm')
composed_variablesInFile = composed_variablesInFile.replace('CBODU mg/L','CBODU_mg_L')
composed_variablesInFile = composed_variablesInFile.replace('/','_')
composed_variablesInFile = composed_variablesInFile.replace('-','_')
sql_parvalues = parseQuery(self,variablesInFile, splitted)
sentence = """INSERT INTO %s (%s)
VALUES(%s);"""% (tableName,composed_variablesInFile,sql_parvalues)
try:
c.execute(sentence)
except Error as e:
error = str(e)
print(error)
print("Error on line %s" % (counter))
print("TRACEBACK >>>>>>" % (counter,sentence))
print("Error while executing: \n %s" % (sentence))
exit()
counter+=1
self.progressBar(counter, nlines, bar_length=20)
c.execute('COMMIT')
conn.close()
end = time. time()
timelength = end - start
print("\nData sucessfully transferred \n Time: %.3f seconds "%(timelength))
#Verifies the validity of list or string argument
if type(fetch_tables) == list:
for item in fetch_tables:
if item in dic_output_files.keys():
pass
else:
print("""You Have wrong keys on the fetch_table
argument. Correct that to continue.
Wrong Key: %s
""" %(item))
print("Program Terminated")
exit()
elif type(fetch_tables) == str:
if item in dic_output_files.keys():
pass
else:
print("""You Have wrong keys on the fetch_table
argument. Correct that to continue.
Wrong Key: %s""" %(item))
print("Program Terminated")
exit()
else:
print("Wrong File type for argument fetch_table")
print("Program Terminated")
exit()
#Runs SWAT under String or List Arguments
if type(fetch_tables) == list:
for item in fetch_tables:
variablesInFile = getVariablesInFile(self, filename = os.path.join(self.TxtInOut,
'output.%s' %(item)), tableName=item,julian=julian)
print("Setting data on table " + item)
makeTable(self,variablesInFile,item)
#print("Counting Lines...")
insertDataToDB(self,variablesInFile = variablesInFile,
filename = output,
tableName = item,
startDate = startDate, # SJ added these lines
endDate = endDate,
freq=freq)
else:
print("Setting data on table " + fetch_tables)
variablesInFile = getVariablesInFile(output)
makeTable(variablesInFile)
insertDataToDB(variablesInFile,output,variablesInFile)
######################################################
def getModelQuery(self,query,file="swat_db.sqlite", pandas_output=False):
import os
"""
Returns the results of a query on SQLITE
:query -> Query Itself. Use of Docstrings is recommended
:file -> FilePath of the Database
:pandas_output -> bolean that returns result on pandas dataframe.
Keep in mind Pandas does not allow large Tables
"""
print("Opening Connection on SQLITE...")
conn = self.open_connection(os.path.join(os.getcwd(),file))
try:
c = conn.cursor()
except Error as e:
print(e)
exit()
try:
print("Running Query on SQLITE...")
c.execute(query)
results = c.fetchall()
header = list(map(lambda x: x[0], c.description))
print("Query Successful")
if pandas_output == True:
import pandas as pd
results = pd.DataFrame(results,columns=header)
# SJ adds a date column
print(results.YEAR)
try:
cols=["YR","MO","DA"]
results['date'] = results[cols].apply(lambda x: '-'.join(x.values.astype(str)), axis="columns")
results.index = pd.to_datetime(results['date'])
except:
results.set_index("id", inplace=True)
return results
else:
results.insert(0,tuple(header))
return results
except Error as e:
print(e)
######################################################
def plot_hru():
def series():
print('hru series')
######################################################
def plot_sub():
def series():
print('sub series')
######################################################
def plot_rch():
def rch():
print('rch series')
######################################################
def changePar(self,parameter, method, value, sb = 'all' , lulc = 'all', hru = None, log= r'E:\log.txt'):
import os
from .assets import dic_par
from .assets import listtype
from distutils.dir_util import copy_tree
import datetime
def inforeader(self, parser):
#Information reader
FirstLine = parser[0]
firstline_read_single_list = list(FirstLine)
check_sub = FirstLine.find("sub")
check_rte = FirstLine.find("rte")
if check_sub != -1 or check_rte != -1:
HRU_number = None
LULC_type = None
FirstLineSplit = FirstLine.split()
SubBasinPos = FirstLineSplit.index('Subbasin:')
sub_number = int(FirstLineSplit[SubBasinPos+1])
return LULC_type, sub_number,HRU_number
else:
SubBasinPos = FirstLine.find('Subbasin:')
SubBasinStart = SubBasinPos + 9
HRUPos = FirstLine.find('HRU:',26)
SubBasinEnd = HRUPos + -1
LULCPos = FirstLine.find('Luse:')
LULCStart = LULCPos+5
LULCEnd = LULCStart + 3
LULC_scan = firstline_read_single_list[LULCStart:LULCEnd+1]
LULC_type = ''.join(LULC_scan)
sub_number = firstline_read_single_list[SubBasinStart:SubBasinEnd+1]
sub_number = ''.join(sub_number)
sub_number = int(sub_number)
HRU_abs_Pos = FirstLine.find('HRU:')
HRU_abs_Start = HRU_abs_Pos+4
HRU_abs_End = SubBasinPos-1
HRU_scan = firstline_read_single_list[HRU_abs_Start:HRU_abs_End+1]
HRU_scan = ''.join(HRU_scan)
HRU_number = HRU_scan.replace(" ", "")
HRU_number = int(HRU_number)
return LULC_type, sub_number,HRU_number
def log_end(parameter,method,value,files_done,total_files):
print("Writing to log file...")
if os.path.isfile(log) == True:
openfile = open(log, 'r')
lines = openfile.readlines()
len_lines = len(lines)
openfile.close()
timenow = datetime.datetime.now()
line_to_append = ("\n"+ str([len_lines,parameter,method,value,timenow]))
spamwriter = open(log, 'a+')
spamwriter.write(line_to_append)
spamwriter.close()
else:
choice = input("You don't have a logfile. Do you want to create one? [y/n]: ")
if choice == "y":
spamwriter = open(log, 'w')
timenow = datetime.datetime.now()
spamwriter.write(str([0,parameter,method,value,timenow]))
spamwriter.close()
if choice == "n":
pass
print("Warning! You did not print this change to the log!")
print ('Program complete! -> %.0f files altered in a total of %.0f' %(files_done,total_files))
print ('You have successfully changed %s %s with a value of %s'%(parameter,method,value))
print("###Starting parameter changer...")
#Locates the file extension and line number in a dictionary
#CHANGE MAIN CODE
instance = dic_par.param_dic()
target_file, linenumber, exceptions = instance.dic_query(parameter)
#gets a filelist for the specificed folder and format
soil_filenumber = len(listtype.listtype(self.TxtInOut,".sol"))
sub_filenumber = len(listtype.listtype(self.TxtInOut,".sub"))-1 #minus 1: output.sub
print ("You have %.0f hrus and %.0f subbasins this project" %
(soil_filenumber,sub_filenumber))
filelist = listtype.listtype(self.TxtInOut,target_file)
#Uses the fetched list to change values
total_files = len(filelist)
files_done = 0
for exception in exceptions:
flag = exception in filelist
if flag == True:#checks if output file has indeed been printed
index_exception = filelist.index(exception)
del filelist[index_exception]
#####Routine for sol files ####
if target_file == '.sol':
for filename in filelist:
address = os.path.join(self.TXTInOut, filename)
openfile = open(address, 'r')
try:
parser = openfile.readlines()
openfile.close()
LULC_type, sub_number, hru_number = self.inforeader(parser)
except:
print ('Your file %s has problems') % filename
exit()
if hru == None and lulc == 'all'and (sb == sub_number or sub_number in sb)\
or hru == None and (lulc == 'all') and (sb == 'all')\
or hru == None and (lulc == LULC_type or LULC_type in lulc) and sb == 'all'\
or hru == None and (lulc == LULC_type or LULC_type in lulc) and (sb == sub_number or sub_number in sb)\
or (hru != None and type(hru) == int and hru_number == hru)\
or (hru != None and type(hru) == list and (hru_number in hru or hru_number == hru)):
line = parser[linenumber]
linesplit = line.split()
#linesplit_data_str = linesplit[3:]
list_line = list(line)
if parameter == 'SOL_AWC':
linesplit_data = linesplit[6:]
elif parameter == 'SOL_K':
linesplit_data = linesplit[3:]
for i in range(len(linesplit_data)):
linesplit_data[i] = float(linesplit_data[i])
#Replace
if method == 'replace':
print('Dont redo replace in soil files, you will screw your model! Exiting program!')
break
exit()
#Relative
if method == 'relative':
#Gets a parameter list from the backup folder
folder_backup = os.path.join(self.TXTInOut, 'Backup')
address_backup = os.path.join(folder_backup, filename)
if os.path.isdir(folder_backup) == False:
os.makedirs(address_backup)
print('Have a backup folder set up before starting')
exit()
#copy_tree(address, address_backup)
elif os.path.isdir(folder_backup) == True:
openfile_bkp = open(address_backup, 'r')
parser_bkp = openfile_bkp.readlines()
openfile_bkp.close()
try:
line_bkp = parser_bkp[linenumber]
linesplit_bkp = line_bkp.split()
if parameter == 'SOL_AWC':
linesplit_data_bkp = linesplit_bkp[6:]
elif parameter == 'SOL_K':
linesplit_data_bkp = linesplit_bkp[3:]
except:
print("Your file %s has problems" %address_backup)
#Multiplies all values in list by the desired value
for i in range(len(list(linesplit_data_bkp))):
linesplit_data_bkp[i] = (float(linesplit_data_bkp[i]))*(1+value)
#Finds the end and beggining of string
for k in range(len(linesplit_data_bkp)):
end_old = 38 + 12* k
start_old = end_old - len(str(linesplit_data[k]))+1
#Replaces the string positions
for j in range(start_old,end_old+1):
list_line[j] = ''
end_new = end_old
string = '%.3f'%(linesplit_data_bkp[k])
startwrite = end_new - len(string)+1
#spaces = len(string) # Variable inspection
list_line[startwrite:end_new+1] = list(string)
list_line.insert(39, '')
list_line = ''.join(list_line)
parser[linenumber] = list_line
#Writes output file
spamwriter = open(address, 'w')
for line in parser:
spamwriter.write(line)
spamwriter.close()
files_done +=1
log_end(parameter,method,value,files_done,total_files)
#####Routine for non-sol files####
elif target_file != '.sol':
#Open file
for filename in filelist:
address = os.path.join(self.TXTInOut, filename)
openfile = open(address, 'r')
parser = openfile.readlines()
openfile.close()
LULC_type, sub_number, hru_number = self.inforeader(parser)
#Routine for rte or sub files
if target_file == '.rte' or target_file == '.sub':
if sub_number == sb or sub_number in sb:
try:
line = parser[linenumber]
except:
print ('Your file %s has problems') % filename
exit()
if method == 'replace':
float_replace = float(value)
str_replace = '%.3f' % float_replace
list_replace = list(str_replace)
#Position calculator
list_line = list(line)
list_other_line = list(parser[4])
first_position = 15 - len(list_replace)+1
for i in range(16):
list_line[i] = ' '
list_line[first_position:16] = list_replace
list_line_to_str = ''.join(list_line)
if parameter == 'CH_N1' or parameter == parameter == 'ALPHA_BNK' or parameter=='CH_K2':
list_line_to_str = list_line_to_str.replace(" ", "", 2)
list_line_to_str = list_line_to_str.replace("|", " |", 1)
list_line_to_str = list_line_to_str.replace("|", " |", 1)
elif parameter == 'CH_N2':
a=2
parser[linenumber] = list_line_to_str
spamwriter = open(address, 'w')
for linhe in parser:
spamwriter.write(linhe)
spamwriter.close()
files_done +=1
#relative method
elif method == 'relative':
#Parses the File in the Backup folder for value
folder_backup = os.path.join(self.TXTInOut, 'Backup')
address_backup = os.path.join(folder_backup, filename)
if os.path.isdir(folder_backup) == False:
os.makedirs(address_backup)
copy_tree(address, address_backup)
elif os.path.isdir(folder_backup) == True:
openfile = open(address_backup, 'r')
parser = openfile.readlines()
try:
line_bkp = parser[linenumber]
par_orig_value = line_bkp[:16]
par_orig_value = float(par_orig_value)
if par_orig_value == 0:
print("Your file file %s has a starting value for %s of zero. Please check\
for relative references"%(filename, parameter))
par_new_value = par_orig_value * (1+value)
openfile.close()
except:
print ('Your file %s has problems') % filename
exit()
#Replaces the parameter with a new calculated value
str_replace = '%.3f' % par_new_value
list_replace = list(str_replace)
#Position calculator
line = parser[linenumber]
list_line = list(line)
if parameter == 'CH_N2':
first_position = 14 - len(list_replace)+1
final_position = 14
else:
first_position = 16 - len(list_replace)+1
final_position = 16