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.005 # 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 print 'cols:',len(lons_interp
)
77 lats_interp
= np
.arange(bounds
[2],bounds
[3],res
)
78 print 'rows:',len(lats_interp
)
79 lons_interp
,lats_interp
= np
.meshgrid(lons_interp
,lats_interp
)
80 glons
= np
.ravel(lons_interp
)
81 glats
= np
.ravel(lats_interp
)
83 # initialize fire_interp
85 fires_interp
= np
.zeros(np
.prod(lons_interp
.shape
))
87 # creating geotransform array with necessary geolocation information
88 geotransform
= [bounds
[0]-res
*.5,res
,rot
,bounds
[2]-res
*.5,rot
,res
]
90 # approximation of the radius for the tree
91 dlon
= abs(data
[gran
].lon
[1,1]-data
[gran
].lon
[0,0])
92 dlat
= abs(data
[gran
].lat
[1,1]-data
[gran
].lat
[0,0])
94 radius
= max(dlon
,dlat
)
95 elif opt_rad
== 'mean':
96 radius
= (dlon
+dlat
)*.5
97 elif opt_rad
== 'min':
98 radius
= min(dlon
,dlat
)
99 print 'radius =',radius
101 # flatten granule data into 1d arrays
102 lons
= np
.ravel(data
[gran
].lon
)
103 lats
= np
.ravel(data
[gran
].lat
)
104 fires
= np
.ravel(data
[gran
].fire
).astype(np
.int8
)
106 M
= (lons
>fbounds
[0])*(lons
<fbounds
[1])*(lats
>fbounds
[2])*(lats
<fbounds
[3])
107 Mg
= (glons
>fbounds
[0])*(glons
<fbounds
[1])*(glats
>fbounds
[2])*(glats
<fbounds
[3])
116 print '> Making the tree...'
118 tree
= spatial
.cKDTree(np
.column_stack((lons
,lats
)))
120 print 'elapsed time: ',abs(t2
-t1
),'s'
121 print '> Interpolating the data...'
123 indexes
= np
.array(tree
.query_ball_point(np
.column_stack((glons
,glats
)),radius
,return_sorted
=True))
125 print 'elapsed time: ',abs(t2
-t1
),'s'
126 filtered_indexes
= np
.array([index
[0] if len(index
) > 0 else np
.nan
for index
in indexes
])
127 fire1d
= [fires
[int(ii
)] if not np
.isnan(ii
) else 0 for ii
in filtered_indexes
]
129 fires_interp
= np
.reshape(fire1d
,lons_interp
.shape
)
131 fires_interp
[Mg
] = fire1d
133 fires_interp
= np
.reshape(fires_interp
,lons_interp
.shape
)
137 print '> Plotting the data...'
138 #number of elements in arrays to plot
141 fig1
, (ax1
,ax2
) = plt
.subplots(nrows
= 2, ncols
=1)
142 ax1
.pcolor(lons_interp
[0::nums1
],lats_interp
[0::nums1
],fires_interp
[0::nums1
])
143 ax2
.scatter(lons
[0::nums
],lats
[0::nums
],c
=fires
[0::nums
],edgecolors
='face')
146 print '> Saving the data...'
148 result
= {'data': fires_interp
.astype(np
.int8
),'geotransform':geotransform
}
149 sio
.savemat(file_name
, mdict
= result
)
151 print 'elapsed time: ',abs(t2
-t1
),'s'
153 print '> File saved as ',file_name
155 print '> File already exists ',file_name
158 print '> Elapsed time for the granule: ',abs(tg2
-tg1
),'s'
163 print '>> Total elapsed time: ',abs(t_final
-t_init
),'s <<'