def test_dimension_mismatch(self): gradient = 0 values = np.zeros([3, 2]) with self.assertRaises(Exception) as e: output = gridpp.simple_gradient(grid, points, values, gradient) with self.assertRaises(Exception) as e: output = gridpp.simple_gradient(grid, grid, values, gradient)
def test_point_to_point_3d(self): values = np.zeros([2, 3, 3]) values[:, 0, 0] = 4 values[:, 1, 1] = 3 gradient = 0 output = gridpp.simple_gradient(grid, points, values, gradient) np.testing.assert_array_almost_equal(output, [[4, 3], [4, 3]]) gradient = 1 output = gridpp.simple_gradient(grid, points, values, gradient) np.testing.assert_array_almost_equal(output, [[9, -2], [9, -2]])
def test_point_to_point(self): values = np.zeros([3, 3]) values[0, 0] = 4 values[1, 1] = 3 # Point 0: Move up 5 meters # Point 1: Move down 5 meters gradient = 0 output = gridpp.simple_gradient(grid, points, values, gradient) np.testing.assert_array_almost_equal(output, [4, 3]) gradient = 1 output = gridpp.simple_gradient(grid, points, values, gradient) np.testing.assert_array_almost_equal(output, [9, -2])
def test_no_point_elev(self): """Check that output is missing when points do not have elevation defined""" points0 = gridpp.Points([-1, 0.9], [-1, 0.9]) values = np.reshape(np.arange(9), [3, 3]) for gradient in [0, 1]: output = gridpp.simple_gradient(grid, points0, values, gradient) np.testing.assert_array_almost_equal(output, [np.nan, np.nan])
def test_no_grid_elev(self): """Check that output is missing when grid does not have elevation defined""" grid0 = gridpp.Grid(lats, lons) values = np.reshape(np.arange(9), [3, 3]) for gradient in [0, 1]: output = gridpp.simple_gradient(grid0, points, values, gradient) np.testing.assert_array_almost_equal(output, [np.nan, np.nan])
def test_grid_to_grid(self): # Output grid has one row and column outside domain values = np.zeros([3, 3]) values[0, 0] = 4 values[1, 1] = 3 lats0, lons0 = np.meshgrid([-0.1, 0.1, 1.1], [-0.1, 0.1, 1.1]) elevs0 = np.reshape(np.arange(9), [3, 3]) grid0 = gridpp.Grid(lats0, lons0, elevs0) gradient = 0 output = gridpp.simple_gradient(grid, grid0, values, gradient) np.testing.assert_array_almost_equal(output, [[4, 4, 0], [4, 4, 0], [0, 0, 3]]) gradient = 1 output = gridpp.simple_gradient(grid, grid0, values, gradient) # Elevation changes: # -10 -> 0, -10 -> 1, 0 -> 2 # -10 -> 3, -10 -> 4, 0 -> 5 # 0 -> 6, 0 -> 7, 10 -> 8 np.testing.assert_array_almost_equal( output, [[14, 15, 2], [17, 18, 5], [6, 7, 1]])
def test_3d(self): values = np.zeros([2, 3, 3]) values[:, 0, 0] = 4 values[:, 1, 1] = 3 lats0, lons0 = np.meshgrid([-0.1, 0.1, 1.1], [-0.1, 0.1, 1.1]) elevs0 = np.reshape(np.arange(9), [3, 3]) grid0 = gridpp.Grid(lats0, lons0, elevs0) gradient = 1 output = gridpp.simple_gradient(grid, grid0, values, gradient) expected = [[14, 15, 2], [17, 18, 5], [6, 7, 1]] expected = [expected, expected] np.testing.assert_array_almost_equal(output, expected)
def test_missing_values(self): values = np.zeros([3, 3]) values[1, 1] = np.nan gradient = 1 output = gridpp.simple_gradient(grid, points, values, gradient) np.testing.assert_array_almost_equal(output, [5, np.nan])
index_control = 0 blats_2500m = file.variables['latitude'][:] blons_2500m = file.variables['longitude'][:] belevs_2500m = file.variables['surface_geopotential'][0, 0, index_control, :, :] / 9.81 bgrid_2500m = gridpp.Grid(blats_2500m, blons_2500m, belevs_2500m) background_2500m = file.variables['air_temperature_2m'][0, 0, index_control, :, :] with netCDF4.Dataset('template.nc', 'r') as file: blats = file.variables['latitude'][:] blons = file.variables['longitude'][:] belevs = file.variables['altitude'][:] bgrid = gridpp.Grid(blats, blons, belevs) # Downscaling gradient = -0.0065 background = gridpp.simple_gradient(bgrid_2500m, bgrid, background_2500m, gradient) points = gridpp.Points(obs_lats[index_valid_obs], obs_lons[index_valid_obs], obs_elevs[index_valid_obs]) pbackground = gridpp.bilinear(bgrid, points, background) variance_ratios = np.full(points.size(), 0.1) h = 100000 v = 200 structure = gridpp.BarnesStructure(h, v) max_points = 50 analysis = gridpp.optimal_interpolation(bgrid, background, points, obs[index_valid_obs], variance_ratios, pbackground, structure, max_points)