Revision e83cb2cb
Added by Benoit Parmentier over 9 years ago
climate/research/oregon/interpolation/global_run_scalingup_mosaicing.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 |
#Different options to explore mosaicing are tested. |
|
5 |
#Analyses, figures, tables and data are also produced in the script. |
|
6 |
#AUTHOR: Benoit Parmentier |
|
7 |
#CREATED ON: 04/14/2015 |
|
8 |
#MODIFIED ON: 04/14/2015 |
|
9 |
#Version: 4 |
|
10 |
#PROJECT: Environmental Layers project |
|
11 |
#COMMENTS: analyses for run 10 global analyses,all regions 1500x4500km and other tiles |
|
12 |
#TODO: |
|
13 |
#1) Split functions and master script |
|
14 |
#2) Make this is a script/function callable from the shell/bash |
|
15 |
#3) Check image format for tif |
|
16 |
|
|
17 |
################################################################################################# |
|
18 |
|
|
19 |
### Loading R library and packages |
|
20 |
#library used in the workflow production: |
|
21 |
library(gtools) # loading some useful tools |
|
22 |
library(mgcv) # GAM package by Simon Wood |
|
23 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
24 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
25 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
26 |
library(gstat) # Kriging and co-kriging by Pebesma et al. |
|
27 |
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines |
|
28 |
library(raster) # Hijmans et al. package for raster processing |
|
29 |
library(gdata) # various tools with xls reading, cbindX |
|
30 |
library(rasterVis) # Raster plotting functions |
|
31 |
library(parallel) # Parallelization of processes with multiple cores |
|
32 |
library(maptools) # Tools and functions for sp and other spatial objects e.g. spCbind |
|
33 |
library(maps) # Tools and data for spatial/geographic objects |
|
34 |
library(reshape) # Change shape of object, summarize results |
|
35 |
library(plotrix) # Additional plotting functions |
|
36 |
library(plyr) # Various tools including rbind.fill |
|
37 |
library(spgwr) # GWR method |
|
38 |
library(automap) # Kriging automatic fitting of variogram using gstat |
|
39 |
library(rgeos) # Geometric, topologic library of functions |
|
40 |
#RPostgreSQL # Interface R and Postgres, not used in this script |
|
41 |
library(gridExtra) |
|
42 |
#Additional libraries not used in workflow |
|
43 |
library(pgirmess) # Krusall Wallis test with mulitple options, Kruskalmc {pgirmess} |
|
44 |
library(colorRamps) |
|
45 |
library(zoo) |
|
46 |
library(xts) |
|
47 |
|
|
48 |
#### FUNCTION USED IN SCRIPT |
|
49 |
|
|
50 |
function_analyses_paper1 <-"contribution_of_covariates_paper_interpolation_functions_07182014.R" #first interp paper |
|
51 |
function_analyses_paper2 <-"multi_timescales_paper_interpolation_functions_08132014.R" |
|
52 |
|
|
53 |
load_obj <- function(f) |
|
54 |
{ |
|
55 |
env <- new.env() |
|
56 |
nm <- load(f, env)[1] |
|
57 |
env[[nm]] |
|
58 |
} |
|
59 |
|
|
60 |
create_dir_fun <- function(out_dir,out_suffix){ |
|
61 |
if(!is.null(out_suffix)){ |
|
62 |
out_name <- paste("output_",out_suffix,sep="") |
|
63 |
out_dir <- file.path(out_dir,out_name) |
|
64 |
} |
|
65 |
#create if does not exists |
|
66 |
if(!file.exists(out_dir)){ |
|
67 |
dir.create(out_dir) |
|
68 |
} |
|
69 |
return(out_dir) |
|
70 |
} |
|
71 |
|
|
72 |
#Remove models that were not fitted from the list |
|
73 |
#All modesl that are "try-error" are removed |
|
74 |
remove_errors_list<-function(list_items){ |
|
75 |
|
|
76 |
#This function removes "error" items in a list |
|
77 |
list_tmp<-list_items |
|
78 |
if(is.null(names(list_tmp))){ |
|
79 |
names(list_tmp) <- paste("l",1:length(list_tmp),sep="_") |
|
80 |
names(list_items) <- paste("l",1:length(list_tmp),sep="_") |
|
81 |
} |
|
82 |
|
|
83 |
for(i in 1:length(list_items)){ |
|
84 |
if(inherits(list_items[[i]],"try-error")){ |
|
85 |
list_tmp[[i]]<-0 |
|
86 |
}else{ |
|
87 |
list_tmp[[i]]<-1 |
|
88 |
} |
|
89 |
} |
|
90 |
cnames<-names(list_tmp[list_tmp>0]) |
|
91 |
x <- list_items[match(cnames,names(list_items))] |
|
92 |
return(x) |
|
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
plot_daily_mosaics <- function(i,list_param_plot_daily_mosaics){ |
|
97 |
#Purpose: |
|
98 |
#This functions mask mosaics files for a default range (-100,100 deg). |
|
99 |
#It produces a masked tif in a given dataType format (FLT4S) |
|
100 |
#It creates a figure of mosaiced region being interpolated. |
|
101 |
#Author: Benoit Parmentier |
|
102 |
#Parameters: |
|
103 |
#lf_m: list of files |
|
104 |
#reg_name:region name with tile size included |
|
105 |
#To do: |
|
106 |
#Add filenames |
|
107 |
#Add range |
|
108 |
#Add output dir |
|
109 |
#Add dataType_val option |
|
110 |
|
|
111 |
##### BEGIN ######## |
|
112 |
|
|
113 |
#Parse the list of parameters |
|
114 |
lf_m <- list_param_plot_daily_mosaics$lf_m |
|
115 |
reg_name <- list_param_plot_daily_mosaics$reg_name |
|
116 |
out_dir_str <- list_param_plot_daily_mosaics$out_dir_str |
|
117 |
out_suffix <- list_param_plot_daily_mosaics$out_suffix |
|
118 |
l_dates <- list_param_plot_daily_mosaics$l_dates |
|
119 |
|
|
120 |
|
|
121 |
#list_param_plot_daily_mosaics <- list(lf_m=lf_m,reg_name=reg_name,out_dir_str=out_dir_str) |
|
122 |
|
|
123 |
|
|
124 |
#rast_list <- vector("list",length=length(lf_m)) |
|
125 |
r_test<- raster(lf_m[i]) |
|
126 |
|
|
127 |
m <- c(-Inf, -100, NA, |
|
128 |
-100, 100, 1, |
|
129 |
100, Inf, NA) #can change the thresholds later |
|
130 |
rclmat <- matrix(m, ncol=3, byrow=TRUE) |
|
131 |
rc <- reclassify(r_test, rclmat,filename=paste("rc_tmp_",i,".tif",sep=""),dataType="FLT4S",overwrite=T) |
|
132 |
file_name <- unlist(strsplit(basename(lf_m[i]),"_")) |
|
133 |
|
|
134 |
#date_proc <- file_name[7] #specific tot he current naming of files |
|
135 |
date_proc <- l_dates[i] |
|
136 |
#paste(raster_name[1:7],collapse="_") |
|
137 |
#add filename option later |
|
138 |
extension_str <- extension(filename(r_test)) |
|
139 |
raster_name_tmp <- gsub(extension_str,"",basename(filename(r_test))) |
|
140 |
raster_name <- file.path(out_dir_str,paste(raster_name_tmp,"_masked.tif",sep="")) |
|
141 |
r_pred <- mask(r_test,rc,filename=raster_name,overwrite=TRUE) |
|
142 |
|
|
143 |
res_pix <- 1200 |
|
144 |
#res_pix <- 480 |
|
145 |
|
|
146 |
col_mfrow <- 1 |
|
147 |
row_mfrow <- 1 |
|
148 |
|
|
149 |
png(filename=paste("Figure9_clim_mosaics_day_test","_",date_proc,"_",reg_name,"_",out_suffix,".png",sep=""), |
|
150 |
width=col_mfrow*res_pix,height=row_mfrow*res_pix) |
|
151 |
|
|
152 |
plot(r_pred,main=paste("Predicted on ",date_proc , " ", reg_name,sep=""),cex.main=1.5) |
|
153 |
dev.off() |
|
154 |
|
|
155 |
return(raster_name) |
|
156 |
|
|
157 |
} |
|
158 |
|
|
159 |
plot_screen_raster_val<-function(i,list_param){ |
|
160 |
##USAGE### |
|
161 |
#Screen raster list and produced plot as png. |
|
162 |
fname <-list_param$lf_raster_fname[i] |
|
163 |
screenRast <- list_param$screenRast |
|
164 |
l_dates<- list_param$l_dates |
|
165 |
out_dir_str <- list_param$out_dir_str |
|
166 |
prefix_str <-list_param$prefix_str |
|
167 |
out_suffix_str <- list_param$out_suffix_str |
|
168 |
|
|
169 |
### START SCRIPT #### |
|
170 |
date_proc <- l_dates[i] |
|
171 |
|
|
172 |
if(screenRast==TRUE){ |
|
173 |
r_test <- raster(fname) |
|
174 |
|
|
175 |
m <- c(-Inf, -100, NA, |
|
176 |
-100, 100, 1, |
|
177 |
100, Inf, NA) #can change the thresholds later |
|
178 |
rclmat <- matrix(m, ncol=3, byrow=TRUE) |
|
179 |
rc <- reclassify(r_test, rclmat,filename=paste("rc_tmp_",i,".tif",sep=""),dataType="FLT4S",overwrite=T) |
|
180 |
#file_name <- unlist(strsplit(basename(lf_m[i]),"_")) |
|
181 |
extension_str <- extension(filename(r_test)) |
|
182 |
raster_name_tmp <- gsub(extension_str,"",basename(filename(r_test))) |
|
183 |
raster_name <- file.path(out_dir_str,paste(raster_name_tmp,"_masked.tif",sep="")) |
|
184 |
|
|
185 |
r_pred <- mask(r_test,rc,filename=raster_name,overwrite=TRUE) |
|
186 |
}else{ |
|
187 |
r_pred <- raster(fname) |
|
188 |
} |
|
189 |
|
|
190 |
#date_proc <- file_name[7] #specific tot he current naming of files |
|
191 |
#date_proc<- "2010010101" |
|
192 |
|
|
193 |
#paste(raster_name[1:7],collapse="_") |
|
194 |
#add filename option later |
|
195 |
|
|
196 |
res_pix <- 960 |
|
197 |
#res_pix <- 480 |
|
198 |
|
|
199 |
col_mfrow <- 1 |
|
200 |
row_mfrow <- 1 |
|
201 |
|
|
202 |
# png(filename=paste("Figure10_clim_world_mosaics_day_","_",date_proc,"_",tile_size,"_",out_suffix,".png",sep=""), |
|
203 |
# width=col_mfrow*res_pix,height=row_mfrow*res_pix) |
|
204 |
png(filename=paste(prefix_str,"_",date_proc,"_",tile_size,"_",out_suffix_str,".png",sep=""), |
|
205 |
width=col_mfrow*res_pix,height=row_mfrow*res_pix) |
|
206 |
|
|
207 |
plot(r_pred,main=paste("Predicted on ",date_proc , " ", tile_size,sep=""),cex.main=1.5) |
|
208 |
dev.off() |
|
209 |
|
|
210 |
} |
|
211 |
|
|
212 |
############################################ |
|
213 |
#### Parameters and constants |
|
214 |
|
|
215 |
#on ATLAS |
|
216 |
|
|
217 |
in_dir <- "~/Data/IPLANT_project/mosaicing_data_test/reg2" |
|
218 |
|
|
219 |
y_var_name <- "dailyTmax" #PARAM1 |
|
220 |
interpolation_method <- c("gam_CAI") #PARAM2 |
|
221 |
out_suffix <- "mosaic_run10_1500x4500_global_analyses_03252015" #PARAM3 |
|
222 |
out_dir <- in_dir #PARAM4 |
|
223 |
create_out_dir_param <- TRUE #PARAM 5 |
|
224 |
|
|
225 |
mosaic_plot <- FALSE #PARAM6 |
|
226 |
|
|
227 |
#if daily mosaics NULL then mosaicas all days of the year |
|
228 |
day_to_mosaic <- c("20100101","20100102","20100103","20100104","20100105", |
|
229 |
"20100301","20100302","20100303","20100304","20100305", |
|
230 |
"20100501","20100502","20100503","20100504","20100505", |
|
231 |
"20100701","20100702","20100703","20100704","20100705", |
|
232 |
"20100901","20100902","20100903","20100904","20100905", |
|
233 |
"20101101","20101102","20101103","20101104","20101105") #PARAM7 |
|
234 |
|
|
235 |
#CRS_locs_WGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 |
|
236 |
CRS_WGS84 <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 #CONSTANT1 |
|
237 |
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 |
|
238 |
|
|
239 |
proj_str<- CRS_WGS84 #PARAM 8 #check this parameter |
|
240 |
file_format <- ".rst" #PARAM 9 |
|
241 |
NA_value <- -9999 #PARAM10 |
|
242 |
NA_flag_val <- NA_value |
|
243 |
|
|
244 |
tile_size <- "1500x4500" #PARAM 11 |
|
245 |
mulitple_region <- TRUE #PARAM 12 |
|
246 |
|
|
247 |
region_name <- "world" #PARAM 13 |
|
248 |
plot_region <- FALSE |
|
249 |
|
|
250 |
########################## START SCRIPT ############################## |
|
251 |
|
|
252 |
|
|
253 |
####### PART 1: Read in data ######## |
|
254 |
|
|
255 |
if(create_out_dir_param==TRUE){ |
|
256 |
out_dir <- create_dir_fun(out_dir,out_suffix) |
|
257 |
setwd(out_dir) |
|
258 |
}else{ |
|
259 |
setwd(out_dir) #use previoulsy defined directory |
|
260 |
} |
|
261 |
|
|
262 |
setwd(out_dir) |
|
263 |
|
|
264 |
###Table 1: Average accuracy metrics |
|
265 |
################## PLOTTING WORLD MOSAICS ################ |
|
266 |
|
|
267 |
lf_mosaic_pred_1500x4500 <-list.files(path=file.path(in_dir), |
|
268 |
pattern=paste(".*.tif$",sep=""),full.names=T) |
|
269 |
|
|
270 |
r1 <- raster(lf_mosaic_pred_1500x4500[11]) |
|
271 |
r2 <- raster(lf_mosaic_pred_1500x4500[2]) |
|
272 |
|
|
273 |
assignVal<- function(x){rep(1,x)} |
|
274 |
r_outline <- init(r2,fun=assignVal,filename="test.rst",overwrite=T) |
|
275 |
r_pol <- rasterToPolygons(r_outline,dissolve=T) |
|
276 |
|
|
277 |
#centroid distance |
|
278 |
c1 <- gCentroid(x,byid=TRUE) |
|
279 |
pt <- gCentroid(shp1) |
|
280 |
|
|
281 |
#then distance... |
|
282 |
gDistance |
|
283 |
|
|
284 |
|
|
285 |
#Then scale on 1 to zero? or 0 to 1000 |
|
286 |
#e.g. for a specific pixel |
|
287 |
#weight_sum=0.2 +0.4 +0.4+0.2=1.2 |
|
288 |
#sum_val (0.2*val1+0.4*val2+0.4*val3+0.2*val4) |
|
289 |
#m_val= /weight_sum |
|
290 |
|
|
291 |
#can do mosaic with sum?? for both weighted sum and val |
|
292 |
# |
|
293 |
#can use gdal calc... |
|
294 |
|
|
295 |
r_m <- r1 + r2 |
|
296 |
name_method <- paste(interpolation_method,"_",y_var_name,"_",sep="") |
|
297 |
##Use python code written by Alberto Guzman |
|
298 |
|
|
299 |
#system("MODULEPATH=$MODULEPATH:/nex/modules/files") |
|
300 |
#system("module load /nex/modules/files/pythonkits/gdal_1.10.0_python_2.7.3_nex") |
|
301 |
lf1 <- lf_world_pred_1000x3000 |
|
302 |
lf2 <- lf_world_pred_1500x4500 |
|
303 |
|
|
304 |
#module_path <- "" |
|
305 |
module_path <- "/nobackupp6/aguzman4/climateLayers/sharedCode/" |
|
306 |
#sh /nobackupp6/aguzman4/climateLayers/sharedCode/gdalCalDiff.sh file1.tif file2.tif output.tif |
|
307 |
#/nobackupp6/aguzman4/climateLayers/sharedCode/mosaicUsingGdalMerge.py |
|
308 |
#l_dates <- paste(day_to_mosaic,collapse=",",sep=" ") |
|
309 |
l_dates <- paste(day_to_mosaic,collapse=",") |
|
310 |
## use region 2 first |
|
311 |
lf_out <- paste("diff_world_","1000_3000","by1500_4500_","mod1","_",l_dates,out_suffix,"_",file_format,sep="") |
|
312 |
|
|
313 |
|
|
314 |
for (i in 1:length(lf_out)){ |
|
315 |
out_file <- lf_out[i] |
|
316 |
in_file1 <- lf1[i] |
|
317 |
in_file2 <- lf2[i] |
|
318 |
|
|
319 |
cmd_str <- paste("sh", file.path(module_path,"gdalCalDiff.sh"), |
|
320 |
in_file1, |
|
321 |
in_file2, |
|
322 |
out_file,sep=" ") |
|
323 |
system(cmd_str) |
|
324 |
|
|
325 |
} |
|
326 |
|
|
327 |
##################### END OF SCRIPT ###################### |
Also available in: Unified diff
global scaling up initial commit for mosaicing script to experiment with different weigted averages to deal with boundaries