def test_build_face_coordinates(): grid = two_triangles() grid.build_face_coordinates() coords = grid.face_coordinates assert coords.shape == (2, 2) assert np.allclose(coords, [(1.1, 0.76666667), (2.1, 1.43333333)])
def test_add_boundary_data(): grid = two_triangles() print(grid.boundaries) # add the boundary definitions: grid.boundaries = [(0,1), (0,2), (1,3), (2,3), ] # create a UVar object for boundary conditions: bnds = UVar('bounds', location='boundary', data=[0, 1, 0, 0, 1]) bnds.attributes["long_name"] = "model boundary conditions" # wrong size for data with pytest.raises(ValueError): grid.add_data(bnds) # correct data bnds.data = [0, 1, 0, 0] grid.add_data(bnds) assert grid.data['bounds'].name == 'bounds' assert np.array_equal( grid.data['bounds'].data, [0, 1, 0, 0] )
def test_with_just_nodes_and_depths(): filename = '2_triangles_depth.nc' grid = two_triangles() del grid.faces del grid.edges depth_array = [1.0, 2.0, 3.0, 4.0] depth = UVar('depth', 'node', np.array([1.0, 2.0, 3.0, 4.0]), {'units':'m', 'positive':'down', 'standard_name' : "sea_floor_depth_below_geoid", }) grid.add_data(depth) with chdir('files'): grid.save_as_netcdf(filename) # read it back in and check it out grid2 = UGrid.from_ncfile(filename, load_data=True) assert grid2.faces is None assert grid2.edges is None assert np.array_equal( grid2.nodes, grid.nodes ) assert np.array_equal( grid2.data['depth'].data, depth_array ) assert grid2.data['depth'].attributes == depth.attributes
def test_with_just_nodes_and_depths(): filename = '2_triangles_depth.nc' grid = two_triangles() del grid.faces del grid.edges depth_array = [1.0, 2.0, 3.0, 4.0] depth = DataSet('depth', 'node', [1.0, 2.0, 3.0, 4.0], {'units':'m', 'positive':'down', 'standard_name' : "sea_floor_depth_below_geoid", }) grid.add_data(depth) with chdir('files'): grid.save_as_netcdf(filename) # read it back in and check it out grid2 = UGrid.from_ncfile(filename, load_data=True) assert grid2.faces is None assert grid2.edges is None assert np.array_equal( grid2.nodes, grid.nodes ) assert np.array_equal( grid2.data['depth'].data, depth_array ) assert grid2.data['depth'].attributes == depth.attributes
def test_write_with_depths(): ''' tests writing a netcdf file with depth data ''' fname = 'temp.nc' grid = two_triangles() grid.mesh_name='mesh1' # create a dataset object for the depths: depths = DataSet('depth', location='node', data=[1.0, 2.0, 3.0, 4.0]) depths.attributes['units'] = 'm' depths.attributes["standard_name"] = "sea_floor_depth_below_geoid" depths.attributes["positive"] = "down" grid.add_data(depths) grid.save_as_netcdf(fname) with netCDF4.Dataset(fname) as ds: assert nc_has_variable(ds, 'mesh1') assert nc_has_variable(ds, 'depth') assert nc_var_has_attr_vals(ds, 'depth', {"coordinates" : "mesh1_node_lon mesh1_node_lat", "location" : "node", "mesh": "mesh1"})
def test_write_with_edge_data(): ''' tests writing a netcdf file with data on the edges (fluxes, maybe?) ''' fname = 'temp.nc' grid = two_triangles() grid.mesh_name = 'mesh2' # create a dataset object for fluxes: flux = DataSet('flux', location='edge', data=[0.0, 0.0, 4.1, 0.0, 5.1, ]) 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) #add coordinates for edges grid.build_edge_coordinates() grid.save_as_netcdf(fname) with netCDF4.Dataset(fname) as ds: assert nc_has_variable(ds, 'mesh2') assert nc_has_variable(ds, 'flux') assert nc_var_has_attr_vals(ds, 'flux', { "coordinates" : "mesh2_edge_lon mesh2_edge_lat", "location" : "edge", 'units' : 'm^3/s', "mesh": "mesh2", }) assert np.array_equal( ds.variables['mesh2_edge_lon'], grid.edge_coordinates[:,0] ) assert np.array_equal( ds.variables['mesh2_edge_lat'], grid.edge_coordinates[:,1] )
def test_add_boundary_data(): grid = two_triangles() print(grid.boundaries) # add the boundary definitions: grid.boundaries = [ (0, 1), (0, 2), (1, 3), (2, 3), ] # create a UVar object for boundary conditions: bnds = UVar('bounds', location='boundary', data=[0, 1, 0, 0, 1]) bnds.attributes["long_name"] = "model boundary conditions" # wrong size for data with pytest.raises(ValueError): grid.add_data(bnds) # correct data bnds.data = [0, 1, 0, 0] grid.add_data(bnds) assert grid.data['bounds'].name == 'bounds' assert np.array_equal(grid.data['bounds'].data, [0, 1, 0, 0])
def test_set_mesh_name(): fname = 'temp.nc' grid = two_triangles() grid.mesh_name = "mesh_2" grid.save_as_netcdf(fname) with netCDF4.Dataset(fname) as ds: assert nc_has_variable(ds, 'mesh_2') assert nc_var_has_attr_vals(ds, 'mesh_2', {'cf_role':'mesh_topology', 'topology_dimension' : 2, 'long_name': u'Topology data of 2D unstructured mesh' }) assert nc_var_has_attr_vals(ds, 'mesh_2', {'cf_role':'mesh_topology', 'topology_dimension' : 2, 'long_name': u'Topology data of 2D unstructured mesh', 'node_coordinates': 'mesh_2_node_lon mesh_2_node_lat', }) assert nc_has_variable(ds, 'mesh_2_node_lon') assert nc_has_variable(ds, 'mesh_2_node_lat') assert nc_has_variable(ds, 'mesh_2_face_nodes') assert nc_has_variable(ds, 'mesh_2_edge_nodes') assert nc_has_dimension(ds, "mesh_2_num_node") assert nc_has_dimension(ds, "mesh_2_num_edge") assert nc_has_dimension(ds, "mesh_2_num_face") assert nc_has_dimension(ds, "mesh_2_num_vertices") assert not nc_var_has_attr(ds, 'mesh_2', "face_edge_connectivity")
def test_build_edges(): ugrid = two_triangles() ugrid.build_edges() edges = ugrid.edges edges.sort(axis=0) print edges assert np.array_equal(edges, [[0, 1], [0, 2], [1, 2], [1, 3], [2, 3]])
def test_build_face_coordinates(): grid = two_triangles() grid.build_face_coordinates() coords = grid.face_coordinates assert coords.shape == (2,2) assert np.allclose(coords, [ (1.1, 0.76666667), (2.1, 1.43333333)])
def test_simple_write(): grid = two_triangles() grid.save_as_netcdf('two_triangles.nc') ## be good to have an actual test here... assert True
def test_add_face_data_wrong(): """too short an array""" grid = two_triangles() # create a UVar object for velocity: u_vel = UVar('u', location='face', data=[1.0]) with pytest.raises(ValueError): grid.add_data(u_vel)
def test_add_node_data_wrong(): """too short an array""" grid = two_triangles() # create a UVar object for the depths: depths = UVar('depth', location='node', data=[1.0, 2.0, 3.0]) with pytest.raises(ValueError): grid.add_data(depths)
def test_build_edge_coordinates(): grid = two_triangles() grid.build_edge_coordinates() coords = grid.edge_coordinates nodes = grid.nodes print coords assert coords.shape == (5, 2) assert np.allclose( coords, [[1.1, 0.1], [2.6, 1.1], [2.1, 2.1], [0.6, 1.1], [1.6, 1.1]])
def test_build_boundaries(): ugrid = two_triangles() ugrid.build_face_face_connectivity() ugrid.build_boundaries() boundaries = ugrid.boundaries.tolist() boundaries.sort() # it doesn' matter what order they are in assert boundaries == [[0, 1], [1, 3], [2, 0], [3, 2]]
def test_add_all_data(): ''' you should not be able add a data dict directly ''' grid = two_triangles() assert grid.data == {} with pytest.raises(AttributeError): grid.data = {'depth': UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])}
def test_add_all_data(): ''' you should not be able add a data dict directly ''' grid = two_triangles() assert grid.data == {} with pytest.raises(AttributeError): grid.data = {'depth': DataSet('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])}
def test_add_edge_data_wrong(): """too long an array""" grid = two_triangles() # create a UVar object for velocity: # a miss-matched set bnds = UVar('bounds', location='edge', data=[0, 1, 0, 0, 1, 3, 3]) with pytest.raises(ValueError): grid.add_data(bnds)
def test_build_face_face_connectivity(): ugrid = two_triangles() ugrid.build_face_face_connectivity() face_face = ugrid.face_face_connectivity # order matters! print ugrid.faces assert np.array_equal(face_face[0], [-1, 1, -1]) assert np.array_equal(face_face[1], [-1, -1, 0])
def test_build_face_face_connectivity(): ugrid = two_triangles() ugrid.build_face_face_connectivity() face_face = ugrid.face_face_connectivity # order matters! print ugrid.faces assert np.array_equal( face_face[0], [-1, 1, -1] ) assert np.array_equal( face_face[1], [-1, -1, 0] )
def test_add_edge_data(): grid = two_triangles() # create a UVar object for velocity: bnds = UVar('bounds', location='edge', data=[0, 1, 0, 0, 1]) bnds.attributes["standard_name"] = "boundary type" grid.add_data(bnds) assert grid.data['bounds'].name == 'bounds' assert np.array_equal( grid.data['bounds'].data, [0, 1, 0, 0, 1] )
def test_add_edge_data(): grid = two_triangles() # create a UVar object for velocity: bnds = UVar('bounds', location='edge', data=[0, 1, 0, 0, 1]) bnds.attributes["standard_name"] = "boundary type" grid.add_data(bnds) assert grid.data['bounds'].name == 'bounds' assert np.array_equal(grid.data['bounds'].data, [0, 1, 0, 0, 1])
def test_build_boundary_coordinates(): grid = two_triangles() # add some boundaries grid.boundaries = [(0, 1), (0, 2), (2, 3), (1, 3)] grid.build_boundary_coordinates() coords = grid.boundary_coordinates nodes = grid.nodes print coords assert coords.shape == (4, 2) assert np.allclose(coords, [[1.1, 0.1], [0.6, 1.1], [2.1, 2.1], [2.6, 1.1]])
def test_build_edge_coordinates(): grid = two_triangles() grid.build_edge_coordinates() coords = grid.edge_coordinates nodes = grid.nodes print coords assert coords.shape == (5,2) assert np.allclose(coords, [[ 1.1, 0.1], [ 2.6, 1.1], [ 2.1, 2.1], [ 0.6, 1.1], [ 1.6, 1.1]])
def test_add_face_data(): grid = two_triangles() # create a UVar object for velocity: u_vel = UVar('u', location='face', data=[1.0, 2.0]) u_vel.attributes['units'] = 'm/s' u_vel.attributes["standard_name"] = "eastward_sea_water_velocity" grid.add_data(u_vel) assert grid.data['u'].name == 'u' assert grid.data['u'].attributes['units'] == 'm/s' assert np.array_equal( grid.data['u'].data, [1.0, 2.0] )
def test_add_face_data(): grid = two_triangles() # create a UVar object for velocity: u_vel = UVar('u', location='face', data=[1.0, 2.0]) u_vel.attributes['units'] = 'm/s' u_vel.attributes["standard_name"] = "eastward_sea_water_velocity" grid.add_data(u_vel) assert grid.data['u'].name == 'u' assert grid.data['u'].attributes['units'] == 'm/s' assert np.array_equal(grid.data['u'].data, [1.0, 2.0])
def test_without_faces(): grid = two_triangles() del grid.faces assert grid.faces is None with chdir('files'): grid.save_as_netcdf('2_triangles.nc') # read it back in and check it out grid2 = UGrid.from_ncfile('2_triangles.nc') assert grid2.faces is None assert np.array_equal(grid.faces, grid2.faces) assert np.array_equal(grid.edges, grid2.edges)
def test_build_face_face_connectivity(): ugrid = two_triangles() ugrid.build_face_face_connectivity() face_face = ugrid.face_face_connectivity #zeroth triangle should have two empty faces assert sum(face_face[0] == -1) == 2 #zeroth triangle should have one neighbor: 1 assert sum(face_face[0] == 1) == 1 #first triangle should have two empty faces assert sum(face_face[1] == -1) == 2 #first triangle should have one neighbor: 0 assert sum(face_face[1] == 1) == 0
def test_add_node_data(): grid = two_triangles() # create a UVar object for the depths: depths = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0]) depths.attributes['units'] = 'm' depths.attributes["standard_name"] = "sea_floor_depth" depths.attributes["positive"] = "down" grid.add_data(depths) assert grid.data['depth'].name == 'depth' assert grid.data['depth'].attributes['units'] == 'm' assert np.array_equal( grid.data['depth'].data, [1.0, 2.0, 3.0, 4.0] )
def test_add_node_data(): grid = two_triangles() # create a UVar object for the depths: depths = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0]) depths.attributes['units'] = 'm' depths.attributes["standard_name"] = "sea_floor_depth" depths.attributes["positive"] = "down" grid.add_data(depths) assert grid.data['depth'].name == 'depth' assert grid.data['depth'].attributes['units'] == 'm' assert np.array_equal(grid.data['depth'].data, [1.0, 2.0, 3.0, 4.0])
def test_simple_write(): fname = 'temp.nc' grid = two_triangles() grid.save_as_netcdf(fname) ## could be lots of tests here... with netCDF4.Dataset(fname) as ds: assert nc_has_variable(ds, 'mesh') assert nc_var_has_attr_vals(ds, 'mesh', {'cf_role':'mesh_topology', 'topology_dimension' : 2, 'long_name': u'Topology data of 2D unstructured mesh' })
def test_write_with_bound_data(): ''' tests writing a netcdf file with data on the boundaries suitable for boundary conditions, for example -- (fluxes, maybe?) ''' fname = 'temp.nc' grid = two_triangles() # using default mesh name # add the boundary definitions: grid.boundaries = [(0,1), (0,2), (1,3), (2,3), ] # create a dataset object for boundary conditions: bnds = DataSet('bnd_cond', location='boundary', data=[0, 1, 0, 0]) 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) with netCDF4.Dataset(fname) as ds: 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", }) ## there should be no coordinates attribute or variable for the boundaries ## as there is no boundaries_coordinates defined assert not nc_has_variable(ds, 'mesh_boundary_lon') assert not nc_has_variable(ds, 'mesh_boundary_lat') assert not nc_var_has_attr(ds, 'bnd_cond', 'coordinates')
def test_build_boundary_coordinates(): grid = two_triangles() # add some boundaries grid.boundaries = [(0,1), (0,2), (2,3), (1,3)] grid.build_boundary_coordinates() coords = grid.boundary_coordinates nodes = grid.nodes print coords assert coords.shape == (4,2) assert np.allclose(coords, [[ 1.1, 0.1], [ 0.6, 1.1], [ 2.1, 2.1], [ 2.6, 1.1]])
def test_with_faces(): """ test with faces, edges, but no face_coordintates or edge_coordinates """ with chdir('files'): grid = two_triangles() grid.save_as_netcdf('2_triangles.nc') # read it back in and check it out grid2 = UGrid.from_ncfile('2_triangles.nc') assert np.array_equal(grid.nodes, grid2.nodes) assert np.array_equal(grid.faces, grid2.faces) print grid2.edges assert np.array_equal(grid.edges, grid2.edges)
def test_with_faces(): """ test with faces, edges, but no face_coordintates or edge_coordinates """ with chdir('files'): grid = two_triangles() grid.save_as_netcdf('2_triangles.nc') # read it back in and check it out grid2 = UGrid.from_ncfile('2_triangles.nc') assert np.array_equal(grid.nodes, grid2.nodes) assert np.array_equal(grid.faces, grid2.faces) print(grid2.edges) assert np.array_equal(grid.edges, grid2.edges)
def test_write_with_velocities(): ''' tests writing a netcdf file with velocities on the faces ''' fname = 'temp.nc' grid = two_triangles() grid.mesh_name = 'mesh2' # create a dataset object for u velocity: u_vel = DataSet('u', location='face', data=[1.0, 2.0]) 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=[3.2, 4.3]) v_vel.attributes['units'] = 'm/s' v_vel.attributes["standard_name"] = "northward_sea_water_velocity" grid.add_data(v_vel) # add coordinates for face data grid.build_face_coordinates() grid.save_as_netcdf(fname) with netCDF4.Dataset(fname) as ds: assert nc_has_variable(ds, 'mesh2') assert nc_has_variable(ds, 'u') assert nc_has_variable(ds, 'v') assert nc_var_has_attr_vals(ds, 'u', { "coordinates" : "mesh2_face_lon mesh2_face_lat", "location" : "face", "mesh": "mesh2", })