Beispiel #1
0
def test_linear_trend_3_time_years(cube_3_time_years):
    """Test calculation of linear trend with three years."""
    cube_trend = linear_trend(cube_3_time_years)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[4.0, 4.0], [4.0, 4.0]])
    assert cube_trend.units == 'kg yr-1'
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)
Beispiel #2
0
def test_linear_trend_3_time_no_metadata(cube_3_time):
    """Test calculation of trend with three time points and no metadata."""
    cube_3_time.units = None
    cube_trend = linear_trend(cube_3_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[4.0, 4.0], [4.0, 4.0]])
    assert cube_trend.units == Unit('unknown')
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)

    # Cube with unknown units
    cube_3_time.units = Unit('unknown')
    cube_trend = linear_trend(cube_3_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[4.0, 4.0], [4.0, 4.0]])
    assert cube_trend.units == Unit('unknown')
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)

    # Cube with no units
    cube_3_time.units = Unit('no unit')
    cube_trend = linear_trend(cube_3_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[4.0, 4.0], [4.0, 4.0]])
    assert cube_trend.units == Unit('no unit')
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)

    # Time with unknown units
    cube_3_time.units = 'kg'
    cube_3_time.coord('time').units = Unit('unknown')
    cube_trend = linear_trend(cube_3_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[4.0, 4.0], [4.0, 4.0]])
    assert cube_trend.units == Unit('unknown')
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)

    # Time with no units
    cube_3_time.coord('time').units = Unit('no unit')
    cube_trend = linear_trend(cube_3_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[4.0, 4.0], [4.0, 4.0]])
    assert cube_trend.units == Unit('kg')
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)
Beispiel #3
0
def test_linear_trend_1_time(cube_1_time):
    """Test calculation of linear trend with single time point."""
    cube_trend = linear_trend(cube_1_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(
        cube_trend.data, np.ma.masked_equal([[0.0, 0.0], [0.0, 0.0]], 0.0))
    assert not cube_trend.coords('time', dim_coords=True)
    assert cube_trend.coords('latitude', dim_coords=True)
    assert cube_trend.coords('longitude', dim_coords=True)
    assert cube_trend.units == 'kg day-1'
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)
Beispiel #4
0
def test_linear_trend_3_time(cube_3_time):
    """Test calculation of linear trend with three time points."""
    cube_3_time.data[0, 0, 0] = 1.0
    cube_trend = linear_trend(cube_3_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[3.5, 4.0], [4.0, 4.0]])
    assert not cube_trend.coords('time', dim_coords=True)
    assert cube_trend.coords('latitude', dim_coords=True)
    assert cube_trend.coords('longitude', dim_coords=True)
    assert cube_trend.units == 'kg day-1'
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)
Beispiel #5
0
def test_linear_trend_3_time_lazy(cube_3_time):
    """Test lazy calculation of linear trend with three time points."""
    cube_3_time.data = -2.0 * da.arange(3 * 2 * 2).reshape(3, 2, 2)
    assert cube_3_time.has_lazy_data()
    cube_trend = linear_trend(cube_3_time)
    assert cube_trend.shape == (2, 2)
    assert_masked_array_equal(cube_trend.data, [[-8.0, -8.0], [-8.0, -8.0]])
    assert not cube_trend.coords('time', dim_coords=True)
    assert cube_trend.coords('latitude', dim_coords=True)
    assert cube_trend.coords('longitude', dim_coords=True)
    assert cube_trend.units == 'kg day-1'
    assert (iris.coords.CellMethod('trend', coords=('time', ))
            in cube_trend.cell_methods)
Beispiel #6
0
def test_linear_trend_longitude(cube_3_time):
    """Test calculation of linear trend along longitude coordinate."""
    cube_3_time.data[1, 0, 0] = np.nan
    cube_3_time.data = np.ma.masked_invalid(cube_3_time.data)
    cube_trend = linear_trend(cube_3_time, coordinate='longitude')
    assert cube_trend.shape == (3, 2)
    assert_masked_array_equal(
        cube_trend.data,
        np.ma.masked_invalid([[0.01, 0.01], [np.nan, 0.01], [0.01, 0.01]]))
    assert cube_trend.coords('time', dim_coords=True)
    assert cube_trend.coords('latitude', dim_coords=True)
    assert not cube_trend.coords('longitude', dim_coords=True)
    assert cube_trend.units == 'kg m-1'
    assert (iris.coords.CellMethod('trend', coords=('longitude', ))
            in cube_trend.cell_methods)
Beispiel #7
0
def test_linear_trend_coord_not_found(cube_no_time):
    """Test calculation of linear trend when dimension is not available."""
    with pytest.raises(iris.exceptions.CoordinateNotFoundError) as err:
        linear_trend(cube_no_time)
    assert 'time' in str(err.value)
    with pytest.raises(iris.exceptions.CoordinateNotFoundError) as err:
        linear_trend(cube_no_time, coordinate='time')
    assert 'time' in str(err.value)
    with pytest.raises(iris.exceptions.CoordinateNotFoundError) as err:
        linear_trend(cube_no_time, coordinate='aux')
    assert 'aux' in str(err.value)