#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+
#+      New Landscape metrics for resampled rasters
#+
#+
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+


library(landscapemetrics)
library(landscapetools)
library(raster)
library(bench)
library(rgdal)

# loop to get metrics values at the landscape value for all sites
library(tidyr)
library(dplyr)
library(sp)
library(rgeos)

setwd("E:/Helsinki/ResultsAug23/For submission/1.Data/2.ObsInfo")

## load unique sites (109)
load('MothSites109.RDATA')

Moth.year = data.frame(SiteID = rep(Moth.m2$SiteID, 29), 
                       YEUREF= rep(Moth.m2$YEUREF, 29), 
                       XEUREF = rep(Moth.m2$XEUREF, 29),  
                       Year = rep(1998:2021, each=109))
head(Moth.year)

Moth.xy = Moth.year
Moth.xy = Moth.xy[order(Moth.xy$SiteID),]

Year = as.factor(sort(unique(Moth$YearStart)))
Site = sort(unique(Moth$SiteID))

Sit = Site


# prepare the different buffer size coordinates
Moth.xy2 = Moth.xy[Moth.xy$SiteID %in% Sit,]



Metrics00 = Metrics00_4 = Metrics00_2 = Metrics00_1 = Metrics00_0.5 =  matrix(NA, nrow = length(Sit), ncol = 7)
for (i in 1:length(Sit)) {
  
  str_name <- paste(Sit[i], '_2000', '.tif', sep='')
  
  
  # 0.5km
  site.i_0.5 <- readAll(st.i) # get values
  
  # round buffer
  polys_0.5 <- gBuffer(SitMoth.xy, width = 500, capStyle = "ROUND", joinStyle = "MITRE") # uses rgeos -> migrate it!
  
  ## Clip corine with target size buffer
  site.i_0.5 = mask(site.i_0.5, polys_0.5)
  
  Metrics00[i,3] = length(table(site.i_0.5@data@values)) # calculate total number of clc cat
  Metrics00[i,4] = lsm_l_ent(site.i_0.5)$value
  
  
  site.i_0.5_00 = site.i_0.5
  site.i_0.5_00[site.i_0.5_00 == 311] <- 300
  site.i_0.5_00[site.i_0.5_00 == 312] <- 300
  site.i_0.5_00[site.i_0.5_00 == 313] <- 300
  
  Metrics00_0.5[i,1] = Sit[i]
  Metrics00_0.5[i,2] = lsm_l_frac_cv(site.i_0.5_00)$value
  Metrics00_0.5[i,5] = lsm_l_cohesion(site.i_0.5_00)$value
  Metrics00_0.5[i,6] = lsm_l_enn_mn(site.i_0.5_00)$value #! problem if only one patch.. 
  Metrics00_0.5[i,7] = lsm_l_cai_cv(site.i_0.5_00)$value
  colnames(Metrics00_0.5) = c('Site', 'FracCV', 'DivL', 'ent', 'cohesion', 'ennMN', 'caiCV')
}



Metrics06 = Metrics06_4 = Metrics06_2 = Metrics06_1 = Metrics06_0.5 =  matrix(NA, nrow = length(Sit), ncol = 7)
for (i in 1:length(Sit)) {
  
  str_name <- paste(Sit[i], '_2006', '.tif', sep='')
  
  
  
  # 0.5km
  site.i_0.5 <- readAll(st.i) # get values
  
  # round buffer
  polys_0.5 <- gBuffer(SitMoth.xy, width = 506, capStyle = "ROUND", joinStyle = "MITRE") # uses rgeos -> migrate it!
  
  ## Clip corine with target size buffer
  site.i_0.5 = mask(site.i_0.5, polys_0.5)
  
  Metrics06[i,3] = length(table(site.i_0.5@data@values)) # calculate total number of clc cat
  Metrics06[i,4] = lsm_l_ent(site.i_0.5)$value
  
  
  site.i_0.5_06 = site.i_0.5
  site.i_0.5_06[site.i_0.5_06 == 311] <- 300
  site.i_0.5_06[site.i_0.5_06 == 312] <- 300
  site.i_0.5_06[site.i_0.5_06 == 313] <- 300

  Metrics06_0.5[i,1] = Sit[i]
  Metrics06_0.5[i,2] = lsm_l_frac_cv(site.i_0.5_06)$value
  Metrics06_0.5[i,5] = lsm_l_cohesion(site.i_0.5_06)$value
  Metrics06_0.5[i,6] = lsm_l_enn_mn(site.i_0.5_06)$value #! problem if only one patch.. 
  Metrics06_0.5[i,7] = lsm_l_cai_cv(site.i_0.5_06)$value
  colnames(Metrics06_0.5) = c('Site', 'FracCV', 'DivL', 'ent', 'cohesion', 'ennMN', 'caiCV')
  
}



