Project

General

Profile

« Previous | Next » 

Revision 82b0f65e

Added by Benoit Parmentier about 12 years ago

LST climatology initial commit script using IDRISI API and python

View differences:

climate/research/oregon/modis-lst/Average_QC_MOD11A1_Oregon_case.py
1
#This script was written by Benoit Parmentier on March 27, 2012.
2
#This script computes the daily average for MOD11A1 data. The code takes into account the QC and computes the number of valid observation.
3
#Note that this code will only work with a specific format: Oregon_2003_054_MOD11A1_Reprojected_LST_Day_1km.rst
4
#This code should be reviewed to use the datetime package in python. As of now, the code relies on the IDRISI software's API.
5

  
6
import os, glob, sys
7
from osgeo import gdal, gdalconst
8
import win32com.client
9
import shutil
10
from datetime import date
11
import numpy
12

  
13
#variables, input parameters
14

  
15
wd1='H:\Data\IPLANT_project\python_working_dir3\\'  #output folder
16
wd2='H:\Data\IPLANT_project\outrst_data\\'         #input folder containg the QC and LST files
17

  
18
min_range =7500  #This value can be changed, it correspond to the valid range in LST MOD11A1
19
max_range =65535
20
background =0
21
min_nb_obs =1
22
nb_years =10
23
year_start=2001
24
bi_sextile_years='2004,2008'
25

  
26
t1 = 2  #minimum threshold to consider a pixel as good quality in the LST QC flag level1
27
t2 = 3  #minimum threshold to consider a pixel as good quality in the LST QC flag level2
28

  
29
#START OF THE SCRIPT
30
api = win32com.client.Dispatch('idrisi32.IdrisiAPIServer')    # api is the handle for IDRISI api
31
##wd = api.GetWorkingDir();                                      #Is variable containing the path of the working directory
32
##wd = 'H:\Benoit_Backup\Paper3_STA_07202011\\'
33

  
34
##PART applies the QC mask to the 
35
#Idrisi_filename2='Oregon_2004_366_MOD11A1_Reprojected_QC_Day'
36
#MODIS QC extraction
37

  
38
listfiles_wd1 = glob.glob(wd2+'*'+'Reprojected'+'*LST*.rst') #list the all the LST files of interest
39
listfiles_wd3 = glob.glob(wd2+'*'+'Reprojected'+'*QC*.rst') #list the all the LST files of interest
40
nb_files = len(listfiles_wd1)
41
loop_number3=0
42
i=0
43
##
44
##for i in range(1,nb_files+1):
45
##            
46
##    loop_number3 = loop_number3 + 1
47
##    filename1= listfiles_wd1[i-1]
48
##    idrisi_filename1= filename1[filename1.rfind('\\')+1:-4] #Remove the path and the extension
49
##
50
##    filename3= listfiles_wd3[i-1]
51
##    idrisi_filename3= filename3[filename3.rfind('\\')+1:-4] #Remove the path and the extension
52
##
53
##    #Conversion of MODISQC into quality information: level 1
54
##    parameters ='2'+'*'+wd2+idrisi_filename3+'.rst'+'*'+wd1+idrisi_filename3+'_L1'+'.rst'+'*1'
55
##    module = 'MODISQC'
56
##    print('Running ' + module + ' module with parameters ' + parameters)
57
##    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
58
##    if not success:
59
##           print('Error running ' + module + '!')
60
##           
61
##    #Conversion of MODISQC into quality information: level 2
62
##    parameters ='2'+'*'+wd2+idrisi_filename3+'.rst'+'*'+wd1+idrisi_filename3+'_L2'+'.rst'+'*2'
63
##    module = 'MODISQC'
64
##    print('Running ' + module + ' module with parameters ' + parameters)
65
##    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
66
##    if not success:
67
##           print('Error running ' + module + '!')
68
##
69
##    #OVERLAY to eliminate bad pixels: this is using QC level 1
70
##
71
##    #RECLASS  I*C:\Data\Benoit\Clark_University\Python\dissertationunburnt_pixels_selection\OVERLAY_ID_83_399_144_TEST_BURNT_83_144_399_allocating_bool_Class_83overlay_random.rst*C:\Data\Benoit\Clark_University\Python\dissertationunburnt_pixels_selection\selection_83.rst*3*C:\Data\Benoit\Clark_University\Python\dissertationunburnt_pixels_selection\idrtemp.rcl*1       
72
##    parameters = 'I'+'*'+wd1+idrisi_filename3+'_L1'+'.rst'+'*'+wd1+idrisi_filename3+'_L1'+'_rec_'+str(t1)+'.rst'+'*2'+'*0*0*'+str(t1)+'*1*'+str(t1)+'*5'+'*-9999*1'  #option 2 is an avl file with option 1 , count in cells in textfile '*.avl'
73
##    module = 'reclass'
74
##    print('Running ' + module + ' module with parameters ' + parameters)
75
##    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
76
##    if not success:
77
##        print('Error running ' + module + '!')
78
##
79
##    #OVERLAY to eliminate bad pixels: this is using QC level 2
80
##    #RECLASS  I*C:\Data\Benoit\Clark_University\Python\dissertationunburnt_pixels_selection\OVERLAY_ID_83_399_144_TEST_BURNT_83_144_399_allocating_bool_Class_83overlay_random.rst*C:\Data\Benoit\Clark_University\Python\dissertationunburnt_pixels_selection\selection_83.rst*3*C:\Data\Benoit\Clark_University\Python\dissertationunburnt_pixels_selection\idrtemp.rcl*1       
81
##    parameters = 'I'+'*'+wd1+idrisi_filename3+'_L2'+'.rst'+'*'+wd1+idrisi_filename3+'_L2'+'_rec_'+str(t2)+'.rst'+'*2'+'*0*0*'+str(t2)+'*1*'+str(t2)+'*5'+'*-9999*1'  #option 2 is an avl file with option 1 , count in cells in textfile '*.avl'
82
##    module = 'reclass'
83
##    print('Running ' + module + ' module with parameters ' + parameters)
84
##    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
85
##    if not success:
86
##        print('Error running ' + module + '!')
87
##            
88
##    #"This file was created by the MODISQC module with the command line:",2*H:\Data\IPLANT_project\outrst_data\Oregon_2004_366_MOD11A1_Reprojected_QC_Day.rst*H:\Data\IPLANT_project\IPLANT_idrisi_project_03062012\test2.rst*2
89
##    ###OVERLAY MODULE###    
90
##    parameters = '3*'+wd1+idrisi_filename3+'_L1'+'_rec_'+str(t1)+'.rst'+'*'+wd1+idrisi_filename3+'_L2'+'_rec_'+str(t2)+'.rst'+'*'+wd1+idrisi_filename3+'_mask_'+str(t1)+'_'+str(t2)+'.rst'
91
##    module = 'overlay'
92
##    print('Running ' + module + ' module with parameters ' + parameters)
93
##    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
94
##    if not success:
95
##        print('Error running ' + module + '!')
96
##
97
##    #This file was created by the MODISQC module with the command line:",2*H:\Data\IPLANT_project\outrst_data\Oregon_2004_366_MOD11A1_Reprojected_QC_Day.rst*H:\Data\IPLANT_project\IPLANT_idrisi_project_03062012\test2.rst*2
98
##    ###OVERLAY MODULE###    
99
##    parameters = '3*'+wd1+idrisi_filename3+'_mask_'+str(t1)+'_'+str(t2)+'.rst'+'*'+wd2+idrisi_filename1+'.rst'+'*'+wd1+idrisi_filename1+'_masked_'+str(t1)+'_'+str(t2)+'.rst'
100
##    module = 'overlay'
101
##    print('Running ' + module + ' module with parameters ' + parameters)
102
##    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
103
##    if not success:
104
##        print('Error running ' + module + '!')
105
##
106
##    ###Reclass valid values ###    
107
##    parameters = 'I'+'*'+wd1+idrisi_filename1+'_masked_'+str(t1)+'_'+str(t2)+'.rst'+'*'+wd1+idrisi_filename1+'_masked_bool_'+str(t1)+'_'+str(t2)+'.rst'+'*2'+'*0*0*'+str(min_range)+'*1*'+str(min_range)+'*'+str(max_range)+'*-9999*1'  #option 2 is an avl file with option 1 , count in cells in textfile '*.avl'
108
##    module = 'reclass'
109
##    print('Running ' + module + ' module with parameters ' + parameters)
110
##    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
111
##    if not success:
112
##        print('Error running ' + module + '!')
113
##                     
114
##
115
#PART 2 Calculating average and number of valid observations
116

  
117
listfiles_wd2 = glob.glob(wd1+'*'+'Reprojected'+'*LST*'+'_masked_'+str(t1)+'_'+str(t2)+'.rst') #list the all the LST files of interest
118
#To find the number of valid points used in the average calculation, one must reclass the pixel values in IDRISI, this is inefficient and
119
#can be improved a lot later on.
120

  
121
f4 = open( wd1+'output_list'+'.rgf','w')
122
line0 = str(len(listfiles_wd2))+'\n'
123
f4.write(line0)
124
for line in listfiles_wd2:
125
    line = line[line.rfind('\\')+1:]
