2222
2323# Products from 1985 are excluded from packaging for 3 reasons: 1) There are very few sampling points in the dataset prior to 1993, the 1985 SCANFI products used for prediction are less reliable, 3) there are very few covariate layers that are available for those years of prediction
2424
25- # TO DO: THINK ABOUT TRUNCATING THE MEAN INSTEAD OF THE INDIVIDUAL RASTERS
26-
2725# PREAMBLE############################
2826
2927# 1. Load packages----
@@ -38,7 +36,7 @@ cc <- FALSE
3836
3937# 3. Set nodes for local vs cluster----
4038if (cc ){ cores <- 32 }
41- if (! cc ){ cores <- 1 }
39+ if (! cc ){ cores <- 6 }
4240
4341# 4. Create and register clusters----
4442print(" * Creating clusters *" )
@@ -110,7 +108,7 @@ brt_package <- function(i){
110108 year == year.i ,
111109 bcr == bcr.i ) | >
112110 rename(samplepath = path ) | >
113- mutate(predpath = file.path(root , " output" , " 08_mosaics " ,
111+ mutate(predpath = file.path(root , " output" , " 08_mosaics_can " ,
114112 paste0(spp , " _" , year , " .tif" )))
115113
116114 }
@@ -126,33 +124,40 @@ brt_package <- function(i){
126124 crop(sf.i , mask = TRUE ) | >
127125 mask(water , inverse = TRUE )
128126
129- # 6. Truncate to 99.8% quantile----
130- q99 <- global(mean.i , fun = function (x ) quantile(x , 0.998 , na.rm = TRUE ))
131- mean.i [[1 ]][values(mean.i [[1 ]]) > q99 [,1 ]] <- q99 [,1 ]
127+ # Truncat to 99.8% quantile----
128+ q99mn <- global(mean.i , fun = function (x ) quantile(x , 0.998 , na.rm = TRUE ))
129+ mean.i [[1 ]][values(mean.i [[1 ]]) > q99mn [,1 ]] <- q99mn [,1 ]
132130
133- # 7 . Calculate sd----
131+ # 6 . Calculate sd----
134132 # FIX DIVISION BY ZERO
135133 sd.i <- stdev(stack.i , na.rm = TRUE ) | >
136134 crop(sf.i , mask = TRUE ) | >
137135 mask(water , inverse = TRUE )
138136
139- # 8 . Calculate cv----
137+ # 7 . Calculate cv----
140138 cv.i <- sd.i / (mean.i + 0.0000001 )
141139
142- # #10. Read in the sampling distance layers----
140+ # Truncate to 99.8% quantile----
141+ q99cv <- global(cv.i , fun = function (x ) quantile(x , 0.998 , na.rm = TRUE ))
142+ cv.i [[1 ]][values(cv.i [[1 ]]) > q99cv ] <- q99cv [,1 ]
143+
144+ # #8. Read in the sampling distance layers----
143145 # sample.i <- try(rast(files.i$samplepath) |>
144146 # project("ESRI:102001"))
145147 #
146148 # if(inherits(sample.i, "try-error")){return(NULL)}
147149 #
148- # #11 . Calculate mean sampling distance----
150+ # #9 . Calculate mean sampling distance----
149151 # samplemn.i <- mean(sample.i, na.rm=TRUE) |>
150152 # resample(mean.i) |>
151153 # crop(sf.i, mask=TRUE) |>
152154 # mask(water, inverse=TRUE)
153155
154- # 12. Read in range limit shp----
155- limit <- c(" CAWA" , " BAWW" , " BBWA" , " BHVI" , " BRCR" , " CONW" , " EVGR" , " GWWA" , " HAFL" , " LEFL" , " PUFI" , " VATH" , " VESP" , " WIWR" )
156+ # 10. Stack----
157+ stack.i <- c(mean.i , cv.i )
158+
159+ # 11. Mask outside range----
160+ limit <- c(" CAWA" , " BAWW" , " BBWA" , " BHVI" , " BRCR" , " CONW" , " EVGR" , " GWWA" , " HAFL" , " LCSP" , " LEFL" , " PUFI" , " VATH" , " VESP" , " WIWR" )
156161
157162 if (spp.i %in% limit ){
158163 limit.i <- try(read_sf(file.path(root , " gis" , " ranges" , paste0(spp.i , " _rangelimit.shp" ))) | >
@@ -161,39 +166,35 @@ brt_package <- function(i){
161166
162167 if (inherits(limit.i , " try-error" )){return (NULL )}
163168
164- # 13. Zero out mean prediction outside range----
165- mask.i <- mask(mean.i , limit.i )
166- mask.i [is.na(mask.i )] <- 0
167- mean.i <- crop(mask.i , sf.i , mask = TRUE ) | >
168- mask(water , inverse = TRUE )
169- }
169+ range.i <- mask(stack.i , limit.i )
170+ range.i [is.na(range.i )] <- 0
171+ } else {range.i <- stack.i }
170172
171- # 14. Stack ----
172- out.i <- c( mean.i , cv.i )
173- names( out.i ) <- c( " mean " , " cv " )
174-
175- # out.i <- c(range.i, mean.i, cv.i)
176- # names(out.i) <- c( "range-limited mean", "mean", "cv" )
173+ # 12. Mask by water and NA layer ----
174+ na <- rast(file.path( root , " gis " , " DataLimitationsMask.tif " ) )
175+ na2 <- resample( na , range.i )
176+ out.i <- range.i | >
177+ mask( water , inverse = TRUE ) | >
178+ mask( na2 )
177179
178- # out.i <- c(range.i, mean.i, cv.i, samplemn.i)
179- # names(out.i) <- c( "range-limited mean", "mean", "cv", "detections")
180+ names(out.i ) <- c(" mean" , " cv" )
180181
181- # 16 . Add some attributes----
182+ # 13 . Add some attributes----
182183 attr(out.i , " species" ) <- spp.i
183184 attr(out.i , " subunit" ) <- bcr.i
184185 attr(out.i , " year" ) <- year.i
185186
186- # 17 . Make folders as needed-----
187- if (! (file.exists(file.path(root , " output" , " 10_packaged " , spp.i )))){
188- dir.create(file.path(root , " output" , " 10_packaged " , spp.i ))
187+ # 14 . Make folders as needed-----
188+ if (! (file.exists(file.path(root , " output" , " 10_packaged_can " , spp.i )))){
189+ dir.create(file.path(root , " output" , " 10_packaged_can " , spp.i ))
189190 }
190191
191- if (! (file.exists(file.path(root , " output" , " 10_packaged " , spp.i , bcr.i )))){
192- dir.create(file.path(root , " output" , " 10_packaged " , spp.i , bcr.i ))
192+ if (! (file.exists(file.path(root , " output" , " 10_packaged_can " , spp.i , bcr.i )))){
193+ dir.create(file.path(root , " output" , " 10_packaged_can " , spp.i , bcr.i ))
193194 }
194195
195- # 18 . Save----
196- writeRaster(out.i , filename = file.path(root , " output" , " 10_packaged " , spp.i , bcr.i , paste0(spp.i , " _" , bcr.i , " _" , year.i , " .tif" )), overwrite = TRUE )
196+ # 15 . Save----
197+ writeRaster(out.i , filename = file.path(root , " output" , " 10_packaged_can " , spp.i , bcr.i , paste0(spp.i , " _" , bcr.i , " _" , year.i , " .tif" )), overwrite = TRUE )
197198
198199}
199200
@@ -212,9 +213,10 @@ predicted <-data.frame(file = list.files(file.path(root, "output", "07_predictio
212213 separate(file , into = c(" folder" , " spp" , " bcr" , " year" , " file" ), remove = FALSE ) | >
213214 mutate(year = as.numeric(year ),
214215 path = file.path(root , " output" , " 07_predictions" , file )) | >
215- dplyr :: select(- folder , - file )
216+ dplyr :: select(- folder , - file ) | >
217+ dplyr :: filter(str_sub(bcr , 1 , 3 )== " can" )
216218
217- mosaiced <- data.frame (file = list.files(file.path(root , " output" , " 08_mosaics " ), pattern = " *.tif" , recursive = TRUE )) | >
219+ mosaiced <- data.frame (file = list.files(file.path(root , " output" , " 08_mosaics_can " ), pattern = " *.tif" , recursive = TRUE )) | >
218220 separate(file , into = c(" spp" , " year" , " filetype" ), remove = FALSE ) | >
219221 mutate(year = as.numeric(year ),
220222 path = file.path(root , " output" , " 09_sampling" , file )) | >
@@ -225,7 +227,7 @@ mosaiced <- data.frame(file = list.files(file.path(root, "output", "08_mosaics")
225227sampled <- rbind(predicted , mosaiced )
226228
227229# 2. Check which have been run----
228- done <- data.frame (file = list.files(file.path(root , " output" , " 10_packaged " ), pattern = " *.tif" , recursive = TRUE )) | >
230+ done <- data.frame (file = list.files(file.path(root , " output" , " 10_packaged_can " ), pattern = " *.tif" , recursive = TRUE )) | >
229231 separate(file , into = c(" sppfolder" , " bcrfolder" , " spp" , " bcr" , " year" , " filetype" ), remove = FALSE ) | >
230232 mutate(year = as.numeric(year )) | >
231233 dplyr :: select(- filetype ) | >
@@ -235,8 +237,8 @@ done <- data.frame(file = list.files(file.path(root, "output", "10_packaged"), p
235237# remove species that we are omitting for now
236238loop <- sampled | >
237239 anti_join(done ) | >
238- dplyr :: filter(! spp %in% c(" BEKI" , " SPGR" , " SPSA" , " CMWA" )) | >
239- # dplyr::filter(str_sub(bcr, 1, 3)=="can" ) |>
240+ dplyr :: filter(! spp %in% c(" BEKI" , " SPGR" , " SPSA" , " CMWA" ),
241+ year > 1985 ) | >
240242 dplyr :: filter(bcr == " mosaic" ) | >
241243 arrange(- year , spp , bcr )
242244
0 commit comments