#' Plot eddies data
#'
#' @param x An \code{"eddy"} table such as returned by \code{\link{read_eddy}}.
#' @param cnd A logical vector to be used to subset x befor plotting.
#' @param add Should the data be plotted on existing graph ?
#' @param cex the size inflation factor of the features.
#' @details Blue ellipses indicate cyclonic eddies (rotating clockwise in the
#' southern hemisphere) while red ellipses indicate anticyclonic eddies.
#' The solid lines represent the boundaries of the eddies for the latest
#' date available in the input \code{x}. Points indicate the center of the features
#' with their size proportional to the eddy solidity. Line width indicate the mean
#' geostrophic speed
#' @export
#' @examples
#' \dontrun{
#' edd <- eddies(as.POSIXct("2010-01-01"))
#' plot(edd)
#' }
plot.eddy <- function(x, cnd = NULL, add = FALSE, cex = 1,
                      col = c(cyc = "blue", anticyc = "red"), ...) {
  x <- x["if"(is.null(cnd), rep(TRUE, nrow(x)), cnd), ]

  if (!add) plot(Lat ~ Lon, x, type = "n")
  is_last <- x$Date == max(x$Date)
  is_cycl <- x$Cyc == -1
  cols <- ifelse(is_cycl, col[1], col[2])
  with(x, ellipses(Lon, Lat,
                   major = unlist(MajorAxisLength) * options()$eddies.pixel_size / 2 *
                     cex * unlist(Extent),
                   minor = unlist(MinorAxisLength) * options()$eddies.pixel_size / 2 *
                     cex * unlist(Extent),
                   agl = unlist(Orientation) * pi /180,
                   col = cols,
                   lwd = x$MeanGeoSpeed / 100,
                   lty = ifelse(is_last, 1, 3))
  )
  with(x[is_last, ],
       points(Lon, Lat, cex = unlist(Solidity), pch = 20,
              col = cols[is_last])
  )

  invisible()
}


# Example for
# http://stackoverflow.com/questions/25085537/permute-groups-in-a-data-frame-r#
#

source("R/Test_Dq_fun.r")

# Test with other data
#
setwd("Simul")
bName <- "neuFish65_256"
Dq1 <- readNeutral_calcDq(paste0(bName,"T100mfSAD.txt"))

plotDq_ReplaceR(Dq1,0.2,0.04,0.0001)

# Compare replacementRate = 1 and 0.1
#
require(plyr)
require(dplyr)

Dqq <- filter(Dq1,MortalityRate==0.2,DispersalDistance==0.04,ColonizationRate==0.0001,
              ReplacementRate==1 | ReplacementRate==0.1, q<11 & q>-11)
names(Dqq)

# Add repetitions

simbyrep <- nrow(Dqq)/3

Dqq$rep <- rep( 1:3,each=simbyrep)

plotDq(Dqq,"as.factor(paste(ReplacementRate,rep))")

adt <- pairwiseAD_Dif(Dqq,"Dq",Dqq[,c(4,10)])
adt

# # Compare using permutations
# #
# require(kSamples)
# ks <- ad.test(list(Dqq$Dq[1:35],Dqq$Dq[36:70]),method="simulated",Nsim=10000)
# ks

require(reshape2)
Dq2<- dcast(Dqq,ReplacementRate+rep~q,value.var="Dq")
require(statmod)
compareTwoGrowthCurves(Dq2$ReplacementRate,Dq2[,3:ncol(Dq2)],nsim=1000)

# Compare ReplacementRate 1 and 0.01

Dqq <- filter(Dq1,MortalityRate==0.2,DispersalDistance==0.04,ColonizationRate==0.0001,
              ReplacementRate==1 | ReplacementRate==0.01, q<11 & q>-11)

simbyrep <- nrow(Dqq)/3

Dqq$rep <- rep( 1:3,each=simbyrep)

plotDq(Dqq,"as.factor(paste(ReplacementRate,rep))")

adt <- pairwiseAD_Dif(Dqq,"Dq",Dqq[,c(4,10)])
adt

Dq2<- dcast(Dqq,ReplacementRate+rep~q,value.var="Dq")
compareTwoGrowthCurves(Dq2$ReplacementRate,Dq2[,3:ncol(Dq2)],nsim=1000)

# Visualizar ecdf en las que se basas AD test

ggplot(Dqq,aes(x = Dq,colour=paste(ReplacementRate,rep))) + stat_ecdf(geom="smooth")


args = commandArgs(trailingOnly=TRUE)
data = read.csv(file=args[1], header=TRUE, sep=",",check.names=FALSE)
pdf(args[2])
#names.arg = data[0,], # x-axis labels
#col = colors, # colors
#fill = sequential[6:1], # 6:1 reorders so legend order matches graph
par(mar=c(5, 11, 4, 4)) # Resize the margin to fit the y axis labels
s <- 1.8
xx = barplot(as.matrix(data),
	horiz=TRUE,
	xlab = "Coverage (%)",
    xlim = c(0,100),
    family = "serif", las=1, font.lab=2,
    col = c("light grey", "white"),
    cex.names=s, cex.axis=s, cex.lab=s, cex.main=s, cex.sub=s)

#legend = c("Complement", "Intersection"),
#legend("topright", c("Complement", "Intersection"), cex=s, pch=s, pt.cex=s, fill = c("light grey", "white"))
legend("topright", c("Complement", "Intersection"), cex=s, pt.cex=s, fill = c("light grey", "white"))

text(y = xx-0.3, x = as.numeric(data[1,]) + as.numeric(data[2,]) + 10, label = as.numeric(data[1,]) + as.numeric(data[2,]), pos = 3, cex = s, family="serif") # show text on top of each bar


#####################################
#Prepare figures
# Logsheets per year by region
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,2,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  a <- table(llv$vessid,llv$op_yr)
  a <- apply(a>0,1,sum)
  print(table(a))

  a <- tapply(llv$vessid,llv$op_yr,length)
  plot(names(a),a,xlab="Year",ylab="Logsheet records",main=paste("Region",r))
  }
savePlot(filename=paste("Number of records",sep=""),type="png")

lu <- function(x) {
  length(unique(x))
  }

# Vessels per year by region
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,2,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  a <- tapply(llv$vessid,llv$op_yr,lu)
  plot(names(a),a,xlab="Year",ylab="",main=paste("Region",r))
  }
savePlot(filename=paste("Unique vessels by year",sep=""),type="png")

# Time distribution of vessels
vy <- list()
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,2,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  vess <- unique(llv$vessid)
  minyr <- maxyr <- vess
  for (i in 1:length(vess)) {
    minyr[i] <- min(llv[vess[i]==llv$vessid,]$op_yr)
    maxyr[i] <- max(llv[vess[i]==llv$vessid,]$op_yr)
    }
  vessyrs <- data.frame(vess=vess,minyr=as.numeric(minyr),maxyr=as.numeric(maxyr),stringsAsFactors=F)
  vessyrs <- vessyrs[order(-floor(vessyrs$minyr),-floor(vessyrs$maxyr)),]
  vy[[r]] <- vessyrs
  plot(1:length(vess),1:length(vess),xlim=c(1978,2010),type="n",xlab="Years",ylab="Vessel",main=paste("Region",r))
  for (i in 1:length(vess)) {
    lines(c(floor(vessyrs[i,]$minyr),floor(vessyrs[i,]$maxyr)),c(i,i))
    }
  }
savePlot(filename=paste("Time distribution of vessels 1",sep=""),type="png")

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,2,2,1))
for (r in 1:6) {
  vessyrs <- vy[[r]]
  plot(1:400,1:400,xlim=c(1952,2010),type="n",xlab="Years",ylab="Vessel",main=paste("Region",r))
  for (i in 1:length(vess)) {
    lines(c(floor(vessyrs[i,]$minyr),floor(vessyrs[i,]$maxyr)),c(i,i))
    }
  }
savePlot(filename=paste("Time distribution of vessels 1b",sep=""),type="png")

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,2,2,1))
for (r in 1:6) {
  vessyrs <- vy[[r]]
  vessyrs <- vessyrs[order(-floor(vessyrs$maxyr)),]
  llv <- op[op$reg==r,]
  vess <- unique(llv$vessid)
  plot(1:length(vess),1:length(vess),xlim=c(1978,2010),type="n",xlab="Years",ylab="Vessel",main=paste("Region",r))
  for (i in 1:length(vess)) {
    lines(c(floor(vessyrs[i,]$minyr),floor(vessyrs[i,]$maxyr)),c(i,i))
    }
  }
savePlot(filename=paste("Time distribution of vessels 2",sep=""),type="png")

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,2,2,1))
for (r in 1:6) {
  vessyrs <- vy[[r]]
  llv <- op[op$reg==r,]
  vess <- unique(llv$vessid)
  minyr <- vess
  plot(1:length(vess),1:length(vess),xlim=c(1978,2010),type="n",xlab="Years",ylab="Vessel",main=paste("Region",r))
  for (i in 1:length(vess)) {
    a <- floor(llv[vessyrs[i,1]==llv$vessid,]$yr)
    pp <- unique(a)
    pp2 <- tapply(a,a,length)
#    points(pp,rep(i,length(pp)),cex=0.6,pch=3)
    symbols(pp,rep(i,length(pp)),sqrt(pp2)/40, add = T, inches =FALSE)
    }
  }
savePlot(filename=paste("Time distribution of vessels 4",sep=""),type="png")

# Effort by region
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  a <- tapply(llv$hooks,llv$yrqtr,sum)
  plot(names(a),a,xlab="Year",ylab="Hooks",main=paste("Region",r))
  }
savePlot(filename="Effort by region by yrqtr",type="png")

# Effort by region by yrqtr by fishingcat
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  a <- tapply(llv$hooks,llv$yrqtr,sum)
  plot(names(a),a,xlab="Year",ylab="Hooks",main=paste("Region",r),col=1,pch=16)
  llv <- op[op$reg==r & op$newfishingcat==1,]  # offshore
  a <- tapply(llv$hooks,llv$yrqtr,sum)
  points(names(a),a,col=2,pch=2)
  llv <- op[op$reg==r & op$newfishingcat==2,]  # DW
  a <- tapply(llv$hooks,llv$yrqtr,sum)
  points(names(a),a,col=4,pch=4)
  }
legend("topright",legend=c("Total","Offshore","Distant water"),col=c(1,2,4),pch=c(16,2,4))
savePlot(filename="Effort by region by yrqtr by fishingcat",type="png")

# Sets by region by yrqtr by fishingcat
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  a <- tapply(llv$hooks,llv$yrqtr,length)
  plot(names(a),a,xlab="Year",ylab="Sets",main=paste("Region",r),col=1,pch=16)
  llv <- op[op$reg==r & op$newfishingcat==1,]
  a <- tapply(llv$hooks,llv$yrqtr,length)
  points(names(a),a,col=2,pch=2)
  llv <- op[op$reg==r & op$newfishingcat==2,]
  a <- tapply(llv$hooks,llv$yrqtr,length)
  points(names(a),a,col=4,pch=4)
  }
legend("topright",legend=c("Total","Offshore","Distant water"),col=c(1,2,4),pch=c(16,2,4))
savePlot(filename="Sets by region by yrqtr by fishingcat",type="png")

# total effort by region and yearqtr
e <- tapply(op$hooks,list(op$yrqtr,op$reg),sum)
write.table(file="effort by region and yrqtr.csv",e,sep=",")

# Tonnage - should be a stacked histogram, but no time
a <- unique(op$vessid)
a <- op[match(a,op$vessid),]
windows(height=14,width=12); par(mfrow=c(2,1),mar=c(3,4,2,1))
hist(a[a$newfishingcat==1,]$tonnage,breaks=seq(0,500,20),ylim=c(0,300),xlab="GRT (metric tonnes)",ylab="Number of vessels",main="Vessels",col=4,density=15,angle=135)
hist(a[a$newfishingcat==2,]$tonnage,breaks=seq(0,500,20),col=1,density=25,angle=45,add=T)
legend("topright",legend=c("Distant water","Offshore"),col=c(4,1),density=c(25,15),angle=c(45,135))

hist(op[op$newfishingcat==1,]$tonnage,breaks=seq(0,500,20),xlab="GRT (metric tonnes)",,ylab="Number of sets",main="Sets",col=4,density=15,angle=135)
hist(op[op$newfishingcat==2,]$tonnage,breaks=seq(0,500,20),xlab="GRT (metric tonnes)",main="",col=1,density=25,angle=45,add=T)
legend("topright",legend=c("Distant water","Offshore"),col=c(4,1),density=c(25,15),angle=c(45,135))
savePlot(file="plot tonnage by fishingcat",type="png")

# Effort coverage by region
# Load JPLLagg from SPC database
ll <- read.table("D:/SPC_NRIFSF_data2011/QUERY091.csv",sep=",",header=T)
ll$region <- rep(0, length(ll$hhooks))
ll$region <- ifelse(ll$latd > 20 & ll$latd < 40 & ll$lond > 110 & ll$lond < 170, 1, ll$region)
ll$region <- ifelse(ll$latd > 20 & ll$latd < 40 & ll$lond > 170 & ll$lond < 210, 2, ll$region)
#ll$region <- ifelse(ll$latd > -10 & ll$latd < 20 & ll$lond > 135 & ll$lond < 170, 3, ll$region)
ll$region <- ifelse(ll$latd > -10 & ll$latd < 20 & ll$lond > 110 & ll$lond < 170, 3, ll$region)
ll$region <- ifelse(ll$latd > -10 & ll$latd < 20 & ll$lond > 170 & ll$lond < 210, 4, ll$region)
ll$region <- ifelse(ll$latd > -35 & ll$latd < -10 & ll$lond > 140 & ll$lond < 170, 5, ll$region)
ll$region <- ifelse(ll$latd > -35 & ll$latd < -10 & ll$lond > 170 & ll$lond < 210, 6, ll$region)
##INDO/PH region
##ll$region <- ifelse(ll$LATD > -10 & ll$LATD < 20 & ll$LOND > 110 & ll$LOND < 135, 7, ll$region)
ll <- ll[ll$region > 0,]
ll$yrqtr <- ll$yy + ll$qtr/4 - 0.125
effort <- tapply(ll$hhooks, list(ll$yrqtr, ll$region), sum)*100
albc <- tapply(ll$alb_no, list(ll$yrqtr, ll$region), sum)
betc <- tapply(ll$bet_no, list(ll$yrqtr, ll$region), sum)
yftc <- tapply(ll$yft_no, list(ll$yrqtr, ll$region), sum)
swoc <- tapply(ll$swo_no, list(ll$yrqtr, ll$region), sum)

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  e <- tapply(llv$hooks,llv$yrqtr,sum)
  agge <- effort[,r]
  plot(names(e),e/agge[match(names(e),names(agge))],xlab="Year",ylab="Hooks",main=paste("Region",r),ylim=c(0,3))
  abline(h=1,lty=2)
  }
