コード例 #1
0
 def test_extrapolation(self):
     """ Check that extrapolation filter works """
     N = 5
     y = np.linspace(0, 1000, N)
     x = np.zeros(N)
     grid = gridpp.Points(y, x, x, x, gridpp.Cartesian)
     points = gridpp.Points([0, 100, 900, 1000], [0, 0, 0, 0], [0, 0, 0, 0],
                            [0, 0, 0, 0], gridpp.Cartesian)
     Y = points.size()
     pratios = 0.1 * np.ones(Y)
     structure = gridpp.BarnesStructure(500)
     pobs = [0, 1, 1, 0]
     background = np.zeros(grid.size())
     pbackground = np.zeros(Y)
     max_points = 10
     output0 = gridpp.optimal_interpolation(grid, background, points, pobs,
                                            pratios, pbackground, structure,
                                            max_points, False)
     output1 = gridpp.optimal_interpolation(grid, background, points, pobs,
                                            pratios, pbackground, structure,
                                            max_points, True)
     # Turning off extrapolation should mean we don't get increments greater than 1
     self.assertTrue(np.max(output0) == 1)
     self.assertTrue(np.max(output1) > 1)
     I = np.where(output1 < 1)[0]
     np.testing.assert_array_almost_equal(output0[I], output1[I])
コード例 #2
0
 def test_points_to_points(self):
     """Check that point to point interpolation works"""
     ipoints = gridpp.Points([0, 5, 10], [0, 5, 10])
     points = gridpp.Points([-1, 6], [-1, 6])
     values = [0, 1, 2]
     output = gridpp.nearest(ipoints, points, values)
     np.testing.assert_array_equal(output, [0, 1])
コード例 #3
0
    def test_invalid_arguments(self):
        """ Check that exception is thrown on invalid input values """

        # Set up struct with valid input arguments
        ok_args = collections.OrderedDict({
            'grid':
            gridpp.Grid([[0, 0, 0]], [[0, 2500, 10000]], [[0, 0, 0]],
                        [[0, 0, 0]], gridpp.Cartesian),
            'background':
            np.zeros([1, 3]),
            'points':
            gridpp.Points([0], [2500], [0], [0], gridpp.Cartesian),
            'pobs': [1],
            'pratios': [0.1],
            'pbackground': [0],
            'structure':
            gridpp.BarnesStructure(2500),
            'max_points':
            10
        })

        # Set up struct with invalid input arguments that will be substituted into ok_args one at a
        # time in order to look for an exception being raised. Use an array of different invalid
        # arguments for each key.
        x = np.zeros([3, 2])
        invalid_args = {
            # Grid size mismatch, and coordinate-type mismatch
            'grid': [
                gridpp.Grid(x, x, x, x, gridpp.Cartesian),
                gridpp.Grid([[0, 0, 0]], [[0, 2500, 10000]])
            ],
            # Points size mismatch, and coordinate-type mismatch
            'points': [
                gridpp.Points([0, 1], [0, 2500], [0, 0], [0, 0],
                              gridpp.Cartesian),
                gridpp.Points([0], [2500])
            ],
            'pratios': [np.zeros(11)],
            'pobs': [np.zeros([11])],
            'background': [np.zeros([2, 11])],
            'pbackground': [np.zeros(21)],
            'max_points': [-1]
        }

        for key in invalid_args.keys():
            for arg in invalid_args[key]:
                args0 = ok_args.copy()
                args0[key] = arg
                q = [args0[f] for f in args0]
                with self.subTest(key=key, arg=arg):
                    with self.assertRaises(ValueError) as e:
                        output = gridpp.optimal_interpolation(*q)
コード例 #4
0
 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])
