1 | """ |
---|
2 | Module to import the project.py 'configuration' file and perform |
---|
3 | sanity checks plus a quick check on mesh generation. |
---|
4 | |
---|
5 | Also callable as a stand-alone program, mainly to view the results |
---|
6 | of the mesh generation. |
---|
7 | """ |
---|
8 | |
---|
9 | import os |
---|
10 | |
---|
11 | from os.path import join, exists |
---|
12 | from anuga.utilities.polygon import read_polygon, number_mesh_triangles |
---|
13 | from anuga.shallow_water.data_manager import csv2polygons |
---|
14 | import project |
---|
15 | |
---|
16 | |
---|
17 | #------------------------------------------------------------------------------- |
---|
18 | # Sanity checks - check that required files/directories, etc, exist. |
---|
19 | #------------------------------------------------------------------------------- |
---|
20 | |
---|
21 | # flag - we check many things and then don't proceed if anything wrong |
---|
22 | sanity_error = False # checked at bottom of this file |
---|
23 | |
---|
24 | # Test that environment variables are defined. |
---|
25 | if os.getenv(project.ENV_INUNDATIONHOME) is None: |
---|
26 | print "Environment variable '%s' is not set" % project.ENV_INUNDATIONHOME |
---|
27 | sanity_error = True |
---|
28 | |
---|
29 | #------------------------------------------------------------------------------- |
---|
30 | # Check filename strings that MUST be set |
---|
31 | #------------------------------------------------------------------------------- |
---|
32 | |
---|
33 | if not project.urs_order_filename: |
---|
34 | print ("Sorry, thinned ordering file parameter 'urs_order_filename' " |
---|
35 | "isn't set") |
---|
36 | sanity_error = True |
---|
37 | |
---|
38 | if not project.landward_boundary_filename: |
---|
39 | print ("Sorry, landward bounding points parameter " |
---|
40 | "'landward_boundary_filename' isn't set") |
---|
41 | sanity_error = True |
---|
42 | |
---|
43 | #------------------------------------------------------------------------------- |
---|
44 | # Directory Structure |
---|
45 | #------------------------------------------------------------------------------- |
---|
46 | |
---|
47 | # check folders generated from environment variables. |
---|
48 | if not exists(project.home): |
---|
49 | print "Sorry, data directory '%s' doesn't exist" % project.home |
---|
50 | sanity_error = True |
---|
51 | |
---|
52 | if not exists(project.anuga_folder): |
---|
53 | print "Sorry, ANUGA directory '%s' doesn't exist" % project.anuga_folder |
---|
54 | sanity_error = True |
---|
55 | |
---|
56 | if not exists(project.topographies_folder): |
---|
57 | print ("Sorry, topo directory '%s' doesn't exist" |
---|
58 | % project.topographies_folder) |
---|
59 | sanity_error = True |
---|
60 | |
---|
61 | if not exists(project.polygons_folder): |
---|
62 | print ("Sorry, polygon directory '%s' doesn't exist" |
---|
63 | % project.polygons_folder) |
---|
64 | sanity_error = True |
---|
65 | |
---|
66 | if not exists(project.boundaries_folder): |
---|
67 | print ("Sorry, boundaries directory '%s' doesn't exist" |
---|
68 | % project.boundaries_folder) |
---|
69 | sanity_error = True |
---|
70 | |
---|
71 | if not exists(project.output_folder): |
---|
72 | print "Sorry, outputs directory '%s' doesn't exist" % project.output_folder |
---|
73 | sanity_error = True |
---|
74 | |
---|
75 | if not exists(project.gauges_folder): |
---|
76 | print "Sorry, gauges directory '%s' doesn't exist" % project.gauges_folder |
---|
77 | sanity_error = True |
---|
78 | |
---|
79 | #----- |
---|
80 | # If this directory doesn't exist, EventSelection hasn't been run. |
---|
81 | #----- |
---|
82 | |
---|
83 | if not exists(project.event_folder): |
---|
84 | print ("Sorry, you must generate event %s with EventSelection." |
---|
85 | % project.event_number) |
---|
86 | sanity_error = True |
---|
87 | |
---|
88 | #------------------------------------------------------------------------------- |
---|
89 | # Determine type of run |
---|
90 | #------------------------------------------------------------------------------- |
---|
91 | |
---|
92 | if project.setup == 'trial': |
---|
93 | print 'trial' |
---|
94 | project.scale_factor = 100 |
---|
95 | project.time_thinning = 96 |
---|
96 | project.yieldstep = 240 |
---|
97 | elif project.setup == 'basic': |
---|
98 | print 'basic' |
---|
99 | project.scale_factor = 4 |
---|
100 | project.time_thinning = 12 |
---|
101 | project.yieldstep = 120 |
---|
102 | elif project.setup == 'final': |
---|
103 | print 'final' |
---|
104 | project.scale_factor = 1 |
---|
105 | project.time_thinning = 4 |
---|
106 | project.yieldstep = 60 |
---|
107 | else: |
---|
108 | print ("Sorry, you must set the 'setup' variable to one of:" |
---|
109 | ' trial - coarsest mesh, fast\n' |
---|
110 | ' basic - coarse mesh\n' |
---|
111 | ' final - fine mesh, slowest\n' |
---|
112 | '\n' |
---|
113 | "'setup' was set to '%s'" % project.setup) |
---|
114 | sanity_error = True |
---|
115 | |
---|
116 | #------------------------------------------------------------------------------- |
---|
117 | # Check for errors detected above. |
---|
118 | #------------------------------------------------------------------------------- |
---|
119 | |
---|
120 | if sanity_error: |
---|
121 | msg = 'You must fix the above errors before continuing.' |
---|
122 | raise Exception, msg |
---|
123 | |
---|
124 | #------------------------------------------------------------------------------- |
---|
125 | # Reading polygons and creating interior regions |
---|
126 | #------------------------------------------------------------------------------- |
---|
127 | |
---|
128 | # Create list of land polygons with initial conditions |
---|
129 | project.land_initial_conditions = [] |
---|
130 | # if it's a list, then it's a list of land condition filenames |
---|
131 | # else it's a string - a single file, multiple land conditions |
---|
132 | if isinstance(project.land_initial_conditions_filename, list): |
---|
133 | for filename, MSL in project.land_initial_conditions_filename: |
---|
134 | polygon = read_polygon(join(project.polygons_folder, filename)) |
---|
135 | project.land_initial_conditions.append([polygon, MSL]) |
---|
136 | elif isinstance(project.land_initial_conditions_filename, basestring): |
---|
137 | polygons, MSL = csv2polygons(join(project.polygons_folder, |
---|
138 | project.land_initial_conditions_filename)) |
---|
139 | for i, key in enumerate(polygons): |
---|
140 | if i%100==0: print i |
---|
141 | poly = polygons[key] |
---|
142 | land = float(MSL[key]) |
---|
143 | project.land_initial_conditions.append([poly, land]) |
---|
144 | else: |
---|
145 | msg = ('project.land_initial_conditions_filename must be a list or ' |
---|
146 | 'string, got %s' |
---|
147 | % type(project.land_initial_conditions_filename)) |
---|
148 | raise Exception, msg |
---|
149 | |
---|
150 | # Create list of interior polygons with scaling factor |
---|
151 | project.interior_regions = [] |
---|
152 | |
---|
153 | if project.PriorityArea_filename is not None: |
---|
154 | polygons, maxareas = csv2polygons(project.PriorityAreas) |
---|
155 | print 'Creating %d internal polygons' % len(polygons) |
---|
156 | #def create_polygon_function(polygons, geo_reference=None): |
---|
157 | project.interior_regions = [] |
---|
158 | for i, key in enumerate(polygons): |
---|
159 | if i%100==0: print i |
---|
160 | poly = polygons[key] |
---|
161 | maxarea = float(maxareas[key]) |
---|
162 | project.interior_regions.append([poly, |
---|
163 | maxarea*project.scale_factor]) |
---|
164 | |
---|
165 | for filename, maxarea in project.interior_regions_data: |
---|
166 | polygon = read_polygon(join(project.polygons_folder, filename)) |
---|
167 | project.interior_regions.append([polygon, |
---|
168 | maxarea*project.scale_factor]) |
---|
169 | |
---|
170 | # Initial bounding polygon for data clipping |
---|
171 | project.bounding_polygon = read_polygon(join(project.polygons_folder, |
---|
172 | project.bounding_polygon_filename)) |
---|
173 | project.bounding_maxarea = project.bounding_polygon_maxarea*project.scale_factor |
---|
174 | |
---|
175 | # Estimate the number of triangles |
---|
176 | trigs_min = number_mesh_triangles(project.interior_regions, |
---|
177 | project.bounding_polygon, |
---|
178 | project.bounding_maxarea) |
---|
179 | |
---|
180 | print 'min estimated number of triangles', trigs_min |
---|