savePlot(filename="Effort coverage by region by yrqtr",type="png")
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  alb<- tapply(llv$alb,llv$yrqtr,sum)
  agge <- albc[,r]
  plot(names(alb),alb/agge[match(names(alb),names(agge))],xlab="Year",ylab="ALB catch",main=paste("Region",r),ylim=c(0,3))
  abline(h=1,lty=2)
  }
savePlot(filename="ALB coverage by region by yrqtr",type="png")
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  bet <- tapply(llv$bet,llv$yrqtr,sum)
  agge <- betc[,r]
  plot(names(bet),bet/agge[match(names(bet),names(agge))],xlab="Year",ylab="BET catch",main=paste("Region",r),ylim=c(0,3))
  abline(h=1,lty=2)
  }
savePlot(filename="BET coverage by region by yrqtr",type="png")
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  yft <- tapply(llv$yft,llv$yrqtr,sum)
  agge <- yftc[,r]
  plot(names(yft),yft/agge[match(names(yft),names(agge))],xlab="Year",ylab="YFT catch",main=paste("Region",r),ylim=c(0,3))
  abline(h=1,lty=2)
  }
savePlot(filename="YFT coverage by region by yrqtr",type="png")
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  swo <- tapply(llv$swo,llv$yrqtr,sum)
  agge <- swoc[,r]
  plot(names(swo),swo/agge[match(names(swo),names(agge))],xlab="Year",ylab="SWO catch",main=paste("Region",r),ylim=c(0,3))
  abline(h=1,lty=2)
  }
savePlot(filename="SWO coverage by region by yrqtr",type="png")

load("alldatraw.RData")
dat$lat_raw <- as.numeric(dat$lat)
dat$lon_raw <- dat$lon
dat$lat[dat$latcode==2] <- (dat$lat_raw[dat$latcode==2]+1) * -1
dat$lon[dat$loncode==2] <- 360 - (dat$lon_raw[dat$loncode==2] + 1)
dat$reg <- 0
dat[dat$lat <  40 & dat$lat >=  20 & dat$lon >= 110 & dat$lon < 170,]$reg <- 1
dat[dat$lat <  40 & dat$lat >=  20 & dat$lon >= 170 & dat$lon < 210,]$reg <- 2
dat[dat$lat <  20 & dat$lat >= -10 & dat$lon >= 110 & dat$lon < 170,]$reg <- 3
dat[dat$lat <  20 & dat$lat >= -10 & dat$lon >= 170 & dat$lon < 210,]$reg <- 4
dat[dat$lat < -10 & dat$lat >= -35 & dat$lon >= 140 & dat$lon < 170,]$reg <- 5
dat[dat$lat < -10 & dat$lat >= -35 & dat$lon >= 170 & dat$lon < 210,]$reg <- 6
dat$yrqtr <- dat$op_yr + floor((dat$op_mon)/3)/4 + 0.125
dat <- dat[dat$reg >0 & dat$reg <7,]
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  cld <- op[op$reg==r,]
  rwd <- dat[dat$reg==r,]
  cle <- tapply(cld$hooks,cld$yrqtr,sum)
  rwe <- tapply(rwd$hooks,rwd$yrqtr,sum)
  plot(names(cle),cle/rwe[match(names(cle),names(rwe))],xlab="Year",ylab="Proportion of hooks",main=paste("Region",r),ylim=c(0,1.1))
  }
savePlot(filename="Cleaned effort proportion by region by yrqtr",type="png")
rm(dat)

# Target by region through time
for (fc in c("OS","DW")) {
  windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(0,0,3,0))
  b <- op[op$yrqtr >1994 & op$newfishingcat==switch(fc,"OS"=1,"DW"=2),]
  for (r in 1:6) {
    llv <- b[b$reg==r,]
    a <- tapply(llv$hooks,list(llv$yrqtr,factor(llv$target,levels=1:3)),sum)
    tota <- apply(a,1,sum,na.rm=T)
    tota[tota==0]<-1
    a[is.na(a)==T]<-0
    plot(names(tota),a[,1]/tota,type="b",col=1,xlim=c(1994,2010),xlab="Year",ylab="Proportion of effort",main=paste("Region",r),ylim=c(0,1))
    lines(names(tota),a[,2]/tota,type="b",col=2,pch=2,lty=1)
    lines(names(tota),a[,3]/tota,type="b",col=3,pch=3,lty=1)
    }
  legend("bottomright",legend=c("Swordfish","Sharks","Other (Tuna)"),col=c(1,2,3),pch=c(1,2,3),lty=c(1,1,1))
  title(switch(fc,"DW"="Distant water","OS"="Offshore"),outer=T)
  savePlot(filename=paste("Target by region by yrqtr",fc),type="png")
}

# Bait type by region through time
for (fc in c("OS","DW")) {
  windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(0,0,3,0))
  b <- op[op$newfishingcat==switch(fc,"OS"=1,"DW"=2),]
  for (r in 1:6) {
    llv <- b[b$reg==r,]
    a <- tapply(llv$hooks,list(llv$yrqtr,factor(llv$bait,levels=1:4)),sum)
    tota <- apply(a,1,sum,na.rm=T)
    tota[tota==0]<-1
    a[is.na(a)==T]<-0
    plot(names(tota),a[,1]/tota,type="p",col=1,xlim=c(1952,2010),xlab="Year",ylab="Proportion of effort",main=paste("Region",r),ylim=c(0,1))
    lines(names(tota),a[,2]/tota,type="p",col=2,pch=2,lty=1)
    lines(names(tota),a[,3]/tota,type="p",col=3,pch=3,lty=1)
    lines(names(tota),a[,4]/tota,type="p",col=4,pch=4,lty=1)
    }
  legend("bottomright",legend=c("Pacific saury", "Squid", "Live bait", "Other"),col=c(1,2,3,4),pch=c(1,2,3,4),lty=c(1,1,1,1))
  title(switch(fc,"DW"="Distant water","OS"="Offshore"),outer=T)
  savePlot(filename=paste("Bait type by region by yrqtr",fc),type="png")
}

# Catch by region
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  yft <- tapply(llv$yft,llv$yrqtr,sum)
  bet <- tapply(llv$bet,llv$yrqtr,sum)
  alb <- tapply(llv$alb,llv$yrqtr,sum)
  swo <- tapply(llv$swo,llv$yrqtr,sum)
  maxy <- max(c(yft,bet,alb,swo))
  plot(names(yft),yft,ylim=c(0,maxy),xlab="Year",ylab="Catch",main=paste("Region",r))
  points(names(bet),bet,col=2,pch=2)
  points(names(alb),alb,col=3,pch=3)
  points(names(swo),swo,col=4,pch=4)
  }
legend("topright",legend=c("Yellowfin","Bigeye","Albacore","Swordfish"),col=c(1,2,3,4),pch=c(1,2,3,4))
savePlot(filename="Catch by region allsp by yrqtr",type="png")

# CPUE by region
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  yft <- tapply(llv$yft,llv$yrqtr,sum)
  eff <- tapply(llv$hooks,llv$yrqtr,sum)
  bet <- tapply(llv$bet,llv$yrqtr,sum)
  alb <- tapply(llv$alb,llv$yrqtr,sum)
  swo <- tapply(llv$swo,llv$yrqtr,sum)
  yft <- 100*yft/eff
  bet <- 100*bet/eff
  alb <- 100*alb/eff
  swo <- 100*swo/eff
  maxy <- max(c(yft,bet,alb,swo))
  plot(names(yft),yft,ylim=c(0,maxy),xlab="Year",ylab="Catch per hundred hooks",main=paste("Region",r))
  points(names(bet),bet,col=2,pch=2)
  points(names(alb),alb,col=3,pch=3)
  points(names(swo),swo,col=4,pch=4)
  }
legend("topright",legend=c("Yellowfin","Bigeye","Albacore","Swordfish"),col=c(1,2,3,4),pch=c(1,2,3,4))
savePlot(filename="CPUE nominal allsp by region by yrqtr",type="png")

# CPUE by region by fishingcat
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(0,0,3,0))
for (fc in 1:2) {
  for (r in 1:6) {
    llv <- op[op$reg==r & op$newfishingcat==fc,]
    yft <- tapply(llv$yft,llv$yrqtr,sum)
    eff <- tapply(llv$hooks,llv$yrqtr,sum)
    bet <- tapply(llv$bet,llv$yrqtr,sum)
    alb <- tapply(llv$alb,llv$yrqtr,sum)
    swo <- tapply(llv$swo,llv$yrqtr,sum)
    yft <- 100*yft/eff
    bet <- 100*bet/eff
    alb <- 100*alb/eff
    swo <- 100*swo/eff
    maxy <- max(c(yft,bet,alb,swo))
    plot(names(yft),yft,ylim=c(0,maxy),xlab="Year",ylab="Catch per hundred hooks",main=paste("Region",r))
    points(names(bet),bet,col=2,pch=2)
    points(names(alb),alb,col=3,pch=3)
    points(names(swo),swo,col=4,pch=4)
    }
  legend("topright",legend=c("Yellowfin","Bigeye","Albacore","Swordfish"),col=c(1,2,3,4),pch=c(1,2,3,4))
  title(switch(fc,"Offshore","Distant water"),outer=T)
  savePlot(filename=paste("CPUE nominal allsp by region by yrqtr",fc),type="png")
  }

# 5 degree squares fished
dimu <- function(x) { length(unique(x)) }
windows(height=14,width=12)
par (mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg == r,]
  yq <- sort(unique(llv$yrqtr))
  strats <- tapply(paste(llv$lat5,llv$lon5),llv$yrqtr,dimu)
  plot(yq, strats, type="p", xlim=c(1952, 2010),pch=1,col=1,ylim=c(0,max(strats)),cex=1,ylab="5 x 5 spatial strata with reported effort",main=paste("Region",r))
#  mtext(side=3, paste("Region", r),line=0.5)
  }
savePlot("Number of spatial strata",type="png")

# Proportion sets with zero catches
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  ay <- tapply(llv$yft==0,llv$yrqtr,sum)
  a <- tapply(llv$hooks,llv$yrqtr,length)
  ab <- tapply(llv$bet==0,llv$yrqtr,sum)
  ay <- 100*ay/a
  ab <- 100*ab/a
  maxy <- max(c(ay,ab))
  plot(names(ay),ay,ylim=c(0,maxy),xlab="Year",ylab="Proportion of zero catches",main=paste("Region",r))
  points(names(ab),ab,col=2,pch=2)
  }
legend("topright",legend=c("Yellowfin","Bigeye"),col=c(1,2),pch=c(1,2))
savePlot(filename="Proportion zeroes by region by yrqtr",type="png")

# Proportion sets with zero catches allspp
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for (r in 1:6) {
  llv <- op[op$reg==r,]
  ay <- tapply(llv$yft==0,llv$yrqtr,sum)
  a <- tapply(llv$hooks,llv$yrqtr,length)
  ab <- tapply(llv$bet==0,llv$yrqtr,sum)
  aalb <- tapply(llv$alb==0,llv$yrqtr,sum)
  aswo <- tapply(llv$swo==0,llv$yrqtr,sum)
  ay <- 100*ay/a
  ab <- 100*ab/a
  aalb <- 100*aalb/a
  aswo <- 100*aswo/a
  maxy <- max(c(ay,ab,aswo,aalb))
  plot(names(ay),ay,ylim=c(0,maxy),xlab="Year",ylab="Proportion of zero catches",main=paste("Region",r))
  points(names(ab),ab,col=2,pch=2)
  points(names(aalb),aalb,col=3,pch=3)
  points(names(aswo),aswo,col=4,pch=4)
  }
legend("topright",legend=c("Yellowfin","Bigeye","Albacore","Swordfish"),col=c(1,2,3,4),pch=c(1,2,3,4))
savePlot(filename="Proportion zeroes by region by yrqtr allspp",type="png")

# Proportion sets with zero catches allspp by fishingcat
windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(0,0,3,0))
for (fc in 1:2) {
  for (r in 1:6) {
    llv <- op[op$reg==r & op$newfishingcat==fc,]
    ay <- tapply(llv$yft==0,llv$yrqtr,sum)
    a <- tapply(llv$hooks,llv$yrqtr,length)
    ab <- tapply(llv$bet==0,llv$yrqtr,sum)
    aalb <- tapply(llv$alb==0,llv$yrqtr,sum)
    aswo <- tapply(llv$swo==0,llv$yrqtr,sum)
    ay <- 100*ay/a
    ab <- 100*ab/a
    aalb <- 100*aalb/a
    aswo <- 100*aswo/a
    maxy <- max(c(ay,ab,aswo,aalb))
    plot(names(ay),ay,ylim=c(0,maxy),xlab="Year",ylab="Proportion of zero catches",main=paste("Region",r))
    points(names(ab),ab,col=2,pch=2)
    points(names(aalb),aalb,col=3,pch=3)
    points(names(aswo),aswo,col=4,pch=4)
    }
  legend("topright",legend=c("Yellowfin","Bigeye","Albacore","Swordfish"),col=c(1,2,3,4),pch=c(1,2,3,4))
  title(switch(fc,"Offshore","Distant water"),outer=T)
  savePlot(filename=paste("Proportion zeroes by region by yrqtr allspp",fc),type="png")
}

