10 |
10 |
# -MODIS LST: mean and obs
|
11 |
11 |
#3) The output is a multiband file in tif format with projected covariates for the processing region/tile.
|
12 |
12 |
#AUTHOR: Benoit Parmentier
|
13 |
|
#DATE: 05/14/2013
|
|
13 |
#DATE: 05/24/2013
|
14 |
14 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--
|
15 |
15 |
|
16 |
16 |
##Comments and TODO:
|
... | ... | |
87 |
87 |
return(rast_list)
|
88 |
88 |
}
|
89 |
89 |
|
|
90 |
mosaic_m_raster_list<-function(j,list_param){
|
|
91 |
#This functions returns a subset of tiles from the modis grid.
|
|
92 |
#Arguments: modies grid tile,list of tiles
|
|
93 |
#Output: spatial grid data frame of the subset of tiles
|
|
94 |
#Note that rasters are assumed to be in the same projection system!!
|
|
95 |
|
|
96 |
#rast_list<-vector("list",length(mosaic_list))
|
|
97 |
#for (i in 1:length(mosaic_list)){
|
|
98 |
# read the individual rasters into a list of RasterLayer objects
|
|
99 |
# this may be changed so that it is not read in the memory!!!
|
|
100 |
|
|
101 |
#parse output...
|
|
102 |
|
|
103 |
#j<-list_param$j
|
|
104 |
mosaic_list<-list_param$mosaic_list
|
|
105 |
out_path<-list_param$out_path
|
|
106 |
out_names<-list_param$out_rastnames
|
|
107 |
## Start
|
|
108 |
|
|
109 |
input.rasters <- lapply(as.character(mosaic_list[[j]]), raster)
|
|
110 |
mosaiced_rast<-input.rasters[[1]]
|
|
111 |
|
|
112 |
for (k in 2:length(input.rasters)){
|
|
113 |
mosaiced_rast<-mosaic(mosaiced_rast,input.rasters[[k]], fun=mean)
|
|
114 |
#mosaiced_rast<-mosaic(mosaiced_rast,raster(input.rasters[[k]]), fun=mean)
|
|
115 |
}
|
|
116 |
|
|
117 |
data_name<-paste("mosaiced_",sep="") #can add more later...
|
|
118 |
raster_name<-paste(data_name,out_names[j],".tif", sep="")
|
|
119 |
writeRaster(mosaiced_rast, filename=file.path(out_path,raster_name),overwrite=TRUE)
|
|
120 |
#Writing the data in a raster file format...
|
|
121 |
rast_list<-file.path(out_path,raster_name)
|
|
122 |
|
|
123 |
return(rast_list)
|
|
124 |
}
|
|
125 |
|
90 |
126 |
covariates_production_temperature<-function(list_param){
|
91 |
127 |
#This functions produce covariates used in the interpolation of temperature.
|
92 |
128 |
#It requires 15 arguments:
|
... | ... | |
193 |
229 |
lst_pat<-"LST_Day_1km" #for the time being change at later stage...
|
194 |
230 |
}
|
195 |
231 |
|
|
232 |
#Need to clean up this section, later on, works for now using mclapply...
|
196 |
233 |
#Get list of files containing the LST averages
|
|
234 |
#lst_pat<-"*"
|
|
235 |
#out_suffix_modis <- "_01252013"
|
|
236 |
#hdfdir<-"/home/layers/commons/modis/MOD11A1_tiles/LST_averages"
|
197 |
237 |
#pat_str2 <- glob2rx(paste("nobs","*",lst_pat,"*.tif",sep=""))
|
198 |
238 |
pat_str2 <- glob2rx(paste("nobs","*",lst_pat,"*",out_suffix_modis,"*.tif",sep=""))
|
199 |
239 |
#mixedsort(list.files(path=hdfdir,pattern=".*tif"))
|
... | ... | |
218 |
258 |
out_rastnames<-paste(list_date_names,out_rastnames,sep="")
|
219 |
259 |
#reproject and crop if necessary
|
220 |
260 |
#nobs_m_list<-list.files(pattern='mosaiced_.*._lst_nobs_VE_03182013.tif')
|
221 |
|
nobs_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
|
|
261 |
#nobs_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
|
|
262 |
list_param_mosaic<-list(j,mosaic_list,out_rastnames,out_path)
|
|
263 |
names(list_param_mosaic)<-c("j","mosaic_list","out_rastnames","out_path")
|
|
264 |
|
|
265 |
nobs_m_list <-mclapply(1:12, list_param=list_param_mosaic, mosaic_m_raster_list,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
|
|
266 |
#test<-mosaic_m_raster_list(1,list_param_mosaic)
|
222 |
267 |
|
223 |
268 |
##Now mosaic for mean: should reorder files!!
|
224 |
269 |
pat_str1 <- glob2rx(paste("mean","*.tif",sep=""))
|
225 |
270 |
tmp_str1<- mixedsort(list.files(pattern=pat_str1))
|
226 |
|
out_rastnames<-paste("_",lst_pat,"_","mean",out_suffix,sep="")
|
|
271 |
out_rastnames_mean<-paste("_",lst_pat,"_","mean",out_suffix,sep="")
|
227 |
272 |
list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
|
228 |
273 |
mosaic_list<-split(tmp_str1,list_date_names)
|
229 |
274 |
new_list<-vector("list",length(mosaic_list))
|
... | ... | |
232 |
277 |
names(mosaic_list)[j]<-list_date_names[i]
|
233 |
278 |
new_list[i]<-mosaic_list[j]
|
234 |
279 |
}
|
235 |
|
mosaic_list<-new_list
|
236 |
|
out_rastnames<-paste(list_date_names,out_rastnames,sep="")
|
|
280 |
mosaic_list<-new_list #list ready for mosaicing
|
|
281 |
out_rastnames_mean<-paste(list_date_names,out_rastnames_mean,sep="")
|
237 |
282 |
#mean_m_list<-list.files(pattern='mosaiced_.*._lst_mean_VE_03182013.tif')
|
238 |
|
mean_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
|
239 |
|
|
|
283 |
#mean_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
|
|
284 |
list_param_mosaic<-list(j,mosaic_list,out_rastnames_mean,out_path)
|
|
285 |
names(list_param_mosaic)<-c("j","mosaic_list","out_rastnames","out_path")
|
|
286 |
|
|
287 |
mean_m_list <-mclapply(1:12, list_param=list_param_mosaic, mosaic_m_raster_list,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement
|
|
288 |
|
240 |
289 |
#Use this as ref file for now?? Ok for the time being: this will need to change to be a processing tile.
|
241 |
|
ref_rast<-raster(mean_m_list[[1]])
|
|
290 |
if (ref_name==""){
|
|
291 |
ref_rast<-raster(mean_m_list[[1]])
|
|
292 |
}
|
|
293 |
|
242 |
294 |
#ref_rast <-raster("mosaiced_dec_lst_mean_VE_03182013.tif")
|
243 |
295 |
#Modis shapefile tile is slighly shifted:
|
244 |
296 |
# +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs for ref_rast
|
... | ... | |
251 |
303 |
#min_val<-(-15+273.16) #if values less than -15C then screen out (note the Kelvin units that will need to be changed later in all datasets)
|
252 |
304 |
#LST[LST < (min_val)]<-NA
|
253 |
305 |
|
|
306 |
#add screening here...
|
|
307 |
|
254 |
308 |
#########################################
|
255 |
309 |
##2) Crop and reproject Canopy height data
|
256 |
310 |
|
257 |
311 |
#Make it a function?
|
258 |
312 |
#canopy_rast<-raster("Simard_Pinto_3DGlobalVeg_JGR.tif")
|
259 |
|
canopy_name<-file.path(in_path,infile_canheight)
|
260 |
|
|
261 |
|
CANHEIGHT<-create_raster_region(canopy_name,ref_rast)
|
|
313 |
canopy_name<-file.path(infile_canheight)
|
|
314 |
#mclapply??
|
|
315 |
CANHEIGHT<-create_raster_region(canopy_name,ref_rast) #reproject and clip to raster region...
|
262 |
316 |
|
263 |
317 |
##########################################
|
264 |
318 |
#3) Creating elev, aspect, slope from STRM
|
... | ... | |
288 |
342 |
lc_list<-list.files(pattern="con_1km_class_.*.tif")
|
289 |
343 |
#lc<-raster(file.path(lc_path,lc_names))
|
290 |
344 |
|
|
345 |
#Very slow to reproject and clip images...
|
|
346 |
#Use mclapply and create new function( will work for any projection later on when tackling tile by tile projections, use ref name)
|
291 |
347 |
lc_reg_list<-vector("list",length(lc_list))
|
292 |
348 |
for (i in 1:length(lc_list)){
|
293 |
|
|
|
349 |
|
294 |
350 |
lc_name<-lc_list[[i]]
|
295 |
351 |
lc_reg<-create_raster_region(lc_name,ref_rast)
|
296 |
352 |
data_name<-paste("reg_",sub(".tif","",lc_name),"_",sep="") #can add more later...
|
covariates production, modification related to output and parallelization to speed up