""" Module to import the project.py 'configuration' file and perform sanity checks plus a quick check on mesh generation. Also callable as a stand-alone program, mainly to view the results of the mesh generation. """ import os from os.path import join, exists from anuga.utilities.polygon import read_polygon, number_mesh_triangles from anuga.shallow_water.data_manager import csv2polygons import project #------------------------------------------------------------------------------- # Sanity checks - check that required files/directories, etc, exist. #------------------------------------------------------------------------------- # flag - we check many things and then don't proceed if anything wrong sanity_error = False # checked at bottom of this file # Test that environment variables are defined. if os.getenv(project.ENV_INUNDATIONHOME) is None: print "Environment variable '%s' is not set" % project.ENV_INUNDATIONHOME sanity_error = True if os.getenv(project.ENV_MUXHOME) is None: print "Environment variable '%s' is not set" % project.ENV_MUXHOME sanity_error = True #------------------------------------------------------------------------------- # Check filename strings that MUST be set #------------------------------------------------------------------------------- if not project.urs_order_filename: print ("Sorry, thinned ordering file parameter 'urs_order_filename' " "isn't set") sanity_error = True if not project.landward_boundary_filename: print ("Sorry, landward bounding points parameter " "'landward_boundary_filename' isn't set") sanity_error = True #------------------------------------------------------------------------------- # Directory Structure #------------------------------------------------------------------------------- # check folders generated from environment variables. if not exists(project.home): print "Sorry, data directory '%s' doesn't exist" % project.home sanity_error = True if not exists(project.muxhome): print "Sorry, MUX directory '%s' doesn't exist" % project.muxhome sanity_error = True if not exists(project.anuga_folder): print "Sorry, ANUGA directory '%s' doesn't exist" % project.anuga_folder sanity_error = True if not exists(project.topographies_folder): print ("Sorry, topo directory '%s' doesn't exist" % project.topographies_folder) sanity_error = True if not exists(project.polygons_folder): print ("Sorry, polygon directory '%s' doesn't exist" % project.polygons_folder) sanity_error = True if not exists(project.boundaries_folder): print ("Sorry, boundaries directory '%s' doesn't exist" % project.boundaries_folder) sanity_error = True if not exists(project.output_folder): print "Sorry, outputs directory '%s' doesn't exist" % project.output_folder sanity_error = True if not exists(project.gauges_folder): print "Sorry, gauges directory '%s' doesn't exist" % project.gauges_folder sanity_error = True if not exists(project.mux_data_folder): print "Sorry, mux data directory '%s' doesn't exist" % project.mux_data_folder sanity_error = True # if multi_mux is True, check if multi-mux file exists if project.multi_mux: if not exists(project.mux_input): print ("Sorry, MUX input file '%s' doesn't exist" % project.mux_input) sanity_error = True #----- # If this directory don't exist, EventSelection hasn't been run. #----- if not exists(project.event_folder): print ("Sorry, you must generate event %s with EventSelection." % project.event_number) sanity_error = True #------------------------------------------------------------------------------- # Determine type of run #------------------------------------------------------------------------------- if project.setup == 'trial': print 'trial' project.scale_factor = 100 project.time_thinning = 96 project.yieldstep = 240 elif project.setup == 'basic': print 'basic' project.scale_factor = 4 project.time_thinning = 12 project.yieldstep = 120 elif project.setup == 'final': print 'final' project.scale_factor = 1 project.time_thinning = 4 project.yieldstep = 60 else: print ("Sorry, you must set the 'setup' variable to one of:" ' trial - coarsest mesh, fast\n' ' basic - coarse mesh\n' ' final - fine mesh, slowest\n' '\n' "'setup' was set to '%s'" % project.setup) sanity_error = True #------------------------------------------------------------------------------- # Check for errors detected above. #------------------------------------------------------------------------------- if sanity_error: msg = 'You must fix the above errors before continuing.' raise Exception, msg #------------------------------------------------------------------------------- # Reading polygons and creating interior regions #------------------------------------------------------------------------------- # Create list of land polygons with initial conditions project.land_initial_conditions = [] # if it's a list, then it's a list of land condition filenames # else it's a string - a single file, multiple land conditions if isinstance(project.land_initial_conditions_filename, list): for filename, MSL in project.land_initial_conditions_filename: polygon = read_polygon(join(project.polygons_folder, filename)) project.land_initial_conditions.append([polygon, MSL]) elif isinstance(project.land_initial_conditions_filename, basestring): polygons, MSL = csv2polygons(join(project.polygons_folder, project.land_initial_conditions_filename)) for i, key in enumerate(polygons): if i%100==0: print i poly = polygons[key] land = float(MSL[key]) project.land_initial_conditions.append([poly, land]) else: msg = ('project.land_initial_conditions_filename must be a list or ' 'string, got %s' % type(project.land_initial_conditions_filename)) raise Exception, msg # Create list of interior polygons with scaling factor project.interior_regions = [] if project.PriorityArea_filename is not None: polygons, maxareas = csv2polygons(project.PriorityAreas) print 'Creating %d internal polygons' % len(polygons) #def create_polygon_function(polygons, geo_reference=None): project.interior_regions = [] for i, key in enumerate(polygons): if i%100==0: print i poly = polygons[key] maxarea = float(maxareas[key]) project.interior_regions.append([poly, maxarea*project.scale_factor]) for filename, maxarea in project.interior_regions_data: polygon = read_polygon(join(project.polygons_folder, filename)) project.interior_regions.append([polygon, maxarea*project.scale_factor]) # Initial bounding polygon for data clipping project.bounding_polygon = read_polygon(join(project.polygons_folder, project.bounding_polygon_filename)) project.bounding_maxarea = project.bounding_polygon_maxarea*project.scale_factor # Estimate the number of triangles trigs_min = number_mesh_triangles(project.interior_regions, project.bounding_polygon, project.bounding_maxarea) print 'min estimated number of triangles', trigs_min