# Maps of effort through time
library(maps)
library(mapproj)
library(mapdata)
windows(width=14,height=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for(yr in seq(1950,2009,by=10)) {
  plot.pacific(plot_title=yr)
  a <- op[op$yrqtr > yr & op$yrqtr < yr+10,]
  lats <- sort(unique(a$lat5))
  lons <- sort(unique(a$lon5))
  a <- table(a$lat5,a$lon5)
  for (i in 1:15) {
    symbols(lons+2.5,rep(lats[i],length(lons))+2.5,circles=sqrt(a[i,])/40,col=1,add = T, inches =F)
    }
  }
savePlot("Plot map sets", type="png")

for (fc in 1:2) {
  windows(width=14,height=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(0,0,3,0))
  a2 <- op[op$newfishingcat==fc,]
  for(yr in seq(1950,2009,by=10)) {
    plot.pacific(plot_title=yr)
    a <- a2[a2$yrqtr > yr & a2$yrqtr < yr+10,]
    lats <- sort(unique(a$lat5))
    lons <- sort(unique(a$lon5))
    a <- table(a$lat5,a$lon5)
    for (i in 1:length(lats)) {
      symbols(lons+2.5,rep(lats[i],length(lons))+2.5,circles=sqrt(a[i,])/30,col=1,add = T, inches =F)
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T)
  savePlot(paste("Plot map sets",fc), type="png")
  }
 #   symbols(pp,rep(i,length(pp)),sqrt(pp2)/40, add = T, inches =FALSE)

# Maps of mean HPB through time
windows(width=14,height=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for(yr in seq(1950,2002,by=10)) {
  plot.pacific(plot_title=yr)
  a <- op[op$yrqtr > yr & op$yrqtr < yr+10,]
  lats <- sort(unique(a$lat5))
  lons <- sort(unique(a$lon5))
  a <- tapply(a$hbf,list(a$lat5,a$lon5),mean,na.rm=T)
  for (i in 1:15) {
    text(lons+2.5,rep(lats[i],length(lons))+2.5,floor(a[i,]),col=1,add = T, inches =F)
    }
  }
savePlot("Plot map mean HBF", type="png")

# Maps of mean HPB through time by FC
for (fc in 1:2) {
  windows(width=14,height=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(0,0,3,0))
  a2 <- op[op$newfishingcat==fc,]
  for(yr in seq(1950,2002,by=10)) {
    plot.pacific(plot_title=yr)
    a <- a2[a2$yrqtr > yr & a2$yrqtr < yr+10,]
    lats <- sort(unique(a$lat5))
    lons <- sort(unique(a$lon5))
    a <- tapply(a$hbf,list(a$lat5,a$lon5),mean,na.rm=T)
    for (i in 1:length(lats)) {
      text(lons+2.5,rep(lats[i],length(lons))+2.5,floor(a[i,]),col=1,add = T, inches =F)
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T)
  savePlot(paste("Plot map mean HBF",fc), type="png")
  }

# Maps of median HPB through time
windows(width=14,height=12); par(mfrow=c(3,2),mar=c(3,4,2,1))
for(yr in seq(1950,2002,by=10)) {
  plot.pacific(plot_title=yr)
  a <- op[op$yrqtr > yr & op$yrqtr < yr+10,]
  lats <- sort(unique(a$lat5))
  lons <- sort(unique(a$lon5))
  a <- tapply(a$hbf,list(a$lat5,a$lon5),median,na.rm=T)
  for (i in 1:length(lats)) {
    text(lons+2.5,rep(lats[i],length(lons))+2.5,(a[i,]),col=1)
    }
  }
savePlot("Plot map median HBF", type="png")

for (fc in 1:2) {
  windows(width=14,height=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(0,0,3,0))
  a2 <- op[op$newfishingcat==fc,]
  for(yr in seq(1950,2002,by=10)) {
    plot.pacific(plot_title=yr)
    a <- a2[a2$yrqtr > yr & a2$yrqtr < yr+10,]
    lats <- sort(unique(a$lat5))
    lons <- sort(unique(a$lon5))
    a <- tapply(a$hbf,list(a$lat5,a$lon5),median,na.rm=T)
    for (i in 1:length(lats)) {
      text(lons+2.5,rep(lats[i],length(lons))+2.5,(a[i,]),col=1)
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T)
  savePlot(paste("Plot map median HBF",fc), type="png")
  }

# Plot hbf by region by year
for (fc in c(1,2)){
  windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(1,1,3,1)); r<-3; hh <- 18
  b<- op[op$newfishingcat==fc,]
  for (r in 1:6) {
    a <- b[b$reg==r,]
    yrs <- sort(unique(a$op_yr))
    a <- table(a$hbf,a$op_yr)
    plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1950, 2010),main=paste("Region",r))
    ilist <- as.numeric(row.names(a))
    for(i in 1:length(ilist)){
      symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]) / 60, add = T, inches =FALSE)
      if(trunc(i/5) == i/5){
      lines(c(1950,2009), rep(i,2), lty=3)}
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T,cex=1)
  savePlot(paste("plot hbf by region by yr",switch(fc,"OS","DW")),type="png")
  }

# Plot sets proportion by line type by hbf by yr by region by fishing category
for (fc in c(1,2)){
  windows(height=14,width=12); par(mfrow=c(3,2),mar=c(3,4,2,1),oma=c(1,1,3,1)); r<-3; hh <- 18
  hbf_grps <- list(c(5:9),10:13, 14:17,18:19,20:25)
  a<- op[op$newfishingcat==fc,]
  for (r in 1:6) {
    plot(1,1,xlim=c(1994,2010),ylim=c(0,1),xlab="Year",ylab="Proportion Nylon",type="n",main=paste("Region",r))
    a1 <- a[a$reg==r,]
    for (hh in 1:5) {
      a2 <- a1[a1$hbf %in% hbf_grps[[hh]],]
      ayr <- floor(a2$op_yr/2)*2
      a2 <- table(a2$mainline,ayr)
      if(sum(a2)>1000) {
        prop <- a2[1,]/(a2[1,] + a2[2,])
        lines(as.numeric(names(prop)),prop,col=hh)
        }
      }
    }
  legend("bottomright",legend=hbf_grps,col=1:5,lty=1)
  title(switch(fc,"Offshore","Distant water"),outer=T,cex=1)
  savePlot(paste("plot prop sets by hbf region and yr",switch(fc,"OS","DW")),type="png")
  }

# Proportion zero by HBF by region by year
countzero <- function(a) {
  z <- sum(a==0)
  tot <- length(a)
  pz <- z/tot ; return(pz)
  }

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2))
for (r in 1:6) {
  a <- op[op$reg==r,]
  yrs <- sort(unique(a$op_yr))
  a <- tapply(a$bet,list(a$hbf,a$op_yr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
  ilist <- as.numeric(row.names(a))
  for(i in 1:length(ilist)){
    symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
    }
  }
savePlot("plot pzero bet by hbf by region by yr",type="png")

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2),oma=c(0,0,3,0))
for(fc in 1:2) {
  for (r in 1:6) {
    a <- op[op$reg==r & op$newfishingcat == fc,]
    yrs <- sort(unique(a$op_yr))
    a <- tapply(a$bet,list(a$hbf,a$op_yr),countzero)
    plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
    ilist <- as.numeric(row.names(a))
    for(i in 1:length(ilist)){
      symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T,cex=1)
  savePlot(paste("plot pzero bet by hbf by region by yr",fc),type="png")
}

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2))
for (r in 1:6) {
  a <- op[op$reg==r,]
  yrs <- sort(unique(a$op_yr))
  a <- tapply(a$yft,list(a$hbf,a$op_yr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
  ilist <- as.numeric(row.names(a))
  for(i in 1:length(ilist)){
    symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
    }
  }
savePlot("plot pzero yft by hbf by region by yr",type="png")

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2),oma=c(0,0,3,0))
for(fc in 1:2) {
  for (r in 1:6) {
    a <- op[op$reg==r & op$newfishingcat == fc,]
    yrs <- sort(unique(a$op_yr))
    a <- tapply(a$yft,list(a$hbf,a$op_yr),countzero)
    plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
    ilist <- as.numeric(row.names(a))
    for(i in 1:length(ilist)){
      symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T,cex=1)
  savePlot(paste("plot pzero yft by hbf by region by yr",fc),type="png")
}

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2))
for (r in 1:6) {
  a <- op[op$reg==r,]
  yrs <- sort(unique(a$op_yr))
  a <- tapply(a$alb,list(a$hbf,a$op_yr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
  ilist <- as.numeric(row.names(a))
  for(i in 1:length(ilist)){
    symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
    }
  }
savePlot("plot pzero alb by hbf by region by yr",type="png")

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2),oma=c(0,0,3,0))
for(fc in 1:2) {
  for (r in 1:6) {
    a <- op[op$reg==r & op$newfishingcat == fc,]
    yrs <- sort(unique(a$op_yr))
    a <- tapply(a$alb,list(a$hbf,a$op_yr),countzero)
    plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
    ilist <- as.numeric(row.names(a))
    for(i in 1:length(ilist)){
      symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T,cex=1)
  savePlot(paste("plot pzero alb by hbf by region by yr",fc),type="png")
}

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2))
for (r in 1:6) {
  a <- op[op$reg==r,]
  yrs <- sort(unique(a$op_yr))
  a <- tapply(a$swo,list(a$hbf,a$op_yr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
  ilist <- as.numeric(row.names(a))
  for(i in 1:length(ilist)){
    symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
    }
  }
savePlot("plot pzero swo by hbf by region by yr",type="png")

windows(height=14,width=12); par(mfrow=c(3,2),mar=c(4,4,2,2),oma=c(0,0,3,0))
for(fc in 1:2) {
  for (r in 1:6) {
    a <- op[op$reg==r & op$newfishingcat == fc,]
    yrs <- sort(unique(a$op_yr))
    a <- tapply(a$swo,list(a$hbf,a$op_yr),countzero)
    plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1952, 2010),main=paste("Region",r))
    ilist <- as.numeric(row.names(a))
    for(i in 1:length(ilist)){
      symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
      }
    }
  title(switch(fc,"Offshore","Distant water"),outer=T,cex=1)
  savePlot(paste("plot pzero swo by hbf by region by yr",fc),type="png")
}

# Proportion zero by HBF by subregion R3 by year
plot_pzero <- function(a,sp) {
  yrs <- sort(unique(a$op_yr))
  a <- tapply(a[,sp],list(a$hbf,a$op_yr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1950, 2010),main=paste("Subregion",r))
  ilist <- as.numeric(row.names(a))
  for(i in 1:length(ilist)){
    symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
    }
  }

windows(height=14,width=12); par(mfrow=c(2,2),mar=c(4,4,2,2))
for (r in c(3.1, 3.2, 3.3, 3.4)) {
  a <- op[op$subreg==r,]
  plot_pzero(a,"bet")
  }
savePlot("plot pzero bet by hbf by subreg R3 by yr",type="png")

windows(height=14,width=12); par(mfrow=c(2,2),mar=c(4,4,2,2))
for (r in c(3.1, 3.2, 3.3, 3.4)) {
  a <- op[op$subreg==r,]
  plot_pzero(a,"yft")
  }
savePlot("plot pzero yft by hbf by subreg R3 by yr",type="png")

# Proportion of zero catches by HBF and year
plot_pzero_ll <- function(a,sp,la,lo) {
 # if(la == -30) browser()
  yrs <- sort(unique(a$op_yr))
  a <- tapply(a[,sp],list(a$hbf,a$op_yr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,23), xlim=c(1955, 2010),main=paste(la+2.5,lo+2.5,sep=", "))
  ilist <- as.numeric(row.names(a))
  if(length(ilist) > 0) {
    for(i in 1:length(ilist)){
      symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,]), add = T, inches =FALSE)
      }
    }
  }
windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(130,165,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo),]
    if(dim(a)[[1]] >0)  plot_pzero_ll(a,"bet",la,lo) else plot(1,1,type="n")
    }
  }
savePlot("plot pzero bet by hbf by R3 latlong2",type="png")

windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(130,165,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo),]
    if(dim(a)[[1]] >0)  plot_pzero_ll(a,"yft",la,lo) else plot(1,1,type="n")
    }
  }
savePlot("plot pzero yft by hbf by R3 latlong2",type="png")

windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(130,165,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo),]
    if(dim(a)[[1]] >0)  plot_pzero_ll(a,"alb",la,lo) else plot(1,1,type="n")
    }
  }
savePlot("plot pzero alb by hbf by R3 latlong2",type="png")
#R4
windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(170,205,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo),]
    if(dim(a)[[1]] >0)  plot_pzero_ll(a,"bet",la,lo) else plot(1,1,type="n")
    }
  }
savePlot("plot pzero bet by hbf by R4 latlong2",type="png")

windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(170,205,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo),]
    if(dim(a)[[1]] >0)  plot_pzero_ll(a,"yft",la,lo) else plot(1,1,type="n")
    }
  }
savePlot("plot pzero yft by hbf by R4 latlong2",type="png")

windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(170,205,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo),]
    if(dim(a)[[1]] >0)  plot_pzero_ll(a,"alb",la,lo) else plot(1,1,type="n")
    }
  }
savePlot("plot pzero alb by hbf by R4 latlong2",type="png")

