### Read in Rajeevan Data (summer JJAS average) 1951 - 2009 and perform PCA nrows=35 ncols=33 ntime = 59 #JJAS 1951 - JJAS 2009 ntimep = 59 # JJAS 1951 - JJAS 2000 N = nrows*ncols ### Lat - Long grid.. ygrid=seq(6.5,38.5,by=1) ny=length(ygrid) xgrid=seq(66.5,100.5,by=1) 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 Rajeevan seasonal average data.. data=readBin("Rajeevan-AISMR-JJAS1951-JJAS2009.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, change it to a negative number. data1[data1 == "NaN"]=-99999. # the lat -long data grid.. index=1:(nx*ny) index1=index[data1 >= 0] # only non-missing data. xygrid1=xygrid[index1,] x1=xygrid1[,2] 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 raindata=matrix(NA,nrow=ntimep, ncol=nsites) for(i in 1:ntimep){ data1=data[,,i] data1[data1 == "NaN"]=-99999. index1=index[data1 >= 0] data2=data1[index1] raindata[i,]=data2 } indexgrid = index1 rm("data") #remove the object data to clear up space ## write out the grid locations.. write(t(xygrid1),file="rajeevan-aismr-locs.txt",ncol=2) ###################### PCA ## PCA on the seasonal SST for the Trop. Pacific Ocean #get variance matrix.. raindata1 = scale(raindata) zs=var(raindata1) #do an Eigen decomposition.. zsvd=svd(zs) #Principal Components... pcs=t(t(zsvd$u) %*% t(raindata1)) #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=35 ncols=33 nglobe = length(xlong)*length(ylat) # also equal to 35*33 zfull = rep(NaN,nglobe) zfull[indexgrid]=zsvd$u[,1] zmat = matrix(zfull,nrow=nrows,ncol=ncols) image.plot(xlong,ylat,zmat) contour(xlong,ylat,zmat,add=TRUE,nlev=6,lwd=2) world(add=TRUE,shift=TRUE) ### Similarly plot the other two Eigen vectors..