Ejemplo n.º 1
0
def get_matrix_A(fn, gauge_name):
    # points to read information from
    point_reader = reader(file(gauge_name))
    points = []
    point_name = []
    for i, row in enumerate(point_reader):
        if i == 0:
            for j, value in enumerate(row):
                if value.strip() == 'easting': easting = j
                if value.strip() == 'northing': northing = j
                if value.strip() == 'name': name = j
                if value.strip() == 'elevation': elevation = j
        else:
            #points.append([float(row[easting]),float(row[northing])])
            points.append([float(row[easting]), float(row[northing])])
            point_name.append(row[name])
    points_array = np.array(points, np.float)
    dim_gauge = int(np.sqrt(points_array.shape[0]))
    interpolation_points = ensure_absolute(points_array)

    # read the sww file to extract something
    fid = NetCDFFile(fn, netcdf_mode_r)
    xllcorner = fid.xllcorner
    yllcorner = fid.yllcorner
    zone = fid.zone
    x = fid.variables['x'][:]
    y = fid.variables['y'][:]

    triangles = fid.variables['volumes'][:]

    x = np.reshape(x, (len(x), 1))
    y = np.reshape(y, (len(y), 1))
    vertex_coordinates = np.concatenate((x, y), axis=1)
    ele = fid.variables['elevation'][:]
    fid.close()

    vertex_coordinates = ensure_absolute(vertex_coordinates)
    triangles = ensure_numeric(triangles)

    interpolation_points = ensure_numeric(interpolation_points)
    interpolation_points[:, 0] -= xllcorner
    interpolation_points[:, 1] -= yllcorner

    mesh = Mesh(vertex_coordinates, triangles)
    mesh_boundary_polygon = mesh.get_boundary_polygon()
    indices = outside_polygon(interpolation_points, mesh_boundary_polygon)
    interp = Interpolate(vertex_coordinates, triangles)
    matrix_A, inside_poly_indices, outside_poly_indices, centroids = interp._build_interpolation_matrix_A(
        interpolation_points, output_centroids=False, verbose=False)

    ele = matrix_A * ele
    ele = np.asarray(ele)
    ele = ele.reshape((100, 100))

    return ele, matrix_A
Ejemplo n.º 2
0
    def get_interpolation_object(self):
        """Get object I that will allow linear interpolation using this mesh

        This is a time consuming process but it needs only to be
        once for the mesh.

        Interpolation can then be done using

        result = I.interpolate_block(vertex_values, interpolation_points)

        where vertex values have been obtained from a quantity using
        vertex_values, triangles = self.get_vertex_values()
        """

        if hasattr(self, 'interpolation_object'):
            I = self.interpolation_object
        else:
            from anuga.fit_interpolate.interpolate import Interpolate

            # Get discontinuous mesh - this will match internal
            # representation of vertex values
            triangles = self.get_disconnected_triangles()
            vertex_coordinates = self.get_vertex_coordinates()

            I = Interpolate(vertex_coordinates, triangles)
            self.interpolation_object = I

        return I
Ejemplo n.º 3
0
    def calc_grid_values(nrows, ncols, cellsize, NODATA_value, x, y, norms,
                         volumes, result, grid_values):

        grid_points = num.zeros((ncols * nrows, 2), num.float)
        vertex_points = num.concatenate((x[:, num.newaxis], y[:, num.newaxis]),
                                        axis=1)
        assert len(vertex_points.shape) == 2

        for i in xrange(nrows):
            yg = i * cellsize
            #            if out_ext == '.asc':
            #                yg = i * cellsize
            #            else:
            #                # this will flip the order of the y values for ers
            #                yg = (nrows-i) * cellsize

            for j in xrange(ncols):
                xg = j * cellsize
                k = i * ncols + j

                grid_points[k, 0] = xg
                grid_points[k, 1] = yg

        # Interpolate
        from anuga.fit_interpolate.interpolate import Interpolate

        # Remove loners from vertex_points, volumes here
        vertex_points, volumes = remove_lone_verts(vertex_points, volumes)
        # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes})

        interp = Interpolate(vertex_points, volumes, verbose=verbose)

        # Interpolate using quantity values
        if verbose: log.critical('Interpolating')
        grid_values[:] = interp.interpolate(result,
                                            grid_points,
                                            NODATA_value=NODATA_value,
                                            verbose=verbose).flatten()
        #print grid_values.shape

        return
