Example #1
0
    def test_boundary_timeII_1_5(self):
        """
        test_boundary_timeII(self):
        Test that starttime can be set in the middle of a boundary condition
        """

        boundary_starttime = 0
        boundary_filename = self.create_sww_boundary(boundary_starttime)
        # print "boundary_filename",boundary_filename

        filename = tempfile.mktemp(".sww")
        # print "filename",filename
        dir, base = os.path.split(filename)
        senario_name = base[:-4]

        mesh = Mesh()
        mesh.add_region_from_polygon([[10, 10], [90, 10], [90, 90], [10, 90]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(senario_name)
        domain.set_datadir(dir)
        domain.set_flow_algorithm("1_5")
        new_starttime = 0.0
        domain.set_starttime(new_starttime)

        # Setup initial conditions
        domain.set_quantity("elevation", 0.0)
        domain.set_quantity("stage", 0.0)
        Bf = anuga.File_boundary(boundary_filename, domain, use_cache=False, verbose=False)

        # Setup boundary conditions
        domain.set_boundary({"exterior": Bf})
        for t in domain.evolve(yieldstep=5, finaltime=9.0):
            pass
            # print domain.boundary_statistics()
            # domain.write_time()
            # print "domain.time", domain.time

        # do an assertion on the time of the produced sww file
        fid = NetCDFFile(filename, netcdf_mode_r)  # Open existing file for read
        times = fid.variables["time"][:]
        stage = fid.variables["stage"][:]
        # print stage
        # print "times", times
        # print "fid.starttime", fid.starttime
        assert num.allclose(fid.starttime, new_starttime)
        fid.close()

        # print "stage[2,0]", stage[2,0]
        msg = "This test is a bit hand crafted, based on the output file. "
        msg += "Not logic. "
        msg += "It's testing that starttime is working"
        assert num.allclose(stage[2, 0], 4.88601), msg

        # clean up
        os.remove(boundary_filename)
        os.remove(filename)
Example #2
0
    def create_sww_boundary(self, boundary_starttime):
        """
        This creates a boundary file with ;
        time     stage
        0        5
        10       2.15268
        20       13.9773
        """

        tide = 5
        boundary_filename = tempfile.mktemp(".sww")
        dir, base = os.path.split(boundary_filename)
        boundary_name = base[:-4]

        # Setup computational domain
        mesh = Mesh()
        mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(boundary_name)
        domain.set_datadir(dir)
        domain.set_starttime(boundary_starttime)

        # Setup initial conditions
        domain.set_quantity("elevation", 0.0)
        domain.set_quantity("stage", tide)

        # Setup boundary conditions
        Bd = anuga.Dirichlet_boundary([tide, 0.0, 0.0])  # Constant boundary values
        Bd = anuga.Time_boundary(domain=domain, function=lambda t: [t, 0.0, 0.0])  # Time dependent boundary
        domain.set_boundary({"exterior": Bd})
        for t in domain.evolve(yieldstep=10, finaltime=20.0):
            pass
            # print domain.boundary_statistics('stage')
            q = Bd.evaluate()

            # FIXME (Ole): This test would not have passed in
            # changeset:5846.
            msg = "Time boundary not evaluated correctly"
            assert num.allclose(t, q[0]), msg

            # print domain.get_quantity('stage').get_values()
            # domain.write_time()
            # print "domain.time", domain.time

        return boundary_filename
Example #3
0
    def test_boundary_time(self):
        """
        test_boundary_time(self):
        test that the starttime of a boundary condition is carried thru
        to the output sww file.
        
        """

        boundary_starttime = 0
        boundary_filename = self.create_sww_boundary(boundary_starttime)
        filename = tempfile.mktemp(".sww")
        dir, base = os.path.split(filename)
        senario_name = base[:-4]

        mesh = Mesh()
        ###mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]])
        mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(senario_name)
        domain.set_datadir(dir)

        # Setup initial conditions
        domain.set_quantity("elevation", 0.0)
        domain.set_quantity("stage", 0.0)
        Bf = anuga.File_boundary(boundary_filename, domain, use_cache=False, verbose=False)

        # Setup boundary conditions
        domain.set_boundary({"exterior": Bf})

        for t in domain.evolve(yieldstep=5.0, finaltime=10.0):
            pass
            # print domain.write_time()
            # print "domain.time", domain.time

        # do an assertion on the time of the produced sww file
        fid = NetCDFFile(filename, netcdf_mode_r)  # Open existing file for read
        times = fid.variables["time"][:]
        # print "times", times
        # print "fid.starttime", fid.starttime
        assert num.allclose(fid.starttime, boundary_starttime)
        fid.close()

        # clean up
        os.remove(boundary_filename)
        os.remove(filename)
Example #4
0
    def setUp(self):

        def elevation_function(x, y):
            return -x
        
        """ Setup for all tests. """
        
        mesh_file = tempfile.mktemp(".tsh")    
        points = [[0.0,0.0],[6.0,0.0],[6.0,6.0],[0.0,6.0]]
        m = Mesh()
        m.add_vertices(points)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)
        
        # Create shallow water domain
        domain = anuga.Domain(mesh_file)
        os.remove(mesh_file)
 
        domain.default_order = 2

        # This test was made before tight_slope_limiters were introduced
        # Since were are testing interpolation values this is OK
        domain.tight_slope_limiters = 0
                
        # Set some field values
        domain.set_quantity('elevation', elevation_function)
        domain.set_quantity('friction', 0.03)
        domain.set_quantity('xmomentum', 3.0)
        domain.set_quantity('ymomentum', 4.0)

        ######################
        # Boundary conditions
        B = anuga.Transmissive_boundary(domain)
        domain.set_boundary( {'exterior': B})

        # This call mangles the stage values.
        domain.distribute_to_vertices_and_edges()
        domain.set_quantity('stage', 1.0)

        domain.set_name('datatest' + str(time.time()))
        domain.smooth = True
        domain.reduction = mean
        
        self.domain = domain
Example #5
0
    def test_boundary_timeII(self):
        """
        test_boundary_timeII(self):
        Test that starttime can be set in the middle of a boundary condition
        """

        boundary_starttime = 0
        boundary_filename = self.create_sww_boundary(boundary_starttime)
        #print "boundary_filename",boundary_filename

        filename = tempfile.mktemp(".sww")
        #print "filename",filename
        dir, base = os.path.split(filename)
        senario_name = base[:-4]

        mesh = Mesh()
        mesh.add_region_from_polygon([[10, 10], [90, 10], [90, 90], [10, 90]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(senario_name)
        domain.set_datadir(dir)
        new_starttime = 0.
        domain.set_starttime(new_starttime)

        # Setup initial conditions
        domain.set_quantity('elevation', 0.0)
        domain.set_quantity('stage', 0.0)
        Bf = anuga.File_boundary(boundary_filename,
                                 domain,
                                 use_cache=False,
                                 verbose=False)

        # Setup boundary conditions
        domain.set_boundary({'exterior': Bf})
        for t in domain.evolve(yieldstep=5, finaltime=9.0):
            pass
            #print domain.boundary_statistics()
            #domain.write_time()
            #print "domain.time", domain.time

        # do an assertion on the time of the produced sww file
        fid = NetCDFFile(filename, netcdf_mode_r)  #Open existing file for read
        times = fid.variables['time'][:]
        stage = fid.variables['stage'][:]
        #print stage
        #print "times", times
        #print "fid.starttime", fid.starttime
        assert num.allclose(fid.starttime, new_starttime)
        fid.close()

        #print "stage[2,0]", stage[2,0]
        msg = "This test is a bit hand crafted, based on the output file. "
        msg += "Not logic. "
        msg += "It's testing that starttime is working"
        assert num.allclose(stage[2, 0], 4.85825), msg

        # clean up
        os.remove(boundary_filename)
        os.remove(filename)
Example #6
0
    def create_sww_boundary(self, boundary_starttime):
        """
        This creates a boundary file with ;
        time     stage
        0        5
        10       2.15268
        20       13.9773
        """

        tide = 5
        boundary_filename = tempfile.mktemp(".sww")
        dir, base = os.path.split(boundary_filename)
        boundary_name = base[:-4]

        # Setup computational domain
        mesh = Mesh()
        mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(boundary_name)
        domain.set_datadir(dir)
        domain.set_starttime(boundary_starttime)
        domain.set_low_froude(0)

        # Setup initial conditions
        domain.set_quantity('elevation', 0.0)
        domain.set_quantity('stage', tide)

        # Setup boundary conditions
        Bd = anuga.Dirichlet_boundary([tide, 0.,
                                       0.])  # Constant boundary values
        Bd = anuga.Time_boundary(
            domain=domain,  # Time dependent boundary  
            function=lambda t: [t, 0.0, 0.0])
        domain.set_boundary({'exterior': Bd})
        for t in domain.evolve(yieldstep=10, finaltime=20.0):
            pass
            #print domain.boundary_statistics('stage')
            q = Bd.evaluate()

            # FIXME (Ole): This test would not have passed in
            # changeset:5846.
            msg = 'Time boundary not evaluated correctly'
            assert num.allclose(t, q[0]), msg

            #print domain.get_quantity('stage').get_values()
            #domain.write_time()
            #print "domain.time", domain.time

        return boundary_filename
Example #7
0
    def test_boundary_time(self):
        """
        test_boundary_time(self):
        test that the starttime of a boundary condition is carried thru
        to the output sww file.
        
        """

        boundary_starttime = 0
        boundary_filename = self.create_sww_boundary(boundary_starttime)
        filename = tempfile.mktemp(".sww")
        dir, base = os.path.split(filename)
        senario_name = base[:-4]

        mesh = Mesh()
        ###mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]])
        mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(senario_name)
        domain.set_datadir(dir)

        # Setup initial conditions
        domain.set_quantity('elevation', 0.0)
        domain.set_quantity('stage', 0.0)
        Bf = anuga.File_boundary(boundary_filename,
                                 domain,
                                 use_cache=False,
                                 verbose=False)

        # Setup boundary conditions
        domain.set_boundary({'exterior': Bf})

        for t in domain.evolve(yieldstep=5.0, finaltime=10.0):
            pass
            #print domain.write_time()
            #print "domain.time", domain.time

        # do an assertion on the time of the produced sww file
        fid = NetCDFFile(filename, netcdf_mode_r)  #Open existing file for read
        times = fid.variables['time'][:]
        #print "times", times
        #print "fid.starttime", fid.starttime
        assert num.allclose(fid.starttime, boundary_starttime)
        fid.close()

        # clean up
        os.remove(boundary_filename)
        os.remove(filename)
Example #8
0
def _create_mesh_from_regions(bounding_polygon,
                              boundary_tags,
                              maximum_triangle_area=None,
                              filename=None,
                              interior_regions=None,
                              interior_holes=None,
                              hole_tags=None,
                              poly_geo_reference=None,
                              mesh_geo_reference=None,
                              minimum_triangle_angle=28.0,
                              fail_if_polygons_outside=True,
                              breaklines=None,
                              verbose=True,
                              regionPtArea=None):
    """_create_mesh_from_regions - internal function.

    See create_mesh_from_regions for documentation.
    """

    # check the segment indexes - throw an error if they are out of bounds
    if boundary_tags is not None:
        max_points = len(bounding_polygon)
        for key in boundary_tags.keys():
            if len([x for x in boundary_tags[key] if x > max_points - 1]) >= 1:
                msg = 'Boundary tag %s has segment out of bounds. '\
                      %(str(key))
                msg += 'Number of points in bounding polygon = %d' % max_points
                raise SegmentError(msg)

        for i in range(max_points):
            found = False
            for tag in boundary_tags:
                if i in boundary_tags[tag]:
                    found = True
            if found is False:
                msg = 'Segment %d was not assigned a boundary_tag.' % i
                msg += 'Default tag "exterior" will be assigned to missing segment'
                #raise Exception(msg)
                # Fixme: Use proper Python warning
                if verbose: log.critical('WARNING: %s' % msg)

    #In addition I reckon the polygons could be of class Geospatial_data
    #(DSG) If polygons were classes caching would break in places.

    # Simple check
    bounding_polygon = ensure_numeric(bounding_polygon, num.float)
    msg = 'Bounding polygon must be a list of points or an Nx2 array'
    assert len(bounding_polygon.shape) == 2, msg
    assert bounding_polygon.shape[1] == 2, msg

    #
    if interior_regions is not None:

        # Test that all the interior polygons are inside the
        # bounding_poly and throw out those that aren't fully
        # included.  #Note, Both poly's have the same geo_ref,
        # therefore don't take into account # geo_ref

        polygons_inside_boundary = []
        for interior_polygon, res in interior_regions:
            indices = inside_polygon(interior_polygon,
                                     bounding_polygon,
                                     closed=True,
                                     verbose=False)

            if len(indices) <> len(interior_polygon):
                msg = 'Interior polygon %s is not fully inside'\
                      %(str(interior_polygon))
                msg += ' bounding polygon: %s.' % (str(bounding_polygon))

                if fail_if_polygons_outside is True:
                    raise PolygonError(msg)
                else:
                    msg += ' I will ignore it.'
                    log.critical(msg)

            else:
                polygons_inside_boundary.append([interior_polygon, res])

        # Record only those that were fully contained
        interior_regions = polygons_inside_boundary

# the following segment of code could be used to Test that all the
# interior polygons are inside the bounding_poly... however it might need
# to be change a bit
#
#count = 0
#for i in range(len(interior_regions)):
#    region = interior_regions[i]
#    interior_polygon = region[0]
#    if len(inside_polygon(interior_polygon, bounding_polygon,
#                   closed = True, verbose = False)) <> len(interior_polygon):
#        print 'WARNING: interior polygon %d is outside bounding polygon' %(i)
#        count += 1

#if count == 0:
#    print 'interior regions OK'
#else:
#    print 'check out your interior polygons'
#    print 'check %s in production directory' %figname
#    import sys; sys.exit()

    if interior_holes is not None:
        # Test that all the interior polygons are inside the bounding_poly
        for interior_polygon in interior_holes:

            # Test that we have a polygon
            if len(num.array(interior_polygon).flat) < 6:
                msg = 'Interior hole polygon %s has too few (<3) points.\n' \
                    %(str(interior_polygon))
                msg = msg + '(Insure that you have specified a LIST of interior hole polygons)'
                raise PolygonError(msg)

            indices = inside_polygon(interior_polygon,
                                     bounding_polygon,
                                     closed=True,
                                     verbose=False)

            if len(indices) <> len(interior_polygon):
                msg = 'Interior polygon %s is outside bounding polygon: %s'\
                      %(str(interior_polygon), str(bounding_polygon))
                raise PolygonError(msg)

    # Resolve geo referencing
    if mesh_geo_reference is None:
        xllcorner = min(bounding_polygon[:, 0])
        yllcorner = min(bounding_polygon[:, 1])
        #
        if poly_geo_reference is None:
            zone = DEFAULT_ZONE
        else:
            zone = poly_geo_reference.get_zone()
            [(xllcorner,yllcorner)] = poly_geo_reference.get_absolute( \
            [(xllcorner,yllcorner)])
        # create a geo_ref, based on the llc of the bounding_polygon
        mesh_geo_reference = Geo_reference(xllcorner=xllcorner,
                                           yllcorner=yllcorner,
                                           zone=zone)

    m = Mesh(geo_reference=mesh_geo_reference)

    # build a list of discrete segments from the breakline polygons
    if breaklines is not None:
        points, verts = polylist2points_verts(breaklines)
        m.add_points_and_segments(points, verts)

    # Do bounding polygon
    m.add_region_from_polygon(bounding_polygon,
                              segment_tags=boundary_tags,
                              geo_reference=poly_geo_reference)

    # Find one point inside region automatically
    if interior_regions is not None:
        excluded_polygons = []
        for polygon, res in interior_regions:
            excluded_polygons.append(polygon)
    else:
        excluded_polygons = None

    # Convert bounding poly to absolute values
    # this sort of thing can be fixed with the geo_points class
    if poly_geo_reference is not None:
        bounding_polygon_absolute = \
            poly_geo_reference.get_absolute(bounding_polygon)
    else:
        bounding_polygon_absolute = bounding_polygon

    inner_point = point_in_polygon(bounding_polygon_absolute)
    inner = m.add_region(inner_point[0], inner_point[1])
    inner.setMaxArea(maximum_triangle_area)

    # Do interior regions
    #    if interior_regions is not None:
    #        for polygon, res in interior_regions:
    #            m.add_region_from_polygon(polygon,
    #                                      geo_reference=poly_geo_reference)
    #            # convert bounding poly to absolute values
    #            if poly_geo_reference is not None:
    #                polygon_absolute = \
    #                    poly_geo_reference.get_absolute(polygon)
    #            else:
    #                polygon_absolute = polygon
    #            inner_point = point_in_polygon(polygon_absolute)
    #            region = m.add_region(inner_point[0], inner_point[1])
    #            region.setMaxArea(res)

    if interior_regions is not None:
        for polygon, res in interior_regions:
            m.add_region_from_polygon(polygon,
                                      max_triangle_area=res,
                                      geo_reference=poly_geo_reference)

    # Do interior holes
    if interior_holes is not None:
        for n, polygon in enumerate(interior_holes):
            try:
                tags = hole_tags[n]
            except:
                tags = {}
            m.add_hole_from_polygon(polygon,
                                    segment_tags=tags,
                                    geo_reference=poly_geo_reference)

    # 22/04/2014
    # Add user-specified point-based regions with max area
    if (regionPtArea is not None):
        for i in range(len(regionPtArea)):
            inner = m.add_region(regionPtArea[i][0], regionPtArea[i][1])
            inner.setMaxArea(regionPtArea[i][2])

    # NOTE (Ole): This was moved here as it is annoying if mesh is always
    # stored irrespective of whether the computation
    # was cached or not. This caused Domain to
    # recompute as it has meshfile as a dependency

    # Decide whether to store this mesh or return it

    if filename is None:
        return m
    else:
        if verbose: log.critical("Generating mesh to file '%s'" % filename)

        m.generate_mesh(minimum_triangle_angle=minimum_triangle_angle,
                        verbose=verbose)
        m.export_mesh_file(filename)

        return m
    def setUp(self):
        # print "****set up****"
        # Create an sww file

        # Set up an sww that has a geo ref.
        # have it cover an area in Australia.  'gong maybe
        # Don't have many triangles though!

        # Site Name:    GDA-MGA: (UTM with GRS80 ellipsoid)
        # Zone:   56
        # Easting:  222908.705  Northing: 6233785.284
        # Latitude:   -34  0 ' 0.00000 ''  Longitude: 150  0 ' 0.00000 ''
        # Grid Convergence:  -1  40 ' 43.13 ''  Point Scale: 1.00054660

        # geo-ref
        # Zone:   56
        # Easting:  220000  Northing: 6230000

        # have  a big area covered.
        mesh_file = tempfile.mktemp(".tsh")
        points_lat_long = [[-33, 152], [-35, 152], [-35, 150], [-33, 150]]
        spat = Geospatial_data(data_points=points_lat_long, points_are_lats_longs=True)
        points_ab = spat.get_data_points(absolute=True)

        geo = Geo_reference(56, 400000, 6000000)
        spat.set_geo_reference(geo)
        m = Mesh()
        m.add_vertices(spat)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)

        # Create shallow water domain
        domain = Domain(mesh_file)

        os.remove(mesh_file)

        domain.default_order = 2
        # Set some field values
        # domain.set_quantity('stage', 1.0)
        domain.set_quantity("elevation", -0.5)
        domain.set_quantity("friction", 0.03)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary({"exterior": B})

        ######################
        # Initial condition - with jumps
        bed = domain.quantities["elevation"].vertex_values
        stage = num.zeros(bed.shape, num.float)

        h = 0.3
        for i in range(stage.shape[0]):
            if i % 2 == 0:
                stage[i, :] = bed[i, :] + h
            else:
                stage[i, :] = bed[i, :]

        domain.set_quantity("stage", stage)
        domain.set_quantity("xmomentum", stage * 22.0)
        domain.set_quantity("ymomentum", stage * 55.0)

        domain.distribute_to_vertices_and_edges()

        self.domain = domain

        C = domain.get_vertex_coordinates()
        self.X = C[:, 0:6:2].copy()
        self.Y = C[:, 1:6:2].copy()

        self.F = bed

        # sww_file = tempfile.mktemp("")
        self.domain.set_name("tid_P0")
        self.domain.format = "sww"
        self.domain.smooth = True
        self.domain.reduction = mean

        sww = SWW_file(self.domain)
        sww.store_connectivity()
        sww.store_timestep()
        self.domain.time = 2.0
        sww.store_timestep()
        self.sww = sww  # so it can be deleted

        # Create another sww file
        mesh_file = tempfile.mktemp(".tsh")
        points_lat_long = [[-35, 152], [-36, 152], [-36, 150], [-35, 150]]
        spat = Geospatial_data(data_points=points_lat_long, points_are_lats_longs=True)
        points_ab = spat.get_data_points(absolute=True)

        geo = Geo_reference(56, 400000, 6000000)
        spat.set_geo_reference(geo)
        m = Mesh()
        m.add_vertices(spat)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)

        # Create shallow water domain
        domain = Domain(mesh_file)

        os.remove(mesh_file)

        domain.default_order = 2
        # Set some field values
        # domain.set_quantity('stage', 1.0)
        domain.set_quantity("elevation", -40)
        domain.set_quantity("friction", 0.03)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary({"exterior": B})

        ######################
        # Initial condition - with jumps
        bed = domain.quantities["elevation"].vertex_values
        stage = num.zeros(bed.shape, num.float)

        h = 30.0
        for i in range(stage.shape[0]):
            if i % 2 == 0:
                stage[i, :] = bed[i, :] + h
            else:
                stage[i, :] = bed[i, :]

        domain.set_quantity("stage", stage)
        domain.set_quantity("xmomentum", stage * 22.0)
        domain.set_quantity("ymomentum", stage * 55.0)

        domain.distribute_to_vertices_and_edges()

        self.domain2 = domain

        C = domain.get_vertex_coordinates()
        self.X2 = C[:, 0:6:2].copy()
        self.Y2 = C[:, 1:6:2].copy()

        self.F2 = bed

        # sww_file = tempfile.mktemp("")
        domain.set_name("tid_P1")
        domain.format = "sww"
        domain.smooth = True
        domain.reduction = mean

        sww = SWW_file(domain)
        sww.store_connectivity()
        sww.store_timestep()
        domain.time = 2.0
        sww.store_timestep()
        self.swwII = sww  # so it can be deleted

        # print "sww.filename", sww.filename
        # Create a csv file
        self.csv_file = tempfile.mktemp(".csv")
        fd = open(self.csv_file, "wb")
        writer = csv.writer(fd)
        writer.writerow(
            ["LONGITUDE", "LATITUDE", STR_VALUE_LABEL, CONT_VALUE_LABEL, "ROOF_TYPE", WALL_TYPE_LABEL, SHORE_DIST_LABEL]
        )
        writer.writerow(["151.5", "-34", "199770", "130000", "Metal", "Timber", 20.0])
        writer.writerow(["151", "-34.5", "150000", "76000", "Metal", "Double Brick", 200.0])
        writer.writerow(["151", "-34.25", "150000", "76000", "Metal", "Brick Veneer", 200.0])
        fd.close()

        # Create a csv file
        self.csv_fileII = tempfile.mktemp(".csv")
        fd = open(self.csv_fileII, "wb")
        writer = csv.writer(fd)
        writer.writerow(
            ["LONGITUDE", "LATITUDE", STR_VALUE_LABEL, CONT_VALUE_LABEL, "ROOF_TYPE", WALL_TYPE_LABEL, SHORE_DIST_LABEL]
        )
        writer.writerow(["151.5", "-34", "199770", "130000", "Metal", "Timber", 20.0])
        writer.writerow(["151", "-34.5", "150000", "76000", "Metal", "Double Brick", 200.0])
        writer.writerow(["151", "-34.25", "150000", "76000", "Metal", "Brick Veneer", 200.0])
        fd.close()
    def test_inundation_damage_list(self):

        # create mesh
        mesh_file = tempfile.mktemp(".tsh")
        points = [[0.0, 0.0], [6.0, 0.0], [6.0, 6.0], [0.0, 6.0]]
        m = Mesh()
        m.add_vertices(points)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)

        # Create shallow water domain
        domain = Domain(mesh_file)
        os.remove(mesh_file)

        domain.default_order = 2

        # Set some field values
        domain.set_quantity("elevation", elevation_function)
        domain.set_quantity("friction", 0.03)
        domain.set_quantity("xmomentum", 22.0)
        domain.set_quantity("ymomentum", 55.0)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary({"exterior": B})

        # This call mangles the stage values.
        domain.distribute_to_vertices_and_edges()
        domain.set_quantity("stage", 0.3)

        # sww_file = tempfile.mktemp("")
        domain.set_name("datatest" + str(time.time()))
        domain.format = "sww"
        domain.smooth = True
        domain.reduction = mean

        sww = SWW_file(domain)
        sww.store_connectivity()
        sww.store_timestep()
        domain.set_quantity("stage", -0.3)
        domain.time = 2.0
        sww.store_timestep()

        # Create a csv file
        csv_file = tempfile.mktemp(".csv")
        fd = open(csv_file, "wb")
        writer = csv.writer(fd)
        writer.writerow(["x", "y", STR_VALUE_LABEL, CONT_VALUE_LABEL, "ROOF_TYPE", WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow([5.5, 0.5, "10", "130000", "Metal", "Timber", 20])
        writer.writerow([4.5, 1.0, "150", "76000", "Metal", "Double Brick", 20])
        writer.writerow([0.1, 1.5, "100", "76000", "Metal", "Brick Veneer", 300])
        writer.writerow([6.1, 1.5, "100", "76000", "Metal", "Brick Veneer", 300])
        fd.close()

        extension = ".csv"
        csv_fileII = tempfile.mktemp(extension)
        fd = open(csv_fileII, "wb")
        writer = csv.writer(fd)
        writer.writerow(["x", "y", STR_VALUE_LABEL, CONT_VALUE_LABEL, "ROOF_TYPE", WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow([5.5, 0.5, "10", "130000", "Metal", "Timber", 20])
        writer.writerow([4.5, 1.0, "150", "76000", "Metal", "Double Brick", 20])
        writer.writerow([0.1, 1.5, "100", "76000", "Metal", "Brick Veneer", 300])
        writer.writerow([6.1, 1.5, "100", "76000", "Metal", "Brick Veneer", 300])
        fd.close()

        sww_file = domain.get_name() + "." + domain.format
        # print "sww_file",sww_file
        marker = "_gosh"
        inundation_damage(sww_file, [csv_file, csv_fileII], exposure_file_out_marker=marker, verbose=False)

        # Test one file
        csv_handle = Exposure(csv_file[:-4] + marker + extension)
        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
        # print "struct_loss",struct_loss
        struct_loss = [float(x) for x in struct_loss]
        # pprint(struct_loss)
        assert num.allclose(struct_loss, [10.0, 150.0, 66.55333347876866, 0.0])
        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
        # print "depth",depth
        depth = [float(x) for x in depth]
        assert num.allclose(depth, [3.000000011920929, 2.9166666785875957, 2.2666666785875957, -0.3])

        # Test another file
        csv_handle = Exposure(csv_fileII[:-4] + marker + extension)
        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
        # print "struct_loss",struct_loss
        struct_loss = [float(x) for x in struct_loss]

        # pprint(struct_loss)
        assert num.allclose(struct_loss, [10.0, 150.0, 66.553333478768664, 0.0])
        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
        # print "depth",depth
        depth = [float(x) for x in depth]
        assert num.allclose(depth, [3.000000011920929, 2.9166666785875957, 2.2666666785875957, -0.3])
        os.remove(sww.filename)
        os.remove(csv_file)
        os.remove(csv_fileII)
    def _build_regular_mesh_dict(self,
                                 maxArea=1000,
                                 is_segments=True,
                                 save=False,
                                 geo=None):
        # make a normalised mesh
        # pretty regular size, with some segments thrown in.

        # don't pass in the geo ref.
        # it is applied in domain
        m = Mesh()  #geo_reference=geo)
        m.addUserVertex(0, 0)
        m.addUserVertex(1.0, 0)
        m.addUserVertex(0, 1.0)
        m.addUserVertex(1.0, 1.0)

        m.auto_segment(alpha=100)

        if is_segments:
            dict = {}
            dict['points'] = [[.10, .10], [.90, .20]]
            dict['segments'] = [[0, 1]]
            dict['segment_tags'] = ['wall1']
            m.addVertsSegs(dict)

            dict = {}
            dict['points'] = [[.10, .90], [.40, .20]]
            dict['segments'] = [[0, 1]]
            dict['segment_tags'] = ['wall2']
            m.addVertsSegs(dict)

            dict = {}
            dict['points'] = [[.20, .90], [.60, .60]]
            dict['segments'] = [[0, 1]]
            dict['segment_tags'] = ['wall3']
            m.addVertsSegs(dict)

            dict = {}
            dict['points'] = [[.60, .20], [.90, .90]]
            dict['segments'] = [[0, 1]]
            dict['segment_tags'] = ['wall4']
            m.addVertsSegs(dict)

        m.generateMesh(mode="Q", maxArea=maxArea, minAngle=20.0)
        if save is True:
            m.export_mesh_file("aaaa.tsh")
        mesh_dict = m.Mesh2IOTriangulationDict()

        #Add vert attribute info to the mesh
        mesh_dict['vertex_attributes'] = []
        # There has to be a better way of doing this..
        for vertex in mesh_dict['vertices']:
            mesh_dict['vertex_attributes'].append([10.0])

        return mesh_dict
    def _build_regular_mesh_dict(self,
                                 maxArea=1000,
                                 is_segments=True,
                                 save=False,
                                 geo=None):
      # make a normalised mesh
        # pretty regular size, with some segments thrown in.

        # don't pass in the geo ref.
        # it is applied in domain
        m = Mesh() #geo_reference=geo)
        m.addUserVertex(0,0)
        m.addUserVertex(1.0,0)
        m.addUserVertex(0,1.0)
        m.addUserVertex(1.0,1.0)
        
        m.auto_segment(alpha = 100 )

        if is_segments:
            dict = {}
            dict['points'] = [[.10,.10],[.90,.20]]
            dict['segments'] = [[0,1]] 
            dict['segment_tags'] = ['wall1']   
            m.addVertsSegs(dict)
    
            dict = {}
            dict['points'] = [[.10,.90],[.40,.20]]
            dict['segments'] = [[0,1]] 
            dict['segment_tags'] = ['wall2']   
            m.addVertsSegs(dict)
        
            dict = {}
            dict['points'] = [[.20,.90],[.60,.60]]
            dict['segments'] = [[0,1]] 
            dict['segment_tags'] = ['wall3'] 
            m.addVertsSegs(dict)
        
            dict = {}
            dict['points'] = [[.60,.20],[.90,.90]]
            dict['segments'] = [[0,1]] 
            dict['segment_tags'] = ['wall4']   
            m.addVertsSegs(dict)

        m.generateMesh(mode = "Q", maxArea = maxArea, minAngle=20.0)       
        if save is True:
            m.export_mesh_file("aaaa.tsh")
        mesh_dict =  m.Mesh2IOTriangulationDict()

        #Add vert attribute info to the mesh
        mesh_dict['vertex_attributes'] = []
        # There has to be a better way of doing this..
        for vertex in mesh_dict['vertices']:
            mesh_dict['vertex_attributes'].append([10.0])

        return mesh_dict
def sts2sww_mesh(basename_in, basename_out=None, 
                 spatial_thinning=1, verbose=False):
    
    from anuga.mesh_engine.mesh_engine import NoTrianglesError
    from anuga.pmesh.mesh import Mesh
    if verbose:
        print("Starting sts2sww_mesh")
    
    mean_stage=0.
    zscale=1.

    if (basename_in[:-4]=='.sts'):
        stsname = basename_in
    else: 
        stsname = basename_in + '.sts'

    if verbose: print("Reading sts NetCDF file: %s" %stsname)
    infile = NetCDFFile(stsname, netcdf_mode_r)
    cellsize = infile.cellsize
    ncols = infile.ncols
    nrows = infile.nrows
    no_data = infile.no_data
    refzone = infile.zone
    x_origin = infile.xllcorner
    y_origin = infile.yllcorner
    origin = num.array([x_origin, y_origin])
    x = infile.variables['x'][:]
    y = infile.variables['y'][:]
    times = infile.variables['time'][:]
    wind_speed_full = infile.variables['wind_speed'][:]
    wind_angle_full = infile.variables['wind_angle'][:]
    pressure_full   =   infile.variables['barometric_pressure'][:]
    infile.close()

    number_of_points = nrows*ncols
    points_utm = num.zeros((number_of_points,2),num.float)
    points_utm[:,0]=x+x_origin
    points_utm[:,1]=y+y_origin

    thinned_indices=[]
    for i in range(number_of_points):
        if (old_div(i,ncols)==0 or old_div(i,ncols)==ncols-1 or (old_div(i,ncols))%(spatial_thinning)==0):
            if ( i%(spatial_thinning)==0 or i%nrows==0 or i%nrows==nrows-1 ):  
                thinned_indices.append(i)

    #Spatial thinning
    points_utm=points_utm[thinned_indices]
    number_of_points = points_utm.shape[0]
    number_of_timesteps = wind_speed_full.shape[0]
    wind_speed = num.empty((number_of_timesteps,number_of_points),dtype=float)
    wind_angle = num.empty((number_of_timesteps,number_of_points),dtype=float)
    barometric_pressure   = num.empty((number_of_timesteps,number_of_points),dtype=float)
    if verbose:
        print("Total number of points: ", nrows*ncols)
        print("Number of thinned points: ", number_of_points)
    for i in range(number_of_timesteps):
        wind_speed[i] = wind_speed_full[i,thinned_indices]
        wind_angle[i] = wind_angle_full[i,thinned_indices]
        barometric_pressure[i]   = pressure_full[i,thinned_indices]

    #P.plot(points_utm[:,0],points_utm[:,1],'ro')
    #P.show()

    if verbose:
        print("Generating sww triangulation of gems data")

    mesh = Mesh()
    mesh.add_vertices(points_utm)
    mesh.auto_segment(smooth_indents=True, expand_pinch=True)
    mesh.auto_segment(mesh.shape.get_alpha() * 1.1)
    try:
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)
    except NoTrianglesError:
        # This is a bit of a hack, going in and changing the data structure.
        mesh.holes = []
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)

    mesh_dic = mesh.Mesh2MeshList()

    points_utm=ensure_numeric(points_utm)
    assert num.alltrue(ensure_numeric(mesh_dic['generatedpointlist'])
                       == ensure_numeric(points_utm))

    volumes = mesh_dic['generatedtrianglelist']

    # Write sww intro and grid stuff.
    if (basename_out is not None and basename_out[:-4]=='.sww'): 
        swwname = basename_out
    else: 
        swwname = basename_in + '.sww'

    if verbose: 'Output to %s' % swwname

    if verbose:
        print("Writing sww wind and pressure field file")
    outfile = NetCDFFile(swwname, netcdf_mode_w)
    sww = Write_sww([], ['wind_speed','wind_angle','barometric_pressure'])
    sww.store_header(outfile, times, len(volumes), len(points_utm),
                     verbose=verbose, sww_precision='d')
    outfile.mean_stage = mean_stage
    outfile.zscale = zscale
    sww.store_triangulation(outfile, points_utm, volumes,
                            refzone,  
                            new_origin=origin, #check effect of this line
                            verbose=verbose)

    if verbose: 
        print('Converting quantities')
    
    # Read in a time slice from the sts file and write it to the SWW file

    #print wind_angle[0,:10]
    for i in range(len(times)):
        sww.store_quantities(outfile,
                             slice_index=i,
                             verbose=verbose,
                             wind_speed=wind_speed[i,:],
                             wind_angle=wind_angle[i,:],
                             barometric_pressure=barometric_pressure[i,:],
                             sww_precision=num.float)

    if verbose: 
        sww.verbose_quantities(outfile)
    outfile.close()
Example #14
0
    def setUp(self):
        #print "****set up****"
        # Create an sww file
        
        # Set up an sww that has a geo ref.
        # have it cover an area in Australia.  'gong maybe
        #Don't have many triangles though!
        
        #Site Name:    GDA-MGA: (UTM with GRS80 ellipsoid) 
        #Zone:   56    
        #Easting:  222908.705  Northing: 6233785.284 
        #Latitude:   -34  0 ' 0.00000 ''  Longitude: 150  0 ' 0.00000 '' 
        #Grid Convergence:  -1  40 ' 43.13 ''  Point Scale: 1.00054660

        #geo-ref
        #Zone:   56    
        #Easting:  220000  Northing: 6230000 


        #have  a big area covered.
        mesh_file = tempfile.mktemp(".tsh")
        points_lat_long = [[-33,152],[-35,152],[-35,150],[-33,150]]
        spat = Geospatial_data(data_points=points_lat_long,
                               points_are_lats_longs=True)
        points_ab = spat.get_data_points( absolute = True)
        
        geo =  Geo_reference(56,400000,6000000)
        spat.set_geo_reference(geo)
        m = Mesh()
        m.add_vertices(spat)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)
        
        #Create shallow water domain
        domain = Domain(mesh_file)

        os.remove(mesh_file)
        
        domain.default_order=2
        #Set some field values
        #domain.set_quantity('stage', 1.0)
        domain.set_quantity('elevation', -0.5)
        domain.set_quantity('friction', 0.03)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary( {'exterior': B})

        ######################
        #Initial condition - with jumps
        bed = domain.quantities['elevation'].vertex_values
        stage = num.zeros(bed.shape, num.float)

        h = 0.3
        for i in range(stage.shape[0]):
            if i % 2 == 0:
                stage[i,:] = bed[i,:] + h
            else:
                stage[i,:] = bed[i,:]

        domain.set_quantity('stage', stage)
        domain.set_quantity('xmomentum', stage*22.0)
        domain.set_quantity('ymomentum', stage*55.0)

        domain.distribute_to_vertices_and_edges()

        self.domain = domain

        C = domain.get_vertex_coordinates()
        self.X = C[:,0:6:2].copy()
        self.Y = C[:,1:6:2].copy()

        self.F = bed
      
        #sww_file = tempfile.mktemp("")
        self.domain.set_name('tid_P0')
        self.domain.format = 'sww'
        self.domain.smooth = True
        self.domain.reduction = mean

        sww = SWW_file(self.domain)
        sww.store_connectivity()
        sww.store_timestep()
        self.domain.time = 2.
        sww.store_timestep()
        self.sww = sww # so it can be deleted
        
        #Create another sww file
        mesh_file = tempfile.mktemp(".tsh")
        points_lat_long = [[-35,152],[-36,152],[-36,150],[-35,150]]
        spat = Geospatial_data(data_points=points_lat_long,
                               points_are_lats_longs=True)
        points_ab = spat.get_data_points( absolute = True)
        
        geo =  Geo_reference(56,400000,6000000)
        spat.set_geo_reference(geo)
        m = Mesh()
        m.add_vertices(spat)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)
        
        #Create shallow water domain
        domain = Domain(mesh_file)

        os.remove(mesh_file)
        
        domain.default_order=2
        #Set some field values
        #domain.set_quantity('stage', 1.0)
        domain.set_quantity('elevation', -40)
        domain.set_quantity('friction', 0.03)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary( {'exterior': B})

        ######################
        #Initial condition - with jumps
        bed = domain.quantities['elevation'].vertex_values
        stage = num.zeros(bed.shape, num.float)

        h = 30.
        for i in range(stage.shape[0]):
            if i % 2 == 0:
                stage[i,:] = bed[i,:] + h
            else:
                stage[i,:] = bed[i,:]

        domain.set_quantity('stage', stage)
        domain.set_quantity('xmomentum', stage*22.0)
        domain.set_quantity('ymomentum', stage*55.0)

        domain.distribute_to_vertices_and_edges()

        self.domain2 = domain

        C = domain.get_vertex_coordinates()
        self.X2 = C[:,0:6:2].copy()
        self.Y2 = C[:,1:6:2].copy()

        self.F2 = bed
      
        #sww_file = tempfile.mktemp("")
        domain.set_name('tid_P1')
        domain.format = 'sww'
        domain.smooth = True
        domain.reduction = mean

        sww = SWW_file(domain)
        sww.store_connectivity()
        sww.store_timestep()
        domain.time = 2.
        sww.store_timestep()
        self.swwII = sww # so it can be deleted

        # print "sww.filename", sww.filename
        #Create a csv file
        self.csv_file = tempfile.mktemp(".csv")
        fd = open(self.csv_file,'wb')
        writer = csv.writer(fd)
        writer.writerow(['LONGITUDE','LATITUDE',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow(['151.5','-34','199770','130000','Metal','Timber',20.])
        writer.writerow(['151','-34.5','150000','76000','Metal','Double Brick',200.])
        writer.writerow(['151','-34.25','150000','76000','Metal','Brick Veneer',200.])
        fd.close()

        #Create a csv file
        self.csv_fileII = tempfile.mktemp(".csv")
        fd = open(self.csv_fileII,'wb')
        writer = csv.writer(fd)
        writer.writerow(['LONGITUDE','LATITUDE',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow(['151.5','-34','199770','130000','Metal','Timber',20.])
        writer.writerow(['151','-34.5','150000','76000','Metal','Double Brick',200.])
        writer.writerow(['151','-34.25','150000','76000','Metal','Brick Veneer',200.])
        fd.close()
Example #15
0
    def test_inundation_damage_list(self):

        # create mesh
        mesh_file = tempfile.mktemp(".tsh")    
        points = [[0.0,0.0],[6.0,0.0],[6.0,6.0],[0.0,6.0]]
        m = Mesh()
        m.add_vertices(points)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)
        
        #Create shallow water domain
        domain = Domain(mesh_file)
        os.remove(mesh_file)
        
        domain.default_order=2

        #Set some field values
        domain.set_quantity('elevation', elevation_function)
        domain.set_quantity('friction', 0.03)
        domain.set_quantity('xmomentum', 22.0)
        domain.set_quantity('ymomentum', 55.0)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary( {'exterior': B})

        # This call mangles the stage values.
        domain.distribute_to_vertices_and_edges()
        domain.set_quantity('stage', 0.3)

        #sww_file = tempfile.mktemp("")
        domain.set_name('datatest' + str(time.time()))
        domain.format = 'sww'
        domain.smooth = True
        domain.reduction = mean

        sww = SWW_file(domain)
        sww.store_connectivity()
        sww.store_timestep()
        domain.set_quantity('stage', -0.3)
        domain.time = 2.
        sww.store_timestep()
        
        #Create a csv file
        csv_file = tempfile.mktemp(".csv")
        fd = open(csv_file,'wb')
        writer = csv.writer(fd)
        writer.writerow(['x','y',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow([5.5,0.5,'10','130000','Metal','Timber',20])
        writer.writerow([4.5,1.0,'150','76000','Metal','Double Brick',20])
        writer.writerow([0.1,1.5,'100','76000','Metal','Brick Veneer',300])
        writer.writerow([6.1,1.5,'100','76000','Metal','Brick Veneer',300])
        fd.close()
        
        extension = ".csv"
        csv_fileII = tempfile.mktemp(extension)
        fd = open(csv_fileII,'wb')
        writer = csv.writer(fd)
        writer.writerow(['x','y',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow([5.5,0.5,'10','130000','Metal','Timber',20])
        writer.writerow([4.5,1.0,'150','76000','Metal','Double Brick',20])
        writer.writerow([0.1,1.5,'100','76000','Metal','Brick Veneer',300])
        writer.writerow([6.1,1.5,'100','76000','Metal','Brick Veneer',300])
        fd.close()
        
        sww_file = domain.get_name() + "." + domain.format
        #print "sww_file",sww_file
        marker='_gosh'
        inundation_damage(sww_file, [csv_file, csv_fileII],
                          exposure_file_out_marker=marker,
                          verbose=False)

        # Test one file
        csv_handle = Exposure(csv_file[:-4]+marker+extension)
        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
        #print "struct_loss",struct_loss
        struct_loss = [float(x) for x in struct_loss]
        #pprint(struct_loss)
        assert num.allclose(struct_loss,[10.0, 150.0, 66.55333347876866, 0.0])       
        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
        #print "depth",depth
        depth = [float(x) for x in depth]
        assert num.allclose(depth, [3.000000011920929, 2.9166666785875957, 2.2666666785875957, -0.3])
       
        # Test another file
        csv_handle = Exposure(csv_fileII[:-4]+marker+extension)
        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
        #print "struct_loss",struct_loss
        struct_loss = [float(x) for x in struct_loss]

        #pprint(struct_loss)
        assert num.allclose(struct_loss, [10.0, 150.0, 66.553333478768664, 0.0])       
        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
        #print "depth",depth
        depth = [float(x) for x in depth]
        assert num.allclose(depth,[3.000000011920929, 2.9166666785875957, 2.2666666785875957, -0.3]) 
        os.remove(sww.filename)
        os.remove(csv_file)
        os.remove(csv_fileII)
def _create_mesh_from_regions(bounding_polygon,
                              boundary_tags,
                              maximum_triangle_area=None,
                              filename=None,                              
                              interior_regions=None,
                              interior_holes=None,
                              hole_tags=None,
                              poly_geo_reference=None,
                              mesh_geo_reference=None,
                              minimum_triangle_angle=28.0,
                              fail_if_polygons_outside=True,
                              breaklines=None,
                              verbose=True,
                              regionPtArea=None):
    """_create_mesh_from_regions - internal function.

    See create_mesh_from_regions for documentation.
    """

    # check the segment indexes - throw an error if they are out of bounds
    if boundary_tags is not None:
        max_points = len(bounding_polygon)
        for key in boundary_tags.keys():
            if len([x for x in boundary_tags[key] if x > max_points-1]) >= 1:
                msg = 'Boundary tag %s has segment out of bounds. '\
                      %(str(key))
                msg += 'Number of points in bounding polygon = %d' % max_points
                raise SegmentError(msg)

        for i in range(max_points):
            found = False
            for tag in boundary_tags:
                if i in boundary_tags[tag]:
                    found = True
            if found is False:
                msg = 'Segment %d was not assigned a boundary_tag.' % i
                msg +=  'Default tag "exterior" will be assigned to missing segment'
                #raise Exception(msg)
                # Fixme: Use proper Python warning
                if verbose: log.critical('WARNING: %s' % msg)
                

    
    #In addition I reckon the polygons could be of class Geospatial_data 
    #(DSG) If polygons were classes caching would break in places.

    # Simple check
    bounding_polygon = ensure_numeric(bounding_polygon, num.float)
    msg = 'Bounding polygon must be a list of points or an Nx2 array'
    assert len(bounding_polygon.shape) == 2, msg
    assert bounding_polygon.shape[1] == 2, msg    

    # 
    if interior_regions is not None:
        
        # Test that all the interior polygons are inside the
        # bounding_poly and throw out those that aren't fully
        # included.  #Note, Both poly's have the same geo_ref,
        # therefore don't take into account # geo_ref


        polygons_inside_boundary = []
        for interior_polygon, res in interior_regions:
            indices = inside_polygon(interior_polygon, bounding_polygon,
                                     closed = True, verbose = False)
    
            if len(indices) <> len(interior_polygon): 
                msg = 'Interior polygon %s is not fully inside'\
                      %(str(interior_polygon))
                msg += ' bounding polygon: %s.' %(str(bounding_polygon))

                if fail_if_polygons_outside is True:
                    raise PolygonError(msg)
                else:
                    msg += ' I will ignore it.'
                    log.critical(msg)

            else:
                polygons_inside_boundary.append([interior_polygon, res])
                
        # Record only those that were fully contained        
        interior_regions = polygons_inside_boundary

            
    
# the following segment of code could be used to Test that all the 
# interior polygons are inside the bounding_poly... however it might need 
# to be change a bit   
#
#count = 0
#for i in range(len(interior_regions)):
#    region = interior_regions[i]
#    interior_polygon = region[0]
#    if len(inside_polygon(interior_polygon, bounding_polygon,
#                   closed = True, verbose = False)) <> len(interior_polygon):
#        print 'WARNING: interior polygon %d is outside bounding polygon' %(i)
#        count += 1

#if count == 0:
#    print 'interior regions OK'
#else:
#    print 'check out your interior polygons'
#    print 'check %s in production directory' %figname
#    import sys; sys.exit()

    if interior_holes is not None:        
        # Test that all the interior polygons are inside the bounding_poly
        for interior_polygon in interior_holes:

            # Test that we have a polygon
            if len(num.array(interior_polygon).flat) < 6:
                msg = 'Interior hole polygon %s has too few (<3) points.\n' \
                    %(str(interior_polygon))
                msg = msg + '(Insure that you have specified a LIST of interior hole polygons)'
                raise PolygonError(msg)
                    
            indices = inside_polygon(interior_polygon, bounding_polygon,
                                     closed = True, verbose = False)

    
            if len(indices) <> len(interior_polygon): 
                msg = 'Interior polygon %s is outside bounding polygon: %s'\
                      %(str(interior_polygon), str(bounding_polygon))
                raise PolygonError(msg)

    # Resolve geo referencing        
    if mesh_geo_reference is None:
        xllcorner = min(bounding_polygon[:,0])
        yllcorner = min(bounding_polygon[:,1])    
        #
        if poly_geo_reference is None:
            zone = DEFAULT_ZONE
        else:
            zone = poly_geo_reference.get_zone()
            [(xllcorner,yllcorner)] = poly_geo_reference.get_absolute( \
            [(xllcorner,yllcorner)])
        # create a geo_ref, based on the llc of the bounding_polygon
        mesh_geo_reference = Geo_reference(xllcorner = xllcorner,
                                           yllcorner = yllcorner,
                                           zone = zone)

    m = Mesh(geo_reference=mesh_geo_reference)

    # build a list of discrete segments from the breakline polygons
    if breaklines is not None:
        points, verts = polylist2points_verts(breaklines)
        m.add_points_and_segments(points, verts)

    # Do bounding polygon
    m.add_region_from_polygon(bounding_polygon,
                              segment_tags=boundary_tags,
                              geo_reference=poly_geo_reference)

    # Find one point inside region automatically
    if interior_regions is not None:
        excluded_polygons = []        
        for polygon, res in interior_regions:
            excluded_polygons.append( polygon )
    else:
        excluded_polygons = None

    # Convert bounding poly to absolute values
    # this sort of thing can be fixed with the geo_points class
    if poly_geo_reference is not None:
        bounding_polygon_absolute = \
            poly_geo_reference.get_absolute(bounding_polygon)
    else:
        bounding_polygon_absolute = bounding_polygon
   
    inner_point = point_in_polygon(bounding_polygon_absolute)
    inner = m.add_region(inner_point[0], inner_point[1])
    inner.setMaxArea(maximum_triangle_area)

    # Do interior regions
#    if interior_regions is not None:    
#        for polygon, res in interior_regions:
#            m.add_region_from_polygon(polygon,
#                                      geo_reference=poly_geo_reference)
#            # convert bounding poly to absolute values
#            if poly_geo_reference is not None:
#                polygon_absolute = \
#                    poly_geo_reference.get_absolute(polygon)
#            else:
#                polygon_absolute = polygon
#            inner_point = point_in_polygon(polygon_absolute)
#            region = m.add_region(inner_point[0], inner_point[1])
#            region.setMaxArea(res)
            
            
    if interior_regions is not None:    
        for polygon, res in interior_regions:
            m.add_region_from_polygon(polygon,
                                      max_triangle_area=res,
                                      geo_reference=poly_geo_reference)
    
            
    # Do interior holes
    if interior_holes is not None:    
        for n, polygon in enumerate(interior_holes):
            try:
                tags = hole_tags[n]
            except:
                tags = {}
            m.add_hole_from_polygon(polygon,
                                    segment_tags=tags,
                                    geo_reference=poly_geo_reference)


    # 22/04/2014
    # Add user-specified point-based regions with max area
    if(regionPtArea is not None):
        for i in range(len(regionPtArea)):
            inner = m.add_region(regionPtArea[i][0], regionPtArea[i][1])
            inner.setMaxArea(regionPtArea[i][2])
        
               


    # NOTE (Ole): This was moved here as it is annoying if mesh is always
    # stored irrespective of whether the computation
    # was cached or not. This caused Domain to
    # recompute as it has meshfile as a dependency

    # Decide whether to store this mesh or return it 
    
    
    if filename is None:
        return m
    else:
        if verbose: log.critical("Generating mesh to file '%s'" % filename)
      
        m.generate_mesh(minimum_triangle_angle=minimum_triangle_angle,
                        verbose=verbose)
        m.export_mesh_file(filename)

        return m
Example #17
0
    def test_import_ungenerate_file_different_region_tags(self):

        fileName = tempfile.mktemp(".txt")
        file = open(fileName, "w")
        file.write("         1       ??      ??\n\
       10.0       10.0\n\
       11.0       10.0\n\
       11.0       11.0\n\
       10.0       11.0\n\
       10.0       10.0\n\
END\n\
         2      ?? ??\n\
       20.0       20.0\n\
       20.0       30.0\n\
       30.0       30.0\n\
END\n\
END\n")
        file.close()

        a = Vertex(0.0, 0.0)  #, attributes = [1.1])
        b = Vertex(0.0, 40.0)  #, attributes = [1.2])
        c = Vertex(40.0, 40.0)  #, attributes = [1.3])
        d = Vertex(40.0, 0.0)  #, attributes = [1.4])

        s1 = Segment(a, b)
        s2 = Segment(b, c)
        s3 = Segment(c, d)
        s4 = Segment(d, a)

        m = Mesh(userVertices=[a, b, c, d], userSegments=[s1, s2, s3, s4])
        dict = load_ungenerate(fileName)
        #os.remove(fileName)

        tag = "DSG"
        Segment.set_default_tag(tag)
        m.addVertsSegs(dict)

        self.assertTrue(
            len(m.userSegments) == 11, 'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11, 'Wrong vertex list length.')

        # Test the method
        a = Vertex(0.0, 0.0)  #, attributes = [1.1])
        b = Vertex(0.0, 40.0)  #, attributes = [1.2])
        c = Vertex(40.0, 40.0)  #, attributes = [1.3])
        d = Vertex(40.0, 0.0)  #, attributes = [1.4])

        s1 = Segment(a, b)
        s2 = Segment(b, c)
        s3 = Segment(c, d)
        s4 = Segment(d, a)

        m = Mesh(userVertices=[a, b, c, d], userSegments=[s1, s2, s3, s4])
        tag = "DSG"
        initial_tag = "PIG"
        Segment.set_default_tag(initial_tag)
        m.import_ungenerate_file(fileName,
                                 tag=tag,
                                 region_tag=["swamp", "coastalp"])

        os.remove(fileName)

        self.assertTrue(Segment.get_default_tag() == initial_tag,
                        'Wrong segment list length.')
        m.export_mesh_file("swamp.tsh")
        #print "m.userSegments",m.userSegments
        self.assertTrue(
            len(m.userSegments) == 11, 'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11, 'Wrong vertex list length.')
        self.assertTrue(len(m.regions) == 2, 'Wrong regions list length.')
        self.assertTrue(m.regions[0].getTag() == "swamp", 'Wrong regions tag.')
        self.assertTrue(m.regions[1].getTag() == "coastalp",
                        'Wrong regions 1 tag.')

        # have to reset this , since it's a class attribute
        Segment.set_default_tag("")
Example #18
0
    def test_ungenerateFileLoading(self):

        fileName = tempfile.mktemp(".txt")
        file = open(fileName, "w")
        file.write("         1       ??      ??\n\
       0.0       0.0\n\
       1.0       0.0\n\
       1.0       1.0\n\
       0.0       1.0\n\
       0.0       0.0\n\
END\n\
         2      ?? ??\n\
       10.0       10.0\n\
       10.0       20.0\n\
       20.0       20.0\n\
       10.0       10.0\n\
END\n\
END\n")
        file.close()

        a = Vertex(0.0, 0.0)  #, attributes = [1.1])
        b = Vertex(0.0, 40.0)  #, attributes = [1.2])
        c = Vertex(40.0, 40.0)  #, attributes = [1.3])
        d = Vertex(40.0, 0.0)  #, attributes = [1.4])

        s1 = Segment(a, b)
        s2 = Segment(b, c)
        s3 = Segment(c, d)
        s4 = Segment(d, a)

        m = Mesh(userVertices=[a, b, c, d], userSegments=[s1, s2, s3, s4])
        dict = load_ungenerate(fileName)
        #os.remove(fileName)

        tag = "DSG"
        Segment.set_default_tag(tag)
        m.addVertsSegs(dict)

        # have to reset this , since it's a class attribute
        Segment.set_default_tag("")

        self.assertTrue(
            len(m.userSegments) == 11, 'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11, 'Wrong vertex list length.')
        self.assertTrue(m.userSegments[10].vertices[0] == m.userVertices[10],
                        'bad vertex on segment.')
        self.assertTrue(m.userSegments[10].vertices[1] == m.userVertices[8],
                        'Bad segment.')
        self.assertTrue(m.userSegments[10].tag == tag, 'wrong tag.')

        ## let's test the method
        a = Vertex(0.0, 0.0)  #, attributes = [1.1])
        b = Vertex(0.0, 40.0)  #, attributes = [1.2])
        c = Vertex(40.0, 40.0)  #, attributes = [1.3])
        d = Vertex(40.0, 0.0)  #, attributes = [1.4])

        s1 = Segment(a, b)
        s2 = Segment(b, c)
        s3 = Segment(c, d)
        s4 = Segment(d, a)

        m = Mesh(userVertices=[a, b, c, d], userSegments=[s1, s2, s3, s4])

        tag = "DSG"
        initial_tag = "PIG"
        Segment.set_default_tag(initial_tag)
        m.import_ungenerate_file(fileName, tag=tag)

        os.remove(fileName)

        self.assertTrue(Segment.get_default_tag() == initial_tag,
                        'Wrong segment list length.')

        # have to reset this , since it's a class attribute
        Segment.set_default_tag("")

        self.assertTrue(
            len(m.userSegments) == 11, 'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11, 'Wrong vertex list length.')
        self.assertTrue(m.userSegments[10].vertices[0] == m.userVertices[10],
                        'bad vertex on segment.')
        self.assertTrue(m.userSegments[10].vertices[1] == m.userVertices[8],
                        'Bad segment.')
        self.assertTrue(m.userSegments[10].tag == tag, 'wrong tag.')
Example #19
0
def urs_ungridded2sww(basename_in='o', basename_out=None, verbose=False,
                      mint=None, maxt=None,
                      mean_stage=0,
                      origin=None,
                      hole_points_UTM=None,
                      zscale=1):
    """
    Convert URS C binary format for wave propagation to
    sww format native to abstract_2d_finite_volumes.

    Specify only basename_in and read files of the form
    basefilename-z-mux, basefilename-e-mux and
    basefilename-n-mux containing relative height,
    x-velocity and y-velocity, respectively.

    Also convert latitude and longitude to UTM. All coordinates are
    assumed to be given in the GDA94 datum. The latitude and longitude
    information is assumed ungridded grid.

    min's and max's: If omitted - full extend is used.
    To include a value min ans max may equal it.
    Lat and lon are assumed to be in decimal degrees.

    origin is a 3-tuple with geo referenced
    UTM coordinates (zone, easting, northing)
    It will be the origin of the sww file. This shouldn't be used,
    since all of anuga should be able to handle an arbitary origin.
    The mux point info is NOT relative to this origin.

    URS C binary format has data organised as TIME, LONGITUDE, LATITUDE
    which means that latitude is the fastest
    varying dimension (row major order, so to speak)

    In URS C binary the latitudes and longitudes are in assending order.

    Note, interpolations of the resulting sww file will be different
    from results of urs2sww.  This is due to the interpolation
    function used, and the different grid structure between urs2sww
    and this function.

    Interpolating data that has an underlying gridded source can
    easily end up with different values, depending on the underlying
    mesh.

    consider these 4 points
    50  -50

    0     0

    The grid can be
     -
    |\|   A
     -
     or;
      -
     |/|  B
      -

    If a point is just below the center of the midpoint, it will have a
    +ve value in grid A and a -ve value in grid B.
    """

    from anuga.mesh_engine.mesh_engine import NoTrianglesError
    from anuga.pmesh.mesh import Mesh

    files_in = [basename_in + WAVEHEIGHT_MUX_LABEL,
                basename_in + EAST_VELOCITY_LABEL,
                basename_in + NORTH_VELOCITY_LABEL]
    quantities = ['HA','UA','VA']

    # instantiate urs_points of the three mux files.
    mux = {}
    for quantity, file in map(None, quantities, files_in):
        mux[quantity] = Read_urs(file)

    # Could check that the depth is the same. (hashing)

    # handle to a mux file to do depth stuff
    a_mux = mux[quantities[0]]

    # Convert to utm
    lat = a_mux.lonlatdep[:,1]
    long = a_mux.lonlatdep[:,0]
    points_utm, zone = convert_from_latlon_to_utm(latitudes=lat,
                                                  longitudes=long)

    elevation = a_mux.lonlatdep[:,2] * -1

    # grid (create a mesh from the selected points)
    # This mesh has a problem.  Triangles are streched over ungridded areas.
    # If these areas could be described as holes in pmesh, that would be great.

    # I can't just get the user to selection a point in the middle.
    # A boundary is needed around these points.
    # But if the zone of points is obvious enough auto-segment should do
    # a good boundary.
    mesh = Mesh()
    mesh.add_vertices(points_utm)
    mesh.auto_segment(smooth_indents=True, expand_pinch=True)

    # To try and avoid alpha shape 'hugging' too much
    mesh.auto_segment(mesh.shape.get_alpha() * 1.1)
    if hole_points_UTM is not None:
        point = ensure_absolute(hole_points_UTM)
        mesh.add_hole(point[0], point[1])

    try:
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)
    except NoTrianglesError:
        # This is a bit of a hack, going in and changing the data structure.
        mesh.holes = []
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)

    mesh_dic = mesh.Mesh2MeshList()

    #mesh.export_mesh_file(basename_in + '_168.tsh')
    #import sys; sys.exit()
    # These are the times of the mux file
    mux_times = []
    for i in range(a_mux.time_step_count):
        mux_times.append(a_mux.time_step * i)
    (mux_times_start_i, mux_times_fin_i) = read_time_from_mux(mux_times, mint, maxt)
    times = mux_times[mux_times_start_i:mux_times_fin_i]

    if mux_times_start_i == mux_times_fin_i:
        # Close the mux files
        for quantity, file in map(None, quantities, files_in):
            mux[quantity].close()
        msg = "Due to mint and maxt there's no time info in the boundary SWW."
        raise Exception(msg)

    # If this raise is removed there is currently no downstream errors

    points_utm=ensure_numeric(points_utm)
    assert num.alltrue(ensure_numeric(mesh_dic['generatedpointlist'])
                       == ensure_numeric(points_utm))

    volumes = mesh_dic['generatedtrianglelist']

    # Write sww intro and grid stuff.
    if basename_out is None:
        swwname = basename_in + '.sww'
    else:
        swwname = basename_out + '.sww'

    if verbose: log.critical('Output to %s' % swwname)

    outfile = NetCDFFile(swwname, netcdf_mode_w)

    # For a different way of doing this, check out tsh2sww
    # work out sww_times and the index range this covers
    sww = Write_sww(['elevation'], ['stage', 'xmomentum', 'ymomentum'])
    sww.store_header(outfile, times, len(volumes), len(points_utm),
                     verbose=verbose, sww_precision=netcdf_float)
    outfile.mean_stage = mean_stage
    outfile.zscale = zscale

    sww.store_triangulation(outfile, points_utm, volumes,
                            zone,  
                            new_origin=origin,
                            verbose=verbose)
    sww.store_static_quantities(outfile, elevation=elevation)

    if verbose: log.critical('Converting quantities')

    # Read in a time slice from each mux file and write it to the SWW file
    j = 0
    for ha, ua, va in map(None, mux['HA'], mux['UA'], mux['VA']):
        if j >= mux_times_start_i and j < mux_times_fin_i:
            stage = zscale*ha + mean_stage
            h = stage - elevation
            xmomentum = ua*h
            ymomentum = -1 * va * h # -1 since in mux files south is positive.
            sww.store_quantities(outfile,
                                 slice_index=j-mux_times_start_i,
                                 verbose=verbose,
                                 stage=stage,
                                 xmomentum=xmomentum,
                                 ymomentum=ymomentum,
                                 sww_precision=num.float)
        j += 1

    if verbose: sww.verbose_quantities(outfile)

    outfile.close()
    def test_ungenerateFileLoading(self):
        
        fileName = tempfile.mktemp(".txt")
        file = open(fileName,"w")
        file.write("         1       ??      ??\n\
       0.0       0.0\n\
       1.0       0.0\n\
       1.0       1.0\n\
       0.0       1.0\n\
       0.0       0.0\n\
END\n\
         2      ?? ??\n\
       10.0       10.0\n\
       10.0       20.0\n\
       20.0       20.0\n\
       10.0       10.0\n\
END\n\
END\n")
        file.close()
        
        
        a = Vertex (0.0, 0.0) #, attributes = [1.1])
        b = Vertex (0.0, 40.0) #, attributes = [1.2])
        c = Vertex (40.0,40.0) #, attributes = [1.3])
        d = Vertex (40.0,0.0) #, attributes = [1.4])
    
        s1 = Segment(a,b)
        s2 = Segment(b,c)
        s3 = Segment(c,d)
        s4 = Segment(d,a)
     
        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
        dict = load_ungenerate(fileName)
        #os.remove(fileName)

        tag = "DSG"
        Segment.set_default_tag(tag)
        m.addVertsSegs(dict)

        # have to reset this , since it's a class attribute
        Segment.set_default_tag("")
            
        self.assertTrue(len(m.userSegments) ==11,
                        'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11,
                        'Wrong vertex list length.')
        self.assertTrue(m.userSegments[10].vertices[0] == m.userVertices[10],
                        'bad vertex on segment.')
        self.assertTrue(m.userSegments[10].vertices[1] == m.userVertices[8],
                        'Bad segment.')
        self.assertTrue(m.userSegments[10].tag == tag,
                        'wrong tag.')

        ## let's test the method
        a = Vertex (0.0, 0.0) #, attributes = [1.1])
        b = Vertex (0.0, 40.0) #, attributes = [1.2])
        c = Vertex (40.0,40.0) #, attributes = [1.3])
        d = Vertex (40.0,0.0) #, attributes = [1.4])
    
        s1 = Segment(a,b)
        s2 = Segment(b,c)
        s3 = Segment(c,d)
        s4 = Segment(d,a)
     
        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])

        tag = "DSG"        
        initial_tag = "PIG"
        Segment.set_default_tag(initial_tag)
        m.import_ungenerate_file(fileName, tag=tag)

        os.remove(fileName)

        self.assertTrue(Segment.get_default_tag() == initial_tag,
                        'Wrong segment list length.')
        

        # have to reset this , since it's a class attribute
        Segment.set_default_tag("")
            
        self.assertTrue(len(m.userSegments) ==11,
                        'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11,
                        'Wrong vertex list length.')
        self.assertTrue(m.userSegments[10].vertices[0] == m.userVertices[10],
                        'bad vertex on segment.')
        self.assertTrue(m.userSegments[10].vertices[1] == m.userVertices[8],
                        'Bad segment.')
        self.assertTrue(m.userSegments[10].tag == tag,
                        'wrong tag.')
    def test_import_ungenerate_file_different_region_tags(self):
        
        fileName = tempfile.mktemp(".txt")
        file = open(fileName,"w")
        file.write("         1       ??      ??\n\
       10.0       10.0\n\
       11.0       10.0\n\
       11.0       11.0\n\
       10.0       11.0\n\
       10.0       10.0\n\
END\n\
         2      ?? ??\n\
       20.0       20.0\n\
       20.0       30.0\n\
       30.0       30.0\n\
END\n\
END\n")
        file.close()
        
        
        a = Vertex (0.0, 0.0) #, attributes = [1.1])
        b = Vertex (0.0, 40.0) #, attributes = [1.2])
        c = Vertex (40.0,40.0) #, attributes = [1.3])
        d = Vertex (40.0,0.0) #, attributes = [1.4])
    
        s1 = Segment(a,b)
        s2 = Segment(b,c)
        s3 = Segment(c,d)
        s4 = Segment(d,a)
     
        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
        dict = load_ungenerate(fileName)
        #os.remove(fileName)

        tag = "DSG"
        Segment.set_default_tag(tag)
        m.addVertsSegs(dict)

        self.assertTrue(len(m.userSegments) ==11,
                        'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11,
                        'Wrong vertex list length.')

        # Test the method
        a = Vertex (0.0, 0.0) #, attributes = [1.1])
        b = Vertex (0.0, 40.0) #, attributes = [1.2])
        c = Vertex (40.0,40.0) #, attributes = [1.3])
        d = Vertex (40.0,0.0) #, attributes = [1.4])
    
        s1 = Segment(a,b)
        s2 = Segment(b,c)
        s3 = Segment(c,d)
        s4 = Segment(d,a)
     
        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
        tag = "DSG"        
        initial_tag = "PIG"
        Segment.set_default_tag(initial_tag)
        m.import_ungenerate_file(fileName, tag=tag, region_tag=["swamp","coastalp"])

        os.remove(fileName)

        self.assertTrue(Segment.get_default_tag() == initial_tag,
                        'Wrong segment list length.')
        m.export_mesh_file("swamp.tsh")
        #print "m.userSegments",m.userSegments
        self.assertTrue(len(m.userSegments) ==11,
                        'Wrong segment list length.')
        self.assertTrue(len(m.userVertices) == 11,
                        'Wrong vertex list length.')
        self.assertTrue(len(m.regions) == 2,
                        'Wrong regions list length.')
        self.assertTrue(m.regions[0].getTag() == "swamp",
                        'Wrong regions tag.')
        self.assertTrue(m.regions[1].getTag() == "coastalp",
                        'Wrong regions 1 tag.')
        
        
        # have to reset this , since it's a class attribute
        Segment.set_default_tag("")
Example #22
0
def urs_ungridded2sww(basename_in='o',
                      basename_out=None,
                      verbose=False,
                      mint=None,
                      maxt=None,
                      mean_stage=0,
                      origin=None,
                      hole_points_UTM=None,
                      zscale=1):
    """
    Convert URS C binary format for wave propagation to
    sww format native to abstract_2d_finite_volumes.

    Specify only basename_in and read files of the form
    basefilename-z-mux, basefilename-e-mux and
    basefilename-n-mux containing relative height,
    x-velocity and y-velocity, respectively.

    Also convert latitude and longitude to UTM. All coordinates are
    assumed to be given in the GDA94 datum. The latitude and longitude
    information is assumed ungridded grid.

    min's and max's: If omitted - full extend is used.
    To include a value min ans max may equal it.
    Lat and lon are assumed to be in decimal degrees.

    origin is a 3-tuple with geo referenced
    UTM coordinates (zone, easting, northing)
    It will be the origin of the sww file. This shouldn't be used,
    since all of anuga should be able to handle an arbitary origin.
    The mux point info is NOT relative to this origin.

    URS C binary format has data organised as TIME, LONGITUDE, LATITUDE
    which means that latitude is the fastest
    varying dimension (row major order, so to speak)

    In URS C binary the latitudes and longitudes are in assending order.

    Note, interpolations of the resulting sww file will be different
    from results of urs2sww.  This is due to the interpolation
    function used, and the different grid structure between urs2sww
    and this function.

    Interpolating data that has an underlying gridded source can
    easily end up with different values, depending on the underlying
    mesh.

    consider these 4 points
    50  -50

    0     0

    The grid can be
     -
    |\|   A
     -
     or;
      -
     |/|  B
      -

    If a point is just below the center of the midpoint, it will have a
    +ve value in grid A and a -ve value in grid B.
    """

    from anuga.mesh_engine.mesh_engine import NoTrianglesError
    from anuga.pmesh.mesh import Mesh

    files_in = [
        basename_in + WAVEHEIGHT_MUX_LABEL, basename_in + EAST_VELOCITY_LABEL,
        basename_in + NORTH_VELOCITY_LABEL
    ]
    quantities = ['HA', 'UA', 'VA']

    # instantiate urs_points of the three mux files.
    mux = {}
    for quantity, file in zip(quantities, files_in):
        mux[quantity] = Read_urs(file)

    # Could check that the depth is the same. (hashing)

    # handle to a mux file to do depth stuff
    a_mux = mux[quantities[0]]

    # Convert to utm
    lat = a_mux.lonlatdep[:, 1]
    long = a_mux.lonlatdep[:, 0]
    points_utm, zone = convert_from_latlon_to_utm(latitudes=lat,
                                                  longitudes=long)

    elevation = a_mux.lonlatdep[:, 2] * -1

    # grid (create a mesh from the selected points)
    # This mesh has a problem.  Triangles are streched over ungridded areas.
    # If these areas could be described as holes in pmesh, that would be great.

    # I can't just get the user to selection a point in the middle.
    # A boundary is needed around these points.
    # But if the zone of points is obvious enough auto-segment should do
    # a good boundary.
    mesh = Mesh()
    mesh.add_vertices(points_utm)
    mesh.auto_segment(smooth_indents=True, expand_pinch=True)

    # To try and avoid alpha shape 'hugging' too much
    mesh.auto_segment(mesh.shape.get_alpha() * 1.1)
    if hole_points_UTM is not None:
        point = ensure_absolute(hole_points_UTM)
        mesh.add_hole(point[0], point[1])

    try:
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)
    except NoTrianglesError:
        # This is a bit of a hack, going in and changing the data structure.
        mesh.holes = []
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)

    mesh_dic = mesh.Mesh2MeshList()

    #mesh.export_mesh_file(basename_in + '_168.tsh')
    #import sys; sys.exit()
    # These are the times of the mux file
    mux_times = []
    for i in range(a_mux.time_step_count):
        mux_times.append(a_mux.time_step * i)
    (mux_times_start_i,
     mux_times_fin_i) = read_time_from_mux(mux_times, mint, maxt)
    times = mux_times[mux_times_start_i:mux_times_fin_i]

    if mux_times_start_i == mux_times_fin_i:
        # Close the mux files
        for quantity, file in zip(quantities, files_in):
            mux[quantity].close()
        msg = "Due to mint and maxt there's no time info in the boundary SWW."
        raise Exception(msg)

    # If this raise is removed there is currently no downstream errors

    points_utm = ensure_numeric(points_utm)
    assert num.alltrue(
        ensure_numeric(mesh_dic['generatedpointlist']) == ensure_numeric(
            points_utm))

    volumes = mesh_dic['generatedtrianglelist']

    # Write sww intro and grid stuff.
    if basename_out is None:
        swwname = basename_in + '.sww'
    else:
        swwname = basename_out + '.sww'

    if verbose: log.critical('Output to %s' % swwname)

    outfile = NetCDFFile(swwname, netcdf_mode_w)

    # For a different way of doing this, check out tsh2sww
    # work out sww_times and the index range this covers
    sww = Write_sww(['elevation'], ['stage', 'xmomentum', 'ymomentum'])
    sww.store_header(outfile,
                     times,
                     len(volumes),
                     len(points_utm),
                     verbose=verbose,
                     sww_precision=netcdf_float)
    outfile.mean_stage = mean_stage
    outfile.zscale = zscale

    sww.store_triangulation(outfile,
                            points_utm,
                            volumes,
                            zone,
                            new_origin=origin,
                            verbose=verbose)
    sww.store_static_quantities(outfile, elevation=elevation)

    if verbose: log.critical('Converting quantities')

    # Read in a time slice from each mux file and write it to the SWW file
    j = 0
    for ha, ua, va in zip(mux['HA'], mux['UA'], mux['VA']):
        if j >= mux_times_start_i and j < mux_times_fin_i:
            stage = zscale * ha + mean_stage
            h = stage - elevation
            xmomentum = ua * h
            ymomentum = -1 * va * h  # -1 since in mux files south is positive.
            sww.store_quantities(outfile,
                                 slice_index=j - mux_times_start_i,
                                 verbose=verbose,
                                 stage=stage,
                                 xmomentum=xmomentum,
                                 ymomentum=ymomentum,
                                 sww_precision=num.float)
        j += 1

    if verbose: sww.verbose_quantities(outfile)

    outfile.close()
def sts2sww_mesh(basename_in, basename_out=None, 
                 spatial_thinning=1, verbose=False):
    
    from anuga.mesh_engine.mesh_engine import NoTrianglesError
    from anuga.pmesh.mesh import Mesh
    if verbose:
        print "Starting sts2sww_mesh"
    
    mean_stage=0.
    zscale=1.

    if (basename_in[:-4]=='.sts'):
        stsname = basename_in
    else: 
        stsname = basename_in + '.sts'

    if verbose: print "Reading sts NetCDF file: %s" %stsname
    infile = NetCDFFile(stsname, netcdf_mode_r)
    cellsize = infile.cellsize
    ncols = infile.ncols
    nrows = infile.nrows
    no_data = infile.no_data
    refzone = infile.zone
    x_origin = infile.xllcorner
    y_origin = infile.yllcorner
    origin = num.array([x_origin, y_origin])
    x = infile.variables['x'][:]
    y = infile.variables['y'][:]
    times = infile.variables['time'][:]
    wind_speed_full = infile.variables['wind_speed'][:]
    wind_angle_full = infile.variables['wind_angle'][:]
    pressure_full   =   infile.variables['barometric_pressure'][:]
    infile.close()

    number_of_points = nrows*ncols
    points_utm = num.zeros((number_of_points,2),num.float)
    points_utm[:,0]=x+x_origin
    points_utm[:,1]=y+y_origin

    thinned_indices=[]
    for i in range(number_of_points):
        if (i/ncols==0 or i/ncols==ncols-1 or (i/ncols)%(spatial_thinning)==0):
            if ( i%(spatial_thinning)==0 or i%nrows==0 or i%nrows==nrows-1 ):  
                thinned_indices.append(i)

    #Spatial thinning
    points_utm=points_utm[thinned_indices]
    number_of_points = points_utm.shape[0]
    number_of_timesteps = wind_speed_full.shape[0]
    wind_speed = num.empty((number_of_timesteps,number_of_points),dtype=float)
    wind_angle = num.empty((number_of_timesteps,number_of_points),dtype=float)
    barometric_pressure   = num.empty((number_of_timesteps,number_of_points),dtype=float)
    if verbose:
        print "Total number of points: ", nrows*ncols
        print "Number of thinned points: ", number_of_points
    for i in xrange(number_of_timesteps):
        wind_speed[i] = wind_speed_full[i,thinned_indices]
        wind_angle[i] = wind_angle_full[i,thinned_indices]
        barometric_pressure[i]   = pressure_full[i,thinned_indices]

    #P.plot(points_utm[:,0],points_utm[:,1],'ro')
    #P.show()

    if verbose:
        print "Generating sww triangulation of gems data"

    mesh = Mesh()
    mesh.add_vertices(points_utm)
    mesh.auto_segment(smooth_indents=True, expand_pinch=True)
    mesh.auto_segment(mesh.shape.get_alpha() * 1.1)
    try:
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)
    except NoTrianglesError:
        # This is a bit of a hack, going in and changing the data structure.
        mesh.holes = []
        mesh.generate_mesh(minimum_triangle_angle=0.0, verbose=False)

    mesh_dic = mesh.Mesh2MeshList()

    points_utm=ensure_numeric(points_utm)
    assert num.alltrue(ensure_numeric(mesh_dic['generatedpointlist'])
                       == ensure_numeric(points_utm))

    volumes = mesh_dic['generatedtrianglelist']

    # Write sww intro and grid stuff.
    if (basename_out is not None and basename_out[:-4]=='.sww'): 
        swwname = basename_out
    else: 
        swwname = basename_in + '.sww'

    if verbose: 'Output to %s' % swwname

    if verbose:
        print "Writing sww wind and pressure field file"
    outfile = NetCDFFile(swwname, netcdf_mode_w)
    sww = Write_sww([], ['wind_speed','wind_angle','barometric_pressure'])
    sww.store_header(outfile, times, len(volumes), len(points_utm),
                     verbose=verbose, sww_precision='d')
    outfile.mean_stage = mean_stage
    outfile.zscale = zscale
    sww.store_triangulation(outfile, points_utm, volumes,
                            refzone,  
                            new_origin=origin, #check effect of this line
                            verbose=verbose)

    if verbose: 
        print 'Converting quantities'
    
    # Read in a time slice from the sts file and write it to the SWW file

    #print wind_angle[0,:10]
    for i in range(len(times)):
        sww.store_quantities(outfile,
                             slice_index=i,
                             verbose=verbose,
                             wind_speed=wind_speed[i,:],
                             wind_angle=wind_angle[i,:],
                             barometric_pressure=barometric_pressure[i,:],
                             sww_precision=num.float)

    if verbose: 
        sww.verbose_quantities(outfile)
    outfile.close()