Ejemplo n.º 1
0
def nearest_neighbour_indices(cube, sample_points):
    msg = (_INTERPOLATE_DEPRECATION_WARNING + '\n' +
           'Please replace usage of '
           'iris.analysis.interpolate.nearest_neighbour_indices() '
           'with iris.coords.Coord.nearest_neighbour_index()).')
    _warn_deprecated(msg)
    return oldinterp.nearest_neighbour_indices(cube, sample_points)
Ejemplo n.º 2
0
    def test_nearest_neighbour_slice(self):
        point_spec = [('latitude', 40)]
        indices = iintrp.nearest_neighbour_indices(self.cube, point_spec)
        self.assertEqual(indices, (20, slice(None, None)))

        b = iintrp.extract_nearest_neighbour(self.cube, point_spec) 
        self.assertCML(b, ('analysis', 'interpolation', 'nearest_neighbour_extract_latitude.cml'))
        
        # cannot get a specific point from these point specifications
        self.assertRaises(ValueError, iintrp.nearest_neighbour_data_value, self.cube, point_spec)
Ejemplo n.º 3
0
    def test_nearest_neighbour_slice(self):
        point_spec = [('latitude', 40)]
        indices = iintrp.nearest_neighbour_indices(self.cube, point_spec)
        self.assertEqual(indices, (20, slice(None, None)))

        b = iintrp.extract_nearest_neighbour(self.cube, point_spec) 
        self.assertCML(b, ('analysis', 'interpolation', 'nearest_neighbour_extract_latitude.cml'))
        
        # cannot get a specific point from these point specifications
        self.assertRaises(ValueError, iintrp.nearest_neighbour_data_value, self.cube, point_spec)
Ejemplo n.º 4
0
 def test_nearest_neighbour_over_specification_which_is_consistent(self):
     # latitude 40 is the 20th point
     point_spec = [('latitude', 40), ('i', 20), ('longitude', 38)]
     
     indices = iintrp.nearest_neighbour_indices(self.cube, point_spec)
     self.assertEqual(indices, (20, 10))
     
     b = iintrp.extract_nearest_neighbour(self.cube, point_spec) 
     self.assertCML(b, ('analysis', 'interpolation', 'nearest_neighbour_extract_latitude_longitude.cml'))
     
     value = iintrp.nearest_neighbour_data_value(self.cube, point_spec)
     # Check that the value back is that which was returned by the extract method
     self.assertEqual(value, b.data)
Ejemplo n.º 5
0
 def test_nearest_neighbour_over_specification_which_is_consistent(self):
     # latitude 40 is the 20th point
     point_spec = [('latitude', 40), ('i', 20), ('longitude', 38)]
     
     indices = iintrp.nearest_neighbour_indices(self.cube, point_spec)
     self.assertEqual(indices, (20, 10))
     
     b = iintrp.extract_nearest_neighbour(self.cube, point_spec) 
     self.assertCML(b, ('analysis', 'interpolation', 'nearest_neighbour_extract_latitude_longitude.cml'))
     
     value = iintrp.nearest_neighbour_data_value(self.cube, point_spec)
     # Check that the value back is that which was returned by the extract method
     self.assertEqual(value, b.data)
Ejemplo n.º 6
0
    def test_nearest_neighbour(self):
        point_spec = [('latitude', 40), ('longitude', 39)]
        
        indices = iintrp.nearest_neighbour_indices(self.cube, point_spec)
        self.assertEqual(indices, (20, 10))
        
        b = iintrp.extract_nearest_neighbour(self.cube, point_spec) 

        # Check that the data has not been loaded on either the original cube,
        # nor the interpolated one.
        self.assertTrue(b.has_lazy_data())
        self.assertTrue(self.cube.has_lazy_data())
        self.assertCML(b, ('analysis', 'interpolation', 'nearest_neighbour_extract_latitude_longitude.cml'))
        
        value = iintrp.nearest_neighbour_data_value(self.cube, point_spec)
        self.assertEqual(value, np.array(285.98785, dtype=np.float32))

        # Check that the value back is that which was returned by the extract method
        self.assertEqual(value, b.data)