#R 1,2,5,6
pzero_hbf_sp_yq <- function(sp,laseq,loseq,fname,ti="") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_pzero_ll(a,sp,la,lo) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }
pzero_hbf_sp_yq("bet",laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot pzero bet by hbf by R1 latlong",ti="Bigeye Region 1")
pzero_hbf_sp_yq("yft",laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot pzero yft by hbf by R1 latlong",ti="Yellowfin Region 1")
pzero_hbf_sp_yq("alb",laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot pzero alb by hbf by R1 latlong",ti="Albacore Region 1")

pzero_hbf_sp_yq("bet",laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot pzero bet by hbf by R2 latlong",ti="Bigeye Region 2")
pzero_hbf_sp_yq("yft",laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot pzero yft by hbf by R2 latlong",ti="Yellowfin Region 2")
pzero_hbf_sp_yq("alb",laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot pzero alb by hbf by R2 latlong",ti="Albacore Region 2")

pzero_hbf_sp_yq("bet",laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot pzero bet by hbf by R5 latlong",ti="Bigeye Region 5")
pzero_hbf_sp_yq("yft",laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot pzero yft by hbf by R5 latlong",ti="Yellowfin Region 5")
pzero_hbf_sp_yq("alb",laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot pzero alb by hbf by R5 latlong",ti="Albacore Region 5")

pzero_hbf_sp_yq("bet",laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot pzero bet by hbf by R6 latlong",ti="Bigeye Region 6")
pzero_hbf_sp_yq("yft",laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot pzero yft by hbf by R6 latlong",ti="Yellowfin Region 6")
pzero_hbf_sp_yq("alb",laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot pzero alb by hbf by R6 latlong",ti="Albacore Region 6")

#R 1,2,3,4,5,6
load("alldatraw.RData")
dat <- dataclean(dat,allHBF=T)
dat <- dataprep(dat)
op <- dat[dat$reg > 0 & dat$reg <=6,c("tonnage","fishingcat","ncrew","target","mainline","branchline","op_yr","op_mon","op_day","lat","lon","hbf",
          "hooks","bet","yft","alb","lat5","lon5","reg","subreg","vessid","yrqtr","latlong","cstart_yr","cstart_mon","cstart_day")]

plot_hbf_5x5 <- function(a,la,lo) {
  yrs <- sort(unique(a$op_yr))
  a <- tapply(a$hooks,list(a$hbf,a$op_yr),sum)
  plot(1,1, type="n", xlab="Year", ylab="HBF", ylim = c(1,25), xlim=c(1955, 2010),main=paste(la+2.5,lo+2.5,sep=", "))
  ilist <- as.numeric(row.names(a))
  if(length(ilist) > 0) {
    for(i in 1:length(ilist)){
      symbols(yrs , rep(ilist[i], length(yrs)), sqrt(a[i,])/400, add = T, inches =F)
    }
  }
}
hbf_sp_yq <- function(sp,laseq,loseq,fname,ti="") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_hbf_5x5(a,la,lo) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }
hbf_sp_yq(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot hbf by R1 latlong",ti="HBF Region 1")
hbf_sp_yq(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot hbf by R2 latlong",ti="HBF Region 2")
hbf_sp_yq(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname="plot hbf by R3 latlong",ti="HBF Region 3")
hbf_sp_yq(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname="plot hbf by R4 latlong",ti="HBF Region 4")
hbf_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot hbf by R5 latlong",ti="HBF Region 5")
hbf_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot hbf by R6 latlong",ti="HBF Region 6")

op <- op[op$hbf>4,]
hbf_sp_yq(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot hbf noHBF4 R1 latlong noHBF4",ti="HBF Region 1")
hbf_sp_yq(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot hbf noHBF4 R2 latlong",ti="HBF Region 2")
hbf_sp_yq(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname="plot hbf noHBF4 R3 latlong",ti="HBF Region 3")
hbf_sp_yq(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname="plot hbf noHBF4 R4 latlong",ti="HBF Region 4")
hbf_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot hbf noHBF4 R5 latlong",ti="HBF Region 5")
hbf_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot hbf noHBF4 R6 latlong",ti="HBF Region 6")

load("op.RData")

# Proportion of zero catches
plot_pzero_both <- function(a,la,lo) {
  cx <- 0.8
  yrs <- sort(unique(a$yrqtr))
  alb <- tapply(a$alb,list(a$yrqtr),countzero)
  yft <- tapply(a$yft,list(a$yrqtr),countzero)
  bet <- tapply(a$bet,list(a$yrqtr),countzero)
#  swo <- tapply(a$swo,list(a$yrqtr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="p(zero catch)", ylim = c(0,1), xlim=c(1952, 2010),main=paste(la+2.5,", ",lo+2.5,sep=""))
  points(yrs, yft,cex=cx)
  points(yrs,bet,col=2,pch=2,cex=cx)
  points(yrs,alb,col=3,pch=3,cex=cx)
#  points(yrs,swo,col=4,pch=4,cex=cx)
  }
pzero_all_sp_yq <- function(laseq,loseq,fname,ti="") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_pzero_both(a,la,lo) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

pzero_all_sp_yq(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot pzero allsp by R1 latlong",ti="Probability of zero catch Region 1")
pzero_all_sp_yq(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot pzero allsp by R2 latlong",ti="Probability of zero catch Region 2")
pzero_all_sp_yq(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname="plot pzero allsp by R3 latlong",ti="Probability of zero catch Region 3")
pzero_all_sp_yq(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname="plot pzero allsp by R4 latlong",ti="Probability of zero catch Region 4")
pzero_all_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot pzero allsp by R5 latlong",ti="Probability of zero catch Region 5")
pzero_all_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot pzero allsp by R6 latlong",ti="Probability of zero catch Region 6")

# Albacore catch distribution histograms
write.csv(table(op$hbf,floor(op$yrqtr/5)*5,op$reg),file="hbf by region by 5 years.csv")
windows();par(mfrow=c(3,2))
for(i in 1:6) {
  a <- op[op$reg==i,]
  hist(a$alb,xlab="Albacore catch",xlim=c(0,250),main=paste("Region",i))
}
savePlot("hist alb by reg",type="png")

windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(130,165,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo,lo+5),]
    hist(a$alb,xlab="Albacore catch",xlim=c(0,250),main=paste(la,lo,sep="."))
    }
  }
savePlot("hist alb R3 latlong",type="png")
windows(height=14,width=24); par(mfrow=c(6,8),mar=c(2,2,1,0))
for (la in seq(15,-10,by=-5)) {
  for (lo in seq(170,205,by=5)) {
    a <- op[op$lat5==la & op$lon5 %in% c(lo,lo+5),]
    hist(a$alb,xlab="Albacore catch",xlim=c(0,250),main=paste(la,lo,sep="."))
    }
  }
savePlot("hist alb R4 latlong",type="png")

# Proportions of CPUE over 20 per 1000 hooks
count_over_n <- function(a,n) {
  z <- sum(a>n)
  tot <- length(a)
  pz <- z/tot ; return(pz)
  }
plot_pcpue <- function(a,la,lo,cpue) {
  cx<-0.9
  yrs <- sort(unique(a$yrqtr))
  alb <- tapply(a$alb/a$hooks,list(a$yrqtr),count_over_n,cpue/1000)
  yft <- tapply(a$yft/a$hooks,list(a$yrqtr),count_over_n,cpue/1000)
  bet <- tapply(a$bet/a$hooks,list(a$yrqtr),count_over_n,cpue/1000)
  plot(1,1, type="n", xlab="Year", ylab="p(zero catch)", ylim = c(0,1), xlim=c(1952, 2010),main=paste(la+2.5,lo+2.5,sep=", "))
  points(yrs,yft,col=1,pch=1,cex=cx)
  points(yrs,bet,col=2,pch=2,cex=cx)
  points(yrs, alb,col=3,pch=3,cex=cx)
  }
pcpue_all_sp_yq <- function(cpue,laseq,loseq,fname,ti="") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_pcpue(a,la,lo,cpue) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

pcpue_all_sp_yq(cpue=10,laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot pcpue allsp by R1 latlong",ti="Probability of cpue > 1/100 hooks Region 1")
pcpue_all_sp_yq(cpue=10,laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot pcpue allsp by R2 latlong",ti="Probability of cpue > 1/100 hooks Region 2")
pcpue_all_sp_yq(cpue=10,laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname="plot pcpue allsp by R3 latlong",ti="Probability of cpue > 1/100 hooks Region 3")
pcpue_all_sp_yq(cpue=10,laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname="plot pcpue allsp by R4 latlong",ti="Probability of cpue > 1/100 hooks Region 4")
pcpue_all_sp_yq(cpue=10,laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot pcpue allsp by R5 latlong",ti="Probability of cpue > 1/100 hooks Region 5")
pcpue_all_sp_yq(cpue=10,laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot pcpue allsp by R6 latlong",ti="Probability of cpue > 1/100 hooks Region 6")

# Catch
plot_catch <- function(a,la,lo) {
  cx <- 0.9
  yrs <- sort(unique(a$op_yr))
  yft <- tapply(a$yft,list(a$op_yr),sum)
  bet <- tapply(a$bet,list(a$op_yr),sum)
  alb <- tapply(a$alb,list(a$op_yr),sum)
  plot(1,1, type="n", xlab="Year", ylab="Catch (numbers)", ylim = c(0,100000), xlim=c(1952, 2010),main=paste(la+2.5,lo+2.5,sep=", "))
  lines(yrs,yft,col=1,cex=cx)
  lines(yrs,bet,col=2,lty=2,cex=cx)
  lines(yrs,alb,col=3,lty=3,cex=cx)
  }
catch_all_sp_yq <- function(laseq,loseq,fname,ti="") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_catch(a,la,lo) else plot(1,1,type="n")
      }
    }
  legend("topright",legend=c("Yellowfin","Bigeye","Albacore"),lty=c(1:3),col=c(1:3))
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

catch_all_sp_yq(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot catch allsp by R1 latlong",ti="Catch Region 1")
catch_all_sp_yq(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot catch allsp by R2 latlong",ti="Catch Region 2")
catch_all_sp_yq(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname="plot catch allsp by R3 latlong",ti="Catch Region 3")
catch_all_sp_yq(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname="plot catch allsp by R4 latlong",ti="Catch Region 4")
catch_all_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot catch allsp by R5 latlong",ti="Catch Region 5")
catch_all_sp_yq(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot catch allsp by R6 latlong",ti="Catch Region 6")

# Effort by fishing category
plot_effort_fc <- function(a,la,lo) {
  yrs <- sort(unique(a$op_yr))
  eff <- tapply(a$hooks,list(factor(a$newfishingcat,levels=c(1:2)),a$op_yr),sum,na.rm=T)
  os_eff <- eff[1,]
  dw_eff <- eff[2,]
  plot(1,1, type="n", xlab="Year", ylab="Effort (hooks)", ylim = c(0,5e6), xlim=c(1952, 2010),main=paste(la+2.5,lo+2.5,sep=", "))
  lines(yrs,os_eff,col=1,lty=1)
  lines(yrs,dw_eff,col=2,lty=2)
  }
effort_all_fc_yq <- function(laseq,loseq,fname,ti="") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_effort_fc(a,la,lo) else plot(1,1,type="n")
      }
    }
  legend("topright",legend=c("Offshore","Distant water"),lty=c(1,2),col=c(1,2))
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

effort_all_fc_yq(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname="plot effort by fc R1 latlong",ti="Effort Region 1")
effort_all_fc_yq(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname="plot effort by fc R2 latlong",ti="Effort Region 2")
effort_all_fc_yq(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname="plot effort by fc R3 latlong",ti="Effort Region 3")
effort_all_fc_yq(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname="plot effort by fc R4 latlong",ti="Effort Region 4")
effort_all_fc_yq(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname="plot effort by fc R5 latlong",ti="Effort Region 5")
effort_all_fc_yq(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname="plot effort by fc R6 latlong",ti="Effort Region 6")

# Median CPUE
plot_median_cpue <- function(a,la,lo) {
  cx=0.9
  yrs <- sort(unique(a$yrqtr))
  alb <- tapply(a$alb/a$hooks,list(a$yrqtr),median)
  yft <- tapply(a$yft/a$hooks,list(a$yrqtr),median)
  bet <- tapply(a$bet/a$hooks,list(a$yrqtr),median)
  plot(1,1, type="n", xlab="Year", ylab="p(zero catch)", ylim = c(0,0.04), xlim=c(1952, 2010),main=paste(la+2.5,lo+2.5,sep=", "))
  points(yrs,yft,col=1,pch=1,cex=cx)
  points(yrs,bet,col=2,pch=2,cex=cx)
  points(yrs, alb,col=3,pch=3,cex=cx)
  }
median_all_sp_yq_fc <- function(cpue,laseq,loseq,fname,ti="",fc="both") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  b <- b[b$newfishingcat %in% switch(fc,"both"=c(1,2),"OS"=1,"DW"=2),]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
#  browser()
      if(dim(a)[[1]] >0)  plot_median_cpue(a,la,lo) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

for(fcc in c("OS","DW","both")) {
  median_all_sp_yq_fc(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname=paste("plot median allsp by R1 latlong",fcc),ti=paste("Median cpue Region 1",fcc),fc=fcc)
  median_all_sp_yq_fc(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname=paste("plot median allsp by R2 latlong",fcc),ti=paste("Median cpue Region 2",fcc),fc=fcc)
  median_all_sp_yq_fc(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname=paste("plot median allsp by R3 latlong",fcc),ti=paste("Median cpue Region 3",fcc),fc=fcc)
  median_all_sp_yq_fc(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname=paste("plot median allsp by R4 latlong",fcc),ti=paste("Median cpue Region 4",fcc),fc=fcc)
  median_all_sp_yq_fc(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname=paste("plot median allsp by R5 latlong",fcc),ti=paste("Median cpue Region 5",fcc),fc=fcc)
  median_all_sp_yq_fc(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname=paste("plot median allsp by R6 latlong",fcc),ti=paste("Median cpue Region 6",fcc),fc=fcc)
}

# Mean CPUE
plot_mean_cpue <- function(a,la,lo) {
  cx=0.9
  yrs <- sort(unique(a$yrqtr))
  alb <- tapply(a$alb/a$hooks,list(a$yrqtr),mean)
  yft <- tapply(a$yft/a$hooks,list(a$yrqtr),mean)
  bet <- tapply(a$bet/a$hooks,list(a$yrqtr),mean)
  plot(1,1, type="n", xlab="Year", ylab="p(zero catch)", ylim = c(0,0.04), xlim=c(1952, 2010),main=paste(la+2.5,lo+2.5,sep=", "))
  points(yrs,yft,col=1,pch=1,cex=cx)
  points(yrs,bet,col=2,pch=2,cex=cx)
  points(yrs, alb,col=3,pch=3,cex=cx)
  }
mean_all_sp_yq_fc <- function(cpue,laseq,loseq,fname,ti="",fc="both") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  b <- b[b$newfishingcat %in% switch(fc,"both"=c(1,2),"OS"=1,"DW"=2),]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_mean_cpue(a,la,lo) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

for(fcc in c("OS","DW","both")) {
  mean_all_sp_yq_fc(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname=paste("plot mean allsp by R1 latlong",fcc),ti=paste("Mean cpue Region 1",fcc),fc=fcc)
  mean_all_sp_yq_fc(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname=paste("plot mean allsp by R2 latlong",fcc),ti=paste("Mean cpue Region 2",fcc),fc=fcc)
  mean_all_sp_yq_fc(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname=paste("plot mean allsp by R3 latlong",fcc),ti=paste("Mean cpue Region 3",fcc),fc=fcc)
  mean_all_sp_yq_fc(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname=paste("plot mean allsp by R4 latlong",fcc),ti=paste("Mean cpue Region 4",fcc),fc=fcc)
  mean_all_sp_yq_fc(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname=paste("plot mean allsp by R5 latlong",fcc),ti=paste("Mean cpue Region 5",fcc),fc=fcc)
  mean_all_sp_yq_fc(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname=paste("plot mean allsp by R6 latlong",fcc),ti=paste("Mean cpue Region 6",fcc),fc=fcc)
  graphics.off()
}

# SWO and SWO targeting
plot_median_cpue_swo <- function(a,la,lo) {
  cx=0.9
  yrs <- sort(unique(a$yrqtr))
  alb <- tapply(a$alb/a$hooks,list(a$yrqtr),median)
  yft <- tapply(a$yft/a$hooks,list(a$yrqtr),median)
  bet <- tapply(a$bet/a$hooks,list(a$yrqtr),median)
  swo <- tapply(a$swo/a$hooks,list(a$yrqtr),median)
  plot(1,1, type="n", xlab="Year", ylab="p(zero catch)",xlim=c(1952, 2010),ylim=c(0,.02),main=paste(la+2.5,lo+2.5,sep=", "))
  points(yrs,yft,col=1,pch=1,cex=cx)
  points(yrs,bet,col=2,pch=2,cex=cx)
#  points(yrs,alb,col=3,pch=3,cex=cx)
  points(yrs,swo,col=4,pch=4,cex=cx)
  }
median_all_spswo_yq_fc <- function(cpue,laseq,loseq,fname,ti="",fc="both") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  b <- b[b$newfishingcat %in% switch(fc,"both"=c(1,2),"OS"=1,"DW"=2),]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_median_cpue_swo(a,la,lo) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

for(fcc in c("OS","DW","both")) {
  median_all_spswo_yq_fc(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname=paste("plot median allspswo by R1 latlong",fcc),ti=paste("Median cpue Region 1",fcc),fc=fcc)
  median_all_spswo_yq_fc(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname=paste("plot median allspswo by R2 latlong",fcc),ti=paste("Median cpue Region 2",fcc),fc=fcc)
  median_all_spswo_yq_fc(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname=paste("plot median allspswo by R5 latlong",fcc),ti=paste("Median cpue Region 5",fcc),fc=fcc)
  median_all_spswo_yq_fc(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname=paste("plot median allspswo by R6 latlong",fcc),ti=paste("Median cpue Region 6",fcc),fc=fcc)
  graphics.off()
}

a <- op[op$lat5 %in% c(25,30) & op$lon5 %in% seq(130,165,5),]
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1),]
  plot(b$bet,b$swo,xlim=c(0,150),ylim=c(0,60),cex=0.6,xlab="Bigeye",ylab="Swordfish",main=paste(y,"-",y+4))
  text(140,50,cor(b$bet,b$swo))
  }
savePlot("plot R1 bet v swo sets",type="png")
a <- op[op$lat5 %in% c(25,30) & op$lon5 %in% seq(130,165,5),]
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1) & a$hbf %in% c(3,4),]
  plot(b$bet,b$swo,xlim=c(0,150),ylim=c(0,60),cex=0.6,xlab="Bigeye",ylab="Swordfish",main=paste(y,"-",y+4))
  text(140,50,cor(b$bet,b$swo))
  }
savePlot("plot R1 bet v swo sets HBF3_4",type="png")
a <- op[op$lat5 %in% c(25,30) & op$lon5 %in% seq(130,165,5),]
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1) & a$hbf >=5,]
  plot(b$bet,b$swo,xlim=c(0,150),ylim=c(0,60),cex=0.6,xlab="Bigeye",ylab="Swordfish",main=paste(y,"-",y+4))
  text(140,50,cor(b$bet,b$swo))
  }
savePlot("plot R1 bet v swo sets HBF5_up",type="png")
a <- op[op$lat5 %in% c(25,30) & op$lon5 %in% seq(180,205,5),]
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1),]
  plot(b$bet,b$swo,xlim=c(0,150),ylim=c(0,60),cex=0.6,xlab="Bigeye",ylab="Swordfish",main=paste(y,"-",y+4))
  text(140,50,cor(b$bet,b$swo))
  }
savePlot("plot R2east bet v swo sets",type="png")
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1),]
  plot(b$bet,b$alb,xlim=c(0,150),ylim=c(0,250),cex=0.6,xlab="Bigeye",ylab="Albacore",main=paste(y,"-",y+4))
  text(140,240,cor(b$bet,b$alb))
  }
savePlot("plot R2east bet v alb sets",type="png")
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1),]
  plot(b$alb,b$swo,xlim=c(0,250),ylim=c(0,60),cex=0.6,xlab="Albacore",ylab="Swordfish",main=paste(y,"-",y+4))
  text(200,50,cor(b$alb,b$swo))
  }
