Project

General

Profile

« Previous | Next » 

Revision 88cf72bb

Added by Benoit Parmentier over 11 years ago

Covariates production, modified projection and distance to coast, added writing up of multiband covariates brick

View differences:

climate/research/oregon/interpolation/covariates_production_temperatures.R
16 16
##Comments and TODO:
17 17
#This script is meant to be for general processing tile by tile or region by region.
18 18
#We must decide on a local projection. This can best set up from the tile/region extent: for now use
19
#- lcc with two standard paralell and one central meridian in the middle of the region.
19
#- lcc with two standard paralells and one central meridian in the middle of the region.
20 20
#- produce a distance to ocean layer that is global.
21 21
#- do not keep output in memory??
22 22

  
......
43 43
lc_path<-"/home/layers/data/land-cover/lc-consensus-global"
44 44
elev_path<-"/home/layers/data/terrain/dem-cgiar-srtm-1km-tif"
45 45

  
46
setwd(in_path)
47

  
48 46
infile1<-"worldborder_sinusoidal.shp"
49 47
infile2<-"modis_sinusoidal_grid_world.shp"
50 48
infile3<-"countries_sinusoidal_world.shp"
51 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
52 51
list_tiles_modis = c('h11v08','h11v07','h12v07','h12v08','h10v07','h10v08') #tile for Venezuel and surrounding area
53 52
infile_reg_outline=""  #input region outline defined by polygon
54 53
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";
55 54
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
56
out_region_name<-"modis_venezuela_region"
55
out_region_name<-"_venezuela_region"
57 56
out_suffix<-"_VE_01292013"
58 57
ref_rast_name<-""  #local raster name defining resolution, exent, local projection--. set on the fly??
59 58
                   #for the processing tile/region? This is a group fo six tiles for now.
......
84 83
  return(reg_outline_poly)