Ejemplo n.º 7
0
    def test_nearest_neighbour(self):
        point_spec = [('latitude', 40), ('longitude', 39)]
        
        indices = iintrp.nearest_neighbour_indices(self.cube, point_spec)
        self.assertEqual(indices, (20, 10))
        
        b = iintrp.extract_nearest_neighbour(self.cube, point_spec) 

        # Check that the data has not been loaded on either the original cube,
        # nor the interpolated one.
        self.assertTrue(b.has_lazy_data())
        self.assertTrue(self.cube.has_lazy_data())
        self.assertCML(b, ('analysis', 'interpolation', 'nearest_neighbour_extract_latitude_longitude.cml'))
        
        value = iintrp.nearest_neighbour_data_value(self.cube, point_spec)
        self.assertEqual(value, np.array(285.98785, dtype=np.float32))

        # Check that the value back is that which was returned by the extract method
        self.assertEqual(value, b.data)
Ejemplo n.º 8
0
    def test_nearest_neighbour_circular(self):
        # test on non-circular coordinate (latitude)
        lat_vals = np.array([
            [-150.0, -90], [-97, -90], [-92, -90], [-91, -90],  [-90.1, -90],
            [-90.0, -90],  [-89.9, -90],
            [-89, -90],  [-88, -87.5],  [-87, -87.5],
            [-86, -85], [-85.5, -85],
            [81, 80], [84, 85], [84.8, 85], [85, 85], [86, 85],
            [87, 87.5], [88, 87.5], [89, 90],
            [89.9, 90], [90.0,  90], [90.1, 90],
            [95, 90], [100, 90], [150, 90]])
        lat_test_vals = lat_vals[:, 0]
        lat_expect_vals = lat_vals[:, 1]
        lat_coord_vals = self.cube.coord('latitude').points

        def near_value(val, vals):
            # return the *exact* value from vals that is closest to val.
            # - and raise an exception if there isn't a close match.
            best_val = vals[np.argmin(np.abs(vals - val))]
            if val == 0.0:
                # absolute tolerance to 0.0 (ok for magnitudes >= 1.0 or so)
                error_level = best_val
            else:
                # calculate relative-tolerance
                error_level = abs(0.5 * (val - best_val) / (val + best_val))
            self.assertTrue(error_level < 1.0e-6,
                            'error_level {}% match of {} to one of {}'.format(
                                100.0 * error_level, val, vals))
            return best_val

        lat_expect_vals = [near_value(v, lat_coord_vals)
                           for v in lat_expect_vals]
        lat_nearest_inds = [
            iintrp.nearest_neighbour_indices(
                self.cube, [('latitude', point_val)])
            for point_val in lat_test_vals]
        lat_nearest_vals = [lat_coord_vals[i[0]] for i in lat_nearest_inds]
        self.assertArrayAlmostEqual(lat_nearest_vals, lat_expect_vals)

        # repeat with *circular* coordinate (longitude)
        lon_vals = np.array([
            [0.0, 0.0],
            [-3.75, 356.25],
            [-1.0, 0], [-0.01, 0], [0.5, 0],
            [2, 3.75], [3, 3.75], [4, 3.75], [5, 3.75], [6, 7.5],
            [350.5, 348.75], [351, 352.5], [354, 352.5],
            [355, 356.25], [358, 356.25],
            [358.7, 0], [359, 0], [360, 0], [361, 0],
            [362, 3.75], [363, 3.75], [364, 3.75], [365, 3.75], [366, 7.5],
            [-725.0, 356.25], [-722, 356.25], [-721, 0], [-719, 0.0],
            [-718, 3.75],
            [1234.56, 153.75], [-1234.56, 206.25]])
        lon_test_vals = lon_vals[:, 0]
        lon_expect_vals = lon_vals[:, 1]
        lon_coord_vals = self.cube.coord('longitude').points
        lon_expect_vals = [near_value(v, lon_coord_vals)
                           for v in lon_expect_vals]
        lon_nearest_inds = [
            iintrp.nearest_neighbour_indices(self.cube,
                                             [('longitude', point_val)])
            for point_val in lon_test_vals]
        lon_nearest_vals = [lon_coord_vals[i[1]] for i in lon_nearest_inds]
        self.assertArrayAlmostEqual(lon_nearest_vals, lon_expect_vals)
