Project

General

Profile

Download (6.24 KB) Statistics
| Branch: | Revision:
1
library(raster)
2
library(gtools)
3
library(rasterVis)
4
library(graphics)
5
library(grid)
6
library(lattice)
7
library(rgdal)
8
##checking results from GRASS climatology calculation...
9
in_path <- "/home/parmentier/Data/benoit_test"
10
in_path <- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/input_data/"
11
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))
16
infile1<-"worldborder_sinusoidal.shp"
17
infile2<-"modis_sinusoidal_grid_world.shp"
18
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
24

    
25
##Function select tiles:
26
out_file_name<-"modis_venezuela_region"
27
create_modis_tiles_region<-function(modis_grid,tiles){
28
  #This functions returns a subset of tiles from the modis grdi.
29
  #Arguments: modies grid tile,list of tiles
30
  #Output: spatial grid data frame of the subset of tiles
31
  
32
  h_list<-lapply(tiles,substr,start=2,stop=3) #passing multiple arguments
33
  v_list<-lapply(tiles,substr,start=5,stop=6) #passing multiple arguments
34
    
35
  selected_tiles<-subset(subset(modis_grid,subset = h %in% as.numeric (h_list) ),
36
                           subset = v %in% as.numeric(v_list)) 
37
  return(selected_tiles)
38
}
39

    
40
modis_tiles<-create_modis_tiles_region(modis_grid,tiles)
41

    
42
##Create covariates for the stuy area: pull everything from the same folder?
43

    
44
##Extracting the variables values from the raster files                                             
45

    
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
50

    
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
54

    
55
## Crop and reproject Canopy height
56

    
57
canopy_rast<-raster("Simard_Pinto_3DGlobalVeg_JGR.tif")
58
new_proj<-proj4string(canopy)                  #Assign coordinates reference system in PROJ4 format
59
region_temp_projected<-spTransform(modis_tiles,CRS(new_proj))     #Project from WGS84 to new coord. system
60
canopy_crop_rast<-crop(canopy, region_temp_projected) #crop using the extent from teh region tile
61
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?
77

    
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
87
s_raster<-dropLayer(s_raster,pos)
88
LC3[is.na(LC3)]<-0
89

    
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
95

    
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
100

    
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)
104

    
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
113

    
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]
124

    
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)
129

    
130

    
131

    
132

    
133

    
134

    
135

    
136

    
137

    
138

    
139

    
140

    
141

    
142

    
143

    
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()
163

    
164

    
165

    
166

    
167

    
168
library(ncdf)
(16-16/33)