prefix <- "sv"

skip <-  100

mpidir <- "../svfx_mpi/"
mpifirst <- 0
mpilast <-  9
nodes <- c("001","002","003","004","005","006","007","008","009","010",
           "011","012","013","014","015")

unidir <- "./"
unifirst <- 1
unilast <- 15

library(MASS)

ps.options(horizontal=FALSE,onefile=FALSE)
ps.options(pagecentre=TRUE)
ps.options(paper="letter")
ps.options(width=7.0,height=6.0)

for (node in nodes) {
  for (i in (mpifirst:mpilast)) {
    if (i < 10) {
     filename <- paste(mpidir,prefix,".stat_parm.",node,".00",i,sep="")
    } else {
     filename <- paste(mpidir,prefix,".stat_parm.",node,".0",i,sep="")
    }
    tmp <- scan(filename)
    rows <- tmp[1]
    cols <- tmp[2]
    etai <- matrix(tmp[3:(2+rows*cols)],nrow=rows,ncol=cols)
    if (node == nodes[1]) {
      eta <- etai
      print(dim(eta))
    } else {
      eta <- cbind(eta,etai)
      print(dim(eta))
    }
  }
}

idx <- seq(1,ncol(eta),skip)
mpieta <-eta[,idx]

for (node in nodes) {
  for (i in (mpifirst:mpilast)) {
    if (i < 10) {
     filename <- paste(mpidir,prefix,".stat_func.",node,".00",i,sep="")
    } else {
     filename <- paste(mpidir,prefix,".stat_func.",node,".0",i,sep="")
    }
    tmp <- scan(filename)
    rows <- tmp[1]
    cols <- tmp[2]
    statsi <- matrix(tmp[3:(2+rows*cols)],nrow=rows,ncol=cols)
    if (node == nodes[1]) {
      stats <- statsi
      print(dim(stats))
    } else {
      stats <- cbind(stats,statsi)
      print(dim(stats))
    }
  }
}

idx <- seq(1,ncol(stats),skip)
mpistats <-stats[,idx]


for (i in (unifirst:unilast)) {
  if (i < 10) {
   filename <- paste(unidir,prefix,".as_st_parm.00",i,".dat",sep="")
  } else {
   filename <- paste(unidir,prefix,".as_st_parm.0",i,".dat",sep="")
  }
  tmp <- scan(filename)
  rows <- tmp[1]
  cols <- tmp[2]
  etai <- matrix(tmp[3:(2+rows*cols)],nrow=rows,ncol=cols)
  if (i == unifirst) {
    eta <- etai
    print(dim(eta))
  } else {
    eta <- cbind(eta,etai)
    print(dim(eta))
  }
}

idx <- seq(1,ncol(eta),skip)
unieta <-eta[,idx]


for (i in (unifirst:unilast)) {
  if (i < 10) {
   filename <- paste(unidir,prefix,".as_st_func.00",i,".dat",sep="")
  } else {
   filename <- paste(unidir,prefix,".as_st_func.0",i,".dat",sep="")
  }
  tmp <- scan(filename)
  rows <- tmp[1]
  cols <- tmp[2]
  statsi <- matrix(tmp[3:(2+rows*cols)],nrow=rows,ncol=cols)
  if (i == unifirst) {
    stats <- statsi
    print(dim(stats))
  } else {
    stats <- cbind(stats,statsi)
    print(dim(stats))
  }
}

idx <- seq(1,ncol(stats),skip)
unistats <-stats[,idx]


mpifunc <- mpieta[8,]^2+mpieta[9,]^2
mpiden <- density(mpifunc,adjust=4)

unifunc <- unieta[8,]^2+unieta[9,]^2
uniden <- density(unifunc,adjust=4)

filename <- paste(prefix,".assess_den.eps",sep="")
postscript(file=filename)

plot(c(mpiden$x,uniden$x),c(mpiden$y,uniden$y),
  type="n",xlab="",ylab="",yaxt="n")
  lines(mpiden$x,mpiden$y,lty="solid")
  lines(uniden$x,uniden$y,lty="dashed")

dev.off()

ps.options(horizontal=FALSE,onefile=FALSE)
ps.options(pagecentre=TRUE)
ps.options(paper="letter")
ps.options(width=7.0,height=10.0)

filename <- paste(prefix,".as_st_den.eps",sep="")
postscript(file=filename)
rows <- nrow(unieta)
par(mfrow=c(rows,1),mar=c(2.5,4,1.5,2)+0.1) # mar=c(bottom,left,top,right)
for (j in 1:rows) {
  uniden <- density(unieta[j,],adjust=4)
  mpiden <- density(mpieta[j,],adjust=4)
  plot(c(mpiden$x,uniden$x),c(mpiden$y,uniden$y),
    type="n",xlab="",ylab="",yaxt="n")
    lines(mpiden$x,mpiden$y,lty="solid")
    lines(uniden$x,uniden$y,lty="dashed")
}
dev.off()
