Project

General

Profile

« Previous | Next » 

Revision 42bdc7c9

Added by Benoit Parmentier about 10 years ago

scaling up NEX assessment part 2, addding RMSE visualization with raster for 75% overlap run

View differences:

climate/research/oregon/interpolation/global_run_scalingup_assessment_part2.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: 09/30/2014            
8
#MODIFIED ON: 10/05/2014            
9 9
#Version: 3
10 10
#PROJECT: Environmental Layers project     
11 11
#COMMENTS: analyses for run 5 global using 6 specific tiles
......
37 37
#Additional libraries not used in workflow
38 38
library(pgirmess)                            # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess}  
39 39
library(colorRamps)
40
library(zoo)
41
library(xts)
40 42

  
41 43
#### FUNCTION USED IN SCRIPT
42 44

  
......
167 169
  return(l_rast[4])
168 170
}
169 171

  
172
assign_FID_spatial_polygons_df <-function(list_spdf,ID_str=NULL){
173
  list_spdf_tmp <- vector("list",length(list_spdf))
174
  if(is.null(ID_str)){
175
    nf <- 0 #number of features
176
    #for(i in 1:length(spdf)){
177
    #    shp1 <- list_spdf[[i]]
178
    #    f <- nrow(shp1)
179
    #    nf <- nf + f
180
    #}
181
    #This assumes that the list has one feature per item list
182
    nf <- length(list_spdf)
183
    ID_str <- as.character(1:nf)
184
  }
185
  for(i in 1:length(list_spdf)){
186
    #test=spRbind(shps_tiles[[1]],shps_tiles[[2]])
187
    shp1 <- list_spdf[[i]]
188
    shp1$FID <- ID_str
189
    shp1<- spChFIDs(shp1, as.character(shp1$FID)) #assign ID
190
    list_spdf_tmp[[i]]  <-shp1
191
  }
192
  return(list_spdf_tmp)
193
}
194

  
195
combine_spatial_polygons_df_fun <- function(list_spdf_tmp,ID_str=NULL){
196
  if(is.null(ID_str)){
197
    #call function
198
    list_spdf_tmp <- assign_FID_spatial_polygons_df
199
  }
200
  combined_spdf <- list_spdf_tmp[[1]]
201
  for(i in 2:length(list_spdf_tmp)){
202
    combined_spdf <- rbind(combined_spdf,list_spdf_tmp[[i]])
203
    #sapply(slot(shps_tiles[[2]], "polygons"), function(x) slot(x, "ID"))
204
    #rownames(as(alaska.tract, "data.frame"))
205
  }
206
  return(combined_spdf)
207
}
208

  
170 209
##############################
171 210
#### Parameters and constants  
172 211

  
......
176 215
in_dir1 <- "/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/output20Deg2"
177 216
# parent output dir for the curent script analyes
178 217
#out_dir <-"/data/project/layers/commons/NEX_data/output_run3_global_analyses_06192014/" #On NCEAS Atlas
179
out_dir <-"/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/"
218
out_dir <-"/data/project/layers/commons/NEX_data/output_run7_global_analyses_10042014/"
180 219
# input dir containing shapefiles defining tiles
181 220
#in_dir_shp <- "/data/project/layers/commons/NEX_data/output_run5_global_analyses_08252014/output/subset/shapefiles"
182 221

  
......
189 228

  
190 229
y_var_name <- "dailyTmax"
191 230
interpolation_method <- c("gam_CAI")
192
out_prefix<-"run6_global_analyses_09162014"
231
out_prefix<-"run7_global_analyses_10042014"
193 232
mosaic_plot <- FALSE
194 233

  
195 234
proj_str<- CRS_WGS84
......
269 308
#/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles
270 309
for(i in 1:length(list_shp_reg_files)){
271 310
  #path_to_shp <- dirname(list_shp_reg_files[[i]])
272
  path_to_shp <- "/data/project/layers/commons/NEX_data/output_run6_global_analyses_09162014/shapefiles"
311
  path_to_shp <- file.path(out_dir,"/shapefiles")
273 312
  layer_name <- sub(".shp","",basename(list_shp_reg_files[[i]]))
274 313
  shp1 <- readOGR(path_to_shp, layer_name)
275 314
  #shp1<-readOGR(dirname(list_shp_reg_files[[i]]),sub(".shp","",basename(list_shp_reg_files[[i]])))
......
280 319
#coord_names <- c("lon","lat")
281 320
#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)
282 321

  
283

  
284 322
#plot info: with labels
285 323
res_pix <- 1200
286 324
col_mfrow <- 1
......
476 514
#print(p)
477 515
#dev.off()
478 516

  
479

  
480 517
######################
481 518
### Figure 5: plot accuracy ranked 
482 519

  
......
569 606
  #plot(ac_mod1,cex=sqrt(ac_mod1$rmse),pch=1,add=T)
570 607
  #plot(ac_mod,cex=(ac_mod$rmse^2)/10,pch=1,col="red",add=T)
571 608

  
609
  coordinates(ac_mod) <- ac_mod[,c("lon","lat")] 
572 610
  p_shp <- layer(sp.polygons(reg_layer, lwd=1, col='black'))
573 611
  #title("(a) Mean for 1 January")
574 612
  p <- bubble(ac_mod,"rmse",main=paste("Averrage RMSE per tile and by ",model_name[i]))
......
744 782

  
745 783
#l_date_selected <- unique(tb$date)
746 784
idx <- seq(as.Date('2010-01-01'), as.Date('2010-12-31'), 'day')
785
#idx <- seq(as.Date('2010-01-01'),by="day", length=365)
786

  
747 787
#idx <- seq(as.Date('20100101'), as.Date('20101231'), 'day')
748 788
#date_l <- strptime(idx[1], "%Y%m%d") # interpolation date being processed
749 789
l_date_selected <- format(idx, "%Y%m%d") # interpolation date being processed
......
769 809
list_param_raster_from_tb$mod_selected <- "mod2"
770 810
list_param_raster_from_tb$var_selected <- "rmse"
771 811
l_rast <- lapply(1:length(l_date_selected),FUN=create_raster_from_tb_diagnostic,list_param=list_param_raster_from_tb)
772
r_mod1_rmse <- stack(l_rast)
812
r_mod2_rmse <- stack(l_rast)
773 813
list_param_raster_from_tb$var_selected <- "n"
774 814
l_rast_n<- lapply(1:length(l_date_selected),FUN=create_raster_from_tb_diagnostic,list_param=list_param_raster_from_tb)
775
r_mod1_n <- stack(l_rast_n)
815
r_mod2_n <- stack(l_rast_n)
816

  
817
#r_mod2_n <- stack(list.files(pattern="r_nn_mod2.*.rst"))
818
#r_mod1_n <- stack(list.files(pattern="r_nn_mod1.*.rst"))
819

  
820
l_rast_n[[1]]
821
#"./r_nn_mod2_20101231_run7_global_analyses_10042014.rst"
776 822

  
823
# Create a raster stack with time series tag
824
r_mod2_rmse <- setZ(r_mod2_rmse, idx)
825
names(r_mod2_rmse) <- l_date_selected
826
r_mod2_n <- setZ(r_mod2_n, idx)
827
names(r_mod2_n) <- l_date_selected
777 828

  
829
writeRaster(r_mod2_n,by=F)
830

  
831
levelplot(r_mod2_rmse,layers=1:12,panel=panel.levelplot.raster, col.regions=matlab.like(25))+p_shp
832

  
833
p_hist <- histogram(r_mod2_rmse,FUN=as.yearmon)
834
p_hist2 <- p_hist
835
print(p_hist)
836
p_hist$x.limits <- rep(list(c(-2,6)),12)
837
print(p_hist)
838
p_bw <- bwplot(r_mod2_rmse,FUN=as.yearmon)
839
print(p_bw)
840
p_bw2 <- p_bw
841
p_bw2$y.limits <- c(-2,6)
842
print(p_bw2)
843
r_mod2_rmse_m <- zApply(r_mod2_rmse,by=as.yearmon,fun="mean")
844
p_lev_rmse_m <- levelplot(r_mod2_rmse_m,panel=panel.levelplot.raster,col.regions=matlab.like(25)) + p_shp
845
print(p_lev_rmse_m)
846

  
847
## analysis of the daily nmber of validation stations per tile
848
r_mod2_n_m <- zApply(r_mod2_n,by=as.yearmon,fun="mean")
849
p_lev_n_m <- levelplot(r_mod2_n_m,panel=panel.levelplot.raster,col.regions=matlab.like(25))+p_shp
850
print(p_lev_n_m)
851

  
852
p_bw_n2 <- bwplot(r_mod2_n,FUN=as.yearmon,do.out=F)
853
print(p_bw_n)
854
p_bw_n$y.limits <- c(0,16)
855
print(p_bw_n)
856
p_ho <- hovmoller(r_mod2_rmse)
857

  
858
p_lev_n <- levelplot(r_mod2_n,layers=1:12,panel=panel.levelplot.raster, col.regions=matlab.like(25))+p_shp
859

  
860
print(p_lev_n)
861

  
862
### mod1
863

  
864
#as(r_mod2_rmse)
778 865

  
779 866
#Make this a function...
780 867
#test <- subset(tb,pred_mod=="mod1" & date=="20100101",select=c("tile_id","n","mae","rmse","me","r","m50"))
......
807 894
}
808 895

  
809 896

  
810
dd <- merge(df_tile_processed,pred_data_month_info,by="tile_id")
811
coordinates(dd) <- c(dd$x,dd$y)
812

  
813
## Make this a function later...
814
list_shp_tmp <- vector("list",length(shps_tiles))
815
for(i in 1:length(shps_tiles)){
816
  #test=spRbind(shps_tiles[[1]],shps_tiles[[2]])
817
  shp1 <- shps_tiles[[i]]
897
# dd <- merge(df_tile_processed,pred_data_month_info,by="tile_id")
898
# coordinates(dd) <- c(dd$x,dd$y)
899
# 
818 900

  
819
  ID_str <- unlist(strsplit(as.character(df_tile_processed$tile_id[i]),"_"))[2]
820
  shp1$FID <- ID_str
821
  shp1<- spChFIDs(shp1, as.character(shp1$FID)) #assign ID
822
  list_shp_tmp[[i]]  <-shp1
823
}
901
######################
902
### Figure 9: Plot the number of stations in a processing tile
824 903

  
825
combined_shp <- list_shp_tmp[[1]]
826
for(i in 2:length(list_shp_tmp)){
827
  combined_shp <- rbind(combined_shp,list_shp_tmp[[i]])
828
  #sapply(slot(shps_tiles[[2]], "polygons"), function(x) slot(x, "ID"))
829
  #rownames(as(alaska.tract, "data.frame"))
830
}
904
## Get ID from tile number...
905
#ID_str <- unlist(lapply(1:nrow(df_tile_processed),function(i){unlist(strsplit(as.character(df_tile_processed$tile_id[i]),"_"))[2]}))
831 906

  
832
combined_shp$tile_id <- df_tile_processed$tile_id
907
#combined_shp$tile_id <- df_tile_processed$tile_id
833 908
 
