示例#1
0
def test_write_with_depths(two_triangles, ncds):
    """Tests writing a netcdf file with depth data."""

    fname, ncds = ncds

    grid = two_triangles
    grid.mesh_name = 'mesh1'

    gds = Dataset(grid=grid)

    # Create a Variable object for the depths:
    depths = Variable(name='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'

    gds.variables['depth'] = depths

    gds.save(ncds, format='netcdf4')

    ds = netCDF4.Dataset(fname)

    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'
        })
    ds.close()
示例#2
0
def test_find_variables():
    """
    does it find the variables?
    """
    ds = Dataset(data_file)

    var_names = list(ds.variables.keys())
    all_vars = ['mesh2d_node_z',
                'mesh2d_Numlimdt',
                'mesh2d_s1',
                'mesh2d_waterdepth',
                'mesh2d_s0',
                'mesh2d_ucx',
                'mesh2d_ucy',
                'mesh2d_ucmag',
                'mesh2d_ucxq',
                'mesh2d_ucyq',
                'mesh2d_taus',
                'mesh2d_czs',
                'mesh2d_sa1',
                'mesh2d_flowelem_ba',
                'mesh2d_flowelem_bl',
                'mesh2d_face_x_bnd',
                'mesh2d_face_y_bnd',
                ]

    for var in all_vars:
        assert var in var_names
    assert len(all_vars) == len(var_names)
示例#3
0
def test_load():
    """
    The file should load without error
    """
    ds = Dataset(data_file)

    assert isinstance(ds.grid, Grid_U)
示例#4
0
def test_find_variables():
    """
    Does it find the variables?
    """
    ds = Dataset(data_file)

    var_names = list(ds.variables.keys())

    all_vars = [
        'mesh2d_Numlimdt', 'mesh2d_czs', 'mesh2d_diu', 'mesh2d_edge_type',
        'mesh2d_edge_x_bnd', 'mesh2d_edge_y_bnd', 'mesh2d_face_x_bnd',
        'mesh2d_face_y_bnd', 'mesh2d_flowelem_ba', 'mesh2d_flowelem_bl',
        'mesh2d_hu', 'mesh2d_node_z', 'mesh2d_q1', 'mesh2d_s0', 'mesh2d_s1',
        'mesh2d_sa1', 'mesh2d_taus', 'mesh2d_u0', 'mesh2d_u1', 'mesh2d_ucmag',
        'mesh2d_ucx', 'mesh2d_ucxq', 'mesh2d_ucy', 'mesh2d_ucyq', 'mesh2d_viu',
        'mesh2d_waterdepth'
    ]

    all_vars.sort()
    var_names.sort()

    assert var_names == all_vars

    # check that they all have valid location attributes:
    VALID_LOCATIONS = [loc for loc in VALID_UGRID_LOCATIONS if loc is not None]
    for varname in var_names:
        assert ds[varname].location in VALID_LOCATIONS
示例#5
0
def test_read_variable_attributes():
    with chdir(test_data_dir):
        ds = Dataset('UGRIDv0.9_eleven_points.nc')
    print(ds.variables['Mesh2_depth'].attributes)
    assert (ds.variables['Mesh2_depth'].attributes['standard_name'] ==
            'sea_floor_depth_below_geoid')
    assert ds.variables['Mesh2_depth'].attributes['units'] == 'm'
