Project

General

Profile

« Previous | Next » 

Revision af9fe762

Added by Benoit Parmentier over 9 years ago

initial commit for assessment of time series profiles from predicted mosaic for North America year 2010

View differences:

climate/research/oregon/interpolation/global_run_scalingup_time_series_analyses.R
1
##############################  INTERPOLATION OF TEMPERATURES  #######################################
2
#######################  Script for assessment of scaling up on NEX ##############################
3
#This script uses outputs from the worklfow code applied to the globe. Results currently reside on NEX/PLEIADES NASA.
4
#We examine outputs from time series mosaiced regionally and globablly.
5
#Analyses, figures, tables and data are also produced in the script.
6
#AUTHOR: Benoit Parmentier 
7
#CREATED ON: 04/27/2015  
8
#MODIFIED ON: 04/30/2015            
9
#Version: 1
10
#PROJECT: NCEAS/IPLANT/NASA Environmental Layers project     
11
#COMMENTS: analyses for run 10 global analyses,all regions 1500x4500km and other tiles
12
#TODO:
13
#
14
#################################################################################################
15

  
16
### Loading R library and packages        
17
#library used in the workflow production:
18
library(gtools)                              # loading some useful tools 
19
library(mgcv)                                # GAM package by Simon Wood
20
library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
21
library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
22
library(rgdal)                               # GDAL wrapper for R, spatial utilities
23
library(gstat)                               # Kriging and co-kriging by Pebesma et al.
24
library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
25
library(raster)                              # Hijmans et al. package for raster processing
26
library(gdata)                               # various tools with xls reading, cbindX
27
library(rasterVis)                           # Raster plotting functions
28
library(parallel)                            # Parallelization of processes with multiple cores
29
library(maptools)                            # Tools and functions for sp and other spatial objects e.g. spCbind
30
library(maps)                                # Tools and data for spatial/geographic objects
31
library(reshape)                             # Change shape of object, summarize results 
32
library(plotrix)                             # Additional plotting functions
33
library(plyr)                                # Various tools including rbind.fill
34
library(spgwr)                               # GWR method
35
library(automap)                             # Kriging automatic fitting of variogram using gstat
36
library(rgeos)                               # Geometric, topologic library of functions
37
#RPostgreSQL                                 # Interface R and Postgres, not used in this script
38
library(gridExtra)
39
#Additional libraries not used in workflow
40
library(pgirmess)                            # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess}  
41
library(colorRamps)                          # Palettes for raster maps
42
library(zoo)                                 # time series analysis objects and tools/functionalities
43
library(xts)                                 # time series analysis extended tools
44

  
45
#### FUNCTION USED IN SCRIPT
46

  
47
function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_07182014.R" #first interp paper
48
function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_08132014.R"
49

  
50
load_obj <- function(f)
51
{
52
  env <- new.env()
53
  nm <- load(f, env)[1]
54
  env[[nm]]
55
}
56

  
57
create_dir_fun <- function(out_dir,out_suffix){
58
  if(!is.null(out_suffix)){
59
    out_name <- paste("output_",out_suffix,sep="")
60
    out_dir <- file.path(out_dir,out_name)
61
  }
62
  #create if does not exists
63
  if(!file.exists(out_dir)){
64
    dir.create(out_dir)
65
  }
66
  return(out_dir)
67
}
68

  
69
 #Remove models that were not fitted from the list
70
#All modesl that are "try-error" are removed
71
remove_errors_list<-function(list_items){
72
  
73
  #This function removes "error" items in a list
74
  list_tmp<-list_items
75
    if(is.null(names(list_tmp))){
76
    names(list_tmp) <- paste("l",1:length(list_tmp),sep="_")
77
    names(list_items) <- paste("l",1:length(list_tmp),sep="_")
78
  }
79

  
80
  for(i in 1:length(list_items)){
81
    if(inherits(list_items[[i]],"try-error")){
82
      list_tmp[[i]]<-0
83
    }else{
84
    list_tmp[[i]]<-1
85
   }
86
  }
87
  cnames<-names(list_tmp[list_tmp>0])
88
  x <- list_items[match(cnames,names(list_items))]
89
  return(x)
90
}
91

  
92

  
93
############################################
94
#### Parameters and constants  
95

  
96
#on ATLAS
97

  
98
in_dir <- "/data/project/layers/commons/NEX_data/output_run10_1500x4500_global_analyses_04172015/mosaics"
99

  
100
y_var_name <- "dailyTmax" #PARAM1
101
interpolation_method <- c("gam_CAI") #PARAM2
102
out_suffix <- "ts_run10_1500x4500_global_analyses_04172015" #PARAM3
103
out_dir <- in_dir #PARAM4
104
create_out_dir_param <- TRUE #PARAM 5
105
  
106
#CRS_locs_WGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
107
CRS_WGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 #CONSTANT1
108
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
109

  
110
proj_str<- CRS_WGS84 #PARAM 8 #check this parameter
111
file_format <- ".rst" #PARAM 9
112
NA_value <- -9999 #PARAM10
113
NA_flag_val <- NA_value
114
                                   
