Beispiel #1
0
def test_locate_node():
    """
    test finding a single node
    """

    ugrid = twenty_one_triangles()

    assert ugrid.locate_nodes((4.58, 5.08)) == 6
Beispiel #2
0
def test_locate_exact():
    """
    The nearest neighbor of the exact node locations had better be
    the nodes!
    """
    ugrid = twenty_one_triangles()

    assert np.array_equal(ugrid.locate_nodes(ugrid.nodes),
                          range(len(ugrid.nodes)))
Beispiel #3
0
def main():
    import sys
    app = wx.App(False)
    F = DrawFrame(None, title="UGRID Test App", size=(700, 700))

    if len(sys.argv) > 1:
        filename = sys.argv[1]
        F.load_ugrid_file(filename)
    else:
        from pyugrid import test_examples
        F.Draw_UGRID(test_examples.twenty_one_triangles())

    app.MainLoop()
def test_build_boundaries2():
    """
    same as above, but with larger set
    """
    ugrid = twenty_one_triangles()

    ugrid.build_face_face_connectivity()
    ugrid.build_boundaries()

    boundaries = ugrid.boundaries.tolist()
    boundaries.sort() # it doesn't matter what order they are in

    assert boundaries == [[0, 1], [1, 5], [2, 0], [3, 6], [4, 3], [5, 11], [6, 9], [7, 2], [9, 10], [10, 4], [11, 14], [12, 7], [13, 12], [14, 16], [15, 13], [16, 18], [17, 15], [18, 19], [19, 17]]
Beispiel #5
0
def main():
    import sys
    app = wx.App(False)
    F = DrawFrame(None, title="UGRID Test App", size=(700, 700))

    if len(sys.argv) > 1:
        filename = sys.argv[1]
        F.load_ugrid_file(filename)
    else:
        from pyugrid import test_examples
        F.Draw_UGRID(test_examples.twenty_one_triangles())

    app.MainLoop()
Beispiel #6
0
def test_locate_middle():
    """
    see what happens the point is equidistant to two nodes
    """
    ugrid = twenty_one_triangles()

    # (3,5) is equidistant bewteen nodes 2, 6 and 7
    # 2 is returned, but might be arbitrary
    # assert ugrid.locate_nodes( (3, 5) ) == 2

    # perturb the point a bit, and nearest changes to:
    assert ugrid.locate_nodes((3.0000000001, 5)) == 6
    assert ugrid.locate_nodes((3, 5.00000000001)) == 7
    assert ugrid.locate_nodes((3, 4.99999999999)) == 2
def test_build_face_face_connectivity2():
    """
    test with a slightly larger mesh
    """
    ugrid = twenty_one_triangles()

    ugrid.build_face_face_connectivity()

    face_face = ugrid.face_face_connectivity

    assert np.sort(face_face[0]).tolist() ==  [-1,  2,  3]
    assert np.sort(face_face[8]).tolist() ==  [-1,  6,  9]
    assert np.sort(face_face[15]).tolist() == [13, 14, 16]
    assert np.sort(face_face[20]).tolist() == [-1, -1, 19]
    assert np.sort(face_face[9]).tolist() ==  [ 7,  8, 10]
def test_build_face_face_connectivity2():
    """
    test with a slightly larger mesh
    """
    ugrid = twenty_one_triangles()

    ugrid.build_face_face_connectivity()

    face_face = ugrid.face_face_connectivity

    assert face_face[0].tolist()  ==  [-1,  3,  2]
    assert face_face[8].tolist()  ==  [-1,  9,  6]
    assert face_face[15].tolist() ==  [14, 16, 13]
    assert face_face[20].tolist() ==  [19, -1, -1]
    assert face_face[9].tolist()  ==  [ 8,  10, 7]
def test_build_face_face_connectivity2():
    """
    test with a slightly larger mesh
    """
    ugrid = twenty_one_triangles()

    ugrid.build_face_face_connectivity()

    face_face = ugrid.face_face_connectivity

    assert face_face[0].tolist() == [-1, 3, 2]
    assert face_face[8].tolist() == [-1, 9, 6]
    assert face_face[15].tolist() == [14, 16, 13]
    assert face_face[20].tolist() == [19, -1, -1]
    assert face_face[9].tolist() == [8, 10, 7]
Beispiel #10
0
def test_locate_nodes():
    """
    test finding multiple nodes at once
    """

    ugrid = twenty_one_triangles()

    assert np.array_equal(
        ugrid.locate_nodes((
            (4.58, 5.08),
            (4.81, 0.89),
            (6.43, 12.9),
            (8.74, 6.86),
            (5.12, 7.31),
        )), (6, 0, 17, 10, 8))
