Revision 3ca58e9d
Added by Benoit Parmentier almost 11 years ago
climate/research/oregon/interpolation/multi_timescales_paper_interpolation_functions.R | ||
---|---|---|
5 | 5 |
#Functions used in the production of figures and data for the multi timescale paper are recorded. |
6 | 6 |
#AUTHOR: Benoit Parmentier # |
7 | 7 |
#DATE CREATED: 11/25/2013 |
8 |
#DATE MODIFIED: 12/09/2013
|
|
8 |
#DATE MODIFIED: 12/12/2013
|
|
9 | 9 |
#Version: 1 |
10 | 10 |
#PROJECT: Environmental Layers project # |
11 | 11 |
################################################################################################# |
... | ... | |
249 | 249 |
#end of function |
250 | 250 |
} |
251 | 251 |
|
252 |
diff_date_rast_pred_fun <- function(i,list_param){ |
|
253 |
|
|
254 |
index <- i #index date |
|
255 |
list_raster_obj_files <- list_param$list_raster_obj_files |
|
256 |
methods_name <- list_param$methods_name |
|
257 |
y_var_name <- list_param$y_var_name |
|
258 |
ref_mod <- list_param$ref_mod |
|
259 |
alt_mod <- list_param$alt_mod |
|
260 |
NA_flag_val <- list_param$NA_flag_val |
|
261 |
file_format <- list_param$file_format |
|
262 |
out_dir <- list_param$out_dir |
|
263 |
out_prefix <- list_param$out_prefix |
|
264 |
|
|
265 |
#index<-244 #index corresponding to Sept 1 |
|
266 |
|
|
267 |
#lf_list<-lapply(list_raster_obj_files[c("gam_daily","gam_CAI","gam_fss")], |
|
268 |
# FUN=function(x){x<-load_obj(x);x$method_mod_obj[[index]][[y_var_name]]}) |
|
269 |
lf_list<-lapply(list_raster_obj_files[methods_name], |
|
270 |
FUN=function(x){x<-load_obj(x);x$method_mod_obj[[index]][[y_var_name]]}) |
|
271 |
diff_pred_list <- vector("list",length=length(lf_list)) |
|
272 |
for (i in 1:length(lf_list)){ |
|
273 |
interpolation_method <- methods_name[i] |
|
274 |
r_diff <- raster(lf_list[[i]][[ref_mod]]) - raster(lf_list[[i]][[alt_mod]]) |
|
275 |
data_name <-paste(index,"_",y_var_name,"_diff_",ref_mod,"_",alt_mod,sep="") |
|
276 |
raster_name <-file.path(out_dir,paste(interpolation_method,"_",data_name,out_prefix,file_format, sep="")) |
|
277 |
diff_pred_list[[i]] <-raster_name |
|
278 |
writeRaster(r_diff, filename=raster_name,NAflag=NA_flag_val,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE) #Writing the data in a raster file format... |
|
279 |
|
|
280 |
} |
|
281 |
return(unlist(diff_pred_list)) |
|
282 |
} |
|
283 |
|
|
284 |
#Extract statistic by zones...make this general by changing names? |
|
285 |
extract_diff_by_landcover <- function(r_stack_diff,s_raster,LC_subset,LC_names,avl){ |
|
286 |
|
|
287 |
rclmat<-matrix(avl,ncol=3,byrow=TRUE) |
|
288 |
|
|
289 |
LC_s <- subset(s_raster,LC_subset) |
|
290 |
LC_s_rec <-reclassify(LC_s,rclmat) |
|
291 |
names(LC_s_rec)<- LC_names |
|
292 |
#plot(LC_s) |
|
293 |
#plot average difference per class of forest and LC2 |
|
294 |
list_avg_diff <- vector("list",length=nlayers(r_stack_diff)) |
|
295 |
list_sd_diff <- vector("list",length=nlayers(r_stack_diff)) |
|
296 |
|
|
297 |
for (i in 1:nlayers(r_stack_diff)){ |
|
298 |
rast_diff <- subset(r_stack_diff,i) |
|
299 |
list_avg <- vector("list",length=nlayers(LC_s_rec)) |
|
300 |
list_sd <- vector("list",length=nlayers(LC_s_rec)) |
|
301 |
for(k in 1:nlayers(LC_s_rec)){ |
|
302 |
LC_rec <- subset(LC_s_rec,k) |
|
303 |
list_avg[[k]] <- zonal(rast_diff,z=LC_rec,stat="mean",na.rm=TRUE)[,2] |
|
304 |
list_sd[[k]] <- zonal(rast_diff,z=LC_rec,stat="sd",na.rm=TRUE)[,2] |
|
305 |
} |
|
306 |
zones_avg <- do.call(cbind,list_avg) |
|
307 |
zones_sd <- do.call(cbind,list_sd) |
|
308 |
colnames(zones_avg)<- LC_names |
|
309 |
colnames(zones_sd)<-LC_names |
|
310 |
list_avg_diff[[i]] <- zones_avg |
|
311 |
list_sd_diff[[i]] <- zones_sd |
|
312 |
} |
|
313 |
|
|
314 |
|
|
315 |
list_zones <- list(avg=list_avg_diff,sd=list_sd_diff) |
|
316 |
return(list_zones) |
|
317 |
} |
|
318 |
|
|
319 |
## Utilit function to quickly write out a stack or brick of rasterlayer to disk using names of layers |
|
320 |
# Note that each layers are written individually, default NA value and format is provided |
|
321 |
write_out_raster_fun <-function(r_stack,out_suffix,out_dir,NA_flag_val=-9999,file_format=".rst"){ |
|
322 |
for(i in 1:nlayers(r_stack)){ |
|
323 |
list_raster_name <- vector("list",length=nlayers(r_stack)) |
|
324 |
r<-subset(r_stack,i) |
|
325 |
raster_name <- paste(names(r_stack)[i],"_",out_suffix,file_format,sep="") |
|
326 |
writeRaster(r, NAflag=NA_flag_val,filename=file.path(out_dir,raster_name) |
|
327 |
,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE) |
|
328 |
list_raster_name[[i]] <- file.path(out_dir,raster_name) |
|
329 |
} |
|
330 |
return(unlist(list_raster_name)) |
|
331 |
} |
|
332 |
|
|
252 | 333 |
################### END OF SCRIPT ################### |
253 | 334 |
|
254 | 335 |
|
Also available in: Unified diff
adding functions to compare methods through diff and land cove effects