source: anuga_work/production/Broome_2008/setup_model.py @ 7159

Last change on this file since 7159 was 7159, checked in by kristy, 15 years ago

from Hobart

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