##REVIEW OF OSBORN AND BRIFFA [2006] FUNCTIONS #TO EXTEND SERIES BY PERSISTENCE extend.persist<-function(tree) { extend.persist<-tree for (j in 1:ncol(tree) ) { test<-is.na(tree[,j]) end1<-max ( c(1:nrow(tree)) [!test]) test2<-( c(1:nrow(tree))>end1) & test extend.persist[test2,j]<-tree[end1,j] } extend.persist } #JONES GRIDCELL ID FROM LAT AND LONG jones<-function(lat,long) { i<-18-floor(lat/5); j<- 36+ceiling(long/5); jones<-72*(i-1) + j jones } #GAUSSIAN FILTER #see http://www.ltrr.arizona.edu/~dmeko/notes_8.pdf gaussian.filter.weights<-function(N,year) { #N number of points; year - is number of years sigma<-year/6 i<-( (-(N-1)/2):((N-1)/2) ) /sigma w<- ((2*pi)^-0.5) * exp(-0.5* i^2) gaussian.filter.weights<-w/sum(w) gaussian.filter.weights } truncated.gauss.weights<-function(year) { a<-gaussian.filter.weights(2*year+1,year) temp<-(a>0.05*max(a)) a<-a[temp] a<-a/sum(a) truncated.gauss.weights<-a truncated.gauss.weights } #RETURN SMOOTHED SERIES filter.combine.pad<-function(x,a,M=NA) { temp<-!is.na(x) year<-c(tsp(x)[1]:tsp(x)[2])[temp] w<-x[temp] N<-length(w) if(is.na(M)) M<-trunc (length(a)/2)+1 w<-c (rep(mean(w[1:M]),M),w,rep(mean(w[(N-M+1):N]),M)) y<-filter(w,a,method="convolution",sides=2) y<-y[(M+1):(length(y)-M)] z<-ts (rep(NA,length(x)),start=tsp(x)[1],end=tsp(x)[2]) z[temp]<-y filter.combine.pad<-ts.union(x,z) #filter.combine<-y[,2] filter.combine.pad } #MANNOMATIC DATA TRANSFORMATION sd.detrend<-function(x) {t<-c(1:length(x));fm<-lm(x~t);sd.detrend<-sd(fm$residuals);sd.detrend} mannomatic<-function(x,M=78) { N<-length(x) xstd<- (x- mean( x[(N-M):N]))/sd(x[(N-M):N]) sdprox<-sd.detrend(xstd[(N-M):N]) mannomatic<- xstd/sdprox mannomatic } #FUNCTION TO LISTFILES AND REMOVE SUFFIXES strip<-function(h,n) { h<-unlist(strsplit(h, "\\.")) h<-array(h,dim=c(n,length(h)/n)) h<-t(h) strip<-h[,1] strip } #CALCULATE ANNUAL AVERAGE FROM MONTHLY TIME SERIES annavg<-function(x) { n<-length(x) m<-n-12*floor(n/12) if(m>0) x<-c(x, rep(NA,12-m)) years<-length(x)/12 x<-array(x,dim=c(12,years)) annavg<-apply(x,2,mean,na.rm=TRUE) return(annavg) }