コード例 #5
0
 def test_point_to_grid_cartesian(self):
     lons, lats = np.meshgrid([0, 1000, 2000], [0, 1000])
     grid = gridpp.Grid(lats, lons, 0 * lats, 0 * lats, gridpp.Cartesian)
     points = gridpp.Points([0, 0], [0, 600], [0, 0], [0, 0],
                            gridpp.Cartesian)
     np.testing.assert_array_almost_equal(
         gridpp.distance(points, grid, 1),
         [[0, 400, 1400],
          [1000,
           np.sqrt(1000**2 + 400**2),
           np.sqrt(1000**2 + 1400**2)]], 4)
     np.testing.assert_array_almost_equal(gridpp.distance(points, grid, 2),
                                          [[600, 1000, 2000],
                                           [
                                               np.sqrt(1000**2 + 600**2),
                                               np.sqrt(2) * 1000,
                                               np.sqrt(1000**2 + 2000**2)
                                           ]], 4)
     np.testing.assert_array_almost_equal(gridpp.distance(points, grid, 10),
                                          [[600, 1000, 2000],
                                           [
                                               np.sqrt(1000**2 + 600**2),
                                               np.sqrt(2) * 1000,
                                               np.sqrt(1000**2 + 2000**2)
                                           ]], 4)
コード例 #6
0
    def __init__(self, operator, geo, dataset, grid_values, max_distance=5000):

        grid_lons = np.asarray(geo.lons)
        grid_lats = np.asarray(geo.lats)
        grid = gridpp.Grid(grid_lats, grid_lons)
        lons = dataset.lons
        lats = dataset.lats
        points = gridpp.Points(lats, lons)

        if operator == "nearest":
            obs_values = gridpp.nearest(grid, points, grid_values)
        elif operator == "bilinear":
            obs_values = gridpp.bilinear(grid, points, grid_values)
        else:
            raise NotImplementedError(operator)

        inside_grid = []
        # Check if they are in grid
        for i in range(0, len(obs_values)):
            lon = lons[i]
            lat = lats[i]
            nn = grid.get_num_neighbours(lat, lon, max_distance)
            # print(i, lons[i], lats[i], obs_values[i], nn)
            if nn == 0:
                inside_grid.append(False)
            else:
                inside_grid.append(True)

        self.inside_grid = inside_grid
        self.obs_values = obs_values
コード例 #7
0
 def test_attributes(self):
     """Test that lat, lons, etc are set"""
     points = gridpp.Points([0, 1], [2, 3], [4, 5], [0.1, 0.2])
     np.testing.assert_array_almost_equal(points.get_lats(), [0, 1])
     np.testing.assert_array_almost_equal(points.get_lons(), [2, 3])
     np.testing.assert_array_almost_equal(points.get_elevs(), [4, 5])
     np.testing.assert_array_almost_equal(points.get_lafs(), [0.1, 0.2])
コード例 #8
0
 def test_get_nearest_neighbour(self):
     points = gridpp.Points([0, 1000, 2000], [0, 1000, 2000], [0, 0, 0],
                            [0, 0, 0], gridpp.Cartesian)
     self.assertEqual(points.get_nearest_neighbour(-100, -100), 0)
     self.assertEqual(points.get_nearest_neighbour(0, 0), 0)
     self.assertEqual(points.get_nearest_neighbour(900, 900), 1)
     self.assertEqual(points.get_nearest_neighbour(2100, 2100), 2)
コード例 #9
0
 def test_get_neighbours_with_distance(self):
     points = gridpp.Points([0, 1000, 2000], [0, 1000, 2000], [0, 0, 0],
                            [0, 0, 0], gridpp.Cartesian)
     indices, distances = points.get_neighbours_with_distance(0, 0, 1500)
     self.assertEqual(len(indices), 2)
     np.testing.assert_array_almost_equal(distances,
                                          [0, np.sqrt(2) * 1000], 4)
コード例 #10
0
 def test_grid_to_points_3d(self):
     """Check that grid to point interpolation for 3D fields works"""
     lons, lats = np.meshgrid([0, 10, 20], [0, 10, 20])
     grid = gridpp.Grid(lats, lons)
     points = gridpp.Points([-1, 6], [-1, 6])
     values = np.reshape(range(18), [2, lons.shape[0], lons.shape[1]])
     output = gridpp.nearest(grid, points, values)
     np.testing.assert_array_equal(output, [[0, 4], [9, 13]])
