Esempio n. 1
0
    def test_gridPointsInPolygon(self):
        #
        # Make a polygon, check the grid points exist where we need them

        # Simple example -- no points on the trial grid are excluded
        myPoly = [[0., 10.], [10., 10.], [10., 0.], [0., 0.]]

        pip = su.gridPointsInPolygon(myPoly, approx_grid_spacing=[1., 1.])
        # There should be 121 points in total
        assert(pip.shape[0] == 121)
        # The min/max x and y should be inside
        assert(min(pip[:, 0]) > 0.)
        assert(max(pip[:, 0]) < 10.)
        assert(min(pip[:, 1]) > 0.)
        assert(max(pip[:, 1]) < 10.)

        # Example where some points on the trial grid would be excluded
        myPoly = [[0., 10.], [10., 10.], [10., 0.]]
        pip = su.gridPointsInPolygon(myPoly, approx_grid_spacing=[1., 1.])
        # The min/max x and y should be inside
        assert(min(pip[:, 0]) > 0.)
        assert(max(pip[:, 0]) < 10.)
        assert(min(pip[:, 1]) > 0.)
        assert(max(pip[:, 1]) < 10.)
        # x+y should always be >= 10, since the line connecting [0,10] and
        # [10,0] is x+y=10
        assert(all(pip[:, 0]+pip[:, 1] >= 10.))
    def test_gridPointsInPolygon(self):
        #
        # Make a polygon, check the grid points exist where we need them

        ## Simple example -- no points on the trial grid are excluded
        myPoly=[[0., 10.], [10., 10.], [10., 0.], [0., 0.]]
        
        pip=su.gridPointsInPolygon(myPoly, approx_grid_spacing=[1.,1.])
        # There should be 121 points in total
        assert(pip.shape[0]==121)
        # The min/max x and y should be inside
        assert(min(pip[:,0])>0.)
        assert(max(pip[:,0])<10.)
        assert(min(pip[:,1])>0.)
        assert(max(pip[:,1])<10.)
       
        ## Example where some points on the trial grid would be excluded 
        myPoly=[[0., 10.], [10., 10.], [10., 0.]]
        pip=su.gridPointsInPolygon(myPoly, approx_grid_spacing=[1.,1.])
        # The min/max x and y should be inside
        assert(min(pip[:,0])>0.)
        assert(max(pip[:,0])<10.)
        assert(min(pip[:,1])>0.)
        assert(max(pip[:,1])<10.)
        # x+y should always be >= 10, since the line connecting [0,10] and
        # [10,0] is x+y=10
        assert(all(pip[:,0]+pip[:,1]>=10.))
Esempio n. 3
0
    def elevation_setter(xc, yc):

        # Return scipy array of values
        out = xc * 0.

        # Get multiple elevation values in each triangle.
        # Process triangles in chunks to reduce function call overhead
        lx = len(xc)
        lx_div_cs = scipy.ceil(lx * 1. / (1. * chunk_size)).astype(int)

        # Crude check that xc/yc are the centroid values
        #
        erMess = ' Result of make_meanFun can ONLY be applied to a vector' +\
            ' of ALL centroid coordinates\n' +\
            ' (since mesh triangles are used to spatially average)'
        assert scipy.all(xc == domain.centroid_coordinates[:, 0]), erMess
        assert scipy.all(yc == domain.centroid_coordinates[:, 1]), erMess

        # Find triangles in which we want to average
        if polygons_for_averaging is not None:

            averaging_flag = 0 * xc

            # Need georeferenced centroid coordinates to find which
            # are in the polygon
            xll = domain.geo_reference.xllcorner
            yll = domain.geo_reference.yllcorner
            centroid_coordinates_georef = scipy.vstack([xc + xll,
                                                        yc + yll]).transpose()

            for j in range(len(polygons_for_averaging)):
                poly_j = polygons_for_averaging[j]
                # poly_j can either be a polygon, or a filename
                if type(poly_j) is str:
                    poly_j = su.read_polygon(poly_j)

                points_in_poly_j = inside_polygon(centroid_coordinates_georef,
                                                  poly_j)

                averaging_flag[points_in_poly_j] = 1

        else:
            averaging_flag = 1 + 0 * xc

        for i in range(lx_div_cs):
            # Evaluate in triangles lb:ub
            lb = i * chunk_size
            ub = min((i + 1) * chunk_size, lx)

            if verbose:
                print 'Averaging in triangles ', lb, '-', ub - 1

            # Store x,y,triangleIndex
            px = scipy.array([])
            py = scipy.array([])
            p_indices = scipy.array([])

            for j in range(lb, ub):
                # If we average this cell, then get a grid
                # of points in it. Otherwise just get the centroid
                # coordinates.
                if averaging_flag[j] == 1:
                    mesh_tri = \
                        domain.mesh.vertex_coordinates[
                            range(3 * j, 3 * j + 3), :].tolist()

                    pts = su.gridPointsInPolygon(
                        mesh_tri, approx_grid_spacing=approx_grid_spacing)
                else:
                    # Careful to keep this a 2D array
                    pts = domain.centroid_coordinates[j, :, None].transpose()

                px = scipy.hstack([px, pts[:, 0]])

                py = scipy.hstack([py, pts[:, 1]])

                p_indices = scipy.hstack(
                    [p_indices, scipy.repeat(j, len(pts[:, 0]))])

            # Get function values at all px,py
            if verbose:
                print '  Evaluating function at ', len(px), ' points'

            allTopo = q_function(px, py)

            # Set output values in lb:ub
            for j in range(lb, ub):
                out_indices = (p_indices == j).nonzero()[0]
                assert len(out_indices) > 0
                if (averaging == 'mean'):
                    out[j] = allTopo[out_indices].mean()
                elif (averaging == 'min'):
                    out[j] = allTopo[out_indices].min()
                elif (averaging == 'max'):
                    out[j] = allTopo[out_indices].max()
                else:
                    raise Exception('Unknown value of averaging')
        return (out)