Ejemplo n.º 4
0
    def calc_grid_values_old(vertex_points, volumes, result):

        grid_points = num.zeros ((ncols*nrows, 2), num.float)

        for i in xrange(nrows):
            if out_ext == '.asc':
                yg = i * cellsize
            else:
                # this will flip the order of the y values for ers
                yg = (nrows-i) * cellsize

            for j in xrange(ncols):
                xg = j * cellsize
                k = i*ncols + j

                grid_points[k, 0] = xg
                grid_points[k, 1] = yg

        # Interpolate
        from anuga.fit_interpolate.interpolate import Interpolate

        # Remove loners from vertex_points, volumes here
        vertex_points, volumes = remove_lone_verts(vertex_points, volumes)
        # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes})


        interp = Interpolate(vertex_points, volumes, verbose = verbose)

        bprint = 0

        # Interpolate using quantity values
        if verbose: log.critical('Interpolating')
        grid_values = interp.interpolate(bprint, result, grid_points).flatten()
        outside_indices = interp.get_outside_poly_indices()

        for i in outside_indices:
            #print 'change grid_value',NODATA_value
            grid_values[i] = NODATA_value
	
        return grid_values
Ejemplo n.º 5
0
    def calc_grid_values(nrows, ncols, cellsize, NODATA_value,
                         x,y, norms, volumes, result, grid_values):

        grid_points = num.zeros ((ncols*nrows, 2), num.float)
        vertex_points = num.concatenate ((x[:,num.newaxis], y[:,num.newaxis]), axis=1)
        assert len(vertex_points.shape) == 2

        for i in xrange(nrows):
            yg = i * cellsize
#            if out_ext == '.asc':
#                yg = i * cellsize
#            else:
#                # this will flip the order of the y values for ers
#                yg = (nrows-i) * cellsize

            for j in xrange(ncols):
                xg = j * cellsize
                k = i*ncols + j

                grid_points[k, 0] = xg
                grid_points[k, 1] = yg

        # Interpolate
        from anuga.fit_interpolate.interpolate import Interpolate

        # Remove loners from vertex_points, volumes here
        vertex_points, volumes = remove_lone_verts(vertex_points, volumes)
        # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes})


        interp = Interpolate(vertex_points, volumes, verbose = verbose)

        # Interpolate using quantity values
        if verbose: log.critical('Interpolating')
        grid_values[:] = interp.interpolate(result, grid_points,
                                         NODATA_value= NODATA_value,
                                         verbose=verbose).flatten()
        #print grid_values.shape

        return
    def calc_grid_values_old(vertex_points, volumes, result):

        grid_points = num.zeros((ncols * nrows, 2), num.float)

        for i in range(nrows):
            if out_ext == '.asc':
                yg = i * cellsize
            else:
                # this will flip the order of the y values for ers
                yg = (nrows - i) * cellsize

            for j in range(ncols):
                xg = j * cellsize
                k = i * ncols + j

                grid_points[k, 0] = xg
                grid_points[k, 1] = yg

        # Interpolate
        from anuga.fit_interpolate.interpolate import Interpolate

        # Remove loners from vertex_points, volumes here
        vertex_points, volumes = remove_lone_verts(vertex_points, volumes)
        # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes})

        interp = Interpolate(vertex_points, volumes, verbose=verbose)

        bprint = 0

        # Interpolate using quantity values
        if verbose: log.critical('Interpolating')
        grid_values = interp.interpolate(bprint, result, grid_points).flatten()
        outside_indices = interp.get_outside_poly_indices()

        for i in outside_indices:
            #print 'change grid_value',NODATA_value
            grid_values[i] = NODATA_value

        return grid_values
Ejemplo n.º 7
0
            yg = i * cellsize
        else:
            # this will flip the order of the y values for ers
            yg = (nrows-i) * cellsize

        for j in xrange(ncols):
            xg = j * cellsize
            k = i*ncols + j

            grid_points[k, 0] = xg
            grid_points[k, 1] = yg

    # Remove loners from vertex_points, volumes here
    vertex_points, volumes = remove_lone_verts(vertex_points, volumes)
    # export_mesh_file('monkey.tsh',{'vertices':vertex_points, 'triangles':volumes})
    interp = Interpolate(vertex_points, volumes, verbose=verbose)

    log.debug('Interpolating')

    # Interpolate using quantity values
    grid_values = interp.interpolate(result, grid_points).flatten()

    log.debug('Interpolated values are in [%f, %f]'
              % (num.min(grid_values), num.max(grid_values)))

    # Assign NODATA_value to all points outside bounding polygon (from interpolation mesh)
    P = interp.mesh.get_boundary_polygon()
    outside_indices = outside_polygon(grid_points, P, closed=True)

    for i in outside_indices:
        grid_values[i] = NODATA_value
