-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_RVH.py
412 lines (369 loc) · 17.4 KB
/
create_RVH.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
#!python
import os
import sys
import timeit
import logging
from basinmaker import basinmaker
import time
import ntpath
#Fakes a display to avoid an error with Qt
os.system('Xvfb :99 -screen 0 640x480x8 -nolisten tcp &')
logging.captureWarnings(True)
#Use Basinmaker Define Project spatial extent.
def defineExtent():
mode = params['extentmode']
demname = ntpath.basename(params['pathdem'])
start = time.time()
try:
print('\nDefine_Project_Spatial_Extent running...\n')
if mode == "using_dem":
bm.Define_Project_Spatial_Extent(
mode=mode,
path_to_dem_input = os.path.join(os.getcwd(),datafolder,'DEM',demname),
gis_platform="qgis"
)
elif mode == "using_hybasin":
hybasinname = ntpath.basename(params['pathhybasin'])
hybasinID = int(params['hybasinid'])
bufferdist = params['bufferdistance']
bm.Define_Project_Spatial_Extent(
mode=mode,
path_to_dem_input = os.path.join(os.getcwd(),datafolder,'DEM',demname),
gis_platform="qgis",
path_to_hydrobasin_polygon = os.path.join(os.getcwd(),datafolder,'hybasin',hybasinname),
hydrobasin_id_of_watershed_outlet = hybasinID,
buffer_distance = float(bufferdist)
)
elif mode == "using_outlet_pt":
outletcoordinates = params['outletlat'], params['outletlon']
bm.Define_Project_Spatial_Extent(
mode=mode,
path_to_dem_input = os.path.join(os.getcwd(),datafolder,'DEM',demname),
gis_platform="qgis",
watershed_outlet_coordinates = outletcoordinates
)
elif mode == "using_provided_ply":
extentpolyname = ntpath.basename(params['path_providedpoly'])
bufferdist = params['bufferdistance']
bm.Define_Project_Spatial_Extent(
mode=mode,
path_to_dem_input = os.path.join(os.getcwd(),datafolder,'DEM',demname),
gis_platform="qgis",
path_to_spatial_extent_polygon = os.path.join(os.getcwd(),datafolder,'extent_poly', extentpolyname),
buffer_distance = float(bufferdist)
)
print('Define_Project_Spatial_Extent was successful...\n')
except Exception as e:
print('Define_Project_Spatial_Extent failed...')
print(e)
sys.exit()
end = time.time()
print("Define_Project_Spation_Extent took", end - start, "seconds")
#Use Basinmaker Delineate routing structure without lakes.
def delineateNoLakes():
mode = params['delineatemode']
facthreshold = float(params['facthreshold'])
start = time.time()
try:
print('\nDelineation_Initial_Subbasins_Without_Lakes running...\n')
if mode == "using_dem":
bm.Delineation_Initial_Subbasins_Without_Lakes(
fac_thresold = facthreshold,
mode=mode,
max_memroy= maxmemory,
gis_platform="qgis",
)
elif mode == "using_fdr":
fdrname = ntpath.basename(params['pathfdr'])
bm.Delineation_Initial_Subbasins_Without_Lakes(
fac_thresold = facthreshold,
mode=mode,
max_memroy= maxmemory,
path_flow_dirction = os.path.join(datafolder, "flow_direction", fdrname),
gis_platform="qgis",
)
print('Delineation_Initial_Subbasins_Without_Lakes was successful...\n')
except Exception as e:
print('Delineation_Initial_Subbasins_Without_Lakes failed...')
print(e)
end = time.time()
print("Delineation_Initial_Subbasins_Without_Lakes took", end - start, "seconds")
#Add lake and obs control points
def addOutletPts():
lakesname = ntpath.basename(params['pathlakes'])
pointsinterestname = ntpath.basename(params['pathpointsinterest'])
lakeid = params['lakeid']
laketype = params['laketype']
lakevol = params['lakevol']
lakeavgdepth = params['lakeavgdepth']
lakearea = params['lakearea']
poiid = params['poiid']
poiname = params['poiname']
poindrainage = params['poidrainarea']
poinsource = params['poisource']
if params['pathlakes'] !='#':
lake_attributes=[lakeid, laketype, lakevol, lakeavgdepth, lakearea]
else:
lake_attributes=[]
start = time.time()
try:
print('\nAdd_New_Subbasin_Outlet_Points running...\n')
if params['pathlakes'] !='#':
bm.Add_New_Subbasin_Outlet_Points(
path_lake_polygon=os.path.join(datafolder,"lakes",lakesname),
lake_attributes=lake_attributes,
connected_lake_area_thresthold=float(params['connectedlake']),
non_connected_lake_area_thresthold=float(params['nonconnectedlake']),
path_point_of_interest=os.path.join(os.getcwd(),datafolder,'stations',pointsinterestname),
point_of_interest_attributes=[poiid, poiname, poindrainage, poinsource],
max_memroy=maxmemory,
gis_platform="qgis",
)
else:
bm.Add_New_Subbasin_Outlet_Points(
path_point_of_interest=os.path.join(os.getcwd(),datafolder,'stations',pointsinterestname),
point_of_interest_attributes=[poiid, poiname, poindrainage, poinsource],
max_memroy=maxmemory,
gis_platform="qgis",
)
print('Add_New_Subbasin_Outlet_Points was successful...\n')
except Exception as e:
print('Add_New_Subbasin_Outlet_Points failed...')
print(e)
end = time.time()
print("Add_New_Subbasin_Outlet_Points took", end - start, "seconds")
#Add hydrology related attributes
def genHydroRoutingAtt():
bankfullname = ntpath.basename(params['pathbankfull'])
landuserast = ntpath.basename(params['pathlanduserast'])
manningtablename = ntpath.basename(params['landusemanning'])
poiid = params['poiid']
poiname = params['poiname']
poindrainage = params['poidrainarea']
poinsource = params['poisource']
lakeid = params['lakeid']
laketype = params['laketype']
lakevol = params['lakevol']
lakeavgdepth = params['lakeavgdepth']
lakearea = params['lakearea']
if ntpath.basename(params['pathlakes']) != '#':
lakeattributes=[lakeid, laketype, lakevol,lakeavgdepth, lakearea]
else:
lakeattributes=[]
projected_epsg_code = str(params['epsgcode'])
if params['pathlanduserast'] != '#':
path_landuse=os.path.join(datafolder,"landuse", landuserast)
path_manning_table = os.path.join(datafolder,"landuse", manningtablename)
else:
path_landuse = params['pathlanduserast']
path_manning_table = '#'
start = time.time()
try:
print('\nGenerate_Hydrologic_Routing_Attributes running...\n')
if params['pathbankfull'] != '#':
width = params['bankfullwidth']
depth = params['bankfulldepth']
discharge = params['bankfulldischarge']
drainage = params['bankfulldrainage']
bm.Generate_Hydrologic_Routing_Attributes(
path_bkfwidthdepth_polyline=os.path.join(datafolder,"bkf_width",bankfullname),
bkfwd_attributes=[width, depth, discharge, drainage],
path_landuse= path_landuse,
path_landuse_and_manning_n_table = path_manning_table,
gis_platform="qgis",
point_of_interest_attributes=[poiid, poiname, poindrainage, poinsource],
lake_attributes=lakeattributes,
path_output_folder=path_output_folder,
projected_epsg_code =projected_epsg_code,
)
else:
bm.Generate_Hydrologic_Routing_Attributes(
gis_platform="qgis",
path_landuse= path_landuse,
path_landuse_and_manning_n_table = path_manning_table,
point_of_interest_attributes=[poiid, poiname, poindrainage, poinsource],
lake_attributes=[lakeid, laketype, lakearea, lakevol, lakeavgdepth],
path_output_folder=path_output_folder,
projected_epsg_code =projected_epsg_code,
k = float(params['kcoef']),
c = float(params['ccoef'])
)
print('Generate_Hydrologic_Routing_Attributes was successful...\n')
except Exception as e:
print('Generate_Hydrologic_Routing_Attributes failed...')
print(e)
end = time.time()
print("Generate_Hydrologic_Routing_Attributes took", end - start, "seconds")
#Combine catchment covered by the same lake
def combinecatchment():
start = time.time()
try:
print('\Combine_Subbasins_Covered_by_The_Same_Lake running...\n')
bm.Combine_Subbasins_Covered_by_The_Same_Lake(
gis_platform="qgis",
routing_product_folder = path_output_folder,
)
print('Combine_Subbasins_Covered_by_The_Same_Lake was successful...\n')
except Exception as e:
print('Combine_Subbasins_Covered_by_The_Same_Lake failed...')
print(e)
end = time.time()
print("Combine_Subbasins_Covered_by_The_Same_Lake took", end - start, "seconds")
# Remove small lakes
def removesmalllakes():
input_routing_product_folder=path_output_folder
folder_product_after_filter_lakes = os.path.join(os.getcwd(),'OIH_Output','network_after_filter_lakes')
filterconnectedlakes = float(params['filterconnectedlakes'])
filternonconnectedlakes = float(params['filternonconnectedlakes'])
selectedlakeid = params['selectedlakeid']
if selectedlakeid != '#':
selectedlakeid = [x.strip() for x in selectedlakeid.split(",")]
else:
selectedlakeid = []
start = time.time()
try:
print('\Remove_Small_Lakes running...\n')
bm_post.Remove_Small_Lakes(
path_output_folder = folder_product_after_filter_lakes,
routing_product_folder = input_routing_product_folder,
connected_lake_area_thresthold=filterconnectedlakes,
non_connected_lake_area_thresthold=filternonconnectedlakes,
selected_lake_ids = selectedlakeid,
gis_platform="qgis",
)
print('Remove_Small_Lakes was successful...\n')
except Exception as e:
print('Remove_Small_Lakes failed...')
print(e)
end = time.time()
print("Remove_Small_Lakes took", end - start, "seconds")
#Increase catchment area
def increaseCatchArea():
input_routing_product_folder = os.path.join(os.getcwd(),'OIH_Output','network_after_filter_lakes')
folder_product_after_increase_catchment = os.path.join(os.getcwd(),'OIH_Output','network_after_increase_catchment')
minsubbasinarea = float(params['minsubbasinarea'])
start = time.time()
try:
print('\Increase_catchment_area running...\n')
bm_post.Decrease_River_Network_Resolution(
path_output_folder = folder_product_after_increase_catchment,
routing_product_folder = input_routing_product_folder,
minimum_subbasin_drainage_area= minsubbasinarea,
gis_platform="qgis",
)
print('Increase_catchment_area was successful...\n')
except Exception as e:
print('Increase_catchment_area failed...')
print(e)
end = time.time()
print("Increase_catchment_area took", end - start, "seconds")
#Generate HRUs
def generateHRUs():
folder_product_after_increase_catchment=os.path.join(os.getcwd(),'OIH_Output','network_after_increase_catchment')
input_routing_product_folder=folder_product_after_increase_catchment
folder_product_after_gen_hrus=os.path.join(os.getcwd(),'OIH_Output','network_after_gen_hrus')
demname = ntpath.basename(params['pathdem'])
landusepoly = ntpath.basename(params['pathlandusepoly'])
landuseinfo = ntpath.basename(params['pathlanduseinfo'])
soilpoly = ntpath.basename(params['pathsoil'])
soilinfo = ntpath.basename(params['pathsoilinfo'])
veginfo = ntpath.basename(params['pathveginfo'])
projected_epsg_code = str(params['epsgcode'])
start = time.time()
try:
print('\Generate_HRUs running...\n')
if params['pathlakes'] != '#':
if os.path.isfile(os.path.join(input_routing_product_folder, "sl_connected_lake.shp")):
connected_lakes = os.path.join(input_routing_product_folder, "sl_connected_lake.shp")
else:
connected_lakes = '#'
if os.path.isfile(os.path.join(input_routing_product_folder, "sl_non_connected_lake.shp")):
non_connected_lakes = os.path.join(input_routing_product_folder, "sl_non_connected_lake.shp")
else:
non_connected_lakes = '#'
bm_post.Generate_HRUs(
path_output_folder=folder_product_after_gen_hrus,
path_subbasin_polygon =os.path.join(input_routing_product_folder, "finalcat_info.shp"),
path_connect_lake_polygon = connected_lakes,
path_non_connect_lake_polygon= non_connected_lakes,
path_landuse_polygon=os.path.join(os.getcwd(),'Data','landuse', landusepoly),
path_soil_polygon =os.path.join(os.getcwd(),'Data','soil',soilpoly),
#path_vegetation_polygon ="#",
#path_other_polygon_1="#",
#path_other_polygon_2="#",
path_landuse_info=os.path.join(os.getcwd(),'Data','landuse', landuseinfo),
path_soil_info =os.path.join(os.getcwd(),'Data','soil', soilinfo),
path_veg_info =os.path.join(os.getcwd(),'Data','landuse', veginfo),
path_to_dem=os.path.join(os.getcwd(),datafolder,'DEM',demname),
importance_order = ['Soil_ID','Landuse_ID'],
min_hru_subbasin_area_ratio = 0.0,
gis_platform="qgis",
projected_epsg_code = projected_epsg_code,
)
else:
bm_post.Generate_HRUs(
path_output_folder=folder_product_after_gen_hrus,
path_subbasin_polygon =os.path.join(input_routing_product_folder, "finalcat_info.shp"),
path_landuse_polygon=os.path.join(os.getcwd(),'Data','landuse', landusepoly),
path_soil_polygon =os.path.join(os.getcwd(),'Data','soil',soilpoly),
#path_vegetation_polygon ="#",
#path_other_polygon_1="#",
#path_other_polygon_2="#",
path_landuse_info=os.path.join(os.getcwd(),'Data','landuse', landuseinfo),
path_soil_info =os.path.join(os.getcwd(),'Data','soil', soilinfo),
path_veg_info =os.path.join(os.getcwd(),'Data','landuse', veginfo),
path_to_dem=os.path.join(os.getcwd(),datafolder,'DEM',demname),
importance_order = ['Soil_ID','Landuse_ID'],
min_hru_subbasin_area_ratio = 0.0,
gis_platform="qgis",
projected_epsg_code = projected_epsg_code,
)
print('Generate_HRUs was successful...\n')
except Exception as e:
print('Generate_HRUs failed...')
print(e)
end = time.time()
print("Generate_HRUs took", end - start, "seconds")
#Generate the Raven RVH files
def generateRavenFiles():
folder_product_after_filter_lakes=os.path.join(os.getcwd(),'OIH_Output','network_after_gen_hrus')
input_routing_product_folder=folder_product_after_filter_lakes
raven_model_dir = folder_product_after_filter_lakes
modelname = params['modelname']
try:
print('Generate_Raven_Model_Inputs running...\n')
bm_post.Generate_Raven_Model_Inputs(
path_hru_polygon = os.path.join(input_routing_product_folder, "finalcat_hru_info.shp"),
model_name = modelname,
subbasingroup_names_channel =["Allsubbasins"],
subbasingroup_length_channel =[-1],
subbasingroup_name_lake =["AllLakesubbasins"],
subbasingroup_area_lake =[-1],
path_output_folder = raven_model_dir,
aspect_from_gis = 'grass',
)
print('Raven model inputs were successfully generated!\n')
except Exception as e:
print('Generate Raven model inputs failed...')
print(e)
#Opens the parameters file created by QRaven and imports them into a dictionary
params = {}
with open("/root/BasinMaker/parameters.txt") as f:
for line in f:
(key, val) = line.split()
params[key] = val
path_folder_to_save_intermediate_outputs = os.path.join(os.getcwd(),'bm_tmp')
datafolder = os.path.join(os.getcwd(),'Data')
path_output_folder = os.path.join(os.getcwd(),'OIH_Output','network_without_simplification')
bm = basinmaker.delineate(path_working_folder=path_folder_to_save_intermediate_outputs)
bm_post = basinmaker.postprocess()
maxmemory = params['maxmemory']
defineExtent()
delineateNoLakes()
addOutletPts()
genHydroRoutingAtt()
combinecatchment()
removesmalllakes()
increaseCatchArea()
generateHRUs()
generateRavenFiles()