-
Notifications
You must be signed in to change notification settings - Fork 0
/
baltic_AC_backwardNN.py
1058 lines (894 loc) · 45.6 KB
/
baltic_AC_backwardNN.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
import collections
import datetime
import json
#from keras.models import load_model
import locale
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
from scipy.optimize import minimize, curve_fit
import sys
import time
import glob
# snappy import
# sys.path.append('/home/cmazeran/.snap/snap-python')
# sys.path.append("C:\\Users\Dagmar\Anaconda3\envs\py36\Lib\site-packages\snappy")
sys.path.append("C:\\Users\Dagmar\.snap\snap-python")
import snappy as snp
from snappy import Product
from snappy import ProductData
from snappy import ProductDataUTC
from snappy import ProductIO
from snappy import ProductUtils
from snappy import ProgressMonitor
from snappy import FlagCoding
from snappy import jpy
from snappy import GPF
from snappy import HashMap
#from snappy import TimeCoding #org.esa.snap.core.datamodel.TimeCoding
from snappy import PixelPos #org.esa.snap.core.datamodel.PixelPos
#fetchOzone = jpy.get_type('org.esa.s3tbx.c2rcc.ancillary.AncillaryCommons.fetchOzone')
AtmosphericAuxdata = jpy.get_type('org.esa.s3tbx.c2rcc.ancillary.AtmosphericAuxdata')
AtmosphericAuxdataBuilder = jpy.get_type('org.esa.s3tbx.c2rcc.ancillary.AtmosphericAuxdataBuilder')
TimeCoding = jpy.get_type('org.esa.snap.core.datamodel.TimeCoding')
AncDownloader = jpy.get_type('org.esa.s3tbx.c2rcc.ancillary.AncDownloader')
AncillaryCommons = jpy.get_type('org.esa.s3tbx.c2rcc.ancillary.AncillaryCommons')
AncRepository = jpy.get_type('org.esa.s3tbx.c2rcc.ancillary.AncRepository')
File = jpy.get_type('java.io.File')
AncDataFormat = jpy.get_type('org.esa.s3tbx.c2rcc.ancillary.AncDataFormat')
Calendar = jpy.get_type('java.util.Calendar')
# local import
from baltic_corrections import gas_correction, glint_correction, white_caps_correction, Rayleigh_correction, diffuse_transmittance, Rmolgli_correction_Hygeos, vicarious_calibration
import get_bands
from misc import default_ADF, nlinear
import luts_olci
import lut_hygeos
from auxdata_handling import setAuxData, checkAuxDataAvailablity, getGeoPositionsForS2Product, yearAndDoyAndHourUTC
# Set locale for proper time reading with datetime
# locale.setlocale(locale.LC_ALL, 'en_US.UTF_8')
def read_NN_input_ranges_fromFile(nnFilePath):
""" Read input range for the forward NN """
input_range = collections.OrderedDict()
with open(nnFilePath) as f:
lines = f.readlines()
for l in lines:
if 'input' in l[:5]:
v = [a for a in l.split(' ') if a!='']
varname = v[3]
r = v[5].split(',')
r = (float(r[0][1:]), float(r[1][:-2]))
input_range[varname] = r
f.close()
return input_range
def get_band_or_tiePointGrid(product, name, dtype='float32', reshape=True):
##
# This function reads a band or tie-points, identified by its name <name>, from SNAP product <product>
# The fuction returns a numpy array of shape (height, width)
##
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
var = np.zeros(width * height, dtype=dtype)
if name in list(product.getBandNames()):
product.getBand(name).readPixels(0, 0, width, height, var)
elif name in list(product.getTiePointGridNames()):
var.shape = (height, width)
for i in range(height):
for j in range(width):
var[i, j] = product.getTiePointGrid(name).getPixelDouble(j, i)
var.shape = (height*width)
else:
raise Exception('{}: neither a band nor a tie point grid'.format(name))
if reshape:
var.shape = (height, width)
return var
def Level1_Reader(product, sensor, band_group='radiance', reshape=True):
input_label = []
if sensor == 'S2MSI':
input_label = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12']
elif sensor == 'OLCI':
if band_group == 'radiance':
input_label = ["Oa%02d_radiance"%(i+1) for i in range(21)]
elif band_group == 'solar_flux':
input_label = ["solar_flux_band_%d"%(i+1) for i in range(21)]
elif band_group == 'lambda0':
input_label = ["lambda0_band_%d"%(i+1) for i in range(21)]
# Initialise and read all bands contained in input_label (pixel x band)
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
var = np.zeros((width * height, len(input_label)))
for i, bn in enumerate(input_label):
var[:,i] = get_band_or_tiePointGrid(product, bn, reshape=reshape)
return var
def get_yday(product,reshape=True):
# Get product size
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
## time handling for match-up files
if str(product.getFileLocation()).endswith('.txt') or str(product.getFileLocation()).endswith('.csv'):
yday = np.zeros((height, width))
for y in range(height):
for x in range(width):
a = product.getSceneTimeCoding().getMJD(PixelPos(x + 0.5, y + 0.5))
year, yday[y, x], hour = yearAndDoyAndHourUTC(a)
else:
## time handling for a scene
# Get yday of each row
dstart = datetime.datetime.strptime(str(product.getStartTime()),'%d-%b-%Y %H:%M:%S.%f')
dstop = datetime.datetime.strptime(str(product.getEndTime()),'%d-%b-%Y %H:%M:%S.%f')
dstart = np.datetime64(dstart)
dstop = np.datetime64(dstop)
dpix = dstart + (dstop-dstart)/float(height-1.)*np.arange(height)
yday = [k.timetuple().tm_yday for k in dpix.astype(datetime.datetime)]
# Apply to all columns
yday = np.array(yday*width).reshape(width,height).transpose()
if not reshape:
yday = np.ravel(yday)
return yday
def radianceToReflectance_Reader(product, sensor = '', print_info=False):
if print_info:
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
name = product.getName()
description = product.getDescription()
band_names = product.getBandNames()
print("Sensor: %s" % sensor)
print("Product: %s, %s" % (name, description))
print("Raster size: %d x %d pixels" % (width, height))
print("Start time: " + str(product.getStartTime()))
print("End time: " + str(product.getEndTime()))
print("Bands: %s" % (list(band_names)))
if sensor == 'OLCI':
rad = Level1_Reader(product, sensor, band_group='radiance',reshape=False)
solar_flux = Level1_Reader(product, sensor, band_group='solar_flux',reshape=False)
SZA = get_band_or_tiePointGrid(product, 'SZA', reshape=False)
refl = rad * np.pi / (solar_flux * np.cos(SZA.reshape(rad.shape[0],1)*np.pi/180.))
elif sensor == 'S2MSI':
refl = Level1_Reader(product, sensor,reshape=False)
return refl
def angle_Reader(product, sensor):
if sensor == 'OLCI':
oaa = get_band_or_tiePointGrid(product, 'OAA', reshape=False)
oza = get_band_or_tiePointGrid(product, 'OZA', reshape=False)
saa = get_band_or_tiePointGrid(product, 'SAA', reshape=False)
sza = get_band_or_tiePointGrid(product, 'SZA', reshape=False)
elif sensor == 'S2MSI':
oaa = get_band_or_tiePointGrid(product, 'view_azimuth_mean', reshape=False)
oza = get_band_or_tiePointGrid(product, 'view_zenith_mean', reshape=False)
saa = get_band_or_tiePointGrid(product, 'sun_azimuth', reshape=False)
sza = get_band_or_tiePointGrid(product, 'sun_zenith', reshape=False)
return oaa, oza, saa, sza
def calculate_diff_azim(oaa, saa):
###
# MERIS/OLCI ground segment definition
raa = np.degrees(np.arccos(np.cos(np.radians(oaa - saa))))
###
# azimuth difference as input to the NN is defined in a range between 0° and 180°
## from c2rcc JAVA implementation
nn_raa = np.abs(180 + oaa - saa)
ID = np.array(nn_raa > 180)
if np.sum(ID) > 0:
nn_raa = 360 - nn_raa
return raa, nn_raa
def run_IdePix_processor(product, sensor):
### todo: check, if L1 flags are given as band 'quality_flags'
# (in CalValus extracts this single band representation of the flags might be missing.)
# calculate single band 'quality_flags' if necessary.
# invoke IdePix.
# define valid pixel expression.
idepixParameters = HashMap()
idepixParameters.put("computeCloudBuffer", 'true')
idepixParameters.put("cloudBufferWidth", '2')
idepix_product = None
if sensor == 'OLCI':
# idepix_product = GPF.createProduct("Idepix.Sentinel3.Olci", idepixParameters, product) # SNAP 6
idepix_product = GPF.createProduct("Idepix.Olci", idepixParameters, product) #
elif sensor == 'S2MSI':
idepixParameters.put("computeCloudBufferForCloudAmbiguous", 'true')
idepix_product = GPF.createProduct("Idepix.Sentinel2", idepixParameters, product)
return idepix_product
def check_valid_pixel_expression_Idepix(product, sensor):
valid_pixel_flag = None
if sensor == 'OLCI':
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
quality_flags = np.zeros(width * height, dtype='uint32')
product.getBand('pixel_classif_flags').readPixels(0, 0, width, height, quality_flags)
# Masks OLCI Idepix
invalid_mask = np.bitwise_and(quality_flags, 2 ** 0) == 2 ** 0
cloud_mask = np.bitwise_and(quality_flags, 2 ** 1) == 2 ** 1
cloudbuffer_mask = np.bitwise_and(quality_flags, 2 ** 4) == 2 ** 4
snowice_mask = np.bitwise_and(quality_flags, 2 ** 6) == 2 ** 6
invalid_mask = np.logical_or(np.logical_or(invalid_mask, snowice_mask), np.logical_or(cloud_mask, cloudbuffer_mask))
valid_pixel_flag = np.logical_not(invalid_mask)
if sensor == 'S2MSI':
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
quality_flags = np.zeros(width * height, dtype='uint32')
product.getBand('pixel_classif_flags').readPixels(0, 0, width, height, quality_flags)
# Masks S2MSI Idepix
invalid_mask = np.bitwise_and(quality_flags, 2 ** 0) == 2 ** 0
cloud_mask = np.bitwise_and(quality_flags, 2 ** 1) == 2 ** 1
cloudbuffer_mask = np.bitwise_and(quality_flags, 2 ** 4) == 2 ** 4
snowice_mask = np.bitwise_and(quality_flags, 2 ** 6) == 2 ** 6
invalid_mask = np.logical_or(np.logical_or(invalid_mask, snowice_mask),
np.logical_or(cloud_mask, cloudbuffer_mask))
valid_pixel_flag = np.logical_not(invalid_mask)
return valid_pixel_flag
def check_valid_pixel_expression_L1(product, sensor):
valid_pixel_flag = None
if sensor == 'OLCI':
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
quality_flags = np.zeros(width * height, dtype='uint32')
# match-up extractions from Calvalus do not have a 'quality_flags' band
if product.getBand('quality_flags') is None:
invalid_mask = np.zeros(width * height, dtype='uint32')
product.getBand('quality_flags.invalid').readPixels(0, 0, width, height, invalid_mask)
land_mask = np.zeros(width * height, dtype='uint32')
product.getBand('quality_flags.land').readPixels(0, 0, width, height, land_mask)
coastline_mask = np.zeros(width * height, dtype='uint32')
product.getBand('quality_flags.coastline').readPixels(0, 0, width, height, coastline_mask)
inland_mask = np.zeros(width * height, dtype='uint32')
product.getBand('quality_flags.fresh_inland_water').readPixels(0, 0, width, height, inland_mask)
land_mask = coastline_mask | (land_mask & ~inland_mask)
bright_mask = np.zeros(width * height, dtype='uint32')
product.getBand('quality_flags.bright').readPixels(0, 0, width, height, bright_mask)
invalid_mask = np.logical_or(invalid_mask, np.logical_or(land_mask, bright_mask))
valid_pixel_flag = np.logical_not(invalid_mask)
else:
product.getBand('quality_flags').readPixels(0, 0, width, height, quality_flags)
# Masks OLCI L1
## flags: 31=land 30=coastline 29=fresh_inland_water 28=tidal_region 27=bright 26=straylight_risk 25=invalid
## 24=cosmetic 23=duplicated 22=sun-glint_risk 21=dubious 20->00=saturated@Oa01->saturated@Oa21
## todo: could also include simple bright NIR test: rho_toa(865nm) < 0.2
invalid_mask = np.bitwise_and(quality_flags, 2 ** 25) == 2 ** 25
land_mask = np.bitwise_and(quality_flags, 2 ** 31) == 2 ** 31
coastline_mask = np.bitwise_and(quality_flags, 2 ** 30) == 2 ** 30
inland_mask = np.bitwise_and(quality_flags, 2 ** 29) == 2 ** 29
land_mask = coastline_mask | (land_mask & ~inland_mask)
bright_mask = np.bitwise_and(quality_flags, 2 ** 27) == 2 ** 27
invalid_mask = np.logical_or(invalid_mask , np.logical_or( land_mask , bright_mask))
valid_pixel_flag = np.logical_not(invalid_mask)
elif sensor == 'S2MSI':
#TODO: set valid pixel expression L1C S2
height = product.getSceneRasterHeight()
width = product.getSceneRasterWidth()
valid_pixel_flag = np.ones(width * height, dtype='uint32')
b8 = get_band_or_tiePointGrid(product, 'B8', reshape=False)
valid_pixel_flag = np.logical_and(np.array(b8 > 0), np.array(b8 < 0.1))
return valid_pixel_flag
def apply_forwardNN_IOP_to_rhow(log_iop, sun_zenith, view_zenith, diff_azimuth, sensor, valid_data, nn_iop_rw, iband_NN_forward, T=15, S=35):
"""
Apply the forwardNN: IOP to rhow
input: numpy array log_iop, shape = (Npixels x log_iop= (log_apig, log_adet, log a_gelb, log_bpart, log_bwit)),
np.array sza, shape = (Npixels,)
np.array oza, shape = (Npixels,)
np.array raa, shape = (Npixels,); range: 0-180
returns: np.array rhow, shape = (Npixels, wavelengths)
T, S: currently constant #TODO take ECMWF temperature at sea surface?
valid ranges can be found at the beginning of the .net-file.
NN output for OLCI (12 bands): log_rw at lambda = 400, 412, 443, 489, 510, 560, 620, 665, 674, 681, 709, 754
NN output for S2MSI (6 bands): log_rw at lambda = 443, 490, 560, 665, 705, 740
"""
# Initialise output
nBands = len(iband_NN_forward)
output = np.zeros((log_iop.shape[0], nBands)) + np.NaN
###
# Launch the NN
# Important:
# OLCI input array has to be of size 10: [SZA, VZA, RAA, T, S, log_apig, log_adet, log a_gelb, log_bpart, log_bwit]
# S2 input array has to be of size 10 (same order as OLCI): [ sun_zeni, view_zeni, azi_diff, T, S, log_conc_apig, log_conc_adet,
# log_conc_agelb, log_conc_bpart, log_conc_bwit]
inputNN = np.zeros(10) -1. # CARE -1. used by default for de-activated IOPs when niop<5
inputNN[3] = T
inputNN[4] = S
for i in range(log_iop.shape[0]):
if valid_data[i]:
inputNN[0] = sun_zenith[i]
inputNN[1] = view_zenith[i]
inputNN[2] = diff_azimuth[i]
for j in range(log_iop.shape[1]): # log_apig, log_adet, log a_gelb, log_bpart, log_bwit
inputNN[j+5] = log_iop[i, j]
log_rw_nn2 = np.array(nn_iop_rw.calc(inputNN), dtype=np.float32)
output[i, :] = np.exp(log_rw_nn2)
# #// (9.5.4)
# #check if log_IOPs out of range
# mi = nn_rw_iop.getOutmin();
# ma = nn_rw_iop.getOutmax();
# boolean iop_oor_flag = false;
# for (int iv = 0; iv < log_iops_nn1.length; iv++) {
# if (log_iops_nn1[iv] < mi[iv] | log_iops_nn1[iv] > ma[iv]) {
# iop_oor_flag = true;
# }
# }
# flags = BitSetter.setFlag(flags, FLAG_INDEX_IOP_OOR, iop_oor_flag);
#
# #// (9.5.5)
# # check if log_IOPs at limit
# int firstIopMaxFlagIndex = FLAG_INDEX_APIG_AT_MAX;
# for (int i = 0; i < log_iops_nn1.length; i++) {
# final boolean iopAtMax = log_iops_nn1[i] > (ma[i] - log_threshfak_oor);
# flags = BitSetter.setFlag(flags, i + firstIopMaxFlagIndex, iopAtMax);
# }
#
# int firstIopMinFlagIndex = FLAG_INDEX_APIG_AT_MIN;
# for (int i = 0; i < log_iops_nn1.length; i++) {
# final boolean iopAtMin = log_iops_nn1[i] < (mi[i] + log_threshfak_oor);
# flags = BitSetter.setFlag(flags, i + firstIopMinFlagIndex, iopAtMin);
# }
#
#
return output
def apply_backwardNN_rhow_to_IOP(rhow, sun_zenith, view_zenith, diff_azimuth, sensor, valid_data, nn_rw_iop, iband_NN_backward, inputRange_backward, T=15, S=35):
"""
Apply the backwardNN: rhow to IOP
input: numpy array rhow, shape = (Npixels x rhow = (log_rw_band1, log_rw_band2_.... )),
np.array sza, shape = (Npixels,)
np.array oza, shape = (Npixels,)
np.array raa, shape = (Npixels,); range: 0-180
returns: np.array log_iop, shape = (Npixels x log_iop= (log_apig, log_adet, log a_gelb, log_bpart, log_bwit))
T, S: currently constant #TODO take ECMWF temperature at sea surface?
valid ranges can be found at the beginning of the .net-file.
NN input for OLCI (12 bands): log_rw at lambda = 400, 412, 443, 489, 510, 560, 620, 665, 674, 681, 709, 754
NN input for S2MSI (8 bands): log_rw at lambda = 443, 490, 560, 665, 705, 740, 783, 865
"""
# Initialise output
nBands = len(iband_NN_backward)
niop = 5
output = np.zeros((rhow.shape[0], niop)) + np.NaN
FlagConstraintApplied = np.zeros(rhow.shape[0])
###
# Launch the NN
# CARE to the input bands
inputNN = np.zeros(nBands+5) -1. # care zero used by default for de-activated IOPs when niop<5
inputNN[3] = T
inputNN[4] = S
for i in range(rhow.shape[0]):
if valid_data[i]:
inputNN[0] = sun_zenith[i]
inputNN[1] = view_zenith[i]
inputNN[2] = diff_azimuth[i]
for j,var in enumerate(list(inputRange_backward.keys())[5:]):
# Threshold input rhow, in case of negative or too high value
j_glob = iband_NN_backward[j]
# inputRange=list(inputRange_backward.keys())[5:][j]
rhow_in = max(rhow[i, j_glob], np.exp(inputRange_backward[var][0]))
rhow_in = min(rhow_in, np.exp(inputRange_backward[var][1]))
if rhow_in != rhow[i, j_glob]:
FlagConstraintApplied[i] = 1
inputNN[j+5] = np.log(rhow_in)
output[i,:] = np.array(nn_rw_iop.calc(inputNN), dtype=np.float32) # log_apig, log_adet, log a_gelb, log_bpart, log_bwit
return output, FlagConstraintApplied
def apply_NN_rhow_to_rhownorm(rhow, sun_zenith, view_zenith, diff_azimuth, sensor, valid_data, nn_rw_rwnorm, iband_NN_norm, inputRange_norm, T=15, S=35):
"""
Apply NN : rhow to rhownorm
input: numpy array rhow, shape = (Npixels x wavelengths),
np.array sza, shape = (Npixels,)
np.array oza, shape = (Npixels,)
np.array raa, shape = (Npixels,); range: 0-180
returns: np.array rhownorm, shape = (Npixels, wavelengths)
NN input and output for OLCI (12 bands): log_rw_norm at lambda = 400, 412, 443, 489, 510, 560, 620, 665, 674, 681, 709, 754
NN input and output for S2MSI (6 bands): log_rw_norm at lambda = 443, 490, 560, 665, 705, 740
"""
# Initialise output
nBands = None
if sensor == 'OLCI':
nBands = 12
elif sensor == 'S2MSI':
nBands = 6
output = np.zeros((rhow.shape[0], nBands)) + np.NaN
###
# Launch the NN
# Important:
# OLCI input array has to be of size 17: [SZA, VZA, RAA, T, S, 12x log rhow]
# S2 input array has to be of size 11 : [ sun_zeni, view_zeni, azi_diff, T, S, 6x log rhow]
inputNN = np.zeros(5+nBands)
inputNN[3] = T
inputNN[4] = S
FlagConstraintApplied = np.zeros(rhow.shape[0])
for i in range(rhow.shape[0]):
if valid_data[i]:
inputNN[0] = sun_zenith[i]
inputNN[1] = view_zenith[i]
inputNN[2] = diff_azimuth[i]
for j,var in enumerate(list(inputRange_norm.keys())[5:]):
# Threshold input rhow, in case of negative or too high value
j_glob = iband_NN_norm[j]
rhow_in = max(rhow[i, j_glob], np.exp(inputRange_norm[var][0]))
rhow_in = min(rhow_in, np.exp(inputRange_norm[var][1]))
if rhow_in != rhow[i, j_glob]:
FlagConstraintApplied[i] = 1
inputNN[j+5] = np.log(rhow_in)
log_rw_nn2 = np.array(nn_rw_rwnorm.calc(inputNN), dtype=np.float32)
output[i, :] = np.exp(log_rw_nn2)
return output, FlagConstraintApplied
def write_BalticP_AC_Product(product, baltic__product_path, sensor, spectral_dict, scalar_dict=None,
copyOriginalProduct=False, outputProductFormat="BEAM-DIMAP", addname='',
add_Idepix_Flags=False, idepixProduct=None, add_L2Flags=False, L2FlagArray=None,
add_Geometry=False):
# Initialise the output product
File = jpy.get_type('java.io.File')
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
bandShape = (height, width)
dirname = os.path.dirname(baltic__product_path)
outname, ext = os.path.splitext(os.path.basename(baltic__product_path))
if outputProductFormat == "BEAM-DIMAP":
baltic__product_path = os.path.join(dirname, outname + addname +'.dim')
elif outputProductFormat == 'CSV':
baltic__product_path = os.path.join(dirname, outname + addname +'.csv')
balticPACProduct = Product('balticPAC', 'balticPAC', width, height)
balticPACProduct.setFileLocation(File(baltic__product_path))
ProductUtils.copyGeoCoding(product, balticPACProduct)
# Define total number of bands (TOA)
if (sensor == 'OLCI'):
nbands = 21
elif (sensor == 'S2MSI'):
nbands = 13
for key in spectral_dict.keys():
data = spectral_dict[key].get('data')
if not data is None:
nbands_key = data.shape[-1]
if outputProductFormat == 'BEAM-DIMAP':
if sensor == 'OLCI':
bsources = [product.getBand("Oa%02d_radiance" % (i + 1)) for i in range(nbands)]
elif sensor == 'S2MSI':
bsources = [product.getBand("B%d" % (i + 1)) for i in range(8)]
bsources.append(product.getBand('B8A'))
[bsources.append(product.getBand("B%d" % (i + 9))) for i in range(4)]
for i in range(nbands_key):
brtoa_name = key + "_" + str(i + 1)
band = balticPACProduct.addBand(brtoa_name, ProductData.TYPE_FLOAT64)
if outputProductFormat == 'BEAM-DIMAP':
ProductUtils.copySpectralBandProperties(bsources[i], band)
band.setNoDataValue(np.nan)
band.setNoDataValueUsed(True)
sourceData = np.array(data[:, i], dtype='float64').reshape(bandShape)
band.setRasterData(ProductData.createInstance(sourceData))
# Create empty bands for scalar fields
if not scalar_dict is None:
for key in scalar_dict.keys():
singleBand = balticPACProduct.addBand(key, ProductData.TYPE_FLOAT64)
singleBand.setNoDataValue(np.nan)
singleBand.setNoDataValueUsed(True)
data = scalar_dict[key].get('data')
if not data is None:
sourceData = np.array(data, dtype='float64').reshape(bandShape)
singleBand.setRasterData(ProductData.createInstance(sourceData))
if copyOriginalProduct:
originalBands = product.getBandNames()
balticBands = balticPACProduct.getBandNames()
for bb in balticBands:
originalBands = [ob for ob in originalBands if ob != bb]
for ob in originalBands:
singleBand = balticPACProduct.addBand(ob, ProductData.TYPE_FLOAT64)
singleBand.setNoDataValue(np.nan)
singleBand.setNoDataValueUsed(True)
data = get_band_or_tiePointGrid(product,ob)
sourceData = np.array(data, dtype='float64').reshape(bandShape)
singleBand.setRasterData(ProductData.createInstance(sourceData))
if add_Idepix_Flags:
flagBand = balticPACProduct.addBand('pixel_classif_flags', ProductData.TYPE_INT32)
flagBand.setDescription('Idepix flag information')
flagBand.setNoDataValue(np.nan)
flagBand.setNoDataValueUsed(True)
data = get_band_or_tiePointGrid(idepixProduct, 'pixel_classif_flags')
sourceData = np.array(data, dtype='int32').reshape(bandShape)
flagBand.setRasterData(ProductData.createInstance(sourceData))
idepixFlagCoding = FlagCoding('pixel_classif_flags')
flagNames = list(idepixProduct.getAllFlagNames())
print(list(flagNames))
IDflags = 'pixel_classif_flags'
flagNames = [fn[(len(IDflags)+1):] for fn in flagNames if IDflags in fn]
for i, fn in enumerate(flagNames):
idepixFlagCoding.addFlag(fn, 2**i, fn)
balticPACProduct.getFlagCodingGroup().add(idepixFlagCoding)
flagBand.setSampleCoding(idepixFlagCoding)
if add_L2Flags:
flagBand = balticPACProduct.addBand('baltic_L2_flags', ProductData.TYPE_INT32)
flagBand.setDescription('L2 flag information for the baltic+ AC')
flagBand.setNoDataValue(np.nan)
flagBand.setNoDataValueUsed(True)
sourceData = np.array(L2FlagArray, dtype='int32').reshape(bandShape)
flagBand.setRasterData(ProductData.createInstance(sourceData))
L2FlagCoding = FlagCoding('baltic_L2_flags')
flagNames = ['OOR_NN_IOP', 'OOR_NN_normalisation', 'NELDER_MEAD_FAIL']
flagDescription = ['input IOPs to forwardNN out of range. at least one IOP has been constrained.',
'input rho_w to NormalisationNN out of range. at least one rho_w has been constrained.',
'Nelder-Mead Optimisation failed.']
#IDflags = 'baltic_L2_flags'
#flagNames = [fn[(len(IDflags) + 1):] for fn in flagNames if IDflags in fn]
i = 0
for fn, dscr in zip(flagNames, flagDescription):
L2FlagCoding.addFlag(fn, 2 ** i, dscr)
i += 1
balticPACProduct.getFlagCodingGroup().add(L2FlagCoding)
flagBand.setSampleCoding(L2FlagCoding)
if add_Geometry:
oaa, oza, saa, sza = angle_Reader(product, sensor)
if sensor == 'OLCI':
geomNames = ['OAA', 'OZA', 'SAA', 'SZA']
elif sensor == 'S2MSI':
geomNames = ['view_azimuth_mean', 'view_zenith_mean', 'sun_azimuth', 'sun_zenith']
dataList = [oaa, oza, saa, sza]
for gn, data in zip(geomNames, dataList):
singleBand = balticPACProduct.addBand(gn, ProductData.TYPE_FLOAT64)
singleBand.setNoDataValue(np.nan)
singleBand.setNoDataValueUsed(True)
sourceData = np.array(data, dtype='float64').reshape(bandShape)
singleBand.setRasterData(ProductData.createInstance(sourceData))
if outputProductFormat == 'BEAM-DIMAP':
# Set auto grouping
autoGroupingString = ':'.join(spectral_dict.keys())
balticPACProduct.setAutoGrouping(autoGroupingString)
GPF.writeProduct(balticPACProduct, File(baltic__product_path), outputProductFormat, False, ProgressMonitor.NULL)
balticPACProduct.closeIO()
def polymer_matrix(bands_sat,bands,valid,rho_g,rho_r,sza,oza,wavelength,adf_ppp):
"""
Compute matrices associated to the polynomial modelling of the atmosphere (POLYMER)
Care: direct matrice (forward model) is for 'bands_sat', while inverse (backward model) is limited to 'bands'
"""
# Define matrix of polynomial modelling (c0 T0 + c1 lambda^-1 + c2 rho_R)
ncoef = 3
nband = len(bands)
nband_sat = len(bands_sat)
npix = rho_g.shape[0]
Aatm = np.zeros((npix, nband_sat, ncoef), dtype='float32') # Care, defined for bands_sat
Aatm_inv = np.zeros((npix, ncoef, nband), dtype='float32') # Care, defined for only bands
# Indices of bands_corr in all bands
iband = np.searchsorted(bands_sat, bands)
# Compute T0
taum = 0.00877*((wavelength/1000.)**(-4.05))
air_mass = 1./np.cos(np.radians(sza))+1./np.cos(np.radians(oza))
rho_g0 = 0.02
factor = (1-0.5*np.exp(-rho_g/rho_g0))*air_mass
T0 = np.exp(-taum*factor[:,None])
Aatm[:,:,0] = T0
Aatm[:,:,1] = (wavelength/1000.)**-1.
Aatm[:,:,2] = rho_r
# Compute pseudo inverse: A* = ((A'.A)^(-1)).A' /!\ limited to bands
Aatm_corr = Aatm[:,iband,:]
Aatm_inv[valid] = np.linalg.pinv(Aatm_corr[valid])
return Aatm, Aatm_inv
def check_and_constrain_iop(log_iop, inputRange,sensor):
#if sensor == 'OLCI':
# log_iops = ['log_apig', 'log_adet', 'log_agelb', 'log_bpart', 'log_bwit']
#elif sensor == 'S2MSI':
# log_iops = ['log_conc_apig', 'log_conc_adet', 'log_conc_agelb', 'log_conc_bpart', 'log_conc_bwit']
for i in range(len(log_iop)):
var = inputRange.keys()[5+i] # discard first variables (angles, temperature, salinity)
mi = inputRange[var][0]
ma = inputRange[var][1]
if log_iop[i] < mi:
log_iop[i] = mi
if log_iop[i] > ma:
log_iop[i] = ma
return log_iop
def ac_cost(coefs, wavelength, sensor, nbands, iband_NN_forward, iband_NN_backward, iband_corr, iband_chi2, rho_rc, td, sza, oza, raa, Aatm, valid, nn_iop_rw, nn_rw_iop, inputRange_backward):
"""
Cost function to be minimized, define for one pixel
"""
# Compute rho_ag
rho_ag_mod = rhoa_mod(wavelength,coefs)
# Compute rho_w
rho_w = (rho_rc - rho_ag_mod)/td
# Compute IOP
log_iop, oorBackwardNN = apply_backwardNN_rhow_to_IOP(np.array([rho_w]), np.array([sza]), np.array([oza]), np.array([raa]), sensor, np.array([valid]), nn_rw_iop, iband_NN_backward, inputRange_backward)
# Check iop range and apply constraints to forwardNN input range
#log_iop = check_and_constrain_iop(log_iop, inputRange, sensor) TODO check it is useless because iop come from backwardNN
# Compute rho_wmod
rho_wmod = np.zeros(nbands) + np.NaN
rho_wmod[iband_NN_forward] = apply_forwardNN_IOP_to_rhow(log_iop, np.array([sza]), np.array([oza]), np.array([raa]), sensor,np.array([valid]), nn_iop_rw, iband_NN_forward) # Care, shape of log_iop is already (1, niop)
# Compute residual and chi2
res = rho_w[iband_chi2]-rho_wmod[iband_chi2]
##res = rho_w[iband_chi2]/rho_wmod[iband_chi2]-1.
chi2 = np.sum(res*res) # TODO cost function should include weighting; option in relative difference
return chi2
def f_opt(xdata_all, c0,c1,c2):
rho_rc_mod = np.ndarray(len(xdata_all))
rho_ag_mod = np.ndarray(len(xdata_all))
rho_w = np.ndarray(len(xdata_all))
rho_wmod = np.ndarray(len(xdata_all))
td = np.ndarray(len(xdata_all))
for ib, xdata in enumerate(xdata_all):
# Get the independent variables
wavelength = xdata[0]
sensor = xdata[1]
nbands = xdata[2]
iband_NN_forward = xdata[3]
iband_NN_backward = xdata[4]
iband_corr = xdata[5]
iband_chi2 = xdata[6]
rho_rc = xdata[7]
td[ib] = xdata[8]
sza = xdata[9]
oza = xdata[10]
raa = xdata[11]
valid = xdata[12]
nn_iop_rw = xdata[13]
nn_rw_iop = xdata[14]
inputRange_backward = xdata[15]
iband_rw = xdata[16]
# Compute rho_ag
coefs = np.array([c0,c1,c2])
rho_ag_mod[ib] = rhoa_mod_mono(wavelength,coefs)
# Compute rho_w
rho_w[ib] = (rho_rc - rho_ag_mod[ib])/td[ib]
# Compute IOP
log_iop, oorBackwardNN = apply_backwardNN_rhow_to_IOP(np.array([rho_w]), np.array([sza]), np.array([oza]), np.array([raa]), sensor, np.array([valid]), nn_rw_iop, iband_NN_backward, inputRange_backward)
# Check iop range and apply constraints to forwardNN input range
#log_iop = check_and_constrain_iop(log_iop, inputRange, sensor) TODO check it is useless because iop come from backwardNN
# Compute rho_wmod
rho_wmod = apply_forwardNN_IOP_to_rhow(log_iop, np.array([sza]), np.array([oza]), np.array([raa]), sensor,np.array([valid]), nn_iop_rw, iband_NN_forward) # Care, shape of log_iop is already (1, niop)
# Compute rhorc_mod
rho_rc_mod = rho_ag_mod + td*rho_wmod # TODO bands_chi ?
return np.ravel(rho_rc_mod)
def baltic_AC_backwardNN(scene_path='', filename='', outpath='', sensor='', subset=None, addName = '', outputSpectral=None,
outputScalar=None, correction='HYGEOS', copyOriginalProduct=False, outputProductFormat="BEAM-DIMAP",
atmosphericAuxDataPath = None, niop=5, add_Idepix_Flags=True, add_L2Flags=False, add_c2rccIOPs=False):
"""
Main function to run the Baltic+ AC based on backward NN
correction: 'HYGEOS' or 'IPF' for Rayleigh+glint correction
"""
# Define forward NN and normalisation NN
if sensor == 'OLCI':
nnForwardFilePath = "forwardNN_c2rcc/olci/olci_20171221/iop_rw/77x77x77_1798.8.net"
nnBackwardFilePath = "forwardNN_c2rcc/olci/olci_20171221/rw_iop/37x37x37_596495.4.net"
nnNormFilePath = "forwardNN_c2rcc/olci/olci_20171221/rw_rwnorm/77x77x77_34029.1.net"
#nnForwardFilePath = "forwardNN_c2rcc/olci/olci_20190414/iop_rw/55x55x55_40.3.net"
#nnNormFilePath = "forwardNN_c2rcc/olci/olci_20190414/rw_rwnorm/77x77x77_34029.1.net"
elif sensor == 'S2MSI':
nnForwardFilePath = "forwardNN_c2rcc/msi/std_s2_20160502/iop_rw/17x97x47_125.5.net"
nnBackwardFilePath = "forwardNN_c2rcc/msi/std_s2_20160502/rw_rwnorm/27x7x27_28.0.net"
nnNormFilePath = "forwardNN_c2rcc/msi/std_s2_20160502/rw_rwnorm/27x7x27_28.0.net"
# Read the NNs
NNffbpAlphaTabFast = jpy.get_type('org.esa.snap.core.nn.NNffbpAlphaTabFast')
nnfile = open(nnForwardFilePath, 'r')
nnCode = nnfile.read()
nn_iop_rw = NNffbpAlphaTabFast(nnCode)
nnfile = open(nnBackwardFilePath, 'r')
nnCode = nnfile.read()
nn_rw_iop = NNffbpAlphaTabFast(nnCode)
nnfile = open(nnNormFilePath, 'r')
nnCode = nnfile.read()
nn_rw_rwnorm = NNffbpAlphaTabFast(nnCode)
# Read NNs input range
inputRange_forward = read_NN_input_ranges_fromFile(nnForwardFilePath)
inputRange_backward = read_NN_input_ranges_fromFile(nnBackwardFilePath)
inputRange_norm = read_NN_input_ranges_fromFile(nnNormFilePath)
# Get sensor & AC bands
bands_sat, bands_rw, bands_corr, bands_chi2, bands_forwardNN, bands_backwardNN, bands_normNN, bands_abs = get_bands.main(sensor,"dummy")
nbands = len(bands_sat)
iband_rw = np.searchsorted(bands_sat, bands_rw)
iband_corr = np.searchsorted(bands_sat, bands_corr)
iband_chi2 = np.searchsorted(bands_sat, bands_chi2)
iband_NN_forward = np.searchsorted(bands_sat, bands_forwardNN)
iband_NN_backward = np.searchsorted(bands_sat, bands_backwardNN)
iband_NN_norm = np.searchsorted(bands_sat, bands_normNN)
iband_abs = np.searchsorted(bands_sat, bands_abs)
# Initialising a product for Reading with snappy
product = snp.ProductIO.readProduct(os.path.join(scene_path, filename))
# Resampling S2MSI to 60m
if sensor == "S2MSI" and product.isMultiSize():
print( "Resample MSI data")
parameters = HashMap()
parameters.put('resolution', '60')
parameters.put('upsampling', 'Bicubic')
parameters.put('downsampling', 'Mean') #
parameters.put('flagDownsampling', 'FlagOr') # 'First', 'FlagAnd'
product = GPF.createProduct('S2Resampling', parameters, product)
# Get scene size
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
npix = width*height
# Read L1B product and convert Radiance to reflectance
rho_toa = radianceToReflectance_Reader(product, sensor=sensor)
# Classify pixels with Level-1 flags
valid = check_valid_pixel_expression_L1(product, sensor)
print(np.sum(valid), len(valid))
if add_Idepix_Flags:
idepixProduct = run_IdePix_processor(product, sensor)
validIdepix = check_valid_pixel_expression_Idepix(idepixProduct, sensor)
valid = np.logical_and(valid, validIdepix)
else:
idepixProduct=None
# Limit processing to sub-box
if subset: #FIXME should be only applied to input raster file
sline,eline,scol,ecol = subset
valid_subset = np.zeros((height,width),dtype='bool')
valid_subset[sline:eline+1,scol:ecol+1] = valid.reshape(height,width)[sline:eline+1,scol:ecol+1]
valid = valid_subset.reshape(height*width)
del valid_subset
# Read geometry and compute relative azimuth angle
oaa, oza, saa, sza = angle_Reader(product, sensor)
raa, nn_raa = calculate_diff_azim(oaa, saa)
# Read wavelength (per-pixel for OLCI) and geolocation
if sensor == 'OLCI':
# Read per-pixel wavelength
wavelength = Level1_Reader(product, sensor, 'lambda0', reshape=False)
# Read latitude, longitude
latitude = get_band_or_tiePointGrid(product, 'latitude', reshape=False)
longitude = get_band_or_tiePointGrid(product, 'longitude', reshape=False)
elif sensor == 'S2MSI':
# Duplicate wavelengths for all pixels
wavelength = np.tile(bands_sat,(len(sza),1)) #TODO: integrate band with S2 SRF
# Read latitude, longitude
latitude, longitude = getGeoPositionsForS2Product(product)
# Read day in the year
yday = get_yday(product, reshape=False)
# Read meteo data
print( "Read meteo data")
if sensor == 'OLCI':
pressure = get_band_or_tiePointGrid(product, 'sea_level_pressure', reshape=False)
ozone = get_band_or_tiePointGrid(product, 'total_ozone', reshape=False)
tcwv = get_band_or_tiePointGrid(product, 'total_columnar_water_vapour', reshape=False)
wind_u = get_band_or_tiePointGrid(product, 'horizontal_wind_vector_1', reshape=False)
wind_v = get_band_or_tiePointGrid(product, 'horizontal_wind_vector_2', reshape=False)
windm = np.sqrt(wind_u*wind_u+wind_v*wind_v)
altitude = get_band_or_tiePointGrid(product, 'altitude', reshape=False)
elif sensor == 'S2MSI':
bandNames = list(product.getBandNames())
## only for match-up data with included meteorology.
if ('pressure' in bandNames) and ('ozone' in bandNames) and ('tcwv' in bandNames)\
and ('wind_u' in bandNames) and ('wind_v' in bandNames):
pressure = get_band_or_tiePointGrid(product, 'pressure', reshape=False)
ozone = get_band_or_tiePointGrid(product, 'ozone', reshape=False)
tcwv = get_band_or_tiePointGrid(product, 'tcwv', reshape=False)
wind_u = get_band_or_tiePointGrid(product, 'wind_u', reshape=False)
wind_v = get_band_or_tiePointGrid(product, 'wind_v', reshape=False)
windm = np.sqrt(wind_v * wind_v + wind_u * wind_u)
else:
if atmosphericAuxDataPath != None:
# Compute aux data (one unique value per scene)
AuxFullFilePath_dict = checkAuxDataAvailablity(atmosphericAuxDataPath, product=product)
AuxDataDict = setAuxData(product, AuxFullFilePath_dict)
# Apply values to the whole image
shape = sza.shape
pressure = np.ones(shape)*AuxDataDict['pressure']
ozone = np.ones(shape)*AuxDataDict['ozone']
tcwv = np.ones(shape)*AuxDataDict['tcwv']
wind_u = np.ones(shape)*AuxDataDict['wind_u']
wind_v = np.ones(shape)*AuxDataDict['wind_v']
windm = np.sqrt(wind_v*wind_v + wind_u*wind_u)
else:
print('Please set a path to the AUX data archive.')
sys.exit(1)
# Read LUTs
if sensor == 'OLCI':
file_adf_acp = default_ADF['OLCI']['file_adf_acp']
file_adf_ppp = default_ADF['OLCI']['file_adf_ppp']
file_adf_clp = default_ADF['OLCI']['file_adf_clp']
adf_acp = luts_olci.LUT_ACP(file_adf_acp)
adf_ppp = luts_olci.LUT_PPP(file_adf_ppp)
adf_clp = luts_olci.LUT_CLP(file_adf_clp)
if correction == 'HYGEOS':
LUT_HYGEOS = lut_hygeos.LUT(default_ADF['OLCI']['file_HYGEOS'])
#elif sensor == 'S2' TODO
print("Pre-corrections")
# Gaseous correction
rho_ng = gas_correction(rho_toa, valid, latitude, longitude, yday, sza, oza, raa, wavelength,
pressure, ozone, tcwv, adf_ppp, adf_clp, sensor)
# Vicarious calibration
#rho_ng = vicarious_calibration(rho_ng, valid, adf_acp, sensor)
# Compute diffuse transmittance (Rayleigh)
td = diffuse_transmittance(sza, oza, pressure, adf_ppp)
# Glint correction - rho_g required even for HYGEOS correction
rho_g, rho_gc = glint_correction(rho_ng, valid, sza, oza, saa, raa, pressure, wind_u, wind_v, windm, adf_ppp)
if correction == 'IPF':
# White-caps correction
#rho_wc, rho_gc = white_caps_correction(rho_ng, valid, windm, td, adf_ppp)
# Rayleigh correction
rho_r, rho_rc = Rayleigh_correction(rho_gc, valid, sza, oza, raa, pressure, windm, adf_acp, adf_ppp, sensor)
elif correction == 'HYGEOS':
# Glint + Rayleigh correction
rho_r, rho_molgli, rho_rc, tau_r, tau_r_mono = Rmolgli_correction_Hygeos(rho_ng, valid, latitude, sza, oza, raa, wavelength,
pressure, windm, LUT_HYGEOS, altitude)
# Atmospheric model
print("Compute atmospheric matrices")
Aatm, Aatm_inv = polymer_matrix(bands_sat,bands_corr,valid,rho_g,rho_r,sza,oza,wavelength,adf_ppp)
l2flags = np.zeros(npix, dtype='int32')
# Inversion of log_iop = [log_apig, log_adet, log a_gelb, log_bpart, log_bwit]
print("Inversion")
coefs = np.zeros((npix,3)) + np.NaN
percent_old = 0
ipix_proc = 0
npix_proc = np.count_nonzero(valid)
for ipix in range(npix):
if not valid[ipix]: continue
# Display processing progress with respect to the valid pixels
percent = (int(float(ipix_proc)/float(npix_proc)*100)/10)*10
if percent != percent_old:
percent_old = percent
sys.stdout.write(" ...%d%%"%percent)
sys.stdout.flush()
# First guess
coefs_0 = np.array([0.01,-1.,-0.3])
# Define the independent variables
xdata_all = []
for iband in iband_NN_forward:
xdata = []
xdata.append(wavelength[ipix,iband])
xdata.append(sensor)
xdata.append(nbands)
xdata.append(iband_NN_forward)
xdata.append(iband_NN_backward)
xdata.append(iband_corr)
xdata.append(iband_chi2)
xdata.append(rho_rc[ipix,iband])
xdata.append(td[ipix,iband])
xdata.append(sza[ipix])
xdata.append(oza[ipix])
xdata.append(nn_raa[ipix])
xdata.append(valid[ipix])
xdata.append(nn_iop_rw)
xdata.append(nn_rw_iop)
xdata.append(inputRange_backward)
xdata.append(iband_rw)
xdata_all.append(xdata)
# Curve fitting optimization
opt, pcov = curve_fit(f_opt, xdata_all, rho_rc[ipix,iband_NN_forward],p0=coefs_0,bounds=([1.E-6,-3.,-0.5],[0.08, 0.5, 0.]))
coefs[ipix,:] = opt
success = True # TODO
if not success:
l2flags[ipix] += 2 ** 2
#coefs[ipix,:] = coefs_0
ipix_proc += 1
print("")
# Compute the final residual
print("Compute final residual")
rho_ag_mod = rhoa_mod(wavelength,coefs)
rho_w = (rho_rc - rho_ag_mod)/td
log_iop, oorBackwardNN = apply_backwardNN_rhow_to_IOP(rho_w, sza, oza, nn_raa, sensor, valid, nn_rw_iop, iband_NN_backward, inputRange_backward)
rho_wmod = np.zeros((npix, nbands)) + np.NaN
rho_wmod[:, iband_NN_forward] = apply_forwardNN_IOP_to_rhow(log_iop, sza, oza, nn_raa, sensor, valid, nn_iop_rw, iband_NN_forward)
rho_ag = rho_rc - td*rho_wmod
# Set absorption band to NaN