コード例 #1
0
 def test_sample_dictionary(self):
     # Pass sample_point arg as a dictionary: this usage mode is deprecated.
     co_x = AuxCoord([1.0, 2.0, 3.0], long_name="x")
     co_y = AuxCoord([10.0, 20.0], long_name="y")
     cube = Cube(np.zeros((2, 3)))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 1)
     sample_point = {"x": 2.8, "y": 18.5}
     exp_emsg = r"must be a list of \(coordinate, value\) pairs"
     with self.assertRaisesRegex(TypeError, exp_emsg):
         nn_ndinds(cube, sample_point)
コード例 #2
0
 def test_sample_dictionary(self):
     # Pass sample_point arg as a dictionary: this usage mode is deprecated.
     co_x = AuxCoord([1.0, 2.0, 3.0], long_name='x')
     co_y = AuxCoord([10.0, 20.0], long_name='y')
     cube = Cube(np.zeros((2, 3)))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 1)
     sample_point = {'x': 2.8, 'y': 18.5}
     exp_emsg = 'must be a list of \(coordinate, value\) pairs'
     with self.assertRaisesRegexp(TypeError, exp_emsg):
         nn_ndinds(cube, sample_point)
コード例 #3
0
 def test_nonlatlon_simple_1d(self):
     co_x = AuxCoord([1.0, 2.0, 3.0, 1.0, 2.0, 3.0], long_name='x')
     co_y = AuxCoord([10.0, 10.0, 10.0, 20.0, 20.0, 20.0], long_name='y')
     cube = Cube(np.zeros(6))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     sample_point = [('x', 2.8), ('y', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(5,)])
コード例 #4
0
 def test_nonlatlon_simple_2d(self):
     co_y = DimCoord([10.0, 20.0], long_name="y")
     co_x = DimCoord([1.0, 2.0, 3.0], long_name="x")
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_point = [("x", 2.8), ("y", 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1, 2)])
コード例 #5
0
 def test_alternate_nonlatlon_names_different(self):
     # Check that (88, 25) is **NOT** closer to (88, 0) than to (87, 25)
     # ... by plain XY euclidean-distance, if coords have non-latlon names.
     cube = self._testcube_latlon_1d(lats=[88, 87], lons=[0, 25])
     cube.coord("latitude").rename("y")
     cube.coord("longitude").rename("x")
     sample_point = [("y", 88), ("x", 25)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1, )])
コード例 #6
0
 def test_nonlatlon_simple_2d(self):
     co_y = DimCoord([10.0, 20.0], long_name='y')
     co_x = DimCoord([1.0, 2.0, 3.0], long_name='x')
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_point = [('x', 2.8), ('y', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1, 2)])
コード例 #7
0
 def test_nonlatlon_multiple_2d(self):
     co_y = DimCoord([10.0, 20.0], long_name='y')
     co_x = DimCoord([1.0, 2.0, 3.0], long_name='x')
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_points = [('x', [2.8, -350.0, 1.7]), ('y', [18.5, 8.7, 12.2])]
     result = nn_ndinds(cube, sample_points)
     self.assertEqual(result, [(1, 2), (0, 0), (0, 1)])
コード例 #8
0
 def test_nonlatlon_multiple_2d(self):
     co_y = DimCoord([10.0, 20.0], long_name="y")
     co_x = DimCoord([1.0, 2.0, 3.0], long_name="x")
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_points = [("x", [2.8, -350.0, 1.7]), ("y", [18.5, 8.7, 12.2])]
     result = nn_ndinds(cube, sample_points)
     self.assertEqual(result, [(1, 2), (0, 0), (0, 1)])
コード例 #9
0
 def test_alternate_latlon_names_okay(self):
     # Check that (88, 25) is **STILL** closer to (88, 0) than to (87, 25)
     # ... when coords have odd, but still recognisable, latlon names.
     cube = self._testcube_latlon_1d(lats=[88, 87], lons=[0, 25])
     cube.coord("latitude").rename("y_latitude_y")
     cube.coord("longitude").rename("x_longitude_x")
     sample_point = [("y_latitude_y", 88), ("x_longitude_x", 25)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(0, )])
