Beispiel #1
0
def isel_bc(da: xr.DataArray, idx, dim, boundary='wrap'):
    """Select points along axis with ghosting given by a boundary condition
    """

    n = da.shape[da.get_axis_num(dim)]
    if isinstance(idx, slice):
        if idx.stop is None:
            idx = slice(idx.start, n, idx.step)
        idx = _slice_to_range(idx)
    elif not isinstance(idx, Iterable):
        idx = [idx]

    idx = np.asarray(idx)

    if boundary == 'wrap':
        idx = idx % n
        return da.isel(**{dim: idx})
    elif boundary == 'extrap':
        idx[idx < 0] = 0
        idx[idx >= n] = n-1
        return da.isel(**{dim: idx})
    else:
        raise ValueError("Received unknown boundary condition value")

    return da.isel(**{dim: idx})
Beispiel #2
0
def arrays_w_tuples():
    da = xr.DataArray(
        np.random.random((3, 21, 4)),
        coords={"time": pd.date_range("2000-01-01", freq="1D", periods=21)},
        dims=("a", "time", "x"),
    )

    arrays = [
        da.isel(time=range(0, 18)),
        da.isel(time=range(2, 20)).rolling(time=3, center=True).mean(),
        xr.DataArray([[1, 2], [1, np.nan]], dims=["x", "time"]),
        xr.DataArray([[1, 2], [np.nan, np.nan]], dims=["x", "time"]),
    ]

    array_tuples = [
        (arrays[0], arrays[0]),
        (arrays[0], arrays[1]),
        (arrays[1], arrays[1]),
        (arrays[2], arrays[2]),
        (arrays[2], arrays[3]),
        (arrays[3], arrays[3]),
    ]

    return arrays, array_tuples
Beispiel #3
0
def test_datetime_to_numeric_cftime():
    times = xr.cftime_range('2000', periods=5, freq='7D')
    da = xr.DataArray(times, coords=[times], dims=['time'])
    result = utils.datetime_to_numeric(da, datetime_unit='h')
    expected = 24 * xr.DataArray(np.arange(0, 35, 7), coords=da.coords)
    assert_identical(result, expected)

    offset = da.isel(time=1)
    result = utils.datetime_to_numeric(da, offset=offset, datetime_unit='h')
    expected = 24 * xr.DataArray(np.arange(-7, 28, 7), coords=da.coords)
    assert_identical(result, expected)

    dtype = np.float32
    result = utils.datetime_to_numeric(da, datetime_unit='h', dtype=dtype)
    expected = 24 * xr.DataArray(
        np.arange(0, 35, 7), coords=da.coords).astype(dtype)
    assert_identical(result, expected)