nrows=72 ncols=36 ntime = 111 #JJAS 1900 - JJAS 2010 ntimep = 111 # JJAS 1900 - JJAS 2010 N = nrows*ncols ### Lat - Long grid.. locs=matrix(scan("sst-lat-long.txt"), ncol=2, byrow=T) ygrid=seq(-87.5,87.5,by=5) ny=length(ygrid) xgrid=seq(27.5,382.5,by=5) #xgrid[xgrid > 180]=xgrid[xgrid > 180]-360 #longitude on 0-360 grid if needed xgrid[xgrid > 180]=xgrid[xgrid > 180] nx=length(xgrid) xygrid=matrix(0,nrow=nx*ny,ncol=2) i=0 for(iy in 1:ny){ for(ix in 1:nx){ i=i+1 xygrid[i,1]=ygrid[iy] xygrid[i,2]=xgrid[ix] } } # REad Kaplan SST data.. data=readBin("Kaplan-SST-JJAS1900-JJAS2011.r4",what="numeric", n=( nrows * ncols * ntime), size=4,endian="swap") data <- array(data = data, dim=c( nrows, ncols, ntime ) ) data1=data[,,1] # Missing value is NaN, put it to a large number.. data1[data1 == "NaN"]=1e+30 # the lat -long data grid.. index=1:(nx*ny) index1=index[data1 < 20] # only non-missing data. xygrid1=xygrid[index1,] x1=xygrid1[,2] #x1[x1 < 0]= x1[x1 < 0] + 360 #xygrid1[,2]=x1 nsites=length(index1) # locations with data -i.e. global locations data2=data1[index1] ### SSTdata matrix - rows are years and columns are locations on the globe with data sstdata=matrix(NA,nrow=ntimep, ncol=nsites) for(i in 1:ntimep){ data1=data[,,i] data1[data1 == "NaN"]=1e+30 index1=index[data1 < 20] data2=data1[index1] sstdata[i,]=data2 } ## Index of locations corresponding to Pacific Tropic xlongs = xygrid[,2] ylats = xygrid[,1] indextrop = index[data1 < 20 & ylats >= -20 & ylats <= 20 & xlongs >= 105 & xlongs <= 290] rm("data") #remove the object data to clear up space ### Tropical Pacific Ocean xlongs = xygrid1[,2] ylats = xygrid1[,1] xlong = xlongs[ylats >= -20 & ylats <= 20 & xlongs >= 105 & xlongs <= 290] ylat = ylats[ylats >= -20 & ylats <= 20 & xlongs >= 105 & xlongs <= 290] index1=1:length(xlongs) index = index1[ylats >= -20 & ylats <= 20 & xlongs >= 105 & xlongs <= 290] ### Tropical seasonal average.. - the data already is seasonal average sstanavgtrop = sstdata[,index] xygridp=cbind(ylat,xlong) ## write out the grid locations.. write(t(xygridp),file="kaplan-sst-pac-locs.txt",ncol=2) ###################### PCA ## PCA on the seasonal SST for the Trop. Pacific Ocean #get variance matrix.. zs=var(sstanavgtrop) #do an Eigen decomposition.. zsvd=svd(zs) #Principal Components... pcs=t(t(zsvd$u) %*% t(sstanavgtrop)) #Eigen Values.. - fraction variance lambdas=(zsvd$d/sum(zsvd$d)) plot(1:40, lambdas[1:40], type="l", xlab="Modes", ylab="Frac. Var. explained") points(1:40, lambdas[1:40], col="red") #plots.. #plot the first spatial component or Eigen Vector pattern.. library(maps) library(akima) library(fields) # the data is on a grid so fill the entire globaal grid with NaN and then populate the ocean grids with # the Eigen vector xlong = sort(unique(xygrid[,2])) ylat = sort(unique(xygrid[,1])) nrows=72 ncols=36 nglobe = length(xlong)*length(ylat) # also equal to 72*36 zfull = rep(NaN,nglobe) zfull[indextrop]=zsvd$u[,1] zmat = matrix(zfull,nrow=nrows,ncol=ncols) image.plot(xlong,ylat,zmat,ylim=range(-40,70)) contour(xlong,ylat,(zmat),ylim=range(-40,70),add=TRUE,nlev=6,lwd=2) world(add=TRUE,shift=TRUE) ### Similarly plot the other two Eigen vectors.. ################## If you wish to remove a component from the data, say first component. #### nmodes = length(zsvd$u[1,]) # number of modes nkeep = c(1) # modes to keep, here we keep the first mode. If more then # nkeep=c(1,2,3) etc.. E = matrix(0,nrow=nmodes,ncol=nmodes) E[,nkeep]=zsvd$u[,nkeep] sstanavgkeep = pcs %*% t(E) sstanrem = sstanavg - sstanavgkeep ## Now perform PCA on sstanrem