-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.0_BPS_Checks.R
76 lines (57 loc) · 2.63 KB
/
4.0_BPS_Checks.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env Rscript
# This is a third check to ensure we all data for CONUS is done
# And that included/excluded BPS tiles have been properly incorporated into the analysis
# Libraries
library(raster)
library(plyr)
library(dplyr)
library(parallel)
# Setting working direcotry
setwd("/data/pubh-glob2loc/pubh0329/Reforestation_Hub/")
#bps.dat <- paste0(getwd(),'/Raw Raster Data/LF_BiphysicalPotential/Tif/LC16_BPS_200.tif')
#bps.raster <- raster(bps.dat)
# Reprojecting BPS raster and saving
cat('Reprojecting Raster')
conus.raster <- raster(paste0(getwd(),'/CONUS_Rasters/CONUS_total_opportunity_deducted.tif'))
# reprojecting
bps.reprojected <- projectRaster(bps.raster,conus.raster,method = 'ngb')
# Saving
writeRaster(bps.reprojected,paste0(getwd(),'bps_check_raster.tif'))
# Overlaps between BPS and CONUS analysis
cat('Getting overlaps')
bps.reprojected <- raster('/data/pubh-glob2loc/pubh0329/bps_check_raster.tif')
# Managing Raster Tiles
r <- raster(conus.raster)
n.side <- 20 # number of tiles per side
dx <- (extent(r)[2]- extent(r)[1])/ n.side # extent of one tile in x direction
dy <- (extent(r)[4]- extent(r)[3])/ n.side # extent of one tile in y direction
xs <- seq(extent(r)[1], by= dx, length= n.side) #lower left x-coordinates
ys <- seq(extent(r)[3], by= dy, length= n.side) #lower left y-coordinates
cS <- expand.grid(x= xs, y= ys)
# values to included
included.bps <- read.csv(paste0(getwd(),'/Other Data Inputs/mmc3.csv'))
excluded.grasslands <- read.csv(paste0(getwd(),'/Other Data Inputs/BPS_to_remove_grasslands.csv'),stringsAsFactors = FALSE)
# Checking - this should end up with an empty folder
dir.create(paste0(getwd(),'/BPS_Checks/'))
# Function to loop
loop.function <- function(i) {
# Loading bps data
ex1 <- c(cS[i,1], cS[i,1]+dx, cS[i,2], cS[i,2]+dy) # create extents for cropping raster
tmp.bps <- crop(bps.reprojected, ex1) # crop raster by extent
tmp.conus <- crop(conus.raster,ex1)
# data frame
tmp.df <- data.frame(bps = getValues(tmp.bps),sequest = getValues(tmp.conus))
tmp.df <- tmp.df %>% filter(!is.na(sequest)) %>% filter(sequest > 0)
# filtering
tmp.df.check <-
tmp.df %>%
filter(!(bps %in% included.bps$BPS_Groups))
tmp.df.check <- tmp.df.check %>% dplyr::select(bps) %>% unique()
tmp.df.check.grass <-
tmp.df %>%
filter(bps %in% excluded.grasslands$BPS_Groups) %>%
dplyr::select(bps) %>% unique()
if(nrow(tmp.df.check)>0){write.csv(tmp.df.check,paste0(getwd(),'/BPS_Checks/Tile_included',i,'.csv'))}
if(nrow(tmp.df.check.grass)>0){write.csv(tmp.df.check.grass,paste0(getwd(),'/BPS_Checks/Tile_grassland',i,'.csv'))}
}
mclapply(1:400,loop.function,mc.cores = 15)