3 import matplotlib
.pyplot
as plt
4 from scipy
.interpolate
import griddata
5 from scipy
import spatial
13 # use a test granule or all the granules?
15 # interpolate the whole granule?
17 # radius options: 'max', 'mean', or 'min'
22 print '1> Test granule'
24 print '1> All the granules'
26 print '2> Interpolating the whole granule'
28 print '2> Interpolating just the fire mesh bounding box'
29 print '3> Radius computed using',opt_rad
32 print '>> Loading data <<'
33 # loading the data file
34 data
,fxlon
,fxlat
,time_num
=sl
.load('data')
36 print 'elapsed time: ',abs(t_init
-t1
),'s'
39 # mapping prefixes from JPSSD to cycling
40 prefixes
={'MOD': 'MOD14', 'MYD': 'MYD14', 'VNP': 'NPP_VAF_L2'}
42 # constants for geotransform
44 res
= 0.05 # resolution
46 res
= 0.01 # resolution
47 fbounds
= [fxlon
.min(),fxlon
.max(),fxlat
.min(),fxlat
.max()] # fire mesh bounds
48 rot
= 0.0 # rotation (not currently supported)
49 print '>> General geotransform constants <<'
50 print 'resolution =',res
51 print 'rotation =',rot
54 # defining granules to process
56 grans
= ['VNP_A2018312_2130']
61 print '>> Processing',NG
,'granules <<'
63 for ng
,gran
in enumerate(grans
):
64 print '>> Processing granule',gran
,'-','%d/%d' % (ng
+1,NG
)
67 splitted
=gran
.split('_')
69 date
=splitted
[1][3:8]+splitted
[2]+'00'
70 file_name
= prefixes
[prefix
]+'.'+date
+'.tif.mat'
72 if not os
.path
.isfile(file_name
):
73 # creating meshgrid where to interpolate the data
74 bounds
= [data
[gran
].lon
.min(),data
[gran
].lon
.max(),data
[gran
].lat
.min(),data
[gran
].lat
.max()]
75 lons_interp
= np
.arange(bounds
[0],bounds
[1],res
)
76 lats_interp
= np
.arange(bounds
[2],bounds
[3],res
)
77 lons_interp
,lats_interp
= np
.meshgrid(lons_interp
,lats_interp
)
78 glons
= np
.reshape(lons_interp
,np
.prod(lons_interp
.shape
))
79 glats
= np
.reshape(lats_interp
,np
.prod(lats_interp
.shape
))
81 # initialize fire_interp
83 fires_interp
= np
.zeros(np
.prod(lons_interp
.shape
))
85 # creating geotransform array with necessary geolocation information
86 geotransform
= [bounds
[0],res
,rot
,bounds
[3],rot
,res
]
88 # approximation of the radius for the tree
89 dlon
= abs(data
[gran
].lon
[1,1]-data
[gran
].lon
[0,0])
90 dlat
= abs(data
[gran
].lat
[1,1]-data
[gran
].lat
[0,0])
92 radius
= max(dlon
,dlat
)
93 elif opt_rad
== 'mean':
94 radius
= (dlon
+dlat
)*.5
95 elif opt_rad
== 'min':
96 radius
= min(dlon
,dlat
)
97 print 'radius =',radius
99 # flatten granule data into 1d arrays
100 lons
= np
.reshape(data
[gran
].lon
,np
.prod(data
[gran
].lon
.shape
))
101 lats
= np
.reshape(data
[gran
].lat
,np
.prod(data
[gran
].lat
.shape
))
102 fires
= np
.reshape(data
[gran
].fire
,np
.prod(data
[gran
].fire
.shape
)).astype(np
.int8
)
104 M
= (lons
>fbounds
[0])*(lons
<fbounds
[1])*(lats
>fbounds
[2])*(lats
<fbounds
[3])
105 Mg
= (glons
>fbounds
[0])*(glons
<fbounds
[1])*(glats
>fbounds
[2])*(glats
<fbounds
[3])
114 print '> Making the tree...'
116 tree
= spatial
.cKDTree(np
.column_stack((lons
,lats
)))
118 print 'elapsed time: ',abs(t2
-t1
),'s'
119 print '> Interpolating the data...'
121 indexes
= np
.array(tree
.query_ball_point(np
.column_stack((glons
,glats
)),radius
,return_sorted
=True))
123 print 'elapsed time: ',abs(t2
-t1
),'s'
124 filtered_indexes
= np
.array([index
[0] if len(index
) > 0 else np
.nan
for index
in indexes
])
125 fire1d
= [fires
[int(ii
)] if not np
.isnan(ii
) else 0 for ii
in filtered_indexes
]
127 fires_interp
= np
.reshape(fire1d
,lons_interp
.shape
)
129 fires_interp
[Mg
] = fire1d
131 fires_interp
= np
.reshape(fires_interp
,lons_interp
.shape
)
135 print '> Plotting the data...'
136 #number of elements in arrays to plot
139 fig1
, (ax1
,ax2
) = plt
.subplots(nrows
= 2, ncols
=1)
140 ax1
.pcolor(lons_interp
[0::nums1
],lats_interp
[0::nums1
],fires_interp
[0::nums1
])
141 ax2
.scatter(lons
[0::nums
],lats
[0::nums
],c
=fires
[0::nums
],edgecolors
='face')
144 print '> Saving the data...'
146 result
= {'data': fires_interp
.astype(np
.int8
),'geotransform':geotransform
}
147 sio
.savemat(file_name
, mdict
= result
)
149 print 'elapsed time: ',abs(t2
-t1
),'s'
151 print '> File saved as ',file_name
153 print '> File already exists ',file_name
156 print '> Elapsed time for the granule: ',abs(tg2
-tg1
),'s'
161 print '>> Total elapsed time: ',abs(t_final
-t_init
),'s <<'