-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
298 lines (243 loc) · 9.56 KB
/
setup.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
# -*- coding: utf-8 -*-
"""
Created: 7/1/2019
License: Creative Commons Attribution 4.0 International (CC BY 4.0)
http://creativecommons.org/licenses/by/4.0/
Python version: Tested on Python 3.7x (x64)
PURPOSE
------------------------------------------------------------------------------
[Floodplain and Channel Evaluation Toolkit: Setup Script]
FACET is a standalone Python tool that uses open source modules to map the
floodplain extent and compute stream channel and floodplain geomorphic metrics
such as channel width, streambank height, active floodplain width,
and stream slope from DEMs.
------------------------------------------------------------------------------
"""
from pathlib import Path
from timeit import default_timer as timer
import argparse
import configparser
import json
import multiprocessing as mp
import os
import shutil
import subprocess
import sys
import zipfile
import requests
import config
def json_to_dict(project_dir):
"""
Reads science base json listing for FACET Ancillary Data
and returns a dictionary of urls of ancillary datasets
The datasets parsed and organized based on their type into
a new dictionary
"""
json_urls = [
"https://www.sciencebase.gov/catalog/item/5d949474e4b0c4f70d0db64a?format=json",
"https://www.sciencebase.gov/catalog/item/5d949489e4b0c4f70d0db64d?format=json",
"https://www.sciencebase.gov/catalog/item/5d94949de4b0c4f70d0db64f?format=json"
]
# new dict
files = {}
for json_url in json_urls:
try:
r = requests.get(json_url, timeout=5)
r.raise_for_status()
except requests.exceptions.HTTPError as err_h:
print("Http Error:", err_h)
sys.exit(1)
except requests.exceptions.ConnectionError as err_c:
print("Error Connecting:", err_c)
sys.exit(1)
except requests.exceptions.Timeout as err_t:
print("Timeout Error:", err_t)
sys.exit(1)
except requests.exceptions.RequestException as err:
print(err)
sys.exit(1)
# load json
tmp_json = json.loads(r.text)
# loop & extract ancillary zip files
for f in tmp_json['files']:
if f['contentType'] == 'application/zip':
f_dir = project_dir / 'ancillary_data'
file = f_dir / f['name']
shp = f_dir / f'{file.stem}.shp'
# update the new dict
files[f['title']] = {
'fname': f['name'],
'download': f['downloadUri'],
'f_dir': f_dir,
'file': file,
'shp': shp
}
return files
def get_data(args):
"""
Pass url to download the file through stream and unzip
"""
v = args
url, f_dir, file, shp = v['download'], v['f_dir'], v['file'], v['shp']
n = file.name
# create folders:
if not f_dir.is_dir():
os.makedirs(f_dir)
if shp.is_file():
# print(shp, ': exists!')
return
try:
with requests.get(url, stream=True) as r:
with open(file, 'wb') as f:
shutil.copyfileobj(r.raw, f)
print('Download Successful: ', n)
except:
print(f'Failed: {n}. Unable to retrieve the file!')
try:
archive = zipfile.ZipFile(file)
archive.extractall(f_dir)
print('Extract Successful: ', n)
except:
print('Failed to extract: ', n)
def validate_TauDEM():
"""
run TauDEM with incomplete/missing args to retrieve
an error message to validate install. Replace with more
substantial test later
"""
try:
msg_bytes = subprocess.check_output('mpiexec -n 2 AreaD8')
#bytes to string + slice 'Error'
msg_str = msg_bytes.decode()[0:5]
if msg_str == 'Error':
print('Call to TauDEM via mpiexec successfully')
return True
except:
print('Call to TauDEM via mpiexec failed. Please recheck and install TauDEM and its dependencies')
sys.exit(1)
def update_config(ini, data_dict, project_dir):
"""update ini file with info from data dict"""
config_file = configparser.ConfigParser(
comment_prefixes=';',
inline_comment_prefixes=';',
allow_no_value=True,
delimiters=':'
)
config_file.read(Path(ini))
# get CBW and DRB physio files
CBW_Physio = list(project_dir.rglob('*physiographic_regions_CBW.shp'))[0]
DRB_Physio = list(project_dir.rglob('*physiographic_regions_DRB.shp'))[0]
# update paths and flags
if validate_TauDEM() == True:
config_file.set('pre process dem', 'taudem', str(True))
config_file.set('pre process dem', 'taudem cores', str(mp.cpu_count()))
else:
config_file.set('pre process dem', 'taudem', str(False))
if CBW_Physio.is_file():
config_file.set('file paths', 'physio cbw', str(CBW_Physio))
if DRB_Physio.is_file():
config_file.set('file paths', 'physio drb', str(DRB_Physio))
if data_dict['census_roads']['shp'].is_file():
config_file.set('file paths', 'census roads', str(data_dict['census_roads']['shp']))
if data_dict['census_rails']['shp'].is_file():
config_file.set('file paths', 'census rails', str(data_dict['census_rails']['shp']))
# update ancillary dir
ancil_dir = list(project_dir.rglob('*ancillary_data'))[0]
if ancil_dir.is_dir():
config_file.set('file paths', 'ancillary dir', str(ancil_dir))
with open(ini, 'w') as cfile:
config_file.write(cfile)
return config_file
def main():
intro_msg = '''
+-+-+-+-+-+ +-+-+-+-+-+
|F|A|C|E|T| |S|E|T|U|P|
+-+-+-+-+-+ +-+-+-+-+-+
setup.py script will automatically download ancillary datasets needed by FACET from Science Base.
It also updates the config file with ancillary data file paths as well.
The script takes anywhere from 30 minutes to 1-2 hours to run successfully depending on your internet speed.\n
'''
start = timer()
# arg parser
parser = argparse.ArgumentParser(
prog='FACET set-up script',
description=intro_msg
)
# user arg defined project path
parser.add_argument('--path', '-p', help='full path for project', required=True)
args = parser.parse_args()
# intro message
print(intro_msg)
# project pathname
project_dir = Path(args.path)
# config file path
config_ini_file = config.get_config_path()
# get science base json data
sb_json = json_to_dict(project_dir)
### STEP 1
# download, extract and clean up
st_step_01 = timer()
print('''
Step #1: Downloading and extracting ancillary datasets. This will take 30-45 mins. Please wait.
''')
# number of items to download at any given time
no_of_items = int(len(sb_json)/3)
pool = mp.Pool(processes=no_of_items)
pool.map(get_data, [(v) for v in sb_json.values()])
pool.close()
end_step_01 = round((timer()-st_step_01)/60.0, 2)
print(f'Step #1 complete. Time elapsed: {end_step_01} mins\n')
### STEP 2
st_step_02 = timer()
# update and save config file
print('Step #2: Updating config file and deleting temp files.')
ini_dict = update_config(config_ini_file, sb_json, project_dir)
# clean up zip files
for v in sb_json.values():
if v['file'].is_file():
os.remove(v['file'])
# result output for users
end_step_02 = round((timer()-st_step_02)/60.0, 2)
print(f'Step #2 complete. Time elapsed: {end_step_02} mins\n')
files_downloaded = list(project_dir.rglob('*/ancillary_data/*.shp'))
end = round((timer()-start)/60.0, 2)
# message 1
msg_1 = '''
Setup complete. See summary below:
1. Files downloaded:
'''
# message 2
msg_2 = f'''
2. Following variables were updated in config file:
taudem: {ini_dict['pre process dem']['taudem']}
taudem: {ini_dict['pre process dem']['taudem cores']}
physio cbw: {ini_dict['file paths']['physio cbw']}
physio drb: {ini_dict['file paths']['physio drb']}
census roads: {ini_dict['file paths']['census roads']}
census rails: {ini_dict['file paths']['census rails']}
*************************************************
*** CAUTION: READ THE SECTION BELOW CAREFULLY ***
*************************************************
Users need to open config.ini, review and edit the following parameters.
For more details please refer to README, it will provide a detailed overview of each
parameter and if it can be modified or not:
* data_dir: This is a sub-directory inside your project directory where your data is stored
* spatial ref: needs to be in PROJ.4 string (see README)
* Under 'pre process dem':
- taudem cores (default is set to using all cores, but users can manually change it)
- resample resolution (see README)
* breach options: 'pre-condition dem & fast-breach' is the default option (see README)
* post processing: 'r exe path' needs to be changed based on your local installation of R
DO NOT update values under the following subsections in config.ini (see README):
* reach and order
* cross section method
* width from curvature via buff. method
Total time elapsed: {end} mins'''
for i in [msg_1, files_downloaded, msg_2]:
if isinstance(i, str):
print(i)
if isinstance(i, list):
for x in i:
print(f'\t{x}')
if __name__ == '__main__':
main()