def test_lat_lon_grid_spacing(self): """Test latitude and longitude point values created around 0,0 with provided grid spacing""" y_coord, x_coord = construct_yx_coords(3, 3, "latlon", grid_spacing=1) self.assertArrayEqual(x_coord.points, [-1.0, 0.0, 1.0]) self.assertArrayEqual(y_coord.points, [-1.0, 0.0, 1.0]) y_coord, x_coord = construct_yx_coords(4, 4, "latlon", grid_spacing=1) self.assertArrayEqual(x_coord.points, [-1.5, -0.5, 0.5, 1.5]) self.assertArrayEqual(y_coord.points, [-1.5, -0.5, 0.5, 1.5])
def test_equal_area_grid_spacing(self): """Test projection_y_coordinate and projection_x_coordinate point values created around 0,0 with provided grid spacing""" y_coord, x_coord = construct_yx_coords(3, 3, "equalarea", grid_spacing=1) self.assertArrayEqual(x_coord.points, [-1.0, 0.0, 1.0]) self.assertArrayEqual(y_coord.points, [-1.0, 0.0, 1.0]) y_coord, x_coord = construct_yx_coords(4, 4, "equalarea", grid_spacing=1) self.assertArrayEqual(x_coord.points, [-1.5, -0.5, 0.5, 1.5]) self.assertArrayEqual(y_coord.points, [-1.5, -0.5, 0.5, 1.5])
def test_lat_lon_domain_corner(self): """Test grid points generated with default grid spacing if domain corner provided and grid spacing not provided""" y_coord, x_coord = construct_yx_coords(3, 3, "latlon", domain_corner=(0, 0)) self.assertArrayEqual(x_coord.points, [0.0, 10.0, 20.0]) self.assertArrayEqual(y_coord.points, [0.0, 10.0, 20.0])
def test_lat_lon_grid_spacing_domain_corner(self): """Test latitude and longitude point values start at domain corner with provided grid spacing""" y_coord, x_coord = construct_yx_coords(3, 3, "latlon", grid_spacing=2, domain_corner=(15, 12)) self.assertArrayEqual(x_coord.points, [12.0, 14.0, 16.0]) self.assertArrayEqual(y_coord.points, [15.0, 17.0, 19.0])
def test_equal_area_grid_spacing_domain_corner(self): """Test projection_y_coordinate and projection_x_coordinate point values start at domain corner with provided grid spacing""" y_coord, x_coord = construct_yx_coords(3, 3, "equalarea", grid_spacing=2, domain_corner=(15, 12)) self.assertArrayEqual(x_coord.points, [12.0, 14.0, 16.0]) self.assertArrayEqual(y_coord.points, [15.0, 17.0, 19.0])
def test_lat_lon(self): """Test coordinates created for a lat-lon grid""" y_coord, x_coord = construct_yx_coords(4, 3, "latlon") self.assertEqual(y_coord.name(), "latitude") self.assertEqual(x_coord.name(), "longitude") for crd in [y_coord, x_coord]: self.assertEqual(crd.units, "degrees") self.assertEqual(crd.dtype, np.float32) self.assertEqual(crd.coord_system, GLOBAL_GRID_CCRS) self.assertEqual(len(y_coord.points), 4) self.assertEqual(len(x_coord.points), 3)
def test_proj_xy(self): """Test coordinates created for an equal area grid""" y_coord, x_coord = construct_yx_coords(4, 3, "equalarea") self.assertEqual(y_coord.name(), "projection_y_coordinate") self.assertEqual(x_coord.name(), "projection_x_coordinate") for crd in [y_coord, x_coord]: self.assertEqual(crd.units, "metres") self.assertEqual(crd.dtype, np.float32) self.assertEqual(crd.coord_system, STANDARD_GRID_CCRS) self.assertEqual(len(y_coord.points), 4) self.assertEqual(len(x_coord.points), 3)
def emos_coefficient_fixture(): """EMOS coefficient cube (unhandled)""" y_coord, x_coord = construct_yx_coords(1, 1, "equalarea") cube = iris.cube.Cube( 0, long_name="emos_coefficient_alpha", units="K", dim_coords_and_dims=None, aux_coords_and_dims=[(y_coord, None), (x_coord, None)], ) return cube
def test_lat_lon_values(self): """Test latitude and longitude point values are as expected""" y_coord, x_coord = construct_yx_coords(3, 3, "latlon") self.assertArrayAlmostEqual(x_coord.points, [-10.0, 0.0, 10.0]) self.assertArrayAlmostEqual(y_coord.points, [-10.0, 0.0, 10.0])
def test_unknown_spatial_grid(self): """Test error raised if spatial_grid unknown""" spatial_grid = "unknown" msg = "Grid type {} not recognised".format(spatial_grid) with self.assertRaisesRegex(ValueError, msg): construct_yx_coords(3, 3, spatial_grid, domain_corner=(0, 0))
def setUp(self): """ Set up cubes for use in testing SpotLapseRateAdjust. Inputs are envisaged as follows: Gridded Lapse rate Orography Temperatures (not used directly) (x DALR) A B C A B C A B C a 2 1 1 1 1 1 270 270 270 b 1 2 1 1 4 1 270 280 270 c 1 1 2 1 1 1 270 270 270 Spot (note the neighbours are identified with the A-C, a-c indices above) Site Temperature Altitude Nearest DZ MinDZ DZ neighbour neighbour 0 280 3 Ac 2 Bb -1 1 270 4 Bb 0 Bb 0 2 280 0 Ca -1 Ca -1 """ # Set up lapse rate cube lapse_rate_data = np.ones(9).reshape(3, 3).astype(np.float32) * DALR lapse_rate_data[0, 2] = 2 * DALR lapse_rate_data[1, 1] = 2 * DALR lapse_rate_data[2, 0] = 2 * DALR self.lapse_rate_cube = set_up_variable_cube(lapse_rate_data, name="lapse_rate", units="K m-1", spatial_grid="equalarea") diagnostic_cube_hash = create_coordinate_hash(self.lapse_rate_cube) # Set up neighbour and spot diagnostic cubes y_coord, x_coord = construct_yx_coords(3, 3, "equalarea") y_coord = y_coord.points x_coord = x_coord.points # neighbours, each group is for a point under two methods, e.g. # [ 0. 0. 0.] is the nearest point to the first spot site, whilst # [ 1. 1. -1.] is the nearest point with minimum height difference. neighbours = np.array([ [[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [2.0, 0.0, -1.0]], [[1.0, 1.0, 2.0], [1.0, 1.0, 2.0], [-1.0, 0.0, -1.0]], ]) altitudes = np.array([3, 4, 0]) # pylint: disable=unsubscriptable-object latitudes = np.array([y_coord[0], y_coord[1], y_coord[2]]) longitudes = np.array([x_coord[0], x_coord[1], x_coord[2]]) wmo_ids = np.arange(3) grid_attributes = ["x_index", "y_index", "vertical_displacement"] neighbour_methods = ["nearest", "nearest_minimum_dz"] self.neighbour_cube = build_spotdata_cube( neighbours, "grid_neighbours", 1, altitudes, latitudes, longitudes, wmo_ids, grid_attributes=grid_attributes, neighbour_methods=neighbour_methods, ) self.neighbour_cube.attributes[ "model_grid_hash"] = diagnostic_cube_hash (time, ) = iris_time_to_datetime(self.lapse_rate_cube.coord("time")) (frt, ) = iris_time_to_datetime( self.lapse_rate_cube.coord("forecast_reference_time")) time_bounds = None time_coords = construct_scalar_time_coords(time, time_bounds, frt) time_coords = [item[0] for item in time_coords] # This temperature cube is set up with the spot sites having obtained # their temperature values from the nearest grid sites. temperatures_nearest = np.array([280, 270, 280]) self.spot_temperature_nearest = build_spotdata_cube( temperatures_nearest, "air_temperature", "K", altitudes, latitudes, longitudes, wmo_ids, scalar_coords=time_coords, ) self.spot_temperature_nearest.attributes[ "model_grid_hash"] = diagnostic_cube_hash # This temperature cube is set up with the spot sites having obtained # their temperature values from the nearest minimum vertical # displacment grid sites. The only difference here is for site 0, which # now gets its temperature from Bb (see doc-string above). temperatures_mindz = np.array([270, 270, 280]) self.spot_temperature_mindz = build_spotdata_cube( temperatures_mindz, "air_temperature", "K", altitudes, latitudes, longitudes, wmo_ids, scalar_coords=time_coords, ) self.spot_temperature_mindz.attributes[ "model_grid_hash"] = diagnostic_cube_hash