def test_cube__simple_3d(self): # Test that an 'index' coord is added to the resultant cube. cube = simple_3d() waypoints = [ { "latitude": 40, "longitude": 40 }, { "latitude": 0, "longitude": 0 }, ] sample_count = 3 new_coord_name = "index" trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory.interpolate(cube) dim_names, named_dims, anon_dims = self._result_cube_metadata(result) new_coord = result.coord(new_coord_name) exp_named_dims = [0, 1] self.assertEqual(result.ndim, cube.ndim - 1) self.assertIn(new_coord_name, dim_names) self.assertEqual(named_dims, exp_named_dims) self.assertIsNone(anon_dims) self.assertEqual(len(new_coord.points), sample_count)
def test_cube__anon_dim(self): cube = simple_4d_with_hybrid_height() cube.remove_coord('model_level_number') # Make cube dim 1 anonymous. waypoints = [{ 'grid_latitude': 21, 'grid_longitude': 31 }, { 'grid_latitude': 23, 'grid_longitude': 33 }] sample_count = 4 new_coord_name = 'index' trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory.interpolate(cube) dim_names, named_dims, anon_dims = self._result_cube_metadata(result) new_coord = result.coord(new_coord_name) exp_named_dims = [0, 2] exp_anon_dims = [1] self.assertEqual(result.ndim, cube.ndim - 1) self.assertIn(new_coord_name, dim_names) self.assertEqual(named_dims, exp_named_dims) self.assertEqual(anon_dims, exp_anon_dims) self.assertEqual(len(new_coord.points), sample_count)
def test_3d(self): dim_names = ["y", "x", "z"] waypoints = [ { dim_names[0]: 0, dim_names[1]: 0, dim_names[2]: 2 }, { dim_names[0]: 1, dim_names[1]: 2, dim_names[2]: 10 }, ] sample_count = 5 trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory._get_interp_points() self.assertEqual(len(result), len(waypoints[0])) self.assertEqual(len(result[0][1]), sample_count) self.assertEqual(len(result[1][1]), sample_count) self.assertEqual(len(result[2][1]), sample_count) self.assertIn(result[0][0], dim_names) self.assertIn(result[1][0], dim_names) self.assertIn(result[2][0], dim_names)
def test_basic(self): dim_names = 'lat' waypoints = [{dim_names: 0}, {dim_names: 1}] sample_count = 5 trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory._get_interp_points() expected_points = list(np.linspace(0, 1, sample_count)) self.assertEqual(len(result), len(waypoints[0])) self.assertEqual(len(result[0][1]), sample_count) self.assertEqual(result[0][1], expected_points) self.assertEqual(result[0][0], dim_names)
def test_basic(self): dim_names = "lat" waypoints = [{dim_names: 0}, {dim_names: 1}] sample_count = 5 trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory._get_interp_points() expected_points = list(np.linspace(0, 1, sample_count)) self.assertEqual(len(result), len(waypoints[0])) self.assertEqual(len(result[0][1]), sample_count) self.assertEqual(result[0][1], expected_points) self.assertEqual(result[0][0], dim_names)
def test_call(self): # Test that :func:`iris.analysis.trajectory.interpolate` is called by # `Trajectory.interpolate`. cube = simple_3d() to_patch = 'iris.analysis.trajectory.interpolate' waypoints = [{'latitude': 40, 'longitude': 40}, {'latitude': 0, 'longitude': 0}] sample_count = 3 trajectory = Trajectory(waypoints, sample_count=sample_count) with mock.patch(to_patch, return_value=cube) as mock_interpolate: trajectory.interpolate(cube) mock_interpolate.assert_called_once()
def test_2d(self): dim_names = ['lat', 'lon'] waypoints = [{dim_names[0]: 0, dim_names[1]: 0}, {dim_names[0]: 1, dim_names[1]: 2}] sample_count = 5 trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory._get_interp_points() self.assertEqual(len(result), len(waypoints[0])) self.assertEqual(len(result[0][1]), sample_count) self.assertEqual(len(result[1][1]), sample_count) self.assertIn(result[0][0], dim_names) self.assertIn(result[1][0], dim_names)
def test_zigzag(self): # 4-seg m-shape waypoints = [ { "lat": 0, "lon": 0 }, { "lat": 1, "lon": 1 }, { "lat": 0, "lon": 2 }, { "lat": 1, "lon": 3 }, { "lat": 0, "lon": 4 }, ] trajectory = Trajectory(waypoints, sample_count=33) self.assertEqual(trajectory.length, 5.6568542494923806) self.assertEqual(trajectory.sample_count, 33) self.assertEqual( trajectory.sampled_points[31], { "lat": 0.12499999999999989, "lon": 3.875 }, )
def test_3_points(self): # basic 2-seg line along x waypoints = [ { "lat": 0, "lon": 0 }, { "lat": 0, "lon": 1 }, { "lat": 0, "lon": 2 }, ] trajectory = Trajectory(waypoints, sample_count=21) self.assertEqual(trajectory.length, 2.0) self.assertEqual(trajectory.sample_count, 21) self.assertEqual( trajectory.sampled_points[19], { "lat": 0.0, "lon": 1.9000000000000001 }, )
def test_2_points(self): # basic 2-seg line along x waypoints = [{"lat": 0, "lon": 0}, {"lat": 1, "lon": 2}] trajectory = Trajectory(waypoints, sample_count=5) self.assertEqual(trajectory.length, np.sqrt(5)) self.assertEqual(trajectory.sample_count, 5) self.assertEqual( trajectory.sampled_points, [ { "lat": 0.0, "lon": 0.0 }, { "lat": 0.25, "lon": 0.5 }, { "lat": 0.5, "lon": 1.0 }, { "lat": 0.75, "lon": 1.5 }, { "lat": 1.0, "lon": 2.0 }, ], )
def test_trajectory_extraction_zigzag(self): # Extract a zig-zag trajectory waypoints = [ { 'grid_latitude': -0.1188, 'grid_longitude': 359.5886 }, { 'grid_latitude': -0.0828, 'grid_longitude': 359.6606 }, { 'grid_latitude': -0.0468, 'grid_longitude': 359.6246 }, ] trajectory = Trajectory(waypoints, sample_count=20) sample_points = self._traj_to_sample_points(trajectory) trajectory_cube = traj_interpolate(self.cube[0, 0], sample_points) expected = np.array([ 287.95953369, 287.9190979, 287.95550537, 287.93240356, 287.83850098, 287.87869263, 287.90942383, 287.9463501, 287.74365234, 287.68856812, 287.75588989, 287.54611206, 287.48522949, 287.53356934, 287.60217285, 287.43795776, 287.59701538, 287.52468872, 287.45025635, 287.52716064 ], dtype=np.float32) self.assertCML(trajectory_cube, ('trajectory', 'zigzag.cml'), checksum=False) self.assertArrayAllClose(trajectory_cube.data, expected, rtol=2.0e-7)
def test_zigzag(self): # 4-seg m-shape waypoints = [{ 'lat': 0, 'lon': 0 }, { 'lat': 1, 'lon': 1 }, { 'lat': 0, 'lon': 2 }, { 'lat': 1, 'lon': 3 }, { 'lat': 0, 'lon': 4 }] trajectory = Trajectory(waypoints, sample_count=33) self.assertEqual(trajectory.length, 5.6568542494923806) self.assertEqual(trajectory.sample_count, 33) self.assertEqual(trajectory.sampled_points[31], { 'lat': 0.12499999999999989, 'lon': 3.875 })
def test_cube__simple_3d(self): # Test that an 'index' coord is added to the resultant cube. cube = simple_3d() waypoints = [{'latitude': 40, 'longitude': 40}, {'latitude': 0, 'longitude': 0}] sample_count = 3 new_coord_name = 'index' trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory.interpolate(cube) dim_names, named_dims, anon_dims = self._result_cube_metadata(result) new_coord = result.coord(new_coord_name) exp_named_dims = [0, 1] self.assertEqual(result.ndim, cube.ndim - 1) self.assertIn(new_coord_name, dim_names) self.assertEqual(named_dims, exp_named_dims) self.assertIsNone(anon_dims) self.assertEqual(len(new_coord.points), sample_count)
def test_cube__anon_dim(self): cube = simple_4d_with_hybrid_height() cube.remove_coord('model_level_number') # Make cube dim 1 anonymous. waypoints = [{'grid_latitude': 21, 'grid_longitude': 31}, {'grid_latitude': 23, 'grid_longitude': 33}] sample_count = 4 new_coord_name = 'index' trajectory = Trajectory(waypoints, sample_count=sample_count) result = trajectory.interpolate(cube) dim_names, named_dims, anon_dims = self._result_cube_metadata(result) new_coord = result.coord(new_coord_name) exp_named_dims = [0, 2] exp_anon_dims = [1] self.assertEqual(result.ndim, cube.ndim - 1) self.assertIn(new_coord_name, dim_names) self.assertEqual(named_dims, exp_named_dims) self.assertEqual(anon_dims, exp_anon_dims) self.assertEqual(len(new_coord.points), sample_count)
def test_trajectory_extraction_axis_aligned(self): # Extract a simple, axis-aligned trajectory that is similar to an # indexing operation. # (It's not exactly the same because the source cube doesn't have # regular spacing.) waypoints = [ {'grid_latitude': -0.1188, 'grid_longitude': 359.57958984}, {'grid_latitude': -0.1188, 'grid_longitude': 359.66870117} ] trajectory = Trajectory(waypoints, sample_count=100) sample_points = self._traj_to_sample_points(trajectory) trajectory_cube = traj_interpolate(self.cube, sample_points) self.assertCML(trajectory_cube, ('trajectory', 'constant_latitude.cml'))