Revision 2b0d84dd
Added by Benoit Parmentier almost 11 years ago
climate/research/oregon/interpolation/global_run_scalingup_assessment_part1.R | ||
---|---|---|
5 | 5 |
#Analyses, figures, tables and data are also produced in the script. |
6 | 6 |
#AUTHOR: Benoit Parmentier |
7 | 7 |
#CREATED ON: 03/23/2014 |
8 |
#MODIFIED ON: 03/29/2014
|
|
9 |
#Version: 1
|
|
8 |
#MODIFIED ON: 05/01/2014
|
|
9 |
#Version: 2
|
|
10 | 10 |
#PROJECT: Environmental Layers project |
11 | 11 |
################################################################################################# |
12 | 12 |
|
... | ... | |
101 | 101 |
out_suffix <- list_param$out_suffix |
102 | 102 |
## Start |
103 | 103 |
|
104 |
input.rasters <- lapply(as.character(mosaic_list[[j]]), raster) |
|
104 |
if(class(mosaic_list[[j]])=="list"){ |
|
105 |
m_list <- unlist(mosaic_list[[j]]) |
|
106 |
} |
|
107 |
input.rasters <- lapply(m_list, raster) |
|
105 | 108 |
mosaiced_rast<-input.rasters[[1]] |
106 | 109 |
|
107 | 110 |
for (k in 2:length(input.rasters)){ |
... | ... | |
230 | 233 |
write.table((tb_diagnostic_v_NA), |
231 | 234 |
file=file.path(out_dir,paste("tb_diagnostic_v2_NA","_",out_prefix,".txt",sep="")),sep=",") |
232 | 235 |
|
233 |
####### PART 2 CREATE MOSAIC OF PREDICTIONS PER DAY ### |
|
236 |
#load data_month for specific tiles |
|
237 |
data_month <- extract_from_list_obj(robj1$clim_method_mod_obj,"data_month") |
|
234 | 238 |
|
235 |
tb <- tb_diagnostic_v_NA |
|
239 |
names(data_month) #this contains LST means (mm_1, mm_2 etc.) as well as TMax and other info |
|
240 |
|
|
241 |
#problem with tile 12...the raster ojbect has missing sub object |
|
242 |
#data_month_list <- lapply(1:length(list_raster_obj_files),x=list_raster_obj_files, |
|
243 |
# FUN=function(i,x){x<-load_obj(x[[i]]); |
|
244 |
# extract_from_list_obj(x$validation_mod_month_obj,"data_s")}) |
|
245 |
|
|
246 |
data_month_list <- lapply(1:length(list_raster_obj_files),x=list_raster_obj_files, |
|
247 |
FUN=function(i,x){x<-load_obj(x[[i]]); |
|
248 |
extract_from_list_obj(x$clim_method_mod_obj,"data_month")}) |
|
249 |
|
|
250 |
names(data_month_list) <- paste("tile","_",1:length(data_month_list),sep="") |
|
251 |
|
|
252 |
|
|
253 |
#names(data_month_list) <- basename(in_dir_list) #use folder id instead |
|
254 |
|
|
255 |
tile_id <- lapply(1:length(data_month_list), |
|
256 |
FUN=function(i,x){rep(names(x)[i],nrow(x[[i]]))},x=data_month_list) |
|
257 |
data_month_NAM <- do.call(rbind.fill,data_month_list) #combined data_month for "NAM" North America |
|
258 |
data_month_NAM$tile_id <- unlist(tile_id) |
|
259 |
|
|
260 |
#plot(mm_01 ~ elev_s,data=data_month_NAM) #Jan across all tiles |
|
261 |
#plot(mm_06 ~ elev_s,data=data_month_NAM) #June across all tiles |
|
262 |
#plot(TMax ~ mm_01,data=data_month_NAM) #monthly tmax averages and LST across all tiles |
|
263 |
|
|
264 |
#copy back to atlas |
|
265 |
system("scp -p ./*.txt ./*.tif parmentier@atlas.nceas.ucsb.edu:/data/project/layers/commons/NEX_data/_run1_NA_analyses_03232013") |
|
266 |
|
|
267 |
####### PART 2 CREATE MOSAIC OF PREDICTIONS PER DAY ### |
|
236 | 268 |
|
237 | 269 |
#list.files(path=in_dir_list[[1]],pattern=".*predicted.*20100101.*.tif") |
238 | 270 |
|
... | ... | |
242 | 274 |
#list_tif_files_dates <- lapply(in_dir_list,FUN=function(x){list.files(path=x,pattern=".*month.*.tif",full.names=T)}) |
243 | 275 |
#list_tif_files_dates <- lapply(in_dir_list,FUN=function(x){list.files(path=x,pattern=".*predicted.*20100101.*.tif",full.names=T)}) |
244 | 276 |
|
245 |
list_tif_files_dates <- lapply(in_dir_list, |
|
246 |
FUN=function(x){list.files(path=x,pattern=".*predicted_mod1_0_1.*20100101.*.tif",full.names=T)}) |
|
247 |
list_tif_files_dates <- lapply(in_dir_list,FUN=function(x){list.files(path=x,pattern=".*predicted_mod1_0_1.*20100901.*.tif",full.names=T)}) |
|
277 |
#".*predicted_mod1_0_1.*20100101.*.tif" |
|
278 |
|
|
279 |
tb <- tb_diagnostic_v_NA |
|
280 |
#get specific dates from tb |
|
281 |
dates_l <- unique(tb$date) |
|
282 |
|
|
283 |
list_tif_fun <- function(i,in_dir_list,pattern_str){ |
|
284 |
#list.files(path=x,pattern=".*predicted_mod1_0_1.*20100101.*.tif",full.names=T)}) |
|
285 |
pat_str<- pattern_str[i] |
|
286 |
list_tif_files_dates <-lapply(in_dir_list, |
|
287 |
FUN=function(x,pat_str){list.files(path=x,pattern=pat_str,full.names=T)},pat_str=pat_str) |
|
288 |
return(list_tif_files_dates) |
|
289 |
} |
|
290 |
|
|
291 |
#First mosaic mod1 |
|
292 |
|
|
293 |
## make this a function? report on number of tiles used for mosaic... |
|
294 |
|
|
295 |
#inputs |
|
296 |
l_pattern_models <- lapply(c(".*predicted_mod1_0_1.*",".*predicted_mod2_0_1.*",".*predicted_mod3_0_1.*"), |
|
297 |
FUN=function(x){paste(x,dates_l,".*.tif",sep="")}) |
|
298 |
out_prefix_s <- paste(c("gam_fusion_dailyTmax_"),c("predicted_mod1_0_01","predicted_mod2_0_01","predicted_mod3_0_01"),sep="") |
|
299 |
dates_l #list of predicted dates |
|
300 |
|
|
301 |
for (i in 1:lenth(l_pattern_models)){ |
|
302 |
|
|
303 |
l_pattern_mod <- l_pattern_models[[i]] |
|
304 |
out_prefix_s <- |
|
305 |
|
|
306 |
l_out_rastnames_var <- paste("gam_fusion_dailyTmax_","predicted_mod1_0_01_",dates_l,sep="") |
|
307 |
|
|
308 |
#list_tif_files_dates <- list_tif_fun(1,in_dir_list,l_pattern_mod) |
|
309 |
|
|
310 |
##List of predicted tif ... |
|
311 |
list_tif_files_dates <-lapply(1:length(l_pattern_mod1),FUN=list_tif_fun, |
|
312 |
in_dir_list=in_dir_list,pattern_str=l_pattern_mod) |
|
313 |
#save(list_tif_files_dates, file=paste("list_tif_files_dates","_",out_prefix,".RData",sep="")) |
|
314 |
|
|
315 |
|
|
316 |
mosaic_list_var <- list_tif_files_dates |
|
317 |
out_rastnames_var <- l_out_rastnames_var |
|
318 |
|
|
319 |
file_format <- ".tif" |
|
320 |
NA_flag_val <- -9999 |
|
321 |
|
|
322 |
j<-1 |
|
323 |
list_param_mosaic<-list(j,mosaic_list_var,out_rastnames_var,out_dir,file_format,NA_flag_val) |
|
324 |
names(list_param_mosaic)<-c("j","mosaic_list","out_rastnames","out_path","file_format","NA_flag_val") |
|
325 |
list_var_mosaiced <- mclapply(1:365,FUN=mosaic_m_raster_list,list_param=list_param_mosaic,mc.preschedule=FALSE,mc.cores = 2) |
|
326 |
|
|
327 |
} |
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
### Now find out how many files were predicted |
|
248 | 334 |
|
249 |
#list_tif_files_dates <- lapply(in_dir1,FUN=function(x){list.files(path=x,pattern=".*predicted_mod1_0_1.*20100101.*.tif",full.names=T)}) |
|
250 |
#list_tif_files_dates <- lapply(in_dir1,FUN=function(x){list.files(path=x,pattern=".*predicted_mod1_0_1.*20100901.*.tif",full.names=T)}) |
|
335 |
l_pattern_mod1<-paste(".*predicted_mod1_0_1.*",dates_l,".*.tif",sep="") |
|
251 | 336 |
|
337 |
l_f_t12 <- list.files(path=in_dir_list[12],".*predicted_mod1_0_1.*") |
|
252 | 338 |
|
253 |
#mosaic_list_var <- vector(list,length=1) |
|
254 |
#mosaic_list_var[[1]] <- unlist(list_tif_files_dates) |
|
255 |
mosaic_list_var <- list_tif_files_dates |
|
256 | 339 |
|
257 |
file_format <- ".tif" |
|
258 |
NA_flag_val <- -9999 |
|
259 |
#mosaic_list_var<-lapply(seq_len(nrow(x)), function(i) x[i,]) #list of tiles by batch to mosaic |
|
260 |
#Prepare list of output names without extension |
|
261 |
#out_rastnames_var <- (basename(gsub(list_tiles_modis[1],"",list_m_var[[1]]))) |
|
262 |
#out_rastnames_var <- gsub(extension(out_rastnames_var),"",out_rastnames_var) |
|
263 |
out_rastnames_var <- "gam_fusion_dailyTmax_predicted_mod1_0_01_20100101" |
|
264 |
out_rastnames_var <- "gam_fusion_dailyTmax_predicted_mod1_0_01_20100901" |
|
340 |
l_f_bytiles<-lapply(in_dir_list,function(x){list.files(path=x,pattern=".*predicted_mod1_0_1.*")}) |
|
265 | 341 |
|
266 |
j<-1 |
|
267 |
list_param_mosaic<-list(j,mosaic_list_var,out_rastnames_var,out_dir,file_format,NA_flag_val) |
|
268 |
names(list_param_mosaic)<-c("j","mosaic_list","out_rastnames","out_path","file_format","NA_flag_val") |
|
269 |
#undebug(mosaic_m_raster_list) |
|
270 |
list_var_mosaiced <- mosaic_m_raster_list(1,list_param_mosaic) |
|
271 |
#Parallelization,this works on MAC laptop too |
|
272 |
#list_var_mosaiced <-mclapply(1:length(mosaic_list_var), list_param=list_param_mosaic, mosaic_m_raster_list,mc.preschedule=FALSE,mc.cores = 2) #This is the end bracket from mclapply(...) statement |
|
273 | 342 |
|
274 |
#list_var_mosaiced <- lapply(1:length(mosaic_list_var), list_param=list_param_mosaic, mosaic_m_raster_list) #This is the end bracket from mclapply(...) statement
|
|
343 |
unlist(lapply(l_f_bytiles,length))
|
|
275 | 344 |
|
276 | 345 |
### Create a combined shape file for all region? |
277 | 346 |
|
... | ... | |
284 | 353 |
#mean predicitons |
285 | 354 |
#mean of kriging error? |
286 | 355 |
|
287 |
tb <- read.table("/data/project/layers/commons/NEX_data/test_run1_03232014/tb_diagnostic_v_NA_run1_NA_analyses_03232013.txt",sep=",") |
|
288 |
|
|
289 |
boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod=="mod1")) |
|
290 |
bwplot(rmse~as.factor(tile_id), data=subset(tb,tb$pred_mod=="mod1")) |
|
291 |
|
|
356 |
tb <- read.table(file.path(out_dir,"tb_diagnostic_v2_NA_run1_NA_analyses_03232013.txt"),sep=",") |
|
357 |
summary_metrics_v <- read.table(file.path(out_dir,"summary_metrics_v2_NA_run1_NA_analyses_03232013.txt"),sep=",") |
|
358 |
|
|
359 |
strsplit(unique(tb$tile_id),"_") |
|
360 |
tb$tile_id <- factor(tb$tile_id, levels=unique(tb$tile_id)) |
|
361 |
#tb$tile_id <- as.character(tb$tile_id) |
|
362 |
boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod=="mod1"), |
|
363 |
names=1:24) |
|
364 |
title("RMSE per tile") |
|
365 |
#bwplot(rmse~as.factor(tile_id), data=subset(tb,tb$pred_mod=="mod1")) |
|
366 |
|
|
367 |
#Boxplot of RMSE by model |
|
368 |
boxplot(rmse~pred_mod,data=tb)#,names=tb$pred_mod) |
|
369 |
title("RMSE per model over 24 tiles") |
|
370 |
|
|
371 |
#Turn summary table to a point shp |
|
372 |
|
|
373 |
tx<-strsplit(as.character(summary_metrics_v$tile_coord),"_") |
|
374 |
lat<- as.numeric(lapply(1:length(tx),function(i,x){x[[i]][1]},x=tx)) |
|
375 |
long<- as.numeric(lapply(1:length(tx),function(i,x){x[[i]][2]},x=tx)) |
|
376 |
summary_metrics_v$lat <- lat |
|
377 |
summary_metrics_v$lon <- long |
|
378 |
|
|
379 |
coordinates(summary_metrics_v) <- cbind(long,lat) |
|
380 |
|
|
381 |
ac_mod1 <- summary_metrics_v[summary_metrics_v$pred_mod=="mod1",] |
|
382 |
|
|
383 |
plot(r2) |
|
384 |
#plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T) |
|
385 |
plot(ac_mod1,cex=(ac_mod1$rmse^2)/10,pch=1,add=T) |
|
386 |
plot(ac_mod1,cex=(ac_mod1$rmse1)*2,pch=1,add=T) |
|
387 |
|
|
388 |
df <-arrange(as.data.frame(ac_mod1),desc(rmse))[,c("rmse","mae","tile_id")] |
|
389 |
#View(df) |
|
390 |
#quick kriging... |
|
391 |
autokrige(rmse~1,r2,) |
|
292 | 392 |
### COMBINED SHAPEFILES AND EXAMING CENTROID |
293 | 393 |
|
294 |
#usa_map <- getData('GADM', country='USA', level=1) #Get US map |
|
295 |
usa_map <- getData('GADM', country='USA',level=1) #Get US map, this is not working right now |
|
296 |
|
|
297 |
usa_map_2 <- usa_map[usa_map$NAME_1!="Alaska",] #remove Alaska |
|
298 |
usa_map_2 <- usa_map_2[usa_map_2$NAME_1!="Hawaii",] #remove Hawai |
|
299 |
|
|
300 | 394 |
##Get the list of shapefiles |
301 | 395 |
in_dir_list_NEX <- read.table("/data/project/layers/commons/NEX_data/test_run1_03232014/output/in_dir_list.txt",sep=" ") |
302 | 396 |
|
... | ... | |
308 | 402 |
lat_shp<- lapply(1:length(tx),function(i,x){x[[i]][1]},x=tx) |
309 | 403 |
long_shp<- lapply(1:length(tx),function(i,x){x[[i]][2]},x=tx) |
310 | 404 |
|
405 |
summary_metrics |
|
311 | 406 |
#print.numeric<-function(x, digits = 2) formatC(x, digits = digits, format = "f") |
312 | 407 |
#print.numeric(long_shp[[1]]) |
313 | 408 |
|
314 | 409 |
pattern_str<-lapply(1:length(long_shp),function(i,y,x){paste(y[[i]],"0_",x[[i]],"0",sep="")},y=lat_shp,x=long_shp) |
315 |
|
|
410 |
#Select the 24 tiles that were used in the predictions based on lat, long |
|
316 | 411 |
list_shp_reg_files <- lapply(pattern_str,FUN=function(x){list.files(path=in_dir_shp, |
317 | 412 |
pattern=paste("*.",x,".*.shp$",sep=""),full.names=T)}) |
318 | 413 |
### |
... | ... | |
320 | 415 |
#ghcn_dat <- readOGR(dsn=dirname(met_stations_obj$monthly_covar_ghcn_data), |
321 | 416 |
# sub(".shp","",basename(met_stations_obj$monthly_covar_ghcn_data))) |
322 | 417 |
|
323 |
plot(r44) |
|
324 |
plot(usa_map_2) |
|
418 |
#plot(r44) |
|
419 |
|
|
420 |
#usa_map <- getData('GADM', country='USA', level=1) #Get US map |
|
421 |
usa_map <- getData('GADM', country='USA',level=1) #Get US map, this is not working right now |
|
422 |
usa_map_2 <- usa_map[usa_map$NAME_1!="Alaska",] #remove Alaska |
|
423 |
usa_map_2 <- usa_map_2[usa_map_2$NAME_1!="Hawaii",] #remove Hawai |
|
424 |
|
|
325 | 425 |
centroids_pts <- vector("list",length(list_shp_reg_files)) |
426 |
shps_tiles <- vector("list",length(list_shp_reg_files)) |
|
427 |
#collect info |
|
326 | 428 |
for(i in 1:length(list_shp_reg_files)){ |
327 | 429 |
shp1<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]]))) |
328 |
plot(shp1,add=T,border="blue") |
|
329 | 430 |
pt <- gCentroid(shp1) |
330 |
plot(pt,add=T,cex=2,pch=5) |
|
331 | 431 |
centroids_pts[[i]] <-pt |
432 |
shps_tiles[[i]] <- shp1 |
|
433 |
} |
|
434 |
#plot info: with labels |
|
435 |
plot(usa_map_2) |
|
436 |
for(i in 1:length(list_shp_reg_files)){ |
|
437 |
shp1 <- shps_tiles[[i]] |
|
438 |
pt <- centroids_pts[[i]] |
|
439 |
plot(shp1,add=T,border="blue") |
|
440 |
#plot(pt,add=T,cex=2,pch=5) |
|
441 |
text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1,col=c("red")) |
|
332 | 442 |
} |
443 |
title("Tiles location 10x10 degrees for NAM") |
|
333 | 444 |
|
445 |
## Now plot RMSE: with labels |
|
446 |
plot(usa_map_2) |
|
447 |
for(i in 1:length(list_shp_reg_files)){ |
|
448 |
shp1 <- shps_tiles[[i]] |
|
449 |
pt <- centroids_pts[[i]] |
|
450 |
plot(shp1,add=T,border="blue") |
|
451 |
#plot(pt,add=T,cex=2,pch=5) |
|
452 |
text(coordinates(pt)[1],coordinates(pt)[2],labels=i,cex=1,col=c("red")) |
|
453 |
} |
|
454 |
title("Tiles location 10x10 degrees for NAM") |
|
455 |
|
|
456 |
|
|
457 |
#unique(summaty_metrics$tile_id) |
|
458 |
#text(lat-shp,) |
|
334 | 459 |
#union(list_shp_reg_files[[1]],list_shp_reg_files[[2]]) |
335 | 460 |
|
461 |
l_m_tif <- list.files(pattern="*mosaiced.*.tif") |
|
462 |
r1 <- raster("mosaiced_gam_fusion_dailyTmax_predicted_mod1_0_01_20100101.tif") |
|
463 |
r2 <- raster("mosaiced_gam_fusion_dailyTmax_predicted_mod1_0_01_20100901.tif") |
|
464 |
|
|
465 |
pred_s <- stack(l_m_tif[2:5]) |
|
466 |
levelplot(pred_s) |
Also available in: Unified diff
assessing scaling up North America run, plots of tiles limit, RMSE, mosaic etc.