85 84
}
86 85

  
87
create_raster_region <-function(raster_name,reg_ref_rast,reg_outline_poly){
86
create_raster_region <-function(raster_name,reg_ref_rast){
88 87
  #This functions returns a subset of tiles from the modis grdi.
89 88
  #Arguments: raster name of the file,reference file with
90 89
  #Output: spatial grid data frame of the subset of tiles
91 90
  
92 91
  layer_rast<-raster(raster_name)
93 92
  new_proj<-proj4string(layer_rast)                  #Extract coordinates reference system in PROJ4 format
94
  region_temp_projected<-spTransform(reg_outline_poly,CRS(new_proj))     #Project from current to region coord. system
93
  region_temp_projected<-projectExtent(reg_ref_rast,CRS(new_proj))     #Project from current to region coord. system
95 94
  layer_crop_rast<-crop(layer_rast, region_temp_projected) #crop using the extent from teh region tile
96 95
  #layer_projected_rast<-projectRaster(from=layer_crop_rast,crs=proj4string(reg_outline),method="ngb")
97 96
  layer_projected_rast<-projectRaster(from=layer_crop_rast,to=reg_ref_rast,method="ngb")
......
129 128

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

  
131
setwd(in_path)
132

  
132 133
filename<-sub(".shp","",infile2)       #Removing the extension from file.
133 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
134 137

  
135 138
if (infile_reg_outline!=""){
136 139
  filename<-sub(".shp","",infile_reg_outline)   #Removing the extension from file.
......
140 143
if (infile_reg_outline==""){
141 144
  reg_outline<-create_modis_tiles_region(modis_grid,list_tiles_modis) #problem...this does not 
142 145
                                                                      #align with extent of modis LST!!!
146
  writeOGR(reg_outline,dsn= ".",layer= paste(out_region_name,"_",out_suffix,sep=""), driver="ESRI Shapefile")
143 147
}
144 148

  
149
tmp<-extent(ref_rast)
150

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

  
......
150 156
################################
151 157
#1) LST climatology: project, mosaic
152 158

  
153
tile<-list_tile_modis[i]
159
tile<-list_tiles_modis[i]
154 160
pat_str2 <- glob2rx(paste("nobs","*.tif",sep=""))
155 161
tmp_str2<- mixedsort(list.files(pattern=pat_str2))
156 162
pat_str1 <- glob2rx(paste("mean","*.tif",sep=""))
......
159 165

  
160 166
#list_date_names<-as.character(0:11)
161 167
#lsit_date_names<-month.abb
162
out_rastnames<-paste("lst_","nobs",out_suffix,sep="")
168
out_rastnames<-paste("_lst_","nobs",out_suffix,sep="")
163 169
list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
164
mosaic_list<-split(tmp_str1,list_date_names)
170
mosaic_list<-split(tmp_str2,list_date_names)
171
new_list<-vector("list",length(mosaic_list))
165 172
for (i in 1:length(list_date_names)){
166 173
  j<-grep(list_date_names[i],mosaic_list,value=FALSE)
167 174
  names(mosaic_list)[j]<-list_date_names[i]
175
  new_list[i]<-mosaic_list[j]
168 176
}
169

  
177
mosaic_list<-new_list
178
out_rastnames<-paste(list_date_names,out_rastnames,sep="")
170 179
#reproject and crop if necessary
171 180
nobs_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path)
172 181
plot(stack(nobs_m_list))
......
190 199
plot(stack(mean_m_list))
191 200
#Use this as ref file for now?? Ok for the time being: this will need to change to be a processing tile.
192 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

  
193 207

  
194 208
#########################################
195 209
##2) Crop and reproject Canopy height data
196 210

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

  
205
CANHEIGHT<-raster(s_raster,layer=pos)             #Select layer from stack
206
s_raster<-dropLayer(s_raster,pos)
207
CANHEIGHT[is.na(CANHEIGHT)]<-0
220
#CANHEIGHT<-raster(s_raster,layer=pos)             #Select layer from stack
221
#s_raster<-dropLayer(s_raster,pos)
222
#CANHEIGHT[is.na(CANHEIGHT)]<-0
223

  
224
CANHEIGHT<-create_raster_region(canopy_name,ref_rast)
208 225

  
209 226
##########################################
210 227
#3) Creating elev, aspect, slope from STRM
211 228

  
212 229
SRTM_name<-file.path(elev_path,infile4)
213
SRTM_reg<-create_raster_region(SRTM_name,reg_outline)
230
SRTM_reg<-create_raster_region(SRTM_name,ref_rast)
214 231

  
215 232
#new_proj<-proj4string(SRTM_rast)                  #Assign coordinates reference system in PROJ4 format
216 233
#region_temp_projected<-spTransform(modis_tiles,CRS(new_proj))     #Project from WGS84 to new coord. system
......
227 244
###calculate slope and aspect
228 245

  
229 246
terrain_rast<-terrain(SRTM_reg, opt=c("slope","aspect"),unit="degrees", neighbors=8) #, filename=\u2019\u2019, ...)
230
pos<-match("ASPECT",layerNames(terrain_rast)) #Find column with name "value"
231
r1<-raster(s_raster,layer=pos)             #Select layer from stack
232
pos<-match("slope",layerNames(terrain_rast)) #Find column with name "value"
233
r2<-raster(s_raster,layer=pos)             #Select layer from stack
247
pos<-match("aspect",names(terrain_rast)) #Find column with name "value"
248
r1<-raster(terrain_rast,layer=pos)             #Select layer from stack
249
pos<-match("slope",names(terrain_rast)) #Find column with name "value"
250
r2<-raster(terrain_rast,layer=pos)             #Select layer from stack
234 251
N<-cos(r1)
235 252
E<-sin(r1)
236 253
Nw<-sin(r2)*cos(r1)   #Adding a variable to the dataframe
......
251 268
for (i in 1:length(lc_list)){
252 269
  
253 270
  lc_name<-lc_list[[i]]
254
  lc_reg<-create_raster_region(lc_name,reg_outline)
271
  lc_reg<-create_raster_region(lc_name,ref_rast)
255 272
  data_name<-paste("reg_",sub(".tif","",lc_name),"_",sep="") #can add more later...
256 273
  raster_name<-paste(data_name,out_suffix,".tif", sep="")
257 274
  writeRaster(lc_reg, filename=file.path(out_path,raster_name),overwrite=TRUE)  
......
260 277
setwd(out_path)
261 278
lc_reg_list<-mixedsort(list.files(pattern="reg_con.*.tif"))
262 279
lc_reg_s<-stack(lc_reg_list)
263

  
280
#lc_reg_s<-as.character(lc_reg_list)
264 281
#Now combine forest classes...in LC1 forest, LC2, LC3, LC4 and LC6-urban...??
265 282

  
266 283
#create a local mask for the tile/processing region
267 284

  
268
LC12<-raster("reg_con_1km_class_12__VE_01292013.tif") #this is open water
285
LC12<-raster(paste("reg_con_1km_class_12_",out_suffix,".tif",sep="")) #this is open water
269 286
LC_mask<-LC12
270 287
LC_mask[LC_mask==100]<-NA
271 288
LC_mask <- LC_mask > 100
272
tmp<-mask(lc_reg_s,LC_mask)
289
lc_reg_s<-mask(lc_reg_s,LC_mask,filename=paste("reg_con_1km_classes_",out_suffix,".tif",sep=""))
273 290

  
274 291
###############################
275 292
#5) DISTOC, distance to coast: Would be useful to have a distance to coast layer ready...
276 293

  
277
#This does not work...clump needs igraph. look into this later...for now I used IDRISI to clump pixels.
294
#This does not work...clump needs igraph. I'll look into this later...for now I used IDRISI to clump pixels.
278 295
#rc<-clump(LC12)
279 296
#tab_freq<-freq(rc)
280

  
281 297
#Modify at a later stage:
282 298
#raster<-"DISTOC_VE_01292013.rst"  
283
ocean_rast<-raster(file.path(in_path,"lc12_tmp_grouped_rec.rst"))
284
ocean_rast[ocean_rast==0]<-NA
299
#ocean_rast<-raster(file.path(in_path,"lc12_tmp_grouped_rec.rst"))
300
#ocean_rast[ocean_rast==0]<-NA
285 301
#Distance calculated in a global layer??
286
distoc_reg<-distance(ocean_rast,doEdge=TRUE) #this is very slow: more than 30min use GRASS instead??
302
#distoc_reg<-distance(ocean_rast,doEdge=TRUE) #this is very slow: more than 35 min use GRASS instead??
303

  
304
#load DISTOC produced from IDRISI: automate the process later
305
distoc_reg<-raster(file.path(in_path,"DISTOC_VE_01292013.rst"))
287 306

  
288 307
################################
289 308
#6) X-Y coordinates and LAT-LONG: do not keep in memory?
290
r1<-ref_rast
291

  
292
xy<-coordinates(r1)  #get x and y projected coordinates...
309
r1 <-ref_rast
310
xy <-coordinates(r1)  #get x and y projected coordinates...
311
CRS_interp<-proj4string(r1)
293 312
xy_latlon<-project(xy, CRS_interp, inv=TRUE) # find lat long for projected coordinats (or pixels...)
294
lon<-raster(xy_latlon) #Transform a matrix into a raster object ncol=ncol(r1), nrow=nrow(r1))
295
lon<-init(r1,v="x")
296
projection(lon)<-CRS  #At this stage this is still an empty raster with 536 nrow and 745 ncell 
297
lat<-lon
298
values(lon)<-xy_latlon[,1]
299
values(lat)<-xy_latlon[,2]
300

  
313
x <-init(r1,v="x")
314
y <-init(r1,v="y")
315
lon <-x
316
lat <-lon
317
lon <-setValues(lon,xy_latlon[,1]) #longitude for every pixel in the processing tile/region
318
lat <-setValues(lat,xy_latlon[,2]) #latitude for every pixel in the processing tile/region
319
rm(r1)
301 320
#coord_s<-stack(x,y,lat,lon)
302 321

  
303 322
################################
......
305 324
#Create a stack in tif format...
306 325

  
307 326
#? output name??
308
r<-stack(N,E,Nw,Ew,lon,lat,LC1,LC3,LC4,LC6, CANHEIGHT,ELEV_SRTM)
309
rnames<-c("Northness","Eastness","Northness_w","Eastness_w", "lon","lat","LC1","LC3","LC4","LC6","CANHEIGHT","ELEV_SRTM")
310
layerNames(r)<-rnames
311
s_raster<-addLayer(s_raster, r)
312

  
313
##Extracting the variables values from the raster files                                             
314

  
315
lines<-read.table(paste(path,"/",inlistf,sep=""), sep=" ")                  #Column 1 contains the names of raster files
316
inlistvar<-lines[,1]
317
inlistvar<-paste(path,"/",as.character(inlistvar),sep="")
318
covar_names<-as.character(lines[,2])                                         #Column two contains short names for covaraites
319

  
320
s_raster<- stack(inlistvar)                                                  #Creating a stack of raster images from the list of variables.
321
layerNames(s_raster)<-covar_names                                            #Assigning names to the raster layers
322
projection(s_raster)<-CRS
323

  
324
#eWrite out stack of number of change 
325
data_name<-paste("covariates_",out_name_region,"_",sep="")
326
raster_name<-paste("A_",data_name,out_suffix,".tif", sep="")
327
writeRaster(s_raster, filename=raster_name,NAflag=-999,bylayer=False,bandorder="BSQ",overwrite=TRUE)  #Writing the data in a raster file format...
327
r<-stack(x,y,lon,lat,N,E,Nw,Ew,SRTM_reg,terrain_rast,CANHEIGHT,distoc_reg)
328
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
329
names(r)<-rnames
330
s_raster<-r
331
#Add landcover layers
332
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
333
names(lc_reg_s)<-lc_names #assign land cover names
334
s_raster<-addLayer(s_raster, lc_reg_s)
335

  
336
lst_s<-stack(c(as.character(mean_m_list),as.character(nobs_m_list)))
337
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",
338
             "nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
339
             "nobs_09","nobs_10","nobs_11","nobs_12")
340
names(lst_s)<-lst_names
341
s_raster<-addLayer(s_raster, lst_s)
342
s_raster<-mask(s_raster,LC_mask,filename="test.tif")
343

  
344
covar_names<-c(rnames,lc_names,lst_names)
345
names(s_raster)<-covar_names
346
#Write out stack of number of change 
347
data_name<-paste("covariates_",out_region_name,"_",sep="")
348
raster_name<-paste(data_name,out_suffix,".tif", sep="")
349
writeRaster(s_raster, filename=raster_name,NAflag=-999,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE)  #Writing the data in a raster file format...
328 350
#using bil format more efficient??
329 351

  
330 352
#######################################################

Also available in: Unified diff