コード例 #10
0
 def test_nonlatlon_simple_1d(self):
     co_x = AuxCoord([1.0, 2.0, 3.0, 1.0, 2.0, 3.0], long_name="x")
     co_y = AuxCoord([10.0, 10.0, 10.0, 20.0, 20.0, 20.0], long_name="y")
     cube = Cube(np.zeros(6))
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     sample_point = [("x", 2.8), ("y", 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(5, )])
コード例 #11
0
 def test_no_x_dim(self):
     # Operate in Y only, returned slice should be [iy, :].
     co_x = DimCoord([1.0, 2.0, 3.0], long_name="x")
     co_y = DimCoord([10.0, 20.0], long_name="y")
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_point = [("y", 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1, slice(None))])
コード例 #12
0
 def test_no_y_dim(self):
     # Operate in X only, returned slice should be [:, ix].
     co_x = DimCoord([1.0, 2.0, 3.0], long_name='x')
     co_y = DimCoord([10.0, 20.0], long_name='y')
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_point = [('x', 2.8)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(slice(None), 2)])
コード例 #13
0
 def test_alternate_nonlatlon_names_different(self):
     # Check that (88, 25) is **NOT** closer to (88, 0) than to (87, 25)
     # ... by plain XY euclidean-distance, if coords have non-latlon names.
     cube = self._testcube_latlon_1d(lats=[88, 87],
                                     lons=[0, 25])
     cube.coord('latitude').rename('y')
     cube.coord('longitude').rename('x')
     sample_point = [('y', 88), ('x', 25)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1,)])
コード例 #14
0
 def test_alternate_latlon_names_okay(self):
     # Check that (88, 25) is **STILL** closer to (88, 0) than to (87, 25)
     # ... when coords have odd, but still recognisable, latlon names.
     cube = self._testcube_latlon_1d(lats=[88, 87],
                                     lons=[0, 25])
     cube.coord('latitude').rename('y_latitude_y')
     cube.coord('longitude').rename('x_longitude_x')
     sample_point = [('y_latitude_y', 88), ('x_longitude_x', 25)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(0,)])
コード例 #15
0
 def test_no_x_dim(self):
     # Operate in Y only, returned slice should be [iy, :].
     co_x = DimCoord([1.0, 2.0, 3.0], long_name='x')
     co_y = DimCoord([10.0, 20.0], long_name='y')
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_point = [('y', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1, slice(None))])
コード例 #16
0
 def test_latlon_simple_1d(self):
     cube = Cube([11.0, 12.0, 13.0, 21.0, 22.0, 23.0])
     co_x = AuxCoord([1.0, 2.0, 3.0, 1.0, 2.0, 3.0],
                     standard_name='longitude', units='degrees')
     co_y = AuxCoord([10.0, 10.0, 10.0, 20.0, 20.0, 20.0],
                     standard_name='latitude', units='degrees')
     cube.add_aux_coord(co_y, 0)
     cube.add_aux_coord(co_x, 0)
     sample_point = [('longitude', 2.8), ('latitude', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(5,)])
コード例 #17
0
 def test_latlon_simple_2d(self):
     co_y = DimCoord([10.0, 20.0],
                     standard_name='latitude', units='degrees')
     co_x = DimCoord([1.0, 2.0, 3.0],
                     standard_name='longitude', units='degrees')
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_point = [('longitude', 2.8), ('latitude', 18.5)]
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(1, 2)])
コード例 #18
0
 def test_latlon_multiple_2d(self):
     co_y = DimCoord([10.0, 20.0],
                     standard_name='latitude', units='degrees')
     co_x = DimCoord([1.0, 2.0, 3.0],
                     standard_name='longitude', units='degrees')
     cube = Cube(np.zeros((2, 3)))
     cube.add_dim_coord(co_y, 0)
     cube.add_dim_coord(co_x, 1)
     sample_points = [('longitude', [2.8, -350.0, 1.7]),
                      ('latitude', [18.5, 8.7, 12.2])]
     result = nn_ndinds(cube, sample_points)
     # Note slight difference from non-latlon version.
     self.assertEqual(result, [(1, 2), (0, 2), (0, 1)])
コード例 #19
0
 def _check_latlon_1d(self, lats, lons, sample_point, expect):
     cube = self._testcube_latlon_1d(lats, lons)
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(expect,)])
コード例 #20
0
 def _check_latlon_1d(self, lats, lons, sample_point, expect):
     cube = self._testcube_latlon_1d(lats, lons)
     result = nn_ndinds(cube, sample_point)
     self.assertEqual(result, [(expect, )])