115
tile_size <- "1500x4500" #PARAM 11
116

  
117
region_name <- "North_America" #PARAM 13
118

  
119
########################## START SCRIPT ##############################
120

  
121

  
122
####### PART 1: Read in data ########
123

  
124
if(create_out_dir_param==TRUE){
125
  out_dir <- create_dir_fun(out_dir,out_suffix)
126
  setwd(out_dir)
127
}else{
128
  setwd(out_dir) #use previoulsy defined directory
129
}
130

  
131
setwd(out_dir)
132

  
133
################## PLOTTING  MOSAICS AND TIME SERIES ################
134

  
135
lf_mosaic_pred_1500x4500 <-list.files(path=file.path(in_dir),    
136
           pattern=paste("reg1.*.tif$",sep=""),full.names=T) 
137
r_stack <- stack(lf_mosaic_pred_1500x4500)#
138

  
139
summary_metrics_v <- read.table(file=file.path("/data/project/layers/commons/NEX_data/output_run10_1500x4500_global_analyses_04172015",paste("summary_metrics_v2_NA_","run10_1500x4500_global_analyses_04172015",".txt",sep="")),sep=",")
140
coordinates(summary_metrics_v) <- cbind(summary_metrics_v$lon,summary_metrics_v$lat)
141

  
142
r1 <- raster(lf_mosaic_pred_1500x4500[11]) 
143

  
144
idx <- seq(as.Date('2010-01-01'), as.Date('2010-12-31'), 'day')
145
date_l <- strptime(idx[1], "%Y%m%d") # interpolation date being processed
146
dates_l <- format(idx, "%Y%m%d") # interpolation date being processed
147

  
148
## Plot mosaics for North America (region1) for daily predictions in 2010
149

  
150
res_pix <- 480
151

  
152
col_mfrow <- 2
153
row_mfrow <- 1
154
  
155
#  png(filename=paste("Figure10_clim_world_mosaics_day_","_",date_proc,"_",tile_size,"_",out_suffix,".png",sep=""),
156
#    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
157
png(filename=paste("Figure1_","time_series_step_in_raster_mosaics",dates_l[11],"_",out_suffix,".png",sep=""),
158
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
159

  
160
plot(r1)
161
plot(summary_metrics_v,add=T)
162
text(summary_metrics_v,summary_metrics_v$tile_id,cex=1.4)
163

  
164
dev.off()
165

  
166
## Get pixel time series at centroids of tiles used in the predictions
167

  
168
df_ts_pixel <- extract(r_stack,summary_metrics_v,df=T,sp=F)
169

  
170
df_ts_pixel <- cbind(summary_metrics_v,df_ts_pixel)
171

  
172
#make a function later on?
173

  
174
#inputs
175
list_selected_pix <- c("tile_4","tile_6","tile_8","tile_11","tile_14","tile_3","tile_5","tile_7","tile_38","tile_12")
176
list_pix <- vector("list",length=length(list_selected_pix))
177
#idx <- seq(as.Date('2010-01-01'), as.Date('2010-12-31'), 'day')
178
#df_ts_pix
179

  
180
#Select one pix to profile/plot
181

  
182
df_ts_pix <- subset(df_ts_pixel,pred_mod=="mod1")
183

  
184
for(i in 1:length(list_selected_pix)){
185
  
186
  selected_pix <- list_selected_pix[i]
187
  data_pixel <- subset(df_ts_pix,tile_id==selected_pix)
188
  pix <- t(data_pixel[1,24:388])
189
  d_z <- zoo(pix,idx) #make a time series ...
190
  list_pix[[i]] <- pix
191

  
192
  res_pix <- 480
193

  
194
  col_mfrow <- 2
195
  row_mfrow <- 1
196
  
197
  #  png(filename=paste("Figure10_clim_world_mosaics_day_","_",date_proc,"_",tile_size,"_",out_suffix,".png",sep=""),
198
  #    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
199
  png(filename=paste("Figure2_","pixel_profile_",selected_pix,"_",out_suffix,".png",sep=""),
200
    width=col_mfrow*res_pix,height=row_mfrow*res_pix)
201

  
202

  
203
  plot(d_z,type="b")
204
  title(paste("Pixel time series",selected_pix,sep=" "))
205

  
206
 dev.off()
207
}
208

  
209
data_dz <- do.call(cbind,list_pix)
210
colnames(data_dz) <- list_selected_pix
211
data_dz <- zoo(data_dz,idx)
212

  
213
list_selected_pix <- c("tile_4","tile_6","tile_8","tile_11","tile_14","tile_3","tile_5","tile_7","tile_38","tile_12")
214
df_ts_pix2 <- subset(df_ts_pix,tile_id%in% list_selected_pix)
215

  
216
pix_data <- t(df_ts_pix2[,24:388])
217

  
218
d_z2 <- zoo(pix_data,idx)
219
names(d_z2)<-
220

  
221
##################### END OF SCRIPT ######################

Also available in: Unified diff