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
|
# based on modified earlier python scripts from J. Regetz
|
11
|
#DATE: 05/29/2013
|
12
|
|
13
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#???--
|
14
|
|
15
|
##################################################################################################
|
16
|
|
17
|
download_calculate_MODIS_LST_climatology <-function(j,list_param){
|
18
|
|
19
|
###Function to download and calculate LST climatology from MODIS tiles
|
20
|
#AUTHOR: Benoit Parmentier
|
21
|
#DATE: 05/29/2013
|
22
|
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--
|
23
|
|
24
|
#1) var
|
25
|
#2) list_tiles_modis
|
26
|
#3) start_year
|
27
|
#4) end_year
|
28
|
#5) hdfdir: destination file where hdf files are stored locally after download.
|
29
|
#6) grass_setting_script
|
30
|
#7) modis_download_script
|
31
|
#8) clim_script
|
32
|
#9) download if 1 then download modis files before calculating averages
|
33
|
#10) clim_calc
|
34
|
#11) out_suffix_modis
|
35
|
|
36
|
###Loading R library and packages
|
37
|
#...
|
38
|
|
39
|
#PARSING OUT input parameters
|
40
|
|
41
|
var <- list_param$var
|
42
|
list_tiles_modis <- list_param$list_tiles_modis
|
43
|
start_year <- list_param$start_year
|
44
|
end_year <- list_param$end_year
|
45
|
hdfdir <- list_param$hdfdir #destination file where hdf files are stored locally after download.
|
46
|
grass_setting_script <- list_param$grass_setting_script
|
47
|
modis_download_script <- list_param$modis_download_script
|
48
|
clim_script <- list_param$clim_script
|
49
|
download <- list_param$download #"1" # if 1 then download modis files before calculating averages
|
50
|
clim_calc <- list_param$clim_calc #"1" # if 1 then download modis files before calculating averages
|
51
|
out_suffix_modis <- list_param$out_suffix_modis
|
52
|
|
53
|
#j to be used later...parallelization??
|
54
|
|
55
|
#Additional parameters
|
56
|
#list_tiles_modis <- unlist(strsplit(list_tiles_modis,",")) # transform string into separate element in char vector
|
57
|
#list_tiles_modis <- list_tiles_modis[j]
|
58
|
end_month= "12"
|
59
|
start_month= "1"
|
60
|
|
61
|
### Create output directory if it does not exist
|
62
|
if (!file.exists(hdfdir)){
|
63
|
dir.create(hdfdir)
|
64
|
#} else{
|
65
|
# out_path <-paste(out_path..)
|
66
|
}
|
67
|
|
68
|
### CALCULATE NIGHT OR DAY CLIMATOLOGY?
|
69
|
|
70
|
|
71
|
if (var=="TMIN") {
|
72
|
night="1" # if 1 then produce night climatology
|
73
|
} else{
|
74
|
night="0" # if 0 then produce day climatology
|
75
|
}
|
76
|
|
77
|
## Prepare list of arguments for python script
|
78
|
|
79
|
list_param_python_script <- list(list_tiles_modis,start_year,end_year,start_month,end_month,hdfdir,
|
80
|
night,download,out_suffix_modis)
|
81
|
names(list_param_python_script)<-c("list_tiles_modis","start_year","end_year","start_month","end_month","hdfdir",
|
82
|
"night","download","out_suffix_modis")
|
83
|
list_param_python_script_str <- paste(unlist(list_param_python_script), collapse=" ")
|
84
|
|
85
|
#command_str<-"python /home/parmentier/Data/test5.py h01v05,h02v05 2001 2005 12 1 /benoit 1 test_out 1"
|
86
|
#paste(toString(list_param_python_script),collapse=",",sep=" ")
|
87
|
|
88
|
##Downownload files if necessary...
|
89
|
if (download==1){
|
90
|
command_download_str <- paste("python",modis_download_script, list_param_python_script_str,sep=" ")
|
91
|
#command_str <- paste("python","/home/parmentier/Data/benoit_test/test5.py", list_param_python_script_str,sep=" ")
|
92
|
system(command_download_str)
|
93
|
}
|
94
|
|
95
|
if (clim_calc==1){
|
96
|
##Now run climatology: Can add other climatology scripts or method to produce climatology later on...
|
97
|
source(grass_setting_script) #Setting to access GRASS (current setting valid for Atlas at nceas)
|
98
|
command_clim_str <- paste("python",clim_script, list_param_python_script_str,sep=" ") #preparing shell command
|
99
|
system(command_clim_str)
|
100
|
#/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_05132013.py
|
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
|
return(list_output_obj)
|
114
|
}
|
115
|
|
116
|
## Run function:
|
117
|
|
118
|
#list_tiles_modis <- c("h10v04,h11v04,h12v04,h13v04,h14v04,h07v06") #tiles for Northenr America, Northern US...
|
119
|
list_tiles_modis <- c("h12v04,h13v04,h14v04,h07v06") #tiles for Northenr America, Northern US...
|
120
|
#list_tiles_modis <- c("h11v08,h11v07,h12v07,h12v08,h10v07,h10v08") #tile for Venezuela and surrounding area
|
121
|
#list_tiles_modis <- c("h08v04,h09v04") #tiles for Oregon #defined above...
|
122
|
|
123
|
#list_tiles_modis <- c("h09v04,h09v04") #tiles for Oregon #defined above...
|
124
|
#list_tiles_modis <- c("h09v08,h09v07,h08v07,h0706,h08v06,h09v06,h10v06,h08v05,h09v05,h10v05,h11v05,h12v05") #tiles for Central America and Mexico Southern US...
|
125
|
#list_tiles_modis <- c("h09v09,h10v09,h11v09,h12v09,h13v09,h14v09")
|
126
|
#list_tiles_modis <-c("h30v10,h31v10,h32v10,h30v11,h31v11") #list("Queensland")
|
127
|
|
128
|
script_path<-"/home/parmentier/Data/IPLANT_project/env_layers_scripts/"
|
129
|
modis_download_script <- file.path(script_path,"modis_download_05142013.py") # LST modis download python script
|
130
|
clim_script <- file.path(script_path,"climatology_05142013.py") # LST climatology python script
|
131
|
grass_setting_script <- file.path(script_path,"grass-setup.R")
|
132
|
var="TMAX"
|
133
|
start_year = "2001"
|
134
|
end_year = "2010"
|
135
|
#path on Jupiter
|
136
|
|
137
|
hdfdir = '/data/project/layers/commons/modis/MOD11A1_tiles' #destination file where hdf files are stored locally after download.
|
138
|
#hdfdir = '/home/parmentier/Data/IPLANT_project/MOD11A1_tiles'
|
139
|
download=1
|
140
|
clim_calc=0
|
141
|
out_suffix_modis="_05292013"
|
142
|
|
143
|
list_param_download_clim_LST_script <- list(list_tiles_modis,start_year,end_year,hdfdir,
|
144
|
var,grass_setting_script,modis_download_script, clim_script,
|
145
|
download,clim_calc,out_suffix_modis)
|
146
|
names(list_param_download_clim_LST_script)<-c("list_tiles_modis","start_year","end_year","hdfdir",
|
147
|
"var","grass_setting_script","modis_download_script","clim_script",
|
148
|
"download","clim_calc","out_suffix_modis")
|
149
|
#debug(download_calculate_MODIS_LST_climatology)
|
150
|
#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
|
151
|
|
152
|
download_calculate_MODIS_LST_climatology(1,list_param_download_clim_LST_script)
|
153
|
|
154
|
### END OF SCRIPT
|