
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  ma.fill.r                                                                              #
  #           Last Updated Funclist  :  08 Feb 2015,  5:12 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   ma.fill            ( x, order=3, warn=TRUE, na.rm=TRUE, safetycheck=1000, showWarnings=warn )                            #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

## ma.fill.r

## NOTE TO SELF:  I may have another similar function in one of the supportFns folders.  
##    (maybe NBS ? )

ma.fill <- function(x, order=3, warn=TRUE, na.rm=TRUE, safetycheck=1000, showWarnings=warn) {
  ## like taking a moving average, but fills in NAs
  ## Not very sound mathematically, but helps
  ##   wtih quickly moving foward when blocked by NAs
  ##
  ## NOTE: if na.rm==0, NAs will be replaced with 0. 
  ##       if na.rm==TRUE, NAs wil be ignored. 
  ##       the difference is mean(c(3, 0, 5)) and mean(c(3, 5)) 
  ##
  ## if x is larger than order, will return the average of the whole set

  if (na.rm == 0)
    x <- removeNA(x, 0)

  ## make na.rm a baltant T/F, since it will be need again later
  na.rm <- isTRUE(na.rm)

  l.x <- length(x)

  ## x should be as large or larger than order and should not be all NA
  if (l.x < order) {
    if (showWarnings)
      warning("The length of x (", l.x, ") is less than the order (", order, ")")
    return(rep(mean(x, na.rm=na.rm), l.x))
  }
  if (all(is.na(x))) {
    if (showWarnings)
      warning("x has length (", l.x, ") and is all NA")
    return(x)
  }

  ## Take an extended range
  hf <- floor(order/2)
  s.x <- seq(-hf, by=1, length.out=l.x) ## aka: seq(-hf, l.x-(order-hf))
  s.o <- seq(order)

  ret <- sapply(s.x, function(i) {
              ind <- s.o+i
              ind <- ind[ind>0]
              mean(x[ind], na.rm=na.rm)
          })

  browser(expr=inDebugMode(), text="in ma.fill right before the while loop.")
 
  ## Begin filling
  w <- 1
  while ( any(is.na(ret)) && safetycheck > 0 && w < l.x ) {
    # cat("safety = ", safetycheck, "    (w = ", w, ")\n", sep="")
    safetycheck <- safetycheck - 1
    ret.last <- copy(ret)

    nas <- which(is.na(ret))
    ret[nas] <- sapply(nas, function(i) {
                  ind <- i+(-w:w)
                  ind <- ind[ind >0 & ind <=l.x]
                  mean(ret[ind], na.rm=TRUE)
                })
    ## if there are no changes, increase the width
    if (identical(is.na(ret), is.na(ret.last)))
      w <- w + 1
    # ret[is.na(ret)] <- 
    #     ma.fill(x=ret, order=order+1, safetycheck=safetycheck-1, na.rm=na.rm, showWarnings=showWarnings)[is.na(ret)]
  }
 
  if (!is.null(names(x)))
    names(ret) <- names(x)

  return(ret)
}
