1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | Program to update a DVD jurisdiction 'anuga' directory from the |
---|
5 | original source directory. |
---|
6 | |
---|
7 | usage: update.py <source> |
---|
8 | |
---|
9 | where <source> is one of: |
---|
10 | Hobart |
---|
11 | BatemansBay |
---|
12 | Gosford |
---|
13 | GoldCoast |
---|
14 | ie, the name of one of the DVD jurisdiction staging directories |
---|
15 | (case-insensitive). |
---|
16 | ''' |
---|
17 | |
---|
18 | import os |
---|
19 | import sys |
---|
20 | import shutil |
---|
21 | import time |
---|
22 | |
---|
23 | |
---|
24 | # common base path to all data and project files |
---|
25 | main_path = '/nas/gemd/georisk_models/inundation' |
---|
26 | |
---|
27 | # structures holding data for each jurisdiction |
---|
28 | # data_src_path : sub-path to data directory |
---|
29 | # proj_src_path : sub-path to project files |
---|
30 | # data_dst_path : where to copy data files to |
---|
31 | # proj_dst_path : where to copy project files to |
---|
32 | # copy_data_dirs : data directories to completely copy |
---|
33 | # make_dst_dirs : make these data directories (may be left empty) |
---|
34 | # copy_data_files: individual data files to copy to <data_dst_path> |
---|
35 | # copy_proj_files: individual project files to copy to <proj_dst_path> |
---|
36 | hobart_data = \ |
---|
37 | {'jurisdiction': 'Hobart', # jurisdiction name |
---|
38 | |
---|
39 | # paths to various source directories |
---|
40 | 'data_src_path': 'data/tasmania/hobart_tsunami_scenario_2009/anuga', |
---|
41 | 'arcgis_src_path': 'data/tasmania/hobart_tsunami_scenario_2009/ArcGIS', |
---|
42 | 'viz_src_path': 'data/tasmania/hobart_tsunami_scenario_2009/visualisations', |
---|
43 | 'proj_src_path': 'sandpits/kvanputten/ANUGA/anuga_work/production/hobart_2009/For_DVD', |
---|
44 | |
---|
45 | # paths to destination directories (under 'jurisdiction' root) |
---|
46 | 'data_dst_path': 'data/tasmania/hobart_tsunami_scenario_2009/anuga', |
---|
47 | 'proj_dst_path': 'project', |
---|
48 | 'viz_dst_path': 'documents', |
---|
49 | 'arcgis_dst_path': 'data/tasmania/hobart_tsunami_scenario_2009/ArcGIS', |
---|
50 | |
---|
51 | # copy or create whole directories |
---|
52 | 'make_dst_dirs': ['outputs', 'topographies'], |
---|
53 | 'copy_data_dirs': ['gauges','boundaries', 'polygons'], |
---|
54 | |
---|
55 | # copy 'data' files or directories |
---|
56 | 'copy_data_files': ['outputs/Event1_HAT', |
---|
57 | 'outputs/Event1_MSL', |
---|
58 | 'outputs/Event2_HAT', |
---|
59 | 'outputs/Event2_MSL', |
---|
60 | 'outputs/Event3_MSL', |
---|
61 | 'topographies/hobart_combined_elevation.pts', |
---|
62 | 'topographies/hobart_combined_elevation.txt'], |
---|
63 | |
---|
64 | # copy 'project' files or directories |
---|
65 | 'copy_proj_files': ['build_elevation.py', 'project.py', 'run_model.py', |
---|
66 | 'export_results_max.py', 'get_timeseries.py', |
---|
67 | 'setup_model.py', 'build_urs_boundary.py'], |
---|
68 | |
---|
69 | # copy 'visualisations' files or directories |
---|
70 | 'copy_viz_files': ['Figures'], |
---|
71 | |
---|
72 | # copy 'arcgis' files or directories |
---|
73 | 'copy_arc_files': [] |
---|
74 | } |
---|
75 | |
---|
76 | batemans_bay_data = \ |
---|
77 | {'jurisdiction': 'BatemansBay', # jurisdiction name |
---|
78 | |
---|
79 | # paths to various source directories |
---|
80 | 'data_src_path': 'data/new_south_wales/batemans_bay_tsunami_scenario_2009/anuga', |
---|
81 | 'arcgis_src_path': 'data/new_south_wales/batemans_bay_tsunami_scenario_2009/ArcGIS', |
---|
82 | 'viz_src_path': 'data/new_south_wales/batemans_bay_tsunami_scenario_2009/visualisations', |
---|
83 | 'proj_src_path': 'sandpits/jgriffin/ANUGA/anuga_work/production/new_south_wales/batemans_bay', |
---|
84 | |
---|
85 | # paths to destination directories (under 'jurisdiction' root) |
---|
86 | 'data_dst_path': 'data/new_south_wales/batemans_bay_tsunami_scenario_2009/anuga', |
---|
87 | 'proj_dst_path': 'project', |
---|
88 | 'viz_dst_path': 'documents', |
---|
89 | 'arcgis_dst_path': 'data/new_south_wales/batemans_bay_tsunami_scenario_2009/ArcGIS', |
---|
90 | |
---|
91 | # copy or create whole directories |
---|
92 | 'make_dst_dirs': ['meshes', 'outputs', 'topographies'], |
---|
93 | 'copy_data_dirs': ['boundaries', 'polygons', 'gauges'], |
---|
94 | |
---|
95 | # copy 'data' files or directories |
---|
96 | 'copy_data_files': ['topographies/batemans_bay_combined_elevation.pts', |
---|
97 | 'outputs/Event1_HAT', |
---|
98 | 'outputs/Event1_MSL', |
---|
99 | 'outputs/Event2_HAT', |
---|
100 | 'outputs/Event2_MSL', |
---|
101 | 'outputs/Event3_MSL', |
---|
102 | 'outputs/Puysegur_200yr', |
---|
103 | 'outputs/Puysegur_500yr', |
---|
104 | 'outputs/Puysegur_1000yr', |
---|
105 | 'outputs/Puysegur_5000yr', |
---|
106 | 'outputs/New_Hebrides_200yr', |
---|
107 | 'outputs/New_Hebrides_500yr', |
---|
108 | 'outputs/New_Hebrides_1000yr', |
---|
109 | 'outputs/New_Hebrides_2000yr', |
---|
110 | 'outputs/New_Hebrides_5000yr', |
---|
111 | 'outputs/elevation'], |
---|
112 | |
---|
113 | # copy 'visualisations' files or directories |
---|
114 | 'copy_viz_files': ['Figures'], |
---|
115 | |
---|
116 | # copy 'project' files or directories |
---|
117 | 'copy_proj_files': ['project.py', 'run_model.py', |
---|
118 | 'setup_model.py', 'build_elevation.py', |
---|
119 | 'export_results_max.py', |
---|
120 | 'file_length.py', 'build_urs_boundary.py'], |
---|
121 | |
---|
122 | # copy 'arcgis' files or directories |
---|
123 | 'copy_arc_files': ['BB_cbd_figure_template.mxd'] |
---|
124 | } |
---|
125 | |
---|
126 | gosford_data = \ |
---|
127 | {'jurisdiction': 'Gosford', # jurisdiction name |
---|
128 | |
---|
129 | # paths to various source directories |
---|
130 | 'data_src_path': 'data/new_south_wales/gosford_tsunami_scenario_2009/anuga', |
---|
131 | 'arcgis_src_path': 'data/new_south_wales/gosford_tsunami_scenario_2009/ArcGIS', |
---|
132 | 'viz_src_path': 'data/new_south_wales/gosford_tsunami_scenario_2009/visualisations', |
---|
133 | 'proj_src_path': 'sandpits/jgriffin/ANUGA/anuga_work/production/new_south_wales/gosford', |
---|
134 | |
---|
135 | # paths to destination directories (under 'jurisdiction' root) |
---|
136 | 'data_dst_path': 'data/new_south_wales/gosford_tsunami_scenario_2009/anuga', |
---|
137 | 'proj_dst_path': 'project', |
---|
138 | 'viz_dst_path': 'documents', |
---|
139 | 'arcgis_dst_path': 'data/new_south_wales/gosford_tsunami_scenario_2009/ArcGIS', |
---|
140 | |
---|
141 | # copy or create whole directories |
---|
142 | 'make_dst_dirs': ['meshes', 'outputs', 'topographies', 'gauges'], |
---|
143 | 'copy_data_dirs': ['boundaries', 'polygons'], |
---|
144 | |
---|
145 | # copy 'data' files or directories |
---|
146 | 'copy_data_files': ['topographies/gosford_combined_elevation.pts', |
---|
147 | 'outputs/Event1_HAT', |
---|
148 | 'outputs/Event1_MSL', |
---|
149 | 'outputs/Event2_HAT', |
---|
150 | 'outputs/Event2_MSL', |
---|
151 | 'outputs/Event3_MSL', |
---|
152 | 'outputs/Puysegur_200yr', |
---|
153 | 'outputs/Puysegur_500yr', |
---|
154 | 'outputs/Puysegur_1000yr', |
---|
155 | 'outputs/Puysegur_5000yr', |
---|
156 | 'outputs/elevation' |
---|
157 | ], |
---|
158 | |
---|
159 | # copy 'visualisations' files or directories |
---|
160 | 'copy_viz_files': ['Figures'], |
---|
161 | |
---|
162 | # copy 'project' files or directories |
---|
163 | 'copy_proj_files': ['export_results_max.py', 'file_length.py', |
---|
164 | 'project.py', 'run_model.py', 'build_elevation.py', |
---|
165 | 'get_timeseries.py', 'setup_model.py', |
---|
166 | 'build_urs_boundary.py' |
---|
167 | ], |
---|
168 | |
---|
169 | # copy 'arcgis' files or directories |
---|
170 | 'copy_arc_files': ['G_umina_figure_template.mxd'] |
---|
171 | } |
---|
172 | |
---|
173 | gold_coast_data = \ |
---|
174 | {'jurisdiction': 'GoldCoast', # jurisdiction name |
---|
175 | |
---|
176 | # paths to various source directories |
---|
177 | 'data_src_path': 'data/queensland/gold_coast_tsunami_scenario_2009/anuga', |
---|
178 | 'arcgis_src_path': 'data/queensland/gold_coast_tsunami_scenario_2009/ArcGIS', |
---|
179 | 'viz_src_path': 'data/queensland/gold_coast_tsunami_scenario_2009/visualisations', |
---|
180 | 'proj_src_path': 'sandpits/lfountain/anuga_work/production/gold_coast_2009', |
---|
181 | |
---|
182 | # paths to destination directories (under 'jurisdiction' root) |
---|
183 | 'data_dst_path': 'data/queensland/gold_coast_tsunami_scenario_2009/anuga', |
---|
184 | 'proj_dst_path': 'project', |
---|
185 | 'viz_dst_path': 'documents', |
---|
186 | 'arcgis_dst_path': 'data/queensland/gold_coast_tsunami_scenario_2009/ArcGIS', |
---|
187 | |
---|
188 | # copy or create whole directories |
---|
189 | 'make_dst_dirs': ['outputs', 'polygons', 'topographies', 'gauges'], |
---|
190 | 'copy_data_dirs': ['boundaries'], |
---|
191 | |
---|
192 | # copy 'data' files or directories |
---|
193 | 'copy_data_files': ['outputs/Event1_HAT', 'outputs/Event1_MSL', |
---|
194 | 'outputs/Event2_HAT', 'outputs/Event2_MSL', |
---|
195 | 'outputs/Event3_HAT', 'outputs/Event3_MSL', |
---|
196 | 'polygons/bounding_polygon.csv', |
---|
197 | 'polygons/area_of_interest.csv', |
---|
198 | 'polygons/intermediate.csv', |
---|
199 | 'polygons/initial_conditions.csv', |
---|
200 | 'gauges/gauges.csv', |
---|
201 | 'polygons/images.csv', |
---|
202 | 'topographies/gold_coast_combined_elevation.pts' |
---|
203 | ], |
---|
204 | |
---|
205 | # copy 'visualisations' files or directories |
---|
206 | 'copy_viz_files': ['Figures'], |
---|
207 | |
---|
208 | # copy 'project' files or directories |
---|
209 | 'copy_proj_files': ['build_elevation.py', 'export_results_max.py', 'file_length.py', |
---|
210 | 'project.py', 'run_model.py', 'setup_model.py', |
---|
211 | 'get_timeseries.py', 'build_urs_boundary.py' |
---|
212 | ], |
---|
213 | |
---|
214 | # copy 'arcgis' files or directories |
---|
215 | 'copy_arc_files': ['gold_coast.mxd', 'gold_coast.gdb'] |
---|
216 | } |
---|
217 | |
---|
218 | # dictionary mapping lower-case jurisdiction name to jurisdiction data dictionary |
---|
219 | source_jurisdiction_path = {'hobart': hobart_data, |
---|
220 | 'batemansbay': batemans_bay_data, |
---|
221 | 'gosford': gosford_data, |
---|
222 | 'goldcoast': gold_coast_data |
---|
223 | } |
---|
224 | |
---|
225 | ###### |
---|
226 | # Routines to automate the script data above. |
---|
227 | ###### |
---|
228 | |
---|
229 | def log(msg=''): |
---|
230 | print(msg) |
---|
231 | |
---|
232 | |
---|
233 | def dir_copy(src, dst): |
---|
234 | cmd = 'cp -R %s %s' % (src, dst) |
---|
235 | log('Doing: %s' % cmd) |
---|
236 | fd = os.popen(cmd) |
---|
237 | fd.close() |
---|
238 | |
---|
239 | def copy_file_or_dir(src, dst): |
---|
240 | '''Copy a file or complete directory.''' |
---|
241 | |
---|
242 | # could be a file or directory being copied |
---|
243 | try: |
---|
244 | shutil.copyfile(src, dst) |
---|
245 | except IOError, e: |
---|
246 | if 'Is a directory' in str(e): |
---|
247 | shutil.copytree(src, dst) |
---|
248 | else: |
---|
249 | log('*' *72) |
---|
250 | log('* %s' % str(e)) |
---|
251 | log('*' *72) |
---|
252 | |
---|
253 | def update_staging(jurisdiction): |
---|
254 | # create a list of jurisdiction names |
---|
255 | jurisdiction_names = [] |
---|
256 | for k in source_jurisdiction_path: |
---|
257 | jurisdiction_names.append(source_jurisdiction_path[k]['jurisdiction']) |
---|
258 | |
---|
259 | # get ready |
---|
260 | j_dict = source_jurisdiction_path[jurisdiction] |
---|
261 | j_name = j_dict['jurisdiction'] |
---|
262 | data_src_path = os.path.join(main_path, j_dict['data_src_path']) |
---|
263 | data_dst_path = os.path.join(os.getcwd(), j_name, j_dict['data_dst_path']) |
---|
264 | proj_src_path = os.path.join(main_path, j_dict['proj_src_path']) |
---|
265 | proj_dst_path = os.path.join(os.getcwd(), j_name, j_dict['proj_dst_path']) |
---|
266 | viz_src_path = os.path.join(main_path, j_dict['viz_src_path']) |
---|
267 | viz_dst_path = os.path.join(os.getcwd(), j_name, j_dict['viz_dst_path']) |
---|
268 | arcgis_src_path = os.path.join(main_path, j_dict['arcgis_src_path']) |
---|
269 | arcgis_dst_path = os.path.join(os.getcwd(), j_name, j_dict['arcgis_dst_path']) |
---|
270 | |
---|
271 | # tell where all stuff is coming from |
---|
272 | log('Getting data from: %s' % data_src_path) |
---|
273 | log('Getting project from: %s' % proj_src_path) |
---|
274 | log('Getting ArcGIS from: %s' % arcgis_src_path) |
---|
275 | log('') |
---|
276 | |
---|
277 | # create new output directory, delete old if there |
---|
278 | if os.path.exists(j_name): |
---|
279 | log('Deleting existing staging directory: %s' % j_name) |
---|
280 | shutil.rmtree(j_name) |
---|
281 | log('Creating staging directory: %s' % j_name) |
---|
282 | os.makedirs(j_name) |
---|
283 | |
---|
284 | # create required directories |
---|
285 | for dir in j_dict['make_dst_dirs']: |
---|
286 | new_dir = os.path.join(data_dst_path, dir) |
---|
287 | log('Creating directory: %s' % dir) |
---|
288 | os.makedirs(new_dir) |
---|
289 | |
---|
290 | # copy required full directories |
---|
291 | for copy_dir in j_dict['copy_data_dirs']: |
---|
292 | src_dir = os.path.join(data_src_path, copy_dir) |
---|
293 | dst_dir = os.path.join(data_dst_path, copy_dir) |
---|
294 | log('Copying directory: %s' % copy_dir) |
---|
295 | copy_file_or_dir(src_dir, dst_dir) |
---|
296 | |
---|
297 | # copy required data files |
---|
298 | for copy_file in j_dict['copy_data_files']: |
---|
299 | src_file = os.path.join(data_src_path, copy_file) |
---|
300 | new_file = os.path.join(data_dst_path, copy_file) |
---|
301 | log('Copying: %s' % copy_file) |
---|
302 | copy_file_or_dir(src_file, new_file) |
---|
303 | |
---|
304 | # copy required visualisation files |
---|
305 | log('Creating directory: %s' % viz_dst_path) |
---|
306 | os.makedirs(viz_dst_path) |
---|
307 | for copy_file in j_dict['copy_viz_files']: |
---|
308 | src_file = os.path.join(viz_src_path, copy_file) |
---|
309 | new_file = os.path.join(viz_dst_path, copy_file) |
---|
310 | log('Copying: %s' % copy_file) |
---|
311 | copy_file_or_dir(src_file, new_file) |
---|
312 | |
---|
313 | # copy required project files |
---|
314 | log('Creating directory: %s' % proj_dst_path) |
---|
315 | os.makedirs(proj_dst_path) |
---|
316 | for copy_file in j_dict['copy_proj_files']: |
---|
317 | src_file = os.path.join(proj_src_path, copy_file) |
---|
318 | new_file = os.path.join(proj_dst_path, copy_file) |
---|
319 | log('Copying: %s' % copy_file) |
---|
320 | copy_file_or_dir(src_file, new_file) |
---|
321 | |
---|
322 | # copy required ArcGIS files |
---|
323 | log('Creating directory: %s' % arcgis_dst_path) |
---|
324 | os.makedirs(arcgis_dst_path) |
---|
325 | for copy_file in j_dict['copy_arc_files']: |
---|
326 | src_file = os.path.join(arcgis_src_path, copy_file) |
---|
327 | new_file = os.path.join(arcgis_dst_path, copy_file) |
---|
328 | log('Copying: %s' % copy_file) |
---|
329 | copy_file_or_dir(src_file, new_file) |
---|
330 | |
---|
331 | # now copy jurisdiction-specific DVD files |
---|
332 | src_dir = 'extra_files/%s/*' % j_name |
---|
333 | dst_dir = j_name |
---|
334 | dir_copy(src_dir, dst_dir) |
---|
335 | |
---|
336 | # copy the extra_files and special directories |
---|
337 | src_dir = 'extra_files/browser_files' |
---|
338 | dst_dir = j_name |
---|
339 | dir_copy(src_dir, dst_dir) |
---|
340 | src_dir = 'extra_files/documents' |
---|
341 | dir_copy(src_dir, dst_dir) |
---|
342 | src_file = 'extra_files/autorun.inf' |
---|
343 | dir_copy(src_file, dst_dir) |
---|
344 | src_dir = 'extra_files/.cache' |
---|
345 | dir_copy(src_dir, dst_dir) |
---|
346 | |
---|
347 | # get size of the staging directory |
---|
348 | cmd = 'du -sh %s' % j_name |
---|
349 | fd = os.popen(cmd, 'r') |
---|
350 | res = fd.read() |
---|
351 | fd.close() |
---|
352 | (res, _) = res.split('\t', 1) |
---|
353 | log('Staging directory %s has size %s' % (j_name, res)) |
---|
354 | |
---|
355 | def usage(): |
---|
356 | print('usage: update.py <source>') |
---|
357 | print('where <source> is one of the jurisdiction staging directories.') |
---|
358 | |
---|
359 | |
---|
360 | if len(sys.argv) != 2: |
---|
361 | usage() |
---|
362 | sys.exit(10) |
---|
363 | |
---|
364 | jurisdiction = sys.argv[1].lower() |
---|
365 | # remove any trailing '/' - from TAB completion |
---|
366 | if jurisdiction.endswith('/'): |
---|
367 | jurisdiction = jurisdiction[:-1] |
---|
368 | |
---|
369 | start_time = time.time() |
---|
370 | update_staging(jurisdiction) |
---|
371 | stop_time = time.time() |
---|
372 | elapsed_time = stop_time - start_time |
---|
373 | log('Elapsed time is %.1fs' % elapsed_time) |
---|