from datetime import datetime


def resample(dset, freq, weights=None, time_coord_name=None, method=None):
    """ Resample given dataset and computes the mean for specified sampling time frequecy

    Parameters
    ----------
    dset : xarray.Dataset
        The data on which to operate

    freq : str
        Time frequency alias. Accepted aliases:

        - ``mon``: for monthly means
        - ``ann``: for annual means

    weights : array_like, optional
            weights to use for each time period. This argument is supported for annual means only!
            If None and dataset doesn't have `time_bound` variable,
            every time period has equal weight of 1.
    time_coord_name : str
            Name for time coordinate to use

    method : str, optional
           Specify how the time values of returned dataset are computed.
           if none, time values are computed as midpoints.

           Accepted values:

           - ``left``: for leftmost time values
           - ``right``: for rightmost time values


    Returns
    -------
    computed_dset : xarray.Dataset
                    The resampled data with computed mean

    """
    accepted_freq = {'mon', 'ann'}
    if freq not in accepted_freq:
        raise ValueError(f'{freq} is not among supported frequency aliases={accepted_freq}')

    if freq == 'mon':
        ds = dset.esmlab.set_time(time_coord_name=time_coord_name).compute_mon_mean(method=method)

    else:
        ds = dset.esmlab.set_time(time_coord_name=time_coord_name).compute_ann_mean(
            weights=weights, method=method
        )

    new_history = f'\n{datetime.now()} esmlab.resample(<DATASET>, freq="{freq}")'
    ds.attrs['history'] = new_history

    return ds