*************************************************************************************** * $Id: rmean.gs,v 1.28 2016/05/11 21:49:20 bguan Exp $ * * Copyright (c) 2007-2015, Bin Guan * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *************************************************************************************** function rmean(arg) * * Apply running mean in time dimension. * input=subwrd(arg,1) arg2=subwrd(arg,2) if(arg2='') usage() return endif arg3=subwrd(arg,3) if(!valnum(arg3)) if(valnum(arg2)!=1 | arg2<3) say '[rmean ERROR] must be integer >=3.' return endif window=arg2 tminus=math_int(window/2) tplus=window-tminus-1 output=arg3 else tminus=-arg2 tplus=arg3 output=subwrd(arg,4) endif if(output='') output=input endif tt=tminus+tplus+1 say '[rmean info] Averaging from t-'tminus' to t+'tplus'.' if(output='display'|output='DISPLAY') 'display tloop(ave('input',t-'tminus',t+'tplus'))' else 'define 'output' = tloop(ave('input',t-'tminus',t+'tplus'))' endif return *************************************************************************************** function usage() * * Print usage information. * say ' Apply running mean in time dimension.' say '' say ' USAGE 1: rmean []' say ' USAGE 2: rmean []' say '' say ' EXAMPLE 1: successively average "sst" over 4 time steps t-2, t-1, t, t+1, and save to variable "sstnew".' say ' rmean sst 4 sstnew' say '' say ' EXAMPLE 2: successively average "sst" over 3 time steps t-1, t, t+1, and save to variable "sstnew".' say ' rmean sst -1 1 sstnew' say '' say ' Copyright (c) 2007-2015, Bin Guan.' return