savePlot("plot R2east alb v swo sets",type="png")
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1),]
  hist(b$swo,breaks=c(0:9,seq(10,1000,5)),ylim=c(0,1),xlim=c(0,50),main=paste(y,"-",y+4))
  }
savePlot("plot R2east swo hist sets",type="png")
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1) & a$hbf<5,]
  plot(b$bet,b$swo,xlim=c(0,150),ylim=c(0,60),cex=0.6,xlab="Bigeye",ylab="Swordfish",main=paste(y,"-",y+4))
  }
savePlot("plot R2east bet v swo sets hbf3-4",type="png")
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1) & a$hbf>5,]
  plot(b$bet,b$swo,xlim=c(0,150),ylim=c(0,60),cex=0.6,xlab="Bigeye",ylab="Swordfish",main=paste(y,"-",y+4))
  }
savePlot("plot R2east bet v swo sets hbf 6-up",type="png")
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1),]
  hist(b$hooks,xlim=c(0,6000),main=paste(y,"-",y+4))
  }
savePlot("plot R2east hooks 5yr",type="png")
windows(height=14,width=24); par(mfrow=c(3,4))
for (y in seq(1950,2009,5)) {
  b <- a[a$op_yr %in% seq(y,y+4,1),]
  plot(b$hooks,b$swo,xlim=c(0,6000),cex=0.6,main=paste(y,"-",y+4))
  b2 <- tapply(b$swo,250*(floor(b$hooks/250)),mean)
  lines(as.numeric(names(b2)),b2,lwd=2,col=2)
  }
savePlot("plot R2east hooks v swo sets",type="png")
for(r in 1:6) {
  a <- op[op$reg==r,]
  windows(height=14,width=24); par(mfrow=c(3,4),oma=c(0,0,1,0))
  for (y in seq(1950,2009,5)) {
    b <- a[a$op_yr %in% seq(y,y+4,1),]
    hist(b$hooks,xlim=c(0,6000),breaks=seq(0,8000,100),main=paste(y,"-",y+4))
    }
  title(paste("Region",r),outer=T)
  savePlot(paste("plot hooks R",r,sep=""),type="png")
  }
for(fc in 1:2) {
  for(r in 1:6) {
    a <- op[op$reg==r & op$newfishingcat==fc,]
    windows(height=14,width=24); par(mfrow=c(3,4),oma=c(0,0,1,0))
    for (y in seq(1950,2009,5)) {
      b <- a[a$op_yr %in% seq(y,y+4,1),]
      hist(b$hooks,xlim=c(0,6000),breaks=seq(0,8000,100),main=paste(y,"-",y+4))
      }
    title(paste("Region",r,switch(fc,"OS","DW")),outer=T)
    savePlot(paste("plot hooks R",r,switch(fc," OS"," DW"),sep=""),type="png")
    }
  }
a <- op[op$lat5 %in% c(25,30) & op$lon5 %in% seq(185,205,5),]
a <- a[a$op_yr < 1970,]
table(a$latlong,100*a$op_yr + a$op_mon + a$op_day/100)

# Proportion of zero catches
plot_pzero_swo <- function(a,la,lo) {
  cx <- 0.8
  yrs <- sort(unique(a$yrqtr))
#  alb <- tapply(a$alb,list(a$yrqtr),countzero)
#  yft <- tapply(a$yft,list(a$yrqtr),countzero)
  bet <- tapply(a$bet,list(a$yrqtr),countzero)
  swo <- tapply(a$swo,list(a$yrqtr),countzero)
  plot(1,1, type="n", xlab="Year", ylab="p(zero catch)", ylim = c(0,1), xlim=c(1952, 2010),main=paste(la+2.5,", ",lo+2.5,sep=""))
#  points(yrs, yft,cex=cx)
  points(yrs,bet,col=2,pch=2,cex=cx)
#  points(yrs,alb,col=3,pch=3,cex=cx)
  points(yrs,swo,col=4,pch=4,cex=cx)
  }
pzero_fq_yq_swo <- function(laseq,loseq,fname,ti="",fc="both") {
  windows(height=14,width=24); par(mfrow=c(length(laseq),length(loseq)),mar=c(2,2,1,0),oma=c(1,1,4,1))
  b <- op[op$lat5 %in% laseq & op$lon5 %in% loseq,]
  b <- b[b$newfishingcat %in% switch(fc,"both"=c(1,2),"OS"=1,"DW"=2),]
  for (la in laseq) {
    for (lo in loseq) {
      a <- b[b$lat5==la & b$lon5 %in% c(lo),]
      if(dim(a)[[1]] >0)  plot_pzero_swo(a,la,lo) else plot(1,1,type="n")
      }
    }
  title(ti,outer=T)
  savePlot(fname,type="png")
  }

for(fcc in c("OS","DW","both")) {
  pzero_fq_yq_swo(laseq=seq(35,20,-5),loseq=seq(130,165,5),fname=paste("plot pzero allspswo by R1 latlong",fcc),ti=paste("Probability of zero catch Region 1",fcc),fc=fcc)
  pzero_fq_yq_swo(laseq=seq(35,20,-5),loseq=seq(170,205,5),fname=paste("plot pzero allspswo by R2 latlong",fcc),ti=paste("Probability of zero catch Region 2",fcc),fc=fcc)
#  pzero_fq_yq_swo(laseq=seq(15,-10,-5),loseq=seq(130,165,5),fname=paste("plot pzero allspswo by R3 latlong",fcc),ti=paste("Probability of zero catch Region 3",fcc),fc=fcc)
#  pzero_fq_yq_swo(laseq=seq(15,-10,-5),loseq=seq(170,205,5),fname=paste("plot pzero allspswo by R4 latlong",fcc),ti=paste("Probability of zero catch Region 4",fcc),fc=fcc)
  pzero_fq_yq_swo(laseq=seq(-15,-35,-5),loseq=seq(140,165,5),fname=paste("plot pzero allspswo by R5 latlong",fcc),ti=paste("Probability of zero catch Region 5",fcc),fc=fcc)
  pzero_fq_yq_swo(laseq=seq(-15,-35,-5),loseq=seq(170,205,5),fname=paste("plot pzero allspswo by R6 latlong",fcc),ti=paste("Probability of zero catch Region 6",fcc),fc=fcc)
  graphics.off()
}

