model = lmer(Ubird ~Uwind*Vwind*sex + (1+individualid|territory), data=xxx, REML=FALSE)

data2 <- data[which(data$season == 's2' & data$cy == 1 & data$name %in% survivors$name & (data$dt > data$eos | is.na(data$eos) == T) & (data$dt < data$sow | is.na(data$sow) == T) & data$long < 40),]

ends <- 

first.dts <- aggregate(data2$dt,by=list(data2$name),FUN='min')
colnames(first.dts) <- c("name","start")
attributes(first.dts$start)$tzone <- "UTC"  

last.dts <- aggregate(data2$dt,by=list(data2$name),FUN='max')
colnames(last.dts) <- c("name","end")
attributes(last.dts$end)$tzone <- "UTC"  








data3 <- data[which(data$name %in% unique(survivors$name) & data$cy == 1 & (data$dt > data$sow) & data$long < 40),]

first.dts <- aggregate(data3$dt,by=list(data3$name),FUN='min')
colnames(first.dts) <- c("name","start")
attributes(first.dts$start)$tzone <- "UTC"  

mid.dts <- aggregate(data3$dt,by=list(data3$name),FUN='median')
colnames(mid.dts) <- c("name","mid")
attributes(mid.dts$mid)$tzone <- "UTC"  

last.dts <- aggregate(data3$dt,by=list(data3$name),FUN='max')
colnames(last.dts) <- c("name","end")
attributes(last.dts$end)$tzone <- "UTC"  

# Merge Summary stats
startends <- merge(first.dts,last.dts,all.x=T)
startends <- merge(startends,mid.dts,all.x=T)

data3 <- merge(data3,startends,all.x=T)

## Add start and ends to df and determine last long and lat
first.longs <- data3[which(data3$dt == data3$start),c("name","long")]
colnames(first.longs)[2] <- c("first.long")

#data3 <- merge(data3,end.longs,all.x=T)
mid.longs <- data3[which(data3$dt == data3$mid),c("name","long")]
colnames(mid.longs)[2] <- c("mid.long")

end.longs <- data3[which(data3$dt == data3$end),c("name","long")]
colnames(end.longs)[2] <- c("end.long")


startends <- merge(startends,first.longs,all.x=T)
startends <- merge(startends,end.longs,all.x=T)
startends <- merge(startends,mid.longs,all.x=T)

ggplot() + geom_point(data=startends,aes(x=end.long,y=first.long),col='red') +
	     geom_point(data=startends,aes(x=mid.long,y=first.long),col='blue') +
		geom_text(data=startends,aes(x=end.long,y=first.long,label=name),col='red',size=2)+
		geom_text(data=startends,aes(x=mid.long,y=first.long,label=name),col='blue',size=2)+
	    geom_abline(intercept=0,slope=1)

data3 <- data[which(data$name %in% unique(survivors$name) & data$cy == 2 & (data$dt < data$eow) & data$long < 40),]

first.dts <- aggregate(data3$dt,by=list(data3$name),FUN='min')
colnames(first.dts) <- c("name","start")
attributes(first.dts$start)$tzone <- "UTC"  
last.dts <- aggregate(data3$dt,by=list(data3$name),FUN='max')
colnames(last.dts) <- c("name","end")
attributes(last.dts$end)$tzone <- "UTC"  

# Merge Summary stats
cy2.startends <- merge(first.dts,last.dts)

data3 <- merge(data3,cy2.startends,all.x=T)

## Add start and ends to df and determine last long and lat
cy2.first.longs <- data3[which(data3$dt == data3$start),c("name","long")]
colnames(cy2.first.longs)[2] <- c("cy2.first.long")

cy2.end.longs <- data3[which(data3$dt == data3$end),c("name","long")]
colnames(cy2.end.longs)[2] <- c("cy2.end.long")

startends <- merge(startends,cy2.first.longs,all.x=T)
startends <- merge(startends,cy2.end.longs,all.x=T)


data3 <- data[which(data$name %in% unique(survivors$name) & data$cy == 3 & (data$dt < data$eow) & data$long < 40),]

first.dts <- aggregate(data3$dt,by=list(data3$name),FUN='min')
colnames(first.dts) <- c("name","start")
attributes(first.dts$start)$tzone <- "UTC"  
last.dts <- aggregate(data3$dt,by=list(data3$name),FUN='max')
colnames(last.dts) <- c("name","end")
attributes(last.dts$end)$tzone <- "UTC"  

