Revision 992aa616
Added by Benoit Parmentier over 11 years ago
climate/research/oregon/interpolation/covariates_production_temperatures.R | ||
---|---|---|
4 | 4 |
# It requires the following inputs: |
5 | 5 |
# 1)list of modis tiles or shape file with region outline |
6 | 6 |
# 2)input names for global covariates: |
7 |
# -SRTM CGIAR 1 km
|
|
7 |
# -elevation: SRTM CGIAR 1 km (replace by global DEM?)
|
|
8 | 8 |
# -Canopy heihgt (Simard) |
9 |
# -land cover concensus (Jetz lab) |
|
10 |
# -MODIS LST: mean and obs |
|
11 |
#3) The output is a multiband file in tif format with projected covariates for the processing region/tile. |
|
9 |
# -12 layers from land cover concensus (Jetz lab) |
|
10 |
# -distance to coast NASA/NOOA (http://oceancolor.gsfc.nasa.gov/DOCS/DistFromCoast/) |
|
11 |
# -24 LST layers: "climatology" produced from MOD11A1, LST (mean and obs) using script in step 1 of workflow |
|
12 |
# 3) The output is a multiband file in tif format with projected covariates for the processing region/tile. |
|
12 | 13 |
#AUTHOR: Benoit Parmentier |
13 |
#DATE: 05/24/2013
|
|
14 |
#DATE: 05/27/2013
|
|
14 | 15 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363-- |
15 | 16 |
|
16 | 17 |
##Comments and TODO: |
17 | 18 |
#This script is meant to be for general processing tile by tile or region by region. |
18 | 19 |
#We must decide on a local projection. This can best set up from the tile/region extent: for now use |
19 | 20 |
#- lcc with two standard paralells and one central meridian in the middle of the region. |
20 |
#- produce a distance to ocean layer that is global. |
|
21 | 21 |
#- do not keep output in memory?? |
22 | 22 |
|
23 | 23 |
################################################################################################## |
... | ... | |
61 | 61 |
return(layer_projected_rast) |
62 | 62 |
} |
63 | 63 |
|
64 |
create__m_raster_region <-function(j,list_param){ |
|
65 |
#This functions returns a subset of tiles from the modis grdid. |
|
66 |
#Arguments: raster name of the file,reference file with |
|
67 |
#Output: spatial grid data frame of the subset of tiles |
|
68 |
|
|
69 |
## Parse input arguments |
|
70 |
raster_name <- list_param$raster_name[j] #list of raster ot project and crop |
|
71 |
reg_ref_rast <- list_param$reg_ref_rast |
|
72 |
out_rast_name <- list_param$out_rast_name[j] |
|
73 |
|
|
74 |
## Start # |
|
75 |
layer_rast<-raster(raster_name) |
|
76 |
new_proj<-proj4string(layer_rast) #Extract coordinates reference system in PROJ4 format |
|
77 |
region_temp_projected<-projectExtent(reg_ref_rast,CRS(new_proj)) #Project from current to region coord. system |
|
78 |
layer_crop_rast<-crop(layer_rast, region_temp_projected) #crop using the extent from the region tile |
|
79 |
#layer_projected_rast<-projectRaster(from=layer_crop_rast,crs=proj4string(reg_outline),method="ngb") |
|
80 |
layer_projected_rast<-projectRaster(from=layer_crop_rast,to=reg_ref_rast,method="ngb") |
|
81 |
|
|
82 |
writeRaster(layer_projected_rast, filename=out_rast_name,overwrite=TRUE) |
|
83 |
|
|
84 |
return(out_rast_name) |
|
85 |
} |
|
86 |
|
|
64 | 87 |
mosaic_raster_list<-function(mosaic_list,out_names,out_path){ |
65 | 88 |
#This functions returns a subset of tiles from the modis grid. |
66 | 89 |
#Arguments: modies grid tile,list of tiles |
... | ... | |
123 | 146 |
return(rast_list) |
124 | 147 |
} |
125 | 148 |
|
149 |
#Works for now improve later? |
|
150 |
change_names_file_list<-function(list_name,out_suffix,out_prefix,extension,out_path=""){ |
|
151 |
#Function to add suffix and prefix to list of file names |
|
152 |
lf_new_names_list<-vector("list",length(list_name)) #this will contain new names for files |
|
153 |
for (i in 1:length(list_name)){ |
|
154 |
|
|
155 |
lf_name<-basename(list_name[[i]]) |
|
156 |
lf_out_path<-dirname(list_name[[i]]) |
|
157 |
data_name<-paste(out_prefix,sub(extension,"",lf_name),"_",sep="") #can add more later... |
|
158 |
raster_name<-paste(data_name,out_suffix, sep="") #out_suffix must include extension!!! |
|
159 |
if((lf_out_path!="") & (out_path=="")){ |
|
160 |
lf_new_names_list[[i]]<-file.path(lf_out_path,raster_name) |
|
161 |
}else{ |
|
162 |
lf_new_names_list[[i]]<-file.path(out_path,raster_name) |
|
163 |
} |
|
164 |
|
|
165 |
} |
|
166 |
return(unlist(lf_new_names_list)) |
|
167 |
} |
|
168 |
|
|
126 | 169 |
covariates_production_temperature<-function(list_param){ |
127 | 170 |
#This functions produce covariates used in the interpolation of temperature. |
128 |
#It requires 15 arguments:
|
|
171 |
#It requires 16 arguments:
|
|
129 | 172 |
#1) var : interpolated variable: TMIN, TMAX, (PRCP?) |
130 | 173 |
#2) out_path : output directory |
131 |
#3) lc_path |
|
132 |
#4) infile_modis_grid |
|
174 |
#3) lc_path : path to land cover consensus product
|
|
175 |
#4) infile_modis_grid : modis grid litles
|
|
133 | 176 |
#5) infile_elev : this is the global file: replace later with the input produced by the DEM team |
134 | 177 |
#6) infile_canheight : Canopy height |
135 |
#7) list_tiles_modis : tile for Venezuela and surrounding area |
|
136 |
#8) infile_reg_outline : input region outline defined by polygon |
|
137 |
#9) CRS_interp : local projection system |
|
138 |
#10) CRS_locs_WGS84 : CRS_locs_WGS84 # |
|
139 |
#11) out_region_name : generated on the fly |
|
140 |
#12) out_suffix : added to the covariates stack/brick |
|
141 |
#13) ref_rast_name: local raster name defining resolution, exent, local projection--. set on the fly?? |
|
142 |
#14) hdfdir: directory where the LST averages are stored... |
|
143 |
#15) out_suffix_modis : suffix used in producing LST climatology |
|
144 |
#16) covar_names : names of covariates |
|
178 |
#7) infile_distoc : global distance to coast |
|
179 |
#8) list_tiles_modis : tile for Venezuela and surrounding area |
|
180 |
#9) infile_reg_outline : input region outline defined by polygon |
|
181 |
#10) CRS_interp : local projection system |
|
182 |
#11) CRS_locs_WGS84 : CRS_locs_WGS84 # |
|
183 |
#12) out_region_name : generated on the fly? |
|
184 |
#13) out_suffix : added to the covariates stack/brick |
|
185 |
#14) out_suffix_modis : suffix used in the LST averages used for the covariates (may vary to use older LST averages) |
|
186 |
#15) ref_rast_name: local raster name defining resolution, exent, local projection--. set on the fly?? |
|
187 |
#16) hdfdir: directory where the LST averages are stored... |
|
188 |
#17) out_suffix_modis : suffix used in producing LST climatology |
|
189 |
#18) covar_names : names of covariates |
|
145 | 190 |
# |
146 | 191 |
# |
147 | 192 |
|
... | ... | |
165 | 210 |
##### STEP 1: PARSING ARGUMENTS |
166 | 211 |
|
167 | 212 |
var<-list_param$var |
168 |
in_path <-list_param$in_path |
|
169 | 213 |
out_path<- list_param$out_path |
170 | 214 |
lc_path<-list_param$lc_path |
171 | 215 |
infile_modis_grid<-list_param$infile_modis_grid |
172 | 216 |
infile_elev<-list_param$infile_elev #this is the global file: replace later with the input produced by the DEM team |
173 | 217 |
infile_canheight<-list_param$infile_canheight #Canopy height |
218 |
infile_distoc<-list_param$infile_distoc #Canopy height |
|
174 | 219 |
list_tiles_modis<-list_param$list_tiles_modis #tile for Venezuela and surrounding area |
175 |
infile_reg_outline<-list_param$infile_reg_outline #input region outline defined by polygon |
|
220 |
infile_reg_outline <-list_param$infile_reg_outline #input region outline defined by polygon
|
|
176 | 221 |
CRS_interp<-list_param$CRS_interp #local projection system |
177 | 222 |
CRS_locs_WGS84<-list_param$CRS_locs_WGS84 # |
178 | 223 |
out_region_name<-list_param$out_region_name #generated on the fly |
... | ... | |
184 | 229 |
|
185 | 230 |
##### SET UP STUDY AREA #### |
186 | 231 |
|
187 |
#setwd(in_path) |
|
188 | 232 |
setwd(out_path) |
189 | 233 |
|
190 | 234 |
list_tiles_modis <- unlist(strsplit(list_tiles_modis,",")) # transform string into separate element in char vector |
... | ... | |
200 | 244 |
|
201 | 245 |
if (infile_reg_outline!=""){ |
202 | 246 |
filename<-sub(".shp","",basename(infile_reg_outline)) #Removing path and the extension from file name. |
203 |
reg_outline<-readOGR(dsn=dirname(infile_reg_outline), filename) |
|
247 |
reg_outline<-readOGR(dsn=dirname(infile_reg_outline), filename) # Read in the region outline
|
|
204 | 248 |
} |
205 | 249 |
|
250 |
#if no shapefile defining the study/processing area then create one using modis grid tiles |
|
206 | 251 |
if (infile_reg_outline==""){ |
207 | 252 |
reg_outline<-create_modis_tiles_region(modis_grid,list_tiles_modis) #problem...this does not |
208 | 253 |
#align with extent of modis LST!!! |
209 |
writeOGR(reg_outline,dsn= out_path,layer= paste("outline",out_region_name,"_",out_suffix,sep=""), |
|
254 |
infile_reg_outline <-paste("outline",out_region_name,"_",out_suffix,".shp",sep="") |
|
255 |
writeOGR(reg_outline,dsn= out_path,layer= sub(".shp","",infile_reg_outline), |
|
210 | 256 |
driver="ESRI Shapefile",overwrite_layer="TRUE") |
211 | 257 |
} |
212 | 258 |
|
... | ... | |
229 | 275 |
lst_pat<-"LST_Day_1km" #for the time being change at later stage... |
230 | 276 |
} |
231 | 277 |
|
232 |
#Need to clean up this section, later on, works for now using mclapply...
|
|
278 |
#Need to clean up this section, later on, this should be written in a function to avoid repetition
|
|
233 | 279 |
#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" |
|
237 |
#pat_str2 <- glob2rx(paste("nobs","*",lst_pat,"*.tif",sep="")) |
|
280 |
hdfdir_lst_avg<-file.path(hdfdir,"LST_averages") |
|
238 | 281 |
pat_str2 <- glob2rx(paste("nobs","*",lst_pat,"*",out_suffix_modis,"*.tif",sep="")) |
239 |
#mixedsort(list.files(path=hdfdir,pattern=".*tif")) |
|
240 |
tmp_str2 <- mixedsort(list.files(path=hdfdir,pattern=pat_str2)) #note that this assumes the LST averages are stored in hdfdir |
|
241 |
#pat_str1 <- glob2rx(paste("mean","*",lst_pat,"*.tif",sep="")) |
|
282 |
tmp_str2 <- mixedsort(list.files(path=hdfdir_lst_avg,pattern=pat_str2,full.names=TRUE)) #note that this assumes the LST averages are stored in hdfdir |
|
242 | 283 |
pat_str1 <- glob2rx(paste("mean","*",lst_pat,"*",out_suffix_modis,"*.tif",sep="")) |
243 |
tmp_str1 <- mixedsort(list.files(path=hdfdir,pattern=pat_str1))
|
|
284 |
tmp_str1 <- mixedsort(list.files(path=hdfdir_lst_avg,pattern=pat_str1,full.names=TRUE))
|
|
244 | 285 |
#add lines using grep to select tiles... |
245 | 286 |
|
246 | 287 |
#Format list for mosaicing: mosaic for every month the relevant number of files |
247 |
#out_rastnames<-paste("_lst_","nobs",out_suffix,sep="") |
|
248 | 288 |
out_rastnames<-paste("_",lst_pat,"_","nobs",out_suffix,sep="") |
249 | 289 |
list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec") |
250 |
mosaic_list<-split(tmp_str2,list_date_names) |
|
290 |
mosaic_list<-split(tmp_str2,list_date_names) #split list of files into groups using lst_date_name_pattern |
|
291 |
|
|
251 | 292 |
new_list<-vector("list",length(mosaic_list)) |
252 | 293 |
for (i in 1:length(list_date_names)){ |
253 | 294 |
j<-grep(list_date_names[i],mosaic_list,value=FALSE) |
254 | 295 |
names(mosaic_list)[j]<-list_date_names[i] |
255 | 296 |
new_list[i]<-mosaic_list[j] |
256 | 297 |
} |
257 |
mosaic_list<-new_list |
|
258 |
out_rastnames<-paste(list_date_names,out_rastnames,sep="") |
|
259 |
#reproject and crop if necessary |
|
260 |
#nobs_m_list<-list.files(pattern='mosaiced_.*._lst_nobs_VE_03182013.tif') |
|
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) |
|
298 |
mosaic_list_nobs<-new_list |
|
299 |
out_rastnames_nobs<-paste(list_date_names,out_rastnames,sep="") |
|
267 | 300 |
|
268 | 301 |
##Now mosaic for mean: should reorder files!! |
269 |
pat_str1 <- glob2rx(paste("mean","*.tif",sep="")) |
|
270 |
tmp_str1<- mixedsort(list.files(pattern=pat_str1)) |
|
271 | 302 |
out_rastnames_mean<-paste("_",lst_pat,"_","mean",out_suffix,sep="") |
272 | 303 |
list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec") |
273 | 304 |
mosaic_list<-split(tmp_str1,list_date_names) |
... | ... | |
277 | 308 |
names(mosaic_list)[j]<-list_date_names[i] |
278 | 309 |
new_list[i]<-mosaic_list[j] |
279 | 310 |
} |
280 |
mosaic_list<-new_list #list ready for mosaicing |
|
311 |
mosaic_list_mean <-new_list #list ready for mosaicing
|
|
281 | 312 |
out_rastnames_mean<-paste(list_date_names,out_rastnames_mean,sep="") |
282 |
#mean_m_list<-list.files(pattern='mosaiced_.*._lst_mean_VE_03182013.tif') |
|
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) |
|
313 |
|
|
314 |
### Now mosaic tiles...Note that function could be improved to use less memory |
|
315 |
list_param_mosaic<-list(j,mosaic_list_mean,out_rastnames_mean,out_path) |
|
316 |
names(list_param_mosaic)<-c("j","mosaic_list","out_rastnames","out_path") |
|
317 |
mean_m_list <-mclapply(1:12, list_param=list_param_mosaic, mosaic_m_raster_list,mc.preschedule=FALSE,mc.cores = 12) #This is the end bracket from mclapply(...) statement |
|
318 |
|
|
319 |
list_param_mosaic<-list(j,mosaic_list_nobs,out_rastnames_nobs,out_path) |
|
285 | 320 |
names(list_param_mosaic)<-c("j","mosaic_list","out_rastnames","out_path") |
321 |
nobs_m_list <-mclapply(1:12, list_param=list_param_mosaic, mosaic_m_raster_list,mc.preschedule=FALSE,mc.cores = 12) #This is the end bracket from mclapply(...) statement |
|
286 | 322 |
|
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 |
|
|
289 | 323 |
#Use this as ref file for now?? Ok for the time being: this will need to change to be a processing tile. |
290 |
if (ref_name==""){ |
|
324 |
if (ref_rast_name==""){
|
|
291 | 325 |
ref_rast<-raster(mean_m_list[[1]]) |
326 |
}else{ |
|
327 |
ref_rast<-raster(ref_rast_name) #This is the reference image used to define the study/processing area |
|
292 | 328 |
} |
293 | 329 |
|
330 |
## Project mosaiced tiles if local projection is provided... |
|
331 |
|
|
332 |
out_suffix_lst <-paste(out_suffix,".tif",sep="") |
|
333 |
mean_lst_list_outnames<-change_names_file_list(mean_m_list,out_suffix_lst,"reg_",".tif",out_path=out_path) |
|
334 |
nobs_lst_list_outnames<-change_names_file_list(nobs_m_list,out_suffix_lst,"reg_",".tif",out_path=out_path) |
|
335 |
|
|
336 |
if (ref_rast_name!=""){ |
|
337 |
#list(mean_m_list) |
|
338 |
list_param_create_region<-list(j,raster_name=mean_m_list,reg_ref_rast=ref_rast,out_rast_name=mean_lst_list_outnames) |
|
339 |
mean_m_list <-mclapply(1:12, list_param=list_param_create_region, create__m_raster_region,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement |
|
340 |
list_param_create_region<-list(j,raster_name=nobs_m_list,reg_ref_rast=ref_rast,out_rast_name=nobs_lst_list_outnames) |
|
341 |
nobs_m_list <-mclapply(1:12, list_param=list_param_create_region, create__m_raster_region,mc.preschedule=FALSE,mc.cores = 6) #This is the end bracket from mclapply(...) statement |
|
342 |
} |
|
343 |
# if ref_name!="" need to reproject and clip!!! do this in mclapply for all the list of covar!!! |
|
344 |
|
|
294 | 345 |
#ref_rast <-raster("mosaiced_dec_lst_mean_VE_03182013.tif") |
295 | 346 |
#Modis shapefile tile is slighly shifted: |
296 | 347 |
# +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs for ref_rast |
297 | 348 |
#"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs" ?? |
298 | 349 |
#reassign proj from modis tile to raster? there is a 10m diff in semi-axes...(a and b) |
299 | 350 |
|
300 |
##Write function to screen data values... |
|
301 |
|
|
302 |
#Screen LST for extreme values? |
|
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) |
|
304 |
#LST[LST < (min_val)]<-NA |
|
351 |
###################################### |
|
352 |
#4) LCC land cover |
|
305 | 353 |
|
306 |
#add screening here...
|
|
354 |
lc_list<-mixedsort(list.files(path=lc_path,pattern="con_1km_class_.*.tif",full.names=TRUE))
|
|
307 | 355 |
|
308 |
######################################### |
|
309 |
##2) Crop and reproject Canopy height data |
|
356 |
out_suffix_lc<-paste(out_suffix,".tif",sep="") |
|
357 |
lc_list_outnames<-change_names_file_list(lc_list,out_suffix_lc,"reg_",".tif",out_path=out_path) |
|
358 |
list_param_create_region<-list(j,raster_name=lc_list,reg_ref_rast=ref_rast,out_rast_name=lc_list_outnames) |
|
359 |
lc_reg_list <-mclapply(1:length(lc_list), list_param=list_param_create_region, create__m_raster_region,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement |
|
360 |
lc_reg_s <- stack(lc_reg_list) |
|
310 | 361 |
|
311 |
#Make it a function? |
|
312 |
#canopy_rast<-raster("Simard_Pinto_3DGlobalVeg_JGR.tif") |
|
313 |
canopy_name<-file.path(infile_canheight) |
|
314 |
#mclapply?? |
|
315 |
CANHEIGHT<-create_raster_region(canopy_name,ref_rast) #reproject and clip to raster region... |
|
362 |
#Now create mask based on water areas |
|
363 |
|
|
364 |
LC12<-raster(lc_reg_s,layer=nlayers(lc_reg_s)) #this is open water |
|
365 |
LC_mask<-LC12 |
|
366 |
LC_mask[LC_mask==100]<-NA |
|
367 |
LC_mask <- LC_mask > 100 |
|
316 | 368 |
|
369 |
############################### |
|
370 |
#5) DISTOC, distance to coast: Would be useful to have a distance to coast layer ready... |
|
371 |
#Crop and reproject Canopy height data |
|
372 |
|
|
373 |
lc_list <- c(infile_elev,infile_canheight,infile_distoc) #, lc_list #15 layers to reproject... |
|
374 |
out_suffix_l <-paste(out_suffix,".tif",sep="") |
|
375 |
lc_list_outnames<-change_names_file_list(lc_list,out_suffix_l,"reg_",".tif",out_path=out_path) |
|
376 |
list_param_create_region<-list(j,raster_name=lc_list,reg_ref_rast=ref_rast,out_rast_name=lc_list_outnames) |
|
377 |
list_covar_layers <- mclapply(1:3, list_param=list_param_create_region, create__m_raster_region,mc.preschedule=FALSE,mc.cores = 3) #This is the end bracket from mclapply(...) statement |
|
378 |
|
|
379 |
SRTM_reg <- raster(list_covar_layers[[1]]) |
|
380 |
CANHEIGHT <- raster(list_covar_layers[[2]]) |
|
381 |
distoc <- raster(list_covar_layers[[3]]) |
|
382 |
|
|
317 | 383 |
########################################## |
318 |
#3) Creating elev, aspect, slope from STRM |
|
319 |
|
|
320 |
SRTM_reg<-create_raster_region(infile_elev,ref_rast) |
|
321 |
|
|
322 |
#Call a function to reproject the data in a local projection defined on the fly using the processing tile |
|
323 |
#extent...For the time being just use sinusoidal projection. |
|
324 |
###calculate slope and aspect |
|
325 |
|
|
384 |
#3) aspect, slope from STRM |
|
385 |
|
|
326 | 386 |
terrain_rast<-terrain(SRTM_reg, opt=c("slope","aspect"),unit="degrees", neighbors=8) #, filename=\u2019\u2019, ...) |
327 | 387 |
pos<-match("aspect",names(terrain_rast)) #Find column with name "value" |
328 | 388 |
r1<-raster(terrain_rast,layer=pos) #Select layer from stack |
... | ... | |
333 | 393 |
Nw<-sin(r2)*cos(r1) #Adding a variable to the dataframe |
334 | 394 |
Ew<-sin(r2)*sin(r1) #Adding variable to the dataframe. |
335 | 395 |
|
336 |
###################################### |
|
337 |
#4) LCC land cover |
|
338 |
|
|
339 |
oldpath<-getwd() |
|
340 |
setwd(lc_path) |
|
341 |
#lc_name<-"con_1km_class_1.tif" |
|
342 |
lc_list<-list.files(pattern="con_1km_class_.*.tif") |
|
343 |
#lc<-raster(file.path(lc_path,lc_names)) |
|
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) |
|
347 |
lc_reg_list<-vector("list",length(lc_list)) |
|
348 |
for (i in 1:length(lc_list)){ |
|
349 |
|
|
350 |
lc_name<-lc_list[[i]] |
|
351 |
lc_reg<-create_raster_region(lc_name,ref_rast) |
|
352 |
data_name<-paste("reg_",sub(".tif","",lc_name),"_",sep="") #can add more later... |
|
353 |
raster_name<-paste(data_name,out_suffix,".tif", sep="") |
|
354 |
writeRaster(lc_reg, filename=file.path(out_path,raster_name),overwrite=TRUE) |
|
355 |
lc_reg_list[[i]]<-file.path(out_path,raster_name) |
|
356 |
} |
|
357 |
setwd(out_path) |
|
358 |
lc_reg_list<-mixedsort(list.files(pattern=paste("^reg_con_1km_class_.*.",out_suffix,".tif",sep=""))) |
|
359 |
lc_reg_s<-stack(lc_reg_list) |
|
360 |
|
|
361 |
#Now combine forest classes...in LC1 forest, LC2, LC3, LC4 and LC6-urban...?? |
|
362 |
|
|
363 |
#create a local mask for the tile/processing region |
|
364 |
|
|
365 |
#LC12<-raster(paste("reg_con_1km_class_12_",out_suffix,".tif",sep="")) #this is open water |
|
366 |
LC12<-raster(lc_reg_s,layer=nlayers(lc_reg_s)) #this is open water |
|
367 |
LC_mask<-LC12 |
|
368 |
LC_mask[LC_mask==100]<-NA |
|
369 |
LC_mask <- LC_mask > 100 |
|
370 |
|
|
371 |
############################### |
|
372 |
#5) DISTOC, distance to coast: Would be useful to have a distance to coast layer ready... |
|
373 |
|
|
374 |
#This does not work...clump needs igraph. I'll look into this later...for now I used IDRISI to clump pixels. |
|
375 |
#rc<-clump(LC12) |
|
376 |
#tab_freq<-freq(rc) |
|
377 |
#Modify at a later stage: |
|
378 |
#raster<-"DISTOC_VE_01292013.rst" |
|
379 |
#ocean_rast<-raster(file.path(in_path,"lc12_tmp_grouped_rec.rst")) |
|
380 |
#ocean_rast[ocean_rast==0]<-NA |
|
381 |
#Distance calculated in a global layer?? |
|
382 |
#distoc_reg<-distance(ocean_rast,doEdge=TRUE) #this is very slow: more than 35 min use GRASS instead?? |
|
383 |
|
|
384 |
#load DISTOC produced from IDRISI: automate the process later |
|
385 |
distoc_reg<-raster(file.path(in_path,"DISTOC_VE_01292013.rst")) |
|
386 |
|
|
387 | 396 |
################################ |
388 | 397 |
#6) X-Y coordinates and LAT-LONG: do not keep in memory? |
389 | 398 |
#ref_rast <-raster("mosaiced_dec_lst_mean_VE_03182013.tif") |
... | ... | |
403 | 412 |
################################ |
404 | 413 |
##Step 3: combine covariates in one stack for the next work flow stage |
405 | 414 |
|
406 |
r<-stack(x,y,lon,lat,N,E,Nw,Ew,SRTM_reg,terrain_rast,CANHEIGHT,distoc_reg)
|
|
415 |
r<-stack(x,y,lon,lat,N,E,Nw,Ew,SRTM_reg,terrain_rast,CANHEIGHT,distoc) |
|
407 | 416 |
#rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC") |
408 | 417 |
s_raster<-r |
409 | 418 |
#Add landcover layers |
... | ... | |
416 | 425 |
|
417 | 426 |
#covar_names<-c(rnames,lc_names,lst_names) |
418 | 427 |
names(s_raster)<-covar_names |
428 |
|
|
429 |
##Write function to screen data values... |
|
430 |
|
|
431 |
#Screen LST for extreme values? |
|
432 |
#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) |
|
433 |
#LST[LST < (min_val)]<-NA |
|
434 |
|
|
435 |
#add screening here... |
|
436 |
|
|
419 | 437 |
#Write out stack of number of change |
420 | 438 |
data_name<-paste("covariates_",out_region_name,"_",sep="") |
421 | 439 |
raster_name<-paste(data_name,var,"_",out_suffix,".tif", sep="") |
422 | 440 |
#writeRaster(s_raster, filename=raster_name,NAflag=-999,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE) #Writing the data in a raster file format... |
441 |
#Mask and save layers |
|
423 | 442 |
s_raster_m<-mask(s_raster,LC_mask,filename=raster_name, |
424 | 443 |
overwrite=TRUE,NAflag=-999,bylayer=FALSE,bandorder="BSQ") |
425 | 444 |
#using bil format more efficient?? |
426 |
return(raster_name) |
|
445 |
|
|
446 |
#return reg_outline!!! |
|
447 |
covar_obj <-list(raster_name,infile_reg_outline) |
|
448 |
names(covar_obj) <-c("infile_covariates","infile_reg_outline") |
|
449 |
return(covar_obj) |
|
427 | 450 |
} |
428 | 451 |
|
429 | 452 |
####################################################### |
Also available in: Unified diff
covariates production script, major changes to automate the process, added also distance to coast product