Project

General

Profile

« Previous | Next » 

Revision 1ee7c0bb

Added by Benoit Parmentier over 11 years ago

adding function to define projection system

View differences:

climate/research/oregon/interpolation/covariates_production_temperatures.R
11 11
# -24 LST layers: "climatology" produced from MOD11A1, LST (mean and obs) using script in step 1 of workflow
12 12
# 3) The output is a multiband file in tif format with projected covariates for the processing region/tile.             
13 13
#AUTHOR: Benoit Parmentier                                                                       
14
#DATE: 06/19/2013                                                                                 
14
#DATE: 06/20/2013                                                                                 
15 15
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
16 16

  
17 17
##Comments and TODO:
......
204 204
  return(r_stack)
205 205
}
206 206

  
207
define_crs_from_extent_fun<-function(reg_outline,buffer_dist){
208
  #Screening values for raster stack
209
  #input: list_val_range: list of character strings comma separated
210
  #        e.g.: "mm_12,-15,50","mm_12,-15,50"
211
  #               variable name, min value, max value
212
  library(rgeos)
213
  
214
  #Buffer function not in use yet!! need query for specific matching MODIS tile !!! use gIntersection
215
  if (buffer_dist!=0){
216
    reg_outline_dissolved <- gUnionCascaded(reg_outline)  #dissolve polygons
217
    reg_outline <- gBuffer(reg_outline_dissolved,width=buffer_dist*1000)
218
  }
219
  
220
  #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=WGS84 +datum=WGS84 +units=m +no_defs";
221
  reg_centroid <- gCentroid(reg_outline)
222
  reg_centroid_WGS84 <- spTransform(reg_centroid,CRS_locs_WGS84) #get cooddinates of center of region in lat, lon
223
  reg_outline_WGS84 <- spTransform(reg_outline,CRS_locs_WGS84) #get cooddinates of center of region in lat, lon
224
  reg_extent <-extent( reg_outline_WGS84) #get boudning box of extent
225
  #  xy_latlon<-project(xy, CRS_interp, inv=TRUE) # find lat long for projected coordinats (or pixels...)
226

  
227
  #Calculate projection parameters
228
  reg_lat_1 <- ymin(reg_extent)+((ymax(reg_extent)- ymin(reg_extent))/4)
229
  reg_lat_2 <- ymax(reg_extent)-((ymax(reg_extent)- ymin(reg_extent))/4)
230

  
231
  reg_lon_0 <- coordinates(reg_centroid_WGS84)[1]
232
  reg_lat_0 <- coordinates(reg_centroid_WGS84)[2]
233
  reg_x_0 <- 0
234
  reg_y_0 <- 0
235
  
236
  #Add false northing and false easting calucation for y_0,x_0
237
  #CRS_interp <- paste("+proj=lcc +lat_1=",43," +lat_2=",45.5," +lat_0=",41.75," +lon_0=",-120.5,
238
  #                    " +x_0=",0,"+y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
239
  
240
  CRS_interp <- paste("+proj=lcc +lat_1=",reg_lat_1," +lat_2=",reg_lat_2," +lat_0=",reg_lat_0," +lon_0=",reg_lon_0,
241
                      " +x_0=",reg_x_0," +y_0=",reg_y_0," +ellps=WGS84 +datum=WGS84 +units=m +no_defs",sep="")
242
  
243
  reg_outline_interp <- spTransform(reg_outline,CRS(CRS_interp)) #get cooddinates of center of region in lat, lon
244
  
245
  #add part to save projected file??
246
  #return reg_outline!!!
247
  reg_outline_obj <-list(reg_outline_interp,CRS_interp)
248
  names(reg_outline_obj) <-c("reg_outline","CRS_interp")
249
  return(reg_outline_obj)
250
} 
251
  
252
  