示例#6
0
def test_write_with_bound_data(two_triangles, ncds):
    """
    Tests writing a netcdf file with data on the boundaries
    suitable for boundary conditions, for example fluxes.

    """
    fname, ncds = ncds
    grid = two_triangles
    grid.mesh_name = 'mesh3'

    gds = Dataset(grid=grid)

    # Add the boundary definitions:
    grid.boundaries = [(0, 1), (0, 2), (1, 3), (2, 3)]

    # Create a Variable object for boundary conditions:
    bnds = Variable('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'

    gds.variables['bnd_cond'] = bnds

    gds.save(ncds)

    ds = netCDF4.Dataset(fname)

    assert nc_has_variable(ds, 'mesh3')
    assert nc_has_variable(ds, 'bnd_cond')
    assert nc_var_has_attr_vals(
        ds, 'mesh3', {'boundary_node_connectivity': 'mesh3_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': 'mesh3',
        })
    # 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')
    ds.close()
示例#7
0
def test_write_with_edge_data(two_triangles, ncds):
    """Tests writing a netcdf file with data on the edges (fluxes, maybe?)."""

    fname, ncds = ncds

    two_triangles.mesh_name = 'mesh2'

    gds = Dataset(grid=two_triangles)

    # Create a Variable object for fluxes:
    flux = Variable('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'

    gds.variables['flux'] = flux

    # Add coordinates for edges.
    gds.grid.build_edge_coordinates()

    gds.save(ncds)

    ds = netCDF4.Dataset(fname)

    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'],
                          gds.grid.edge_coordinates[:, 0])
    assert np.array_equal(ds.variables['mesh2_edge_lat'],
                          gds.grid.edge_coordinates[:, 1])
    ds.close()
示例#8
0
def test_read_TAMU():
    with chdir(test_data_dir):
        if os.path.exists('TAMU.nc'):
            ds = Dataset('TAMU.nc')
            print("TAMU variables are:", ds.variables.keys())
            assert isinstance(ds, Dataset)
            assert isinstance(ds.grid, SGrid)
            assert isinstance(ds.variables, dict)
            assert 'water_u' in ds.variables.keys()
            assert 'water_v' in ds.variables.keys()
示例#9
0
def test_read_FVCOM():
    '''Optional test to make sure that files from TAMU and NGOFS are read correctly.'''
    with chdir(test_data_dir):
        if os.path.exists('COOPS_NGOFS.nc'):
            ds = Dataset('COOPS_NGOFS.nc')
            print("COOPS_NGOFS variables are:", ds.variables.keys())
            assert isinstance(ds, Dataset)
            assert isinstance(ds.grid, UGrid)
            assert isinstance(ds.variables, dict)
            assert 'u' in ds.variables.keys()
            assert 'v' in ds.variables.keys()
示例#10
0
def test_missing_variables(var_name):
    """
    these are known not to be found

    but they should be!
    """
    ds = Dataset(data_file)

    var_names = list(ds.variables.keys())

    assert var_name in var_names
示例#11
0
def test_read_variables():
    """
    It should get the test_read_variables
    """
    with chdir(test_data_dir):
        ds = Dataset('UGRIDv0.9_eleven_points.nc')
    varnames = list(ds.variables.keys())
    varnames.sort()
    print("variables are:", varnames)
    assert varnames == ['Mesh2_depth', 'Mesh2_face_u', 'Mesh2_face_v']
    for v in ds.variables.values():
        assert isinstance(v, Variable)
示例#12
0
def test_info():
    """
    Make sure the info property is working
    This doesn't test much -- jsut tht it won't crash
    """
    gds = Dataset(sample_sgrid_file)

    info = gds.info

    print(info)
    # just a couple checks to make sure it's not totally bogus
    assert "gridded.Dataset:" in info
    assert "variables:" in info
    assert "attributes:" in info
示例#13
0
def test_write_with_velocities(two_triangles, ncds):
    """Tests writing a netcdf file with velocities on the faces."""

    fname, ncds = ncds

    two_triangles.mesh_name = 'mesh2'

    gds = Dataset(grid=two_triangles)

    # Create a Variable object for u velocity:
    u_vel = Variable('u', location='face', data=[1.0, 2.0])
    u_vel.attributes['units'] = 'm/s'
    u_vel.attributes['standard_name'] = 'eastward_sea_water_velocity'

    gds.variables['u'] = u_vel

    # Create a Variable object for v velocity:
    v_vel = Variable('v', location='face', data=[3.2, 4.3])
    v_vel.attributes['units'] = 'm/s'
    v_vel.attributes['standard_name'] = 'northward_sea_water_velocity'

    gds.variables['v'] = v_vel

    # Add coordinates for face data.
    gds.grid.build_face_coordinates()

    gds.save(ncds)

    ds = netCDF4.Dataset(fname)

    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',
        })
    ds.close()
示例#14
0
def test_load_sgrid():
    """ tests you can intitilize an conforming sgrid file"""
    sinusoid = Dataset(sample_sgrid_file)

    assert True  # just to make it a test
示例#15
0
def test_save_invalid_format():
    ds = Dataset()

    with pytest.raises(ValueError):
        ds.save("a_filename.txt", format="text")
示例#16
0
def test_simple_read():
    """Can it be read at all?"""
    with chdir(test_data_dir):
        ds = Dataset('UGRIDv0.9_eleven_points.nc')
    assert isinstance(ds, Dataset)
    assert isinstance(ds.grid, UGrid)
示例#17
0
def test_write_everything(twenty_one_triangles, ncds):
    """An example with all features enabled, and a less trivial grid."""

    fname, ncds = ncds
    grid = twenty_one_triangles
    grid.mesh_name = 'mesh'

    gds = Dataset(grid=grid)

    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 = Variable('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'
    gds.variables['depth'] = depths

    # Create a Variable object for u velocity:
    u_vel = Variable('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'

    gds.variables['u'] = u_vel

    # Create a Variable object for v velocity:
    v_vel = Variable('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'

    gds.variables['v'] = v_vel

    # Fluxes on the edges:
    flux = Variable('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'

    gds.variables['flux'] = flux

    # Boundary conditions:
    bounds = np.zeros((19, ), dtype=np.uint8)
    bounds[7] = 1
    bnds = Variable('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'

    gds.variables['bnd_cond'] = bnds

    gds.save(ncds)

    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!
    gds = Dataset(fname)
    grid = gds.grid
    # 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 = gds.variables['depth']
    assert depth.attributes['units'] == 'm'

    u = gds.variables['u']
    assert u.attributes['units'] == 'm/s'