Revision e775748d
Added by Benoit Parmentier almost 12 years ago
climate/research/oregon/interpolation/covariates_production_temperatures.R | ||
---|---|---|
1 |
################## Data preparation for interpolation ####################################### |
|
2 |
############################ Covariate production for a given tile/region ########################################## |
|
3 |
#This script produces covariates raster for a a specfied study area. |
|
4 |
# It requires the following inputs: |
|
5 |
# 1)list of modis tiles or shape file with region outline |
|
6 |
# 2)input names for global covariates: |
|
7 |
# -SRTM CGIAR 1 km |
|
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. |
|
12 |
#AUTHOR: Benoit Parmentier |
|
13 |
#DATE: 01/28/2013 |
|
14 |
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363-- |
|
15 |
|
|
16 |
##Comments and TODO: |
|
17 |
#This script is meant to be for general processing tile by tile or region by region. |
|
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. |
|
20 |
#- produce a distance to ocean layer that is global. |
|
21 |
#- do not keep output in memory?? |
|
22 |
|
|
23 |
################################################################################################## |
|
24 |
|
|
25 |
###Loading R library and packages |
|
26 |
library(RPostgreSQL) |
|
27 |
library(sp) # Spatial pacakge with class definition by Bivand et al. |
|
28 |
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al. |
|
29 |
library(rgdal) # GDAL wrapper for R, spatial utilities |
|
1 | 30 |
library(raster) |
2 | 31 |
library(gtools) |
3 | 32 |
library(rasterVis) |
4 | 33 |
library(graphics) |
5 | 34 |
library(grid) |
6 | 35 |
library(lattice) |
7 |
library(rgdal) |
|
8 |
##checking results from GRASS climatology calculation... |
|
36 |
|
|
37 |
### Parameters and arguments |
|
38 |
|
|
39 |
##Paths to inputs and output |
|
9 | 40 |
in_path <- "/home/parmentier/Data/benoit_test" |
10 | 41 |
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/" |
42 |
out_path<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/output_data/" |
|
43 |
lc_path<-"/home/layers/data/land-cover/lc-consensus-global" |
|
44 |
elev_path<-"/home/layers/data/terrain/dem-cgiar-srtm-1km-tif" |
|
45 |
|
|
11 | 46 |
setwd(in_path) |
12 |
#mean_h12v08_dec_11 |
|
13 |
tile="h12v08" |
|
14 |
pat_str <- glob2rx(paste("mean","*", tile,"*01232013.tif",sep="")) |
|
15 |
tmp_str<- mixedsort(list.files(pattern=pat_str)) |
|
47 |
|
|
16 | 48 |
infile1<-"worldborder_sinusoidal.shp" |
17 | 49 |
infile2<-"modis_sinusoidal_grid_world.shp" |
18 | 50 |
infile3<-"countries_sinusoidal_world.shp" |
19 |
tiles = c('h11v08','h11v07','h12v07','h12v08','h10v07','h10v08') |
|
20 |
|
|
21 |
###Reading the station data |
|
22 |
filename<-sub(".shp","",infile2) #Removing the extension from file. |
|
23 |
modis_grid<-readOGR(".", filename) #Reading shape file using rgdal library |
|
51 |
infile4<-"srtm_1km.tif" #this is the global file: replace later with the input produced by the DEM team |
|
52 |
list_tiles_modis = c('h11v08','h11v07','h12v07','h12v08','h10v07','h10v08') #tile for Venezuel and surrounding area |
|
53 |
infile_reg_outline="" #input region outline defined by polygon |
|
54 |
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 |
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84 |
|
56 |
out_region_name<-"modis_venezuela_region" |
|
57 |
out_suffix<-"_VE_01292013" |
|
58 |
ref_rast_name<-"" #local raster name defining resolution, exent, local projection--. set on the fly?? |
|
59 |
#for the processing tile/region? This is a group fo six tiles for now. |
|
60 |
|
|
61 |
#### Functions used in the script ### |
|
24 | 62 |
|
25 |
##Function select tiles: |
|
26 |
out_file_name<-"modis_venezuela_region" |
|
27 | 63 |
create_modis_tiles_region<-function(modis_grid,tiles){ |
28 | 64 |
#This functions returns a subset of tiles from the modis grdi. |
29 | 65 |
#Arguments: modies grid tile,list of tiles |
... | ... | |
31 | 67 |
|
32 | 68 |
h_list<-lapply(tiles,substr,start=2,stop=3) #passing multiple arguments |
33 | 69 |
v_list<-lapply(tiles,substr,start=5,stop=6) #passing multiple arguments |
34 |
|
|
70 |
|
|
35 | 71 |
selected_tiles<-subset(subset(modis_grid,subset = h %in% as.numeric (h_list) ), |
36 |
subset = v %in% as.numeric(v_list))
|
|
72 |
subset = v %in% as.numeric(v_list)) |
|
37 | 73 |
return(selected_tiles) |
38 | 74 |
} |
39 | 75 |
|
40 |
modis_tiles<-create_modis_tiles_region(modis_grid,tiles) |
|
76 |
#This function is very very slow not be used most likely |
|
77 |
create_polygon_from_extent<-function(reg_ref_rast){ |
|
78 |
#This functions returns polygon sp from input rast |
|
79 |
#Arguments: input ref rast |
|
80 |
#Output: spatial polygon |
|
81 |
set1f <- function(x){rep(1, x)} |
|
82 |
tmp_rast <- init(reg_ref_rast, fun=set1f, overwrite=TRUE) |
|
83 |
reg_outline_poly<-rasterToPolygons(tmp_rast) |
|
84 |
return(reg_outline_poly) |
|
85 |
} |
|
86 |
|
|
87 |
create_raster_region <-function(raster_name,reg_ref_rast,reg_outline_poly){ |
|
88 |
#This functions returns a subset of tiles from the modis grdi. |
|
89 |
#Arguments: raster name of the file,reference file with |
|
90 |
#Output: spatial grid data frame of the subset of tiles |
|
91 |
|
|
92 |
layer_rast<-raster(raster_name) |
|
93 |
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 |
|
95 |
layer_crop_rast<-crop(layer_rast, region_temp_projected) #crop using the extent from teh region tile |
|
96 |
#layer_projected_rast<-projectRaster(from=layer_crop_rast,crs=proj4string(reg_outline),method="ngb") |
|
97 |
layer_projected_rast<-projectRaster(from=layer_crop_rast,to=reg_ref_rast,method="ngb") |
|
98 |
return(layer_projected_rast) |
|
99 |
} |
|
100 |
|
|
101 |
mosaic_raster_list<-function(mosaic_list,out_names,out_path){ |
|
102 |
#This functions returns a subset of tiles from the modis grid. |
|
103 |
#Arguments: modies grid tile,list of tiles |
|
104 |
#Output: spatial grid data frame of the subset of tiles |
|
105 |
#Note that rasters are assumed to be in the same projection system!! |
|
106 |
|
|
107 |
rast_list<-vector("list",length(mosaic_list)) |
|
108 |
for (i in 1:length(mosaic_list)){ |
|
109 |
# read the individual rasters into a list of RasterLayer objects |
|
110 |
# this may be changed so that it is not read in the memory!!! |
|
111 |
input.rasters <- lapply(as.character(mosaic_list[[i]]), raster) |
|
112 |
mosaiced_rast<-input.rasters[[1]] |
|
113 |
|
|
114 |
for (k in 2:length(input.rasters)){ |
|
115 |
mosaiced_rast<-mosaic(mosaiced_rast,input.rasters[[k]], fun=mean) |
|
116 |
#mosaiced_rast<-mosaic(mosaiced_rast,raster(input.rasters[[k]]), fun=mean) |
|
117 |
} |
|
118 |
data_name<-paste("mosaiced_",sep="") #can add more later... |
|
119 |
raster_name<-paste(data_name,out_names[i],".tif", sep="") |
|
120 |
writeRaster(mosaiced_rast, filename=file.path(out_path,raster_name),overwrite=TRUE) |
|
121 |
#Writing the data in a raster file format... |
|
122 |
rast_list[[i]]<-file.path(out_path,raster_name) |
|
123 |
} |
|
124 |
return(rast_list) |
|
125 |
} |
|
126 |
|
|
127 |
########################################################### |
|
128 |
############ Main body: BEGIN--START OF THE SCRIPT ################### |
|
129 |
|
|
130 |
##### STEP 1: Reading region or tile information to set the study or processing region |
|
131 |
|
|
132 |
filename<-sub(".shp","",infile2) #Removing the extension from file. |
|
133 |
modis_grid<-readOGR(".", filename) #Reading shape file using rgdal library |
|
134 |
|
|
135 |
if (infile_reg_outline!=""){ |
|
136 |
filename<-sub(".shp","",infile_reg_outline) #Removing the extension from file. |
|
137 |
reg_outline<-readOGR(".", filename) |
|
138 |
} |
|
139 |
|
|
140 |
if (infile_reg_outline==""){ |
|
141 |
reg_outline<-create_modis_tiles_region(modis_grid,list_tiles_modis) #problem...this does not |
|
142 |
#align with extent of modis LST!!! |
|
143 |
} |
|
41 | 144 |
|
145 |
#modis_tiles<-create_modis_tiles_region(modis_grid,list_tiles_modis) |
|
42 | 146 |
##Create covariates for the stuy area: pull everything from the same folder? |
43 | 147 |
|
44 |
##Extracting the variables values from the raster files
|
|
148 |
#### STEP 2: process and/or produce covariates for the tile/region
|
|
45 | 149 |
|
46 |
lines<-read.table(paste(path,"/",inlistf,sep=""), sep=" ") #Column 1 contains the names of raster files |
|
47 |
inlistvar<-lines[,1] |
|
48 |
inlistvar<-paste(path,"/",as.character(inlistvar),sep="") |
|
49 |
covar_names<-as.character(lines[,2]) #Column two contains short names for covaraites |
|
150 |
################################ |
|
151 |
#1) LST climatology: project, mosaic |
|
50 | 152 |
|
51 |
s_raster<- stack(inlistvar) #Creating a stack of raster images from the list of variables. |
|
52 |
layerNames(s_raster)<-covar_names #Assigning names to the raster layers |
|
53 |
projection(s_raster)<-CRS |
|
153 |
tile<-list_tile_modis[i] |
|
154 |
pat_str2 <- glob2rx(paste("nobs","*.tif",sep="")) |
|
155 |
tmp_str2<- mixedsort(list.files(pattern=pat_str2)) |
|
156 |
pat_str1 <- glob2rx(paste("mean","*.tif",sep="")) |
|
157 |
tmp_str1<- mixedsort(list.files(pattern=pat_str1)) |
|
158 |
#add lines using grep to select tiles... |
|
159 |
|
|
160 |
#list_date_names<-as.character(0:11) |
|
161 |
#lsit_date_names<-month.abb |
|
162 |
out_rastnames<-paste("lst_","nobs",out_suffix,sep="") |
|
163 |
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) |
|
165 |
for (i in 1:length(list_date_names)){ |
|
166 |
j<-grep(list_date_names[i],mosaic_list,value=FALSE) |
|
167 |
names(mosaic_list)[j]<-list_date_names[i] |
|
168 |
} |
|
169 |
|
|
170 |
#reproject and crop if necessary |
|
171 |
nobs_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path) |
|
172 |
plot(stack(nobs_m_list)) |
|
173 |
|
|
174 |
##Now mosaic for mean: should reorder files!! |
|
175 |
pat_str1 <- glob2rx(paste("mean","*.tif",sep="")) |
|
176 |
tmp_str1<- mixedsort(list.files(pattern=pat_str1)) |
|
177 |
out_rastnames<-paste("_lst_","mean",out_suffix,sep="") |
|
178 |
list_date_names<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec") |
|
179 |
mosaic_list<-split(tmp_str1,list_date_names) |
|
180 |
new_list<-vector("list",length(mosaic_list)) |
|
181 |
for (i in 1:length(list_date_names)){ |
|
182 |
j<-grep(list_date_names[i],mosaic_list,value=FALSE) |
|
183 |
names(mosaic_list)[j]<-list_date_names[i] |
|
184 |
new_list[i]<-mosaic_list[j] |
|
185 |
} |
|
186 |
mosaic_list<-new_list |
|
187 |
out_rastnames<-paste(list_date_names,out_rastnames,sep="") |
|
188 |
|
|
189 |
mean_m_list<-mosaic_raster_list(mosaic_list,out_rastnames,out_path) |
|
190 |
plot(stack(mean_m_list)) |
|
191 |
#Use this as ref file for now?? Ok for the time being: this will need to change to be a processing tile. |
|
192 |
ref_rast<-raster(mean_m_list[[1]]) |
|
54 | 193 |
|
55 |
## Crop and reproject Canopy height |
|
194 |
######################################### |
|
195 |
##2) Crop and reproject Canopy height data |
|
56 | 196 |
|
197 |
#Make it a function? |
|
57 | 198 |
canopy_rast<-raster("Simard_Pinto_3DGlobalVeg_JGR.tif") |
58 | 199 |
new_proj<-proj4string(canopy) #Assign coordinates reference system in PROJ4 format |
59 | 200 |
region_temp_projected<-spTransform(modis_tiles,CRS(new_proj)) #Project from WGS84 to new coord. system |
60 | 201 |
canopy_crop_rast<-crop(canopy, region_temp_projected) #crop using the extent from teh region tile |
61 | 202 |
canopy_projected_rast<-projectRaster(from=canopy_crop_rast,crs=proj4string(modis_tiles),method="ngb") |
62 |
#system( 'gdalwarp...') |
|
63 |
#1) Creating elev, aspect, slope from STRM |
|
64 |
|
|
65 |
pos<-match("ASPECT",layerNames(s_raster)) #Find column with name "value" |
|
66 |
r1<-raster(s_raster,layer=pos) #Select layer from stack |
|
67 |
pos<-match("slope",layerNames(s_raster)) #Find column with name "value" |
|
68 |
r2<-raster(s_raster,layer=pos) #Select layer from stack |
|
69 |
N<-cos(r1*pi/180) |
|
70 |
E<-sin(r1*pi/180) |
|
71 |
Nw<-sin(r2*pi/180)*cos(r1*pi/180) #Adding a variable to the dataframe |
|
72 |
Ew<-sin(r2*pi/180)*sin(r1*pi/180) #Adding variable to the dataframe. |
|
73 |
|
|
74 |
#2) DISTOOC, distance to coast: Would be useful to have a distance to coast layer ready... |
|
75 |
|
|
76 |
#3) LST clim? |
|
203 |
#Use GDAL instead?? system( 'gdalwarp...') |
|
77 | 204 |
|
78 |
#4) LCC land cover |
|
79 |
path_data<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/LandCover" |
|
80 |
setwd(path_data) |
|
81 |
pos<-match("LC1",layerNames(s_raster)) #Find column with name "value" |
|
82 |
LC1<-raster(s_raster,layer=pos) #Select layer from stack |
|
83 |
s_raster<-dropLayer(s_raster,pos) |
|
84 |
LC1[is.na(LC1)]<-0 #NA must be set to zero. |
|
85 |
pos<-match("LC3",layerNames(s_raster)) #Find column with name "value" |
|
86 |
LC3<-raster(s_raster,layer=pos) #Select layer from stack |
|
205 |
CANHEIGHT<-raster(s_raster,layer=pos) #Select layer from stack |
|
87 | 206 |
s_raster<-dropLayer(s_raster,pos) |
88 |
LC3[is.na(LC3)]<-0
|
|
207 |
CANHEIGHT[is.na(CANHEIGHT)]<-0
|
|
89 | 208 |
|
90 |
#Modification added to account for other land cover |
|
91 |
pos<-match("LC4",layerNames(s_raster)) #Find column with name "value" |
|
92 |
LC4<-raster(s_raster,layer=pos) #Select layer from stack |
|
93 |
s_raster<-dropLayer(s_raster,pos) |
|
94 |
LC4[is.na(LC4)]<-0 |
|
209 |
########################################## |
|
210 |
#3) Creating elev, aspect, slope from STRM |
|
95 | 211 |
|
96 |
pos<-match("LC6",layerNames(s_raster)) #Find column with name "value" |
|
97 |
LC6<-raster(s_raster,layer=pos) #Select layer from stack |
|
98 |
s_raster<-dropLayer(s_raster,pos) |
|
99 |
LC6[is.na(LC6)]<-0 |
|
212 |
SRTM_name<-file.path(elev_path,infile4) |
|
213 |
SRTM_reg<-create_raster_region(SRTM_name,reg_outline) |
|
100 | 214 |
|
101 |
LC_s<-stack(LC1,LC3,LC4,LC6) |
|
102 |
layerNames(LC_s)<-c("LC1_forest","LC3_grass","LC4_crop","LC6_urban") |
|
103 |
#plot(LC_s) |
|
215 |
#new_proj<-proj4string(SRTM_rast) #Assign coordinates reference system in PROJ4 format |
|
216 |
#region_temp_projected<-spTransform(modis_tiles,CRS(new_proj)) #Project from WGS84 to new coord. system |
|
217 |
#SRTM_crop_rast<-crop(SRTM, region_temp_projected) #crop using the extent from teh region tile |
|
218 |
#SRTM_projected_rast<-projectRaster(from=SRTM,crs=proj4string(modis_tiles),method="ngb") |
|
104 | 219 |
|
105 |
pos<-match("CANHEIGHT",layerNames(s_raster)) #Find column with name "value" |
|
106 |
CANHEIGHT<-raster(s_raster,layer=pos) #Select layer from stack |
|
107 |
s_raster<-dropLayer(s_raster,pos) |
|
108 |
CANHEIGHT[is.na(CANHEIGHT)]<-0 |
|
109 |
pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM" |
|
110 |
ELEV_SRTM<-raster(s_raster,layer=pos) #Select layer from stack on 10/30 |
|
111 |
s_raster<-dropLayer(s_raster,pos) |
|
112 |
ELEV_SRTM[ELEV_SRTM <0]<-NA |
|
220 |
#pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with name "ELEV_SRTM" |
|
221 |
#ELEV_SRTM<-raster(s_raster,layer=pos) #Select layer from stack on 10/30 |
|
222 |
#s_raster<-dropLayer(s_raster,pos) |
|
223 |
#ELEV_SRTM[ELEV_SRTM <0]<-NA |
|
113 | 224 |
|
114 |
xy<-coordinates(r1) #get x and y projected coordinates... |
|
115 |
xy_latlon<-project(xy, CRS, inv=TRUE) # find lat long for projected coordinats (or pixels...) |
|
116 |
lon<-raster(xy_latlon) #Transform a matrix into a raster object ncol=ncol(r1), nrow=nrow(r1)) |
|
117 |
ncol(lon)<-ncol(r1) |
|
118 |
nrow(lon)<-nrow(r1) |
|
119 |
extent(lon)<-extent(r1) |
|
120 |
projection(lon)<-CRS #At this stage this is still an empty raster with 536 nrow and 745 ncell |
|
121 |
lat<-lon |
|
122 |
values(lon)<-xy_latlon[,1] |
|
123 |
values(lat)<-xy_latlon[,2] |
|
225 |
#Call a function to reproject the data in a local projection defined on the fly using the processing tile |
|
226 |
#extent...For the time being just use sinusoidal projection. |
|
227 |
###calculate slope and aspect |
|
124 | 228 |
|
125 |
r<-stack(N,E,Nw,Ew,lon,lat,LC1,LC3,LC4,LC6, CANHEIGHT,ELEV_SRTM) |
|
126 |
rnames<-c("Northness","Eastness","Northness_w","Eastness_w", "lon","lat","LC1","LC3","LC4","LC6","CANHEIGHT","ELEV_SRTM") |
|
127 |
layerNames(r)<-rnames |
|
128 |
s_raster<-addLayer(s_raster, r) |
|
229 |
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 |
|
234 |
N<-cos(r1) |
|
235 |
E<-sin(r1) |
|
236 |
Nw<-sin(r2)*cos(r1) #Adding a variable to the dataframe |
|
237 |
Ew<-sin(r2)*sin(r1) #Adding variable to the dataframe. |
|
129 | 238 |
|
239 |
#topo_rast<-stack(STRM_reg,N,E,Nw,Ew) |
|
130 | 240 |
|
241 |
###################################### |
|
242 |
#4) LCC land cover |
|
131 | 243 |
|
244 |
oldpath<-getwd() |
|
245 |
setwd(lc_path) |
|
246 |
#lc_name<-"con_1km_class_1.tif" |
|
247 |
lc_list<-list.files(pattern="con_1km_class_.*.tif") |
|
248 |
#lc<-raster(file.path(lc_path,lc_names)) |
|
132 | 249 |
|
250 |
lc_reg_list<-vector("list",length(lc_list)) |
|
251 |
for (i in 1:length(lc_list)){ |
|
252 |
|
|
253 |
lc_name<-lc_list[[i]] |
|
254 |
lc_reg<-create_raster_region(lc_name,reg_outline) |
|
255 |
data_name<-paste("reg_",sub(".tif","",lc_name),"_",sep="") #can add more later... |
|
256 |
raster_name<-paste(data_name,out_suffix,".tif", sep="") |
|
257 |
writeRaster(lc_reg, filename=file.path(out_path,raster_name),overwrite=TRUE) |
|
258 |
lc_reg_list[[i]]<-file.path(out_path,raster_name) |
|
259 |
} |
|
260 |
setwd(out_path) |
|
261 |
lc_reg_list<-mixedsort(list.files(pattern="reg_con.*.tif")) |
|
262 |
lc_reg_s<-stack(lc_reg_list) |
|
133 | 263 |
|
264 |
#Now combine forest classes...in LC1 forest, LC2, LC3, LC4 and LC6-urban...?? |
|
134 | 265 |
|
266 |
#create a local mask for the tile/processing region |
|
135 | 267 |
|
268 |
LC12<-raster("reg_con_1km_class_12__VE_01292013.tif") #this is open water |
|
269 |
LC_mask<-LC12 |
|
270 |
LC_mask[LC_mask==100]<-NA |
|
271 |
LC_mask <- LC_mask > 100 |
|
272 |
tmp<-mask(lc_reg_s,LC_mask) |
|
136 | 273 |
|
274 |
############################### |
|
275 |
#5) DISTOC, distance to coast: Would be useful to have a distance to coast layer ready... |
|
137 | 276 |
|
277 |
#This does not work...clump needs igraph. look into this later...for now I used IDRISI to clump pixels. |
|
278 |
#rc<-clump(LC12) |
|
279 |
#tab_freq<-freq(rc) |
|
138 | 280 |
|
281 |
#Modify at a later stage: |
|
282 |
#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 |
|
285 |
#Distance calculated in a global layer?? |
|
286 |
distoc_reg<-distance(ocean_rast,doEdge=TRUE) #this is very slow: more than 30min use GRASS instead?? |
|
139 | 287 |
|
288 |
################################ |
|
289 |
#6) X-Y coordinates and LAT-LONG: do not keep in memory? |
|
290 |
r1<-ref_rast |
|
140 | 291 |
|
292 |
xy<-coordinates(r1) #get x and y projected coordinates... |
|
293 |
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] |
|
141 | 300 |
|
301 |
#coord_s<-stack(x,y,lat,lon) |
|
142 | 302 |
|
303 |
################################ |
|
304 |
##Step 3: combine covariates in one stack for the next work flow stage |
|
305 |
#Create a stack in tif format... |
|
143 | 306 |
|
144 |
#tmp<- readGDAL(tmp_str) |
|
145 |
#r<-GDAL.open(tmp_str) |
|
146 |
#rt<-create2GDAL(tmp_str, type="Float32") |
|
147 |
tmp_rast<-stack(tmp_str) |
|
148 |
X11(height=18,width=18) |
|
149 |
plot(tmp_rast) |
|
150 |
plot(subset(tmp_rast,12)) |
|
151 |
#levelplot(subset(tmp_rast,12)) |
|
152 |
png |
|
153 |
pat_str2 <- glob2rx(paste("nobs","*", tile,"*.tif",sep="")) |
|
154 |
tmp_str2<- mixedsort(list.files(pattern=pat_str2)) |
|
155 |
tmp_rast2<-stack(tmp_str2) |
|
156 |
plot(tmp_rast2) |
|
157 |
plot(subset(tmp_rast2,12)) |
|
158 |
levelplot(subset(tmp_rast2,12)) |
|
159 |
levelplot(tmp_rast2) |
|
160 |
png(filename="test.png",width=2400,height=2400) |
|
161 |
levelplot(tmp_rast2) |
|
162 |
dev.off() |
|
307 |
#? 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) |
|
163 | 312 |
|
313 |
##Extracting the variables values from the raster files |
|
164 | 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 |
|
165 | 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 |
|
166 | 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... |
|
328 |
#using bil format more efficient?? |
|
167 | 329 |
|
168 |
library(ncdf) |
|
330 |
####################################################### |
|
331 |
################### END OF SCRIPT ##################### |
Also available in: Unified diff
Covariates production, major reorganization, added sections for LST, LC and other covariates