Project

General

Profile

« Previous | Next » 

Revision b4dae80c

Added by Benoit Parmentier over 11 years ago

LST processing R function, initial commit, calls to python scripts

View differences:

climate/research/oregon/interpolation/download_and_produce_MODIS_LST_climatology.R
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/16/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/16/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
  end_month= "12"
57
  start_month= "1"
58
  
59
  ### Create output directory if it does not exist
60
  if (!file.exists(hdfdir)){
61
    dir.create(hdfdir)
62
    #} else{
63
    #  out_path <-paste(out_path..)
64
  }
65
  
66
  ### CALCULATE NIGHT OR DAY CLIMATOLOGY?
67
  
68
  if (var=="TMIN") {
69
    night="1" # if 1 then produce night climatology
70
  } else{
71
    night="0" # if 0 then produce day climatology
72
  }  
73
  
74
  ## Prepare list of arguments for python script
75
  
76
  list_param_python_script <- list(list_tiles_modis,start_year,end_year,start_month,end_month,hdfdir,
77
                                   night,download,out_suffix_modis)
78
  names(list_param_python_script)<-c("list_tiles_modis","start_year","end_year","start_month","end_month","hdfdir",
79
                                     "night","download","out_suffix_modis")
80
  list_param_python_script_str <- paste(unlist(list_param_python_script), collapse=" ")
81
  
82
  #command_str<-"python /home/parmentier/Data/test5.py h01v05,h02v05 2001 2005 12 1 /benoit 1 test_out 1"
83
  #paste(toString(list_param_python_script),collapse=",",sep=" ")
84
  
85
  ##Downownload files if necessary...
86
  if (download==1){
87
    command_download_str <- paste("python",modis_download_script, list_param_python_script_str,sep=" ")
88
    #command_str <- paste("python","/home/parmentier/Data/benoit_test/test5.py", list_param_python_script_str,sep=" ")
89
    system(command_download_str)
90
  }
91
  
92
  if (clim_calc==1){
93
    ##Now run climatology: Can add other climatology scripts or method to produce climatology later on...
94
    source(grass_setting_script) #Setting to access GRASS (current setting valid for Atlas at nceas)
95
    command_clim_str <- paste("python",clim_script, list_param_python_script_str,sep=" ")
96
    system(command_clim_str)
97
    #/home/parmentier/Data/IPLANT_project/Venezuela_interpolation/Venezuela_01142013/climatology_05132013.py
98
  }
99
  
100
  ## return list of files???, may be modified later to return clim list and list of downloaded files + missing.
101
  list_output_obj <-list(command_download_str,command_clim_str)
102
  names(list_output_obj) <- c("command_download_str", "command_clim_str")
103
  
104
  return(list_output_obj)
105
}
106

  
107
## Run function:
108

  
109
#list_tiles_modis <- c("h11v08,h11v07,h12v07,h12v08,h10v07,h10v08") #tile for Venezuela and surrounding area
110
#list_tiles_modis <- c("h08v04,h09v04") #tiles for Oregon #defined above...
111
#list_tiles_modis <- c("h09v09,h10v09,h11v09,h12v09,h13v09,h14v09")
112
#list_tiles_modis <- c("h09v09,h10v09,h11v09,h12v09,h13v09,h14v09,
113
#                       h10v10,h11v10,h12v11,h13v12,h14v10")
114
#list_tiles_modis <- c("h08v04")
115
script_path<-"/home/parmentier/Data/IPLANT_project/env_layers_scripts/"
116
modis_download_script <- file.path(script_path,"modis_download_05142013.py") # LST modis download python script
117
clim_script <- file.path(script_path,"climatology_05132013.py") # LST climatology python script
118
grass_setting_script <- file.path(script_path,"grass-setup.R")
119
#source(file.path(script_path,"download_and_produce_MODIS_LST_climatology_05162013.R"))
120

  
121
var="TMIN"
122
list_tiles_modis <- c("h08v04")
123
start_year = "2001"
124
#end_year = "2010"
125
end_year = "2002" #for testing (year included?)
126
#end_month= "12"
127
#start_month= "1"
128
hdfdir =  '/home/layers/commons/modis/MOD11A1_tiles' #destination file where hdf files are stored locally after download.
129
#hdfdir =  '/home/parmentier/Data/IPLANT_project/MOD11A1_tiles'
130
download=1
131
clim_calc=0
132
out_suffix_modis="_05132013"
133
#end_month= "12"
134
#start_month= "1"
135

  
136
list_param_download_clim_LST_script <- list(list_tiles_modis,start_year,end_year,hdfdir,
137
                                 var,grass_setting_script,modis_download_script, clim_script,
138
                                 night,download,clim_calc,out_suffix_modis)
139
names(list_param_download_clim_LST_script)<-c("list_tiles_modis","start_year","end_year","hdfdir",
140
                                   "var","grass_setting_script","modis_download_script","clim_script",
141
                                   "night","download","clim_calc","out_suffix_modis")
142
#debug(download_calculate_MODIS_LST_climatology)
143

  
144
download_calculate_MODIS_LST_climatology(1,list_param_download_clim_LST_script)
145

  
146

  

Also available in: Unified diff