Ejemplo n.º 8
0
    # Post condition: Now q has dimension: number_of_points
    assert len(q.shape) == 1
    assert q.shape[0] == number_of_points

    if verbose:
        log.critical('Processed values for %s are in [%f, %f]' %
                     (quantity, min(q), max(q)))

    # Create grid and update xll/yll corner and x,y
    vertex_points = num.concatenate((x[:, num.newaxis], y[:, num.newaxis]),
                                    axis=1)
    assert len(vertex_points.shape) == 2

    # Interpolate
    from anuga.fit_interpolate.interpolate import Interpolate
    interp = Interpolate(vertex_points, volumes, verbose=verbose)

    # Interpolate using quantity values
    if verbose: log.critical('Interpolating')
    interpolated_values = interp.interpolate(q, data_points).flatten()

    if verbose:
        log.critical(
            'Interpolated values are in [%f, %f]' %
            (num.min(interpolated_values), num.max(interpolated_values)))

    # Assign NODATA_value to all points outside bounding polygon
    # (from interpolation mesh)
    P = interp.mesh.get_boundary_polygon()
    outside_indices = outside_polygon(data_points, P, closed=True)
Ejemplo n.º 9
0
    # Post condition: Now q has dimension: number_of_points
    assert len(q.shape) == 1
    assert q.shape[0] == number_of_points

    if verbose:
        log.critical('Processed values for %s are in [%f, %f]'
                     % (quantity, min(q), max(q)))

    # Create grid and update xll/yll corner and x,y
    vertex_points = num.concatenate((x[:, num.newaxis], y[:, num.newaxis]), axis=1)
    assert len(vertex_points.shape) == 2

    # Interpolate
    from anuga.fit_interpolate.interpolate import Interpolate
    interp = Interpolate(vertex_points, volumes, verbose=verbose)

    # Interpolate using quantity values
    if verbose: log.critical('Interpolating')
    interpolated_values = interp.interpolate(q, data_points).flatten()

    if verbose:
        log.critical('Interpolated values are in [%f, %f]'
                     % (num.min(interpolated_values),
                        num.max(interpolated_values)))

    # Assign NODATA_value to all points outside bounding polygon
    # (from interpolation mesh)
    P = interp.mesh.get_boundary_polygon()
    outside_indices = outside_polygon(data_points, P, closed=True)
