from datetime import datetime


def anomaly(dset, clim_freq, slice_mon_clim_time=None, time_coord_name=None):
    """Compute anomalies for a specified time frequency

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

    clim_freq : str
        Climatology frequency alias. Accepted alias:

        - ``mon``: for monthly climatologies

    slice_mon_clim_time : slice, optional
                          a slice object passed to
                          `dset.isel(time=slice_mon_clim_time)` for subseting
                          the time-period overwhich the climatology is computed
    time_coord_name : str
            Name for time coordinate to use


    Returns
    -------
    computed_dset : xarray.Dataset
                    The computed anomaly data

    """

    accepted_freq = {'mon'}
    if clim_freq not in accepted_freq:
        raise ValueError(f'{clim_freq} is not among supported frequency aliases={accepted_freq}')
    else:
        ds = dset.esmlab.set_time(time_coord_name=time_coord_name).compute_mon_anomaly(
            slice_mon_clim_time=slice_mon_clim_time
        )
        new_history = f'\n{datetime.now()} esmlab.anomaly(<DATASET>, clim_freq="{clim_freq}", slice_mon_clim_time="{slice_mon_clim_time}")'
        ds.attrs['history'] = new_history
        return ds