Exemplo n.º 1
0
def test_create_cube_2D():
    """Test creation of 2D output grid."""
    data = np.ones([2, 3])

    # Create a source cube with metadata and scalar coords
    src_cube = Cube(np.zeros(5))
    src_cube.units = "K"
    src_cube.attributes = {"a": 1}
    src_cube.standard_name = "air_temperature"
    scalar_height = AuxCoord([5], units="m", standard_name="height")
    scalar_time = DimCoord([10], units="s", standard_name="time")
    src_cube.add_aux_coord(scalar_height)
    src_cube.add_aux_coord(scalar_time)

    mesh_dim = 0

    grid_x = DimCoord(np.arange(3), standard_name="longitude")
    grid_y = DimCoord(np.arange(2), standard_name="latitude")

    cube = _create_cube(data, src_cube, (mesh_dim,), (grid_x, grid_y), 2)
    src_metadata = src_cube.metadata

    expected_cube = Cube(data)
    expected_cube.metadata = src_metadata
    expected_cube.add_dim_coord(grid_x, 1)
    expected_cube.add_dim_coord(grid_y, 0)
    expected_cube.add_aux_coord(scalar_height)
    expected_cube.add_aux_coord(scalar_time)
    assert expected_cube == cube
Exemplo n.º 2
0
def test_create_cube_4D():
    """Test creation of 2D output grid."""
    data = np.ones([4, 2, 3, 5])

    # Create a source cube with metadata and scalar coords
    src_cube = Cube(np.zeros([4, 5, 5]))
    src_cube.units = "K"
    src_cube.attributes = {"a": 1}
    src_cube.standard_name = "air_temperature"
    scalar_height = AuxCoord([5], units="m", standard_name="height")
    scalar_time = DimCoord([10], units="s", standard_name="time")
    src_cube.add_aux_coord(scalar_height)
    src_cube.add_aux_coord(scalar_time)
    first_coord = DimCoord(np.arange(4), standard_name="air_pressure")
    src_cube.add_dim_coord(first_coord, 0)
    last_coord = AuxCoord(np.arange(5), long_name="last_coord")
    src_cube.add_aux_coord(last_coord, 2)
    multidim_coord = AuxCoord(np.ones([4, 5]), long_name="2d_coord")
    src_cube.add_aux_coord(multidim_coord, (0, 2))
    ignored_coord = AuxCoord(np.arange(5), long_name="ignore")
    src_cube.add_aux_coord(ignored_coord, 1)

    mesh_dim = 1

    grid_x = iris.coords.DimCoord(np.arange(3), standard_name="longitude")
    grid_y = iris.coords.DimCoord(np.arange(2), standard_name="latitude")

    cube = _create_cube(data, src_cube, (mesh_dim,), (grid_x, grid_y), 2)
    src_metadata = src_cube.metadata

    expected_cube = iris.cube.Cube(data)
    expected_cube.metadata = src_metadata
    expected_cube.add_dim_coord(grid_x, 2)
    expected_cube.add_dim_coord(grid_y, 1)
    expected_cube.add_dim_coord(first_coord, 0)
    expected_cube.add_aux_coord(last_coord, 3)
    expected_cube.add_aux_coord(multidim_coord, (0, 3))
    expected_cube.add_aux_coord(scalar_height)
    expected_cube.add_aux_coord(scalar_time)
    assert expected_cube == cube
Exemplo n.º 3
0
    def setUp(self):
        """Create a cube with a single non-zero point."""
        time_coord, frt_coord, fp_coord = time_coords_for_test_cubes()

        lat_coord = DimCoord(np.linspace(-45.0, 45.0, 2),
                             'latitude',
                             units='degrees')
        lon_coord = DimCoord(np.linspace(120, 180, 2),
                             'longitude',
                             units='degrees')
        threshold = DimCoord([0.4, 1.0],
                             long_name="precipitation_amount",
                             units="kg m^-2 s^-1",
                             var_name="threshold")

        data = np.zeros((3, 2, 2))
        data[0][:][:] = 1.0
        data[1][:][:] = 2.0
        data[2][:][:] = 3.0
        cube = Cube(data,
                    standard_name="precipitation_amount",
                    units="kg m^-2 s^-1",
                    dim_coords_and_dims=[(frt_coord, 0), (lat_coord, 1),
                                         (lon_coord, 2)])
        cube.add_aux_coord(time_coord)
        cube.add_aux_coord(fp_coord, data_dims=0)
        self.attributes = {
            'mosg__grid_domain': 'global',
            'mosg__grid_type': 'standard',
            'mosg__grid_version': '1.2.0',
            'mosg__model_configuration': 'gl_det',
            'title': 'Operational ENGL Model Forecast'
        }
        cube.attributes = self.attributes
        self.cube = cube

        new_scalar_coord = AuxCoord(1,
                                    long_name='dummy_scalar_coord',
                                    units='no_unit')
        cube_with_scalar = cube.copy()
        cube_with_scalar.add_aux_coord(new_scalar_coord)
        self.cube_with_scalar = cube_with_scalar

        data_threshold = np.zeros((2, 3, 2, 2))
        data_threshold[0, 0, :, :] = 0.2
        data_threshold[0, 1, :, :] = 0.4
        data_threshold[0, 2, :, :] = 0.6
        data_threshold[1, 0, :, :] = 0.4
        data_threshold[1, 1, :, :] = 0.6
        data_threshold[1, 2, :, :] = 0.8

        cube_threshold = Cube(
            data_threshold,
            long_name="probability_of_precipitation_amount_below_threshold")
        cube_threshold.add_dim_coord(threshold, 0)
        cube_threshold.add_dim_coord(frt_coord, 1)
        cube_threshold.add_dim_coord(lat_coord, 2)
        cube_threshold.add_dim_coord(lon_coord, 3)
        cube_threshold.add_aux_coord(time_coord)
        cube_threshold.add_aux_coord(fp_coord, data_dims=1)

        cube_threshold.attributes.update({'relative_to_threshold': 'below'})
        cube_threshold.attributes = self.attributes
        self.cube_threshold = cube_threshold

        # Weights cubes
        # 1D varying with forecast reference time.
        weights1d = np.array([0.6, 0.3, 0.1], dtype=np.float32)
        # 1D varying with threshold.
        weights_threshold = np.array([0.8, 0.2], dtype=np.float32)
        # 3D varying in space and forecast reference time.
        weights3d = np.array(
            [[[0.1, 0.3], [0.2, 0.4]], [[0.1, 0.3], [0.2, 0.4]],
             [[0.8, 0.4], [0.6, 0.2]]],
            dtype=np.float32)

        self.weights1d = Cube(weights1d,
                              long_name='weights',
                              dim_coords_and_dims=[(frt_coord, 0)],
                              aux_coords_and_dims=[(fp_coord, 0)])
        self.weights1d.add_aux_coord(time_coord)

        self.weights_threshold = Cube(weights_threshold,
                                      long_name='weights',
                                      dim_coords_and_dims=[(threshold, 0)])

        self.weights3d = Cube(weights3d,
                              long_name='weights',
                              dim_coords_and_dims=[(frt_coord, 0),
                                                   (lat_coord, 1),
                                                   (lon_coord, 2)],
                              aux_coords_and_dims=[(fp_coord, 0),
                                                   (time_coord, None)])