-
Notifications
You must be signed in to change notification settings - Fork 9
/
prepare_project.py
287 lines (229 loc) · 15.5 KB
/
prepare_project.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
import sys, os, zipfile, glob, shutil, re
import cj_function_lib as cj
import mdbtools as mdt
path = os.getcwd()
root = path.replace("workflow_lib", "")#[0:-1]
sys.path.insert(0, root)
current_root = sys.argv[1] + "/model"
cj.create_directory(current_root)
import namelist
def _removeNonAscii(s): return "".join(i for i in s if ord(i)<128)
def copyshape(infile, outbase, outdir):
basename = infile[0:-3] + '*'
for filename in glob.iglob(basename):
suffix = os.path.splitext(filename)[1]
outfile = os.path.join(outdir, outbase + suffix)
shutil.copy(filename, outfile)
print "____________________________________________________________________________\nPreparing Project...\n"
project_name = namelist.Project_Name
cj.create_directory(os.path.join(root, project_name))
cj.create_directory(os.path.join(current_root, project_name, "Scenarios", "Default", "TablesIn"))
cj.create_directory(os.path.join(current_root, project_name, "Scenarios", "Default", "TablesOut"))
cj.create_directory(os.path.join(current_root, project_name, "Scenarios", "Default", "TxtInOut"))
cj.create_directory(os.path.join(current_root, project_name, "Source", "crop"))
cj.create_directory(os.path.join(current_root, project_name, "Source", "soil", "soilmap"))
cj.create_directory(os.path.join(current_root, project_name, "Watershed", "Grid"))
cj.create_directory(os.path.join(current_root, project_name, "Watershed", "Shapes"))
cj.create_directory(os.path.join(current_root, project_name, "Watershed", "Tables"))
cj.create_directory(os.path.join(current_root, project_name, "Watershed", "temp"))
cj.create_directory(os.path.join(current_root, project_name, "Watershed", "Text"))
cj.create_directory(os.path.join(current_root, project_name, "Weather"))
# Here we copy database files
print "\t> Getting Databases..."
cj.copy_file(root + "/workflow_lib/DefaultProject.mdb", current_root + "/" + project_name + "/" + project_name + ".mdb")
cj.copy_file(root + "/workflow_lib/QSWATRef2012.mdb", current_root + "/" + project_name + "/" + "QSWATRef2012.mdb")
# put placeholders
print "\t> Extracting placeholders..."
with zipfile.ZipFile(root + "/workflow_lib/placeholdingtiffs.zip","r") as zip_ref:
zip_ref.extractall(current_root + "/" + project_name + "/Source/")
with zipfile.ZipFile(root + "/workflow_lib/placeholdingshapes.zip","r") as zip_ref:
zip_ref.extractall(current_root + "/" + project_name + "/Watershed/Shapes/")
print "\t> Getting tiff from dem\t\t\t: " + namelist.Topography
# copy dem as tiff
cj.convert_raster(sys.argv[1] + "/Data/rasters/" + namelist.Topography, current_root + "/" + project_name + "/Source/dem.tif")
print "\t> Getting landuse and soil maps..."
if os.path.isdir(os.path.join(sys.argv[1], "Data/rasters/", namelist.Soils)):
cj.copytree(os.path.join(sys.argv[1], "Data/rasters/", namelist.Soils), current_root + "/" + project_name + "/Source/soil/{0}/".format(namelist.Soils))
else:
cj.copy_file(os.path.join(sys.argv[1], "Data/rasters/", namelist.Soils), os.path.join(current_root, project_name, "Source", "soil", namelist.Soils))
if os.path.isdir(os.path.join(sys.argv[1], "Data/rasters/", namelist.Land_Use)):
cj.copytree(os.path.join(sys.argv[1], "Data/rasters/", namelist.Land_Use), os.path.join(current_root, project_name, "Source", "crop", namelist.Land_Use))
else:
cj.copy_file(os.path.join(sys.argv[1], "Data/rasters/", namelist.Land_Use), os.path.join(current_root, project_name, "Source", "crop", namelist.Land_Use))
if not namelist.Burn_in_shape == "":
burn_in_shape = sys.argv[1] + "/Data/shapes/" + namelist.Burn_in_shape
if os.path.isfile(burn_in_shape):
copyshape(burn_in_shape, namelist.Burn_in_shape[0:-4], current_root + "/" + project_name + "/Source/")
else:
print("\t! Error: the specified burn-in file was not found, check that it exists and try again.")
print "\t> Getting outlet shape file\t\t: {0}".format(namelist.Outlet)
outletshapefile = os.path.join(sys.argv[1], "Data", "shapes", namelist.Outlet)
if os.path.isfile(outletshapefile):
copyshape(outletshapefile, "outlet", current_root + "/" + project_name + "/Watershed/Shapes/")
copyshape(outletshapefile, "outlet_sel", current_root + "/" + project_name + "/Watershed/Shapes/")
copyshape(outletshapefile, "outlet_sel_snap", current_root + "/" + project_name + "/Watershed/Shapes/")
else:
print("\t! Error: either No outletshape file was specified, or it does not exist.")
print("\t> Getting database tables \t\t: {0}".format(os.path.join(sys.argv[1], "Data", "tables")))
# Here we set lookup tables, usersoil and WGEN_user in the Ref and Project Databases
soil_lu = cj.read_from(os.path.join(sys.argv[1], "Data", "tables", namelist.soil_lookup))
landuse_lu = cj.read_from(os.path.join(sys.argv[1], "Data", "tables", namelist.landuse_lookup))
WGEN_data = cj.read_from(os.path.join(sys.argv[1], "Data", "tables", namelist.WGEN_user))
usersoil_data = cj.read_from(os.path.join(sys.argv[1], "Data", "tables", namelist.Usersoil))
print("\t> Configuring databasses \t\t: {0}.mdb & QSWATRef2012.mdb".format(project_name))
prj_dbase = mdt.mdb_with_ops(os.path.join(current_root, project_name, project_name + ".mdb"))
ref_dbase = mdt.mdb_with_ops(os.path.join(current_root, project_name, "QSWATRef2012.mdb"))
# how to remove existing table if any?
try:
# prj_dbase.delete_table("landuse_lookup")
prj_dbase.delete_table("hru")
except:
pass
try:
prj_dbase.delete_table("soil_lookup")
except:
pass
prj_dbase.clear_table ("landuse_lookup")
prj_dbase.clear_table("soil_lookup")
try:
prj_dbase.create_table("landuse_lookup", "LANDUSE_ID", "INTEGER")
except:
pass
try:
prj_dbase.add_field("landuse_lookup", "SWAT_CODE", "TEXT")
except:
pass
try:
prj_dbase.create_table("soil_lookup", "SOIL_ID", "INTEGER")
except:
pass
try:
prj_dbase.add_field("soil_lookup", "SNAM", "TEXT")
except:
pass
soil_lu_row = {}
for i in range (1, len(soil_lu)):
soil_lu_row["SOIL_ID"] = soil_lu[i].split(",")[0].strip("\n").strip('"')
soil_lu_row["SNAM"] = soil_lu[i].split(",")[1].strip("\n").strip('"')
prj_dbase.insert_row("soil_lookup", soil_lu_row, True)
landuse_lu_row = {}
for i in range (1, len(landuse_lu)):
landuse_lu_row["LANDUSE_ID"] = landuse_lu[i].split(",")[0].strip("\n").strip('"')
landuse_lu_row["SWAT_CODE"] = landuse_lu[i].split(",")[1].strip("\n").strip('"')
prj_dbase.insert_row("landuse_lookup", landuse_lu_row, True)
usersoil_table = {}
wgen_user = {}
wgen_headers = WGEN_data[0].split(",")
usersoil_headers = usersoil_data[0].split(",")
ref_dbase.clear_table("WGEN_user")
ref_dbase.clear_table("usersoil")
import re
for line in WGEN_data:
if (line.split(",")[1] == 'STATION') or (line.split(",")[1] == 'OBJECTID'):
continue
for i in range(0,len(wgen_headers)):
try:
wgen_user[WGEN_data[0].split(",")[i].strip(" ")] = cj._removeNonAscii(line.split(",")[i])
except:
print "\t\t! Your WGEN_user table may have one or more non-ascii characters: convert it at http://utils.paranoiaworks.org/diacriticsremover/"
ref_dbase.insert_row("WGEN_user", wgen_user, True)
for line in usersoil_data:
if line.split(",")[1] == 'MUID':
continue
for i in range(0,len(usersoil_headers)):
usersoil_table[usersoil_data[0].split(",")[i].strip("\xef\xbb\xbf")] = line.split(",")[i]
ref_dbase.insert_row("usersoil", usersoil_table, True)
ref_dbase.disconnect()
prj_dbase.disconnect()
# Get projection info for the project file
proj4, is_proj = cj.get_proj4_from(current_root + "/" + project_name + "/Source/crop/" + namelist.Land_Use)
if not is_proj:
print("\t\t! Warning: makesure your land use map is projected.")
xmin, ymax, xmax, ymin = cj.get_extents(current_root + "/" + project_name + "/Source/dem.tif")
epsg_code, srs_id, prj_name = cj.get_auth(current_root + "/" + project_name + "/Source/dem.tif")
soil_epsg_code, soil_srs_id, soil_prj_name = cj.get_auth(os.path.join(current_root, project_name, "Source", "crop", namelist.Land_Use))
cj.write_to("epsg_code.tmp~", "{0}".format(epsg_code))
projlist = proj4.split(" ")
proj4_dic = {}
for item in projlist:
if (item == '+no_defs') or (item == ''):
continue
else:
proj4_dic[item.split("=")[0]] = item.split("=")[1]
## Here we replace the text inside the project file
project_file_list = cj.read_from(root + "/workflow_lib/template.qgs")
project_file_string = ""
for line in project_file_list:
project_file_string += line
new_pj_string = project_file_string.replace('<slopeBands type="QString">[0, 9999]</slopeBands>', '<slopeBands type="QString">[' + namelist.Slope_classes + ']</slopeBands>')
new_pj_string = new_pj_string.replace('<threshold type="int">0000</threshold>', '<threshold type="int">' + str(namelist.WS_threshold) + '</threshold>')
new_pj_string = new_pj_string.replace('<snapThreshold type="int">000</snapThreshold>', '<snapThreshold type="int">' + str(namelist.OUT_Snap_threshold) + '</snapThreshold>')
new_pj_string = new_pj_string.replace('default_project_name', project_name)
if namelist.HRU_creation_method == 1:
new_pj_string = new_pj_string.replace('<isMultiple type="int">1</isMultiple>', '<isMultiple type="int">0</isMultiple>')
if namelist.HRU_creation_method == 2:
new_pj_string = new_pj_string.replace('<isDominantHRU type="int">0</isDominantHRU>', '<isDominantHRU type="int">1</isDominantHRU>')
new_pj_string = new_pj_string.replace('<isMultiple type="int">1</isMultiple>', '<isMultiple type="int">0</isMultiple>')
if namelist.HRU_creation_method == 3:
new_pj_string = new_pj_string.replace('<areaVal type="int">0</areaVal>', '<areaVal type="int">' + str(namelist.Target_Value) + '</areaVal>')
new_pj_string = new_pj_string.replace('<isArea type="int">0</isArea>', '<isArea type="int">1</isArea>')
if namelist.HRU_creation_method == 4:
new_pj_string = new_pj_string.replace('<isTarget type="int">0</isTarget>', '<isTarget type="int">1</isTarget>')
new_pj_string = new_pj_string.replace('<targetVal type="int">0</targetVal>', '<targetVal type="int">' + str(namelist.Target_Value) + '</targetVal>')
if namelist.HRU_creation_method == 5:
new_pj_string = new_pj_string.replace('<landuseVal type="int">0</landuseVal>', '<landuseVal type="int">' + str(namelist.HRU_thres_LandUse) + '</landuseVal>')
new_pj_string = new_pj_string.replace('<soilVal type="int">0</soilVal>', '<soilVal type="int">' + str(namelist.HRU_thres_Soil) + '</soilVal>')
new_pj_string = new_pj_string.replace('<slopeVal type="int">0</slopeVal>', '<slopeVal type="int">' + str(namelist.HRU_thres_Slope) + '</slopeVal>')
if namelist.HRU_thresholds_type == 1:
new_pj_string = new_pj_string.replace('<isArea type="int">0</isArea>', '<isArea type="int">1</isArea>')
if not namelist.Burn_in_shape == "":
pass
#new_pj_string = new_pj_string.replace('<burn type="QString"></burn>', '<burn type="QString">Source\\' + namelist.Burn_in_shape + '</burn>')
new_pj_string = new_pj_string.replace('Landuses (Landuse)', 'Landuses (' + str(namelist.Land_Use.split(".")[0]) + ')')
if os.path.isdir(current_root + "/" + project_name + "/Source/crop/" + namelist.Land_Use):
new_pj_string = new_pj_string.replace('crop/landuse/hdr.adf', 'crop/' + str(namelist.Land_Use) + '/hdr.adf')
new_pj_string = new_pj_string.replace('crop\\landuse\\hdr.adf', 'crop\\' + str(namelist.Land_Use) + '\\hdr.adf')
else:
new_pj_string = new_pj_string.replace('crop/landuse/hdr.adf', 'crop/' + str(namelist.Land_Use))
new_pj_string = new_pj_string.replace('crop\\landuse\\hdr.adf', 'crop\\' + str(namelist.Land_Use))
if os.path.isdir(current_root + "/" + project_name + "/Source/soil/" + namelist.Soils):
new_pj_string = new_pj_string.replace('soil/soilmap/hdr.adf', 'soil/' + str(namelist.Soils) + '/hdr.adf')
new_pj_string = new_pj_string.replace('soil\\soilmap\\hdr.adf', 'soil\\' + str(namelist.Soils) + '\\hdr.adf')
else:
new_pj_string = new_pj_string.replace('soil/soilmap/hdr.adf', 'soil/' + str(namelist.Soils))
new_pj_string = new_pj_string.replace('soil\\soilmap\\hdr.adf', 'soil\\' + str(namelist.Soils))
new_pj_string = new_pj_string.replace('Landuses__Landuse', 'Landuses__' + str(namelist.Land_Use).split(".")[0])
new_pj_string = new_pj_string.replace('soilmap', '' + str(namelist.Soils).split(".")[0])
# set projection info
new_pj_string = new_pj_string.replace('<proj4>default_proj4</proj4>', '<proj4>' + proj4 + '</proj4>')
new_pj_string = new_pj_string.replace('<projectionacronym>default_projection_acr</projectionacronym>', '<projectionacronym>' + proj4_dic["+proj"] + '</projectionacronym>')
new_pj_string = new_pj_string.replace('<geographicflag>default_flag</geographicflag>', '<geographicflag>' + is_proj + '</geographicflag>')
# set canvas extents
try:
new_pj_string = new_pj_string.replace('<xmin>300000</xmin>', '<xmin>' + str(xmin) + '</xmin>')
new_pj_string = new_pj_string.replace('<xmax>350000</xmax>', '<xmax>' + str(xmax) + '</xmax>')
new_pj_string = new_pj_string.replace('<ymin>1200000</ymin>', '<ymin>' + str(ymin) + '</ymin>')
new_pj_string = new_pj_string.replace('<ymax>1250000</ymax>', '<ymax>' + str(ymax) + '</ymax>')
except:
pass
try:
new_pj_string = new_pj_string.replace('<ellipsoidacronym>default_elipsoid_acr</ellipsoidacronym>', '<ellipsoidacronym>' + proj4_dic["+ellps"] + '</ellipsoidacronym>')
except:
new_pj_string = new_pj_string.replace('<ellipsoidacronym>default_el_acronym</ellipsoidacronym>', '<ellipsoidacronym>' + '</ellipsoidacronym>')
if not prj_name is None:
new_pj_string = new_pj_string.replace('<description></description>', '<description>' + prj_name + '</description>')
new_pj_string = new_pj_string.replace('<authid>prj_authid</authid>', '<authid>EPSG:' + epsg_code + '</authid>')
new_pj_string = new_pj_string.replace('<srid>prj_sr_id</srid>', '<srid>' + str(epsg_code) + '</srid>')
new_pj_string = new_pj_string.replace('<srsid>prj_srs_id</srsid>', '<srsid>' + srs_id + '</srsid>')
new_pj_string = new_pj_string.replace('EPSG:prj_authid', 'EPSG:' + epsg_code)
new_pj_string = new_pj_string.replace('EPSG:soil_authid', 'EPSG:' + soil_epsg_code)
new_pj_string = new_pj_string.replace('default_project_name', project_name)
dem_stats = cj.get_raster_stats(current_root + "/" + project_name + "/Source/dem.tif")
new_pj_string = new_pj_string.replace('<item alpha="255" value="376" label="376 - 1671" color="#0a640a"/>', '<item alpha="255" value="' + str(dem_stats['min']) + '" label="' + str(int(dem_stats['min'])) + ' - ' + str(int(((float(dem_stats['mean']) - float(dem_stats['min']))/2) + float(dem_stats['min']))) + '" color="#0a640a"/>')
new_pj_string = new_pj_string.replace('<item alpha="255" value="2318" label="1671 - 2966" color="#997d19"/>', '<item alpha="255" value="' + str(dem_stats['mean']) + '" label="' + str(int(((float(dem_stats['mean']) - float(dem_stats['min']))/2) + float(dem_stats['min']))) + ' - ' + str(int(((float(dem_stats['max']) - float(dem_stats['mean']))/2) + float(dem_stats['mean']))) + '" color="#997d19"/>')
new_pj_string = new_pj_string.replace('<item alpha="255" value="4261" label="2966 - 4261" color="#ffffff"/>', '<item alpha="255" value="' + str(dem_stats['max']) + '" label="' + str(int(((float(dem_stats['max']) - float(dem_stats['mean']))/2) + float(dem_stats['mean']))) + ' - ' + str(int(dem_stats['max'])) + '" color="#ffffff"/>')
cj.write_to(current_root + "/" + project_name + ".qgs", new_pj_string)
# Copying Weather Files
print "\t> Getting Weather Data.."
cj.copytree(sys.argv[1] + "/Data/Weather/", current_root + "/" + project_name + "/Weather/")
print "\t> Finnished...\n____________________________________________________________________________"