def test_bad_grid(): grid = RectilinearField((np.arange(12), )) var_0_data = np.arange(grid.get_point_count()) grid.add_field('var_0', var_0_data, centering='point') with cd_temp() as _: with assert_raises(TypeError): tofile('test-1d', grid)
def new_rectilinear(self, **kwds): ndims = kwds.pop('ndims', 1) shape = np.random.random_integers(2, 101, ndims) args = [] for size in shape: args.append(np.cumsum((1. - np.random.random(size)))) return RectilinearField(*args, **kwds)
def test_bad_grid(tmpdir): grid = RectilinearField((np.arange(12),)) var_0_data = np.arange(grid.get_point_count()) grid.add_field("var_0", var_0_data, centering="point") with tmpdir.as_cwd(): with pytest.raises(TypeError): tofile("test-1d", grid)
def new_rectilinear(**kwds): from pymt.grids import RectilinearField ndims = kwds.pop("ndims", 1) shape = np.random.randint(3, 101 + 1, ndims) args = [] for size in shape: args.append(np.cumsum((1.0 - np.random.random(size)))) return RectilinearField(*args, **kwds)
def rectilinear_3d_field(): field = RectilinearField( [-1, 0, 1], [2, 4, 6], [10, 20, 30], coordinate_names=("elevation", "latitude", "longitude"), units=("m", "degrees_north", "degrees_east"), ) data = np.arange(field.get_point_count(), dtype=float) field.add_field("air__temperature", data, centering="point", units="F") return field
def test_1d(self): vtk_file = self.vtk_files["test_1d"] field = RectilinearField((1, 2, 4, 5), indexing="ij", units=("m", )) data = np.arange(4.0) field.add_field("Elevation", data, centering="point") attrs = dict(description="Example 1D nc file", author="Eric") field_tofile(field, vtk_file, attrs=attrs, append=True) self.assertTrue(os.path.isfile(vtk_file))
def test_1d(self): vtk_file = self.vtk_files['test_1d'] field = RectilinearField((1, 2, 4, 5), indexing='ij', units=('m', )) data = np.arange(4.) field.add_field('Elevation', data, centering='point') attrs = dict(description='Example 1D nc file', author='Eric') field_tofile(field, vtk_file, attrs=attrs, append=True) self.assertTrue(os.path.isfile(vtk_file))
def test_rectilinear(tmpdir, ndims): coords = [np.arange(dim) for dim in np.random.randint(1, 101, ndims)] field = RectilinearField(*coords, units=["m"] * ndims) data = np.arange(field.get_point_count()) field.add_field("Elevation", data, centering="point") attrs = dict(description="Example {0}D nc file".format(ndims), author="pytest") with tmpdir.as_cwd(): field_tofile(field, "rectilinear.nc", attrs=attrs, append=True) assert os.path.isfile("rectilinear.nc")
def test_2d(self): nc_file = self.temp_file_name(prefix='rectilinear.2d.', suffix='.nc') field = RectilinearField((1, 4, 5), (2, 3), units=('degrees_north', 'degrees_east'), coordinate_names=['latitude', 'longitude']) data = np.arange(6.) field.add_field('Temperature', data * 10, centering='point', units='C') field.add_field('Elevation', data, centering='point', units='meters') field.add_field('Velocity', data * 100, centering='point', units='m/s') field.add_field('Temp', data * 2, centering='point', units='F') attrs = dict(description='Example nc file', author='Eric') field_tofile(field, nc_file, attrs=attrs, append=True) self.assertTrue(os.path.isfile(nc_file)) root = self.open_as_netcdf(nc_file) self.assertDataVariableNames(root, [ 'mesh', 'Temperature', 'Elevation', 'Velocity', 'Temp', 'longitude', 'latitude', 'time' ]) self.assertDimensionsEqual(root, ['longitude', 'latitude', 'time']) self.assertDataVariableArrayEqual(root, 'longitude', [2., 3.]) self.assertDataVariableArrayEqual(root, 'latitude', [1., 4., 5.]) self.assertDataVariableLongNameEqual(root, 'longitude', 'longitude') self.assertDataVariableLongNameEqual(root, 'latitude', 'latitude') self.assertDataVariableUnitsEqual(root, 'longitude', 'degrees_east') self.assertDataVariableUnitsEqual(root, 'latitude', 'degrees_north') for name in ['Temperature', 'Elevation', 'Velocity', 'Temp']: self.assertDataVariableLongNameEqual(root, name, name) for (v, u) in dict(Temperature='C', Elevation='meters', Velocity='m/s', Temp='F').items(): self.assertDataVariableUnitsEqual(root, v, u) root.close()
def test_1d(self): nc_file = self.temp_file_name(prefix='rectilinear.1d.', suffix='.nc') field = RectilinearField((1, 2, 4, 5), units=('m', )) data = np.arange(4.) field.add_field('Elevation', data, centering='point') attrs = dict(description='Example 1D nc file', author='Eric') field_tofile(field, nc_file, attrs=attrs, append=True) self.assertIsFile(nc_file) root = self.open_as_netcdf(nc_file) self.assertDataVariableNames(root, ['mesh', 'Elevation', 'x', 'time']) self.assertDimensionsEqual(root, ['x', 'time']) self.assertDataVariableArrayEqual(root, 'x', [1, 2, 4, 5]) self.assertDataVariableLongNameEqual(root, 'Elevation', 'Elevation') self.assertDataVariableUnitsEqual(root, 'Elevation', '-') self.assertDataVariableUnitsEqual(root, 'x', 'm') root.close()
def test_2d(self): vtk_file = self.vtk_files["test_2d"] field = RectilinearField( (1, 4, 5), (2, 3), indexing="ij", units=("degrees_north", "degrees_east"), coordinate_names=["latitude", "longitude"], ) data = np.arange(6.0) field.add_field("Temperature", data * 10, centering="point", units="C") field.add_field("Elevation", data, centering="point", units="meters") field.add_field("Velocity", data * 100, centering="point", units="m/s") field.add_field("Temp", data * 2, centering="point", units="F") attrs = dict(description="Example nc file", author="Eric") field_tofile(field, vtk_file, attrs=attrs, append=True) self.assertTrue(os.path.isfile(vtk_file))
def test_2d(self): vtk_file = self.vtk_files['test_2d'] field = RectilinearField( (1, 4, 5), (2, 3), indexing='ij', units=('degrees_north', 'degrees_east'), coordinate_names=['latitude', 'longitude']) data = np.arange(6.) field.add_field('Temperature', data*10, centering='point', units='C') field.add_field('Elevation', data, centering='point', units='meters') field.add_field('Velocity', data*100, centering='point', units='m/s') field.add_field('Temp', data*2, centering='point', units='F') attrs = dict(description='Example nc file', author='Eric') field_tofile(field, vtk_file, attrs=attrs, append=True) self.assertTrue(os.path.isfile(vtk_file))
def rectilinear_2d_field(): field = RectilinearField([-1, 0, 1], [2, 4, 6]) data = np.arange(field.get_point_count()) field.add_field("air__temperature", data, centering="point", units="F") return field
def sample_field(): x = np.arange(100.0) y = np.arange(100.0) field = RectilinearField(y, x) field.add_field("probability", sample_surface(x, y, 1.0)) return field