コード例 #11
0
 def test_get_nearest_neighbour_no_match(self):
     """Check that an exact match is removed"""
     points = gridpp.Points([0, 1, 2, 2, 4], [0] * 5)
     self.assertAlmostEqual(points.get_nearest_neighbour(0, 0, False), 1)
     self.assertAlmostEqual(points.get_nearest_neighbour(0, 0, True), 0)
     # Check that multiple of identical matches are removed
     self.assertEqual(points.get_nearest_neighbour(2, 0, False), 1)
     self.assertTrue(points.get_nearest_neighbour(2, 0, True) in [2, 3])
コード例 #12
0
 def test_one_row(self):
     gridpp.set_omp_threads(1)
     lons, lats = np.meshgrid([0], [30, 40, 50])
     grid = gridpp.Grid(lats, lons)
     values = np.zeros(lons.shape)
     values[:] = np.reshape(range(3), lons.shape)
     points = gridpp.Points([25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55], [-1, -1, -1, 0, 0, 0, 10, 10, 10, 20, 20, 20, 21, 21, 21])
     output = gridpp.nearest(grid, points, values)
     np.testing.assert_array_equal(output, (0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2))
コード例 #13
0
def make_points_from_grb(grb_data, valid_points):

    latlats, lonlons = np.meshgrid(
        grb_data.latitude.data, grb_data.longitude.data
    )

    grid_object = gridpp.Points(latlats[valid_points], lonlons[valid_points])

    return grid_object
コード例 #14
0
 def test_get_neighbours(self):
     points = gridpp.Points([0, 1000, 2000], [0, 1000, 2000], [0, 0, 0],
                            [0, 0, 0], gridpp.Cartesian)
     np.testing.assert_array_almost_equal(
         np.sort(points.get_neighbours(0, 0, 1500)), [0, 1])
     np.testing.assert_array_almost_equal(
         np.sort(points.get_neighbours(900, 900, 1600)), [0, 1, 2])
     np.testing.assert_array_almost_equal(
         np.sort(points.get_neighbours(-100, -100, 100)), [])
コード例 #15
0
def get_points(coordinate_type):
    lats = np.array([0, 1, 2])
    lons = np.array([0, 0, 0])
    if coordinate_type == gridpp.Geodetic:
        factor = 1
    else:
        factor = 111000
    return gridpp.Points(lats * factor, lons * factor, lons * 0, lons * 0,
                         coordinate_type)
コード例 #16
0
 def test_grid_to_point_geodetic(self):
     lons, lats = np.meshgrid([0, 1, 2], [0, 1])
     grid = gridpp.Grid(lats, lons)
     points = gridpp.Points([0, 0], [0, 0.6])
     np.testing.assert_array_almost_equal(gridpp.distance(grid, points, 1),
                                          [0, 44528], 0)
     np.testing.assert_array_almost_equal(gridpp.distance(grid, points, 2),
                                          [111319.49, 66791.7], 0)
     np.testing.assert_array_almost_equal(gridpp.distance(grid, points, 10),
                                          [248907.83, 191514.84], 0)
コード例 #17
0
    def test_missing_values(self):
        """Check that missing values are not used in OI"""
        obs = np.array([1, np.nan, 2, 3, np.nan, np.nan, 4, np.nan])
        N = len(obs)
        y = np.arange(0, N * 1000, 1000)
        background = np.zeros(N)
        points = gridpp.Points(y, np.zeros(N), np.zeros(N), np.zeros(N),
                               gridpp.Cartesian)
        ratios = np.ones(N)
        structure = gridpp.BarnesStructure(1000, 0)
        analysis = gridpp.optimal_interpolation(points, background, points,
                                                obs, ratios, background,
                                                structure, 100)

        I = np.where(np.isnan(y) == 0)[0]
        points1 = gridpp.Points(y[I], np.zeros(len(I)), np.zeros(len(I)),
                                np.zeros(len(I)), gridpp.Cartesian)
        analysis1 = gridpp.optimal_interpolation(points, background, points1,
                                                 obs[I], ratios[I],
                                                 background[I], structure, 100)
        np.testing.assert_array_almost_equal(analysis, analysis1)
