Exemplo n.º 1
0
def test_write_with_depths():
    """Tests writing a netcdf file with depth data."""

    grid = two_triangles()
    grid.mesh_name = 'mesh1'

    # 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_below_geoid'
    depths.attributes['positive'] = 'down'

    grid.add_data(depths)

    fname = 'temp.nc'
    with chdir(test_files):
        grid.save_as_netcdf(fname)
        ds = netCDF4.Dataset(fname)
        os.remove(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()
Exemplo n.º 2
0
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])
Exemplo n.º 3
0
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] )
Exemplo n.º 4
0
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 UVar object for fluxes:
    flux = UVar('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] )
Exemplo n.º 5
0
def test_write_with_edge_data():
    """Tests writing a netcdf file with data on the edges (fluxes, maybe?)."""

    grid = two_triangles()
    grid.mesh_name = 'mesh2'

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

    fname = 'temp.nc'
    with chdir(test_files):
        grid.save_as_netcdf(fname)
        ds = netCDF4.Dataset(fname)
        os.remove(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'],
                          grid.edge_coordinates[:, 0])
    assert np.array_equal(ds.variables['mesh2_edge_lat'],
                          grid.edge_coordinates[:, 1])
    ds.close()
Exemplo n.º 6
0
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 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_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"})
Exemplo n.º 7
0
def test_add_attributes():
    d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])
    d.attributes = {'standard_name': 'sea_floor_depth_below_geoid',
                    'units': 'm',
                    'positive': 'down'}
    assert d.attributes['units'] == 'm'
    assert d.attributes['positive'] == 'down'
Exemplo n.º 8
0
def test_write_with_depths():
    """Tests writing a netcdf file with depth data."""

    grid = two_triangles()
    grid.mesh_name = 'mesh1'

    # 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_below_geoid'
    depths.attributes['positive'] = 'down'

    grid.add_data(depths)

    fname = 'temp.nc'
    with chdir(test_files):
        grid.save_as_netcdf(fname)
        ds = netCDF4.Dataset(fname)
        os.remove(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()
Exemplo n.º 9
0
def test_write_with_edge_data():
    """Tests writing a netcdf file with data on the edges (fluxes, maybe?)."""

    grid = two_triangles()
    grid.mesh_name = 'mesh2'

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

    fname = 'temp.nc'
    with chdir(test_files):
        grid.save_as_netcdf(fname)
        ds = netCDF4.Dataset(fname)
        os.remove(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'],
                          grid.edge_coordinates[:, 0])
    assert np.array_equal(ds.variables['mesh2_edge_lat'],
                          grid.edge_coordinates[:, 1])
    ds.close()
Exemplo n.º 10
0
def two_triangles_with_depths():
    grid = two_triangles()

    depths = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])
    depths.attributes['units'] = 'unknown'
    depths.attributes['standard_name'] = 'sea_floor_depth_below_geoid'
    depths.attributes['positive'] = 'down'
    grid.add_data(depths)

    return grid
Exemplo n.º 11
0
def two_triangles_with_depths():
    grid = two_triangles()

    depths = UVar("depth", location="node", data=[1.0, 2.0, 3.0, 4.0])
    depths.attributes["units"] = "unknown"
    depths.attributes["standard_name"] = "sea_floor_depth_below_geoid"
    depths.attributes["positive"] = "down"
    grid.add_data(depths)

    return grid
Exemplo n.º 12
0
def test_init():
    d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])

    assert d.name == 'depth'
    assert np.array_equal(d.data, [1.0, 2.0, 3.0, 4.0])
    assert d.location == 'node'
    assert d.attributes == {}

    with pytest.raises(ValueError):
        d = UVar('depth', location='nodes')
Exemplo n.º 13
0
def two_triangles_with_depths():
    grid = two_triangles()

    depths = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])
    depths.attributes['units'] = 'unknown'
    depths.attributes['standard_name'] = 'sea_floor_depth_below_geoid'
    depths.attributes['positive'] = 'down'
    grid.add_data(depths)

    return grid
Exemplo n.º 14
0
def add_attributes():
    d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])

    d.attributes = {"standard_name" : "sea_floor_depth_below_geoid",
                    "units" : "m",
                    "positive" : "down",
                    }

    assert d.attributes['units'] == 'm'
    assert d.attributes['posative'] == 'down'
Exemplo n.º 15
0
def twenty_one_triangles_with_depths():
    """Returns a basic triangle grid with 21 triangles, a hole and a tail."""
    grid = twenty_one_triangles()

    depths = UVar('depth', location='node', data=list(range(1, 21)))
    depths.attributes['units'] = 'unknown'
    depths.attributes['standard_name'] = 'sea_floor_depth_below_geoid'
    depths.attributes['positive'] = 'down'
    grid.add_data(depths)

    return grid
Exemplo n.º 16
0
def twenty_one_triangles_with_depths():
    """Returns a basic triangle grid with 21 triangles, a hole and a tail."""
    grid = twenty_one_triangles()

    depths = UVar('depth', location='node', data=list(range(1, 21)))
    depths.attributes['units'] = 'unknown'
    depths.attributes['standard_name'] = 'sea_floor_depth_below_geoid'
    depths.attributes['positive'] = 'down'
    grid.add_data(depths)

    return grid
