# W
# Computations of SVD
pref<-"Cosmic Ray filter on, reduc. + PE + FLF"
print(Sys.time())
Ta<-proc.time()
Vsvd<-svd(Vo)
VV<-Vsvd$v[,1:16]
VD<-Vsvd$d[1:16]
VDs<- sum(Vsvd$d)
VDp<-100*VD/VDs
Xwid<- max(XlocUnique) - min(XlocUnique)
Ywid<- max(YlocUnique) - min(YlocUnique)
YlocUnique
cat('Top 16 SVDs percent:',perc<-round(100*Vsvd$d[1:16]/sum(Vsvd$d),2),'\n')
Ev<-t(VV)%*%t(Vo)

for (i in 1:10){
quartz(width=9, height=8*Ywid/Xwid)
#PCV<-matrix(Ev[i,], nrows,ncols, byrow=TRUE) [,ncols:1]
PCV<-t(matrix(Ev[i,], nrows,ncols, byrow=TRUE)) [,nrows:1]
filled.contour(XlocUnique, YlocUnique, PCV, main=paste(filnam, "PCV",i), color = topo.colors)
mtext(paste(pref,"SVD % is",perc[i] ))	
	            }
browser()
Yadj<-c(-0.15, 0.00, 0.05, 0.13, 0.1, -0.12, -0.05, -0.05, 0.13, 0.1)

quartz(width=10.5, height=6)
plot(c(min(Xo), max(Xo)),c(-1,1), typ='n', main=paste(filnam,'Top 5 SVDs'))
lines(Xo, -2*VV[,1]-0.8+Yadj[1], typ='l', col='black')
lines(Xo,VV[,2]-0.4+Yadj[2], typ='l', col='red')
lines(Xo,VV[,3]+Yadj[3], typ='l', col='green')
lines(Xo,VV[,4]+0.4+Yadj[4], typ='l', col='blue')
lines(Xo,VV[,5]+0.8+Yadj[5], typ='l', col='purple')
labs<- c('black','red','green','blue','purple')
mtext(paste(pref))
for (i in 1:5) {text(1700,-1.15+i*0.4,paste("SDV",i,'=',round(VDp[i],2)), col=labs[i])}

Apaum<-c(430, 449, 581, 590, 609, 960,1047, 1070); yup<-0.8; di<-0.05
for(i in 1:8) { lines(c(Apaum[i],Apaum[i]), c(-0.9,yup), col='brown', lwd=2, lty=2)
	            text(Apaum[i], yup+di*(i%%3), Apaum[i], pos=3, cex=0.66)}
	            Ap<-1318
	            lines(c(Ap,Ap), c(-0.9,0.01), col='purple', lwd=2, lty=2)
                text(Ap, -0.9, Ap, pos=1, cex=0.66)
browser()
quartz(width=10.5, height=6)
plot(c(min(Xo), max(Xo)),c(-1,1), typ='n', main=paste(filnam,'Top 6-10 SVDs'))
lines(Xo,VV[,6]-0.8+Yadj[1], typ='l', col='black')
lines(Xo,VV[,7]-0.4+Yadj[2], typ='l', col='red')
lines(Xo,VV[,8]+Yadj[3], typ='l', col='green')
lines(Xo,VV[,9]+0.4+Yadj[4], typ='l', col='blue')
lines(Xo,VV[,10]+0.8+Yadj[5], typ='l', col='purple')
labs<- c('black','red','green','blue','purple')
mtext(paste(pref))
for (i in 6:10) {text(1700,-1.15+(i-5)*0.4,paste("SDV",i,'=',round(VDp[i],2)), col=labs[i-5])}

Apaum<-c(430, 449, 581, 590, 609, 960,1047, 1070); yup<-0.8; di<-0.05
for(i in 1:8) { lines(c(Apaum[i],Apaum[i]), c(-0.9,yup), col='brown', lwd=2, lty=2)
	            text(Apaum[i], yup+di*(i%%3), Apaum[i], pos=3, cex=0.66)}
	            Ap<-1318
	            lines(c(Ap,Ap), c(-0.9,0.01), col='purple', lwd=2, lty=2)
                text(Ap, -0.9, Ap, pos=1, cex=0.66)
                
browser()
quartz(width=10.5, height=6)
N<-nrows*ncols
Lvo<-length(Vav<-Vo[1,])
                
for (i in 1:Lvo) Vav[i]<- mean(Vo[,i])  

plot(Xo,Vav, typ='l', main=paste(filnam, 'average'))
mtext(pref)
save(filnam, Xo, Vav, nrows, ncols, XlocUnique, YlocUnique, file=paste(filnam,'_avg.Dta', sep=''))

print(proc.time()-Ta)