forked from DHI-GRAS/qgis-processing-swat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWAT_PEST_utilities.py
183 lines (176 loc) · 11.1 KB
/
SWAT_PEST_utilities.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
"""
***************************************************************************
SWAT_PEST_utilities.py
-------------------------------------
Copyright (C) 2014 TIGER-NET (www.tiger-net.org)
***************************************************************************
* This plugin is part of the Water Observation Information System (WOIS) *
* developed under the TIGER-NET project funded by the European Space *
* Agency as part of the long-term TIGER initiative aiming at promoting *
* the use of Earth Observation (EO) for improved Integrated Water *
* Resources Management (IWRM) in Africa. *
* *
* WOIS is a free software i.e. 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 3 of the License, *
* or (at your option) any later version. *
* *
* WOIS 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. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************
"""
from SWAT_parameter_specs import SWAT_parameter_specs
from SWAT_PEST_specs import SWAT_PEST_specs
from SWAT_output_format_specs import SWAT_output_format_specs
import numpy
import os
import datetime
from dateutil.relativedelta import *
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from read_SWAT_out import read_SWAT_time
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
def create_PEST_template(src_folder, SWATparameter, parameter_name, subid, hruid, PARVAL1, PARLBND, PARUBND):
if (SWATparameter[0:5]=='RFINC') | (SWATparameter[0:6]=='TMPINC'):
if len(parameter_name)>6:
raise GeoAlgorithmExecutionException('Parameter name for '+SWATparameter+' should have maximum 6 characters.')
if len(parameter_name)>12:
raise GeoAlgorithmExecutionException('Parameter name for '+SWATparameter+' should have maximum 12 characters.')
specs = SWAT_parameter_specs()
pestspecs = SWAT_PEST_specs()
pars = numpy.array(specs.PARAMETERS)
index = int(numpy.where(pars == SWATparameter)[0])
if specs.PARLEVELS[index] == 'bsn':
SWAT_filename = 'basins.bsn'
TEMPLATE_filename = 'basins_bsn.tpl'
elif specs.PARLEVELS[index] == 'sub':
SWAT_filename = "%05d" % subid + '0000.' + specs.PARFILES[index]
TEMPLATE_filename = "%05d" % subid + '0000_' + specs.PARFILES[index] + '.tpl'
elif specs.PARLEVELS[index] =='hru':
SWAT_filename = "%05d" % subid + "%04d" % hruid + '.' + specs.PARFILES[index]
TEMPLATE_filename = "%05d" % subid + "%04d" % hruid + '_' + specs.PARFILES[index] + '.tpl'
SWAT_filename = src_folder + os.sep + SWAT_filename
TEMPLATE_filename = src_folder + os.sep + TEMPLATE_filename
if os.path.isfile(SWAT_filename):
if os.path.isfile(TEMPLATE_filename):
lines = open(TEMPLATE_filename,'r').readlines()
changeline = lines[specs.PARLINES[index]]
if (SWATparameter[0:5]=='RFINC') | (SWATparameter[0:6]=='TMPINC') | (SWATparameter[0:5]=='SOL_Z') | (SWATparameter[0:7]=='SOL_AWC') | (SWATparameter[0:5]=='SOL_K'):
lines[specs.PARLINES[index]] = changeline[:specs.PARSTARTCOL[index]-1] + pestspecs.ptf + parameter_name.rjust(specs.PARENDCOL[index]-specs.PARSTARTCOL[index]-1) + pestspecs.ptf + changeline[specs.PARENDCOL[index]:len(changeline)]
else:
lines[specs.PARLINES[index]] = pestspecs.ptf + parameter_name.rjust(specs.PARENDCOL[index]-specs.PARSTARTCOL[index]) + pestspecs.ptf + changeline[specs.PARENDCOL[index]+1:len(changeline)]
ntfile = open(TEMPLATE_filename,'w')
ntfile.writelines(lines)
ntfile.close()
else:
line1 = 'ptf ' + pestspecs.ptf + '\n'
lines = open(SWAT_filename).readlines()
changeline = lines[specs.PARLINES[index]-1]
if (SWATparameter[0:5]=='RFINC') | (SWATparameter[0:6]=='TMPINC') | (SWATparameter[0:5]=='SOL_Z') | (SWATparameter[0:7]=='SOL_AWC') | (SWATparameter[0:5]=='SOL_K'):
lines[specs.PARLINES[index]-1] = changeline[:specs.PARSTARTCOL[index]-1] + pestspecs.ptf + parameter_name.rjust(specs.PARENDCOL[index]-specs.PARSTARTCOL[index]-1) + pestspecs.ptf + changeline[specs.PARENDCOL[index]:len(changeline)]
else:
lines[specs.PARLINES[index]-1] = pestspecs.ptf + parameter_name.rjust(specs.PARENDCOL[index]-specs.PARSTARTCOL[index]) + pestspecs.ptf + changeline[specs.PARENDCOL[index]+1:len(changeline)]
ntfile = open(TEMPLATE_filename,'w')
ntfile.writelines(line1)
ntfile.writelines(lines)
ntfile.close()
parblockname = src_folder + os.sep + parameter_name + '.pbf'
parblockfile = open(parblockname,'w')
parblockfile.writelines(parameter_name.ljust(13) + specs.PARTRANS[index].ljust(6) + specs.PARCHGLIM[index].ljust(9) + str(PARVAL1).ljust(9) + str(PARLBND).ljust(9) + str(PARUBND).ljust(9) + SWATparameter.ljust(13) + specs.SCALE[index].ljust(5) + specs.OFFSET[index].ljust(5) + specs.DERCOM[index])
parblockfile.close()
else:
raise GeoAlgorithmExecutionException('SWAT input file ' + SWAT_filename + ' not found in source directory')
return TEMPLATE_filename, SWAT_filename
##def create_PEST_instruction(src_folder, obsfile, obsgroup, reachid, nreaches, tmpres):
def create_PEST_instruction(src_folder, obsfile, obsgroup, nreaches, tmpres):
specs = SWAT_output_format_specs()
pestspecs = SWAT_PEST_specs()
obsdata = numpy.genfromtxt(obsfile, delimiter = ',', skiprows=0, missing_values = 'NaN')
obsdata[:,0] = obsdata[:,0] + specs.PYEX_DATE_OFFSET
SWAT_time_info = read_SWAT_time(src_folder)
startyr = datetime.date(int(SWAT_time_info[1]),1,1)
startdate = startyr + datetime.timedelta(days=int(SWAT_time_info[2])-1)
if SWAT_time_info[4] > 0:
startdate = datetime.date(int(SWAT_time_info[1]+SWAT_time_info[4]),1,1)
endyear = datetime.date(int(SWAT_time_info[1]+SWAT_time_info[0]-1),1,1)
enddate = endyear + datetime.timedelta(days=int(SWAT_time_info[3])-1)
insname = obsfile.split('.')[0] + '_' + obsfile.split('.')[1] + '.ins'
insfile = open(insname,'w')
insfile.writelines('pif ' + pestspecs.pif + '\n')
insfile.writelines('l9\n')
obsblockname = obsfile.split('.')[0] + '_' + obsfile.split('.')[1] + '_observation_block.obf'
obsblockfile = open(obsblockname,'w')
obsid = 1
if tmpres == 0:
tsteps = int((enddate-startdate).days) + 1
for i in range(0,tsteps):
currentdate = startdate + datetime.timedelta(i)
currentdatenum = matplotlib.dates.date2num(currentdate)
obs = obsdata[obsdata[:,0]==currentdatenum,1]
obsw = obsdata[obsdata[:,0]==currentdatenum,2]
obsw = 1./numpy.power(obsw,2)
try:
reachid = int(obsdata[obsdata[:,0]==currentdatenum,3][0])
except:
pass
if ~numpy.isnan(obs):
obsblockfile.writelines('reach_' + str(reachid) + '_' + str(obsid) + ' ' + str(obs[0]) + ' ' + str(obsw[0]) + ' ' + obsgroup + '\n')
insfile.writelines('l' + str(reachid) + ' [' + 'reach_' + str(reachid) + '_' + str(obsid) + ']' + str(pestspecs.flowoutinicol) + ':' + str(pestspecs.flowoutendcol) + '\n')
if reachid < nreaches:
insfile.writelines('l' + str(nreaches-reachid) + '\n')
obsid = obsid + 1
else:
insfile.writelines('l' + str(nreaches) + '\n')
elif tmpres == 1:
tsteps = int((enddate-startdate).days) + 1
currentyear = startdate
while (currentyear <= endyear):
if currentyear == endyear:
for m in range(0,enddate.month):
currentdate = currentyear + relativedelta(months=+m)
currentdatenum = matplotlib.dates.date2num(currentdate)
obs = obsdata[obsdata[:,0]==currentdatenum,1]
obsw = obsdata[obsdata[:,0]==currentdatenum,2]
obsw = 1./numpy.power(obsw,2)
try:
reachid = int(obsdata[obsdata[:,0]==currentdatenum,3][0])
except:
pass
if ~numpy.isnan(obs):
obsblockfile.writelines('reach_' + str(reachid) + '_' + str(obsid) + ' ' + str(obs[0]) + ' ' + str(obsw[0]) + ' ' + obsgroup + '\n')
insfile.writelines('l' + str(reachid) + ' [' + 'reach_' + str(reachid) + '_' + str(obsid) + ']' + str(50) + ':' + str(61) + '\n')
if reachid < nreaches:
insfile.writelines('l' + str(nreaches-reachid) + '\n')
obsid = obsid + 1
else:
insfile.writelines('l' + str(nreaches) + '\n')
insfile.writelines('l' + str(nreaches) + '\n')
currentyear = currentyear + relativedelta(years=+1)
else:
for m in range(0,12):
currentdate = currentyear + relativedelta(months=+m)
currentdatenum = matplotlib.dates.date2num(currentdate)
obs = obsdata[obsdata[:,0]==currentdatenum,1]
obsw = obsdata[obsdata[:,0]==currentdatenum,2]
obsw = 1./numpy.power(obsw,2)
try:
reachid = int(obsdata[obsdata[:,0]==currentdatenum,3][0])
except:
pass
if ~numpy.isnan(obs):
obsblockfile.writelines('reach_' + str(reachid) + '_' + str(obsid) + ' ' + str(obs[0]) + ' ' + str(obsw[0]) + ' ' + obsgroup + '\n')
insfile.writelines('l' + str(reachid) + ' [' + 'reach_' + str(reachid) + '_' + str(obsid) + ']' + str(50) + ':' + str(61) + '\n')
if reachid < nreaches:
insfile.writelines('l' + str(nreaches-reachid) + '\n')
obsid = obsid + 1
else:
insfile.writelines('l' + str(nreaches) + '\n')
insfile.writelines('l' + str(nreaches) + '\n')
currentyear = currentyear + relativedelta(years=+1)
return insname, obsblockname