library(tidyr)
library(dplyr)
library(RColorBrewer)

# Environmental data
## CLIM


load('ClimSite080623.RDATA')
head(ClimSite)
colnames(ClimSite)[1:2] = c('SiteID', 'YearStart')

ClimSite2 = ClimSite[, c(1,4,17, 11,7,15,18)] ## 
head(ClimSite2)

ClimSite3 = ClimSite2[ClimSite2$Year>1997,]
ClimSite3 = ClimSite3[ClimSite3$Year < 2021,]
dim(ClimSite3)
head(ClimSite3)


## HAB
# buuffer 0.5km

# fragmentation

load('LMFrag_Moths.RDATA')
head(Metrics00_0.5)



seqC1 = seq(1998, 2003, 1)

Metricsc1 = list()
for (i in 1:length(seqC1)) {
  Metricsc1[[i]] = cbind(rep(seqC1[i], 109), as.data.frame(Metrics00_0.5[,c(1,5,4,6)]))
  colnames(Metricsc1[[i]])[1]= 'YearStart'
}

MetricsCLC1 = do.call(rbind, Metricsc1)


seqC2 = seq(2004, 2009, 1)

Metricsc2 = list()
for (i in 1:length(seqC2)) {
  Metricsc2[[i]] = cbind(rep(seqC2[i], 109), as.data.frame(Metrics06_0.5[,c(1,5,4,6)]))
  colnames(Metricsc2[[i]])[1]= 'YearStart'
}

MetricsCLC2 = do.call(rbind, Metricsc2)



seqC3 = seq(2010, 2015, 1)

Metricsc3 = list()
for (i in 1:length(seqC3)) {
  Metricsc3[[i]] = cbind(rep(seqC3[i], 109), as.data.frame(Metrics12_0.5[,c(1,5,4,6)]))
  colnames(Metricsc3[[i]])[1]= 'YearStart'
}

MetricsCLC3 = do.call(rbind, Metricsc3)



seqC4 = seq(2016, 2020, 1)

Metricsc4 = list()
for (i in 1:length(seqC4)) {
  Metricsc4[[i]] = cbind(rep(seqC4[i], 109), as.data.frame(Metrics18_0.5[,c(1,5,4,6)]))
  colnames(Metricsc4[[i]])[1]= 'YearStart'
}

MetricsCLC4 = do.call(rbind, Metricsc4)

Metricsclc = rbind(MetricsCLC1, MetricsCLC2, MetricsCLC3, MetricsCLC4)
colnames(Metricsclc)
summary(Metricsclc)

Metricsclc[,3][is.na(Metricsclc[,3])] = 0 # NAs are due to not presence of this cat a certain site/year
Metricsclc[,5][is.na(Metricsclc[,5])] = 0

colnames(Metricsclc)[2] = 'SiteID'

par(mfrow=c(2,2))
hist(Metricsclc$clumpy)
hist(Metricsclc$ent)
hist(Metricsclc$ennMN)
par(mfrow=c(1,1))

summary(Metricsclc)




#hab
load('Moth_hab0.5km.RDATA')


Sit = Metrics00_0.5[,1]

## 2000
Prop00_0.5 = Mat00.0.5

ppixel00.mat = plyr::ldply(Prop00_0.5, rbind)
rownames(ppixel00.mat) = Sit

Mat00 = cbind(Sit, ppixel00.mat)


## 2006
Prop06_0.5 = Mat06.0.5
ppixel06.mat = plyr::ldply(Prop06_0.5, rbind)
rownames(ppixel06.mat) = Sit
Mat06 = cbind(Sit, ppixel06.mat)


## 2012
Prop12_0.5 = Mat12.0.5
ppixel12.mat = plyr::ldply(Prop12_0.5, rbind)
rownames(ppixel12.mat) = Sit
Mat12 = cbind(Sit, ppixel12.mat)


## 2018
Prop18_0.5 = Mat18.0.5
ppixel18.mat = plyr::ldply(Prop18_0.5, rbind)
rownames(ppixel18.mat) = Sit
Mat18 = cbind(Sit, ppixel18.mat)


# prepare overall CLC matrix
# duplicate CLC year of previous years

seqC1 = seq(1998, 2003, 1)

Mc1 = list()
for (i in 1:length(seqC1)) {
  Mc1[[i]] = cbind(rep(seqC1[i], 109), Mat00)
  colnames(Mc1[[i]])[1]= 'YearStart'
}

MCLC1 = do.call(rbind, Mc1)

#some NAs -> to verify why..


summary(MCLC1)

seqC2 = seq(2004, 2009, 1)

Mc2 = list()
for (i in 1:length(seqC2)) {
  Mc2[[i]] = cbind(rep(seqC2[i], 109), Mat06)
  colnames(Mc2[[i]])[1]= 'YearStart'
}

MCLC2 = do.call(rbind, Mc2)



seqC3 = seq(2010, 2015, 1)

Mc3 = list()
for (i in 1:length(seqC3)) {
  Mc3[[i]] = cbind(rep(seqC3[i], 109), Mat12)
  colnames(Mc3[[i]])[1]= 'YearStart'
}

MCLC3 = do.call(rbind, Mc3)



seqC4 = seq(2016, 2020, 1)

Mc4 = list()
for (i in 1:length(seqC4)) {
  Mc4[[i]] = cbind(rep(seqC4[i], 109), Mat18)
  colnames(Mc4[[i]])[1]= 'YearStart'
}

MCLC4 = do.call(rbind, Mc4)


Mclc = rbind(MCLC1, MCLC2, MCLC3, MCLC4)
colnames(Mclc) = c('YearStart', 'SiteID', 'SNHherb', 'BLforest', 'Cforest', 'Mforest')

Mclc$BLforest[is.na(Mclc$BLforest)] = 0 # NAs are due to not presence of this cat a certain site/year
Mclc$Cforest[is.na(Mclc$Cforest)] = 0
Mclc$Mforest[is.na(Mclc$Mforest)] = 0
Mclc$SNHherb[is.na(Mclc$SNHherb)] = 0
summary(Mclc)



par(mfrow=c(2,2))
hist(Mclc$BLforest)
hist(Mclc$Cforest)
hist(Mclc$Mforest)
hist(Mclc$SNHherb)
par(mfrow=c(1,1))

## ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
dim(ClimSite3)
dim(Mclc)
dim(Metricsclc)


save(Mclc, Metricsclc, file='HabSites1108.RDATA')


#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

## Merging information
### Adding clim data

Mclc = Mclc[order(Mclc$SiteID),]
Metricsclc = Metricsclc[order(Metricsclc$SiteID),]

head(Mclc)
head(Metricsclc)
head(ClimSite3)

Siteenv = cbind(ClimSite3, Metricsclc[, c(3:5)], Mclc[, c(4:6,3)])
summary(Siteenv)

save(Siteenv, file='Site_env2508.RDATA')