setwd("D:/SimonHOYLE/AddDataforAlbacore/indices"); runsp="bet"; maxqtrs=200; addmain<-F; addbranch<-F;addother=F;addalb=T ;minqtrs_byreg = c(4,4,10,10,4,2)
for(runreg in c(1,2,3,4,5,6)) {
  minqtrs <- minqtrs_byreg[runreg]
  for(runsp in c("bet")) {
    fnamedelta <- paste(runsp,"_R",runreg," indices HBF10 ",minqtrs,"-",maxqtrs,"_qtrs ","deltabin",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    fnamepos <-   paste(runsp,"_R",runreg," indices HBF10 ",minqtrs,"-",maxqtrs,"_qtrs ","deltapos",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    combine_delta_xl_indices(fnamedelta,fnamepos)
    }
  }

setwd("D:/SimonHOYLE/AddDataforAlbacore/albcat"); runsp="bet"; maxqtrs=200; addmain<-F; addbranch<-F;addother=F;addalb=T ;maxqtrs=200; minqtrs_byreg = c(2,2,8,8,2,2,2,8)
for(runreg in c(1,2,3,4,5,6)) {
  minqtrs <- minqtrs_byreg[runreg]
  for(runsp in c("bet")) {
    fnamedelta <- paste(runsp,"_R",runreg,"eq ",minqtrs,"-",maxqtrs,"_qtrs ","deltabin",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    fnamepos <-   paste(runsp,"_R",runreg,"eq ",minqtrs,"-",maxqtrs,"_qtrs ","deltapos",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    combine_delta_xl(fnamedelta,fnamepos)
    }
  }
for(runreg in c(3)) {
  minqtrs <- 9
  for(runsp in c("bet")) {
    fnamedelta <- paste(runsp,"_R",runreg,"all ",minqtrs,"-",maxqtrs,"_qtrs ","deltabin",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    fnamepos <-   paste(runsp,"_R",runreg,"all ",minqtrs,"-",maxqtrs,"_qtrs ","deltapos",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    combine_delta_xl(fnamedelta,fnamepos)
    }
  }

setwd("D:/SimonHOYLE/AddDataforAlbacore/HBF10"); runsp="bet"; maxqtrs=200; addmain<-F; addbranch<-F;addother=F;addalb=F ;maxqtrs=200; minqtrs_byreg = c(2,2,8,8,2,2,2,8)
for(runreg in c(1,2,3,4,5,6)) {
  minqtrs <- minqtrs_byreg[runreg]
  for(runsp in c("bet")) {
    fnamedelta <- paste(runsp,"_R",runreg,"eq HBF10 ",minqtrs,"-",maxqtrs,"_qtrs ","deltabin",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat","")," (2)",sep="")
    fnamepos <-   paste(runsp,"_R",runreg,"eq HBF10 ",minqtrs,"-",maxqtrs,"_qtrs ","deltapos",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat","")," (2)",sep="")
    combine_delta_xl(fnamedelta,fnamepos)
    }
  }
for(runreg in c(3)) {
  minqtrs <- 9
  for(runsp in c("bet")) {
    fnamedelta <- paste(runsp,"_R",runreg,"all HBF10 ",minqtrs,"-",maxqtrs,"_qtrs ","deltabin",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    fnamepos <-   paste(runsp,"_R",runreg,"all HBF10 ",minqtrs,"-",maxqtrs,"_qtrs ","deltapos",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    combine_delta_xl(fnamedelta,fnamepos)
    }
  }

setwd("D:/SimonHOYLE/AddDataforAlbacore/std_eq_and_all"); runsp="bet"; maxqtrs=200; addmain<-F; addbranch<-F;addother=F;addalb=F ;maxqtrs=200; minqtrs_byreg = c(2,2,8,8,2,2,2,8)
for(runreg in c(1,2,3,4,5,6)) {
  minqtrs <- minqtrs_byreg[runreg]
  for(runsp in c("bet")) {
    fnamedelta <- paste(runsp,"_R",runreg,"eq ",minqtrs,"-",maxqtrs,"_qtrs ","deltabin",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    fnamepos <-   paste(runsp,"_R",runreg,"eq ",minqtrs,"-",maxqtrs,"_qtrs ","deltapos",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    combine_delta_xl(fnamedelta,fnamepos)
    }
  }
for(runreg in c(3)) {
  minqtrs <- 9
  for(runsp in c("bet")) {
    fnamedelta <- paste(runsp,"_R",runreg,"all ",minqtrs,"-",maxqtrs,"_qtrs ","deltabin",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    fnamepos <-   paste(runsp,"_R",runreg,"all ",minqtrs,"-",maxqtrs,"_qtrs ","deltapos",ifelse(addother,"_othersp",""),ifelse(addalb,"_albcat",""),sep="")
    combine_delta_xl(fnamedelta,fnamepos)
    }
  }


library(GEOquery)
library(pheatmap)

# get the data from GEO ( GSE matrix & annotation GPL)
# define destination to save data, SO didn't need load data each time
series = "GSE34670"
platform = "GPL96"
gset = getGEO(series, GSEMatrix =TRUE, AnnotGPL=TRUE, destdir = "Data/")
# if the data have several platforms, choose the certain platform
if (length(gset) > 1) idx <- grep(platform, attr(gset, "names")) else idx <- 1
gset = gset[[idx]]

# define groups for samples
gr = c(rep("cALL_BM", 5) , "cALL_PB" ,rep("cALL_BM", 19), "CL_cALL2" , "CL_697" , "CL_NALM6" , rep("CD10", 6), rep("CD10_pool" , 3) )
# elicit expression matrix from data
ex = exprs(gset)

# log2 transformation
ex = log2(ex + 1 )
exprs(gset) = ex

#correlation Heatmap
pdf("Results/corheatmap.pdf", width = 15 , height = 15)
pheatmap(cor(ex), labels_row = gr , labels_col = gr)
dev.off()


a called
a(1) || b(1) -> TRUE

a called
b called
a(1) && b(1) -> TRUE

a called
b called
a(0) || b(1) -> TRUE

a called
a(0) && b(1) -> FALSE

a called
a(1) || b(0) -> TRUE

a called
b called
a(1) && b(0) -> FALSE

a called
b called
a(0) || b(0) -> FALSE

a called
a(0) && b(0) -> FALSE


# A <- c(393.979,	419.038)
# B <- c(366.358, 550.138)

# st_coords = A; target_coords = B; sd_Hz = 3; e_cent_from = 1; e_cent_to = 1; e_focus = 0; Hz0 = 0; ng = 1; faces = "mean"; sd_sd_Hz = 0.3; sd_r = 1; seed = NULL; type = "dms"; axes = c("Northing", "Easting")

sim_Hz <- function(st_coords, target_coords, sd_Hz, e_cent_from = 0, e_cent_to = 0, e_focus = 0, Hz0 = 0, ng = 1, faces = list("mean", "f1f2" ,"face1", "face2"), sd_sd_Hz = 0.3, sd_r = 1, seed = NULL, type = list("dms", "dec", "rad"), axes = c("Northing", "Easting")){
  ## check if the axis were set:
  if(length(axes) < 2) stop("axes must be defined with Easting and Northing")
  if(!any(axes %in% list("Northing", "Easting"))){ stop(paste(type, "axes must be Northing and Easting.")) }

  Northing_ind <- which(axes == "Northing")
  Easting_ind <- which(axes == "Easting")

  ## check if the type exists:
  if(length(type) > 1){ type <- type[[1]] }
  if(!any(type %in% list("dms", "dec", "rad"))){ stop(paste(type, "method not available.")) }

  d <- dist(pt1_coords = st_coords, pt2_coords = target_coords)
  ro <- 180/pi*3600

  if(is.null(seed)){seed = runif(1,0,100)}

  # set.seed(seed)
  # e_cent_from <- abs(runif(1, 0, e_cent_from))

    st_coords_e <- ecent_coords(pt_coords = st_coords, e_cent = e_cent_from, seed = runif(1,0,100))
    e_cent_from <- dist(pt1_coords = st_coords, pt2_coords = st_coords_e)*1000
    sd_e_cent_from <- sqrt((e_cent_from^2*ro^2)/(2*(d*1000)^2))

  # set.seed(seed)
  # e_cent_to <- abs(runif(1, 0, e_cent_to))

    target_coords_e <- ecent_coords(pt_coords = target_coords, e_cent = e_cent_to, seed = runif(1,0,100)+1)
    e_cent_to <- dist(pt1_coords = target_coords, pt2_coords = target_coords_e)*1000
    sd_e_cent_to <- sqrt((e_cent_to^2*ro^2)/(2*(d*1000)^2)) #Use units lookup table!!

  ni1 <- ni(pt1_coords = st_coords, pt2_coords = target_coords, type = "dec")
  z <- 360-Hz0

  ## check if the type exists:
  if(length(type) > 1){type <- type[[1]]}
  if(!any(type %in% list("dms", "dec", "rad"))){ stop(paste(type, "method not available.")) }

  if(ni1 >= Hz0){
    Hz <- z+ni1-360
  }else{
    Hz <- z+ni1
  }

  #set.seed(seed)
  sd_hz <- sqrt(2*(sd_r^2+e_focus^2+sd_Hz^2)/ng + sd_e_cent_from^2+sd_e_cent_to^2)
  #sd_hz <- rnorm(1, sd_hz, sd_sd_Hz)/3600

  # sd_hz_rand <- 0
  # while(abs(sd_hz_rand) < sd_hz | abs(sd_hz_rand) > 2*sd_hz){
  #   sd_hz_rand <- rnorm(1, 0, sd_Hz)
  # }
  # sd_hz <- sd_hz_rand/3600
  #sd_hz <- rnorm(1, sd_hz, sd_sd_Hz)/3600

  sd_hz <- rnorm(1, 0, sd_Hz) #rnorm(1, sd_hz, sd_sd_Hz)/3600
  #sd_hz <- mean((sd_hz[abs(sd_hz < 0+2*sd_Hz) & abs(sd_hz > 0+1*sd_Hz)]))

  Hz <- Hz + sd_hz/3600 #sample(c(-1,1), 1)*sd_hz #+ e_cent_from/3600 + e_cent_to/3600

  if(type == "dms"){
    HzD <- floor(Hz); HzM <- floor((Hz-HzD)*60); HzS <- ((Hz-HzD)*60-HzM)*60
    Hz <- c(HzD, HzM, HzS)
    names(Hz) <- c("deg","min","sec")
    return(round(Hz))
  }
  if(type == "rad"){
    Hz <- Hz*pi/180
  }
  return(Hz)
}

rand_coords <- function(snet.points, sp){
  sp <- sp/1000
  mu <- snet.points %>% st_drop_geometry() %>% .[, c("x","y")] %>% as.matrix()
  sigma <- matrix(c(sp^2, 0, 0, sp^2),2)
  for(i in 1:dim(mu)[1]){
    mu[i, ] <- mvrnorm(1, mu[i, ], sigma)
  }
  return(mu)
}

# survey.net.st <- survey.net[[2]] %>% filter(from == "PP2")

# survey.net.st %>% st_drop_geometry() %>% .[1, c("x_from", "y_from")] %>% as.numeric()

#sim_face_hz(hz = a[1]+a[2]/60+a[3]/3600, sd_Hz = 15, e_colim = 10, sd_e_colim = 1, faces = "f1f2", sd_

# survey.net <- dns
# e_cent_from = 1
# e_cent_to = 1

# pt1_coords = as.numeric(observations[i, c("x_from", "y_from")]); pt2_coords = as.numeric(observations[i, c("x_to", "y_to")]); e_cent_from = as.numeric(observations[i, "e_cent_from"]); e_cent_to = as.numeric(observations[i, "e_cent_to"]); sd_dist = as.numeric(observations[i, "sd_dist"]); seed = NULL

#set.seed(seed)
#rnorm(1, 0, sd_dist)

#survey.net = mreza; red = TRUE;red = TRUE; Hz0 = NA; seed = NULL

sim_dist_all <- function(obs_d, points, e_cent_from = c(1, 1, 1, 1), e_cent_to = c(1, 1, 1, 1), sd_dist = c(3, 3, 3, 3), seed = c(111, 222, 333, 444)){

  if(length(e_cent_from) == 1) e_cent_from <- rep(e_cent_from, length(unique(obs_d$station)))
  if(length(e_cent_from) != length(unique(obs_d$station))) stop("e_cent_from must be of length either 1 or the number of stations")

  if(length(e_cent_to) == 1) e_cent_to <- rep(e_cent_to, length(unique(obs_d$station)))
  if(length(e_cent_to) != length(unique(obs_d$station))) stop("e_cent_to must be of length either 1 or the number of stations")

  if(length(sd_dist) == 1) sd_dist <- rep(sd_dist, length(unique(obs_d$station)))
  #if(length(sd_dist) != length(unique(obs_d$station))) stop("sd_dist must be of length either 1 or the number of stations")

  if(!is.null(seed)){
    if(length(seed) == 1) seed <- rep(seed, length(unique(obs_d$station)))
    if(length(seed) != length(unique(obs_d$station))) stop("seed must be of length either 1 or the number of stations")
  }else{
    seed <- sample(1:1000, size = length(unique(obs_d$station))) # rep(NULL, length(unique(obs_d$station)))
  }

  obs_d <- mutate(obs_d, x_station = rep(NA, dim(obs_d)[1]), y_station = rep(NA, dim(obs_d)[1]), x_target = rep(NA, dim(obs_d)[1]), y_target = rep(NA, dim(obs_d)[1]))
  for(i in 1:dim(obs_d)[1]){
    obs_d[i , c("x_station", "y_station")] <- points[which(obs_d$station[i] == points$Name), c("x", "y")]
    obs_d[i , c("x_target", "y_target")] <- points[which(obs_d$obs.point[i] == points$Name), c("x", "y")]
  }

  obs.d.list <- split(obs_d, obs_d$station)
  for(i in 1:length(obs.d.list)){
    obs.d.list[[i]]$e_cent_from <- rep(e_cent_from[i], dim(obs.d.list[[i]])[1])
    obs.d.list[[i]]$e_cent_to <- rep(e_cent_to[i], dim(obs.d.list[[i]])[1])
    obs.d.list[[i]]$sd_dist <- rep(sd_dist[i], dim(obs.d.list[[i]])[1])
    obs.d.list[[i]]$seed <- rep(seed[i], dim(obs.d.list[[i]])[1])
  }

  meas.d.list <- as.list(rep(NA, length(obs.d.list)))
  if(!is.null(seed)){
    for(i in 1:length(obs.d.list)){
      meas.d.list[[i]] <- data.frame(dist = (apply(obs.d.list[[i]], 1, function(x) sim_dist(st_coords = as.numeric(x[c(3:4)]), target_coords = as.numeric(x[c(5:6)]), e_cent = as.numeric(x[7]), e_cent_to = as.numeric(x[8]), sd_dist = as.numeric(x[9]), seed = as.numeric(x[10])))))
    }
  }else{
    for(i in 1:length(obs.d.list)){
      meas.d.list[[i]] <- data.frame(dist = (apply(obs.d.list[[i]], 1, function(x) sim_dist(st_coords = as.numeric(x[c(3:4)]), target_coords = as.numeric(x[c(5:6)]), e_cent = as.numeric(x[7]), e_cent_to = as.numeric(x[8]), sd_dist = as.numeric(x[9])))))
    }
  }

  d_meas <- cbind(do.call(rbind, obs.d.list), do.call(rbind, meas.d.list)) %>% select(station, obs.point, dist)

  return(d_meas)
}

#points = B.survey.net[[1]]; obs.plan = B_sim_obs; sd_Hz = 5; e_cent_from = 1; sd_dist = 3; e_cent_to = 1; Hz0 = NA

sim.obs <- function(points, obs.plan, Hz0 = NA, red = TRUE, sd_Hz = 10, sd_dist = 3, e_cent_from = 2, e_cent_to = 3, seed = NULL){
  obs.dist <- filter(obs.plan, type == "d") %>% select(1,2) # Selekcija merenih duzina
  sim.dist <- sim_dist_all(obs_d = obs.dist, points = points, e_cent_from = e_cent_from, e_cent_to = e_cent_to, seed = seed)

  obs.Hz <- filter(obs.plan, type == "p") %>% select(1,2) # Selekcija merenih pravaca
  sim.Hz <- sim_Hz_all(obs_Hz = obs.Hz, points = points, Hz0 = Hz0, red = red, e_cent_from = e_cent_from, e_cent_to = e_cent_to, sd_Hz = sd_Hz, seed = seed)

  obs <- dplyr::full_join(sim.Hz, sim.dist) %>%
    mutate(sd_Hz = sd_Hz, sd_dist = sd_dist, SD = NA, VzD = NA, VzM = NA, VzS = NA, sd_Vz = NA) %>%
    dplyr::select(from = station, to = obs.point, HzD = deg, HzM = minut, HzS = sec, HD = dist, SD, VzD, VzM, VzS, sd_Hz, sd_dist, sd_Vz)
  obs.list <- list(Points = points, Observations = obs)
  return(obs.list)
}

sim_ang <- function(st_coords, orient_coords, target_coords, seed, Hz0 = 0, sd_Hz = 5, c =  10, e_cent = 2, e_cent_back = 2, e_cent_to = 0, type = list("dms", "dec", "rad"), axes = c("Easting", "Northing")){
  ## check if the axis were set:
  if(length(axes) < 2) stop("axes must be defined with Easting and Northing")
  if(!any(axes %in% list("Northing", "Easting"))){ stop(paste(type, "axes must be Northing and Easting.")) }

  ## check if the type exists:
  if(length(type) > 1){ type <- type[[1]] }
  if(!any(type %in% list("dms", "dec", "rad"))){ stop(paste(type, "method not available.")) }

  Hz1 <- sim_Hz(st_coords = st_coords, target_coords =  target_coords, Hz0 = Hz0, e_cent = e_cent, sd_Hz = sd_Hz, e_cent_to = e_cent_to, type = "dec", seed = seed, axes = c("Northing", "Easting"))
  Hz2 <- sim_Hz(st_coords = st_coords, target_coords =  orient_coords, Hz0 = Hz0, e_cent = e_cent, sd_Hz = sd_Hz, e_cent_to = e_cent_back, type = "dec", seed = seed, axes = c("Northing", "Easting"))

  # ni1 <- ni(pt1_coords = st_coords, pt2_coords = target_coords, type = "dec", axes = c("Northing", "Easting"))
  # ni2 <- ni(pt1_coords = st_coords, pt2_coords = orient_coords, type = "dec", axes = c("Northing", "Easting"))

  dang <- as.numeric(Hz1-Hz2)
  dang <- ifelse(dang < 0, 360+dang, dang)

  if(type == "dms"){
    deg <- floor(dang); minut <- floor((dang-deg)*60); sec <- ((dang-deg)*60-minut)*60
    dang <- c(deg, minut, sec)
    names(dang) <- c("deg","min","sec")
  }
  if(type == "rad"){
    dang <- dang*pi/180
  }

  return(dang)
}

station_Hz <- function(station, target_points, Hz0 = 0, e_cent_from = 2, sd_Hz = 5, e_colim =  10, sd_e_colim = 5, e_cent_to = NULL, e_focus = NULL, seed = NULL, faces = list("mean", "f1f2" ,"face1", "face2"), type = list("dms", "dec", "rad"), axes = c("Northing", "Easting")){

  target_points %>% group_by(Name) %>% Hz(st_coords = station, target_coords =  (target_points[,c(2,3)]), Hz0 = Hz0, e_cent = e_cent, sd_Hz = sd_Hz, e_cent_to = e_cent_to, type = "dec", seed = seed, axes = c("Northing", "Easting"))

  t(apply(target_points, 1, function(x) Hz(st_coords = station, target_coords = as.numeric(x[c(2:3)]), Hz0 = 270, e_cent = 2, sd_Hz = 5, e_cent_to = 2, type = "dms", seed = 111, axes = c("Northing", "Easting"))))

}

sim_Hz_all <- function(survey.net, red = TRUE, Hz0 = NA, seed = NULL, type = list("dms", "dec", "rad")){

  obs_Hz <- survey.net[[2]] %>% dplyr::filter(direction) %>% dplyr::select(from, to, HzD, HzM, HzS, sd_Hz, x_from, y_from, x_to, y_to, e_cent_from,	e_cent_to,	e_focus) %>% dplyr::mutate_at(., .vars = c("e_cent_from", "e_cent_to", "e_focus"), ~replace(., is.na(.), 0))

  if(is.na(Hz0)) {Hz0 <- sample(1:359, size = length(unique(obs_Hz$from)))}else{
    if(length(Hz0) != length(unique(obs_Hz$from))) stop("Hz0 must be of length either 1 or number of stations")
  }

  if(!is.null(seed)){
    if(length(seed) == 1) seed <- rep(seed, length(unique(obs_Hz$from)))
    if(length(seed) != length(unique(obs_Hz$from))) stop("seed must be of length either 1 or the number of stations")
  }else{
    seed <- runif(length(unique(obs_Hz$from)),0,100)
  }

  # TODO: Replace NA with ZEROS for cent errors.

  obs.Hz.list <- split(obs_Hz, obs_Hz$from)
  for(i in 1:length(obs.Hz.list)){
    obs.Hz.list[[i]]$Hz0 <- rep(Hz0[i], dim(obs.Hz.list[[i]])[1])
    obs.Hz.list[[i]]$seed <- rep(seed[i], dim(obs.Hz.list[[i]])[1])
    obs.Hz.list[[i]]$Hz <- rep(NA, dim(obs.Hz.list[[i]])[1])
  }

  for(i in 1:length(obs.Hz.list)){
    obs.Hz.list[[i]]$Hz <- as.numeric(t(apply(obs.Hz.list[[i]], 1, function(x) sim_Hz(st_coords = as.numeric(x[c("x_from", "y_from")]), target_coords = as.numeric(x[c("x_to", "y_to")]), Hz0 = as.numeric(x["Hz0"]), e_cent_from = as.numeric(x["e_cent_from"]), e_cent_to = as.numeric(x["e_cent_to"]), sd_Hz = as.numeric(x["sd_Hz"]), type = "dec", seed = as.numeric(x["seed"]), axes = c("Northing", "Easting")))))
  }

  if(red){
    obs_Hz <- obs.Hz.list %>% lapply(., function(x) dplyr::mutate(x, Hz = Hz - Hz[1])) %>% lapply(., function(x) dplyr::mutate(x, Hz = case_when(Hz < 0 ~ Hz + 360, TRUE ~ Hz))) %>% do.call(rbind,.)
  }
  obs_Hz <- obs_Hz %>% mutate(HzD = floor(Hz), HzM = floor((Hz-HzD)*60), HzS = ((Hz-HzD)*60-HzM)*60) %>%
    select(from, to, HzD, HzM, HzS, sd_Hz, geometry) %>%
    sf::st_drop_geometry() %>%
    dplyr::select(from, to, HzD, HzM, HzS) %>%
    left_join(dplyr::select(survey.net[[2]], -c("HzD", "HzM", "HzS")), .) %>%
    dplyr::select(from, to, HzD, HzM, HzS, everything())

  obs_dist <- survey.net[[2]] %>% dplyr::filter(distance) %>% dplyr::select(from, to, sd_dist, x_from, y_from, x_to, y_to, e_cent_from, e_cent_to, e_focus) %>% dplyr::mutate_at(., .vars = c("e_cent_from", "e_cent_to", "e_focus"), ~replace(., is.na(.), 0))

  obs.dist.list <- split(obs_dist, obs_dist$from)
  for(i in 1:length(obs.dist.list)){
    obs.dist.list[[i]]$seed <- rep(seed[i], dim(obs.dist.list[[i]])[1])
    obs.dist.list[[i]]$HD_sim <- rep(NA, dim(obs.dist.list[[i]])[1])
  }

  obs.dist.list <- obs.dist.list %>% lapply(., function(x) rowwise(x) %>% dplyr::mutate(., HD = dist(pt1_coords = c(x_from, y_from), pt2_coords = c(x_to, y_to))) %>% ungroup()) %>% do.call(rbind, .) %>% dplyr::select(from, to, HD, sd_dist)

  aa <- inner_join(obs.Hz.list, obs.dist.list)
  # Ovo Je super!!! TODO: Simulacija duzina nije zavrsena!
}

sim_dist <- function(st_coords, target_coords, e_cent_from, e_cent_to, sd_dist = 3, seed = NULL,  sd_e_cent_from = 0.5, sd_e_cent_to = 0.5, sd_sd_dist = 0.2, axes = c("Easting", "Northing")){
  ## check if the axis were set:
  if(length(axes) < 2) stop("axes must be defined with Easting and Northing")
  if(!any(axes %in% list("Northing", "Easting"))){ stop(paste(type, "axes must be Northing and Easting.")) }

  # if(!is.null(e_cent_from)){
  #   set.seed(seed)
  #   e_cent_from <- abs(runif(1, 0, e_cent_from))
  #   e_cent_from <- sqrt(e_cent_from^2/2)
  # }
  # if(!is.null(e_cent_to)){
  #   set.seed(seed)
  #   e_cent_to <- abs(runif(1, 0, e_cent_to))
  #   e_cent_to <- sqrt(e_cent_to^2/2
  # })

  if(is.null(seed)){seed = runif(1,0,100)}

  st_coords_e <- ecent_coords(pt_coords = st_coords, e_cent = e_cent_from, seed = seed)
  e_cent_from <- dist(pt1_coords = st_coords, pt2_coords = st_coords_e)*1000
  sd_e_cent_from <-  sqrt(e_cent_from^2/2)

  set.seed(seed)
  target_coords_e <- ecent_coords(pt_coords = target_coords, e_cent = e_cent_to, seed = seed+1)
  e_cent_to <- dist(pt1_coords = target_coords, pt2_coords = target_coords_e)*1000
  sd_e_cent_to <- sqrt(e_cent_to^2/2)

  distance = dist(pt1_coords = st_coords_e, pt2_coords = target_coords_e)

  sd_dist <- sqrt(sd_dist^2 + sd_e_cent_from^2 + sd_e_cent_from^2)

  # sd_dist_rand <- 0
  # while(abs(sd_dist_rand) < sd_dist | abs(sd_dist_rand) > 3*sd_dist){
  #   sd_dist_rand <- rnorm(1, 0, sd_dist)
  # }
  # sd_dist <- sd_dist_rand/1000

  sd_dist <-  rnorm(1, 0, sd_dist) # rnorm(1, sd_dist, sd_sd_dist)
  #set.seed(seed)
  distance <- distance + sd_dist/1000 #sample(c(-1,1),1)*sd_dist/1000
  return(distance)
}

observations <- sf::st_drop_geometry(survey.net[[2]])
for(i in 1:dim(observations)[1]){
  if(observations$direction[i]){
    observations$Hz_sim[i] <- sim_Hz(st_coords = as.numeric(observations[i, c("x_from", "y_from")]), target_coords = as.numeric(observations[i, c("x_to", "y_to")]), Hz0 = as.numeric(observations[i, "Hz0"]), e_cent_from = as.numeric(observations[i, "e_cent_from"]), e_cent_to = as.numeric(observations[i, "e_cent_to"]), sd_Hz = as.numeric(observations[i, "sd_Hz"]), type = "dec", seed = as.numeric(observations[i, "seed"]), axes = c("Northing", "Easting"))
  }
  if(observations$distance[i]){
    observations$dist_sim[i] <- sim_dist(st_coords = as.numeric(observations[i, c("x_from", "y_from")]), target_coords = as.numeric(observations[i, c("x_to", "y_to")]), e_cent_from = as.numeric(observations[i, "e_cent_from"]), e_cent_to = as.numeric(observations[i, "e_cent_to"]), sd_dist = as.numeric(observations[i, "sd_dist"]), seed = as.numeric(observations[i, "seed"]))
  }
}

if(red){
  observations <- observations %>% dplyr::group_by(from) %>% dplyr::mutate_at(., .vars = "Hz_sim", ~Hz_sim - Hz_sim[1]) %>% dplyr::mutate(Hz_sim = dplyr::case_when(Hz_sim < 0 ~ Hz_sim + 360, Hz_sim >= 0 ~ Hz_sim)) %>% dplyr::group_by(from) %>% dplyr::arrange(ID, Hz_sim) %>% dplyr::ungroup()
}

survey.net[[2]] <- observations %>%
  dplyr::mutate(HzD = floor(Hz_sim), HzM = floor((Hz_sim-HzD)*60), HzS = ((Hz_sim-HzD)*60-HzM)*60, HD = dist_sim, Hz = Hz_sim) %>%
  dplyr::select(ID:diff_level) %>%
  left_join(., dplyr::select(survey.net[[2]], from, to), by = c("from", "to")) %>%
  as.data.frame() %>%
  st_sf(., crs = prj)


# Estimates phylogentic species variability (PSV) across a global grid of 2x2 (resolution as in the original estimation of the presence absence matrix).
# The code also estimates spatial null models for PSV by shuffling the PAM in two ways:
# - NULL 1 randomly shuffles occurrences within sites (i.e., per-row shuffling), keeping the observed species richness.
# - NULL 2 randomly shuffles occurrences within families (i.e., per-column shuffling), keeping the observed family prevalence.
# PSV is estimated on a dated phylogeny that is pruned to includes a sinlge tip per family.
# Continues with R objects generated in previous steps.

tre.pruned <- drop.tip(tre,tip=list[which(duplicated(list_fams))])
tre.pruned<- ladderize(tre.pruned, FALSE)
list <- tre.pruned$tip.label
list_outs <- foreach(i=1:length(list),.combine=c) %do% {strsplit(list,"_")[[i]][2]}
outs<-c("Welwitschiaceae","Pinaceae","Ginkgoaceae","Cupressaceae","Gnetaceae")
tre.pruned <- drop.tip(tre.pruned,tip=tre.pruned$tip.label[which(list_outs%in%outs)])
list<-tre.pruned$tip.label
list <- foreach(i=1:length(list),.combine=c) %do% {strsplit(list,"_")[[i]][2]}
bb <- resB_fams[match(list,rownames(resB_fams)),]
match(list,rownames(bb))
print(bb[1:5,];tre.pruned$tip.label[1:5])

list <- tre.pruned$tip.label
list_fams <- foreach(i=1:length(list),.combine=c) %do% {strsplit(list,"_")[[i]][2]}
tre.pruned$tip.label<-list_fams
is.rooted(tre.pruned); is.ultrametric(tre.pruned)
tre.pruned

iden <- which(rowSums(prPAM_g2[,-c(1:2)]) > 5)
psv <- psv(samp = prPAM_g2[iden,-c(1:2)],tree = tre.pruned)
PSV_random_freq <- matrix(nrow = dim(prPAM_g2[iden,])[1], ncol = 1002)
for (i in 3:ncol(PSV_random_freq)){
  cat(i, "\n")
  psv_random <- psv(samp = randomizeMatrix(prPAM_g2[iden,-c(1:2)],"freq",10000000),tree = tre.pruned)
  PSV_random_freq[,i] <- psv_random$PSVs
}
save(PSV_random_freq,file=paste(ruta_write,"PSV_random_freq.Rdata",sep=""))
PSV_random_rich <- matrix(nrow = dim(prPAM_g2[iden,])[1], ncol = 1002)
for (i in 1:ncol(PSV_random_rich)){
  cat(i, "\n")
  psv_random <- psv(samp = randomizeMatrix(prPAM_g2[iden,-c(1:2)],"rich",10000000),tree = tre.pruned)
  PSV_random_rich[,i] <- psv_random$PSVs
}
save(PSV_random_rich,file=paste(ruta_write,"PSV_random_rich.Rdata",sep=""))

pdf(paste(ruta_write,"10.PSV_randonm.pdf",sep=""))
ssp <- coordinates(prPAM_g2[iden,1:2])
Data<-data.frame(Order=1:dim(prPAM_g2[iden,])[1],z=psv$PSVs[iden])
Data<-Data[order(Data$z),]
f <- clean_coordinates(as.data.frame(ssp), lon = "LON",lat = "LAT",
                       tests="seas",species=NULL,value="flagged",seas_scale=110)
k=20
breaks <- getJenksBreaks(Data$z,k)
cols_breaks <- rev(viridis(k,begin=0.0,end=1))
Data$col <- cols_breaks[k]
for (i in k:1){
Data$col[which(Data$z <= breaks[i])] <- cols_breaks[i]}
orderedcolors<-Data[order(Data$Order),"col"]
maps::map("world",interior=F)
points(ssp[f,],pch=22,lwd=0.1,bg=orderedcolors[f],cex=0.6,col=NULL)
rect(xleft =-180,xright = 190,ybottom = -86,ytop = 85,border = "black")
color.legend(xl=-35,xr=35,yb=-83,yt=-67,align="lt",gradient="x",
             legend=round(range(psv$PSVs,na.rm = T),2),rect.col=cols_breaks)
abline(h=c(23.5,-23.5),lwd=2,lty=3)
title("Phylogenetic Species Variability")

SES_psv <- (psv$PSVs[iden] - apply(PSV_random_rich[,-c(1:2)],MARGIN=1,FUN=mean,na.rm=T))/(apply(PSV_random_rich[,-c(1:2)],MARGIN=1,FUN=sd,na.rm=T))
Data<-data.frame(Order=1:dim(prPAM_g2[iden,])[1],z=SES_psv)
Data<-Data[order(Data$z),]
k=20
breaks <- getJenksBreaks(Data$z,k)
cols_breaks <- rev(viridis(k))
Data$col <- cols_breaks[k]
for (i in k:1){
  Data$col[which(Data$z <= breaks[i])] <- cols_breaks[i]}
orderedcolors<-Data[order(Data$Order),"col"]
maps::map("world",interior=F)
points(ssp[f,],pch=22,lwd=0.1,bg=orderedcolors[f],cex=0.5,col=NULL)
rect(xleft =-180,xright = 190,ybottom = -86,ytop = 85,border = "black")
color.legend(xl=-35,xr=35,yb=-83,yt=-67,align="lt",gradient="x",
             legend=round(range(SES_psv,na.rm = T),2),rect.col=cols_breaks)
title("SES-Phylogenetic Species Variability (NULL1)")

SES_psv <- (psv$PSVs[iden] - apply(PSV_random_freq[,-c(1:2)],MARGIN=1,FUN=mean,na.rm=T))/(apply(PSV_random_freq[,-c(1:2)],MARGIN=1,FUN=sd,na.rm=T))
Data<-data.frame(Order=1:dim(prPAM_g2[iden,])[1],z=SES_psv)
Data<-Data[order(Data$z),]
k=20
breaks <- getJenksBreaks(Data$z,k)
cols_breaks <- rev(viridis(k))
Data$col <- cols_breaks[k]
for (i in k:1){
  Data$col[which(Data$z <= breaks[i])] <- cols_breaks[i]}
orderedcolors<-Data[order(Data$Order),"col"]
maps::map("world",interior=F)
points(ssp[f,],pch=22,lwd=0.1,bg=orderedcolors[f],cex=0.5,col=NULL)
rect(xleft =-180,xright = 190,ybottom = -86,ytop = 85,border = "black")
color.legend(xl=-35,xr=35,yb=-83,yt=-67,align="lt",gradient="x",
             legend=round(range(SES_psv,na.rm = T),2),rect.col=cols_breaks)
title("SES-Phylogenetic Species Variability (NULL2)")
dev.off()


# 1. init
install.packages(c("data.table", "dplyr"))
library(data.table)
library(dplyr)

if (!file.info("UCI HAR Dataset")$isdir) {
  dataFile <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
  dir.create("assignment")
  download.file(dataFile, "assignment/UCI-HAR-dataset.zip", method="curl")
  unzip("assignment/UCI-HAR-dataset.zip")
}

# 2. Merges the training and the test sets to create one data set.
features <- read.table("./UCI HAR Dataset/features.txt")
X_test <- read.table("./UCI HAR Dataset/test/X_test.txt", col.names=features[,2])
X_train <- read.table("./UCI HAR Dataset/train/X_train.txt", col.names=features[,2])
X <- rbind(X_test, X_train)

# 3. Extracts only the measurements on the mean and standard deviation for each measurement.
interestingFeatures <- features[grep("(mean|std)\\(", features[,2]),]
mean_and_std <- X[,interestingFeatures[,1]]

# 4. Uses descriptive activity names to name the activities in the data set
y_test <- read.table("./UCI HAR Dataset/test/y_test.txt", col.names = c('activity'))
y_train <- read.table("./UCI HAR Dataset/train/y_train.txt", col.names = c('activity'))
y <- rbind(y_test, y_train)

labels <- read.table("./UCI HAR Dataset/activity_labels.txt")
for (i in 1:nrow(labels)) {
  code <- as.numeric(labels[i, 1])
  name <- as.character(labels[i, 2])
  y[y$activity == code, ] <- name
}

# 5. Appropriately labels the data set with descriptive activity names.
X_with_labels <- cbind(y, X)
mean_and_std_with_labels <- cbind(y, mean_and_std)

# 6. Creates a second, independent tidy data set with the average of each variable
subject_test <- read.table("./UCI HAR Dataset/test/subject_test.txt", col.names = c('subject'))
subject_train <- read.table("./UCI HAR Dataset/train/subject_train.txt", col.names = c('subject'))
subject <- rbind(subject_test, subject_train)
averages <- aggregate(X, by = list(activity = y[,1], subject = subject[,1]), mean)

write.csv(averages, file='assignment/result.txt', row.names=FALSE)


#' rcond
#'
#' Reciprocal condition number estimate.
#'
#' @details
#' The estimate is computed by first forming the R matrix from a QR.  Currently
#' this involves first computing a crossproduct, so the estimate can be thought
#' of as being fairly liberal.  Afterwards, the condition number is estimated by
#' calling \code{base::rcond()} on the local R matrix.
#'
#' @section Communication:
#' The operation is completely local except for forming the crossproduct, which
#' is an \code{allreduce()} call, quadratic on the number of columns.
#'
#' @param x
#' A shaq.
#' @param norm,triangular,...
#' Arguments passed to \code{base::rcond()}.
#'
#' @return
#' A number.
#'
#' @examples
#' \dontrun{
#' library(kazaam)
#' x = ranshaq(runif, 10, 3)
#' rc = rcond(x)
#' comm.print(rc)
#'
#' x = expand(matrix(1:30, 10))
#' rc = rcond(x)
#' comm.print(rc)
#'
#' finalize()
#' }
#'
#' @method rcond shaq
#' @name rcond
#' @rdname rcond
NULL

rcond.shaq = function(x, norm=c("O", "I", "1"), triangular=FALSE, ...)
{
  norm = pbdMPI::comm.match.arg(toupper(norm), c("O", "I", "1"))
  R = qr_R(x)
  base::rcond(R, norm=norm, triangular=TRUE, ...)
}

#' @rdname rcond
#' @export
setMethod("rcond", signature(x="shaq"), rcond.shaq)


library(sf)
library(purrr)
library(ggplot2)

# Load in the data from the shapefile for Australia
ozbig <- read_sf("data/gadm36_AUS_shp/gadm36_AUS_1.shp")
oz <- thin(ozbig)

# Helper function to turn a matrix into a single data frame with variables long, lat, group, order
mat2df <- function(mat){
  data.frame(long = mat[, 1],
             lat  = mat[, 2],
             group = rep(rnorm(1),nrow(mat)),
             order= 1:nrow(mat))
}

# Create data frames from the geometry variable and plot
oz$geometry %>%
  flatten() %>%
  flatten() %>%
  map_df(.id ="group", .f = mat2df) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
  geom_polygon() +
  geom_path(aes(x = long, y = lat, group = group), alpha = 0.2, inherit.aes = FALSE) +
  theme_bw()


table_auc <- function(covar.list, coef.tb) {

  #### Calculate AUC ####
  event_codes = unique(coef.tb$code)
  timelevels = c('pre24', 'pre12', 'post03', 'post06',
                 'post09', 'post12', 'post24', 'post36', 'post48')
  t.delta = c(12, 12, 3, 3, 3, 3, 12, 12, 12)

  auc <- vector(mode = "numeric", length = length(event_codes))
  auc.var <- vector(mode = "numeric", length = length(event_codes))
  auc.df <- vector()
  names(auc) <- event_codes
  names(auc.var) <- event_codes
  auc.table <- tibble()

  for (dv in names(covar.list)) {
    for (event in event_codes) {

      beta.coef <- coef.tb %>%
        filter(code == event & outcome == dv, term %in% timelevels) %>%
        select(term, estimate, outcome)

      beta.vcov <- covar.list[[dv]][[event]]

      # auc[event] = sum(abs(beta.coef$estimate)*t.delta)
      auc[event] = sum(beta.coef$estimate*t.delta)
      auc.var[event] <- as.numeric(t(t.delta) %*% beta.vcov %*% t.delta)
      auc.df[event] <- unique(coef.tb[coef.tb$code == event & coef.tb$outcome == dv, "N"])
    }

    bind_rows(auc, auc.var, auc.df) %>%
      mutate(x = c("auc", "variance", "N")) %>%
      gather(code, val, -x) %>%
      spread(x, val) -> tb

    tb$outcome = dv
    auc.table <- bind_rows(auc.table, tb)

  }

  #### Add p-values ####
  auc.table %>%
    filter(outcome == "cognitive") %>%
    select(code, auc) %>%
    deframe() -> coef_cognitive

  auc.table %>%
    filter(outcome == "affective") %>%
    select(code, auc) %>%
    deframe() -> coef_affective

  auc.table %>%
    filter(outcome == "cognitive") %>%
    select(code, variance) %>%
    deframe() -> var_cognitive

  auc.table %>%
    filter(outcome == "affective") %>%
    select(code, variance) %>%
    deframe() -> var_affective

  .num = coef_cognitive - coef_affective
  .den = sqrt(var_cognitive + var_affective)

  z_score = .num / .den
  p_2tail = pnorm(abs(z_score), lower.tail = FALSE)*2

  auc.table %<>%
    left_join(
      enframe(z_score, name = "code", value = "z_score"), by = "code"
      ) %>%
    left_join(
      enframe(p_2tail, name = "code", value = "p_2tail"), by = "code"
      ) -> auc.table

  return(auc.table)
}


suppressWarnings(library(e1071))

train_model <- function(X, Y, ...){
  feats = read.table("task1/data/processed/abt_features.txt", stringsAsFactors=FALSE)[,1]
  naiveBayes(X[feats], Y)
}

predict_model <- function(mod, X, type="raw", ...){
  predict(mod, newdata=X, type=type)
}


library(viridis)
library(heatmap3)
library(gplots)
library(superheat)
library(WGCNA)

setwd("/media/yuri/Data/home1/GNI_data/infection/ResultsSciRep")
load("Sepsis_WGCNA.Rdat")

hmap <- cor(datME)
idx <- stats::hclust(dist(hmap), method = "ward.D2")$order
idx <- rev(idx)
hmap <- hmap[idx, idx]
hmap <- hmap[1:nrow(hmap), nrow(hmap):1]
hmap_col <- col2hex(gsub("ME", "", rownames(hmap)))
rownames(hmap) <- NULL
colnames(hmap) <- NULL

datME_sig <- datME
datME_sig <- datME_sig[,idx]
datME_sig <- apply(datME_sig, 2, function(x) t.test(x ~ phenotype_SPS)$p.value)
datME_sig <- p.adjust(datME_sig, "BH")
datME_sig <- -log10(datME_sig)
# datME_sig[grep("skyblue", names(datME_sig))]

# datME_sig[datME_sig < -log10(0.05)] <- 0
# datME_sig <- apply(datME_sig, 2, function(x) dcor.test(x, phenotype, R = 1000)$p.value)

datME_sig_col <- col2hex(gsub("ME", "", names(datME_sig)))
names(datME_sig) <- NULL

p1 <- superheat(hmap,
  # set heatmap color map
    heat.pal = viridis(100),
    heat.pal.values = seq(0.025, 1, length.out = 100),
    # heat.col.scheme = "viridis",
    heat.lim = c(0.5, 1),
    heat.na.col = "black",
    grid.hline = FALSE,
    grid.vline = FALSE,
    left.label.text.size = -1,
    bottom.label.text.size = -1,
    left.label.col = hmap_col,
    bottom.label.col = rev(hmap_col),
  # right plot: HDI
    yr = datME_sig,
    yr.plot.type = "bar",
    yr.axis.name = "-log10(FDR)",
    yr.bar.col = 1,
    yr.obs.col = datME_sig_col,
    yr.lim = c(0, -log10(1e-03))
)

setwd("/media/yuri/Data/home1/GNI_data/infection/ResultsSciRep/Figures/")
ggplot2::ggsave("FigureS5C.pdf", p1$plot, height = 12, width = 12)


 a <- read.table("netz-gene-order.txt",header=F)
 b <- read.table("netz-mRNA-order.txt",header=F)
 c <- read.table("netz-pearson-order.txt",header=F)
 d <- read.table("netz-STRING-order.txt",header=F)
 e <- read.table("genez-order.txt",header=F)
 f <- read.table("net-name.txt",header=F)

 a1 <- 0.3*a
 b1 <- 0.9*b
 c1 <- 0.5*c
 d1 <- 0.4*d
 e1 <- 0.2*e

 x <- a1+b1+c1+d1+e1

 y <- cbind(f,x)

 write.table(y,"result39542.txt",sep="\t",col.names=F,row.names=F,quote=F)