126
    #line = line[line.rfind('\\')+1:-4]
127
    line=line.replace('.rst','\n')
128
    f4.write(line) 
129
f4.close()
130

  
131
#EXTRACTING THE DOY from the files names: Note that this code will only work with a specific format: Oregon_2003_054_MOD11A1_Reprojected_LST_Day_1km.rst
132
day=listfiles_wd2[:]
133
i=0;
134
for filename in listfiles_wd2:
135
    #filename = listfiles_wd2[i] #list the files of interest
136
    day[i] = filename[filename.rfind('_MOD')-3:filename.rfind('_MOD')]
137
    i=i+1
138

  
139
day_no =list(set(day))    #this is the unique set of day in the list. It must be 366 if there are bisextiles years
140
day_no.sort()             
141

  
142

  
143
table_data = numpy.ones((len(day_no),2)) #declaring an array of size stop+1 and initialized with number 1
144

  
145
i=0
146
#Grouping files per day from 1 to 366 (or 
147
for day in day_no:
148
    i=i+1
149
    #listfiles_wd3=glob.glob(wd1+'*20**'+day+'*Reprojected'+'*LST*'_masked_'+str(t1)+'_'+str(t2)+'.rst') #list the files of interest: finding the day of each file
150
    listfiles_wd3=glob.glob(wd1+'*20**'+day+'*Reprojected'+'*LST*'+'_masked_'+str(t1)+'_'+str(t2)+'.rst')
