-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_download_cmd.py
63 lines (40 loc) · 1.36 KB
/
write_download_cmd.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
import os
from cli import parse_write_download_cmd
def mk_climate_maca_wget_sh(maca_path, out_dir):
'''
Make a sh file using wget to download all MACA downscaled climate data.
'''
maca_name = os.path.basename(maca_path).split('_')[0]
f = open(maca_path)
f_lines = f.readlines()
f.close()
command = "#!/bin/bash \n"
for line in f_lines:
line = line.strip()
if line == '':
continue
out_fl = line.split('/')[-1].split('?')[0]
subfldr_lst = out_fl.split('_')
subfldr_1 = out_dir + '/' + subfldr_lst[2] + '_' + subfldr_lst[0]
print(subfldr_1)
if not os.path.exists(subfldr_1):
os.mkdir(subfldr_1)
subfldr_2 = subfldr_1 + '/' + subfldr_lst[4].upper()
print(subfldr_2)
if not os.path.exists(subfldr_2):
os.mkdir(subfldr_2)
out_fl_path = subfldr_2 + '/' + out_fl
command += 'wget -nc -c -nd "{0}" -O {1} \n'.format(line, out_fl_path)
output = open('./{}_wget.sh'.format(maca_name), 'x')
output.write(command)
output.close()
print('./{}_wget.sh saved'.format(maca_name))
return()
def main():
args = parse_write_download_cmd()
in_fl = args.in_file
out_dir = args.out_dir
mk_climate_maca_wget_sh(in_fl, out_dir)
return()
if __name__ == '__main__':
main()