Ejemplo n.º 9
0
def nearest_neighbour_indices(cube, sample_points):
    msg = (_INTERPOLATE_DEPRECATION_WARNING + '\n' + 'Please replace usage of '
           'iris.analysis.interpolate.nearest_neighbour_indices() '
           'with iris.coords.Coord.nearest_neighbour_index()).')
    _warn_deprecated(msg)
    return oldinterp.nearest_neighbour_indices(cube, sample_points)
Ejemplo n.º 10
0
    def test_nearest_neighbour_circular(self):
        # test on non-circular coordinate (latitude)
        lat_vals = np.array([
            [-150.0, -90], [-97, -90], [-92, -90], [-91, -90],  [-90.1, -90],
            [-90.0, -90],  [-89.9, -90],
            [-89, -90],  [-88, -87.5],  [-87, -87.5],
            [-86, -85], [-85.5, -85],
            [81, 80], [84, 85], [84.8, 85], [85, 85], [86, 85],
            [87, 87.5], [88, 87.5], [89, 90],
            [89.9, 90], [90.0,  90], [90.1, 90],
            [95, 90], [100, 90], [150, 90]])
        lat_test_vals = lat_vals[:, 0]
        lat_expect_vals = lat_vals[:, 1]
        lat_coord_vals = self.cube.coord('latitude').points

        def near_value(val, vals):
            # return the *exact* value from vals that is closest to val.
            # - and raise an exception if there isn't a close match.
            best_val = vals[np.argmin(np.abs(vals - val))]
            if val == 0.0:
                # absolute tolerance to 0.0 (ok for magnitudes >= 1.0 or so)
                error_level = best_val
            else:
                # calculate relative-tolerance
                error_level = abs(0.5 * (val - best_val) / (val + best_val))
            self.assertTrue(error_level < 1.0e-6,
                            'error_level {}% match of {} to one of {}'.format(
                                100.0 * error_level, val, vals))
            return best_val

        lat_expect_vals = [near_value(v, lat_coord_vals)
                           for v in lat_expect_vals]
        lat_nearest_inds = [
            iintrp.nearest_neighbour_indices(
                self.cube, [('latitude', point_val)])
            for point_val in lat_test_vals]
        lat_nearest_vals = [lat_coord_vals[i[0]] for i in lat_nearest_inds]
        self.assertArrayAlmostEqual(lat_nearest_vals, lat_expect_vals)

        # repeat with *circular* coordinate (longitude)
        lon_vals = np.array([
            [0.0, 0.0],
            [-3.75, 356.25],
            [-1.0, 0], [-0.01, 0], [0.5, 0],
            [2, 3.75], [3, 3.75], [4, 3.75], [5, 3.75], [6, 7.5],
            [350.5, 348.75], [351, 352.5], [354, 352.5],
            [355, 356.25], [358, 356.25],
            [358.7, 0], [359, 0], [360, 0], [361, 0],
            [362, 3.75], [363, 3.75], [364, 3.75], [365, 3.75], [366, 7.5],
            [-725.0, 356.25], [-722, 356.25], [-721, 0], [-719, 0.0],
            [-718, 3.75],
            [1234.56, 153.75], [-1234.56, 206.25]])
        lon_test_vals = lon_vals[:, 0]
        lon_expect_vals = lon_vals[:, 1]
        lon_coord_vals = self.cube.coord('longitude').points
        lon_expect_vals = [near_value(v, lon_coord_vals)
                           for v in lon_expect_vals]
        lon_nearest_inds = [
            iintrp.nearest_neighbour_indices(self.cube,
                                             [('longitude', point_val)])
            for point_val in lon_test_vals]
        lon_nearest_vals = [lon_coord_vals[i[1]] for i in lon_nearest_inds]
        self.assertArrayAlmostEqual(lon_nearest_vals, lon_expect_vals)