R/climvar.R

Defines functions climvar

Documented in climvar

#' Seasonally varying variance
#'
#' \code{climvar} estimates how the variance varies with season
#' in terms of the inter-annual variability of daily standard deviation
#'
#' @param x an input object of class 'station'
#' @param plot a boolean; if TRUE show plot
#' @param FUN a function
#' @param verbose a boolean; if TRUE print information about progress
#' @param \dots additional arguments
#'
#' @export
climvar <- function(x,FUN='sd',plot=TRUE,verbose=FALSE,...) {
  yrs <- as.numeric(rownames(table(year(x))))
  #print(yrs)
  ny <- length(yrs)
  X <- x; class(X) <- "zoo"
  
  if ( (attr(x,'unit') == "deg C") | (attr(x,'unit') == "degree Celsius") ) {
    unit <- expression(degree*C) 
  } else {
    unit <- attr(x,'unit')
  }
  main <- NULL
  eval(parse(text=paste("main <- expression(paste('seasonal ",
#               deparse(substitute(FUN))," of ',",
               FUN," of ',",attr(x,'variable'),"))")))
  Z <- matrix(rep(NA,ny*365),ny,365)
  
  for (i in 1:ny) {
    y <- window(X,start=as.Date(paste(yrs[i],'-01-01',sep='')),
                    end=as.Date(paste(yrs[i],'-12-31',sep='')))
    t <- julian(index(y)) - julian(as.Date(paste(yrs[i],'-01-01',sep='')))
    Z[i,] <- approx(t,y,1:365)$y
  }
  z <- apply(Z,2,FUN,na.rm=TRUE,...)
  wt <- 2*pi*(1:365)/365
  s1 <- sin(wt); c1 <- cos(wt); s2 <- sin(2*wt); c2 <- cos(2*wt)
  s3 <- sin(3*wt); c3 <- cos(3*wt); s4 <- sin(4*wt); c4 <- cos(4*wt)
  acfit <- predict(lm(z ~s1 + c1 + s2 + c2 + s3 + c3 + c4 + s4))
    
  if (plot) {
    par0 <- par()
    dev.new()
    par(bty="n")
    plot(c(0,365),range(z,na.rm=TRUE),
         type="n",xlab="",
         main=main,
        sub=attr(x,'location'),ylab=ylab(x))
    grid()
    lines(z,lwd=5)
    lines(z,lwd=3,col="grey")
    lines(acfit,lwd=5)
    lines(acfit,lwd=3,col="red")

    par(new=TRUE,fig=c(0.15,0.35,0.70,0.90),mar=c(0,0,0,0),
        yaxt="n",xaxt="n",las=1)
    plot(c(0,1),c(0,1),type="n",xlab="",ylab="")
    legend(0,1,c("raw data","harmonic fit"),lwd=3,col=c("grey","red"),bty="n",cex=0.6)  
    par(bty=par0$bty, fig=par0$fig, mar=par0$mar, 
        yaxt=par0$yaxt, xaxt=par0$xaxt, las=par0$las)
  }
  
  #acfit <- attrcp(x,acfit)
  #attr(acfit,'raw_data') <- z
  #attr(acfit,'history') <- history.stamp(x)
  return(z)
}
metno/esd documentation built on April 29, 2024, 3:34 p.m.