Project

General

Profile

« Previous | Next » 

Revision b11c177e

Added by Benoit Parmentier over 10 years ago

run6 assessment NEX part2: adding new functions to generate raster map of accuracy overall and daily

View differences:

climate/research/oregon/interpolation/global_run_scalingup_assessment_part2.R
62 62
  return(out_dir)
63 63
}
64 64

  
65
#Function to rasterize a table with coordinates and variables...,maybe add option for ref image??
66
rasterize_df_fun <- function(data_tb,coord_names,proj_str,out_suffix,out_dir=".",file_format=".rst",NA_flag_val=-9999,tolerance_val= 0.000120005){
67
  data_spdf <- data_tb
68
  coordinates(data_spdf) <- cbind(data_spdf[[coord_names[1]]],data_spdf[[coord_names[2]]])
69
  proj4string(data_spdf) <- proj_str
70

  
71
  data_pix <- try(as(data_spdf,"SpatialPixelsDataFrame"))
72
  #tolerance_val <- 0.000120005 
73
  #tolerance_val <- 0.000856898
74
  if(inherits(data_pix,"try-error")){
75
      data_pix <- SpatialPixelsDataFrame(data_spdf, data=data_spdf@data, tolerance=tolerance_val) 
76
  }
77
  
78
  #test <- as(data_spdf,"SpatialPixelsDataFrame")
79

  
80
  # set up an 'empty' raster, here via an extent object derived from your data
81
  #e <- extent(s100[,1:2])
82
  #e <- e + 1000 # add this as all y's are the same
83

  
84
  #r <- raster(e, ncol=10, nrow=2)
85
  # or r <- raster(xmn=, xmx=,  ...
86

  
87
  data_grid <- as(data_pix,"SpatialGridDataFrame") #making it a regural grid
88
  r_ref <- raster(data_grid) #this is the ref image
89
  rast_list <- vector("list",length=ncol(data_tb))
90
  
91
  for(i in 1:(ncol(data_tb))){
92
    field_name <- names(data_tb)[i]
93
    var <- as.numeric(data_spdf[[field_name]])
94
    data_spdf$var  <- var
95
    #r <-rasterize(data_spdf,r_ref,field_name)
96
    r <-rasterize(data_spdf,r_ref,"var",NAflag=NA_flag_val,fun=mean) #prolem with NA in NDVI!!
97

  
98
    data_name<-paste("r_",field_name,sep="") #can add more later...
99
    #raster_name<-paste(data_name,out_names[j],".tif", sep="")
100
    raster_name<-paste(data_name,out_suffix,file_format, sep="")
101
  
102
    writeRaster(r, NAflag=NA_flag_val,filename=file.path(out_dir,raster_name),overwrite=TRUE)
103
    #Writing the data in a raster file format...
104
    rast_list[i] <-file.path(out_dir,raster_name)
105
  }
106
  return(unlist(rast_list))
107
}
108

  
65 109

  
66 110
##############################
67 111
#### Parameters and constants  
......
87 131
interpolation_method <- c("gam_CAI")
88 132
out_prefix<-"run6_global_analyses_09162014"
89 133

  
134
proj_str<- CRS_WGS84
135
file_format <- ".rst"
136
NA_value <- -9999
137
NA_flag_val <- NA_value
138
out_suffix <-out_prefix  
90 139

  
91 140
#out_dir <-paste(out_dir,"_",out_prefix,sep="")
92 141
create_out_dir_param <- FALSE
......
156 205
shps_tiles <- vector("list",length(list_shp_reg_files))
157 206
#collect info: read in all shapfiles
158 207
#This is slow...make a function and use mclapply??
159
/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles
208
#/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles
160 209
for(i in 1:length(list_shp_reg_files)){
161 210
  #path_to_shp <- dirname(list_shp_reg_files[[i]])
162 211
  path_to_shp <- "/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles"
......
167 216
  centroids_pts[[i]] <-pt
168 217
  shps_tiles[[i]] <- shp1
169 218
}
219
coord_names <- c("lon","lat")
220
l_rast <- rasterize_df_fun(test,coord_names,proj_str,out_suffix=out_prefix,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005)
170 221

  
171 222

  
172 223
#plot info: with labels
......
303 354
#xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
304 355
#                                           pred_mod!="mod_kr"),type="h")
305 356

  
306
#xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
307
#                                           pred_mod!="mod_kr"),type="h")
357
xyplot(n~pred_mod | tile_id,data=subset(as.data.frame(summary_metrics_v),
358
                                           pred_mod!="mod_kr"),type="h")
