esmvaltool/diag_scripts/ocean/diagnostic_timeseries.py
def moving_average(cube, window):
    """
    Calculate a moving average.

    The window is a string which is a number and a measuremet of time.
    For instance, the following are acceptable windows:
        '5 days'
        '12 years'
        '1 month'
        '5 yr'

    Also note the the value used is the total width of the window.
    For instance, if the window provided was '10 years', the the moving
    average returned would be the average of all values within 5 years
    of the central value.
    In the case of edge conditions, at the start an end of the data, they
    only include the average of the data available. Ie the first value
    in the moving average of a '10 year' window will only include the average
    of the five subsequent years.
    """
    window = window.split()
    window_len = int(window[0]) / 2.
    win_units = str(window[1])

    if win_units not in ['days', 'day', 'dy',
                         'months', 'month', 'mn',
                         'years', 'yrs', 'year', 'yr']:
        raise ValueError("Moving average window units not recognised: " +
                         "{}".format(win_units))

    times = cube.coord('time').units.num2date(cube.coord('time').points)

    datetime = diagtools.guess_calendar_datetime(cube)

    output = []

    times = np.array([datetime(time_itr.year, time_itr.month, time_itr.day,
                               time_itr.hour, time_itr.minute)
                      for time_itr in times])

    for time_itr in times:
        if win_units in ['years', 'yrs', 'year', 'yr']:
            tmin = datetime(time_itr.year - window_len, time_itr.month,
                            time_itr.day, time_itr.hour, time_itr.minute)
            tmax = datetime(time_itr.year + window_len, time_itr.month,
                            time_itr.day, time_itr.hour, time_itr.minute)

        if win_units in ['months', 'month', 'mn']:
            tmin = datetime(time_itr.year, time_itr.month - window_len,
                            time_itr.day, time_itr.hour, time_itr.minute)
            tmax = datetime(time_itr.year, time_itr.month + window_len,
                            time_itr.day, time_itr.hour, time_itr.minute)

        if win_units in ['days', 'day', 'dy']:
            tmin = datetime(time_itr.year, time_itr.month,
                            time_itr.day - window_len, time_itr.hour,
                            time_itr.minute)
            tmax = datetime(time_itr.year, time_itr.month,
                            time_itr.day + window_len, time_itr.hour,
                            time_itr.minute)

        arr = np.ma.masked_where((times < tmin) + (times > tmax), cube.data)
        output.append(arr.mean())
    cube.data = np.array(output)
    return cube

    Calculate a moving average.

    The window is a string which is a number and a measuremet of time.
    For instance, the following are acceptable windows:
        '5 days'
        '12 years'
        '1 month'
        '5 yr'

    Also note the the value used is the total width of the window.
    For instance, if the window provided was '10 years', the the moving
    average returned would be the average of all values within 5 years
    of the central value.
    In the case of edge conditions, at the start an end of the data, they
    only include the average of the data available. Ie the first value
    in the moving average of a '10 year' window will only include the average
    of the five subsequent years.
    
Calculate a moving average.
The window is a string which is a number and a measuremet of time.

