-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconstruct.py
More file actions
417 lines (350 loc) · 14.6 KB
/
Copy pathconstruct.py
File metadata and controls
417 lines (350 loc) · 14.6 KB
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
413
414
415
416
417
try:
from fabsim.base.fab import *
except ImportError:
from base.fab import *
# Import V&V primitives.
try:
import fabsim.VVP.vvp as vvp
except ImportError:
import VVP.vvp as vvp
try:
from FabFlee import *
except ImportError:
from FabFlee import *
import glob
import csv
import os
import numpy as np
import pandas as pd
from shutil import copyfile, rmtree, move
# Add local script, blackbox and template path.
add_local_paths("FabFlee")
@task
# Syntax: fabsim localhost clear_active_conflict
def clear_active_conflict():
"""
Delete all content in the active conflict directory.
"""
local(template("rm -rf %s/conflict_data/active_conflict/"
% (get_plugin_path("FabFlee"))))
@task
# Syntax: fabsim localhost load_conflict:<conflict_name>
def load_conflict(conflict_name):
"""
Load source data and flee csv files for a specific conflict from
conflict data to active conflict directory.
"""
# copies *.csv files from $FabFlee/conflict_data/<conflict_name> to
# $FabFlee/conflict_data/active_conflict.
# 1. Load locations.csv, routes.csv and closures.csv files which
# correspond to a specific conflict.
# These CSV will be store in $FabFlee/conflict_data. Each conflict will be
# stored in a separate folder.
# 2. Move these CSVs to an "active_conflict" directory.
# This is located in $FABSIM/conflict_data/active_conflict.
local(template("mkdir -p %s/conflict_data/active_conflict"
% (get_plugin_path("FabFlee"))))
local(template("cp %s/conflict_data/%s/*.csv \
%s/conflict_data/active_conflict/")
% (get_plugin_path("FabFlee"), conflict_name,
get_plugin_path("FabFlee")))
local(template("mkdir -p %s/conflict_data/active_conflict/source_data"
% (get_plugin_path("FabFlee"))))
local(template("cp %s/conflict_data/%s/source_data/*.csv \
%s/conflict_data/active_conflict/source_data/")
% (get_plugin_path("FabFlee"), conflict_name,
get_plugin_path("FabFlee")))
local(template("cp %s/config_files/run.py \
%s/conflict_data/active_conflict")
% (get_plugin_path("FabFlee"), get_plugin_path("FabFlee")))
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost load_conflict:%s\n" % conflict_name)
@task
# Syntax: fabsim localhost add_camp:camp_name,region,country(,lat,lon)
def add_camp(camp_name, region=" ", country=" ", lat=0.0, lon=0.0):
"""
Add an additional new camp to locations.csv.
"""
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost add_camp:%s\n" % camp_name)
# 1. Add (or make existing forwarding hub) a new camp to locations.csv
# If new camp, add country,lat,lon,location_type(camp)
# If existing camp, change location_type to camp
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee")), "r"))
lines = [l for l in r]
for i in range(1, len(lines)):
if lines[i][0].strip() != camp_name:
continue
print("Warning: camp %s is already present in locations.csv."
% (camp_name))
return
# 2. Append one line to lines, containing the details of the new camp.
add_camp = [camp_name, region, country, lat, lon, "camp"]
with open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee")), "a") as new_csv:
writer = csv.writer(new_csv)
writer.writerow(add_camp)
print(add_camp)
@task
# Syntax: fabsim localhost delete_location:<location_name>
def delete_location(location_name):
"""
Deletes not-required camp (or location) from locations.csv.
"""
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost delete_location:%s\n" % location_name)
# 1. Delete camp from locations.csv containing the details of the camp.
# 2. Write the updated CSV file.
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee")), "r"))
lines = [l for l in r]
writer = csv.writer(open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee")), "w"))
for i in range(0, len(lines)):
if lines[i][0].strip() != location_name:
writer.writerow(lines[i])
continue
print(lines[i])
# 3. Check whether wanted to delete camp is present in locations.csv
for i in range(1, len(lines)):
if lines[i][0] == location_name:
continue
print("Warning: camp %s is deleted from locations.csv."
% (location_name))
return
@task
# Syntax: fabsim localhost change_distance:name1,name2,distance
def change_distance(source, destination, distance):
"""
Change distance between two locations in routes.csv.
"""
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost change_distance:%s,%s,%s\n"
% (source, destination, distance))
# 1. Read routes.csv and for each location in the dict, find in the csv,
# and change distance between two locations.
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/routes.csv"
% (get_plugin_path("FabFlee"))))
lines = [l for l in r]
for i in range(1, len(lines)):
if lines[i][0].strip() != source:
continue
if lines[i][1].strip() != destination:
continue
lines[i][2] = distance
print(lines[i])
# 2. Write the updated closures.csv in the active_conflict directory.
writer = csv.writer(open("%s/conflict_data/active_conflict/routes.csv"
% (get_plugin_path("FabFlee")), "w"))
writer.writerows(lines)
@task
# Syntax: fabsim localhost close_camp:camp_name,country(,closure_start,closure_end)
def close_camp(camp_name, country, closure_start=0, closure_end=-1):
"""
Close camp located within neighbouring country.
"""
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost close_camp:%s,%s\n" % (camp_name, country))
# 1. Change closure_start and closure_end or add a new
# camp closure to closures.csv.
# Format: closure type <location>,name1,name2,closure_start,closure_end
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/closures.csv"
% (get_plugin_path("FabFlee")))) # Here your csv file
lines = [l for l in r]
camp_found = False
for i in range(1, len(lines)):
if lines[i][0].strip() != "location":
continue
if lines[i][1].strip() != camp_name:
continue
if lines[i][2].strip() != country:
continue
lines[i][3] = closure_start
lines[i][4] = closure_end
camp_found = True
print(lines[i])
if not camp_found:
lines.append(["location", camp_name, country,
closure_start, closure_end])
# print(lines)
# 2. Write the updated closures.csv in the active_conflict directory.
writer = csv.writer(open("%s/conflict_data/active_conflict/closures.csv"
% (get_plugin_path("FabFlee")), "w"))
writer.writerows(lines)
@task
# Syntax: fabsim localhost close_border:country1,country2(,closure_start,closure_end)
def close_border(country1, country2, closure_start=0, closure_end=-1):
"""
Close border between conflict country and camps located
within specific neighbouring country.
"""
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost close_border:%s,%s\n"
% (country1, country2))
# 1. Change closure_start and closure_end or add a new camp
# closure to closures.csv.
# Format: closure type <country>,name1,name2,closure_start,closure_end
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/closures.csv"
% (get_plugin_path("FabFlee"))))
lines = [l for l in r]
border_found = False
for i in range(1, len(lines)):
if lines[i][0].strip() != "country":
continue
if lines[i][1].strip() != country1:
continue
if lines[i][2].strip() != country2:
continue
lines[i][3] = closure_start
lines[i][4] = closure_end
border_found = True
print(lines[i])
if not border_found:
lines.append(["country", country1, country2,
closure_start, closure_end])
"""
local(template("cp %s/conflict_data/%s/*.csv \
%s/conflict_data/active_conflict/")
% (get_plugin_path("FabFlee"), conflict_name,
get_plugin_path("FabFlee")))
print(lines)
"""
# 2. Write the updated closures.csv in the active_conflict directory.
writer = csv.writer(open("%s/conflict_data/active_conflict/closures.csv"
% (get_plugin_path("FabFlee")), "w"))
writer.writerows(lines)
@task
# Syntax: fabsim localhost change_capacities:camp_name=capacity(,camp_name2=capacity2)
def change_capacities(**capacities):
"""
Change the capacity of a set of camps in the active conflict directory.
"""
# Note: **capacities will be a Python dict object.
capacities_string = ""
for c in capacities.keys():
capacities_string += "%s=%s" % (c, capacities[c])
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost change_capacities:%s\n"
% capacities_string)
# 1. Read in locations.csv
# 2. for each location in the dict, find it in the csv, and modify the
# population value accordingly.
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee"))))
lines = [l for l in r]
for camp_name in capacities.keys():
for i in range(1, len(lines)):
if lines[i][5].strip() != "camp":
continue
if lines[i][0].strip() != camp_name:
continue
lines[i][7] = capacities[camp_name]
print(lines[i])
# 3. Write the updated CSV file.
writer = csv.writer(open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee")), "w"))
writer.writerows(lines)
@task
# Syntax: fabsim localhost redirect:location_name1,location_name2
def redirect(source, destination):
"""
Redirect from town or (small/other)camp to (main)camp.
"""
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fabsim localhost redirect:%s,%s\n" % (source, destination))
# 1. Read locations.csv and for each location in the dict, find in the csv,
# and redirect refugees from location in neighbouring country to camp.
# 2. Change location_type of source location to forwarding_hub.
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee"))))
lines = [l for l in r]
for i in range(1, len(lines)):
if lines[i][0].strip() != source:
continue
lines[i][5] = "forwarding_hub"
print(lines[i])
# 3. Write the updated CSV file.
writer = csv.writer(open("%s/conflict_data/active_conflict/locations.csv"
% (get_plugin_path("FabFlee")), "w"))
writer.writerows(lines)
# 4. Find the route from source to destination in routes.csv, and enable
# forced_redirection.
r = csv.reader(open("%s/conflict_data/active_conflict/routes.csv"
% (get_plugin_path("FabFlee"))))
lines = [l for l in r]
for i in range(1, len(lines)):
if lines[i][0].strip() != source:
continue
if lines[i][1].strip() != destination:
continue
lines[i][3] = "2"
print(lines[i])
for i in range(1, len(lines)):
if lines[i][0].strip() != destination:
continue
if lines[i][1].strip() != source:
continue
lines[i][3] = "1"
print(lines[i])
# 5. Write the updated CSV file.
writer = csv.writer(open("%s/conflict_data/active_conflict/routes.csv"
% (get_plugin_path("FabFlee")), "w"))
writer.writerows(lines)
@task
# Syntax: fabsim localhost add_new_link:<name1>,<name2>,<distance>
def add_new_link(name1, name2, distance):
"""
Add a new link between locations to routes.csv.
"""
with open("%s/conflict_data/active_conflict/commands.log.txt"
% (get_plugin_path("FabFlee")), "a") as myfile:
myfile.write("fab localhost add_new_link:%s,%s,%s\n"
% (name1, name2, distance))
# 1. Read routes.csv and for each location in the dict, find in the csv,
# and change distance between two locations.
import csv
r = csv.reader(open("%s/conflict_data/active_conflict/routes.csv"
% (get_plugin_path("FabFlee"))))
lines = [l for l in r]
for i in range(1, len(lines)):
if lines[i][0].strip() != name1:
continue
if lines[i][1].strip() != name2:
continue
lines[i][2] = distance
print(lines[i])
# 2. Append one line to lines, containing the details of links.
add_new_link = [name1, name2, distance]
with open("%s/conflict_data/active_conflict/routes.csv"
% (get_plugin_path("FabFlee")), "a") as new_csv:
writer = csv.writer(new_csv)
writer.writerow(add_new_link)
print(add_new_link)
@task
# Syntax: fabsim localhost find_capacity:<csv_name>
def find_capacity(csv_name):
"""
Find the highest refugee number within csv file of source data
for neighbouring camps.
"""
import csv
csv_file = open("%s/conflict_data/active_conflict/source_data/%s"
% (get_plugin_path("FabFlee"), csv_name)).readlines()
print(max(((i, int(l.split(',')[1])) for i, l in enumerate(
csv_file)), key=lambda t: t[1])[1])