def test_build_boundaries2():
    """
    same as above, but with larger set
    """
    ugrid = twenty_one_triangles()

    ugrid.build_face_face_connectivity()
    ugrid.build_boundaries()

    boundaries = ugrid.boundaries.tolist()
    boundaries.sort()  # it doesn't matter what order they are in

    assert boundaries == [[0, 1], [1, 5], [2, 0], [3, 6], [4, 3], [5, 11],
                          [6, 9], [7, 2], [9, 10], [10, 4], [11, 14], [12, 7],
                          [13, 12], [14, 16], [15, 13], [16, 18], [17, 15],
                          [18, 19], [19, 17]]
Beispiel #12
0
def test_write_everything():
    """ An example with all features enabled, and a less trivial grid """

    # use a small, but interesting grid
    fname = 'full_example.nc'

    grid = twenty_one_triangles() # using default mesh name
    grid.build_face_face_connectivity()
    grid.build_edges()

    grid.build_edge_coordinates()
    grid.build_face_coordinates()
    grid.build_boundary_coordinates()

    # depth on the nodes
    depths = DataSet('depth', location='node', data=np.linspace(1,10,20))
    depths.attributes['units'] = 'm'
    depths.attributes["standard_name"] = "sea_floor_depth_below_geoid"
    depths.attributes["positive"] = "down"

    grid.add_data(depths)

    # velocities on the faces:
    u_vel = DataSet('u', location='face', data=np.sin(np.linspace(3,12,21)))
    u_vel.attributes['units'] = 'm/s'
    u_vel.attributes["standard_name"] = "eastward_sea_water_velocity"

    grid.add_data(u_vel)

    # create a dataset object for v velocity:
    v_vel = DataSet('v', location='face', data=np.sin(np.linspace(12,15,21)))
    v_vel.attributes['units'] = 'm/s'
    v_vel.attributes["standard_name"] = "northward_sea_water_velocity"

    grid.add_data(v_vel)

    # fluxes on the edges:
    flux = DataSet('flux', location='edge', data=np.linspace(1000,2000,41))
    flux.attributes['units'] = 'm^3/s'
    flux.attributes["long_name"] = "volume flux between cells"
    flux.attributes["standard_name"] = "ocean_volume_transport_across_line"

    grid.add_data(flux)

    # Some boundary conditions:

    bounds = np.zeros( (19,), dtype=np.uint8 )
    bounds[7] = 1
    bnds = DataSet('bnd_cond', location='boundary', data=bounds)
    bnds.attributes["long_name"] = "model boundary conditions"
    bnds.attributes["flag_values"] = "0 1"
    bnds.attributes["flag_meanings"] = "no_flow_boundary  open_boundary"

    grid.add_data(bnds)

    grid.save_as_netcdf(fname)

    ## now the tests:
    with netCDF4.Dataset(fname) as ds:

        assert nc_has_variable(ds, 'mesh')
        assert nc_has_variable(ds, 'depth')

        assert nc_var_has_attr_vals(ds, 'depth', {"coordinates" : "mesh_node_lon mesh_node_lat",
                                                  "location" : "node"})

        assert nc_has_variable(ds, 'u')
        assert nc_has_variable(ds, 'v')

        assert nc_var_has_attr_vals(ds, 'u', {
                                              "coordinates" : "mesh_face_lon mesh_face_lat",
                                              "location" : "face",
                                              "mesh": "mesh"
                                              })

        assert nc_var_has_attr_vals(ds, 'v', {
                                              "coordinates" : "mesh_face_lon mesh_face_lat",
                                              "location" : "face",
                                              "mesh": "mesh",
                                              })

        assert nc_has_variable(ds, 'flux')

        assert nc_var_has_attr_vals(ds, 'flux', {
                                              "coordinates" : "mesh_edge_lon mesh_edge_lat",
                                              "location" : "edge",
                                              'units' : 'm^3/s',
                                              "mesh": "mesh",
                                              })
        assert nc_has_variable(ds, 'mesh')
        assert nc_has_variable(ds, 'bnd_cond')

        assert nc_var_has_attr_vals(ds, 'mesh', {
                                              "boundary_node_connectivity" : "mesh_boundary_nodes",
                                              })

        assert nc_var_has_attr_vals(ds, 'bnd_cond', {
                                              "location" : "boundary",
                                              "flag_values" : "0 1",
                                              "flag_meanings" : "no_flow_boundary  open_boundary",
                                              "mesh": "mesh",
                                              })
    # and make sure pyugrid can reload it!
    grid = UGrid.from_ncfile(fname,load_data=True)
    # and that some things are the same:
    # note:  more testing might be good here...
    #        maybe some grid comparison functions? 

    assert grid.mesh_name == 'mesh'

    print "grid data:", grid.data
    assert len(grid.nodes) == 20


    depth = grid.data['depth']
    assert depth.attributes['units'] == 'm'

    u = grid.data['u']
    assert u.attributes['units'] == 'm/s'
