-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths2_aero.py
495 lines (471 loc) · 25.8 KB
/
s2_aero.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#/usr/bin/env python
import os
import sys
sys.path.insert(0, 'python')
import gdal
import json
import datetime
import logging
import numpy as np
from ddv import ddv
from glob import glob
from scipy import signal, ndimage
import cPickle as pkl
from osgeo import osr
from scipy.ndimage import binary_dilation, binary_erosion
from smoothn import smoothn
from grab_s2_toa import read_s2
from multi_process import parmap
from reproject import reproject_data
from scipy.interpolate import griddata
from grab_brdf import MCD43_SurRef, array_to_raster
from grab_uncertainty import grab_uncertainty
from atmo_solver import solving_atmo_paras
from emulation_engine import AtmosphericEmulationEngine
from psf_optimize import psf_optimize
import warnings
warnings.filterwarnings("ignore")
class solve_aerosol(object):
'''
Prepareing modis data to be able to pass into
atmo_cor for the retrieval of atmospheric parameters.
'''
def __init__(self,
year,
month,
day,
emus_dir = '/home/ucfajlg/Data/python/S2S3Synergy/optical_emulators',
mcd43_dir = '/data/selene/ucfajlg/Ujia/MCD43/',
s2_toa_dir = '/home/ucfafyi/DATA/S2_MODIS/s_data/',
global_dem = '/home/ucfafyi/DATA/Multiply/eles/global_dem.vrt',
wv_emus_dir = '/home/ucfafyi/DATA/Multiply/emus/wv_msi_retrieval.pkl',
cams_dir = '/home/ucfafyi/DATA/Multiply/cams/',
mod08_dir = '/home/ucfafyi/DATA/Multiply/mod08/',
s2_tile = '29SQB',
s2_psf = None,
qa_thresh = 255,
aero_res = 1220, # resolution for aerosol retrival in meters should be larger than 500
reconstruct_s2_angle = True):
self.year = year
self.month = month
self.day = day
self.date = datetime.datetime(self.year, self.month, self.day)
self.doy = self.date.timetuple().tm_yday
self.mcd43_dir = mcd43_dir
self.emus_dir = emus_dir
self.qa_thresh = qa_thresh
self.s2_toa_dir = s2_toa_dir
self.global_dem = global_dem
self.wv_emus_dir = wv_emus_dir
self.cams_dir = cams_dir
self.mod08_dir = mod08_dir
self.s2_tile = s2_tile
self.s2_psf = s2_psf
self.s2_u_bands = 'B02', 'B03', 'B04', 'B08', 'B11', 'B12', 'B8A', 'B09' #bands used for the atmo-cor
self.band_indexs = [1, 2, 3, 7, 11, 12]
self.boa_bands = [469, 555, 645, 869, 1640, 2130]
self.full_res = (10980, 10980)
self.aero_res = aero_res
self.mcd43_tmp = '%s/MCD43A1.A%d%03d.%s.006.*.hdf'
self.reconstruct_s2_angle = reconstruct_s2_angle
self.s2_spectral_transform = [[ 1.06946607, 1.03048916, 1.04039226, 1.00163932, 1.00010918, 0.95607606, 0.99951677],
[ 0.0035921 , -0.00142761, -0.00383504, -0.00558762, -0.00570695, 0.00861192, 0.00188871]]
def _load_xa_xb_xc_emus(self,):
xap_emu = glob(self.emus_dir + '/isotropic_%s_emulators_*_xap.pkl'%(self.s2_sensor))[0]
xbp_emu = glob(self.emus_dir + '/isotropic_%s_emulators_*_xbp.pkl'%(self.s2_sensor))[0]
xcp_emu = glob(self.emus_dir + '/isotropic_%s_emulators_*_xcp.pkl'%(self.s2_sensor))[0]
f = lambda em: pkl.load(open(em, 'rb'))
self.emus = parmap(f, [xap_emu, xbp_emu, xcp_emu])
def repeat_extend(self,data, shape=(10980, 10980)):
da_shape = data.shape
re_x, re_y = int(1.*shape[0]/da_shape[0]), int(1.*shape[1]/da_shape[1])
new_data = np.zeros(shape)
new_data[:] = -9999
new_data[:re_x*da_shape[0], :re_y*da_shape[1]] = np.repeat(np.repeat(data, re_x,axis=0), re_y, axis=1)
return new_data
def gaussian(self, xstd, ystd, angle, norm = True):
win = 2*int(round(max(1.96*xstd, 1.96*ystd)))
winx = int(round(win*(2**0.5)))
winy = int(round(win*(2**0.5)))
xgaus = signal.gaussian(winx, xstd)
ygaus = signal.gaussian(winy, ystd)
gaus = np.outer(xgaus, ygaus)
r_gaus = ndimage.interpolation.rotate(gaus, angle, reshape=True)
center = np.array(r_gaus.shape)/2
cgaus = r_gaus[center[0]-win/2: center[0]+win/2, center[1]-win/2:center[1]+win/2]
if norm:
return cgaus/cgaus.sum()
else:
return cgaus
def _get_ddv_aot(self, selected_img):
red_emus = self.emus[0][3], self.emus[1][3], self.emus[2][3]
blue_emus = self.emus[0][1], self.emus[1][1], self.emus[2][1]
sza = self.sza
blue_vza = self.vza[0]
red_vza = self.vza[2]
blue_raa = self.vaa[0] - self.saa
red_raa = self.vaa[2] - self.saa
b2, b4, = selected_img['B02']/10000., selected_img['B04']/10000.
b8, b12 = selected_img['B08']/10000., selected_img['B12']/10000.
b12 = np.repeat(np.repeat(b12, 2, axis = 1), 2, axis = 0)
solved = ddv(b2, b4, b8, b12, 'msi', sza,
np.array([blue_vza, red_vza]),
np.array([blue_raa, red_raa]),
self.elevation, self.tcwv, self.tco3, ~self.cloud,\
red_emus = red_emus, blue_emus = blue_emus)._ddv_prior()
return solved
def _get_tcwv(self, img, vza, vaa, sza, saa, ele):
b8a, b9 = np.repeat(np.repeat(img['B8A']*0.0001, 2, axis=0), 2, axis=1)[self.Hx, self.Hy],\
np.repeat(np.repeat(img['B09']*0.0001, 6, axis=0), 6, axis=1)[self.Hx, self.Hy]
wv_emus = pkl.load(open(self.wv_emus_dir, 'rb'))
vza, vaa = vza['B09'][self.Hx, self.Hy], vaa['B09'][self.Hx, self.Hy]
sza, saa = sza[self.Hx, self.Hy], saa[self.Hx, self.Hy]
elevation = ele[self.Hx, self.Hy]
inputs = np.array([b9, b8a, vza, sza, abs(saa-vaa), elevation]).T
tcwv_mask = ( b8a < 0.1 ) | self.cloud[self.Hx, self.Hy]
tcwv = np.zeros(self.full_res)
tcwv[:] = np.nan
tcwv_unc = tcwv.copy()
s2_tcwv, s2_tcwv_unc, _ = wv_emus.predict(inputs, do_unc = True)
if tcwv_mask.sum() >= 1:
s2_tcwv[tcwv_mask] = np.interp(np.flatnonzero( tcwv_mask), \
np.flatnonzero(~tcwv_mask), s2_tcwv[~tcwv_mask]) # simple interpolation
tcwv [self.Hx, self.Hy] = s2_tcwv
tcwv_unc[self.Hx, self.Hy] = s2_tcwv_unc
self.tcwv = np.nanmean(tcwv .reshape(self.num_blocks, self.block_size, \
self.num_blocks, self.block_size), axis = (3,1))
self.tcwv_unc = np.nanmax (tcwv_unc.reshape(self.num_blocks, self.block_size, \
self.num_blocks, self.block_size), axis = (3,1)) + 0.05
def _get_psf(self, selected_img):
self.s2_logger.info('No PSF parameters specified, start solving.')
high_img = np.repeat(np.repeat(selected_img['B11'], 2, axis=0), 2, axis=1)*0.0001
high_indexs = self.Hx, self.Hy
low_img = np.ma.array(self.s2_boa[4])
qa, cloud = self.s2_boa_qa[4], self.cloud
xstd, ystd = 29.75, 39
psf = psf_optimize(high_img, high_indexs, low_img, qa, cloud, 0.1, xstd = xstd, ystd = ystd)
xs, ys = psf.fire_shift_optimize()
ang = 0
self.s2_logger.info('Solved PSF parameters are: %.02f, %.02f, %d, %d, %d, and the correlation is: %f.' \
%(xstd, ystd, 0, xs, ys, 1-psf.costs.min()))
return xstd, ystd, ang, xs, ys
def _mcd43_cloud(self,flist, lx, ly, example_file, boa, b12):
g = gdal.BuildVRT('', list(flist))
temp_data = np.zeros((g.RasterYSize, g.RasterXSize))
temp_data[:] = np.nan
temp_data[lx, ly] = boa[5,:]
self.boa_b12 = reproject_data(array_to_raster(temp_data, g), example_file, outputType = gdal.GDT_Float32).data
toa_b12 = np.repeat(np.repeat(b12/10000., 2, axis=0), 2, axis=1)
mask = (abs(self.boa_b12 - toa_b12)>0.1) | (self.boa_b12 < 0.01) | (self.boa_b12 > 1.)
emask = binary_erosion(mask, structure=np.ones((3,3)).astype(bool), iterations=15)
dmask = binary_dilation(emask, structure=np.ones((3,3)).astype(bool), iterations=100)
self.cloud = self.s2.cloud | dmask | (toa_b12 < 0.0001)
#import pylab as plt
#plt.imshow(self.cloud)
#plt.show()
def _mod08_aot(self,):
try:
temp = 'HDF4_EOS:EOS_GRID:"%s":mod08:Aerosol_Optical_Depth_Land_Ocean_Mean'
g = gdal.Open(temp%glob('%s/MOD08_D3.A2016%03d.006.*.hdf'%(self.mod08_dir, self.doy))[0])
dat = reproject_data(g, self.s2_file_dir+'/B01.jp2', outputType= gdal.GDT_Float32).data * g.GetRasterBand(1).GetScale() + g.GetRasterBand(1).GetOffset()
dat[dat<=0] = np.nan
dat[dat>1.5] = np.nan
mod08_aot = np.nanmean(dat)
except:
mod08_aot = np.nan
return mod08_aot
def _get_ddv_aot(self, toa, tcwv, tco3, ele_data):
ndvi_mask = (((toa[3] - toa[2])/(toa[3] + toa[2])) > 0.4) & (toa[5] > 0.01) & (toa[5] < 0.25)
if ndvi_mask.sum() < 100:
self.logger.info('No enough DDV found in this sence for aot restieval, and only cams prediction used.')
else:
Hx, Hy = np.where(ndvi_mask)
if ndvi_mask.sum() > 1000:
random_choice = np.random.choice(len(Hx), 1000, replace=False)
random_choice.sort()
Hx, Hy = Hx[random_choice], Hy[random_choice]
ndvi_mask[:] = False
ndvi_mask[Hx, Hy] = True
Hx, Hy = np.where(ndvi_mask)
blue_vza = np.cos(np.deg2rad(self.vza[0, Hx, Hy]))
blue_sza = np.cos(np.deg2rad(self.sza[Hx, Hy]))
red_vza = np.cos(np.deg2rad(self.vza[2, Hx, Hy]))
red_sza = np.cos(np.deg2rad(self.sza[Hx, Hy]))
blue_raa = np.cos(np.deg2rad(self.vaa[0, Hx, Hy] - self.saa[Hx, Hy]))
red_raa = np.cos(np.deg2rad(self.vaa[2, Hx, Hy] - self.saa[Hx, Hy]))
red, blue = toa[2, Hx, Hy], toa[0, Hx, Hy]
swif = toa[5, Hx, Hy]
red_emus = np.array(self.emus)[:, 3]
blue_emus = np.array(self.emus)[:, 1]
zero_aod = np.zeros_like(red)
red_inputs = np.array([red_sza, red_vza, red_raa, zero_aod, tcwv[Hx, Hy], tco3[Hx, Hy], ele_data[Hx, Hy]])
blue_inputs = np.array([blue_sza, blue_vza, blue_raa, zero_aod, tcwv[Hx, Hy], tco3[Hx, Hy], ele_data[Hx, Hy]])
p = np.r_[np.arange(0, 1., 0.02), np.arange(1., 1.5, 0.05), np.arange(1.5, 2., 0.1)]
f = lambda aot: self._ddv_cost(aot, blue, red, swif, blue_inputs, red_inputs, blue_emus, red_emus)
costs = parmap(f, p)
min_ind = np.argmin(costs)
self.logger.info('DDV solved aod is %.02f, and it will used as the mean value of cams prediction.'% p[min_ind])
self.aot[:] = p[min_ind]
def _ddv_cost(self, aot, blue, red, swif, blue_inputs, red_inputs, blue_emus, red_emus):
blue_inputs[3, :] = aot
red_inputs [3, :] = aot
blue_xap_emu, blue_xbp_emu, blue_xcp_emu = blue_emus
red_xap_emu, red_xbp_emu, red_xcp_emu = red_emus
blue_xap, blue_xbp, blue_xcp = blue_xap_emu.predict(blue_inputs.T)[0], \
blue_xbp_emu.predict(blue_inputs.T)[0], \
blue_xcp_emu.predict(blue_inputs.T)[0]
red_xap, red_xbp, red_xcp = red_xap_emu.predict(red_inputs.T) [0], \
red_xbp_emu.predict(red_inputs.T) [0], \
red_xcp_emu.predict(red_inputs.T) [0]
y = blue_xap * blue - blue_xbp
blue_sur = y / (1 + blue_xcp * y)
y = red_xap * red - red_xbp
red_sur = y / (1 + red_xcp * y)
blue_dif = (blue_sur - 0.25 * swif)**2
red_dif = (red_sur - 0.5 * swif)**2
cost = 0.5 * (blue_dif + red_dif)
return cost.sum()
def _s2_aerosol(self,):
self.s2_logger.propagate = False
self.s2_logger.info('Start to retrieve atmospheric parameters.')
self.s2 = read_s2(self.s2_toa_dir, self.s2_tile, self.year, self.month, self.day, self.s2_u_bands)
self.s2_logger.info('Reading in TOA reflectance.')
selected_img = self.s2.get_s2_toa()
self.s2_file_dir = self.s2.s2_file_dir
self.example_file = self.s2.s2_file_dir+'/B04.jp2'
self.s2.get_s2_cloud()
self.s2_logger.info('Loading emulators.')
self._load_xa_xb_xc_emus()
self.s2_logger.info( 'Getting the angles and simulated surface reflectance.')
self.s2.get_s2_angles()
sa_files = [self.s2.angles['saa'], self.s2.angles['sza']]
if os.path.exists(self.s2_file_dir + '/angles/'):
va_files = [self.s2_file_dir + '/angles/VAA_VZA_B%02d.img'%i for i in [2,3,4,8,11,12]]
else:
va_files = [np.array([self.s2.angles['vaa'][band], self.s2.angles['vza'][band]]) for band in self.s2_u_bands[:-2]]
if len(glob(self.s2_file_dir + '/MCD43.npz')) == 0:
boa, unc, hx, hy, lx, ly, flist = MCD43_SurRef(self.mcd43_dir, self.example_file, \
self.year, self.doy, [sa_files, va_files],
sun_view_ang_scale=[1, 0.01], bands = [3,4,1,2,6,7], tolz=0.001, reproject=False)
np.savez(self.s2_file_dir + '/MCD43.npz', boa=boa, unc=unc, hx=hx, hy=hy, lx=lx, ly=ly, flist=flist)
else:
f = np.load(self.s2_file_dir + '/MCD43.npz')
boa, unc, hx, hy, lx, ly, flist = f['boa'], f['unc'], f['hx'], f['hy'], f['lx'], f['ly'], f['flist']
self.Hx, self.Hy = hx, hy
self.s2_logger.info('Update cloud mask.')
flist = [f.split('/')[0] + self.mcd43_dir+'/'+ f.split('/')[-1] for f in flist]
self._mcd43_cloud(flist, lx, ly, self.example_file, boa, selected_img['B12'])
self.s2_logger.info('Applying spectral transform.')
self.s2_boa_qa = np.ma.array(unc)
self.s2_boa = np.ma.array(boa)*np.array(self.s2_spectral_transform)[0,:-1][...,None] + \
np.array(self.s2_spectral_transform)[1,:-1][...,None]
shape = (self.num_blocks, self.s2.angles['sza'].shape[0] / self.num_blocks, \
self.num_blocks, self.s2.angles['sza'].shape[1] / self.num_blocks)
self.sza = self.s2.angles['sza'].reshape(shape).mean(axis = (3, 1))
self.saa = self.s2.angles['saa'].reshape(shape).mean(axis = (3, 1))
self.vza = []
self.vaa = []
for band in self.s2_u_bands[:-2]:
self.vza.append(self.s2.angles['vza'][band].reshape(shape).mean(axis = (3, 1)))
self.vaa.append(self.s2.angles['vaa'][band].reshape(shape).mean(axis = (3, 1)))
self.vza = np.array(self.vza)
self.vaa = np.array(self.vaa)
self.raa = self.saa[None, ...] - self.vaa
self.s2_logger.info('Getting elevation.')
ele_data = reproject_data(self.global_dem, self.example_file, outputType= gdal.GDT_Float32).data
mask = ~np.isfinite(ele_data)
ele_data = np.ma.array(ele_data, mask = mask)/1000.
self.elevation = ele_data.reshape((self.num_blocks, ele_data.shape[0] / self.num_blocks, \
self.num_blocks, ele_data.shape[1] / self.num_blocks)).mean(axis=(3,1))
self.s2_logger.info('Getting pripors from ECMWF forcasts.')
sen_time_str = json.load(open(self.s2.s2_file_dir+'/tileInfo.json', 'r'))['timestamp']
self.sen_time = datetime.datetime.strptime(sen_time_str, u'%Y-%m-%dT%H:%M:%S.%fZ')
aot, tcwv, tco3 = np.array(self._read_cams(self.example_file)).reshape((3, self.num_blocks, \
self.block_size, self.num_blocks, self.block_size)).mean(axis=(4, 2))
mod08_aot = self._mod08_aot()
#if np.isnan(mod08_aot):
self.aot = aot - (aot.mean() - np.nanmean([mod08_aot, aot.mean()]))
#else:
# temp = np.zeros_like(aot)
# temp[:] = mod08_aot
# self.aot = temp
self.tco3 = tco3 * 46.698
tcwv = tcwv / 10.
self.tco3_unc = np.ones(self.tco3.shape) * 0.2
self.aot_unc = np.ones(self.aot.shape) * 2.
self.s2_logger.info('Trying to get the tcwv from the emulation of sen2cor look up table.')
try:
self._get_tcwv(selected_img, self.s2.angles['vza'], self.s2.angles['vaa'], self.s2.angles['sza'], self.s2.angles['saa'], ele_data)
except:
self.s2_logger.warning('Getting tcwv from the emulation of sen2cor look up table failed, ECMWF data used.')
self.tcwv = tcwv
self.tcwv_unc = np.ones(self.tcwv.shape) * 0.2
'''
self.s2_logger.info('Trying to get the aot from ddv method.')
try:
solved = self._get_ddv_aot(selected_img)
if solved[0] < 0:
self.s2_logger.warning('DDV failed and only cams data used for the prior.')
else:
self.s2_logger.info('DDV solved aot is %.02f, and it will used as the mean value of cams prediction.'%solved[0])
self.aot[:] = solved[0] #self.aot + (solved[0]-self.aot.mean())
except:
self.s2_logger.warning('Getting aot from ddv failed.')
'''
self.s2_logger.info('Mean values for priors are: %.02f, %.02f, %.02f and mod08 aot is %.02f'%(aot.mean(), self.tcwv.mean(), self.tco3.mean(), mod08_aot))
self.s2_logger.info('Applying PSF model.')
if self.s2_psf is None:
xstd, ystd, ang, xs, ys = self._get_psf(selected_img)
else:
xstd, ystd, ang, xs, ys = self.s2_psf
# apply psf shifts without going out of the image extend
shifted_mask = np.logical_and.reduce(((self.Hx+int(xs)>=0),
(self.Hx+int(xs)<self.full_res[0]),
(self.Hy+int(ys)>=0),
(self.Hy+int(ys)<self.full_res[0])))
self.Hx, self.Hy = self.Hx[shifted_mask]+int(xs), self.Hy[shifted_mask]+int(ys)
#self.Lx, self.Ly = self.Lx[shifted_mask], self.Ly[shifted_mask]
self.s2_boa = self.s2_boa [:, shifted_mask]
self.s2_boa_qa = self.s2_boa_qa[:, shifted_mask]
self.s2_logger.info('Getting the convolved TOA reflectance.')
#self.valid_pixs = sum(shifted_mask) # count how many pixels is still within the s2 tile
ker_size = 2*int(round(max(1.96*xstd, 1.96*ystd)))
#self.bad_pixs = np.zeros(self.valid_pixs).astype(bool)
imgs = []
for i, band in enumerate(self.s2_u_bands[:-2]):
if selected_img[band].shape != self.full_res:
imgs.append( self.repeat_extend(selected_img[band], shape = self.full_res))
else:
imgs.append(selected_img[band])
border_mask = np.zeros(self.full_res).astype(bool)
border_mask[[0, -1], :] = True
border_mask[:, [0, -1]] = True
self.dcloud = binary_dilation(self.cloud | border_mask, structure=np.ones((3,3)).astype(bool), iterations=ker_size/2).astype(bool)
self.bad_pixs = self.dcloud[self.Hx, self.Hy]
del selected_img; del self.s2.selected_img; del self.s2.angles['vza']; del self.s2.angles['vaa']
del self.s2.angles['sza']; del self.s2.angles['saa']; del self.s2.sza; del self.s2.saa; del self.s2
ker = self.gaussian(xstd, ystd, ang)
f = lambda img: signal.fftconvolve(img, ker, mode='same')[self.Hx, self.Hy]*0.0001
half = parmap(f,imgs[:3])
self.s2_toa = np.array(half + parmap(f,imgs[3:]))
del imgs
# get the valid value masks
qua_mask = np.all(self.s2_boa_qa <= self.qa_thresh, axis = 0)
boa_mask = np.all(~self.s2_boa.mask,axis = 0 ) &\
np.all(self.s2_boa >= 0.001, axis = 0) &\
np.all(self.s2_boa < 1, axis = 0)
toa_mask = (~self.bad_pixs) &\
np.all(self.s2_toa >= 0.0001, axis = 0) &\
np.all(self.s2_toa < 1., axis = 0)
self.s2_mask = boa_mask & toa_mask & qua_mask
self.Hx = self.Hx [self.s2_mask]
self.Hy = self.Hy [self.s2_mask]
self.s2_toa = self.s2_toa [:, self.s2_mask]
self.s2_boa = self.s2_boa [:, self.s2_mask]
self.s2_boa_unc = self.s2_boa_qa[:, self.s2_mask]
self.s2_logger.info('Solving...')
tempm = np.zeros(self.full_res)
tempm[self.Hx, self.Hy] = 1
tempm = tempm.reshape(self.num_blocks, self.block_size, \
self.num_blocks, self.block_size).astype(int).sum(axis=(3,1))
mask = ~self.dcloud
self.mask = mask.reshape(self.num_blocks, self.block_size, \
self.num_blocks, self.block_size).astype(int).sum(axis=(3,1))
self.mask = ((self.mask/((1.*self.block_size)**2)) >= 0.5) & ((tempm/((self.aero_res/500.)**2)) >= 0.5)
self.mask = binary_erosion(self.mask, structure=np.ones((3,3)).astype(bool))
self.aero = solving_atmo_paras(self.s2_boa,
self.s2_toa,
self.sza,
self.vza,
self.saa,
self.vaa,
self.aot,
self.tcwv,
self.tco3,
self.elevation,
self.aot_unc,
self.tcwv_unc,
self.tco3_unc,
self.s2_boa_unc,
self.Hx, self.Hy,
self.mask,
self.full_res,
self.aero_res,
self.emus,
self.band_indexs,
self.boa_bands)
solved = self.aero._optimization()
return solved
def _read_cams(self, example_file, parameters = ['aod550', 'tcwv', 'gtco3']):
netcdf_file = datetime.datetime(self.sen_time.year, self.sen_time.month, \
self.sen_time.day).strftime("%Y-%m-%d.nc")
template = 'NETCDF:"%s":%s'
ind = np.abs((self.sen_time.hour + self.sen_time.minute/60. + \
self.sen_time.second/3600.) - np.arange(0,25,3)).argmin()
sr = osr.SpatialReference()
sr.ImportFromEPSG(4326)
proj = sr.ExportToWkt()
results = []
for para in parameters:
fname = template%(self.cams_dir + '/' + netcdf_file, para)
g = gdal.Open(fname)
g.SetProjection(proj)
sub = g.GetRasterBand(ind+1)
offset = sub.GetOffset()
scale = sub.GetScale()
bad_pix = int(sub.GetNoDataValue())
rep_g = reproject_data(g, example_file, outputType= gdal.GDT_Float32).g
data = rep_g.GetRasterBand(ind+1).ReadAsArray()
data = data*scale + offset
mask = (data == (bad_pix*scale + offset)) | np.isnan(data)
if mask.sum()>=1:
data[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask), data[~mask])
results.append(data)
return results
def solving_s2_aerosol(self,):
self.s2_logger = logging.getLogger('Sentinel 2 Atmospheric Correction')
self.s2_logger.setLevel(logging.INFO)
if not self.s2_logger.handlers:
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
self.s2_logger.addHandler(ch)
self.s2_logger.propagate = False
self.s2_sensor = 'msi'
self.s2_logger.info('Doing Sentinel 2 tile: %s on %d-%02d-%02d.'%(self.s2_tile, self.year, self.month, self.day))
self.block_size = int(self.aero_res/10)
self.num_blocks = int(np.ceil(self.full_res[0]/self.block_size))
self.solved = self._s2_aerosol()[0].reshape(3, self.num_blocks, self.num_blocks)
self.s2_logger.info('Finished retrieval and saving them into local files.')
g = gdal.Open(self.s2_file_dir+'/B04.jp2')
xmin, ymax = g.GetGeoTransform()[0], g.GetGeoTransform()[3]
projection = g.GetProjection()
para_names = 'aot', 'tcwv', 'tco3'
priors = [self.aot, self.tcwv, self.tco3]
for i,para_map in enumerate(self.solved):
if self.mask.sum()>0:
g_data = griddata(np.array(np.where(self.mask)).T, para_map[self.mask], \
(np.repeat(range(self.num_blocks), self.num_blocks).reshape(self.num_blocks, self.num_blocks), \
np.tile (range(self.num_blocks), self.num_blocks).reshape(self.num_blocks, self.num_blocks)), method='nearest')
else:
g_data = priors[i]
self.solved[i] = g_data
xres, yres = self.block_size*10, self.block_size*10
geotransform = (xmin, xres, 0, ymax, 0, -yres)
nx, ny = self.num_blocks, self.num_blocks
outputFileName = self.s2_file_dir + '/%s.tif'%para_names[i]
if os.path.exists(outputFileName):
os.remove(outputFileName)
dst_ds = gdal.GetDriverByName('GTiff').Create(outputFileName, ny, nx, 1, gdal.GDT_Float32)
dst_ds.SetGeoTransform(geotransform)
dst_ds.SetProjection(projection)
dst_ds.GetRasterBand(1).WriteArray(g_data)
dst_ds.FlushCache()
dst_ds = None
self.aot_map, self.tcwv_map, self.tco3_map = self.solved
if __name__ == "__main__":
aero = solve_aerosol( 2016, 2, 10, mcd43_dir = '/data/selene/ucfajlg/Hebei/MCD43/', s2_toa_dir = '/store/S2_data/',\
emus_dir = '/home/ucfafyi/DATA/Multiply/emus/', s2_tile='50SLG', s2_psf=None)
aero.solving_s2_aerosol()
#solved = aero.prepare_modis()