1
|
#################################### INTERPOLATION TEMPERATURES #######################################
|
2
|
############################ AAG 2013 and OREGON transitition script #######################################
|
3
|
#This script reads information concerning the Oregon case study to adapt data for the revised
|
4
|
# interpolation code.
|
5
|
#Figures and data for the AAG conference are also produced.
|
6
|
#AUTHOR: Benoit Parmentier #
|
7
|
#DATE: 04/08/2013
|
8
|
#Version: 1
|
9
|
#PROJECT: Environmental Layers project #
|
10
|
#################################################################################################
|
11
|
|
12
|
###Loading R library and packages
|
13
|
library(gtools) # loading some useful tools
|
14
|
library(mgcv) # GAM package by Simon Wood
|
15
|
library(sp) # Spatial pacakge with class definition by Bivand et al.
|
16
|
library(spdep) # Spatial pacakge with methods and spatial stat. by Bivand et al.
|
17
|
library(rgdal) # GDAL wrapper for R, spatial utilities
|
18
|
library(gstat) # Kriging and co-kriging by Pebesma et al.
|
19
|
library(fields) # NCAR Spatial Interpolation methods such as kriging, splines
|
20
|
library(raster) # Hijmans et al. package for raster processing
|
21
|
library(gdata) # various tools with xls reading
|
22
|
library(rasterVis)
|
23
|
library(parallel)
|
24
|
library(maptools)
|
25
|
library(maps)
|
26
|
library(reshape)
|
27
|
library(plotrix)
|
28
|
|
29
|
#### UNCTION USED IN SCRIPT
|
30
|
|
31
|
create_modis_tiles_region<-function(tiles,modis_sp){
|
32
|
#This functions returns a subset of tiles from the modis grdi.
|
33
|
#Arguments: modies grid tile,list of tiles
|
34
|
#Output: spatial grid data frame of the subset of tiles
|
35
|
|
36
|
h_list<-lapply(tiles,substr,start=2,stop=3) #passing multiple arguments
|
37
|
v_list<-lapply(tiles,substr,start=5,stop=6) #passing multiple arguments
|
38
|
selected_tiles<-subset(subset(modis_sp,subset = h %in% as.numeric (h_list) ),
|
39
|
subset = v %in% as.numeric(v_list))
|
40
|
return(selected_tiles)
|
41
|
}
|
42
|
|
43
|
### Parameters and argument
|
44
|
|
45
|
lc_path<-"/home/layers/data/land-cover/lc-consensus-global"
|
46
|
infile_modis_grid<-"modis_sinusoidal_grid_world.shp"
|
47
|
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
|
48
|
infile_canheight<-"Simard_Pinto_3DGlobalVeg_JGR.tif" #Canopy height
|
49
|
#list_tiles_modis = c('h11v08','h11v07','h12v07','h12v08','h10v07','h10v08') #tile for Venezuel and surrounding area
|
50
|
list_tiles_modis = c('h08v04','h09v04')
|
51
|
|
52
|
#infile_reg_outline="" #input region outline defined by polygon
|
53
|
infile_reg_outline= "OR83M_state_outline.shp"
|
54
|
infile_countries_sinusoidal<-"countries_sinusoidal_world.shp"
|
55
|
|
56
|
#CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
|
57
|
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";
|
58
|
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
|
59
|
out_region_name<-"Oregon_region" #generated on the fly
|
60
|
out_prefix<-"_OR_04052013"
|
61
|
ref_rast_name<- "mean_day244_rescaled.rst" #This is the shape file of outline of the study area. #local raster name defining resolution, exent, local projection--. set on the fly??
|
62
|
infile_covariates<-"covariates__venezuela_region__VE_01292013.tif" #this is an output from covariate script and used in stage 3 and stage 4
|
63
|
|
64
|
#The names of covariates can be changed...these names should be output/input from covar script!!!
|
65
|
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
|
66
|
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10","LC11","LC12")
|
67
|
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",
|
68
|
"nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
|
69
|
"nobs_09","nobs_10","nobs_11","nobs_12")
|
70
|
covar_names<-c(rnames,lc_names,lst_names)
|
71
|
infile2<-"/home/layers/data/climate/ghcn/v2.92-upd-2012052822/ghcnd-stations.txt" #This is the textfile of station
|
72
|
|
73
|
in_path<- "/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_covariates"
|
74
|
|
75
|
#c("Oregon", c("h08v04","h09v04","h08v05","h09v05"))
|
76
|
study_area_list_tiles <- vector("list",6)
|
77
|
study_area_list_tiles[[1]] <-list("Oregon", c("h08v04","h09v04"))
|
78
|
study_area_list_tiles[[2]] <-list("Venezuela",c("h10v07", "h10v08", "h11v7", "h11v08", "h12v07", "h12v08"))
|
79
|
study_area_list_tiles[[3]] <-list("Norway",c("h18v02","h18v03", "h19v02", "h19v03"))
|
80
|
study_area_list_tiles[[4]] <-list("East_Africa",c("h20v08", "h21v08", "h22v08", "h20v09", "h21v09", "h22v09", "h21v10"))
|
81
|
study_area_list_tiles[[5]] <-list("South_Africa",c("h19v11", "h20v11", "h19v12", "h20v12"))
|
82
|
study_area_list_tiles[[6]] <-list("Queensland",c("h31v10", "h31v10", "h32v10", "h30v11", "h31v11"))
|
83
|
|
84
|
#######################################################################################
|
85
|
########################### BEGIN SCRIPT ######################################
|
86
|
|
87
|
|
88
|
setwd(in_path)
|
89
|
|
90
|
|
91
|
####### PART I: Prepare data for figures and for Oregon interpolation ##########
|
92
|
|
93
|
### Read in Venezuela covariate stack
|
94
|
|
95
|
s_raster_Ven<-brick(infile_covariates) #read brick
|
96
|
names(s_raster_Ven)<-covar_names #assign names
|
97
|
mm_01_Ven<-subset(s_raster_ven,"mm_01") #select LST January month average
|
98
|
|
99
|
### Read in world map to show stuy areas.
|
100
|
|
101
|
world_sp <- getData("countries") # different resolutions available
|
102
|
outfile2<-file.path(in_path,paste("word_countries.shp",sep="")) #Name of the file
|
103
|
writeOGR(world_sp,dsn= dirname(outfile2),layer= sub(".shp","",basename(outfile2)), driver="ESRI Shapefile",overwrite_layer=TRUE)
|
104
|
|
105
|
### Read in sinusoidal grid and world countries
|
106
|
filename<-sub(".shp","",infile_modis_grid) #Removing the extension from file.
|
107
|
modis_grid<-readOGR(".", filename) #Reading shape file using rgdal library
|
108
|
|
109
|
### Create list ofALL STUDY AREAS/TEST SITES
|
110
|
|
111
|
## Create list of study area regions:
|
112
|
list_tiles<-lapply(1:length(study_area_list_tiles),function(k) study_area_list_tiles[[k]][[2]])
|
113
|
modis_reg_outlines<-lapply(list_tiles,FUN=create_modis_tiles_region,modis_sp=modis_grid) #problem...this does not
|
114
|
|
115
|
#writeOGR(modis_reg_outline,dsn= ".",layer= paste("outline",out_region_name,"_",out_suffix,sep=""),
|
116
|
# driver="ESRI Shapefile",overwrite_layer="TRUE")
|
117
|
|
118
|
####################################################
|
119
|
#Read in GHCND database station locations
|
120
|
|
121
|
dat_stat <- read.fwf(infile2,
|
122
|
widths = c(11,9,10,7,3,31,4,4,6),fill=TRUE)
|
123
|
colnames(dat_stat)<-c("STAT_ID","lat","lon","elev","state","name","GSNF","HCNF","WMOID")
|
124
|
coords<- dat_stat[,c('lon','lat')]
|
125
|
coordinates(dat_stat)<-coords
|
126
|
proj4string(dat_stat)<-CRS_locs_WGS84 #this is the WGS84 projection
|
127
|
#Save shapefile for later
|
128
|
outfile1<-file.path(in_path,paste("ghcnd_stations.shp",sep="")) #Name of the file
|
129
|
writeOGR(dat_stat,dsn= dirname(outfile1),layer= sub(".shp","",basename(outfile1)), driver="ESRI Shapefile",overwrite_layer=TRUE)
|
130
|
|
131
|
interp_area <- readOGR(dsn=in_path,sub(".shp","",infile_reg_outline))
|
132
|
interp_area_WGS84 <-spTransform(interp_area,CRS_locs_WGS84) # Project from WGS84 to new coord. system
|
133
|
|
134
|
# Spatial query to find relevant stations
|
135
|
|
136
|
inside <- !is.na(over(dat_stat, as(interp_area_WGS84, "SpatialPolygons"))) #Finding stations contained in the current interpolation area
|
137
|
stat_reg_OR<-dat_stat[inside,] #Selecting stations contained in the current interpolation area
|
138
|
|
139
|
stat_reg_OR <-spTransform(stat_reg_OR,CRS(proj4string(interp_area))) # Project from WGS84 to new coord. system
|
140
|
|
141
|
#Now Venezuela
|
142
|
interp_area_Ven_WGS84 <-spTransform(modis_reg_outlines[[2]],CRS_locs_WGS84) # Project from WGS84 to new coord. system
|
143
|
inside <- !is.na(over(dat_stat, as(interp_area_Ven_WGS84, "SpatialPolygons"))) #Finding stations contained in the current interpolation area
|
144
|
stat_reg_Ven <-dat_stat[inside,] #Selecting stations contained in the current interpolation area
|
145
|
|
146
|
## Get the data in the local projection
|
147
|
stat_reg_Ven <-spTransform(stat_reg_Ven,CRS(proj4string(modis_reg_outlines[[2]]))) # Project from WGS84 to new coord. system
|
148
|
|
149
|
### READ IN COVARIATES FILES FOR OREGON AND MAKE IT A MULTI-BAND FILE
|
150
|
|
151
|
inlistf<-"list_files_covariates_04032013.txt"
|
152
|
lines<-read.table(paste(inlistf,sep=""), sep=" ") #Column 1 contains the names of raster files
|
153
|
inlistvar<-lines[,1]
|
154
|
inlistvar<-as.character(inlistvar)
|
155
|
covar_names_OR<-as.character(lines[,2]) #Column two contains short names for covaraites
|
156
|
|
157
|
s_raster_OR<- stack(inlistvar) #Creating a stack of raster images from the list of variables.
|
158
|
layerNames(s_raster_OR)<-covar_names_OR #Assigning names to the raster layers
|
159
|
|
160
|
aspect<-subset(s_raster_OR,"aspect") #Select layer from stack
|
161
|
slope<-subset(s_raster_OR,"slope") #Select layer from stack
|
162
|
distoc<-subset(s_raster_OR,"DISTOC")
|
163
|
|
164
|
N<-cos(aspect*pi/180)
|
165
|
E<-sin(aspect*pi/180)
|
166
|
Nw<-sin(slope*pi/180)*cos(aspect*pi/180) #Adding a variable to the dataframe
|
167
|
Ew<-sin(slope*pi/180)*sin(aspect*pi/180) #Adding variable to the dataframe.
|
168
|
|
169
|
xy <-coordinates(slope) #get x and y projected coordinates...
|
170
|
xy_latlon<-project(xy, CRS_interp, inv=TRUE) # find lat long for projected coordinats (or pixels...)
|
171
|
|
172
|
x <-init(slope,v="x")
|
173
|
y <-init(slope,v="y")
|
174
|
lon<-x
|
175
|
lat<-y
|
176
|
lon <-setValues(lon,xy_latlon[,1]) #longitude for every pixel in the processing tile/region
|
177
|
lat <-setValues(lat,xy_latlon[,2]) #latitude for every pixel in the processing tile/region
|
178
|
CANHEIGHT<-subset(s_raster_OR,"CANHEIGHT")
|
179
|
CANHEIGHT[is.na(CANHEIGHT)]<-0
|
180
|
|
181
|
elev<-subset(s_raster_OR,"elev")
|
182
|
elev[elev==-9999] <- NA
|
183
|
|
184
|
r<-stack(x,y,lon,lat,N,E,Nw,Ew,elev,slope,aspect,CANHEIGHT,distoc)
|
185
|
rnames<-c("x","y","lon","lat","N","E","N_w","E_w","elev","slope","aspect","CANHEIGHT","DISTOC")
|
186
|
s_raster<-r
|
187
|
#Add landcover layers
|
188
|
lc_names<-c("LC1","LC2","LC3","LC4","LC5","LC6","LC7","LC8","LC9","LC10")
|
189
|
lc_reg_s<-subset(s_raster_OR,lc_names)
|
190
|
#Should be a function...ok for now??
|
191
|
test<-vector("list",nlayers(lc_reg_s))
|
192
|
for (k in 1:nlayers(lc_reg_s)){
|
193
|
LC<-raster(lc_reg_s,layer=k) #Select layer from stack
|
194
|
LC[is.na(LC)]<-0
|
195
|
test[[k]]<-LC
|
196
|
}
|
197
|
#tmp_df<-freq(lc_reg_s,merge=TRUE) #check to see if it worked
|
198
|
#head(tmp_df)
|
199
|
lc_reg_s<-stack(test)
|
200
|
s_raster<-addLayer(s_raster, lc_reg_s)
|
201
|
|
202
|
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",
|
203
|
"nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
|
204
|
"nobs_09","nobs_10","nobs_11","nobs_12")
|
205
|
lst_mm_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")
|
206
|
lst_mm_s<-subset(s_raster_OR,lst_mm_names)
|
207
|
lst_mm_s <- lst_mm_s - 273.16
|
208
|
lst_nobs_names<-c("nobs_01","nobs_02","nobs_03","nobs_04","nobs_05","nobs_06","nobs_07","nobs_08",
|
209
|
"nobs_09","nobs_10","nobs_11","nobs_12")
|
210
|
lst_nobs_s<-subset(s_raster_OR,lst_nobs_names)
|
211
|
|
212
|
s_raster<-addLayer(s_raster, lst_mm_s)
|
213
|
s_raster<-addLayer(s_raster, lst_nobs_s)
|
214
|
covar_names<-c(rnames,lc_names,lst_names)
|
215
|
names(s_raster)<-covar_names
|
216
|
|
217
|
# create mask!!! Should combine with mask of elev
|
218
|
LC10<-subset(s_raster,"LC10")
|
219
|
|
220
|
LC10_mask<-LC10
|
221
|
LC10_mask[is.na(LC10_mask)]<- 0
|
222
|
LC10_mask[LC10==100]<- NA
|
223
|
LC10_mask[LC10_mask<100]<- 1
|
224
|
LC10_mask[is.na(LC10_mask)]<- 0
|
225
|
mask_land_NA<-LC10_mask
|
226
|
mask_land_NA[mask_land_NA==0]<-NA
|
227
|
|
228
|
|
229
|
##### SAVE AS MULTIBAND...make this a function...
|
230
|
|
231
|
#list of files...
|
232
|
file_format<-".tif"
|
233
|
NA_val<- -9999
|
234
|
band_order<- "BSQ"
|
235
|
var<-"TMAX"
|
236
|
data_name<-paste("covariates_",out_region_name,"_",sep="")
|
237
|
raster_name<-paste(data_name,var,"_",out_prefix,file_format, sep="")
|
238
|
#writeRaster(s_raster, filename=raster_name,NAflag=-999,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE) #Writing the data in a raster file format...
|
239
|
#if mask
|
240
|
#stat_val<- extract(s_raster, ghcn3) #Extracting values from the raster stack for every point location in coords data frame.
|
241
|
s_raster_m<-mask(s_raster,mask_land_NA,filename=raster_name,
|
242
|
overwrite=TRUE,NAflag=NA_val,bylayer=FALSE,bandorder=band_order)
|
243
|
#if no mask
|
244
|
#writeRaster(s_raster, filename=raster_name,NAflag=-999,bylayer=FALSE,bandorder="BSQ",overwrite=TRUE) #Writing the data in a raster file format...
|
245
|
|
246
|
############# PART II: PRODUCE FIGURES #######
|
247
|
|
248
|
### CREATE FIGURE TEST SITES
|
249
|
|
250
|
dat_stat_sinusoidal <- spTransform(dat_stat,CRS(proj4string(modis_grid)))
|
251
|
world_sinusoidal <- readOGR(dsn=".",sub(".shp","",infile_countries_sinusoidal))
|
252
|
|
253
|
png(paste("Study_area_modis_grid",out_prefix,".png",sep=""))
|
254
|
plot(world_sinusoidal)
|
255
|
plot(dat_stat_sinusoidal,cex=0.2,pch=16,col=c("blue"),add=TRUE)
|
256
|
plot(modis_grid,add=TRUE)
|
257
|
for (k in 1:length(modis_reg_outlines)){
|
258
|
plot(modis_reg_outlines[[k]],border=c("red"),lwd=2.5,add=TRUE)
|
259
|
}
|
260
|
title("Study area for temperature and precipitation predictions")
|
261
|
#legend
|
262
|
dev.off()
|
263
|
|
264
|
### CREATE FIGURE MEAN DAILY AND MEAN MONTHLY: AAG 2013 ####
|
265
|
|
266
|
lst_md<-raster(ref_rast_name)
|
267
|
lst_mm_09<-subset(s_raster,"mm_09")
|
268
|
plot(stack(lst_md,lst_mm_09))
|
269
|
|
270
|
lst_md<-raster("mean_day001_rescaled.rst")
|
271
|
lst_md<- lst_md - 273.16
|
272
|
lst_mm_01<-subset(s_raster,"mm_01")
|
273
|
|
274
|
png(filename=paste("Comparison_daily_monthly_mean_lst",out_prefix,".png",sep=""),width=960,height=480)
|
275
|
par(mfrow=c(1,2))
|
276
|
plot(lst_md)
|
277
|
plot(interp_area,add=TRUE)
|
278
|
title("Mean January 1")
|
279
|
plot(lst_mm_01)
|
280
|
plot(interp_area,add=TRUE)
|
281
|
title("Mean for monht of January")
|
282
|
dev.off()
|
283
|
|
284
|
### CREATE FIGURE NUMBER OF STATIONS PER SITE
|
285
|
|
286
|
png(paste("stations_for_Venezuela_Oregon_areas",out_prefix,".png",sep=""),,width=960,height=480)
|
287
|
par(mfrow=c(1,2))
|
288
|
#Oregon data
|
289
|
plot(lst_mm_01)
|
290
|
plot(interp_area,add=TRUE)
|
291
|
plot(stat_reg_OR,add=TRUE)
|
292
|
title("Stations located in Oregon from GHNCD")
|
293
|
plot(mm_01_Ven)
|
294
|
plot(modis_reg_outlines[[2]],add=TRUE)
|
295
|
plot(stat_reg_Ven,add=TRUE)
|
296
|
title("Stations located in Venezuela from GHNCD")
|
297
|
dev.off()
|
298
|
|
299
|
### CREATE FIGURE NUMBER OF STATIONS PER SITE AND SPECIFIC MONTH...
|
300
|
|
301
|
#png(paste("stations_for_Venezuela_Oregon_areas_per_month",out_prefix,".png",sep=""))
|
302
|
#par(mfrow=c(1,2))
|
303
|
#plot(interp_area_WGS84)
|
304
|
#plot(stat_reg_OR,add=TRUE)
|
305
|
#plot(modis_reg_outlines[[2]])
|
306
|
#plot(stat_reg_Ven,add=TRUE)
|
307
|
#dev.off()
|
308
|
|
309
|
############ PART III: SCREENING OF COVARIATES #############
|
310
|
|
311
|
### SCREENING FUNCTION for covariate stack and GHNCD data base to add later in the functions
|
312
|
|
313
|
#Screen for extreme values": this needs more thought, min and max val vary with regions
|
314
|
#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)
|
315
|
#r1[r1 < (min_val)]<-NA
|
316
|
#s_raster<-addLayer(s_raster,LST) #Adding current month
|
317
|
|
318
|
nel<-12
|
319
|
#tab_range<-data.frame(varname=character(nel),varterm=character(nel),vmin=numeric(nel),vmax=numeric(nel))
|
320
|
val_range<-vector("list",nel) #list of one row data.frame
|
321
|
val_rst<-vector("list",nel) #list of one row data.frame
|
322
|
val_range[[1]]<-data.frame(varname="lon",vmin=-180,vmax=180)
|
323
|
val_range[[2]]<-data.frame(varname="lat",vmin=-90,vmax=90)
|
324
|
val_range[[3]]<-data.frame(varname="N",vmin=-1,vmax=1)
|
325
|
val_range[[4]]<-data.frame(varname="E",vmin=-1,vmax=1)
|
326
|
val_range[[5]]<-data.frame(varname="N_w",vmin=-1,vmax=1)
|
327
|
val_range[[6]]<-data.frame(varname="E_w",vmin=-1,vmax=1)
|
328
|
val_range[[7]]<-data.frame(varname="elev",vmin=0,vmax=6000)
|
329
|
val_range[[8]]<-data.frame(varname="slope",varterm="slope",vmin=0,vmax=90)
|
330
|
val_range[[9]]<-data.frame(varname="aspect",varterm="aspect",vmin=0,vmax=360)
|
331
|
val_range[[10]]<-data.frame(varname="DISTOC",vmin=-0,vmax=10000000)
|
332
|
val_range[[11]]<-data.frame(varname="CANHEIGHT",vmin=0,vmax=255)
|
333
|
val_range[[12]]<-data.frame(varname="LC1",vmin=0,vmax=100)
|
334
|
val_range[[13]]<-data.frame(varname="LC3",vmin=0,vmax=100)
|
335
|
val_range[[14]]<-data.frame(varname="mm_01",vmin=-15,vmax=50)
|
336
|
val_range[[15]]<-data.frame(varname="mm_02",vmin=-15,vmax=50)
|
337
|
val_range[[16]]<-data.frame(varname="mm_03",vmin=-15,vmax=50)
|
338
|
val_range[[17]]<-data.frame(varname="mm_04",vmin=-15,vmax=50)
|
339
|
val_range[[18]]<-data.frame(varname="mm_05",vmin=-15,vmax=50)
|
340
|
val_range[[19]]<-data.frame(varname="mm_06",vmin=-15,vmax=50)
|
341
|
val_range[[20]]<-data.frame(varname="mm_07",vmin=-15,vmax=50)
|
342
|
val_range[[21]]<-data.frame(varname="mm_08",vmin=-15,vmax=50)
|
343
|
val_range[[22]]<-data.frame(varname="mm_09",vmin=-15,vmax=50)
|
344
|
val_range[[23]]<-data.frame(varname="mm_10",vmin=-15,vmax=50)
|
345
|
val_range[[24]]<-data.frame(varname="mm_11",vmin=-15,vmax=50)
|
346
|
val_range[[25]]<-data.frame(varname="mm_12",vmin=-15,vmax=50)
|
347
|
|
348
|
tab_range<-do.call(rbind,val_range)
|
349
|
|
350
|
#pos<-match("ELEV_SRTM",layerNames(s_raster)) #Find column with the current month for instance mm12
|
351
|
#ELEV_SRTM<-raster(s_raster,pos)
|
352
|
|
353
|
screening_val_covariates_fun<-function(tab_range,r_stack){
|
354
|
#Screening for raster stack
|
355
|
#
|
356
|
for (k in 1:nrow(tab_range)){
|
357
|
avl<-c(-Inf,tab_range$vmin[k],NA, tab_range$vmax[k],+Inf,NA) #This creates a input vector...val 1 are -9999, 2 neg, 3 positive
|
358
|
rclmat<-matrix(avl,ncol=3,byrow=TRUE)
|
359
|
#s_raster_r<-raster(r_stack,match(tab_range$varterm[k],names(r_stack))) #select relevant layer from stack
|
360
|
s_raster_r<-raster(r_stack,match(tab_range$varname[k],names(r_stack)))
|
361
|
s_raster_r<-reclass(s_raster_r,rclmat) #now reclass values
|
362
|
r_stack<-dropLayer(r_stack,"N")
|
363
|
names(s_raster_r)<-tab_range$varname[k] #Loss of layer names when using reclass
|
364
|
val_rst[[k]]<-s_raster_r
|
365
|
}
|
366
|
s_rst_m<-stack(val_rst) #This a raster stack with valid range of values
|
367
|
r_stack<-addLayer(r_stack,s_rst_m) #add back layers that were screened out
|
368
|
return(r_stack)
|
369
|
}
|
370
|
|
371
|
#### ADD SCREENING FUNCTION FOR GHCND extracted!!!
|
372
|
|
373
|
#Remove NA for LC and CANHEIGHT
|
374
|
#ghcn$LC1[is.na(ghcn$LC1)]<-0
|
375
|
#ghcn$LC3[is.na(ghcn$LC3)]<-0
|
376
|
#ghcn$CANHEIGHT[is.na(ghcn$CANHEIGHT)]<-0
|
377
|
#ghcn$LC4[is.na(ghcn$LC4)]<-0
|
378
|
#ghcn$LC6[is.na(ghcn$LC6)]<-0
|
379
|
##ghcn$ELEV_SRTM[ghcn$ELEV_SRTM==-9999]<-NA
|
380
|
#dst<-subset(dst,dst$TMax>-15 & dst$TMax<40)
|
381
|
#dst<-subset(dst,dst$ELEV_SRTM>0) #This will drop two stations...or 24 rows
|