Beispiel #13
0
        Updates the status bar with the world coordinates

        """
        self.SetStatusText("%.2f, %.2f" % tuple(event.Coords))

    def OnQuit(self, Event):
        self.Destroy()

    def OnOpen(self, event):
        dlg = wx.FileDialog(self, 'Choose a ugrid file to open', '.', '',
                            '*.nc', wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            self.load_ugrid_file(filename)
        dlg.Destroy()


if __name__ == "__main__":
    import sys
    app = wx.App(False)
    F = DrawFrame(None, title="UGRID Test App", size=(700, 700))

    if len(sys.argv) > 1:
        filename = sys.argv[1]
        F.load_ugrid_file(filename)
    else:
        from pyugrid import test_examples
        F.Draw_UGRID(test_examples.twenty_one_triangles())

    app.MainLoop()
Beispiel #14
0
        self.Canvas.AddPointSet(nodes, Diameter=5, Color=self.node_color) 
        self.Canvas.ZoomToBB()

    def OnMove(self, event):
        """
        Updates the status bar with the world coordinates

        """
        self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))


if __name__ == "__main__":
    from pyugrid import test_examples

    app = wx.App(False)
    F = DrawFrame(None, title="UGRID Test App", size=(700,700) )
    #F.Draw_UGRID( test_examples.two_triangles() )
    F.Draw_UGRID( test_examples.twenty_one_triangles() )

    app.MainLoop()
    
    
    







def test_write_everything():
    """ An example with all features enabled, and a less trivial grid """

    # use a small, but interesting grid
    fname = 'full_example.nc'

    grid = twenty_one_triangles() # using default mesh name
    grid.build_face_face_connectivity()
    grid.build_edges()

    grid.build_edge_coordinates()
    grid.build_face_coordinates()
    grid.build_boundary_coordinates()

    # depth on the nodes
    depths = DataSet('depth', location='node', data=np.linspace(1,10,20))
    depths.attributes['units'] = 'm'
    depths.attributes["standard_name"] = "sea_floor_depth_below_geoid"
    depths.attributes["positive"] = "down"

    grid.add_data(depths)

    # velocities on the faces:
    u_vel = DataSet('u', location='face', data=np.sin(np.linspace(3,12,21)))
    u_vel.attributes['units'] = 'm/s'
    u_vel.attributes["standard_name"] = "eastward_sea_water_velocity"

    grid.add_data(u_vel)

    # create a dataset object for v velocity:
    v_vel = DataSet('v', location='face', data=np.sin(np.linspace(12,15,21)))
    v_vel.attributes['units'] = 'm/s'
    v_vel.attributes["standard_name"] = "northward_sea_water_velocity"

    grid.add_data(v_vel)

    # fluxes on the edges:
    flux = DataSet('flux', location='edge', data=np.linspace(1000,2000,41))
    flux.attributes['units'] = 'm^3/s'
    flux.attributes["long_name"] = "volume flux between cells"

    grid.add_data(flux)

    # Some boundary conditions:

    print grid.boundaries.shape
    print grid.boundaries
    bounds = np.zeros( (19,), dtype=np.uint8 )
    bounds[7] = 1
    bnds = DataSet('bnd_cond', location='boundary', data=bounds)
    bnds.attributes["long_name"] = "model boundary conditions"
    bnds.attributes["flag_values"] = "0 1"
    bnds.attributes["flag_meanings"] = "no_flow_boundary  open_boundary"

    grid.add_data(bnds)

    grid.save_as_netcdf(fname)

    ## now the tests:
    with netCDF4.Dataset(fname) as ds:

        assert nc_has_variable(ds, 'mesh')
        assert nc_has_variable(ds, 'depth')

        assert nc_var_has_attr_vals(ds, 'depth', {"coordinates" : "mesh_node_lon mesh_node_lat",
                                                  "location" : "node"})

        assert nc_has_variable(ds, 'u')
        assert nc_has_variable(ds, 'v')

        assert nc_var_has_attr_vals(ds, 'u', {
                                              "coordinates" : "mesh_face_lon mesh_face_lat",
                                              "location" : "face",
                                              "mesh": "mesh"
                                              })

        assert nc_var_has_attr_vals(ds, 'v', {
                                              "coordinates" : "mesh_face_lon mesh_face_lat",
                                              "location" : "face",
                                              "mesh": "mesh",
                                              })

        assert nc_has_variable(ds, 'flux')

        assert nc_var_has_attr_vals(ds, 'flux', {
                                              "coordinates" : "mesh_edge_lon mesh_edge_lat",
                                              "location" : "edge",
                                              'units' : 'm^3/s',
                                              "mesh": "mesh",
                                              })
        assert nc_has_variable(ds, 'mesh')
        assert nc_has_variable(ds, 'bnd_cond')

        assert nc_var_has_attr_vals(ds, 'mesh', {
                                              "boundary_node_connectivity" : "mesh_boundary_nodes",
                                              })

        assert nc_var_has_attr_vals(ds, 'bnd_cond', {
                                              "location" : "boundary",
                                              "flag_values" : "0 1",
                                              "flag_meanings" : "no_flow_boundary  open_boundary",
                                              "mesh": "mesh",
                                              })