3 import matplotlib
.pyplot
as plt
4 from scipy
.interpolate
import griddata
5 from scipy
import spatial
7 print('Loading data...')
8 data
,fxlon
,fxlat
,time_num
=sl
.load('data')
9 prefixes
={'MOD': 'MOD14', 'MYD': 'MYD14', 'VNP': 'NPP_VAF_L2'}
11 # constants for geotransform
12 res
= 0.01 # resolution
13 rot
= 0.0 # rotation (not currently supported)
18 for gran
in list(data
):
19 splitted
=gran
.split('_')
21 date
=splitted
[1][3:8]+splitted
[2]+'00'
22 file_name
= prefixes
[prefix
]+'.'+date
+'.tif.mat'
26 bounds
= [data
[gran
].lon
.min(),data
[gran
].lon
.max(),data
[gran
].lat
.min(),data
[gran
].lat
.max()]
27 lons_interp
= np
.arange(bounds
[0],bounds
[1],res
)
28 lats_interp
= np
.arange(bounds
[2],bounds
[3],res
)
29 lons_interp
,lats_interp
= np
.meshgrid(lons_interp
,lats_interp
)
31 geotransform
= [bounds
[0],res
,rot
,bounds
[3],rot
,res
]
34 # flatten to 1d the arrays
35 lons
= np
.reshape(data
[gran
].lon
,np
.prod(data
[gran
].lon
.shape
))
36 lats
= np
.reshape(data
[gran
].lat
,np
.prod(data
[gran
].lat
.shape
))
37 fires
= np
.reshape(data
[gran
].fire
,np
.prod(data
[gran
].fire
.shape
)).astype(np
.int8
)
41 tree
= spatial
.cKDTree(np
.column_stack((lons
,lats
)))
42 glons
= np
.reshape(lons_interp
,np
.prod(lons_interp
.shape
))
43 glats
= np
.reshape(lats_interp
,np
.prod(lats_interp
.shape
))
44 indexes
= np
.array(tree
.query_ball_point(np
.column_stack((glons
,glats
)),radius
))
45 filtered_indexes
= np
.array([index
[0] if len(index
) > 0 else np
.nan
for index
in indexes
])
46 fire1d
= [fires
[int(ii
)] if not np
.isnan(ii
) else 0 for ii
in filtered_indexes
]
47 fires_interp
= np
.reshape(fire1d
,lons_interp
.shape
)
49 #coordinates = np.array((lons,lats)).T
50 #print(coordinates.shape)
52 #print(lons_interp.shape)
55 print(lats_interp.shape)
58 #fires_interp = griddata(coordinates,fires,(lons_interp,lats_interp),method='nearest',fill_value = np.nan)
59 print('Done interpolating, now plotting')
61 print('Nans in fire: ',np
.isnan(fires_interp
).sum())
65 #number of elements in arrays to plot
68 fig1
, (ax1
,ax2
) = plt
.subplots(nrows
= 2, ncols
=1)
69 ax1
.pcolor(lons_interp
[0::nums1
],lats_interp
[0::nums1
],fires_interp
[0::nums1
])
71 ax2
.scatter(lons
[0::nums
],lats
[0::nums
],c
=fires
[0::nums
],edgecolors
='face')
73 result
= {'data': fires_interp
.astype(np
.int8
),'geotransform':geotransform
}
74 sio
.savemat(file_name
,mdict
= result
)
76 print('File saved as ',file_name
)