Skip to content

Commit 62ef207

Browse files
committed
Move correction raster calculation out
1 parent fa16219 commit 62ef207

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

gis/CorrectionRasters.R

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# ---
2+
# Title: Using extapolation rasters to produce correction masks
3+
# Author: Anna Drake, adapted by Elly Knight
4+
# Date: June 2025
5+
# ---
6+
7+
#NOTES################################
8+
9+
#PREAMBLE####
10+
11+
#1. Load packages----
12+
print("* Loading packages on master *")
13+
library(sf)
14+
library(tidyverse)
15+
library(terra)
16+
17+
#2. Root file path----
18+
root <- "G:/Shared drives/BAM_NationalModels5"
19+
20+
#3. Get overlap raster----
21+
MosaicOverlap <- rast(file.path(root, "gis", "ModelOverlap.tif"))
22+
23+
#4. List of extrapolation rasters----
24+
extrap <- data.frame(file = list.files(file.path(root, "gis", "extrapolation"), pattern="*.tif", recursive = TRUE)) |>
25+
separate(file, into=c("bcr", "year", "filetype"), remove=FALSE) |>
26+
mutate(year = as.numeric(year),
27+
path = file.path(root, "gis", "extrapolation", file)) |>
28+
dplyr::select(-file)
29+
30+
#5. BCR perimeter ----
31+
bcr <- read_sf(file.path(root, "gis", "BAM_BCR_NationalModel_Unbuffered.shp")) |>
32+
mutate(bcr = paste0(country, subUnit)) |>
33+
st_transform(crs="ESRI:102001") |>
34+
vect()
35+
36+
#1. Set up to loop through years ----
37+
years <- seq(1985, 2020, 5)
38+
for(i in 1:length(years)){
39+
40+
#2. Filter to the year ----
41+
extrap.i <- dplyr::filter(extrap, year==years[i])
42+
43+
#6. Import, mosaic, and sum extrapolation rasters-------
44+
f.mosaic <- lapply(loop.i$path.extrap, rast) |>
45+
sprc() |>
46+
mosaic(fun="sum") |>
47+
crop(ext(MosaicOverlap))
48+
49+
#7. Fix extent of overlap raster----
50+
overlap.i <- crop(MosaicOverlap, ext(f.mosaic))
51+
52+
#8. Divide extrapolation layers by available layers to determine if alternate exists-----
53+
# If == 1 there is no potential raster, if == 0.25-0.75 then a potential raster exists
54+
Overlay <- f.mosaic/overlap.i
55+
56+
#9. Produce region-wide extrapolation raster----
57+
#Areas we retain extrapolation because we have no alternative == 1
58+
Extrapolation <- Overlay
59+
Extrapolation[Extrapolation < 1] <- 0
60+
Extrapolation[Extrapolation > 0] <- 1
61+
# writeRaster(Extrapolation, file.path(root, "output", "mosaics", "extrapolation", paste0(spp.i, "_", boot.i, "_", year.i, ".tif")), overwrite=T)
62+
63+
#10. Produce correction raster----
64+
Correction <- Overlay
65+
Correction[Correction==1] <- 0 #ignore areas where nothing *or* everything is missing
66+
Correction[Correction>0] <- 1 #flag areas where non-extrapolated predictions exist in any layer
67+
68+
#8. Save
69+
writeRaster(Extrap.mn, file.path(root, "output", "10_packaged", "extrapolation", paste0("Extrapolation_", years[i], ".tif")), overwrite=T)
70+
71+
cat(i, " ")
72+
73+
}
74+
75+

0 commit comments

Comments
 (0)