Project

General

Profile

« Previous | Next » 

Revision 2852eefc

Added by Benoit Parmentier over 11 years ago

Covariates production raster first step turning script into function

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: 01/28/2013                                                                                 
13
#DATE: 03/19/2013                                                                                 
14 14
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
15 15

  
16 16
##Comments and TODO:
......
37 37
### Parameters and arguments
38 38

  
39 39
##Paths to inputs and output
40
in_path <- "/home/parmentier/Data/benoit_test"
40
var<-"TMAX"
41 41
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
42 42
out_path<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data/"
43 43
lc_path<-"/home/layers/data/land-cover/lc-consensus-global"
44
elev_path<-"/home/layers/data/terrain/dem-cgiar-srtm-1km-tif"
45

  
46
infile1<-"worldborder_sinusoidal.shp"
47
infile2<-"modis_sinusoidal_grid_world.shp"
48
infile3<-"countries_sinusoidal_world.shp"
49
infile4<-"srtm_1km.tif"  #this is the global file: replace later with the input produced by the DEM team
50
infile5<-"Simard_Pinto_3DGlobalVeg_JGR.tif"              #Canopy height
44
#elev_path<-"/home/layers/data/terrain/dem-cgiar-srtm-1km-tif"
45
#infile3<-"countries_sinusoidal_world.shp"
46
#infile1<-"worldborder_sinusoidal.shp"
47
infile_modis_grid<-"modis_sinusoidal_grid_world.shp"
48
infile_elev<-"/home/layers/data/terrain/dem-cgiar-srtm-1km-tif/srtm_1km.tif"  #this is the global file: replace later with the input produced by the DEM team
49
infile_canheight<-"Simard_Pinto_3DGlobalVeg_JGR.tif"              #Canopy height
51 50
list_tiles_modis = c('h11v08','h11v07','h12v07','h12v08','h10v07','h10v08') #tile for Venezuel and surrounding area
52 51
infile_reg_outline=""  #input region outline defined by polygon
53 52
CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
54 53
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
55
out_region_name<-"_venezuela_region"
56
out_suffix<-"_VE_02082013"
54
out_region_name<-"_venezuela_region" #generated on the fly
55
out_suffix<-"_VE_03182013"
57 56
ref_rast_name<-""  #local raster name defining resolution, exent, local projection--. set on the fly??
58 57
                   #for the processing tile/region? This is a group fo six tiles for now.
59 58

  
59
#The names of covariates can be changed...these names should be output/input from covar script!!!
60
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
61
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
62
lst_names<-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",
63
             "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
64
             "nobs_09","nobs_10","nobs_11","nobs_12")
65
covar_names<-c(rnames,lc_names,lst_names)
66

  
67
list_param_covar_production<-list(var,in_path,out_path,lc_path,infile_modis_grid,infile_elev,infile_canheight,
68
                                  list_tiles_modis,infile_reg_outline,CRS_interp,CRS_locs_WGS84,out_region_name,
69
                                  out_suffix,ref_rast_name,covar_names) 
70

  
71
names(list_param_covar_production)<-c("var","in_path","out_path","lc_path","infile_modis_grid","infile_elev","infile_canheight",
72
                                  "list_tiles_modis","infile_reg_outline","CRS_interp","CRS_locs_WGS84","out_region_name",
73
                                  "out_suffix","ref_rast_name","covar_names") 