コード例 #18
0
    def test_cross_validation(self):
        y = np.array([0, 1000, 2000, 3000])
        N = len(y)
        obs = np.array([0, 1, 2, 3])
        background = np.zeros(N)
        points = gridpp.Points(y, np.zeros(N), np.zeros(N), np.zeros(N),
                               gridpp.Cartesian)
        ratios = np.ones(N)
        Icv = [0, 2, 3]
        points_cv = gridpp.Points(y[Icv], np.zeros(N - 1), np.zeros(N - 1),
                                  np.zeros(N - 1), gridpp.Cartesian)
        structure = gridpp.BarnesStructure(1000, 0)
        structure_cv = gridpp.CrossValidation(structure, 750)

        analysis = gridpp.optimal_interpolation(points, background, points_cv,
                                                obs[Icv], ratios[Icv],
                                                background[Icv], structure,
                                                100)
        analysis_cv = gridpp.optimal_interpolation(points, background, points,
                                                   obs, ratios, background,
                                                   structure_cv, 100)
コード例 #19
0
 def test_empty(self):
     points = gridpp.Points()
     self.assertEqual(points.get_nearest_neighbour(0, 0), -1)
     np.testing.assert_array_equal(points.get_closest_neighbours(0, 0, 5),
                                   [])
     np.testing.assert_array_equal(points.get_neighbours(0, 0, 1000), [])
     self.assertEqual(points.get_num_neighbours(0, 0, 1000), 0)
     np.testing.assert_array_equal(points.get_lats(), [])
     np.testing.assert_array_equal(points.get_lons(), [])
     np.testing.assert_array_equal(points.get_elevs(), [])
     np.testing.assert_array_equal(points.get_lafs(), [])
     self.assertEqual(points.size(), 0)
コード例 #20
0
 def test_copy_constructor(self):
     lats = [0, 1, 2]
     lons = [3, 4, 5]
     elevs = [6, 7, 8]
     lafs = [0.1, 0.2, 0.3]
     points = gridpp.Points(lats, lons, elevs, lafs)
     points2 = points
     for p in [points, points2]:
         np.testing.assert_array_almost_equal(p.get_lats(), lats)
         np.testing.assert_array_almost_equal(p.get_lons(), lons)
         np.testing.assert_array_almost_equal(p.get_elevs(), elevs)
         np.testing.assert_array_almost_equal(p.get_lafs(), lafs)
コード例 #21
0
    def test_cross_validation_grid(self):
        """ Check that the CV structure function works """
        np.random.seed(1000)
        y, x = np.meshgrid(np.arange(0, 3500, 500), np.arange(0, 3500, 500))
        Y = y.shape[0]
        X = y.shape[1]
        grid = gridpp.Grid(y, x, np.zeros(x.shape), np.zeros(x.shape),
                           gridpp.Cartesian)
        background = np.random.rand(Y, X) * 0

        obs = np.array([10, 20, 30])
        x_o = np.array([1000, 2000, 3000])
        y_o = np.array([1000, 2000, 3000])
        N = len(obs)
        points = gridpp.Points(y_o, x_o, np.zeros(N), np.zeros(N),
                               gridpp.Cartesian)

        background_o = gridpp.nearest(grid, points, background)

        ratios = np.ones(N)
        k = 0
        ii = np.arange(N).astype('int') != k
        points_cv = gridpp.Points(y_o[ii], x_o[ii], np.zeros(N - 1),
                                  np.zeros(N - 1), gridpp.Cartesian)
        structure = gridpp.BarnesStructure(1000, 0)
        structure_cv = gridpp.CrossValidation(structure, 750)

        analysis = gridpp.optimal_interpolation(grid, background, points_cv,
                                                obs[ii], ratios[ii],
                                                background_o[ii], structure,
                                                100)
        analysis_cv = gridpp.optimal_interpolation(points, background_o,
                                                   points, obs, ratios,
                                                   background_o, structure_cv,
                                                   100)

        self.assertAlmostEqual(
            gridpp.nearest(grid, points, analysis)[k], analysis_cv[k])
