-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·125 lines (105 loc) · 3.58 KB
/
run.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
#!/usr/bin/env python2
"""
Run code, analyis or build documents related to this repository
Usage:
run.py SAC [--mpi --np=NP]
run.py gdf [--mpi --np=NP]
run.py fluxcalc [--mpi --np=NP]
run.py surfflux (--tube-r=R) [--mpi --np=NP]
run.py analysis (--tube-r=R) [--mpi --np=NP]
run.py notebook [--port=PORT]
run.py download (ini)
Options:
--mpi Use MPI, i.e. call mpiexec.
--np=NP Number of processors to use for MPI
--tube-r=R Specify the Flux Surface radius to use.
--port=PORT Notebook port. [default: 8899]
"""
import os
import sys
import bz2
import subprocess
try:
import docopt
except ImportError:
from scripts.extern import docopt
try:
import progressbar
except ImportError:
from scripts.extern import progressbar
try:
import requests
except ImportError:
from scripts.extern import requests
os.chdir(os.path.dirname(os.path.realpath(__file__)))
arguments = docopt.docopt(__doc__, version='1.0.0')
from scripts import sacconfig
cfg = sacconfig.SACConfig()
#MPI Exec function
def mpi_exec(arguments, command):
if not arguments['--mpi']:
os.system(command)
if arguments['--mpi'] and arguments['--np']:
os.system('mpirun -np %s %s'%(arguments['--np'], command))
if arguments['--mpi'] and arguments['--np'] is None:
os.system('mpirun -np {} {}'.format(cfg.mpi_size, command))
#Run SAC
if arguments['SAC'] == 'SAC':
os.chdir("sac/sac")
mpi_exec(arguments, './vac < vac.par')
os.chdir("../")
#Run gdf translator
if arguments['gdf'] or arguments['SAC'] == 'gdf':
os.chdir("scripts")
mpi_exec(arguments, 'python gdf_converter_mpi.py')
os.chdir("../")
#Run Analysis
if arguments['analysis']:
os.chdir("analysis")
mpi_exec(arguments, './surface_analysis_mpi.py --tube-r=%s'%arguments['--tube-r'])
os.chdir("../")
#Run Flux Calcs
if arguments['surfflux']:
os.chdir("analysis")
mpi_exec(arguments, './surface_flux.py --tube-r=%s'%arguments['--tube-r'])
os.chdir("../")
#Run Flux Calcs
if arguments['fluxcalc'] or arguments['SAC'] == 'fluxcalc':
os.chdir("analysis")
mpi_exec(arguments, './wave_flux.py')
os.chdir("../")
#Start notebook sever
if arguments['notebook'] or arguments['SAC'] == 'notebook':
if arguments['--port'] is None:
arguments['--port'] = 8899
import IPython
IPython.start_ipython(['notebook', '--notebook-dir=analysis/notebooks',
'--port={}'.format(arguments['--port'])])
#Download data
if arguments['download'] or arguments['SAC'] == 'download':
ini_data_url = 'http://files.figshare.com/1905552/3D_tube_128_128_128.ini.bz2'
if arguments['ini']:
data_url = ini_data_url
filename = os.path.join(cfg.ini_dir, '3D_tube_128_128_128.ini')
else:
print "No Download Specified. Try run.py download ini"
sys.exit()
CHUNK_SIZE = 1024 * 1024 # 1MB
#Open file and get total size
r = requests.get(data_url)
total_size = int(r.headers['content-length'])
#Create a progressbar
print "Downloading and decompressing data:"
pbar = progressbar.ProgressBar(widgets=[progressbar.Percentage(), progressbar.Bar()], maxval=total_size).start()
#Create a stream bz2 decompressor
bz = bz2.BZ2Decompressor()
#Initial postition of file is 0
pos = 0
#Open the file and read and decompress chunk by chunk:
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
f.write(bz.decompress(chunk))
#position is len of downloaded (compressed) file
pos += len(chunk)
pbar.update(pos)
pbar.finish()