splitvar/splitvar.py
def splitbytime(var, freq, timedim='time'):
    """Given an xarray variable, split into periods of time defined by freq
    """

    # Check freq is greater than or equal to the frequency of the variable
    # (so delta is <)
    freq_delta = to_timedelta(freq) 
    varvals = var.indexes[timedim].values
    vardelta = pd.Timedelta(min(varvals[1:-1]-varvals[0:-2]))
    if (freq_delta < vardelta):
        # raise ValueError("Split frequency ({}) is higher than data frequency ({}): not supported".format(freq,strfdelta(vardelta,"{D}d {H}h {M}m {S}s")))
        raise ValueError("Split frequency ({}) is higher than data frequency ({}): not supported".format(freq,vardelta))

    if (type(var.indexes[timedim][0]) == cftime.DatetimeNoLeap):
        # Variable is using a non-standard noleap calendar
        # Create a temporary pandas data frame with the start and end extracted time dimension
        # and the same frequency, but this has leap years (not an issue for what we will use this)
        pdtime = pd.DataFrame(index=pd.date_range(start=varvals[0]._to_real_datetime(),end=varvals[-1]._to_real_datetime(),freq=vardelta))
        # Change the time dimension to datetime so it can be sliced correctly
        var[timedim] = [d._to_real_datetime() for d in var.indexes[timedim].values]
    else:
        pdtime = pd.DataFrame(index=var.indexes[timedim])

    # Use pandas time grouping to cycle through at freq, grabbing out
    # the start and end points to select out data
    for k, v in pdtime.groupby(pd.Grouper(freq=freq)):
        s, e = v.index.values[[0,-1]]
        yield var.sel(time=slice(pd.Timestamp(s),pd.Timestamp(e)))
Given an xarray variable, split into periods of time defined by freq
    
Given an xarray variable, split into periods of time defined by freq
Given an xarray variable split into periods of time defined by freq
def splitbytime(var, freq, timedim='time'):
    freq_delta = to_timedelta(freq) 
    varvals = var.indexes[timedim].values
    vardelta = pd.Timedelta(min(varvals[1:-1]-varvals[0:-2]))
    if (freq_delta < vardelta):
        raise ValueError("Split frequency ({}) is higher than data frequency ({}): not supported".format(freq,vardelta))
    if (type(var.indexes[timedim][0]) == cftime.DatetimeNoLeap):
        pdtime = pd.DataFrame(index=pd.date_range(start=varvals[0]._to_real_datetime(),end=varvals[-1]._to_real_datetime(),freq=vardelta))
        var[timedim] = [d._to_real_datetime() for d in var.indexes[timedim].values]
    else:
        pdtime = pd.DataFrame(index=var.indexes[timedim])
    for k, v in pdtime.groupby(pd.Grouper(freq=freq)):
        s, e = v.index.values[[0,-1]]
        yield var.sel(time=slice(pd.Timestamp(s),pd.Timestamp(e)))
def splitbytime ( var , freq , timedim = 'time' ) : freq_delta = to_timedelta ( freq ) varvals = var . indexes [ timedim ] . values vardelta = pd . Timedelta ( min ( varvals [ 1 : - 1 ] - varvals [ 0 : - 2 ] ) ) if ( freq_delta < vardelta ) : raise ValueError ( "Split frequency ({}) is higher than data frequency ({}): not supported" . format ( freq , vardelta ) ) if ( type ( var . indexes [ timedim ] [ 0 ] ) == cftime . DatetimeNoLeap ) : pdtime = pd . DataFrame ( index = pd . date_range ( start = varvals [ 0 ] . _to_real_datetime ( ) , end = varvals [ - 1 ] . _to_real_datetime ( ) , freq = vardelta ) ) var [ timedim ] = [ d . _to_real_datetime ( ) for d in var . indexes [ timedim ] . values ] else : pdtime = pd . DataFrame ( index = var . indexes [ timedim ] ) for k , v in pdtime . groupby ( pd . Grouper ( freq = freq ) ) : s , e = v . index . values [ [ 0 , - 1 ] ] yield var . sel ( time = slice ( pd . Timestamp ( s ) , pd . Timestamp ( e ) ) )
Given an xarray variable, split into periods of time defined by freq
Given an xarray variable split into periods of time defined by freq
"""Given an xarray variable, split into periods of time defined by freq
    """ # Check freq is greater than or equal to the frequency of the variable # (so delta is <) # raise ValueError("Split frequency ({}) is higher than data frequency ({}): not supported".format(freq,strfdelta(vardelta,"{D}d {H}h {M}m {S}s"))) # Variable is using a non-standard noleap calendar # Create a temporary pandas data frame with the start and end extracted time dimension # and the same frequency, but this has leap years (not an issue for what we will use this) # Change the time dimension to datetime so it can be sliced correctly # Use pandas time grouping to cycle through at freq, grabbing out # the start and end points to select out data
var
freq
timedim