207 253
covariates_production_temperature<-function(list_param){
208 254
  #This functions produce covariates used in the interpolation of temperature.
209 255
  #It requires 16 arguments:
......
265 311
  out_suffix_modis <- list_param$out_suffix_modis
266 312
  covar_names<-list_param$covar_names 
267 313
  list_val_range <-list_param$list_val_range
314
  buffer_dist <-list_param$buffer_dist
268 315
  
269 316
  ##### SET UP STUDY AREA ####
270 317
  
......
288 335
  
289 336
  #if no shapefile defining the study/processing area then create one using modis grid tiles
290 337
  if (infile_reg_outline==""){
291
    reg_outline<-create_modis_tiles_region(modis_grid,list_tiles_modis) #problem...this does not 
338
    reg_outline_modis <-create_modis_tiles_region(modis_grid,list_tiles_modis) #problem...this does not 
292 339
    #align with extent of modis LST!!!
340
    #now add projection on the fly
341
    infile_reg_outline <-paste("modis_outline",out_region_name,"_",out_suffix,".shp",sep="")
342
    writeOGR(reg_outline_modis,dsn= out_path,layer= sub(".shp","",infile_reg_outline), 
343
             driver="ESRI Shapefile",overwrite_layer="TRUE")
344
    
345
    reg_outline_obj <- define_crs_from_extent_fun(reg_outline_modis,buffer_dist)
346
    reg_outline <-reg_outline_obj$reg_outline
347
    CRS_interp <-reg_outline_obj$CRS_interp
348
    
293 349
    infile_reg_outline <-paste("outline",out_region_name,"_",out_suffix,".shp",sep="")
294 350
    writeOGR(reg_outline,dsn= out_path,layer= sub(".shp","",infile_reg_outline), 
295 351
             driver="ESRI Shapefile",overwrite_layer="TRUE")
......
365 421
  
366 422
  #Use this as ref file for now?? Ok for the time being: this will need to change to be a processing tile.
367 423
  if (ref_rast_name==""){
368
    ref_rast<-raster(mean_m_list[[1]]) 
369 424
    #Use one mosaiced modis tile as reference image...We will need to add a function 
425
    ref_rast_temp <-raster(mean_m_list[[1]]) 
426
    ref_rast <-projectRaster(from=ref_rast_temp,crs=CRS_interp,method="ngb")
427

  
370 428
    #to define a local reference system and reproject later!!
371 429
    #Assign new projection system here in the argument CRS_interp (!it is used later)
372 430
  }else{
373 431
    ref_rast<-raster(ref_rast_name) #This is the reference image used to define the study/processing area
374
    proj4string(ref_rast) <- CRS_interp #Assign given reference system
432
    proj4string(ref_rast) <- CRS_interp #Assign given reference system from master script...
375 433
  }
376 434
  
377 435
  ## Project mosaiced tiles if local projection is provided...
......
380 438
  mean_lst_list_outnames<-change_names_file_list(mean_m_list,out_suffix_lst,"reg_",".tif",out_path=out_path)     
381 439
  nobs_lst_list_outnames<-change_names_file_list(nobs_m_list,out_suffix_lst,"reg_",".tif",out_path=out_path)   
382 440
  
441
  #list(mean_m_list)
442
  list_param_create_region<-list(j,raster_name=mean_m_list,reg_ref_rast=ref_rast,out_rast_name=mean_lst_list_outnames)
443
  #test<-create__m_raster_region(1,list_param_create_region)
444
  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
445
  list_param_create_region<-list(j,raster_name=nobs_m_list,reg_ref_rast=ref_rast,out_rast_name=nobs_lst_list_outnames)
446
  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
447
  
383 448
  # if ref_name!="" need to reproject and clip!!! do this in mclapply for all the list of covar!!!
384
  if (ref_rast_name!=""){
385
    #list(mean_m_list)
386
    list_param_create_region<-list(j,raster_name=mean_m_list,reg_ref_rast=ref_rast,out_rast_name=mean_lst_list_outnames)
387
    #test<-create__m_raster_region(1,list_param_create_region)
388
    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
389
    list_param_create_region<-list(j,raster_name=nobs_m_list,reg_ref_rast=ref_rast,out_rast_name=nobs_lst_list_outnames)
390
    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
391
  }
392

  
449
  #if (ref_rast_name!=""){
450
  #  #list(mean_m_list)
451
  #  list_param_create_region<-list(j,raster_name=mean_m_list,reg_ref_rast=ref_rast,out_rast_name=mean_lst_list_outnames)
452
  #  #test<-create__m_raster_region(1,list_param_create_region)
453
  #  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
454
  #  list_param_create_region<-list(j,raster_name=nobs_m_list,reg_ref_rast=ref_rast,out_rast_name=nobs_lst_list_outnames)
455
  #  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
456
  #}
393 457
  
394 458
  #ref_rast <-raster("mosaiced_dec_lst_mean_VE_03182013.tif")
395 459
  #Modis shapefile tile is slighly shifted:
climate/research/oregon/interpolation/master_script_temp.R
10 10
#STAGE 5: Output analyses: assessment of results for specific dates...
11 11
#
12 12
#AUTHOR: Benoit Parmentier                                                                       
13
#DATE: 06/19/2013                                                                                 
13
#DATE: 06/20/2013                                                                                 
14 14

  
15 15
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363, TASK$568--   
16 16

  
......
53 53
clim_script <- file.path(script_path,"climatology_05312013.py") # LST climatology python script
54 54
grass_setting_script <- file.path(script_path,"grass-setup.R") #Set up system shell environment for python+GRASS
55 55
source(file.path(script_path,"download_and_produce_MODIS_LST_climatology_05302013.R"))
56
source(file.path(script_path,"covariates_production_temperatures_06192013.R"))
56
source(file.path(script_path,"covariates_production_temperatures_06202013.R"))
57 57
source(file.path(script_path,"Database_stations_covariates_processing_function_05212013.R"))
58 58
source(file.path(script_path,"GAM_fusion_analysis_raster_prediction_multisampling_06082013.R"))
59 59
source(file.path(script_path,"results_interpolation_date_output_analyses_06102013.R"))
......
101 101
#infile_reg_outline<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/outline_venezuela_region__VE_01292013.shp" 
102 102
#infile_covariates<-"/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/covariates__venezuela_region_TMIN__VE_03192013.tif" #covariates stack for TMIN
103 103
#infile_covariates<- "/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/covariates_Oregon_region_TMAX__OR_04052013.tif" #Oregon covar TMAX from earlier codes...for continuity
104
#infile_reg_outline=""  #input region outline defined by polygon: none for Venezuela
104
infile_reg_outline=""  #input region outline defined by polygon: none for Venezuela
105 105
#This is the shape file of outline of the study area                                                      #It is an input/output of the covariate script
106
infile_reg_outline <- "/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/OR83M_state_outline.shp"  #input region outline defined by polygon: Oregon
106
#infile_reg_outline <- "/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/OR83M_state_outline.shp"  #input region outline defined by polygon: Oregon
107 107
#infile_reg_outline <-"OR83M_state_outline.shp" #remove this parameter!!!
108
#ref_rast_name<-""  #local raster name defining resolution, exent, local projection--. set on the fly?? 
108
ref_rast_name<-""  #local raster name defining resolution, exent, local projection--. set on the fly?? 
109 109
#this may be redundant with infile_reg_outline
110
ref_rast_name<-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/mean_day244_rescaled.rst"  #local raster name defining resolution, exent: oregon
110
#ref_rast_name<-"/home/parmentier/Data/IPLANT_project/Oregon_interpolation/Oregon_03142013/mean_day244_rescaled.rst"  #local raster name defining resolution, exent: oregon
111
buffer_dist<-0 #not in use yet, must change climatology step to make sure additional tiles are downloaded and LST averages
112
               #must also be calculated for neighbouring tiles.
111 113

  
112 114
#covar_names see stage 2
113 115

  
114 116
#list_tiles_modis <- c("h11v08,h11v07,h12v07,h12v08,h10v07,h10v08") #tile for Venezuela and surrounding area
115 117
list_tiles_modis <- c("h08v04,h09v04") #tiles for Oregon
116 118
  
117
#CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
118
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";
119
CRS_interp<-"+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs";
120
#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";
121
#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";
122

  
119 123
#"+proj=lcc +lat_1=43 +lat_2=45.5 +lat_0=41.75 +lon_0=-120.5 +x_0=400000 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80"
120 124
CRS_locs_WGS84<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0") #Station coords WGS84
121 125
#out_region_name<-"_venezuela_region" #generated on the fly
......
163 167

  
164 168
############ STAGE 2: Covariate production ################
165 169

  
166
#list of 17 parameters
170
#list of 18 parameters
167 171
list_param_covar_production<-list(var,out_path,lc_path,infile_modis_grid,infile_elev,infile_canheight,
168 172
                                  infile_distoc,list_tiles_modis,infile_reg_outline,CRS_interp,CRS_locs_WGS84,out_region_name,
169
                                  list_val_range,out_suffix,out_suffix_modis,ref_rast_name,hdfdir,covar_names) 
173
                                  buffer_dist,list_val_range,out_suffix,out_suffix_modis,ref_rast_name,hdfdir,covar_names) 
170 174

  
171 175
names(list_param_covar_production)<-c("var","out_path","lc_path","infile_modis_grid","infile_elev","infile_canheight",
172 176
                                      "infile_distoc","list_tiles_modis","infile_reg_outline","CRS_interp","CRS_locs_WGS84","out_region_name",
173
                                      "list_val_range","out_suffix","out_suffix_modis","ref_rast_name","hdfdir","covar_names") 
177
                                      "buffer_dist","list_val_range","out_suffix","out_suffix_modis","ref_rast_name","hdfdir","covar_names") 
174 178

  
175 179
## Modify to store infile_covar_brick in output folder!!!
176 180
if (stages_to_run[2]==2){

Also available in: Unified diff