/
NDVI_June_5.py
213 lines (175 loc) · 6.69 KB
/
NDVI_June_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
from binit import fastbin
import fasthist as fh
from matplotlib.colors import Normalize
from matplotlib import cm
from plot_rads import make_dir
import matplotlib.pyplot as plt
import pyhdf.SD
from mpl_toolkits.basemap import Basemap
import read_mod13
import os
import scipy.io as si
# #List of hdf files 2002
# modis_file_lst = ['MODIS_data_2/June10_2002/MOD13A1.A2002161.h09v04.005.2008241032814.hdf',
# 'MODIS_data_2/June10_2002/MOD13A1.A2002161.h10v04.005.2008241023056.hdf',
# 'MODIS_data_2/June10_2002/MOD13A1.A2002161.h11v03.005.2008241021235.hdf',
# 'MODIS_data_2/June10_2002/MOD13A1.A2002161.h11v04.005.2008241020511.hdf',
# 'MODIS_data_2/June10_2002/MOD13A1.A2002161.h12v03.005.2008241025942.hdf',
# 'MODIS_data_2/June10_2002/MOD13A1.A2002161.h13v03.005.2008241062259.hdf',
# 'MODIS_data_2/June10_2002/MOD13A1.A2002161.h10v03.005.2008241065032.hdf']
#List of hdf files 2009
modis_file_lst = ['MODIS_data_2/June10_2009/MOD13A1.A2009161.h10v03.005.2009180094703.hdf',
'MODIS_data_2/June10_2009/MOD13A1.A2009161.h10v04.005.2009181023650.hdf',
'MODIS_data_2/June10_2009/MOD13A1.A2009161.h11v03.005.2009181002829.hdf',
'MODIS_data_2/June10_2009/MOD13A1.A2009161.h11v04.005.2009180211016.hdf',
'MODIS_data_2/June10_2009/MOD13A1.A2009161.h12v03.005.2009182053254.hdf',
'MODIS_data_2/June10_2009/MOD13A1.A2009161.h13v03.005.2009181170826.hdf']
#check to see if lat/lon grids exist and if not, create them
if os.path.exists('MODIS_data_2/June10_2009/lat_lon_grids.mat'):
print 'lat lon grid file exists'
data = si.loadmat('MODIS_data_2/June10_2009/lat_lon_grids.mat')
lat_grids = data['lat_grids'].astype(np.float32)
lon_grids = data['lon_grids'].astype(np.float32)
else:
print ' No lat lon grid file exists, creating one'
#loop to get lat/lon grids for each file
lat_grids = []
lon_grids = []
for modis_file_name in modis_file_lst:
data = read_mod13.lat_lon_data(modis_file_name)
lat_grid = data['lat_grid'].astype(np.float32)
lon_grid = data['lon_grid'].astype(np.float32)
lat_grids.append(lat_grid)
lon_grids.append(lon_grid)
savedict = {'lat_grids': lat_grids, 'lon_grids': lon_grids}
si.savemat('MODIS_data_2/June10_2009/lat_lon_grids.mat',savedict)
#print out corners of grids
for i in range(0,len(lat_grids)):
lat_grid = lat_grids[i]
lon_grid = lon_grids[i]
#print corners of grid
print (lon_grid[0,0], lat_grid[0,0])
print (lon_grid[2399,0], lat_grid[2399,0])
print (lon_grid[0,2399], lat_grid[0,2399])
print (lon_grid[2399,2399], lat_grid[2399,2399])
print ' '
#Set up NDVI grids
NDVI_grids = []
lat_centers_lst = []
lon_centers_lst = []
figcount = 1
for j in range(0,len(modis_file_lst)):
#for j in range(0,1):
print 'Loop Index', j
lat_grid = lat_grids[j]
lon_grid = lon_grids[j]
modis_file_name = modis_file_lst[j]
numlatbins=1000
numlonbins=1000
#set limits for fastbin to grid corners
north = lat_grid[0,0]
south = lat_grid[2399,0]
east = lon_grid[2399,2399]
west = lon_grid[0,0]
#lat/lon bins
bin_lats=fastbin(south,north,numlatbins,-999,-888)
bin_lons=fastbin(west,east,numlonbins,-999,-888)
#lat/lon centers
lon_centers=bin_lons.get_centers()
lat_centers=bin_lats.get_centers()
lon_centers_lst.append(lon_centers)
lat_centers_lst.append(lat_centers)
new_hist=fh.pyhist(lat_grid,lon_grid,bin_lats,bin_lons)
lat_lon_counts=new_hist.get_hist2d()
dirname='plots'
make_dir(dirname)
granule_info='count'
#Figure 1: Lat/lon bin count
fig1 = plt.figure(figcount)
fig1.clf()
cmap=cm.RdBu_r
cmap.set_over('y')
cmap.set_under('k')
vmin= 0
vmax= 20
the_norm=Normalize(vmin=vmin,vmax=vmax,clip=False)
axis1=fig1.add_subplot(111)
im=axis1.pcolormesh(lon_centers,lat_centers,lat_lon_counts,cmap=cmap,norm=the_norm)
cb=plt.colorbar(im,extend='both')
the_label=cb.ax.set_ylabel('counts',rotation=270)
axis1.set_title('{}: 2-d histogram (pixel count in each lat/lon bin'.format(granule_info))
fig1.canvas.draw()
#fig1.savefig('{0:s}/{1:s}_hist2d.png'.format(dirname,granule_info))
figcount += 1
#get NDVI data
model13_file = modis_file_name
NDVI=pyhdf.SD.SD(model13_file)
NDVI_data=NDVI.select('500m 16 days NDVI')
NDVI=NDVI_data.get()
#scale NDVI data
scale_NDVI=10000
offset_NDVI=0
NDVI = (NDVI*1.e-4).astype(np.float32)
#set up NDVI grid
NDVI_grid=new_hist.get_mean(NDVI)
NDVI_grids.append(NDVI_grid)
#Figure 2:
fig1 = plt.figure(figcount)
fig1.clf()
del cmap
cmap=cm.RdBu_r
cmap.set_over('y')
cmap.set_under('k')
vmin= -1.
vmax= 1.
the_norm=Normalize(vmin=vmin,vmax=vmax,clip=False)
axis1=fig1.add_subplot(111)
im=axis1.pcolormesh(lon_centers,lat_centers,NDVI_grid,cmap=cmap,\
norm=the_norm)
cb=plt.colorbar(im,extend='both')
the_label=cb.ax.set_ylabel('NDVI',rotation=270)
axis1.set_title('{}: NDVI'.format(granule_info))
fig1.canvas.draw()
#fig1.savefig('{0:s}/{1:s}_NDVI.png'.format(dirname,granule_info))
figcount += 1
#Figure 3: NDVI plotted over base map.
fig = plt.figure(figcount)
fig.clf()
del cmap
cmap=cm.jet
cmap.set_over('y')
cmap.set_under('k', alpha = 0)
vmin= 0.
vmax= 1.
the_norm=Normalize(vmin=vmin,vmax=vmax,clip=False)
axis1=fig.add_subplot(111)
#create base map
lcc_transform = Basemap(llcrnrlon=-125,llcrnrlat=40,urcrnrlon=-85,urcrnrlat=60,projection='lcc',
resolution='c',lat_1=40.,lat_2=60,lon_0=-100.)
#lcc_transform.fillcontinents(color='coral',lake_color='aqua')
for k in range(0,len(NDVI_grids)):
#for k in range(0,1):
print 'Index', k
NDVI_grid = NDVI_grids[k]
lon_centers = lon_centers_lst[k]
lat_centers = lat_centers_lst[k]
#convert lat/lon to x,y coordinates
lon_array,lat_array=np.meshgrid(lon_centers, lat_centers)
x,y=lcc_transform(lon_array,lat_array)
#plot NDVI on base map
im=axis1.pcolormesh(x,y,NDVI_grid,cmap=cmap,\
norm=the_norm)
lcc_transform.drawcoastlines(linewidth = 1)
lcc_transform.drawparallels(np.arange(40.,60.,1.),labels=[1,0,0,0],labelstyle='+/-',fontsize=8)
lcc_transform.drawmeridians(np.arange(-130.,-85.,2.),labels=[0,0,0,1],labelstyle='+/-',fontsize=8)
lcc_transform.drawstates(linewidth = 1)
lcc_transform.drawcountries(linewidth = 1)
cb=lcc_transform.colorbar(im,location='bottom',pad='10%',fig=fig,extend='both')
cb.set_ticks([0,0.25,0.5,0.75,1], update_ticks=True)
cb.set_label('NDVI')
axis1.set_title('NDVI June 2009')
#fig.savefig('{0:s}/{1:s}_NDVI_Mapped.png'.format(dirname,granule_info))
# si.savemat('MODIS_data_2/MB_May9_2002/lat_lon_grids.mat',savedict)
fig.savefig('MODIS_data_2/June10_2009/NDVI_June_Figure2.png')
#plt.show()