Metrics12 = Metrics12_4 = Metrics12_2 = Metrics12_1 = Metrics12_0.5 =  matrix(NA, nrow = length(Sit), ncol = 7)
for (i in 1:length(Sit)) {
  
  str_name <- paste(Sit[i], '_2012', '.tif', sep='')
  
  
  # 0.5km
  site.i_0.5 <- readAll(st.i) # get values
  
  # round buffer
  polys_0.5 <- gBuffer(SitMoth.xy, width = 512, capStyle = "ROUND", joinStyle = "MITRE") # uses rgeos -> migrate it!
  
  ## Clip corine with target size buffer
  site.i_0.5 = mask(site.i_0.5, polys_0.5)
  
  Metrics12[i,3] = length(table(site.i_0.5@data@values)) # calculate total number of clc cat
  Metrics12[i,4] = lsm_l_ent(site.i_0.5)$value
  
  
  site.i_0.5_12 = site.i_0.5
  site.i_0.5_12[site.i_0.5_12 == 311] <- 300
  site.i_0.5_12[site.i_0.5_12 == 312] <- 300
  site.i_0.5_12[site.i_0.5_12 == 313] <- 300
  
  Metrics12_0.5[i,1] = Sit[i]
  Metrics12_0.5[i,2] = lsm_l_frac_cv(site.i_0.5_12)$value
  Metrics12_0.5[i,5] = lsm_l_cohesion(site.i_0.5_12)$value
  Metrics12_0.5[i,6] = lsm_l_enn_mn(site.i_0.5_12)$value #! problem if only one patch.. 
  Metrics12_0.5[i,7] = lsm_l_cai_cv(site.i_0.5_12)$value
  colnames(Metrics12_0.5) = c('Site', 'FracCV', 'DivL', 'ent', 'cohesion', 'ennMN', 'caiCV')
  
  
}


setwd("L:/bytdk_rec_team/temp/Emy/Moth x Corine/Clipped raster/resampled/2018")

Metrics18 = Metrics18_4 = Metrics18_2 = Metrics18_1 = Metrics18_0.5 =  matrix(NA, nrow = length(Sit), ncol = 7)
for (i in 1:length(Sit)) {
  
  str_name <- paste(Sit[i], '_2018', '.tif', sep='')
  
  
  # 0.5km
  site.i_0.5 <- readAll(st.i) # get values
  
  # round buffer
  polys_0.5 <- gBuffer(SitMoth.xy, width = 518, capStyle = "ROUND", joinStyle = "MITRE") # uses rgeos -> migrate it!
  
  ## Clip corine with target size buffer
  site.i_0.5 = mask(site.i_0.5, polys_0.5)
  
  Metrics18[i,3] = length(table(site.i_0.5@data@values)) # calculate total number of clc cat
  Metrics18[i,4] = lsm_l_ent(site.i_0.5)$value
  
  
  site.i_0.5_18 = site.i_0.5
  site.i_0.5_18[site.i_0.5_18 == 311] <- 300
  site.i_0.5_18[site.i_0.5_18 == 312] <- 300
  site.i_0.5_18[site.i_0.5_18 == 313] <- 300
  
  Metrics18_0.5[i,1] = Sit[i]
  Metrics18_0.5[i,2] = lsm_l_frac_cv(site.i_0.5_18)$value
  Metrics18_0.5[i,5] = lsm_l_cohesion(site.i_0.5_18)$value
  Metrics18_0.5[i,6] = lsm_l_enn_mn(site.i_0.5_18)$value #! problem if only one patch.. 
  Metrics18_0.5[i,7] = lsm_l_cai_cv(site.i_0.5_18)$value
  colnames(Metrics18_0.5) = c('Site', 'FracCV', 'DivL', 'ent', 'cohesion', 'ennMN', 'caiCV')
  
}



setwd("C:/Users/guilbaul/OneDrive/DataREC")

save(Metrics00_0.5, Metrics06_0.5,  Metrics12_0.5, Metrics18_0.5,
     file = 'LMFrag_Moths.RDATA')