コード例 #22
0
    def test_1(self):
        lons, lats = np.meshgrid([0, 1, 2, 3, 4], [0, 1, 2, 3, 4])
        grid = gridpp.Grid(lats * 1000, lons * 1000, np.zeros([5, 5]), np.zeros([5, 5]), gridpp.Cartesian)
        points = gridpp.Points([0, 0, 3000], [0, 3000, 3000], [0, 0, 0], [0, 0, 0], gridpp.Cartesian)
        values = np.zeros([5, 5])
        radii = [1010, 10, 2010]
        value = 1
        outside = False
        output = gridpp.fill(grid, values, points, radii, value, outside)
        np.testing.assert_array_almost_equal(output, [[1, 1, 0, 1, 0], [1, 0, 0, 1, 0],[0, 0, 1, 1, 1], [0, 1, 1, 1, 1], [0, 0, 1, 1, 1]])

        outside = True
        output = gridpp.fill(grid, values, points, radii, value, outside)
        np.testing.assert_array_almost_equal(output, [[0, 0, 1, 0, 1], [0, 1, 1, 0, 1],[1, 1, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0]])
コード例 #23
0
 def test_radius_match(self):
     """Check that points right on the radius edge count as a match"""
     points = gridpp.Points([0, 1000, 2000], [0, 0, 0], [0, 0, 0],
                            [0, 0, 0], gridpp.Cartesian)
     I = points.get_neighbours(900, 0, 501)
     np.testing.assert_array_equal(I, [1])
     I = points.get_neighbours(900, 0, 99.99)
     np.testing.assert_array_equal(I, [])
     I = points.get_neighbours(0, 0, 1000)
     np.testing.assert_array_equal(I, [0])
     I = points.get_neighbours(0, 0, 1001)
     np.testing.assert_array_equal(I, [0, 1])
     I = points.get_neighbours(0, 0, 1001, False)
     np.testing.assert_array_equal(I, [1])
コード例 #24
0
 def test_neighbourhood(self):
     """Simple check
      2  X
      1  X
      0  X
         0  1
     """
     for num_treads in [1, 2]:
         gridpp.set_omp_threads(num_treads)
         lons, lats = np.meshgrid([0, 1], [0, 1, 2])
         grid = gridpp.Grid(lats, lons)
         points = gridpp.Points([0, 1, 2], [0, 0, 0])
         radius = 120000
         output = gridpp.count(points, grid, radius)
         np.testing.assert_array_equal(output, ((2,1), (3,1), (2,1)))
コード例 #25
0
ファイル: bilinear_test.py プロジェクト: met-ie/gridpp
 def test_neighbourhood(self):
     """Simple check
     50  6  7  8
     40  3  4  5
     30  0  1  2
         0 10 20
     """
     lons, lats = np.meshgrid([0, 10, 20], [30, 40, 50])
     grid = gridpp.Grid(lats, lons)
     values = np.zeros(lons.shape)
     values[:] = np.reshape(range(9), lons.shape)
     points = gridpp.Points([25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55], [-1, -1, -1, 0, 0, 0, 10, 10, 10, 20, 20, 20, 21, 21, 21])
     output = gridpp.nearest(grid, points, values)
     np.testing.assert_array_equal(output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))
     output = gridpp.bilinear(grid, points, values)
     np.testing.assert_array_equal(output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))
コード例 #26
0
    def test_dimension_mismatch(self):
        lons, lats = np.meshgrid([0, 10, 20], [30, 40, 50])
        grid = gridpp.Grid(lats, lons)
        points = gridpp.Points([0, 1], [0, 1])
        values = np.zeros([3, 2])
        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, grid, values)

        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, points, values)

        values3 = np.zeros([3, 2, 3])
        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, grid, values3)

        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, points, values3)