74

  
75
#LST_night_rast<-raster("mean_LST_Night_1km_h11v08_dec_11_03192013.tif")
60 76
#### Functions used in the script  ###
61 77

  
62 78
create_modis_tiles_region<-function(modis_grid,tiles){
......
123 139
  return(rast_list)
124 140
}
125 141

  
126
###########################################################
127
############ Main body: BEGIN--START OF THE SCRIPT ###################
128

  
129
##### STEP 1: Reading region or tile information to set the study or processing region
130

  
131
setwd(in_path)
132

  
133
filename<-sub(".shp","",infile2)       #Removing the extension from file.
134
modis_grid<-readOGR(".", filename)     #Reading shape file using rgdal library
135
filename<-sub(".shp","",infile1)       #Removing the extension from file.
136
world_countries<-readOGR(".", filename)     #Reading shape file using rgdal library
137

  
138
if (infile_reg_outline!=""){
139
  filename<-sub(".shp","",infile_reg_outline)   #Removing the extension from file.
140
  reg_outline<-readOGR(".", filename)
141
}
142

  
143
if (infile_reg_outline==""){
144
  reg_outline<-create_modis_tiles_region(modis_grid,list_tiles_modis) #problem...this does not 
145
                                                                      #align with extent of modis LST!!!
146
  writeOGR(reg_outline,dsn= ".",layer= paste("outline",out_region_name,"_",out_suffix,sep=""), driver="ESRI Shapefile")
147
}
148

  
149
tmp<-extent(ref_rast)
150

  
151
#modis_tiles<-create_modis_tiles_region(modis_grid,list_tiles_modis)
152
##Create covariates for the stuy area: pull everything from the same folder?
153

  
154
#### STEP 2: process and/or produce covariates for the tile/region
155

  
156
################################
157
#1) LST climatology: project, mosaic
158

  
159
tile<-list_tiles_modis[i]
160
pat_str2 <- glob2rx(paste("nobs","*.tif",sep=""))
161
tmp_str2<- mixedsort(list.files(pattern=pat_str2))
162
pat_str1 <- glob2rx(paste("mean","*.tif",sep=""))
163
tmp_str1<- mixedsort(list.files(pattern=pat_str1))
164
#add lines using grep to select tiles...
165

  
166
#list_date_names<-as.character(0:11)
167
#lsit_date_names<-month.abb
168
out_rastnames<-paste("_lst_","nobs",out_suffix,sep="")
169
list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
170
mosaic_list<-split(tmp_str2,list_date_names)
171
new_list<-vector("list",length(mosaic_list))
172
for (i in 1:length(list_date_names)){
173
  j<-grep(list_date_names[i],mosaic_list,value=FALSE)
174
  names(mosaic_list)[j]<-list_date_names[i]
175
  new_list[i]<-mosaic_list[j]
176
}
177
mosaic_list<-new_list
178
out_rastnames<-paste(list_date_names,out_rastnames,sep="")
179
#reproject and crop if necessary
180
nobs_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
181
plot(stack(nobs_m_list))
182

  
183
##Now mosaic for mean: should reorder files!!
184
pat_str1 <- glob2rx(paste("mean","*.tif",sep=""))
185
tmp_str1<- mixedsort(list.files(pattern=pat_str1))
186
out_rastnames<-paste("_lst_","mean",out_suffix,sep="")
187
list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
188
mosaic_list<-split(tmp_str1,list_date_names)
189
new_list<-vector("list",length(mosaic_list))
190
for (i in 1:length(list_date_names)){
191
  j<-grep(list_date_names[i],mosaic_list,value=FALSE)
192
  names(mosaic_list)[j]<-list_date_names[i]
193
  new_list[i]<-mosaic_list[j]
194
}
195
mosaic_list<-new_list
196
out_rastnames<-paste(list_date_names,out_rastnames,sep="")
197

  
198
mean_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
199
plot(stack(mean_m_list))
200
#Use this as ref file for now?? Ok for the time being: this will need to change to be a processing tile.
201
ref_rast<-raster(mean_m_list[[1]]) 
202
#Modis shapefile tile is slighly shifted:
203
# +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs for ref_rast
204
#"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs" ??
205
#reassign proj from modis tile to raster? there is a 10m diff in semi-axes...(a and b)
206

  
207
#Screen LST for extreme values?
208
#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)
209
#LST[LST < (min_val)]<-NA
210

  
211

  
212
#########################################
213
##2) Crop and reproject Canopy height data
214

  
215
#Make it a function?
216
#canopy_rast<-raster("Simard_Pinto_3DGlobalVeg_JGR.tif")
217
canopy_name<-file.path(in_path,infile5)
218
#new_proj<-proj4string(canopy)                  #Assign coordinates reference system in PROJ4 format
219
#region_temp_projected<-spTransform(modis_tiles,CRS(new_proj))     #Project from WGS84 to new coord. system
220
#canopy_crop_rast<-crop(canopy, region_temp_projected) #crop using the extent from teh region tile
221
#canopy_projected_rast<-projectRaster(from=canopy_crop_rast,crs=proj4string(modis_tiles),method="ngb")
222
#Use GDAL instead?? system( 'gdalwarp...')
223

  
224
#CANHEIGHT<-raster(s_raster,layer=pos)             #Select layer from stack
225
#s_raster<-dropLayer(s_raster,pos)
226
#CANHEIGHT[is.na(CANHEIGHT)]<-0
227

  
228
CANHEIGHT<-create_raster_region(canopy_name,ref_rast)
229

  
230
##########################################
231
#3) Creating elev, aspect, slope from STRM
232

  
233
SRTM_name<-file.path(elev_path,infile4)
234
SRTM_reg<-create_raster_region(SRTM_name,ref_rast)
235

  
236
#new_proj<-proj4string(SRTM_rast)                  #Assign coordinates reference system in PROJ4 format
237
#region_temp_projected<-spTransform(modis_tiles,CRS(new_proj))     #Project from WGS84 to new coord. system
238
#SRTM_crop_rast<-crop(SRTM, region_temp_projected) #crop using the extent from teh region tile
239
#SRTM_projected_rast<-projectRaster(from=SRTM,crs=proj4string(modis_tiles),method="ngb")
240

  
241
#pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM"
242
#ELEV_SRTM<-raster(s_raster,layer=pos)             #Select layer from stack on 10/30
243
#s_raster<-dropLayer(s_raster,pos)
244
#ELEV_SRTM[ELEV_SRTM <0]<-NA
245

  
246
#Call a function to reproject the data in a local projection defined on the fly using the processing tile
247
#extent...For the time being just use sinusoidal projection.
248
###calculate slope and aspect
249

  
250
terrain_rast<-terrain(SRTM_reg, opt=c("slope","aspect"),unit="degrees", neighbors=8) #, filename=\u2019\u2019, ...)
251
pos<-match("aspect",names(terrain_rast)) #Find column with name "value"
252
r1<-raster(terrain_rast,layer=pos)             #Select layer from stack
253
pos<-match("slope",names(terrain_rast)) #Find column with name "value"
254
r2<-raster(terrain_rast,layer=pos)             #Select layer from stack
255
N<-cos(r1)
256
E<-sin(r1)
257
Nw<-sin(r2)*cos(r1)   #Adding a variable to the dataframe
258
Ew<-sin(r2)*sin(r1)   #Adding variable to the dataframe.
259

  
260
#topo_rast<-stack(STRM_reg,N,E,Nw,Ew)
261

  
262
######################################
263
#4) LCC land cover
264

  
265
oldpath<-getwd()
266
setwd(lc_path)
267
#lc_name<-"con_1km_class_1.tif"
268
lc_list<-list.files(pattern="con_1km_class_.*.tif")
269
#lc<-raster(file.path(lc_path,lc_names))
270

  
271
lc_reg_list<-vector("list",length(lc_list))
272
for (i in 1:length(lc_list)){
273
  
274
  lc_name<-lc_list[[i]]
275
  lc_reg<-create_raster_region(lc_name,ref_rast)
276
  data_name<-paste("reg_",sub(".tif","",lc_name),"_",sep="") #can add more later...
277
  raster_name<-paste(data_name,out_suffix,".tif", sep="")
278
  writeRaster(lc_reg, filename=file.path(out_path,raster_name),overwrite=TRUE)  
279
  lc_reg_list[[i]]<-file.path(out_path,raster_name)
142
covariates_production_temperature<-function(list_param){
143
  #This functions produce covariates used in the interpolation of temperature.
144
  #It requires 15 arguments:
145
  #
146
  #
147
  #
148
  ###########################################################
149
  ############ Main body: BEGIN--START OF THE SCRIPT ###################
150
  
151
  ##### STEP 1: Reading region or tile information to set the study or processing region
152
  
153
  var<-list_param$var
154
  in_path <-list_param$in_path
155
  out_path<- list_param$out_path
156
  lc_path<-list_param$lc_path 
157
  #elev_path<-"/home/layers/data/terrain/dem-cgiar-srtm-1km-tif"
158
  #infile3<-"countries_sinusoidal_world.shp"
159
  #infile1<-"worldborder_sinusoidal.shp"
160
  infile_modis_grid<-list_param$infile_modis_grid
161
  infile_elev<-list_param$infile_elev #this is the global file: replace later with the input produced by the DEM team
162
  infile_canheight<-list_param$infile_canheight #Canopy height
163
  list_tiles_modis<-list_param$list_tiles_modis #tile for Venezuel and surrounding area
164
  infile_reg_outline<-list_param$infile_reg_outline   #input region outline defined by polygon
165
  CRS_interp<-list_param$CRS_interp #local projection system
166
  CRS_locs_WGS84<-list_param$CRS_locs_WGS84 #
167
  out_region_name<-list_param$out_region_name  #generated on the fly
168
  out_suffix<-list_param$out_suffix 
169
  ref_rast_name<-list_param$ref_rast_name #local raster name defining resolution, exent, local projection--. set on the fly??
170
  #for the processing tile/region? This is a group fo six tiles for now.
171
  
172
  covar_names<-list_param$covar_names 
173
  
174
  setwd(in_path)
175
  
176
  filename<-sub(".shp","",infile_modis_grid)       #Removing the extension from file.
177
  modis_grid<-readOGR(".", filename)     #Reading shape file using rgdal library
178
  #filename<-sub(".shp","",infile1)       #Removing the extension from file.
179
  #world_countries<-readOGR(".", filename)     #Reading shape file using rgdal library
180
  
181
  if (infile_reg_outline!=""){
182
    filename<-sub(".shp","",infile_reg_outline)   #Removing the extension from file.
183
    reg_outline<-readOGR(".", filename)
184
  }
185
  
186
  if (infile_reg_outline==""){
187
    reg_outline<-create_modis_tiles_region(modis_grid,list_tiles_modis) #problem...this does not 
188
    #align with extent of modis LST!!!
189
    writeOGR(reg_outline,dsn= ".",layer= paste("outline",out_region_name,"_",out_suffix,sep=""), 
190
             driver="ESRI Shapefile",overwrite_layer="TRUE")
191
  }
192
  
193
  #Should add option for a reference file here...
194
  #tmp<-extent(ref_rast)
195
  
196
  #modis_tiles<-create_modis_tiles_region(modis_grid,list_tiles_modis)
197
  ##Create covariates for the stuy area: pull everything from the same folder?
198
  
199
  #### STEP 2: process and/or produce covariates for the tile/region
200
  
201
  ################################
202
  #1) LST climatology: project, mosaic
203
  i<-1
204
  tile<-list_tiles_modis[i]
205
  if (var=="TMIN"){
206
    lst_pat<-"LST_Night_1km"
207
  }
208
  if (var=="TMAX"){
209
    lst_pat<-"" #for the time being change at later stage...
210
    #day_pat<-"LST_Day_1km"
211
  }
212
  
213
  #Get list of files containing the LST averages
214
  pat_str2 <- glob2rx(paste("nobs","*",lst_pat,"*.tif",sep=""))
215
  tmp_str2<- mixedsort(list.files(pattern=pat_str2))
216
  pat_str1 <- glob2rx(paste("mean","*",lst_pat,"*.tif",sep=""))
217
  tmp_str1<- mixedsort(list.files(pattern=pat_str1))
218
  #add lines using grep to select tiles...
219
 
220
  #Format list for mosaicing: mosaic for every month the relevant number of files
221
  #out_rastnames<-paste("_lst_","nobs",out_suffix,sep="")
222
  out_rastnames<-paste("_",lst_pat,"_","nobs",out_suffix,sep="")
223
  list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
224
  mosaic_list<-split(tmp_str2,list_date_names)
225
  new_list<-vector("list",length(mosaic_list))
226
  for (i in 1:length(list_date_names)){
227
    j<-grep(list_date_names[i],mosaic_list,value=FALSE)
228
    names(mosaic_list)[j]<-list_date_names[i]
229
    new_list[i]<-mosaic_list[j]
230
  }
231
  mosaic_list<-new_list
232
  out_rastnames<-paste(list_date_names,out_rastnames,sep="")
233
  #reproject and crop if necessary
234
  #nobs_m_list<-list.files(pattern='mosaiced_.*._lst_nobs_VE_03182013.tif')
235
  nobs_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
236
  
237
  ##Now mosaic for mean: should reorder files!!
238
  pat_str1 <- glob2rx(paste("mean","*.tif",sep=""))
239
  tmp_str1<- mixedsort(list.files(pattern=pat_str1))
240
  out_rastnames<-paste("_",lst_pat,"_","mean",out_suffix,sep="")
241
  list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
242
  mosaic_list<-split(tmp_str1,list_date_names)
243
  new_list<-vector("list",length(mosaic_list))
244
  for (i in 1:length(list_date_names)){
245
    j<-grep(list_date_names[i],mosaic_list,value=FALSE)
246
    names(mosaic_list)[j]<-list_date_names[i]
247
    new_list[i]<-mosaic_list[j]
248
  }
249
  mosaic_list<-new_list
250
  out_rastnames<-paste(list_date_names,out_rastnames,sep="")
251
  #mean_m_list<-list.files(pattern='mosaiced_.*._lst_mean_VE_03182013.tif')
252
  mean_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
253

  
254
  #Use this as ref file for now?? Ok for the time being: this will need to change to be a processing tile.
255
  ref_rast<-raster(mean_m_list[[1]])
256
  #ref_rast <-raster("mosaiced_dec_lst_mean_VE_03182013.tif")
257
  #Modis shapefile tile is slighly shifted:
258
  # +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs for ref_rast
259
  #"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs" ??
260
  #reassign proj from modis tile to raster? there is a 10m diff in semi-axes...(a and b)
261
  
262
  ##Write function to screen data values...
263
  
264
  #Screen LST for extreme values?
265
  #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)
266
  #LST[LST < (min_val)]<-NA
267
  
268
  #########################################
269
  ##2) Crop and reproject Canopy height data
270
  
271
  #Make it a function?
272
  #canopy_rast<-raster("Simard_Pinto_3DGlobalVeg_JGR.tif")
273
  canopy_name<-file.path(in_path,infile_canheight)
274
  
275
  CANHEIGHT<-create_raster_region(canopy_name,ref_rast)
276
  
277
  ##########################################
278
  #3) Creating elev, aspect, slope from STRM
279
  
280
  SRTM_reg<-create_raster_region(infile_elev,ref_rast)
281
  
282
  #Call a function to reproject the data in a local projection defined on the fly using the processing tile
283
  #extent...For the time being just use sinusoidal projection.
284
  ###calculate slope and aspect
285
  
286
  terrain_rast<-terrain(SRTM_reg, opt=c("slope","aspect"),unit="degrees", neighbors=8) #, filename=\u2019\u2019, ...)
287
  pos<-match("aspect",names(terrain_rast)) #Find column with name "value"
288
  r1<-raster(terrain_rast,layer=pos)             #Select layer from stack
289
  pos<-match("slope",names(terrain_rast)) #Find column with name "value"
290
  r2<-raster(terrain_rast,layer=pos)             #Select layer from stack
291
  N<-cos(r1)
292
  E<-sin(r1)
293
  Nw<-sin(r2)*cos(r1)   #Adding a variable to the dataframe
294
  Ew<-sin(r2)*sin(r1)   #Adding variable to the dataframe.
295
  
296
  #topo_rast<-stack(STRM_reg,N,E,Nw,Ew)
297
  
298
  ######################################
299
  #4) LCC land cover
300
  
301
  oldpath<-getwd()
302
  setwd(lc_path)
303
  #lc_name<-"con_1km_class_1.tif"
304
  lc_list<-list.files(pattern="con_1km_class_.*.tif")
305
  #lc<-raster(file.path(lc_path,lc_names))
306
  
307
  lc_reg_list<-vector("list",length(lc_list))
308
  for (i in 1:length(lc_list)){
309
    
310
    lc_name<-lc_list[[i]]
311
    lc_reg<-create_raster_region(lc_name,ref_rast)
312
    data_name<-paste("reg_",sub(".tif","",lc_name),"_",sep="") #can add more later...
313
    raster_name<-paste(data_name,out_suffix,".tif", sep="")
314
    writeRaster(lc_reg, filename=file.path(out_path,raster_name),overwrite=TRUE)  
315
    lc_reg_list[[i]]<-file.path(out_path,raster_name)
316
  }
317
  setwd(out_path)
318
  lc_reg_list<-mixedsort(list.files(pattern=paste("^reg_con_1km_class_.*.",out_suffix,".tif",sep="")))
319
  lc_reg_s<-stack(lc_reg_list)
320
  
321
  #Now combine forest classes...in LC1 forest, LC2, LC3, LC4 and LC6-urban...??
322
  
323
  #create a local mask for the tile/processing region
324
  
325
  #LC12<-raster(paste("reg_con_1km_class_12_",out_suffix,".tif",sep="")) #this is open water
326
  LC12<-raster(lc_reg_s,layer=nlayers(lc_reg_s)) #this is open water
327
  
328
  LC_mask<-LC12
329
  LC_mask[LC_mask==100]<-NA
330
  LC_mask <- LC_mask > 100
331
  #lc_reg_s<-mask(x=lc_reg_s,mask=LC_mask,filename=paste("reg_con_1km_all_classes_",out_suffix,".tif",sep=""),
332
  #               bandorder="BSQ",overwrite=TRUE)
333
  
334
  ###############################
335
  #5) DISTOC, distance to coast: Would be useful to have a distance to coast layer ready...
336
  
337
  #This does not work...clump needs igraph. I'll look into this later...for now I used IDRISI to clump pixels.
338
  #rc<-clump(LC12)
339
  #tab_freq<-freq(rc)
340
  #Modify at a later stage:
341
  #raster<-"DISTOC_VE_01292013.rst"  
342
  #ocean_rast<-raster(file.path(in_path,"lc12_tmp_grouped_rec.rst"))
343
  #ocean_rast[ocean_rast==0]<-NA
344
  #Distance calculated in a global layer??
345
  #distoc_reg<-distance(ocean_rast,doEdge=TRUE) #this is very slow: more than 35 min use GRASS instead??
346
  
347
  #load DISTOC produced from IDRISI: automate the process later
348
  distoc_reg<-raster(file.path(in_path,"DISTOC_VE_01292013.rst"))
349
  
350
  ################################
351
  #6) X-Y coordinates and LAT-LONG: do not keep in memory?
352
  #ref_rast <-raster("mosaiced_dec_lst_mean_VE_03182013.tif")
353
  r1 <-ref_rast
354
  xy <-coordinates(r1)  #get x and y projected coordinates...
355
  CRS_interp<-proj4string(r1)
356
  xy_latlon<-project(xy, CRS_interp, inv=TRUE) # find lat long for projected coordinats (or pixels...)
357
  x <-init(r1,v="x")
358
  y <-init(r1,v="y")
359
  lon <-x
360
  lat <-lon
361
  lon <-setValues(lon,xy_latlon[,1]) #longitude for every pixel in the processing tile/region
362
  lat <-setValues(lat,xy_latlon[,2]) #latitude for every pixel in the processing tile/region
363
  rm(r1)
364
  #coord_s<-stack(x,y,lat,lon)
365
  
366
  ################################
367
  ##Step 3: combine covariates in one stack for the next work flow stage
368
  #Create a stack in tif format...
369
  
370
  #? output name??
371
  r<-stack(x,y,lon,lat,N,E,Nw,Ew,SRTM_reg,terrain_rast,CANHEIGHT,distoc_reg)
372
  #rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
373
  #names(r)<-rnames
374
  s_raster<-r
375
  #Add landcover layers
376
  #lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
377
  #names(lc_reg_s)<-lc_names #assign land cover names
378
  s_raster<-addLayer(s_raster, lc_reg_s)
379
  
380
  lst_s<-stack(c(as.character(mean_m_list),as.character(nobs_m_list)))
381
  #lst_names<-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",
382
  #             "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
383
  #             "nobs_09","nobs_10","nobs_11","nobs_12")
384
  #names(lst_s)<-lst_names
385
  s_raster<-addLayer(s_raster, lst_s)
386
  
387
  #covar_names<-c(rnames,lc_names,lst_names)
388
  names(s_raster)<-covar_names
389
  #Write out stack of number of change 
390
  data_name<-paste("covariates_",out_region_name,"_",sep="")
391
  raster_name<-paste(data_name,var,"_",out_suffix,".tif", sep="")
392
  #writeRaster(s_raster, filename=raster_name,NAflag=-999,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE)  #Writing the data in a raster file format...
393
  s_raster_m<-mask(s_raster,LC_mask,filename=raster_name,
394
                 overwrite=TRUE,NAflag=-999,bylayer=FALSE,bandorder="BSQ")
395
  #using bil format more efficient??
396
  return(raster_name)
280 397
}
281
setwd(out_path)
282
lc_reg_list<-mixedsort(list.files(pattern="^reg_con.*.tif"))
283
lc_reg_s<-stack(lc_reg_list)
284
#lc_reg_s<-as.character(lc_reg_list)
285
#Now combine forest classes...in LC1 forest, LC2, LC3, LC4 and LC6-urban...??
286

  
287
#create a local mask for the tile/processing region
288

  
289
#LC12<-raster(paste("reg_con_1km_class_12_",out_suffix,".tif",sep="")) #this is open water
290
LC12<-raster(lc_reg_s,layer=nlayers(lc_reg_s)) #this is open water
291

  
292
LC_mask<-LC12
293
LC_mask[LC_mask==100]<-NA
294
LC_mask <- LC_mask > 100
295
lc_reg_s<-mask(lc_reg_s,LC_mask,filename=paste("reg_con_1km_classes_",out_suffix,".tif",sep=""))
296 398

  
297
###############################
298
#5) DISTOC, distance to coast: Would be useful to have a distance to coast layer ready...
299

  
300
#This does not work...clump needs igraph. I'll look into this later...for now I used IDRISI to clump pixels.
301
#rc<-clump(LC12)
302
#tab_freq<-freq(rc)
303
#Modify at a later stage:
304
#raster<-"DISTOC_VE_01292013.rst"  
305
#ocean_rast<-raster(file.path(in_path,"lc12_tmp_grouped_rec.rst"))
306
#ocean_rast[ocean_rast==0]<-NA
307
#Distance calculated in a global layer??
308
#distoc_reg<-distance(ocean_rast,doEdge=TRUE) #this is very slow: more than 35 min use GRASS instead??
309

  
310
#load DISTOC produced from IDRISI: automate the process later
311
distoc_reg<-raster(file.path(in_path,"DISTOC_VE_01292013.rst"))
312

  
313
################################
314
#6) X-Y coordinates and LAT-LONG: do not keep in memory?
315
r1 <-ref_rast
316
xy <-coordinates(r1)  #get x and y projected coordinates...
317
CRS_interp<-proj4string(r1)
318
xy_latlon<-project(xy, CRS_interp, inv=TRUE) # find lat long for projected coordinats (or pixels...)
319
x <-init(r1,v="x")
320
y <-init(r1,v="y")
321
lon <-x
322
lat <-lon
323
lon <-setValues(lon,xy_latlon[,1]) #longitude for every pixel in the processing tile/region
324
lat <-setValues(lat,xy_latlon[,2]) #latitude for every pixel in the processing tile/region
325
rm(r1)
326
#coord_s<-stack(x,y,lat,lon)
327

  
328
################################
329
##Step 3: combine covariates in one stack for the next work flow stage
330
#Create a stack in tif format...
331

  
332
#? output name??
333
r<-stack(x,y,lon,lat,N,E,Nw,Ew,SRTM_reg,terrain_rast,CANHEIGHT,distoc_reg)
334
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
335
names(r)<-rnames
336
s_raster<-r
337
#Add landcover layers
338
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
339
names(lc_reg_s)<-lc_names #assign land cover names
340
s_raster<-addLayer(s_raster, lc_reg_s)
341

  
342
lst_s<-stack(c(as.character(mean_m_list),as.character(nobs_m_list)))
343
lst_names<-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",
344
             "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
345
             "nobs_09","nobs_10","nobs_11","nobs_12")
346
names(lst_s)<-lst_names
347
s_raster<-addLayer(s_raster, lst_s)
348

  
349
covar_names<-c(rnames,lc_names,lst_names)
350
names(s_raster)<-covar_names
351
#Write out stack of number of change 
352
data_name<-paste("covariates_",out_region_name,"_",sep="")
353
raster_name<-paste(data_name,out_suffix,".tif", sep="")
354
#writeRaster(s_raster, filename=raster_name,NAflag=-999,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE)  #Writing the data in a raster file format...
355
s_raster<-mask(s_raster,LC_mask,filename=raster_name,
356
               overwrite=TRUE,NAflag=-999,bylayer=FALSE,bandorder="BSQ")
357
#using bil format more efficient??
358 399

  
359 400
#######################################################
360
################### END OF SCRIPT #####################
401
################### END OF SCRIPT/FUNCTION #####################

Also available in: Unified diff