151
    table_data[i-1,0]= day
152
    table_data[i-1,1]= len(listfiles_wd3)    #number of available data for a specific date
153
    f4 = open( wd1+'list_day_'+day+'.rgf','w')  #This is the output rgf masked
154
    line0 = str(len(listfiles_wd3))+'\n' #This line contains the number of files per day
155
    f4.write(line0)
156
    for line in listfiles_wd3:
157
        line = line[line.rfind('\\')+1:]
158
        #line = line[line.rfind('\\')+1:-4]
159
        line=line.replace('.rst','\n')
160
        f4.write(line) 
161
    f4.close()
162

  
163
    #listfiles_wd3=glob.glob(wd1+'*20**'+day+'*Reprojected'+'*LST*'_masked_'+str(t1)+'_'+str(t2)+'.rst') #list the files of interest: finding the day of each file
164
    listfiles_wd4=glob.glob(wd1+'*20**'+day+'*Reprojected'+'*LST*'+'_masked_bool_'+str(t1)+'_'+str(t2)+'.rst')
165
    #f4 = open( wd2+'ps_'+inputf3+'_'+str(nb_files)+'.rgf','w')
166
    f5 = open( wd1+'list_day_valid_obs_'+day+'.rgf','w')  #This is the output rgf masked
167
    line0 = str(len(listfiles_wd4))+'\n'
