Exemple #1
0
    def test_simple(self, tas_series, coord, expected, use_dask, ufunc):
        # if use_dask and ufunc:
        #     pytest.xfail("Ufunc run length algorithms not implemented for dask arrays.")
        t = np.zeros(60)
        t[30:40] = 2
        tas = tas_series(t, start="2000-01-01")
        runs = xr.concat((tas, tas), dim="dim0")

        if use_dask:
            runs = runs.chunk({"time": -1 if ufunc else 10, "dim0": 1})

        out = rl.first_run(runs, window=1, dim="time", coord=coord)
        np.testing.assert_array_equal(out.load(), expected)
Exemple #2
0
    def test_simple(self, tas_series, coord, expected, use_dask, use_1dim):
        t = np.zeros(60)
        t[30:40] = 2
        tas = tas_series(t, start="2000-01-01")
        runs = xr.concat((tas, tas), dim="dim0")

        if use_dask:
            runs = runs.chunk({"time": 10, "dim0": 1})

        out = rl.first_run(runs,
                           window=1,
                           dim="time",
                           coord=coord,
                           ufunc_1dim=use_1dim)
        np.testing.assert_array_equal(out.load(), expected)
Exemple #3
0
def find_end_time(da: xr.DataArray, window=1):
    """fonction qui retourne la derniere valeur de la coordonnees time d'une
    serie de valeurs vraies dont la longueur minimum est definie par la
    valeur de la variable window

    Parameters
    ----------
    da : xr.DataArray
      Input N-dimensional DataArray (boolean)
    window : int
      Minimum duration of consecutive run to accumulate values.

    Returns
    -------
    end_time: xr.DataArray
      Value of the last item of the last valid run. Returns np.nan if no valid run 
      is found.

    """
    index_reverse = first_run(da.sortby('time', ascending=False),
                              window=window)
    index = da.time.size - index_reverse - 1

    # patch
    # on load index pour pouvoir l'utiliser dans isel plus tard ...
    index.load()

    # patch pour probleme avec les valeur d'index nan
    # on ajoute une champ bidon a da et on assigne la valeur de l'index
    # a ce champ bidon. On pose ensuite une valeur nan a toute les dates
    # correspondant a celle de ce champ bidon.
    index_has_nans = (index.count() != index.size)
    if index_has_nans:
        tampon = da.isel(time=0)
        date0 = tampon.time.dt
        date_type = get_time_date_type(da)
        date_nan = genere_date(date_type, date0.year + 1, date0.month,
                               date0.day)
        tampon['time'] = date_nan
        da_tampon = xr.concat((da, tampon), dim='time')
        index_tampon = index.fillna(da.time.size)
        end_time = da_tampon.time.isel(time=index_tampon.astype(int))
        end_time = end_time.where(end_time != date_nan)
    else:
        # cas ou index ne contient pas de nans
        end_time = da.time.isel(time=index.astype(int))

    return end_time
Exemple #4
0
 def test_real_simple(self):
     a = xr.DataArray(np.zeros(100, bool), dims=("x", ))
     a[10:20] = 1
     i = rl.first_run(a, 5, dim="x")
     assert 10 == i