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'
def two_triangles_with_depths(two_triangles): 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
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')
def twenty_one_triangles_with_depths(twenty_one_triangles): """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
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])
def test_no_std_name(two_triangles_with_depths): """ 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'
def test_with_just_nodes_and_depths(two_triangles): 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_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])
def test_str(): d = UVar('depth', location='node', data=[1.0, 2.0, 3.0, 4.0]) assert str(d) == ('UVar object: depth, on the nodes, and 4 data points\n' 'Attributes: {}')
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, [])