コード例 #27
0
    def test_get_in_domain_indices(self):
        lons, lats = np.meshgrid([0, 1, 2], [0, 1, 2])
        grid = gridpp.Grid(lats, lons)
        # 4 corners, 4 edges, inside, outside each edge
        lats = np.array([0, 0, 2, 2, 0, 1, 1, 2, 1, -1, 1, 1, 3])
        lons = np.array([0, 2, 0, 2, 1, 0, 2, 1, 1, 1, -1, 3, 1])
        N = len(lats)
        points = gridpp.Points(lats, lons)
        indices = points.get_in_domain_indices(grid)
        Iinside = [0, 1, 2, 3, 4, 5, 6, 7, 8]
        np.testing.assert_array_equal(indices, Iinside)

        points1 = points.get_in_domain(grid)
        np.testing.assert_array_equal(np.sort(points1.get_lats()),
                                      np.sort(lats[Iinside]))
        np.testing.assert_array_equal(np.sort(points1.get_lons()),
                                      np.sort(lons[Iinside]))
コード例 #28
0
 def test_simple_1d(self):
     N = 3
     y = [[0, 0, 0]]
     x = [[0, 2500, 10000]]
     grid = gridpp.Grid(y, x, y, y, gridpp.Cartesian)
     points = gridpp.Points([0], [2500], [0], [0], gridpp.Cartesian)
     pratios = [0.1]
     structure = gridpp.BarnesStructure(2500)
     pobs = [1]
     background = np.zeros([1, N])
     pbackground = [0]
     max_points = 10
     output = gridpp.optimal_interpolation(grid, background, points, pobs,
                                           pratios, pbackground, structure,
                                           max_points)
     np.testing.assert_array_almost_equal(
         output,
         np.array([[np.exp(-0.5) / 1.1, 1 / 1.1,
                    np.exp(-0.5 * 9) / 1.1]]))
コード例 #29
0
 def test_simple_1d_full(self):
     N = 3
     y = [[0, 0, 0]]
     x = [[0, 2500, 10000]]
     grid = gridpp.Grid(y, x, y, y, gridpp.Cartesian)
     points = gridpp.Points([0], [2500], [0], [0], gridpp.Cartesian)
     bvariance = np.ones([1, N])
     obs_variance = [0.1]
     bvariance_at_points = [1]
     structure = gridpp.BarnesStructure(2500)
     pobs = [1]
     background = np.zeros([1, N])
     background_at_points = [0]
     max_points = 10
     output, sigma = gridpp.optimal_interpolation_full(
         grid, background, bvariance, points, pobs, obs_variance,
         background_at_points, bvariance_at_points, structure, max_points)
     # np.testing.assert_array_almost_equal(output, np.array([[np.exp(-0.5)/1.1, 1/1.1, np.exp(-0.5*9)/1.1]]))
     # np.testing.assert_array_almost_equal(sigma, np.array([[0, np.sqrt(0.1/1.1), 1]]))
     self.assertAlmostEqual(sigma[0, 1], 0.1 / 1.1)
コード例 #30
0
 def test_grid_to_point(self):
     """Check that grid to point interpolation works
     50  6  7  8
     40  3  4  5
     30  0  1  2
         0 10 20
     """
     for num_treads in [1, 2]:
         gridpp.set_omp_threads(num_treads)
         lons, lats = np.meshgrid([0, 10, 20], [30, 40, 50])
         grid = gridpp.Grid(lats, lons)
         values = np.reshape(range(9), lons.shape)
         points = gridpp.Points(
             [25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55],
             [-1, -1, -1, 0, 0, 0, 10, 10, 10, 20, 20, 20, 21, 21, 21])
         output = gridpp.nearest(grid, points, values)
         np.testing.assert_array_equal(
             output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))
         output = gridpp.bilinear(grid, points, values)
         np.testing.assert_array_equal(
             output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))