library(spict)

## load what has been saved
setwd("run")
for(f in dir(pattern="RData"))load(f) 
setwd("..")

source("src/common.R")
# Function to do the plotting 
plots<-function(){
    plotspict.biomass(fit)
    plotspict.f(fit)
    plotspict.catch(fit)
    plotspict.bbmsy(fit,lineat=0.5)
    plotspict.ffmsy(fit)
    plotspict.fb(fit)
    plotspict.production(fit)
    plotspict.tc(fit)
    plotspict.priors(fit)
    if("logphi" %in% names(fit$par.fixed)) plotspict.season(fit)
    plotspict.osar(fit)
    plotspict.diagnostic(fit)
    if(exists("RETRO")) plotspict.retro(RETRO)
    if(exists("FORECAST")){
        plotspict.bbmsy(FORECAST,lineat=0.5)
        plotspict.ffmsy(FORECAST,qlegend=FALSE)
        plotspict.catch(FORECAST,qlegend=FALSE)
    }
}

# Save graphics in suitable formats
setwd('res')
file.remove(dir(pattern='png$'))
file.remove(dir(pattern='pdf$'))

stamp<-gsub('-[[:digit:]]{4}$','',gsub(':','.',gsub(' ','-',gsub('^[[:alpha:]]{3} ','',date()))))
png(filename = paste(stamp,"_%03d.png", sep=''), width = 480, height = 480,
    units = "px", pointsize = 10, bg = "white")
  plots()    
dev.off()

png(filename = paste("big_",stamp,"_%03d.png", sep=''), width = 1200, height = 1200, 
    units = "px", pointsize = 20, bg = "white")
  plots()    
dev.off()

##pdf(onefile=FALSE, width = 10, height = 8)
##  plots()    
##dev.off()

##################### TABLES ##################################
file.remove(dir(pattern='html$'))
tab1 <- sumspict.parest(fit);
xtab(tab1,caption="Parameter estimates",cornername="Parameter",file=paste(stamp,'_tab1.html',sep=''),dec=rep(4,ncol(tab1)))

tab2 <- sumspict.drefpoints(fit);
xtab(tab2,caption="Deterministic reference points",cornername="Reference point",file=paste(stamp,'_tab2.html',sep=''),dec=rep(4,ncol(tab2)))

tab3 <- sumspict.srefpoints(fit);
xtab(tab3,caption="Stochastic reference points",cornername="Reference point",file=paste(stamp,'_tab3.html',sep=''),dec=rep(4,ncol(tab3)))

tab4 <- sumspict.states(fit);
xtab(tab4,caption="Estimated states",cornername="",file=paste(stamp,'_tab4.html',sep=''),dec=rep(4,ncol(tab4)))

tab5 <- sumspict.predictions(fit);
xtab(tab5,caption="Forecast",cornername="",file=paste(stamp,'_tab5.html',sep=''),dec=rep(4,ncol(tab5)))


bbmsy <- get.par("logBBmsy",fit,exp=TRUE)
ffmsy <- get.par("logFFmsy",fit,exp=TRUE)
xtab(bbmsy,caption="B/B_msy",cornername="",file=paste(stamp,'_tab6.html',sep=''),dec=rep(4,ncol(bbmsy)))
xtab(ffmsy,caption="F/F_msy",cornername="",file=paste(stamp,'_tab7.html',sep=''),dec=rep(4,ncol(ffmsy)))

bb <- get.par("logB",fit,exp=TRUE)
ff <- get.par("logF",fit,exp=TRUE)
xtab(bb,caption="B",cornername="",file=paste(stamp,'_tab8.html',sep=''),dec=rep(4,ncol(bbmsy)))
xtab(ff,caption="F",cornername="",file=paste(stamp,'_tab9.html',sep=''),dec=rep(4,ncol(ffmsy)))

if(exists("FORECAST")){
    mansum <- mansummary(FORECAST)
    xtab(mansum,caption="Management summary",file=paste(stamp,'_tab9a.html',sep=''),dec=rep(3,ncol(mansum)))
    
}

cat('',file='footprint') 