Exemplo n.º 17
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])
Exemplo n.º 18
0
def test_add_data():
    d = UVar('depth', location='node')
    assert d.name == 'depth'
    assert np.array_equal(d.data, [])

    # Add the data:
    d.data = [1.0, 2.0, 3.0, 4.0]

    assert np.array_equal(d.data, [1.0, 2.0, 3.0, 4.0])
    # Duck type check of nd.ndarray.
    d.data *= 2
    assert np.array_equal(d.data, [2.0, 4.0, 6.0, 8.0])
Exemplo n.º 19
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] )
Exemplo n.º 20
0
def twenty_one_triangles_with_depths():
    """
    returns a basic triangle grid with 21 triangles, a hole and a "tail"
    """
    grid = twenty_one_triangles()

    depths = UVar("depth", location="node", data=range(1, 21))
    depths.attributes["units"] = "unknown"
    depths.attributes["standard_name"] = "sea_floor_depth_below_geoid"
    depths.attributes["positive"] = "down"
    grid.add_data(depths)

    return grid
Exemplo n.º 21
0
def test_no_std_name():
    """
    tests to make sure it doesn' crash if a UVar does not have a standard_name
    """
    grid = two_triangles_with_depths()

    junk = UVar('junk', location='node', data=[1.0, 2.0, 3.0, 4.0])
    junk.attributes['units'] = 'unknown'
    grid.add_data(junk)

    depths = find_depths(grid)

    assert depths.name == 'depth'
Exemplo n.º 22
0
def test_no_std_name():
    """
    tests to make sure it doesn' crash if a UVar does not have a standard_name
    """
    grid = two_triangles_with_depths()

    junk = UVar("junk", location="node", data=[1.0, 2.0, 3.0, 4.0])
    junk.attributes["units"] = "unknown"
    grid.add_data(junk)

    depths = find_depths(grid)

    assert depths.name == "depth"
Exemplo n.º 23
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] )
Exemplo n.º 24
0
def test_init():
    # create a UVar object for the depths:
    d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])

    assert d.name == 'depth'
    print( d )
    print ( d.data )
    assert np.array_equal( d.data, [1.0, 2.0, 3.0, 4.0] )
    assert d.location == 'node'

    assert d.attributes == {}

    with pytest.raises(ValueError):       
        d = UVar('depth', location='nodes')
Exemplo n.º 25
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])
Exemplo n.º 26
0
def test_no_std_name():
    """
    Tests to make sure it doesn't crash if a `UVar` does not have a
    `standard_name`.

    """
    grid = two_triangles_with_depths()

    junk = UVar('junk', location='node', data=[1.0, 2.0, 3.0, 4.0])
    junk.attributes['units'] = 'unknown'
    grid.add_data(junk)

    depths = find_depths(grid)

    assert depths.name == 'depth'
Exemplo n.º 27
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] )
Exemplo n.º 28
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])
Exemplo n.º 29
0
def test_delete_data():
    # create a UVar object for the depths:
    d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])

    del d.data

    assert np.array_equal(d.data, [] )
Exemplo n.º 30
0
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
Exemplo n.º 31
0
def test_nc_variable():
    """
    test that it works with a netcdf variable object
    """
    import netCDF4

    # make a variable
    with chdir(test_files):
        fname = 'junk.nc'
        ds = netCDF4.Dataset(fname, mode='w')
        ds.createDimension('dim', (10))
        var = ds.createVariable('a_var', float, ('dim'))
        var[:] = np.arange(10)
        # give it some attributes
        var.attr_1 = 'some value'
        var.attr_2 = 'another value'

        # make a UVar from it
        uvar = UVar("a_var", 'node', data=var)

        assert uvar._data is var  # preserved the netcdf variable
        print(uvar.attributes)
        assert uvar.attributes == {'attr_1': 'some value',
                                   'attr_2': 'another value'}
        # access the data
        assert np.array_equal(uvar[3:5], [3.0, 4.0])
Exemplo n.º 32
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     
Exemplo n.º 33
0
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 UVar object for boundary conditions:
    bnds = UVar('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')
Exemplo n.º 34
0
def test_write_with_bound_data():
    """
    Tests writing a netcdf file with data on the boundaries
    suitable for boundary conditions, for example fluxes.

    """
    grid = two_triangles()

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

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

    fname = 'temp.nc'
    with chdir(test_files):
        grid.save_as_netcdf(fname)
        ds = netCDF4.Dataset(fname)
        os.remove(fname)

    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')
    ds.close()
Exemplo n.º 35
0
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)
Exemplo n.º 36
0
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)
Exemplo n.º 37
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)
Exemplo n.º 38
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': UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])
        }
Exemplo n.º 39
0
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 UVar object for u 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)

    # create a Uvar object for v velocity:
    v_vel = UVar('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",
                                              })
Exemplo n.º 40
0
def test_write_with_velocities():
    """Tests writing a netcdf file with velocities on the faces."""
    grid = two_triangles()
    grid.mesh_name = 'mesh2'

    # Create a UVar object for u 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)

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

    fname = 'temp.nc'
    with chdir(test_files):
        grid.save_as_netcdf(fname)
        ds = netCDF4.Dataset(fname)
        os.remove(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()
Exemplo n.º 41
0
def test_str():
    d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])
    print(str(d))
    assert str(d) == "UVar object: depth, on the nodes, and 4 data points\nAttributes: {}"
Exemplo n.º 42
0
def test_delete_data():
    d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0])

    del d.data
    assert np.array_equal(d.data, [])
Exemplo n.º 43
0
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)
Exemplo n.º 44
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 = 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)

    # velocities on the faces:
    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)

    # Some 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)

    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'