Ejemplo n.º 10
0
def sww2pts(name_in,
            name_out=None,
            data_points=None,
            quantity=None,
            timestep=None,
            reduction=None,
            NODATA_value=-9999,
            verbose=False,
            origin=None):
    """Read SWW file and convert to interpolated values at selected points

    The parameter 'quantity' must be the name of an existing quantity or
    an expression involving existing quantities. The default is 'elevation'.

    if timestep (an index) is given, output quantity at that timestep.

    if reduction is given use that to reduce quantity over all timesteps.

    data_points (Nx2 array) give locations of points where quantity is to 
    be computed.
    """

    import sys
    from anuga.geometry.polygon import inside_polygon, outside_polygon
    from anuga.abstract_2d_finite_volumes.util import \
             apply_expression_to_dictionary
    from anuga.geospatial_data.geospatial_data import Geospatial_data

    if quantity is None:
        quantity = 'elevation'

    if reduction is None:
        reduction = max

    basename_in, in_ext = os.path.splitext(name_in)

    if name_out != None:
        basename_out, out_ext = os.path.splitext(name_out)
    else:
        basename_out = basename_in + '_%s' % quantity
        out_ext = '.pts'
        name_out = basename_out + out_ext

    if in_ext != '.sww':
        raise IOError('Input format for %s must be .sww' % name_in)

    if out_ext != '.pts':
        raise IOError('Output format for %s must be .pts' % name_out)

    # Read sww file
    if verbose: log.critical('Reading from %s' % name_in)
    from anuga.file.netcdf import NetCDFFile
    fid = NetCDFFile(name_in)

    # Get extent and reference
    x = fid.variables['x'][:]
    y = fid.variables['y'][:]
    volumes = fid.variables['volumes'][:]

    try:  # works with netcdf4
        number_of_timesteps = len(fid.dimensions['number_of_timesteps'])
        number_of_points = len(fid.dimensions['number_of_points'])
    except:  #works with scientific.io.netcdf
        number_of_timesteps = fid.dimensions['number_of_timesteps']
        number_of_points = fid.dimensions['number_of_points']

    if origin is None:
        # Get geo_reference
        # sww files don't have to have a geo_ref
        try:
            geo_reference = Geo_reference(NetCDFObject=fid)
        except AttributeError as e:
            geo_reference = Geo_reference()  # Default georef object

        xllcorner = geo_reference.get_xllcorner()
        yllcorner = geo_reference.get_yllcorner()
        zone = geo_reference.get_zone()
    else:
        zone = origin[0]
        xllcorner = origin[1]
        yllcorner = origin[2]

    # FIXME: Refactor using code from file_function.statistics
    # Something like print swwstats(swwname)
    if verbose:
        x = fid.variables['x'][:]
        y = fid.variables['y'][:]
        times = fid.variables['time'][:]
        log.critical('------------------------------------------------')
        log.critical('Statistics of SWW file:')
        log.critical('  Name: %s' % swwfile)
        log.critical('  Reference:')
        log.critical('    Lower left corner: [%f, %f]' %
                     (xllcorner, yllcorner))
        log.critical('    Start time: %f' % fid.starttime[0])
        log.critical('  Extent:')
        log.critical('    x [m] in [%f, %f], len(x) == %d' %
                     (num.min(x), num.max(x), len(x.flat)))
        log.critical('    y [m] in [%f, %f], len(y) == %d' %
                     (num.min(y), num.max(y), len(y.flat)))
        log.critical('    t [s] in [%f, %f], len(t) == %d' %
                     (min(times), max(times), len(times)))
        log.critical('  Quantities [SI units]:')
        for name in ['stage', 'xmomentum', 'ymomentum', 'elevation']:
            q = fid.variables[name][:].flat
            log.critical('    %s in [%f, %f]' % (name, min(q), max(q)))

    # Get quantity and reduce if applicable
    if verbose: log.critical('Processing quantity %s' % quantity)

    # Turn NetCDF objects into numeric arrays
    quantity_dict = {}
    for name in list(fid.variables.keys()):
        quantity_dict[name] = fid.variables[name][:]

    # Convert quantity expression to quantities found in sww file
    q = apply_expression_to_dictionary(quantity, quantity_dict)

    if len(q.shape) == 2:
        # q has a time component and needs to be reduced along
        # the temporal dimension
        if verbose: log.critical('Reducing quantity %s' % quantity)

        q_reduced = num.zeros(number_of_points, num.float)
        for k in range(number_of_points):
            q_reduced[k] = reduction(q[:, k])
        q = q_reduced

    # Post condition: Now q has dimension: number_of_points
    assert len(q.shape) == 1
    assert q.shape[0] == number_of_points

    if verbose:
        log.critical('Processed values for %s are in [%f, %f]' %
                     (quantity, min(q), max(q)))

    # Create grid and update xll/yll corner and x,y
    vertex_points = num.concatenate((x[:, num.newaxis], y[:, num.newaxis]),
                                    axis=1)
    assert len(vertex_points.shape) == 2

    # Interpolate
    from anuga.fit_interpolate.interpolate import Interpolate
    interp = Interpolate(vertex_points, volumes, verbose=verbose)

    # Interpolate using quantity values
    if verbose: log.critical('Interpolating')
    interpolated_values = interp.interpolate(q, data_points).flatten()

    if verbose:
        log.critical(
            'Interpolated values are in [%f, %f]' %
            (num.min(interpolated_values), num.max(interpolated_values)))

    # Assign NODATA_value to all points outside bounding polygon
    # (from interpolation mesh)
    P = interp.mesh.get_boundary_polygon()
    outside_indices = outside_polygon(data_points, P, closed=True)

    for i in outside_indices:
        interpolated_values[i] = NODATA_value

    # Store results
    G = Geospatial_data(data_points=data_points,
                        attributes=interpolated_values)

    G.export_points_file(name_out, absolute=True)

    fid.close()