forked from ssmall41/FloodForecasters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecaster_group_example.py
86 lines (71 loc) · 2.09 KB
/
forecaster_group_example.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
import time
import subprocess
import sys
def CheckHaltFile(haltfilename):
halt = 0
with open(haltfilename,'r') as haltfile:
halt = int(haltfile.readline())
return halt
np = sys.argv[1]
#Number of forecasters in the group
num_forecasters = 1
#Filenames
haltfilename = 'examples/fgroup/halt'
timesfilename = 'examples/fgroup/times'
ptimesfilename = 'examples/fgroup/output_'
#Create halt file
halt = 0
with open(haltfilename,'w') as haltfile:
haltfile.write(str(halt))
#Get the first batch of init and final times
with open(timesfilename) as infile:
old_times = []
for line in infile:
if line.strip():
old_times.append([int(x) for x in line.split()])
#Check that enough initial timestamps are set in the times file
N = len(old_times)
if N != num_forecasters:
print 'Error: expected',num_forecasters,'forecasters. Got data for ',N
sys.exit(1)
while(halt == 0):
#Call programs #######################
idx = -1
#0: Toplayer - IFC (ifc1c)
idx += 1
cmd = 'mpirun -np '+str(np)+' ./FORECASTER_MAPS_END examples/GlobalForecast262_ifc1c.gbl examples/fcast_file.fcst '+str(old_times[idx][0])+' '+str(old_times[idx][1])+' '+ptimesfilename+str(idx)+' '+str(old_times[idx][0]-3600)+' 0 0'
print '\nRunning command',cmd
sys.stdout.flush()
time.sleep(1)
subprocess.call(cmd,shell=True)
#Create new times files
new_times = []
outfile = open(timesfilename,'w')
for i in range(N):
with open(ptimesfilename+str(i),'r') as infile:
for line in infile:
towrite = ''
holder = []
for x in line.split():
holder.append(int(x))
holder.append(int(x))
towrite = towrite + x + ' '
new_times.append([x for x in holder])
outfile.write(towrite+towrite+'\n')
outfile.close()
print 'Got',new_times
sys.stdout.flush()
#Check the halt file
halt = CheckHaltFile(haltfilename)
#Check if any progress was made
if halt == 0:
if old_times == new_times:
print 'No progress made. Sleeping...'
sys.stdout.flush()
time.sleep(10*60)
halt = CheckHaltFile(haltfilename)
else:
print 'Going for the next round'
sys.stdout.flush()
old_times = new_times
print 'Halt signal received'