Project

General

Profile

Download (7.89 KB) Statistics
| Branch: | Revision:
1
################################ DOWNLOAD AND PRODUCE LST CLIMATOLOGY  #######################################
2
############################ Production of spatial average from MODIS ##########################################
3
#This script downloads modis tiles and computes monthly average over a given time period.
4
#Part 1: Download given list of tiles...
5
#Part 2: Compute climatology using monhtly average method over 10 years 
6
#
7
# This R script calls python scripts. 
8
#
9
#AUTHORS: Benoit Parmentier    
10
#         Using python scripts started by J. Regetz and modified by B.Parmentier.
11
#DATE: 05/30/2013                                                                                 
12
#To Do:
13
#-collect list of LST files created
14
#-solve issue realted to parallelization of python script: GRASS problem?
15
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#???--   
16

    
17
##################################################################################################
18

    
19
download_calculate_MODIS_LST_climatology <-function(j,list_param){
20
  
21
  ###Function to download and calculate LST climatology from MODIS tiles
22
  #AUTHOR: Benoit Parmentier                                                                       
23
  #DATE: 05/30/2013                                                                                 
24
  #PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
25
  
26
  #1) var 
27
  #2) list_tiles_modis 
28
  #3) start_year 
29
  #4) end_year 
30
  #5) hdfdir: destination file where hdf files are stored locally after download.
31
  #6) grass_setting_script 
32
  #7) modis_download_script 
33
  #8) clim_script
34
  #9) download  if 1 then download modis files before calculating averages
35
  #10) clim_calc
36
  #11) out_suffix_modis
37
  
38
  ###Loading R library and packages   
39
  #...
40
   
41
  #PARSING OUT input parameters
42
  
43
  var <- list_param$var
44
  list_tiles_modis <- list_param$list_tiles_modis
45
  start_year <- list_param$start_year 
46
  end_year <- list_param$end_year 
47
  hdfdir <- list_param$hdfdir #destination file where hdf files are stored locally after download.
48
  grass_setting_script <- list_param$grass_setting_script
49
  modis_download_script <- list_param$modis_download_script
50
  clim_script <- list_param$clim_script
51
  download <- list_param$download #"1"  # if 1 then download modis files before calculating averages
52
  clim_calc <- list_param$clim_calc #"1"  # if 1 then download modis files before calculating averages
53
  out_suffix_modis <- list_param$out_suffix_modis
54
  
55
  #j to be used later...parallelization??
56
  
57
  #Additional parameters
58
  list_tiles_modis <- unlist(strsplit(list_tiles_modis,","))  # transform string into separate element in char vector
59
  list_tiles_modis <- list_tiles_modis[j]
60
  end_month= "12"
61
  start_month= "1"
62
  
63
  ### Create output directory if it does not exist
64
  if (!file.exists(hdfdir)){
65
    dir.create(hdfdir)
66
    #} else{
67
    #  out_path <-paste(out_path..)
68
  }
69
  
70
  ### CALCULATE NIGHT OR DAY CLIMATOLOGY?
71
  
72
  
73
  if (var=="TMIN") {
74
    night="1" # if 1 then produce night climatology
75
  } else{
76
    night="0" # if 0 then produce day climatology
77
  }  
78
  
79
  ## Prepare list of arguments for python script
80
  
81
  list_param_python_script <- list(list_tiles_modis,start_year,end_year,start_month,end_month,hdfdir,
82
                                   night,download,out_suffix_modis)
83
  names(list_param_python_script)<-c("list_tiles_modis","start_year","end_year","start_month","end_month","hdfdir",
84
                                     "night","download","out_suffix_modis")
85
  list_param_python_script_str <- paste(unlist(list_param_python_script), collapse=" ")
86
  
87
  #command_str<-"python /home/parmentier/Data/test5.py h01v05,h02v05 2001 2005 12 1 /benoit 1 test_out 1"
88
  #paste(toString(list_param_python_script),collapse=",",sep=" ")
89
  command_download_str <- paste("python",modis_download_script, list_param_python_script_str,sep=" ")
90
  command_clim_str <- paste("python",clim_script, list_param_python_script_str,sep=" ") #preparing shell command
91
  
92
  ##Downownload files if necessary...
93
  if (download==1){
94
    system(command_download_str)
95
  }
96
  
97
  if (clim_calc==1){
98
    ##Now run climatology: Can add other climatology scripts or method to produce climatology later on...
99
    source(grass_setting_script) #Setting to access GRASS (current setting valid for Atlas at nceas)  
100
    system(command_clim_str)
101
  }
102
  
103
#   ## return list of files???, may be modified later to return clim list and list of downloaded files + missing.
104
#   if (clim_calc==1){
105
#     list_output_obj <-list(command_download_str,command_clim_str)
106
#     names(list_output_obj) <- c("command_download_str", "command_clim_str")
107
#   } else{
108
#     list_output_obj <-list(command_download_str)
109
#     names(list_output_obj) <- c("command_download_str")
110
#   }
111
  #Add function to list files created or modify in python...
112
  
113
  list_output_obj <-list(command_download_str,command_clim_str)
114
  names(list_output_obj) <- c("command_download_str", "command_clim_str")
115
  return(list_output_obj)
116
}
117

    
118
## Run function:
119
#list_tiles_modis <- c("h19v12,h20v12,h20v11,h21v11,h19v10,h20v10,h21v10") #Africa, S. Africa and more
120

    
121
#list_tiles_modis <- c("h09v02,h10v02,h11v02,h12v02,h13v02,h14v02,h15v02") #tiles for Northenr America, Northern US...
122
#list_tiles_modis <- c("h10v04,h11v04,h12v04,h13v04,h14v04,h07v06") #tiles for Northenr America, Northern US...
123
list_tiles_modis <- c("h12v04,h13v04,h14v04,h07v06") #tiles for Northenr America, Northern US...
124
#list_tiles_modis <- c("h11v08,h11v07,h12v07,h12v08,h10v07,h10v08") #tile for Venezuela and surrounding area
125
#list_tiles_modis <- c("h08v04,h09v04") #tiles for Oregon #defined above...
126
#list_tiles_modis <- c("h09v04,h09v04") #tiles for Oregon #defined above...
127

    
128
#list_tiles_modis <- c("h09v04,h09v04") #tiles for Oregon #defined above...
129
#list_tiles_modis <- c("h09v08,h09v07,h08v07,h0706,h08v06,h09v06,h10v06,h08v05,h09v05,h10v05,h11v05,h12v05") #tiles for Central America and Mexico Southern US...
130
#list_tiles_modis <- c("h09v09,h10v09,h11v09,h12v09,h13v09,h14v09")
131
#list_tiles_modis <-c("h30v10,h31v10,h32v10,h30v11,h31v11") #list("Queensland")
132

    
133
script_path<-"/home/parmentier/Data/IPLANT_project/env_layers_scripts/"
134
modis_download_script <- file.path(script_path,"modis_download_05142013.py") # LST modis download python script
135
clim_script <- file.path(script_path,"climatology_06092013.py") # LST climatology python script
136
grass_setting_script <- file.path(script_path,"grass-setup.R")
137
var="TMAX"
138
start_year = "2001"
139
end_year = "2010"
140
#end_year = "2002"
141

    
142
#path on Jupiter
143

    
144
hdfdir =  '/data/project/layers/commons/modis/MOD11A1_tiles' #destination file where hdf files are stored locally after download.
145
#hdfdir =  '/home/parmentier/Data/IPLANT_project/MOD11A1_tiles'
146
download=0
147
clim_calc=1
148
out_suffix_modis="_05242013"
149

    
150
list_param_download_clim_LST_script <- list(list_tiles_modis,start_year,end_year,hdfdir,
151
                                var,grass_setting_script,modis_download_script, clim_script,
152
                                download,clim_calc,out_suffix_modis)
153
names(list_param_download_clim_LST_script)<-c("list_tiles_modis","start_year","end_year","hdfdir",
154
                                  "var","grass_setting_script","modis_download_script","clim_script",
155
                                  "download","clim_calc","out_suffix_modis")
156
#debug(download_calculate_MODIS_LST_climatology)
157
#clim_production_obj <-mclapply(1:2, list_param=list_param_download_clim_LST_script, download_calculate_MODIS_LST_climatology,mc.preschedule=FALSE,mc.cores = 2) #This is the end bracket from mclapply(...) statement
158
no_tiles <- length(unlist(strsplit(list_tiles_modis,",")))  # transform string into separate element in char vector
159
clim_production_obj <-lapply(1:no_tiles, list_param=list_param_download_clim_LST_script, download_calculate_MODIS_LST_climatology) #,mc.preschedule=FALSE,mc.cores = 2) #This is the end bracket from mclapply(...) statement
160

    
161
#download_calculate_MODIS_LST_climatology(1,list_param_download_clim_LST_script)
162
#source(file.path(script_path,"covariates_production_temperatures_05312013.R"))
163
### END OF SCRIPT
(29-29/53)