def test_two_triangles_without_edges(): grid = two_triangles_with_depths() grid.edges = None fname = '2_triangles_without_edges.nc' with chdir(test_files): grid.save_as_netcdf(fname) ug = UGrid.from_ncfile(fname, load_data=True) os.remove(fname) assert ug.nodes.shape == (4, 2) assert ug.nodes.shape == grid.nodes.shape # FIXME: Not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes[0, :], (0.1, 0.1)) assert np.array_equal(ug.nodes[-1, :], (3.1, 2.1)) assert np.array_equal(ug.nodes, grid.nodes) assert ug.faces.shape == grid.faces.shape assert ug.edges is None depths = find_depths(ug) assert depths.data.shape == (4, ) assert depths.data[0] == 1 assert depths.attributes['units'] == 'unknown'
def test_with_just_nodes_and_depths(): expected = two_triangles() del expected.faces del expected.edges 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'}) expected.add_data(depth) fname = '2_triangles_depth.nc' with chdir(test_files): expected.save_as_netcdf(fname) grid = UGrid.from_ncfile(fname, load_data=True) os.remove(fname) assert grid.faces is None assert grid.edges is None assert np.array_equal(expected.nodes, grid.nodes) assert np.array_equal(expected.data['depth'].data, grid.data['depth'].data) assert expected.data['depth'].attributes == grid.data['depth'].attributes
def test_two_triangles_without_edges(): grid = two_triangles_with_depths() grid.edges = None grid.save_as_netcdf('2_triangles_without_edges.nc') # read it back in and check it out ug = UGrid.from_ncfile('2_triangles_without_edges.nc', load_data=True) assert ug.nodes.shape == (4, 2) assert ug.nodes.shape == grid.nodes.shape # not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes[0, :], (0.1, 0.1)) assert np.array_equal(ug.nodes[-1, :], (3.1, 2.1)) assert np.array_equal(ug.nodes, grid.nodes) assert ug.faces.shape == grid.faces.shape assert ug.edges is None depths = find_depths(ug) assert depths.data.shape == (4, ) assert depths.data[0] == 1 assert depths.attributes['units'] == "unknown"
def ugrid(location, name): """ Create a cube from an unstructured grid. Args: * location: A string whose value represents the path to a file or URL to an OpenDAP resource conforming to the Unstructured Grid Metadata Conventions for Scientific Datasets https://github.com/ugrid-conventions/ugrid-conventions * name: A string whose value represents a cube loading constraint of first the standard name if found, then the long name if found, then the variable name if found, before falling back to the value of the default which itself defaults to "unknown" Returns: An instance of :class:`iris.cube.Cube` decorated with an instance of :class:`pyugrid.ugrid.Ugrid` bound to an attribute of the cube called "mesh" """ cube = iris.load_cube(location, name) ug = UGrid.from_ncfile(location) cube.mesh = ug cube.mesh_dimension = 1 # {0:time, 1:node} return cube
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_two_triangles_without_edges(): grid = two_triangles_with_depths() grid.edges = None fname = '2_triangles_without_edges.nc' with chdir(test_files): grid.save_as_netcdf(fname) ug = UGrid.from_ncfile(fname, load_data=True) os.remove(fname) assert ug.nodes.shape == (4, 2) assert ug.nodes.shape == grid.nodes.shape # FIXME: Not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes[0, :], (0.1, 0.1)) assert np.array_equal(ug.nodes[-1, :], (3.1, 2.1)) assert np.array_equal(ug.nodes, grid.nodes) assert ug.faces.shape == grid.faces.shape assert ug.edges is None depths = find_depths(ug) assert depths.data.shape == (4,) assert depths.data[0] == 1 assert depths.attributes['units'] == 'unknown'
def test_with_just_nodes_and_depths(): expected = two_triangles() del expected.faces del expected.edges 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' }) expected.add_data(depth) fname = '2_triangles_depth.nc' with chdir(test_files): expected.save_as_netcdf(fname) grid = UGrid.from_ncfile(fname, load_data=True) os.remove(fname) assert grid.faces is None assert grid.edges is None assert np.array_equal(expected.nodes, grid.nodes) assert np.array_equal(expected.data['depth'].data, grid.data['depth'].data) assert expected.data['depth'].attributes == grid.data['depth'].attributes
def test_two_triangles_without_edges(): grid = two_triangles_with_depths() grid.edges = None grid.save_as_netcdf("2_triangles_without_edges.nc") # read it back in and check it out ug = UGrid.from_ncfile("2_triangles_without_edges.nc", load_data=True) assert ug.nodes.shape == (4, 2) assert ug.nodes.shape == grid.nodes.shape # not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes[0, :], (0.1, 0.1)) assert np.array_equal(ug.nodes[-1, :], (3.1, 2.1)) assert np.array_equal(ug.nodes, grid.nodes) assert ug.faces.shape == grid.faces.shape assert ug.edges is None depths = find_depths(ug) assert depths.data.shape == (4,) assert depths.data[0] == 1 assert depths.attributes["units"] == "unknown"
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_with_just_nodes_and_depths(): expected = two_triangles() del expected.faces del expected.edges 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"}, ) expected.add_data(depth) fname = "2_triangles_depth.nc" with chdir(test_files): expected.save_as_netcdf(fname) grid = UGrid.from_ncfile(fname, load_data=True) os.remove(fname) assert grid.faces is None assert grid.edges is None assert np.array_equal(expected.nodes, grid.nodes) assert np.array_equal(expected.data["depth"].data, grid.data["depth"].data) assert expected.data["depth"].attributes == grid.data["depth"].attributes
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_without_faces(): expected = two_triangles() del expected.faces assert expected.faces is None fname = '2_triangles.nc' with chdir(test_files): expected.save_as_netcdf(fname) grid = UGrid.from_ncfile(fname) os.remove(fname) assert grid.faces is None assert np.array_equal(expected.faces, grid.faces) assert np.array_equal(expected.edges, grid.edges)
def test_21_triangles(): grid = twenty_one_triangles_with_depths() grid.save_as_netcdf("21_triangles.nc") # read it back in and check it out ug = UGrid.from_ncfile("21_triangles.nc", load_data=True) assert ug.nodes.shape == grid.nodes.shape # not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes, grid.nodes) depths = find_depths(ug) assert depths.data.shape == (20,) assert depths.data[0] == 1 assert depths.attributes["units"] == "unknown"
def test_with_faces(): """ Test with faces, edges, but no `face_coordinates` or `edge_coordinates`. """ expected = two_triangles() fname = '2_triangles.nc' with chdir(test_files): expected.save_as_netcdf(fname) grid = UGrid.from_ncfile(fname) os.remove(fname) assert np.array_equal(expected.nodes, grid.nodes) assert np.array_equal(expected.faces, grid.faces) assert np.array_equal(expected.edges, grid.edges)
def test_21_triangles(): grid = twenty_one_triangles_with_depths() grid.save_as_netcdf('21_triangles.nc') # read it back in and check it out ug = UGrid.from_ncfile('21_triangles.nc', load_data=True) assert ug.nodes.shape == grid.nodes.shape # not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes, grid.nodes) depths = find_depths(ug) assert depths.data.shape == (20, ) assert depths.data[0] == 1 assert depths.attributes['units'] == "unknown"
def test_21_triangles(): grid = twenty_one_triangles_with_depths() fname = '21_triangles.nc' with chdir(test_files): grid.save_as_netcdf(fname) ug = UGrid.from_ncfile(fname, load_data=True) os.remove(fname) assert ug.nodes.shape == grid.nodes.shape # FIXME: Not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes, grid.nodes) depths = find_depths(ug) assert depths.data.shape == (20,) assert depths.data[0] == 1 assert depths.attributes['units'] == 'unknown'
def test_21_triangles(): grid = twenty_one_triangles_with_depths() fname = '21_triangles.nc' with chdir(test_files): grid.save_as_netcdf(fname) ug = UGrid.from_ncfile(fname, load_data=True) os.remove(fname) assert ug.nodes.shape == grid.nodes.shape # FIXME: Not ideal to pull specific values out, but how else to test? assert np.array_equal(ug.nodes, grid.nodes) depths = find_depths(ug) assert depths.data.shape == (20, ) assert depths.data[0] == 1 assert depths.attributes['units'] == 'unknown'
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_everything(): """An example with all features enabled, and a less trivial grid.""" grid = twenty_one_triangles() grid.build_edges() grid.build_face_face_connectivity() grid.build_edge_coordinates() grid.build_face_coordinates() grid.build_boundary_coordinates() # Depth on the nodes. depths = UVar('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) # Create a UVar object for u velocity: u_vel = UVar('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 UVar object for v velocity: v_vel = UVar('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 = UVar('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) # Boundary conditions: bounds = np.zeros((19,), dtype=np.uint8) bounds[7] = 1 bnds = UVar('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) fname = 'full_example.nc' with chdir(test_files): grid.save_as_netcdf(fname) ds = netCDF4.Dataset(fname) # Now the tests: 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', }) ds.close() # And make sure pyugrid can reload it! with chdir(test_files): grid = UGrid.from_ncfile('full_example.nc', 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' assert len(grid.nodes) == 20 depth = grid.data['depth'] assert depth.attributes['units'] == 'm' u = grid.data['u'] assert u.attributes['units'] == 'm/s' os.remove(fname)
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'
def test_write_everything(): """An example with all features enabled, and a less trivial grid.""" grid = twenty_one_triangles() grid.build_edges() grid.build_face_face_connectivity() grid.build_edge_coordinates() grid.build_face_coordinates() grid.build_boundary_coordinates() # Depth on the nodes. depths = UVar('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) # Create a UVar object for u velocity: u_vel = UVar('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 UVar object for v velocity: v_vel = UVar('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 = UVar('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) # Boundary conditions: bounds = np.zeros((19,), dtype=np.uint8) bounds[7] = 1 bnds = UVar('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) fname = 'full_example.nc' with chdir(test_files): grid.save_as_netcdf(fname) ds = netCDF4.Dataset(fname) # Now the tests: 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'}) ds.close() # And make sure pyugrid can reload it! with chdir(test_files): grid = UGrid.from_ncfile('full_example.nc', 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' assert len(grid.nodes) == 20 depth = grid.data['depth'] assert depth.attributes['units'] == 'm' u = grid.data['u'] assert u.attributes['units'] == 'm/s' os.remove(fname)