pangeo-data/climpred

resample uninitalized from initialized in `HindcastEnsemble`

aaronspring opened this issue · 0 comments

Is your feature request related to a problem? Please describe.
You only have initialized forecasts (S2S) and want to compute uninitialized skill from initialized forecasts.

Describe the solution you'd like
Take initialized data but verify with different year making the forecasts not initialized for the verification year. would be great to have generate_uninitialized also for HindcastEnsemble

Describe alternatives you've considered

def resample_uninitialized_from_initialized(init, resample_dim=['init', 'member']):
    init_notnull = init.where(init.notnull(), drop=True)
    full_years = list(set(init_notnull.init.dt.year.values))  # todo: ensure full years taken # [1:-1]

    import numpy as np
    same_year = False
    while not same_year:
        m = full_years.copy()
        np.random.shuffle(m)
        same_year = not (np.array(m) - np.array(full_years) == 0).any()
    # assert not (np.array(m) - np.array(full_years) == 0).any()

    ms = [str(i) for i in m]

    resampled_inits = xr.concat([init.sel(init=i).init for i in ms], "init")

    resampled_uninit = init.sel(init=resampled_inits)
    resampled_uninit["init"] = init_notnull.sel(
        init=slice(str(full_years[0]), str(full_years[-1]))
    ).init
    # resample members
    if 'member' in resample_dim:
        resampled_members = np.random.randint(0, init.member.size, init.member.size)
        resampled_uninit = resampled_uninit.isel(member=resampled_members)
        resampled_uninit["member"] = init.member
    return resampled_uninit.drop("valid_time")

todo:

  • check if ds.init.dt.year.groupby("init.year").count()
  • use .isel(lead=0)
  • resample with replacement for init also