Revision 553a9eee
Added by Benoit Parmentier almost 11 years ago
climate/research/oregon/interpolation/global_run_scalingup_assessment_part1.R | ||
---|---|---|
1 |
#################################### INTERPOLATION OF TEMPERATURES ####################################### |
|
2 |
############################ Script for assessment of scaling up on NEX ############################## |
|
3 |
#This script uses the worklfow code applied to the globe. Results currently reside on NEX/PLEIADES NASA. |
|
4 |
#Accuracy methods are added in the the function scripts to evaluate results. |
|
5 |
#Analyses, figures, tables and data are also produced in the script. |
|
6 |
#AUTHOR: Benoit Parmentier |
|
7 |
#CREATED ON: 03/23/2014 |
|
8 |
#MODIFIED ON: 03/23/2014 |
|
9 |
#Version: 1 |
|
10 |
#PROJECT: Environmental Layers project |
|
11 |
################################################################################################# |
|
12 |
|
|
13 |
### Loading R library and packages |
|
14 |
#library used in the workflow production: |
|
15 |
library(gtools) # loading some useful tools |
|
16 |
library(mgcv) # GAM package by Simon Wood |
|
17 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
18 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
19 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
20 |
library(gstat) # Kriging and co-kriging by Pebesma et al. |
|
21 |
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines |
|
22 |
library(raster) # Hijmans et al. package for raster processing |
|
23 |
library(gdata) # various tools with xls reading, cbindX |
|
24 |
library(rasterVis) # Raster plotting functions |
|
25 |
library(parallel) # Parallelization of processes with multiple cores |
|
26 |
library(maptools) # Tools and functions for sp and other spatial objects e.g. spCbind |
|
27 |
library(maps) # Tools and data for spatial/geographic objects |
|
28 |
library(reshape) # Change shape of object, summarize results |
|
29 |
library(plotrix) # Additional plotting functions |
|
30 |
library(plyr) # Various tools including rbind.fill |
|
31 |
library(spgwr) # GWR method |
|
32 |
library(automap) # Kriging automatic fitting of variogram using gstat |
|
33 |
library(rgeos) # Geometric, topologic library of functions |
|
34 |
#RPostgreSQL # Interface R and Postgres, not used in this script |
|
35 |
library(gridExtra) |
|
36 |
#Additional libraries not used in workflow |
|
37 |
library(pgirmess) # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess} |
|
38 |
library(colorRamps) |
|
39 |
|
|
40 |
#### FUNCTION USED IN SCRIPT |
|
41 |
|
|
42 |
function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_10222013.R" #first interp paper |
|
43 |
function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_03182014.R" |
|
44 |
|
|
45 |
load_obj <- function(f) |
|
46 |
{ |
|
47 |
env <- new.env() |
|
48 |
nm <- load(f, env)[1] |
|
49 |
env[[nm]] |
|
50 |
} |
|
51 |
|
|
52 |
############################## |
|
53 |
#### Parameters and constants |
|
54 |
|
|
55 |
script_path<-"/home/parmentier/Data/IPLANT_project/env_layers_scripts/" #path to script |
|
56 |
source(file.path(script_path,function_analyses_paper1)) #source all functions used in this script 1. |
|
57 |
source(file.path(script_path,function_analyses_paper2)) #source all functions used in this script 2. |
|
58 |
|
|
59 |
|
|
60 |
#in_dir1 <- "/data/project/layers/commons/NEX_data/test_run1_03232014/output" #On Atlas |
|
61 |
in_dir1 <- "/nobackupp4/aguzman4/climateLayers/output" #On NEX |
|
62 |
in_dir_list <- list.dirs(path=in_dir1) #get the list of directories with resutls by 10x10 degree tiles |
|
63 |
#in_dir_list <- as.list(in_dir_list[-1]) |
|
64 |
in_dir_list <- in_dir_list[-1] #the first one is the in_dir1 |
|
65 |
in_dir_list <- in_dir_list[-25] # the last directory contains shapefiles #]in_dir_list[-1] |
|
66 |
|
|
67 |
|
|
68 |
##raster_prediction object : contains testing and training stations with RMSE and model object |
|
69 |
|
|
70 |
list_raster_obj_files <- lapply(in_dir_list,FUN=function(x){list.files(path=x,pattern="^raster_prediction_obj.*.RData",full.names=T)}) |
|
71 |
|
|
72 |
names(list_raster_obj_files)<- paste("tile",1:length(list_raster_obj_files),sep="_") |
|
73 |
|
|
74 |
y_var_name <- "dailyTmax" |
|
75 |
out_prefix<-"run1_NA_analyses_03232013" |
|
76 |
#out_dir<-"/data/project/layers/commons/NEX_data/" |
|
77 |
out_dir <- "/nobackup/bparmen1/" |
|
78 |
out_dir <-paste(out_dir,"_",out_prefix,sep="") |
|
79 |
|
|
80 |
#system("ls /nobackup/bparmen1") |
|
81 |
|
|
82 |
if (!file.exists(out_dir)){ |
|
83 |
dir.create(out_dir) |
|
84 |
#} else{ |
|
85 |
# out_path <-paste(out_path..) |
|
86 |
} |
|
87 |
setwd(out_dir) |
|
88 |
|
|
89 |
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 |
|
90 |
|
|
91 |
###################### PART I: Generate tables to collect information over all tiles in North America ########## |
|
92 |
#Table 1: Average accuracy metrics |
|
93 |
#Table 2: daily accuracy metrics for all tiles |
|
94 |
|
|
95 |
##Quick exploration of raster object |
|
96 |
robj1 <- load_obj(list_raster_obj_files[[1]]) |
|
97 |
names(robj1) |
|
98 |
names(robj1$clim_method_mod_obj[[1]]$data_month) #for January |
|
99 |
names(robj1$validation_mod_month_obj[[1]]$data_s) #for January with predictions |
|
100 |
|
|
101 |
data_month_list <- lapply(list_raster_obj_files,FUN=function(x){x<-load_obj(x);x[["summary_metrics_v"]]$avg}) |
|
102 |
|
|
103 |
robj1$tb_diagnostic_v[1:10,] #first 10 rows of accuarcy metrics per day and model (for specific tile) |
|
104 |
robj1$summary_metrics_v #first 10 rows of accuarcy metrics per day and model (for specific tile) |
|
105 |
|
|
106 |
#summary_metrics_v_list <- lapply(list_raster_obj_files,FUN=function(x){x<-load_obj(x);x[["summary_metrics_v"]]$avg$rmse}) |
|
107 |
summary_metrics_v_list <- lapply(list_raster_obj_files,FUN=function(x){x<-load_obj(x);x[["summary_metrics_v"]]$avg}) |
|
108 |
#summary_metrics_v_NA <- do.call(rbind,summary_metrics_v_list) #create a df for NA tiles with all accuracy metrics |
|
109 |
summary_metrics_v_NA <- do.call(rbind.fill,summary_metrics_v_list) #create a df for NA tiles with all accuracy metrics |
|
110 |
tile_id <- lapply(1:length(summary_metrics_v_list),FUN=function(i,x){rep(names(x)[i],nrow(x[[i]]))},x=summary_metrics_v_list) |
|
111 |
summary_metrics_v_NA$tile_id <-unlist(tile_id) |
|
112 |
summary_metrics_v_NA$n <- as.integer(summary_metrics_v_NA$n) |
|
113 |
write.table(as.data.frame(summary_metrics_v_NA),file=paste("summary_metrics_v_NA_",out_prefix,".txt",sep=""),sep=",") |
|
114 |
#Function to collect all the tables from tiles into a table |
|
115 |
|
|
116 |
tb_diagnostic_v_list <- lapply(list_raster_obj_files,FUN=function(x){x<-load_obj(x);x[["tb_diagnostic_v"]]}) |
|
117 |
names(tb_diagnostic_v_list) |
|
118 |
|
|
119 |
tb_diagnostic_v_NA <- do.call(rbind.fill,tb_diagnostic_v_list) #create a df for NA tiles with all accuracy metrics |
|
120 |
tile_id <- lapply(1:length(tb_diagnostic_v_list),FUN=function(i,x){rep(names(x)[i],nrow(x[[i]]))},x=tb_diagnostic_v_list) |
|
121 |
tb_diagnostic_v_NA$tile_id <- unlist(tile_id) #adding identifier for tile |
|
122 |
write.table((tb_diagnostic_v_NA),file=paste("tb_diagnostic_v_NA","_",out_prefix,".txt",sep=""),sep=",") |
|
123 |
|
|
124 |
### Create a combined shape file for all region? |
|
125 |
|
|
126 |
#get centroid |
|
127 |
#plot the average RMSE at the centroid?? |
|
128 |
#quick kriging for every tile? |
|
129 |
|
|
130 |
### Create a combined boxplot for every tile (can also do that in pannel) |
|
131 |
### Create a quick mosaic (mean method...) |
|
132 |
#mean predicitons |
|
133 |
#mean of kriging error? |
|
134 |
|
|
135 |
tb <- read.table("/data/project/layers/commons/NEX_data/test_run1_03232014/tb_diagnostic_v_NA_run1_NA_analyses_03232013.txt",sep=",") |
|
136 |
|
|
137 |
boxplot(rmse~tile_id,data=subset(tb,tb$pred_mod=="mod1")) |
|
138 |
bwplot(rmse~as.factor(tile_id), data=subset(tb,tb$pred_mod=="mod1")) |
Also available in: Unified diff
assessing scaling up, North America run initial commit