# Merge Summary stats
cy3.startends <- merge(first.dts,last.dts)

data3 <- merge(data3,cy3.startends,all.x=T)

## Add start and ends to df and determine last long and lat
cy3.first.longs <- data3[which(data3$dt == data3$start),c("name","long")]
colnames(cy3.first.longs)[2] <- c("cy3.first.long")

cy3.end.longs <- data3[which(data3$dt == data3$end),c("name","long")]
colnames(cy3.end.longs)[2] <- c("cy3.end.long")

startends <- merge(startends,cy3.first.longs,all.x=T)
startends <- merge(startends,cy3.end.longs,all.x=T)

startends$longdiff.cy1 <- startends$first.long - startends$end.long
startends$longdiff.cy2 <- startends$end.long - startends$cy2.end.long
startends$longdiff.cy3 <- startends$cy2.end.long - startends$cy3.end.long

startends$label <- ifelse(abs(startends$longdiff.cy1) > 5 | abs(startends$longdiff.cy2) > 5 | abs(startends$longdiff.cy3) > 5,1,0)

maxage <- aggregate(as.numeric(data[which(data$age == 'juv'),]$cy),by=list(data[which(data$age == 'juv'),]$name),FUN='max')
colnames(maxage) <- c("name","maxage")

startends <- merge(startends,maxage,all.x=T)

ggplot() + geom_point(data=startends[which(startends$maxage > 2),],aes(x=end.long,y=first.long),col='red') +
		geom_text(data=startends[which(startends$label == 1 & startends$maxage > 2),],aes(x=end.long,y=first.long-1,label=name),col='red',size=2.8)+
#	     geom_point(data=startends[which(startends$maxage > 2),],aes(x=mid.long,y=first.long),col='blue') +
#		geom_text(data=startends[which(startends$label == 1 & startends$maxage > 2),],aes(x=mid.long,y=first.long,label=name),col='blue',size=2.8)+
#	     geom_point(data=startends[which(startends$maxage > 2),],aes(x=cy2.first.long,y=first.long),col='green') +
#		geom_text(data=startends[which(startends$label == 1 & startends$maxage > 2),],aes(x=cy2.first.long,y=first.long,label=name),col='green',size=2.8)+
	     geom_point(data=startends[which(startends$maxage > 2),],aes(x=cy2.end.long,y=first.long),col='blue') +
		geom_text(data=startends[which(startends$label == 1 & startends$maxage > 2),],aes(x=cy2.end.long,y=first.long-1,label=name),col='blue',size=2.8)+
	     geom_point(data=startends[which(startends$maxage > 2),],aes(x=cy3.end.long,y=first.long),col='darkgreen') +
		geom_text(data=startends[which(startends$label == 1 & startends$maxage > 2),],aes(x=cy3.end.long,y=first.long-1,label=name),col='darkgreen',size=2.8)+
	    theme_bw()+ coord_equal()+ ylab("Longitude at end of first autumn migration") + ylab("Longitude in Africa until CY3") +
	    geom_abline(intercept=5,slope=1,linetype='dashed')+
	    geom_abline(intercept=0,slope=1)+
	    geom_abline(intercept=-5,slope=1,linetype='dashed')

ggsave('Vansteelant-etal-PRSB-SupplFigure-LongitudesInAfrica.tiff',dpi=300,width=7,height=7)


data <- merge(data,maxage,all.x=T)
data3 <- data[which(data$name %in% unique(survivors$name) & data$maxage > 2 & ((data$cy == 1 & data$dt > data$sow) |data$cy == 2 |(data$cy == 3 & data$dt < data$eow)) & data$long < 40),]
data3 <- data3[order(data3$migr,data3$dt),]

latlimits <- c(min(data3$lat),max(data3$lat))
longlimits <- c(min(data3$long),max(data3$long))


ggplot() + geom_path(data=data3,aes(x=long,y=lat,col=cy,group=name),size=1.4) + borders('world') +
	coord_equal(xlim=longlimits,ylim=latlimits)+ facet_wrap(~name,ncol=4)+
	theme_bw() + xlab("Longitude") + ylab("Latitude")

ggsave('Vansteelant-etal-PRSB-SupplFigure-TracksInAfrica.tiff',dpi=300,width=8,height=7)


ggplot() + geom_path(data=data3,aes(x=dt,y=long,col=name,group=paste(name,yr)),size=1.5) + borders('world') 

 +



#data3 <- merge(data3,end.longs,all.x=T)