308 359

  
309
#xyplot(n~month | tile_id + pred_mod,data=subset(as.data.frame(tb_month_s),
360
#xyplot(n~month | tile_id + pred_mod,data=subset(as.data.frame(tb_month_s_),
310 361
#                                           pred_mod!="mod_kr"),type="h")
311 362

  
312 363
# 
......
500 551
######################
501 552
### Figure 9: Plot the number of stations in a processing tile
502 553

  
503
dd <- merge(df_tile_processed,pred_data_month_info,"tile_id")
554
#data_tb <- merge(df_tile_processed,summary_metrics_v,by="tile_id") #keep only the common id, id tiles with pred ac
555
#data_tb <- merge(df_tile_processed,summary_metrics_v,by="tile_id",all=T) #keep all
556

  
557
mod1_data_tb <- merge(df_tile_processed,subset(summary_metrics_v,pred_mod=="mod1"),by="tile_id",all=T) #keep all
558
mod2_data_tb <- merge(df_tile_processed,subset(summary_metrics_v,pred_mod=="mod2"),by="tile_id",all=T) #keep all
559
mod_kr_data_tb <- merge(df_tile_processed,subset(summary_metrics_v,pred_mod=="mod_kr"),by="tile_id",all=T) #keep all
560

  
561
##First create an image of the tiles, use ratify?? with tile id as the raster id for the attribute table??
562
#Transform table text file into a raster image
563
#coord_names <- c("XCoord","YCoord")
564
coord_names <- c("lon.x","lat.x")
565
#l_r_ast <- rasterize_df_fun(df_tile_processed,coord_names,proj_str,out_suffix,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005)
566
out_suffix_str <- paste("mod1_",out_suffix)
567
mod1_l_rast <- rasterize_df_fun(mod1_data_tb,coord_names,proj_str,out_suffix_str,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005)
568
mod1_stack <- stack(mod1_l_rast)
569
names(mod1_stack) <- names(mod1_data_tb)
570

  
571
out_suffix_str <- paste("mod2_",out_suffix)
572
mod2_l_rast <- rasterize_df_fun(mod2_data_tb,coord_names,proj_str,out_suffix_str,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005)
573
mod2_stack <- stack(mod2_l_rast)
574
names(mod2_stack) <- names(mod2_data_tb)
575

  
576
out_suffix_str <- paste("mod_kr_",out_suffix)
577
mod_kr_l_rast <- rasterize_df_fun(mod_kr_data_tb,coord_names,proj_str,out_suffix_str,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005)
578
mod_kr_stack <- stack(mod_kr_l_rast)
579
names(mod_kr_stack) <- names(mod_kr_data_tb)
580

  
581
#Number of daily predictions 
582
p0 <- levelplot(mod1_stack,layer=21,margin=F)
583
p<- p0+p_shp
584
print(p)
585
p0<- levelplot(mod2_stack,layer=21,margin=F)
586
p<- p0+p_shp
587
print(p)
588
plot(mod_kr_stack,y=21)
589

  
590
#RMSE
591
p0 <- levelplot(mod1_stack,layer=10,margin=F,col.regions=matlab.like(25),main="Average RMSE for mod1")
592
p<- p0+p_shp
593
print(p)
594

  
595
p0 <- levelplot(mod1_stack,layer=10,margin=F,col.regions=matlab.like(25),main="Average RMSE for mod2")
596
p<- p0+p_shp
597
print(p)
598

  
599
#plot(mod1_stack,y=10)
600
#plot(mod2_stack,y=10)
601
#plot(mod_kr_stack,y=10)
602

  
603
## Can make maps of daily and accuracy metrics!!!
604

  
605
#mod1_tb <- subset(tb,pred_mod=="mod1")
606

  
607
## loop or create image for specific day
608
#proj_str<- CRS_WGS84
609
#file_format <- ".rst"
610
#NA_value <- -9999
611
#NA_flag_val <- NA_value
612
#out_suffix <-out_prefix  
613

  
614
#first need to join x and y coord
615
date_selected <- "20100101"
616
mod_selected <- "mod1"
617
var_selected <- "rmse"
618
#test2_data_tb <- merge(df_tile_processed,test2,by="tile_id",all=T) #keep all
619

  
620
#l_date_selected <- unique(tb$date)
621
idx <- seq(as.Date('2010-01-01'), as.Date('2010-12-31'), 'day')
622
#idx <- seq(as.Date('20100101'), as.Date('20101231'), 'day')
623
#date_l <- strptime(idx[1], "%Y%m%d") # interpolation date being processed
624
l_date_selected <- format(idx, "%Y%m%d") # interpolation date being processed
625

  
626
list_param_raster_from_tb <- list(tb_dat,df_tile_processed,l_date_selected,
627
                                  mod_selected,var_selected,out_suffix,
628
                                  proj_str,file_format,NA_value,NA_flag_val)
629

  
630
names(list_param_raster_from_tb) <- c("tb_dat","df_tile_processed","date_selected",
631
                                      "mod_selected","var_selected","out_suffix",
632
                                      "proj_str","file_format","NA_value","NA_flag_val")