168
    f5.write(line0)
169
    for line in listfiles_wd4:
170
        line = line[line.rfind('\\')+1:]
171
        #line = line[line.rfind('\\')+1:-4]
172
        line=line.replace('.rst','\n')
173
        f5.write(line) 
174
    f5.close()    
175
   
176
    rgf_name = wd1+'list_day_'+str(day)+'.rgf'
177
    outfilename =wd1+'mean_day_'+day
178
   
179
    # Calulating the average with TSTAT     
180
    parameters ='1'+'*'+rgf_name+'*'+outfilename+'*'+str(min_range)+'*'+str(max_range)+'*'+str(background)+'*'+str(min_nb_obs)
181
    module = 'TStats'
182
    print('Running ' + module + ' module with parameters ' + parameters)
183
    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
184
    if not success:
185
           print('Error running ' + module + '!')
186

  
187
    rgf_name2 = wd1+'list_day_valid_obs_'+str(day)+'.rgf'
188
    outfilename2 =wd1+'mean_day_valid_obs_'+day
189
    
190
    # Calulating the number of valid observation with TSTAT     
191
    parameters ='100000'+'*'+rgf_name2+'*'+outfilename2+'*'+str(0)+'*'+str(1)+'*'+str(background)+'*'+str(min_nb_obs)
192
    module = 'TStats'
193
    print('Running ' + module + ' module with parameters ' + parameters)
194
    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
195
    if not success:
196
           print('Error running ' + module + '!')           
197

  
198
    # Rescaling the values with scalar
199
    #H:\Data\IPLANT_project\python_working_dir\Oregon_2008_366_MOD11A1_Reprojected_LST_Day_1km_masked_1_3.rst*H:\Data\IPLANT_project\IPLANT_idrisi_project_03062012\test.rst*3*0.02
200
    parameters = wd1+'mean_day_'+day+'_Mean.rst'+'*'+wd1+'mean_day'+day+'_rescaled.rst'+'*3*'+'0.02'
201
    module = 'scalar'
202
    print('Running ' + module + ' module with parameters ' + parameters)
203
    success = api.RunModule(module, parameters, True, '', '', '', '', True)  #the output of api.runmodule is a value '1' if it is sucessful and '0' if not.
204
    if not success:
205
           print('Error running ' + module + '!')
206

  
207
outfile1=wd1+'table_data.txt'
208
numpy.savetxt(outfile1, table_data, fmt='%-7.2f')    
209

  
210
#REPORT ON THE NUMBER OF MISSING VALUES FOR EACH DATE!!! and each pixel??        
211
#GDALcommand='gdal_translate F:\Data\Paper2_STA_03182012\dem\dem.bil F:\Data\Paper2_STA_03182012\dem_output2.rst -of RST'
212
#output = os.popen(GDALcommand).read()
213
        
214
#print('END OF SCRIPT')
215
### ==================================== #
216
### Load GDAL Drivers:
217
##dst_frmts    = 'rst' #data format driver for gdal
218
##dst_driver = gdal.GetDriverByName( dst_frmts )
219
##
220
##if dst_driver == None:
221
##    Usage()
222
##
223
###src_ds = gdal.Open( inputf1 )
224
##src_ds = gdal.Open( 'G:\\Benoit_Backup\\STA_PAPER2_02262011\\test5_ID.rst' )
225
##src_ds = gdal.Open('G:\Benoit_Backup\STA_PAPER2_02262011\GAP_ALB_07182011_filled_t3\\GAP_ALB_07182011_filled_t3_GAP_ALBEDO_16D_MASKED2_FILL4_6_MEAN.rst')
226
##src_rb = src_ds.GetRasterBand( 1 );
227
##
228
##x_size = src_rb.XSize  #nb_cols
229
##y_size = src_rb.YSize  #nb_rows
230
##
231
##src_rb.ReadAsArray(0,6,None,None,24,None)
232

  
233
# today = datetime.datetime.now()
234
# today.strftime('%j')  Returns the day of the year in a string.
235
# '083'  

Also available in: Unified diff