834
test <- combined_shp
835
test2 <- merge(test,pred_data_month_info, by="tile_id")
909
#r <- raster(lf_pred_list[i])
836 910

  
837
r <- raster(lf_pred_list[i])
911
#plot(combined_shp)
838 912

  
839
plot(combined_shp)
840
# polygons
841
plot(combined_shp, col = fillColour, border = outlineColour)
913
#p0 <- spplot(combined_shp, "Stations",col.regions=matlab.like(100))
914
#p1 <- spplot(usa_map,"ISO",colorkey=FALSE) #Use ISO instead of NAME_1 to see no color?
915
#p2 <- spplot(can_map,"ISO",colorkey=FALSE) #Use ISO instead of NAME_1 to see no color?
916
#p3 <- spplot(mex_map,"ISO",colorkey=FALSE) #Use ISO instead of NAME_1 to see no color?
842 917

  
843
p0 <- spplot(combined_shp, "Stations",col.regions=matlab.like(100))
844
p1 <- spplot(reg_layer,"ISO",colorkey=FALSE) #Use ISO instead of NAME_1 to see no color?
845
p0 +p1
918
#p0 +p1+p2+p3
846 919

  
847 920
### Now plot number of training for monthly data
848 921

  
849
df_dat <- subset(pred_data_month_info, pred_mod == "mod1" & date =="20100115")
922
#df_dat <- subset(pred_data_month_info, pred_mod == "mod1" & date =="20100115")
850 923
#shp_dat <-merge(combined_shp,df_dat,by="tile_id")
851
shp_dat <-merge(x=combined_shp,y=df_dat,by="tile_id",all.x=T) #if tile is missing then add rows with NA
924
#shp_dat <-merge(x=combined_shp,y=df_dat,by="tile_id",all.x=T) #if tile is missing then add rows with NA
852 925
#shp_dat <- merge(shp_dat,df_tile_processed,by="tile_id")
853 926
#coordinates(shp_dat) <- cbind(shp_dat$lon,shp_dat$lat) 
854 927
#proj4string(shp_dat) <- CRS_WGS84
855 928
  
856 929
#test <- overlay(combined_shp,shp_dat)
857
pol <- SpatialPolygons(combined_shp@polygons,proj4string=CRS(CRS_WGS84))
858
spp <- SpatialPolygonsDataFrame(pol,data=shp_dat)
930
#pol <- SpatialPolygons(combined_shp@polygons,proj4string=CRS(CRS_WGS84))
931
#spp <- SpatialPolygonsDataFrame(pol,data=shp_dat)
859 932

  
860
p0 <- spplot(spp, "n_mod",col.regions=matlab.like(100))
861
p1 <- spplot(reg_layer,"ISO",colorkey=FALSE) #Use ISO instead of NAME_1 to see no color?
862
p0 +p1
933
#p0 <- spplot(spp, "n_mod",col.regions=matlab.like(100))
934
#p1 <- spplot(reg_layer,"ISO",colorkey=FALSE) #Use ISO instead of NAME_1 to see no color?
935
#p0 + p1 + p2 + p3
863 936

  
864 937
##################### END OF SCRIPT ######################

Also available in: Unified diff