## Plot
.libPaths(".")


library(mussel)
library(gridConstruct)
library(sp)
load("res.RData.exe")


source("src/common.R")

tab1 <- tab2 <- NULL
# Function to do the plotting 
plots<-function(){

  ##plotMaps(env, latestYear)
  ##for(qw in 1:4)setcap("title","caption")
  tab1 <<- plotTimeSeries(env,"Natura Loegstoer")
  tab2 <<- plotTimeSeries(env, "Lovns Bredning")
  
  k <- which(spatialRegions2 == "Natura Loegstoer")
  plotBiomassMap(env, time=latestYear, xlim=range(gr$lon[k]), ylim=range(gr$lat[k]))

  k <- which(spatialRegions2 == "Lovns Bredning")
  plotBiomassMap(env, time=latestYear, xlim=range(gr$lon[k]), ylim=range(gr$lat[k]))

  ## All
  plotBiomassMap(env, time=latestYear, xlim=range(gr$lon), ylim=range(gr$lat))
  
  plotCalib()
  plotCalib("efficiency")
}

setcap("Biomass Natura Loegstoer","kg/m^2")
setcap("Biomass Lovns Bredning","kg/m^2")
setcap("Biomass Natura Loegstoer","kg/m^2")
setcap("Biomass Lovns Bredning","kg/m^2")
setcap("Biomass All","kg/m^2")
setcap("Gear calibration")
setcap("Gear calibration")

# Save graphics in suitable formats
setwd('res')
file.remove(dir(pattern='png$'))
file.remove(dir(pattern='pdf$'))
file.remove(dir(pattern='html$'))

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()

writeLines(unlist(tit.list),'titles.cfg') 


cat('',file='footprint') 


####################### Animations ##########################
if(!("animation" %in% installed.packages()))
    devtools::install_version("animation", version = "2.5", repos = "https://cloud.r-project.org", upgrade=FALSE)
library(animation)
ani.options(verbose=FALSE)
##formals(saveHTML)$single.opts <- "'defaultFrame': 'last', 'controls': ['first', 'previous', 'play', 'next', 'last', 'loop', 'speed'], 'loopMode': 'none' "
saveHTML <- function(..., htmlfile) {
    img.name <- sub(".html", "", htmlfile)
	animation:::saveHTML(..., img.name=img.name, htmlfile=htmlfile, single.opts = "'defaultFrame': 'last', 'controls': ['first', 'previous', 'play', 'next', 'last', 'loop', 'speed'], 'loopMode': 'none' ")
    include_anim(htmlfile)
}

animYears <- 2006:latestYear

k <- which(spatialRegions2 == "Natura Loegstoer")
saveHTML(quote(for(i in animYears)plotBiomassMap(env, time=i, xlim=range(gr$lon[k]), ylim=range(gr$lat[k]))),htmlfile=paste(stamp,'_anim1.html',sep=''))

k <- which(spatialRegions2 == "Lovns Bredning")
saveHTML(quote(for(i in animYears)plotBiomassMap(env, time=i, xlim=range(gr$lon[k]), ylim=range(gr$lat[k]))),htmlfile=paste(stamp,'_anim2.html',sep=''))

## All
saveHTML(quote(for(i in animYears)plotBiomassMap(env, time=i, xlim=range(gr$lon), ylim=range(gr$lat))),htmlfile=paste(stamp,'_anim3.html',sep=''))

##tab1 <- table(d$Year, d$Gear)
##xtab(tab1,caption="Number of hauls by Year and Gear",cornername="",file=paste(stamp,'_tab1.html',sep=''),dec=rep(0,ncol(tab1)))

dec <- c(2,2,2,0,0,0,2)
xtab(tab1,caption="Natura Loegstoer", cornername="", file=paste(stamp,'_tab1.html',sep=''), dec=dec )
xtab(tab2,caption="Lovns Bredning", cornername="", file=paste(stamp,'_tab2.html',sep=''), dec=dec )