633
#undebug(create_raster_from_tb_diagnostic)
634
rast <- create_raster_from_tb_diagnostic (i,list_param=list_param_raster_from_tb)
635
l_rast <- lapply(1:length(l_date_selected),FUN=create_raster_from_tb_diagnostic,list_param=list_param_raster_from_tb)
636
r_rmse <- stack(l_rast)
637
#Make this a function...
638
#test <- subset(tb,pred_mod=="mod1" & date=="20100101",select=c("tile_id","n","mae","rmse","me","r","m50"))
639

  
640
plot_raster_tb_diagnostic(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix)
641
mod_selected <- "mod2"
642
plot_raster_tb_diagnostic(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix)
643
mod_selected <- "mod1"
644
date_selected  <- "20100901"
645
plot_raster_tb_diagnostic(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix)
646

  
647
list_date_selected <- c("20100101","20100901")
648
for (i in 1:length(list_date_selected)){
649
  mod_selected <- "mod1"
650
  date_selected  <- list_date_selected[i]
651
  var_selected <- "rmse"
652
  plot_raster_tb_diagnostic(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix)
653
  var_selected <- "n"
654
  plot_raster_tb_diagnostic(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix)
655
}
656

  
657
list_date_selected <- c("20100101","20100901")
658
for (i in 1:length(list_date_selected)){
659
  mod_selected <- "mod2"
660
  date_selected  <- list_date_selected[i]
661
  var_selected <- "rmse"
662
  plot_raster_tb_diagnostic(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix)
663
  var_selected <- "n"
664
  plot_raster_tb_diagnostic(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix)
665
}
666

  
667
plot_raster_tb_diagnostic <- function(reg_layer,tb_dat,df_tile_processed,date_selected,mod_selected,var_selected,out_suffix){
668
  
669
  test <- subset(tb_dat,pred_mod==mod_selected & date==date_selected,select=c("tile_id",var_selected))
670

  
671
  test_data_tb <- merge(df_tile_processed,test,by="tile_id",all=T) #keep all
672
  test_r <- subset(test_data_tb,select=c("lat","lon","tile_id",var_selected))
673
  out_suffix_str <- paste(var_selected,mod_selected,date_selected,out_suffix,sep="_")
674
  coord_names <- c("lon","lat")
675
  l_rast <- rasterize_df_fun(test_r,coord_names,proj_str,out_suffix_str,out_dir=".",file_format=".tif",NA_flag_val=-9999,tolerance_val=0.000120005)
676
  #mod_kr_stack <- stack(mod_kr_l_rast)
677
  d_tb_rast <- stack(l_rast)
678
  names(d_tb_rast) <- names(test_r)
679
  #plot(d_tb_rast)
680
  r <- subset(d_tb_rast,"rmse")
681
  names(r) <- paste(mod_selected,var_selected,date_selected,sep="_")
682
  #plot info: with labels
683

  
684
  res_pix <- 1200
685
  col_mfrow <- 1
686
  row_mfrow <- 1
687

  
688
  png(filename=paste("Figure9_",names(r),"_map_processed_region_",region_name,"_",out_prefix,".png",sep=""),
689
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
690

  
691
  #plot(reg_layer)
692
  #p1 <- spplot(reg_layer,"ISO",colorkey=FALSE) #Use ISO instead of NAME_1 to see no color?
693
  title_str <- paste(names(r),"for ", region_name,sep="")
694

  
695
  p0 <- levelplot(r,col.regions=matlab.like(25),margin=F,main=title_str)
696
  p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
697

  
698
  p <- p0 + p_shp
699
  print(p)
700

  
701
  dev.off()
702

  
703
}
704

  
705
create_raster_from_tb_diagnostic <- function(i,list_param){
706
  #create a raster image using tile centroids and given fields  from tb diagnostic data
707
  tb_dat <- list_param$tb_dat
708
  df_tile_processed <- list_param$df_tile_processed
709
  date_selected <- list_param$date_selected[i]
710
  mod_selected <- list_param$mod_selected
711
  var_selected <- list_param$var_selected
712
  out_suffix <- list_param$out_suffix
713
  
714
  test <- subset(tb_dat,pred_mod==mod_selected & date==date_selected,select=c("tile_id",var_selected))
715

  
716
  test_data_tb <- merge(df_tile_processed,test,by="tile_id",all=T) #keep all
717
  test_r <- subset(test_data_tb,select=c("lat","lon","tile_id",var_selected))
718
  out_suffix_str <- paste(var_selected,mod_selected,date_selected,out_suffix,sep="_")
719
  coord_names <- c("lon","lat")
720
  l_rast <- rasterize_df_fun(test_r,coord_names,proj_str,out_suffix_str,out_dir=".",file_format,NA_flag_val,tolerance_val=0.000120005)
721
  #mod_kr_stack <- stack(mod_kr_l_rast)
722
  #d_tb_rast <- stack(l_rast)
723
  #r <- subset(d_tb_rast,var_selected)
724
  #names(d_tb_rast) <- names(test_r)
725
  return(l_rast[4])
726
}
727

  
728
dd <- merge(df_tile_processed,pred_data_month_info,by="tile_id")
504 729
coordinates(dd) <- c(dd$x,dd$y)
505 730

  
506 731
## Make this a function later...

Also available in: Unified diff