Project

General

Profile

« Previous | Next » 

Revision c723ce75

Added by Benoit Parmentier over 11 years ago

covariates production, modification related to output and parallelization to speed up

View differences:

climate/research/oregon/interpolation/covariates_production_temperatures.R
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...
climate/research/oregon/interpolation/master_script_temp.R
10 10
#STAGE 5: Output analyses-visualization of results for specific dates...
11 11
#
12 12
#AUTHOR: Benoit Parmentier                                                                       
13
#DATE: 05/25/2013                                                                                 
13
#DATE: 05/24/2013                                                                                 
14 14

  
15 15
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363, TASK$568--   
16 16

  
......
71 71
stages_to_run<-c(0,0,3,4,5) #May decide on antoher strategy later on...
72 72

  
73 73
var<-"TMAX" # variable being interpolated
74
out_prefix<-"_365d_GAM_fus_all_lst_05252013"                #User defined output prefix
75
out_suffix<-"_VE_05252013"
76
out_suffix_modis="_05252013"
77

  
74
out_prefix<-"_365d_GAM_fus_all_lst_05242013"                #User defined output prefix
78 75
#interpolation_method<-c("gam_fusion","gam_CAI") #other otpions to be added later
79 76
#interpolation_method<-c("gam_CAI") #other otpions to be added later
80 77
interpolation_method<-c("gam_fusion") #other otpions to be added later
......
104 101
#CRS_interp <-"+proj=lcc +lat_1=43 +lat_2=45.5 +lat_0=41.75 +lon_0=-120.5 +x_0=400000 +y_0=0 +ellps=GRS80 +units=m +no_defs";
105 102
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
106 103
out_region_name<-"_venezuela_region" #generated on the fly
107
#out_suffix<-"_VE_05252013"
104
out_suffix<-"_VE_05242013"
108 105
ref_rast_name<-""  #local raster name defining resolution, exent, local projection--. set on the fly??
109 106
#ref_rast_name<-"mean_day244_rescaled.rst"  #local raster name defining resolution, exent: oregon
110 107
  
......
128 125
hdfdir =  '/home/parmentier/Data/IPLANT_project/MOD11A1_tiles'
129 126
download=1
130 127
clim_calc=0
131
#out_suffix_modis="_05252013"
128
out_suffix_modis="_05242013"
132 129
#end_month= "12"
133 130
#start_month= "1"
134 131

  
......
221 218
dates_selected<-"" # if empty string then predict for the full year specified earlier
222 219

  
223 220
#Models to run...this can be change for each run
224
list_models<-c("y_var ~ te(x,y)",
225
               "y_var ~ te(LST)",
226
               "y_var ~ te(x,y,LST)",
227
               "y_var ~ te(LST,elev_s)",
228
               "y_var ~ te(lat,lon) + te(elev_s) + te(N_w,E_w) + te(LST)", 
229
               "y_var ~ te(lat,lon) + te(elev_s) + te(N_w,E_w) + te(LST) + te(LC2)",
230
               "y_var ~ te(lat,lon) + te(elev_s) + te(N_w,E_w) + te(LST) + te(LC6)")
221
list_models<-c("y_var ~ s(x,y)",
222
               "y_var ~ s(LST)",
223
               "y_var ~ s(x,y,LST)",
224
               "y_var ~ s(LST,elev_s)",
225
               "y_var ~ s(lat,lon) + s(elev_s) + s(N_w,E_w) + s(LST)", 
226
               "y_var ~ s(lat,lon) + s(elev_s) + s(N_w,E_w) + s(LST) + s(LC2)",
227
               "y_var ~ s(lat,lon) + s(elev_s) + s(N_w,E_w) + s(LST) + s(LC6)")
228
#               "y_var ~ s(lat,lon) + s(elev_s) + s(N_w,E_w) + s(LST) + s(DISTOC)")
229

  
231 230

  
232 231
#Default name of LST avg to be matched               
233 232
lst_avg<-c("mm_01","mm_02","mm_03","mm_04","mm_05","mm_06","mm_07","mm_08","mm_09","mm_10","mm_11","mm_12")  

Also available in: Unified diff