R/splineInterpolate.R

Defines functions splineInterpolate

#Interpolation of magpie objects
splineInterpolate<-function(x,years,method="fmm",...){
  if(!is.magpie(x)) stop("input has to be a MAgPIE object!")
  xvals<-getYears(x,as.integer=T)
  if(all(is.character(years))) years <- as.integer(substring(years,2))
  if(min(years)<min(xvals)||max(years)>max(xvals)) stop("Only interpolation allowed, no extrapolation.")
  out<-new.magpie(cells_and_regions=getCells(x),years=years,names=getNames(x))
  for(i in getCells(x)){
    for(j in ifelse(is.null(getNames(x)),1,getNames(x))){
      if(is.null(j)) j <- 1
      tmp <-spline(x=xvals,y=as.vector(x[i,,j]),method=method,xout=years,...)
      suppressWarnings(out[i,tmp$x,j] <- tmp$y)
    }
  }
  return(out)
}
pik-piam/validation documentation built on Nov. 5, 2019, 12:50 a.m.