Skip to content

Commit 7469f20

Browse files
committed
Canada only
1 parent b9a562e commit 7469f20

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

analysis/08.MosaicPredictions.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ brt_mosaic <- function(i){
114114

115115
if(inherits(p, "try-error")){break}
116116

117+
if(ext(p)!=ext(w)){
118+
p <- p |>
119+
resample(w) |>
120+
crop(w)
121+
}
122+
117123
#9. Weight the prediction----
118124
SppStack[[j]] <- p*w #apply weighting to the predictions
119125

@@ -122,7 +128,7 @@ brt_mosaic <- function(i){
122128
}
123129

124130
#10. Skip to next if there were corrupt files----
125-
if(inherits(p, "try-error") | inherits(mask.j, "try-error")){
131+
if(inherits(p, "try-error")){
126132

127133
return(NULL)
128134

@@ -138,13 +144,7 @@ brt_mosaic <- function(i){
138144
mosaic(fun="sum") # sum weighted predictions
139145

140146
#12. Correct the weighting ----
141-
Weighted <- CANwideSp/(CANwideW)
142-
143-
#13. Fill in NAs----
144-
#some from hard border transition
145-
FinalOut <- cover(Weighted, focal(Weighted, w=9, fun=mean, na.policy="only", na.rm=TRUE)) |>
146-
mask(st_transform(bcr, crs(CANwideSp)))
147-
#remaining NAs are waterbodies that will be masked out anyway
147+
FinalOut <- CANwideSp/(CANwideW)
148148

149149
#14. Save-----
150150
writeRaster(FinalOut, file.path(root, "output", "08_mosaics_can",paste0(spp.i, "_", year.i, ".tif")), overwrite=T)
@@ -158,10 +158,11 @@ predicted <- data.frame(file.pred = list.files(file.path(root, "output", "07_pre
158158
separate(file.pred, into=c("folder", "spp", "bcr", "year", "file"), remove=FALSE) |>
159159
mutate(year = as.numeric(year),
160160
path.pred = file.path(root, "output", "07_predictions", file.pred)) |>
161-
dplyr::select(-folder, -file)
161+
dplyr::select(-folder, -file) |>
162+
dplyr::filter(str_sub(bcr, 1, 3)=="can")
162163

163164
#2. Get list of mosaics completed----
164-
mosaiced <- data.frame(file.mosaic = list.files(file.path(root, "output", "08_mosaics"), pattern="*.tif")) |>
165+
mosaiced <- data.frame(file.mosaic = list.files(file.path(root, "output", "08_mosaics_can"), pattern="*.tif")) |>
165166
separate(file.mosaic, into=c("spp", "year"), sep="_", remove=FALSE) |>
166167
mutate(year = as.numeric(str_sub(year, -100, -5)),
167168
path.mosaic = file.path(root, "output", "08_mosaics_can", file.mosaic))
@@ -176,16 +177,15 @@ todo <- birdlist |>
176177
expand_grid(year = seq(1985, 2020, 5))
177178

178179
#4. Remove spp*boot*year combinations that don't have all BCRs----
179-
loop <- full_join(predicted, todo) |>
180+
loop <- inner_join(predicted, todo) |>
180181
mutate(na = ifelse(is.na(path.pred), 1, 0)) |>
181182
group_by(spp, year) |>
182183
summarize(nas = sum(na)) |>
183184
ungroup() |>
184185
dplyr::filter(nas==0) |>
185186
anti_join(mosaiced) |>
186187
arrange(-year) |>
187-
dplyr::filter(year > 1985,
188-
str_sub(bcr, 1, 3)=="can")
188+
dplyr::filter(year > 1985)
189189

190190
#5. Get the full list of zeroed bcr raster----
191191
zeros <- data.frame(path.zero = list.files(file.path(root, "gis", "zeros"), full.names = TRUE),

0 commit comments

Comments
 (0)