Esempio n. 1
0
def test_invalid_args():
    """
    Test for :func:`esmf_regrid.experimental.unstructured_scheme.regrid_unstructured_to_rectilinear`.

    Tests that an appropriate error is raised when arguments are invalid.
    """
    n_lons = 6
    n_lats = 5
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)
    node_src = _gridlike_mesh_cube(n_lons, n_lats, location="node")
    edge_src = _gridlike_mesh_cube(n_lons, n_lats, location="edge")
    face_src = _gridlike_mesh_cube(n_lons, n_lats, location="face")
    tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)

    with pytest.raises(ValueError):
        _ = regrid_unstructured_to_rectilinear(tgt, tgt, method="bilinear")
    with pytest.raises(ValueError):
        _ = regrid_unstructured_to_rectilinear(face_src, tgt, method="other")
    with pytest.raises(ValueError) as excinfo:
        _ = regrid_unstructured_to_rectilinear(node_src,
                                               tgt,
                                               method="conservative")
    expected_message = (
        "Conservative regridding requires a source cube located on "
        "the face of a cube, target cube had the node location.")
    assert expected_message in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        _ = regrid_unstructured_to_rectilinear(edge_src,
                                               tgt,
                                               method="bilinear")
    expected_message = (
        "Bilinear regridding requires a source cube with a node "
        "or face location, target cube had the edge location.")
    assert expected_message in str(excinfo.value)
Esempio n. 2
0
def test_bilinear():
    """
    Basic test for :func:`esmf_regrid.experimental.unstructured_scheme.regrid_unstructured_to_rectilinear`.

    Tests with the bilinear method.
    """
    n_lons = 6
    n_lats = 5
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)
    src = _gridlike_mesh_cube(n_lons, n_lats, location="node")
    tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)
    # Ensure data in the target grid is different to the expected data.
    # i.e. target grid data is all zero, expected data is all one
    tgt.data[:] = 0

    src = _add_metadata(src)
    src.data[:] = 1  # Ensure all data in the source is one.
    result = regrid_unstructured_to_rectilinear(src, tgt, method="bilinear")

    expected_data = np.ones([n_lats, n_lons])
    expected_cube = _add_metadata(tgt)

    # Lenient check for data.
    assert np.allclose(expected_data, result.data)

    # Check metadata and scalar coords.
    expected_cube.data = result.data
    assert expected_cube == result
Esempio n. 3
0
def test_resolution():
    """
    Basic test for :func:`esmf_regrid.experimental.unstructured_scheme.regrid_unstructured_to_rectilinear`.

    Tests the resolution keyword with grids that would otherwise not work.
    """
    src = _flat_mesh_cube()

    # The resulting grid has full latitude bounds and cells must be split up.
    n_lons = 1
    n_lats = 5
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)
    tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds)
    # Ensure data in the target grid is different to the expected data.
    # i.e. target grid data is all zero, expected data is all one
    tgt.data[:] = 0

    src = _add_metadata(src)
    src.data[:] = 1  # Ensure all data in the source is one.
    result = regrid_unstructured_to_rectilinear(src, tgt, resolution=8)

    expected_data = np.ones([n_lats, n_lons])
    expected_cube = _add_metadata(tgt)

    # Lenient check for data.
    # Note that when resolution=None, this would be a fully masked array.
    assert np.allclose(expected_data, result.data)

    # Check metadata and scalar coords.
    expected_cube.data = result.data
    assert expected_cube == result
def test_real_data():
    """
    Test for :func:`esmf_regrid.experimental.unstructured_scheme.regrid_unstructured_to_rectilinear`.

    Tests with cubes derived from realistic data.
    """
    # Load source cube.
    test_data_dir = iris.config.TEST_DATA_DIR
    src_fn = os.path.join(test_data_dir, "NetCDF", "unstructured_grid",
                          "lfric_surface_mean.nc")
    with PARSE_UGRID_ON_LOAD.context():
        src = iris.load_cube(src_fn, "rainfall_flux")

    # Load target grid cube.
    tgt_fn = os.path.join(test_data_dir, "NetCDF", "global", "xyt",
                          "SMALL_hires_wind_u_for_ipcc4.nc")
    tgt = iris.load_cube(tgt_fn)

    # Perform regridding.
    result = regrid_unstructured_to_rectilinear(src, tgt)

    # Check data.
    assert result.shape == (1, 160, 320)
    assert np.isclose(result.data.mean(), 2.93844e-5)
    assert np.isclose(result.data.std(), 2.71724e-5)

    # Check metadata.
    assert result.metadata == src.metadata
    assert result.coord("time") == src.coord("time")
    assert result.coord("latitude") == tgt.coord("latitude")
    assert result.coord("longitude") == tgt.coord("longitude")
    assert result.coord_dims("time") == (0, )
    assert result.coord_dims("latitude") == (1, )
    assert result.coord_dims("longitude") == (2, )
Esempio n. 5
0
def test_multidim_cubes():
    """
    Test for :func:`esmf_regrid.experimental.unstructured_scheme.regrid_unstructured_to_rectilinear`.

    Tests with multidimensional cubes. The source cube contains
    coordinates on the dimensions before and after the mesh dimension.
    """
    mesh = _full_mesh()
    mesh_length = mesh.connectivity(contains_face=True).shape[0]

    h = 2
    t = 3
    height = DimCoord(np.arange(h), standard_name="height")
    time = DimCoord(np.arange(t), standard_name="time")

    src_data = np.empty([t, mesh_length, h])
    src_data[:] = np.arange(t * h).reshape([t, h])[:, np.newaxis, :]
    cube = Cube(src_data)
    mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords("face")
    cube.add_aux_coord(mesh_coord_x, 1)
    cube.add_aux_coord(mesh_coord_y, 1)
    cube.add_dim_coord(time, 0)
    cube.add_dim_coord(height, 2)

    n_lons = 6
    n_lats = 5
    lon_bounds = (-180, 180)
    lat_bounds = (-90, 90)
    tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)

    result = regrid_unstructured_to_rectilinear(cube, tgt)

    # Lenient check for data.
    expected_data = np.empty([t, n_lats, n_lons, h])
    expected_data[:] = np.arange(t * h).reshape(t, h)[:, np.newaxis,
                                                      np.newaxis, :]
    assert np.allclose(expected_data, result.data)

    expected_cube = Cube(expected_data)
    expected_cube.add_dim_coord(time, 0)
    expected_cube.add_dim_coord(tgt.coord("latitude"), 1)
    expected_cube.add_dim_coord(tgt.coord("longitude"), 2)
    expected_cube.add_dim_coord(height, 3)

    # Check metadata and scalar coords.
    result.data = expected_data
    assert expected_cube == result