Also note the the value used is the total width of the window.
For instance, if the window provided was '10 years', the the moving
average returned would be the average of all values within 5 years
of the central value.
In the case of edge conditions, at the start an end of the data, they
only include the average of the data available. Ie the first value
in the moving average of a '10 year' window will only include the average
of the five subsequent years.
Calculate a moving average . The window is a string which is a number and a measuremet of time . Also note the the value used is the total width of the window . For instance if the window provided was ' 10 years ' the the moving average returned would be the average of all values within 5 years of the central value . In the case of edge conditions at the start an end of the data they only include the average of the data available . Ie the first value in the moving average of a ' 10 year ' window will only include the average of the five subsequent years .
def moving_average(cube, window):
    window = window.split()
    window_len = int(window[0]) / 2.
    win_units = str(window[1])
    if win_units not in ['days', 'day', 'dy',
                         'months', 'month', 'mn',
                         'years', 'yrs', 'year', 'yr']:
        raise ValueError("Moving average window units not recognised: " +
                         "{}".format(win_units))
    times = cube.coord('time').units.num2date(cube.coord('time').points)
    datetime = diagtools.guess_calendar_datetime(cube)
    output = []
    times = np.array([datetime(time_itr.year, time_itr.month, time_itr.day,
                               time_itr.hour, time_itr.minute)
                      for time_itr in times])
    for time_itr in times:
        if win_units in ['years', 'yrs', 'year', 'yr']:
            tmin = datetime(time_itr.year - window_len, time_itr.month,
                            time_itr.day, time_itr.hour, time_itr.minute)
            tmax = datetime(time_itr.year + window_len, time_itr.month,
                            time_itr.day, time_itr.hour, time_itr.minute)
        if win_units in ['months', 'month', 'mn']:
            tmin = datetime(time_itr.year, time_itr.month - window_len,
                            time_itr.day, time_itr.hour, time_itr.minute)
            tmax = datetime(time_itr.year, time_itr.month + window_len,
                            time_itr.day, time_itr.hour, time_itr.minute)
        if win_units in ['days', 'day', 'dy']:
            tmin = datetime(time_itr.year, time_itr.month,
                            time_itr.day - window_len, time_itr.hour,
                            time_itr.minute)
            tmax = datetime(time_itr.year, time_itr.month,
                            time_itr.day + window_len, time_itr.hour,
                            time_itr.minute)
        arr = np.ma.masked_where((times < tmin) + (times > tmax), cube.data)
        output.append(arr.mean())
    cube.data = np.array(output)
    return cube
def moving_average ( cube , window ) : window = window . split ( ) window_len = int ( window [ 0 ] ) / 2. win_units = str ( window [ 1 ] ) if win_units not in [ 'days' , 'day' , 'dy' , 'months' , 'month' , 'mn' , 'years' , 'yrs' , 'year' , 'yr' ] : raise ValueError ( "Moving average window units not recognised: " + "{}" . format ( win_units ) ) times = cube . coord ( 'time' ) . units . num2date ( cube . coord ( 'time' ) . points ) datetime = diagtools . guess_calendar_datetime ( cube ) output = [ ] times = np . array ( [ datetime ( time_itr . year , time_itr . month , time_itr . day , time_itr . hour , time_itr . minute ) for time_itr in times ] ) for time_itr in times : if win_units in [ 'years' , 'yrs' , 'year' , 'yr' ] : tmin = datetime ( time_itr . year - window_len , time_itr . month , time_itr . day , time_itr . hour , time_itr . minute ) tmax = datetime ( time_itr . year + window_len , time_itr . month , time_itr . day , time_itr . hour , time_itr . minute ) if win_units in [ 'months' , 'month' , 'mn' ] : tmin = datetime ( time_itr . year , time_itr . month - window_len , time_itr . day , time_itr . hour , time_itr . minute ) tmax = datetime ( time_itr . year , time_itr . month + window_len , time_itr . day , time_itr . hour , time_itr . minute ) if win_units in [ 'days' , 'day' , 'dy' ] : tmin = datetime ( time_itr . year , time_itr . month , time_itr . day - window_len , time_itr . hour , time_itr . minute ) tmax = datetime ( time_itr . year , time_itr . month , time_itr . day + window_len , time_itr . hour , time_itr . minute ) arr = np . ma . masked_where ( ( times < tmin ) + ( times > tmax ) , cube . data ) output . append ( arr . mean ( ) ) cube . data = np . array ( output ) return cube
Calculate a moving average.
Calculate a moving average .
"""
    Calculate a moving average.

    The window is a string which is a number and a measuremet of time.
    For instance, the following are acceptable windows:
        '5 days'
        '12 years'
        '1 month'
        '5 yr'

    Also note the the value used is the total width of the window.
    For instance, if the window provided was '10 years', the the moving
    average returned would be the average of all values within 5 years
    of the central value.
    In the case of edge conditions, at the start an end of the data, they
    only include the average of the data available. Ie the first value
    in the moving average of a '10 year' window will only include the average
    of the five subsequent years.
    """
cube
window