Example #1
0
def test_data_fits():
    """Tests the is_correct_grid() function"""
    # Should get False returned if the number of points is incorrect
    test_grid = Geogrid('1deg-global')
    bad_y = test_grid.num_y - 1
    bad_x = test_grid.num_x - 1
    test_array = np.random.rand(bad_y, bad_x)
    assert test_grid.data_fits(test_array) == False
    # Should get True returned if the number of points is correct
    test_grid = Geogrid('1deg-global')
    test_array = np.random.rand(test_grid.num_y, test_grid.num_x)
    assert test_grid.data_fits(test_array) == True
    # Should raise a GridError if the data isn't a NumPy array
    test_grid = Geogrid('1deg-global')
    with raises(GeogridError):
        test_grid.data_fits(1)
def test_interpolation():
    """Test that interpolation can be done between all grids"""
    # Create fake data for every grid
    fake_data = {}
    for gridname in list_builtin_geogrids():
        grid = Geogrid(gridname)
        fake_data[gridname] = np.random.rand(grid.num_y,
                                             grid.num_x).astype('float16')
    # Loop over every possible combination of grids
    for gridname1 in list_builtin_geogrids():
        for gridname2 in list_builtin_geogrids():
            # Create 2 Grid objects
            grid1 = Geogrid(gridname1)
            grid2 = Geogrid(gridname2)
            interpolate(fake_data[gridname1], grid1, grid2)
    # Make sure a 1-d input results in a 1-d output
    grid1 = Geogrid(list_builtin_geogrids()[0])
    grid2 = Geogrid(list_builtin_geogrids()[1])
    data1 = np.random.rand(grid1.num_y * grid1.num_x)
    data2 = interpolate(data1, grid1, grid2)
    assert data1.ndim == data2.ndim
    # Make sure a 2-d input results in a 2-d output
    grid1 = Geogrid(list_builtin_geogrids()[0])
    grid2 = Geogrid(list_builtin_geogrids()[1])
    data1 = np.random.rand(grid1.num_y, grid1.num_x)
    data2 = interpolate(data1, grid1, grid2)
    assert data1.ndim == data2.ndim
def test_smooth():
    """Test the function that smooths an array of spatial data"""
    # Make sure when passing in a MaskedArray, a MaskedArray is returned
    test_array = np.ma.masked_array(np.random.rand(2, 2))
    returned_array = fill_outside_mask_borders(test_array)
    mask = returned_array.mask
    # Make sure when passing in a non-MaskedArray, a non-MaskedArray is returned
    test_array = np.random.rand(2, 2)
    returned_array = fill_outside_mask_borders(test_array)
    with raises(AttributeError):
        returned_array.mask
    # Make sure a GridError is raised if the grid doesn't match the data
    test_grid = Geogrid(list_builtin_geogrids()[-1])
    test_array = np.random.rand(5, 5)
    with raises(GeogridError):
        smooth(test_array, test_grid)
    # Make sure the dimensions of the data don't change when smoothed
    test_grid = Geogrid(list_builtin_geogrids()[-1])
    test_array = np.random.rand(test_grid.num_y, test_grid.num_x)
    smoothed_array = smooth(test_array, test_grid)
    assert test_array.shape == smoothed_array.shape
Example #4
0
def test_latlon_to_gridpoint():
    """Tests the latlon_to_gridpoint() function"""
    # gridpoint of latlon pair (0, 0) should be 90 for a 1deg-global Geogrid
    grid = Geogrid('1deg-global')
    assert set(grid.latlon_to_1d_index((0, 0))) == {90}
    # gridpoint should be [-1] if the latlon pair isn't found on the Geogrid
    grid = Geogrid('2deg-conus')
    assert set(grid.latlon_to_1d_index((0, 0))) == {-1}
Example #5
0
def test_create_geogrid():
    """Ensure the Geogrid object can be created"""
    for builtin_geogrid in list_builtin_geogrids():
        Geogrid(builtin_geogrid)
Example #6
0
def test_print_returns_something(capfd):
    """Ensure the print_info() functions returns a non-empty string"""
    for builtin_geogrid in list_builtin_geogrids():
        print(Geogrid(builtin_geogrid))
        out, err = capfd.readouterr()
        assert out
Example #7
0
def test_create_custom_geogrid():
    """Ensure a custom Grid object can be created"""
    Geogrid(ll_corner=(0, 0), ur_corner=(90, 90), res=1)
Example #8
0
def test_exception_raised_for_no_grid_name():
    """Ensure an exception is raised for an unsupported units"""
    with raises(GeogridError):
        Geogrid()