Esempio n. 4
0
    def elevation_setter(xc, yc):

        # Return scipy array of values
        out = xc * 0.

        # Get multiple elevation values in each triangle.
        # Process triangles in chunks to reduce function call overhead
        lx = len(xc)
        lx_div_cs = scipy.ceil(lx * 1. / (1. * chunk_size)).astype(int)

        # Crude check that xc/yc are the centroid values
        #
        erMess = ' Result of make_meanFun can ONLY be applied to a vector' +\
            ' of ALL centroid coordinates\n' +\
            ' (since mesh triangles are used to spatially average)'
        assert scipy.all(xc == domain.centroid_coordinates[:, 0]), erMess
        assert scipy.all(yc == domain.centroid_coordinates[:, 1]), erMess

        for i in range(lx_div_cs):
            # Evaluate in triangles lb:ub
            lb = i * chunk_size
            ub = min((i + 1) * chunk_size, lx)

            if verbose:
                print 'Averaging in triangles ', lb, '-', ub - 1

            # Store x,y,triangleIndex
            px = scipy.array([])
            py = scipy.array([])
            p_indices = scipy.array([])

            for j in range(lb, ub):

                mesh_tri = \
                    domain.mesh.vertex_coordinates[
                        range(3 * j, 3 * j + 3), :].tolist()

                pts = su.gridPointsInPolygon(
                    mesh_tri, approx_grid_spacing=approx_grid_spacing)

                px = scipy.hstack([px, pts[:, 0]])

                py = scipy.hstack([py, pts[:, 1]])

                p_indices = scipy.hstack(
                    [p_indices, scipy.repeat(j, len(pts[:, 0]))])

            # Get function values at all px,py
            if verbose:
                print '  Evaluating function at ', len(px), ' points'

            allTopo = q_function(px, py)

            # Set output values in lb:ub
            for j in range(lb, ub):
                out_indices = (p_indices == j).nonzero()[0]
                assert len(out_indices) > 0
                if (averaging == 'mean'):
                    out[j] = allTopo[out_indices].mean()
                elif (averaging == 'min'):
                    out[j] = allTopo[out_indices].min()
                elif (averaging == 'max'):
                    out[j] = allTopo[out_indices].max()
                elif (averaging == 'harmonic_mean'):
                    out[j] = 1.0 / (1.0 / allTopo[out_indices]).mean()
                else:
                    raise Exception, 'Unknown value of averaging'
        return (out)
    def elevation_setter(xc, yc):

        # Return scipy array of values
        out = xc * 0.

        # Get multiple elevation values in each triangle.
        # Process triangles in chunks to reduce function call overhead
        lx = len(xc)
        lx_div_cs = scipy.ceil(lx * 1. / (1. * chunk_size)).astype(int)

        # Crude check that xc/yc are the centroid values
        #
        erMess = ' Result of make_meanFun can ONLY be applied to a vector' +\
            ' of ALL centroid coordinates\n' +\
            ' (since mesh triangles are used to spatially average)'
        assert scipy.all(xc == domain.centroid_coordinates[:, 0]), erMess
        assert scipy.all(yc == domain.centroid_coordinates[:, 1]), erMess

        # Find triangles in which we want to average
        if polygons_for_averaging is not None:

            averaging_flag = 0*xc

            # Need georeferenced centroid coordinates to find which
            # are in the polygon
            xll = domain.geo_reference.xllcorner
            yll = domain.geo_reference.yllcorner
            centroid_coordinates_georef = scipy.vstack([xc + xll, yc + yll]).transpose()

            for j in range(len(polygons_for_averaging)):
                poly_j = polygons_for_averaging[j]
                # poly_j can either be a polygon, or a filename
                if type(poly_j) is str:
                    poly_j = su.read_polygon(poly_j)
                
                points_in_poly_j = inside_polygon(centroid_coordinates_georef, 
                    poly_j)
                
                averaging_flag[points_in_poly_j] = 1
                
        else:
            averaging_flag = 1 + 0*xc
        

        for i in range(lx_div_cs):
            # Evaluate in triangles lb:ub
            lb = i * chunk_size
            ub = min((i + 1) * chunk_size, lx)

            if verbose:
                print 'Averaging in triangles ', lb, '-', ub - 1

            # Store x,y,triangleIndex
            px = scipy.array([])
            py = scipy.array([])
            p_indices = scipy.array([])

            for j in range(lb, ub):
                # If we average this cell, then get a grid
                # of points in it. Otherwise just get the centroid
                # coordinates. 
                if averaging_flag[j] == 1:
                    mesh_tri = \
                        domain.mesh.vertex_coordinates[
                            range(3 * j, 3 * j + 3), :].tolist()

                    pts = su.gridPointsInPolygon(
                        mesh_tri,
                        approx_grid_spacing=approx_grid_spacing)
                else:
                    # Careful to keep this a 2D array
                    pts = domain.centroid_coordinates[j,:, None].transpose()

                px = scipy.hstack([px, pts[:, 0]])

                py = scipy.hstack([py, pts[:, 1]])

                p_indices = scipy.hstack([p_indices,
                                          scipy.repeat(j, len(pts[:, 0]))])

            # Get function values at all px,py
            if verbose:
                print '  Evaluating function at ', len(px), ' points'

            allTopo = q_function(px, py)

            # Set output values in lb:ub
            for j in range(lb, ub):
                out_indices = (p_indices == j).nonzero()[0]
                assert len(out_indices) > 0
                if(averaging == 'mean'):
                    out[j] = allTopo[out_indices].mean()
                elif(averaging == 'min'):
                    out[j] = allTopo[out_indices].min()
                elif(averaging == 'max'):
                    out[j] = allTopo[out_indices].max()
                else:
                    raise Exception('Unknown value of averaging')
        return(out)
    def elevation_setter(xc, yc):

        # Return scipy array of values
        out = xc * 0.

        # Get multiple elevation values in each triangle.
        # Process triangles in chunks to reduce function call overhead
        lx = len(xc)
        lx_div_cs = scipy.ceil(lx * 1. / (1. * chunk_size)).astype(int)

        # Crude check that xc/yc are the centroid values
        #
        erMess = ' Result of make_meanFun can ONLY be applied to a vector' +\
            ' of ALL centroid coordinates\n' +\
            ' (since mesh triangles are used to spatially average)'
        assert scipy.all(xc == domain.centroid_coordinates[:, 0]), erMess
        assert scipy.all(yc == domain.centroid_coordinates[:, 1]), erMess

        for i in range(lx_div_cs):
            # Evaluate in triangles lb:ub
            lb = i * chunk_size
            ub = min((i + 1) * chunk_size, lx)

            if verbose:
                print 'Averaging in triangles ', lb, '-', ub - 1

            # Store x,y,triangleIndex
            px = scipy.array([])
            py = scipy.array([])
            p_indices = scipy.array([])

            for j in range(lb, ub):

                mesh_tri = \
                    domain.mesh.vertex_coordinates[
                        range(3 * j, 3 * j + 3), :].tolist()

                pts = su.gridPointsInPolygon(
                    mesh_tri,
                    approx_grid_spacing=approx_grid_spacing)

                px = scipy.hstack([px, pts[:, 0]])

                py = scipy.hstack([py, pts[:, 1]])

                p_indices = scipy.hstack([p_indices,
                                          scipy.repeat(j, len(pts[:, 0]))])

            # Get function values at all px,py
            if verbose:
                print '  Evaluating function at ', len(px), ' points'

            allTopo = q_function(px, py)

            # Set output values in lb:ub
            for j in range(lb, ub):
                out_indices = (p_indices == j).nonzero()[0]
                assert len(out_indices) > 0
                if(averaging == 'mean'):
                    out[j] = allTopo[out_indices].mean()
                elif(averaging == 'min'):
                    out[j] = allTopo[out_indices].min()
                elif(averaging == 'max'):
                    out[j] = allTopo[out_indices].max()
                elif(averaging == 'harmonic_mean'):
                    out[j] = 1.0 / (1.0 / allTopo[out_indices]).mean()
                else:
                    raise Exception, 'Unknown value of averaging'
        return(out)