Revision 8e595ae4
Added by Benoit Parmentier about 8 years ago
climate/research/oregon/interpolation/global_product_assessment_part2.R | ||
---|---|---|
218 | 218 |
debug(plot_and_animate_raster_time_series) |
219 | 219 |
|
220 | 220 |
metric_name <- "var_pred" #use RMSE if accuracy |
221 |
|
|
221 |
#df_raster <- read.table("df_raster_global_assessment_reg6_10102016.txt",sep=",",header=T) |
|
222 |
#plot_figure <- |
|
222 | 223 |
plot_and_animate_raster_time_series(lf_raster=lf_raster, |
223 | 224 |
NAvalue=NA_flag_val, |
224 | 225 |
item_no=13, |
... | ... | |
236 | 237 |
|
237 | 238 |
## Create function here: |
238 | 239 |
|
239 |
plot_and_animate_raster_time_series <- function(lf_raster,NAvalue, item_no,region_name,var_name,metric_name,frame_speed,animation_format,zlim_val,plot_figure,generate_animation,num_cores,out_suffix,out_dir){
|
|
240 |
plot_and_animate_raster_time_series <- function(lf_raster, item_no,region_name,var_name,metric_name,NA_flag_val,filenames_figures=NULL,frame_speed=60,animation_format=".gif",zlim_val=NULL,plot_figure=T,generate_animation=T,num_cores=2,out_suffix="",out_dir="."){
|
|
240 | 241 |
#Function to generate figures and animation for a list of raster |
241 | 242 |
# |
242 | 243 |
# |
243 | 244 |
#INPUTS |
244 |
#1) lf_raster, |
|
245 |
#1) lf_raster |
|
246 |
#2) filenames_figures |
|
245 | 247 |
#2) NAvalue |
246 | 248 |
#3) item_no |
247 | 249 |
#4) region_name, |
... | ... | |
259 | 261 |
# |
260 | 262 |
# |
261 | 263 |
|
262 |
lf_mosaic_list <- lf_raster |
|
264 |
|
|
265 |
|
|
266 |
#lf_mosaic_list <- lf_raster |
|
263 | 267 |
variable_name <- var_name |
264 | 268 |
|
265 |
if(plot_figure==T){
|
|
269 |
if(!is.null(plot_figure)){
|
|
266 | 270 |
#item_no <- 13 |
267 | 271 |
list_dates_produced <- unlist(mclapply(1:length(lf_raster), |
268 | 272 |
FUN = extract_date, |
... | ... | |
280 | 284 |
month_str = month_str, |
281 | 285 |
year = year_str, |
282 | 286 |
day = day_str, |
283 |
dir = dirname(lf_mosaic_list))
|
|
287 |
dir = dirname(lf_raster))
|
|
284 | 288 |
|
285 | 289 |
df_raster_fname <- file.path(out_dir, paste0("df_raster_", out_suffix, ".txt")) |
286 | 290 |
write.table(df_raster,file = df_raster_fname,sep = ",",row.names = F) |
... | ... | |
288 | 292 |
############### PART5: Make raster stack and display maps ############# |
289 | 293 |
#### Extract corresponding raster for given dates and plot |
290 | 294 |
|
291 |
r_stack_subset <- subset(r_stack, 1:11)
|
|
292 |
l_dates <- list_dates_produced_date_val[1:11] |
|
295 |
r_stack <- stack(lf_raster,quick=T)
|
|
296 |
l_dates <- list_dates_produced_date_val #[1:11]
|
|
293 | 297 |
|
294 | 298 |
#undebug(plot_raster_mosaic) |
295 |
zlim_val <- NULL
|
|
299 |
zlim_val <- zlim_val
|
|
296 | 300 |
|
297 | 301 |
### Now run for the full time series |
298 | 302 |
#13.26 Western time: start |
299 |
l_dates <- list_dates_produced_date_val |
|
300 |
r_stack_subset <- r_stack |
|
301 |
zlim_val <- NULL |
|
302 |
list_param_plot_raster_mosaic <- list(l_dates,r_stack_subset,NA_flag_val,out_dir, |
|
303 |
out_suffix,region_name,variable_name,zlim_val) |
|
303 |
#l_dates <- list_dates_produced_date_val |
|
304 |
#r_stack_subset <- r_stack |
|
305 |
#zlim_val <- NULL |
|
306 |
out_suffix_str <- paste0(var_name,"_",metric_name,"_",out_suffix) |
|
307 |
list_param_plot_raster_mosaic <- list(l_dates,r_stack,NA_flag_val,out_dir, |
|
308 |
out_suffix_str,region_name,variable_name,zlim_val) |
|
304 | 309 |
names(list_param_plot_raster_mosaic) <- c("l_dates","r_mosaiced_scaled","NA_flag_val_mosaic","out_dir", |
305 | 310 |
"out_suffix", "region_name","variable_name","zlim_val") |
306 | 311 |
|
307 |
lf_mosaic_plot_fig <- mclapply(1:length(l_dates[1:11]), |
|
308 |
FUN = plot_raster_mosaic, |
|
309 |
list_param = list_param_plot_raster_mosaic, |
|
310 |
mc.preschedule = FALSE, |
|
311 |
mc.cores = num_cores) |
|
312 |
##start at 14.12 |
|
313 |
##finished at 16.47 |
|
312 |
#lf_mosaic_plot_fig <- mclapply(1:length(l_dates[1:11]), |
|
313 |
# FUN = plot_raster_mosaic, |
|
314 |
# list_param = list_param_plot_raster_mosaic, |
|
315 |
# mc.preschedule = FALSE, |
|
316 |
# mc.cores = num_cores) |
|
317 |
|
|
318 |
##start at 12.29 |
|
319 |
##finished at 15.23 (for reg 6 with 2,991 figures) |
|
314 | 320 |
lf_mosaic_plot_fig <- mclapply(1:length(l_dates), |
315 | 321 |
FUN = plot_raster_mosaic, |
316 | 322 |
list_param = list_param_plot_raster_mosaic, |
317 | 323 |
mc.preschedule = FALSE, |
318 | 324 |
mc.cores = num_cores) |
319 |
|
|
325 |
|
|
326 |
if (is.null(zlim_val)) { |
|
327 |
out_suffix_movie <- paste("min_max_", out_suffix_str, sep = "") |
|
328 |
} else{ |
|
329 |
zlim_val_str <- paste(zlim_val, sep = "_", collapse = "_") |
|
330 |
out_suffix_movie <- paste(zlim_val_str, "_", out_suffix, sep = "") |
|
331 |
} |
|
332 |
filenames_figures_mosaic <- paste0("list_figures_animation_", out_suffix_movie, ".txt") |
|
333 |
|
|
334 |
write.table(unlist(lf_mosaic_plot_fig),filenames_figures_mosaic,row.names = F,col.names = F,quote = F) |
|
335 |
|
|
320 | 336 |
} |
321 | 337 |
|
322 | 338 |
### Part 2 generate movie |
323 | 339 |
|
324 | 340 |
if(generate_animation==TRUE){ |
325 | 341 |
|
342 |
out_suffix_str <- paste0(var_name,"_",metric_name,"_",out_suffix) |
|
343 |
|
|
326 | 344 |
if (is.null(zlim_val)) { |
327 | 345 |
out_suffix_movie <- paste("min_max_", out_suffix, sep = "") |
328 | 346 |
} else{ |
329 | 347 |
zlim_val_str <- paste(zlim_val, sep = "_", collapse = "_") |
330 | 348 |
out_suffix_movie <- paste(zlim_val_str, "_", out_suffix, sep = "") |
331 | 349 |
} |
332 |
#r_stack_subset <- subset(r_stack,1:11) |
|
333 |
#l_dates <- list_dates_produced_date_val[1:11] |
|
334 |
|
|
335 |
filenames_figures_mosaic <- paste0("list_figures_animation_", out_suffix_movie, ".txt") |
|
336 | 350 |
|
337 |
write.table(unlist(lf_mosaic_plot_fig),filenames_figures_mosaic,row.names = F,col.names = F,quote = F) |
|
351 |
#already provided as a parameter |
|
352 |
#filenames_figures_mosaic <- paste0("list_figures_animation_", out_suffix_movie, ".txt") |
|
353 |
#write.table(unlist(lf_mosaic_plot_fig),filenames_figures_mosaic,row.names = F,col.names = F,quote = F) |
|
338 | 354 |
|
339 | 355 |
#now generate movie with imageMagick |
340 | 356 |
#frame_speed <- 60 |
341 | 357 |
#animation_format <- ".gif" |
342 |
out_suffix_str <- out_suffix |
|
358 |
#out_suffix_str <- out_suffix
|
|
343 | 359 |
#started |
344 | 360 |
#debug(generate_animation_from_figures_fun) |
345 |
|
|
346 |
out_filename_figure_animation <- generate_animation_from_figures_fun(filenames_figures = unlist(lf_mosaic_plot_fig[1:11]), |
|
347 |
frame_speed = frame_speed, |
|
348 |
format_file = animation_format, |
|
349 |
out_suffix = out_suffix_str, |
|
350 |
out_dir = out_dir, |
|
351 |
out_filename_figure_animation = "test2_reg6_animation.gif") |
|
361 |
#lf_mosaic_plot_fig <- read.table(filenames_figure,sep=",") |
|
362 |
|
|
363 |
#out_filename_figure_animation <- generate_animation_from_figures_fun(filenames_figures = unlist(lf_mosaic_plot_fig[1:11]), |
|
364 |
# frame_speed = frame_speed, |
|
365 |
# format_file = animation_format, |
|
366 |
# out_suffix = out_suffix_str, |
|
367 |
# out_dir = out_dir, |
|
368 |
# out_filename_figure_animation = "test2_reg6_animation.gif") |
|
352 | 369 |
|
353 | 370 |
#started 17.36 Western time on Oct 10 and 18.18 |
354 |
out_filename_figure_animation <- generate_animation_from_figures_fun(filenames_figures = filenames_figures_mosaic, |
|
371 |
# 15.58 oct 11 for |
|
372 |
out_filename_figure_animation <- generate_animation_from_figures_fun(filenames_figures = filenames_figures_mosaic, |
|
355 | 373 |
frame_speed = frame_speed, |
356 | 374 |
format_file = animation_format, |
357 | 375 |
out_suffix = out_suffix_movie, |
Also available in: Unified diff
modifying plot animate raster time series in part 2 global product assessment