Pular para conteúdo

Geedar

estimation(algos, productID, demandIDs=[-1], running_mode=1)

Essa função executa um conjunto de algoritmos de estimação.

Source code in geedar_lib/geedar.py
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
def estimation(algos:int, productID:int, demandIDs = [-1], running_mode:int = 1):
    """ Essa função executa um conjunto de algoritmos de estimação."""
    global image_collection
    global anyError

    if not isinstance(algos, list):
        algos = [algos]

    productBands = list(set(list(bands.values())))
    image_collection = ee.ImageCollection(image_collection).select(
        productBands + export_bands
        )

    for algo_i in range(len(algos)):
        algo = algos[algo_i]

        # Check if the required bands for running the estimation algorithm are prensent in the product.
        requiredBands = ESTIMATION_ALGO_SPECS[algo]["requiredBands"]

        if not all(band in list(bands.keys()) for band in requiredBands):
            msg = "(!) The product #" 
            + str(productID) 
            + " does not contain all the bands required to run the estimation algorithm #" 
            + str(algo) + ": " 
            + str(requiredBands) + "."

            if running_mode < 3:
                print(msg)

            elif running_mode >= 3:
                anyError = True
                print("[DEMANDID " + str(demandIDs[algo_i]) + "] " + msg)
                writeToLogFile(msg, "Error", "DEMANDID " + str(demandIDs[algo_i]))

            continue

        # Add the estimated variable to the list of reduction.
        varName = ESTIMATION_ALGO_SPECS[algo]["paramName"]

        if not isinstance(varName, list):
            varName = [varName]

        if not varName == [""]:
            export_bands.extend(varName)

        # 00 is the most simple one. It does nothing with the images.
        if algo == 0:
            pass

        # Conc. de clorofila-a em açudes do Nordeste.

        elif algo == 1:
            def estim(image):
                red = image.select(bands["red"])
                green = image.select(bands["green"])
                ind = red.subtract(red.pow(2).divide(green))
                return image.addBands(ind.pow(2).multiply(0.0004).add(
                        ind.multiply(0.213)).add(4.3957).rename(varName[0]
                    ))
            image_collection = image_collection.map(estim)

        # Sedimentos em Suspensão na Superfície no Solimões.

        elif algo == 2:
            image_collection = image_collection.map(
                lambda image: image.addBands(
                    image.select(bands["NIR"]).divide(
                    image.select(bands["red"])).pow(1.9189).multiply(759.12)
                    .rename(varName[0])
                    )
                )

        # Sedimentos em Suspensão na Superfície do Rio Madeira.
        elif algo == 3:
            def estim(image):
                nir = image.select(bands["NIR"])
                red = image.select(bands["red"])
                nirRedRatio = nir.divide(red)
                filter = nirRedRatio.pow(2).multiply(421.63).add(
                    nirRedRatio.multiply(1027.6)
                    ).subtract(nir).abs()
                sss = nirRedRatio.updateMask(
                    filter.lt(200)).pow(2.94).multiply(1020).rename(varName[0])
                return image.addBands(sss)
            image_collection = image_collection.map(estim)                                

        # Sedimentos em Suspensão na Superfície em Óbidos, no rio Amazonas.
        elif algo == 4:
            image_collection = image_collection.map(
                lambda image: image.addBands(
                    image.select(bands["NIR"]).multiply(0.2019).add(-14.222).rename(varName[0])
                    )
                )

        # Turbidez nos reservatórios do Paranapanema.
        elif algo == 5:
            image_collection = image_collection.map(
                lambda image: image.addBands(
                    image.select(bands["red"]).multiply(0.00223).exp()
                    .multiply(2.45).rename(varName[0])
                    )
                )

        # SSS no Paraopeba.
        elif algo == 10:

            def estim(image):
                nir = image.select(bands["NIR"])
                red = image.select(bands["red"])
                green = image.select(bands["green"])
                rejeito = green.divide(
                    math.pi * 10000).pow(-1).subtract(
                    red.divide(math.pi * 10000).pow(-1)
                    )
                ind1 = nir.divide(math.pi*10000).multiply(red.divide(green))
                ind2 = nir.divide(red)
                normalCase = ind1.pow(2).multiply(18381).add(
                    ind1.multiply(3874.8)
                    )
                specialCase = ind2.pow(2).multiply(9205.5).add(
                    ind2.multiply(-9253.8)
                    )
                sss = normalCase.where(
                    ind2.gte(0.9).And(rejeito), 
                    specialCase).rename(varName[0]
                    )
                return image.addBands(sss)

            image_collection = image_collection.map(estim)

        # SSS, ISS, OSS and chla in Brazilian semiarid reservoirs.
        elif algo == 11:

            def estim(image):
                nir = image.select(bands["NIR"])
                red = image.select(bands["red"])
                green = image.select(bands["green"])
                blue = image.select(bands["blue"])
                iss = red.subtract(nir).multiply(0.059).add(
                    green.subtract(nir).multiply(-0.0245)).add(0.74)
                iss = iss.where(iss.lt(0), 0).rename(varName[1])
                sss = red.subtract(blue).multiply(0.06318).add(
                    green.multiply(0.009793)).add(1.363)
                sss = sss.where(iss.gt(sss), iss).rename(varName[0])
                oss = sss.subtract(iss).rename(varName[2])
                chla = green.multiply(0.0937).add(
                    iss.multiply(-3.752)).add(-10.92)
                chla = chla.where(chla.lt(0), 0).rename(varName[3])
                biomass = chla.multiply(0.02386).exp().multiply(
                    1.55465).rename(varName[4])
                return image.addBands(sss).addBands(iss).addBands(
                    oss).addBands(chla).addBands(biomass)

            image_collection = image_collection.map(estim)

        # Chla in Brazilian semiarid reservoirs.
        elif algo == 12:

            def estim(image):
                chla = image.select(bands["green"]).multiply(
                    0.1396).add(image.select(
                    bands["red"]).multiply(-0.1006)).add(
                    -4.227).rename(varName[0])

                return image.addBands(chla)
            image_collection = image_collection.map(estim)

        # 99 is for tests only.
        elif algo == 99:
            image_collection = image_collection.map(
                lambda image: image.addBands(
                ee.Image(1234).rename(varName[0]))
                )

getAvailableDates(productID, dateList)

Retorna um array de valores da propriedade "img_date" de cada imagem da coleção de imagens.

Parameters:

Name Type Description Default
productID int

Identificação do produto espectral.

required
dateList list

Lista de datas para verificação de dados disponíveis

required

Returns:

Type Description
list

Um array com valores da propriedade "img_date" de cada imagem da coleção de imagens.

Examples:

>>> getAvailableDates(101, ['2020-01-01'])
Source code in geedar_lib/geedar.py
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
def getAvailableDates(productID:int, dateList:list) -> list:
    """
    Retorna um array de valores da propriedade "img_date" de cada imagem da coleção de imagens.

    Args:
        productID: Identificação do produto espectral.
        dateList: Lista de datas para verificação de dados disponíveis

    Returns:
        Um array com valores da propriedade "img_date" de cada imagem da coleção de imagens. 

    Examples:
        >>> getAvailableDates(101, ['2020-01-01'])

    """
    aoi = None
    dateMin = dateList[0]
    dateMax = (pd.Timestamp(dateList[-1]) 
               + pd.Timedelta(1, "day")).strftime("%Y-%m-%d")
    imageCollection = ee.ImageCollection(getCollection(productID)) \
        .filterBounds(aoi) \
        .filterDate(dateMin, dateMax) \
        .map(lambda image: image.set(
            "img_date", ee.Image(image).date().format("YYYY-MM-dd"))) \
        .filter(ee.Filter.inList("img_date", dateList))
    return imageCollection.aggregate_array("img_date").getInfo()

getCollection(productID)

Returna uma lista com uma coleção de imagens GEE de determinado produto GEEDaR.

Parameters:

Name Type Description Default
productID int

Identificação do produto espectral.

required

Returns:

Type Description
ee.imagecollection.ImageCollection

Uma lista com uma coleção de imagens do GEE relacionadas a determinado produto GEEDaR.

Examples:

>>> getCollection(101)
ee.ImageCollection({"functionInvocationValue": {"functionName": "Element.set","arguments": {"key": {"constantValue": "product_id"},"object": {"functionInvocationValue": {"functionName": "ImageCollection.load","arguments": {"id": {"constantValue": "MODIS/006/MOD09GA"}}}},"value": {"constantValue": 101}}}})
Source code in geedar_lib/geedar.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def getCollection(productID:int) -> ee.imagecollection.ImageCollection:
    """
    Returna uma lista com uma coleção de imagens GEE de determinado produto GEEDaR.

    Args:
        productID: Identificação do produto espectral.

    Returns:
        Uma lista com uma coleção de imagens do GEE relacionadas a determinado produto GEEDaR.

    Examples:
        >>> getCollection(101)
        ee.ImageCollection({"functionInvocationValue": {"functionName": "Element.set","arguments": {"key": {"constantValue": "product_id"},"object": {"functionInvocationValue": {"functionName": "ImageCollection.load","arguments": {"id": {"constantValue": "MODIS/006/MOD09GA"}}}},"value": {"constantValue": 101}}}})
    """
    return PRODUCT_SPECS[productID]["collection"].set("product_id", productID)

getSpectralBands(productID)

Retorna um dicionario com os nomes das bandas de uma determinada região espectrais

Parameters:

Name Type Description Default
productID int

Identificação do produto espectral.

required

Returns:

Type Description
dict

Um dicionário com o nome das bandas espectrais de um produto GEEDaR.

Examples:

>>> getSpectralBands(101)
{'blue': 'sur_refl_b03', 'green': 'sur_refl_b04', 'red': 'sur_refl_b01', 'NIR': 'sur_refl_b02', 'SWIR': 'sur_refl_b06', 'wl490': 'sur_refl_b03', 'wl800': 'sur_refl_b02', 'wl1200': 'sur_refl_b05', 'wl1500': 'sur_refl_b06', 'wl2000': 'sur_refl_b07', 'sur_refl_b01': 'sur_refl_b01', 'sur_refl_b02': 'sur_refl_b02', 'sur_refl_b03': 'sur_refl_b03', 'sur_refl_b04': 'sur_refl_b04', 'sur_refl_b05': 'sur_refl_b05', 'sur_refl_b06': 'sur_refl_b06', 'sur_refl_b07': 'sur_refl_b07'}
Source code in geedar_lib/geedar.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def getSpectralBands(productID:int) -> dict:
    """
    Retorna um dicionario com os nomes das bandas de uma determinada região espectrais

    Args:
        productID: Identificação do produto espectral.

    Returns:
        Um dicionário com o nome das bandas espectrais de um produto GEEDaR.

    Examples:
        >>> getSpectralBands(101)
        {'blue': 'sur_refl_b03', 'green': 'sur_refl_b04', 'red': 'sur_refl_b01', 'NIR': 'sur_refl_b02', 'SWIR': 'sur_refl_b06', 'wl490': 'sur_refl_b03', 'wl800': 'sur_refl_b02', 'wl1200': 'sur_refl_b05', 'wl1500': 'sur_refl_b06', 'wl2000': 'sur_refl_b07', 'sur_refl_b01': 'sur_refl_b01', 'sur_refl_b02': 'sur_refl_b02', 'sur_refl_b03': 'sur_refl_b03', 'sur_refl_b04': 'sur_refl_b04', 'sur_refl_b05': 'sur_refl_b05', 'sur_refl_b06': 'sur_refl_b06', 'sur_refl_b07': 'sur_refl_b07'}
    """
    commonBandsDict = {k: PRODUCT_SPECS[productID]["bandList"][v] for k, v in PRODUCT_SPECS[productID]["commonBands"].items() if v >= 0}
    spectralBandsList = [PRODUCT_SPECS[productID]["bandList"][v] for v in PRODUCT_SPECS[productID]["spectralBandInds"]]
    spectralBandsDict = {k: k for k in spectralBandsList}
    return {**commonBandsDict, **spectralBandsDict}

imageProcessing(algo, productID, dateList, clip=True)

Aplica um algoritmo as coleções de imagens para conseguir os dados espectrais

Source code in geedar_lib/geedar.py
 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
def imageProcessing(algo, productID, dateList, clip = True):
    """
    Aplica um algoritmo as coleções de imagens para conseguir os dados espectrais
    """
    global image_collection
    global bands
    global export_vars, export_bands

    # Adicionado a partir dos objetos globais usados entre as funções
    aoi = None

    # Band dictio/lists:
    bands = getSpectralBands(productID)
    irBands = [bands[band] for band in ["wl740", "wl780", "wl800", "wl900", "wl1200", "wl1500", "wl2000"] if band in bands]
    spectralBands = [PRODUCT_SPECS[productID]["bandList"][i] for i in PRODUCT_SPECS[productID]["spectralBandInds"]]

    # Reference band:
    refBand = PRODUCT_SPECS[productID]["scaleRefBand"]

    # Lists of bands and variables which will be calculated and must be exported to the result data frame.
    export_vars = ["img_time"]
    export_bands = []

    # Filter and prepare the image collection.
    dateMin = dateList[0]
    dateMax = (pd.Timestamp(dateList[-1]) 
               + pd.Timedelta(1, "day")).strftime("%Y-%m-%d")
    image_collection = ee.ImageCollection(
        getCollection(productID)
        ).filterBounds(aoi).filterDate(dateMin, dateMax)
    # Set image date and time (manually set time for Modis products).
    if productID in [101,103,105,111,113,115]:
        image_collection = image_collection.map(
            lambda image: image.set(
                "img_date", ee.Image(image).date().format("YYYY-MM-dd"), 
                "img_time", "10:30"
                ))
    elif productID in [102,104,106,112,114,116]:
        image_collection = image_collection.map(
            lambda image: image.set(
                "img_date", ee.Image(image).date().format("YYYY-MM-dd"), 
                "img_time", "13:30"
                ))
    elif productID in [107,117]:
        image_collection = image_collection.map(
            lambda image: image.set(
                "img_date", ee.Image(image).date().format("YYYY-MM-dd"), 
                "img_time", "12:00"
                ))    
    else:
        image_collection = image_collection.map(
            lambda image: image.set(
                "img_date", ee.Image(image).date().format("YYYY-MM-dd"),
                "img_time", ee.Image(image).date().format("HH:mm")
                ))
    image_collection = image_collection.filter(
        ee.Filter.inList("img_date", dateList)
        )
    sortedCollection = image_collection.sort("img_date")
    imageCollection_list = sortedCollection.toList(5000)
    imgDates = ee.List(sortedCollection.aggregate_array("img_date"))
    distinctDates = imgDates.distinct()
    dateFreq = distinctDates.map(lambda d: imgDates.frequency(d))
    # Function to be mapped to the image list and mosaic same-date images.
    def oneImgPerDate(freq, imgList):
        freq = ee.Number(freq)
        localImgList = ee.List(imgList).slice(0, freq)
        firstImg = ee.Image(localImgList.get(0))
        properties = firstImg.toDictionary(
                firstImg.propertyNames()
            ).remove(["system:footprint"], True)
        proj = firstImg.select(refBand).projection()
        #mosaic = ee.Image(qaMask_collection(productID, ee.ImageCollection(localImgList), True).qualityMosaic("qa_mask").setMulti(properties)).setDefaultProjection(proj).select(firstImg.bandNames())
        mosaic = ee.Image(ee.ImageCollection(localImgList).reduce(
            ee.Reducer.mean()).setMulti(
                properties)).setDefaultProjection(proj).rename(
                    firstImg.bandNames()
                    )
        singleImg = ee.Image(
            ee.Algorithms.If(freq.gt(1), mosaic, firstImg)
            )        
        return ee.List(imgList).splice(0, freq).add(singleImg)
    mosaicImgList = ee.List(dateFreq.iterate(
        oneImgPerDate, imageCollection_list)
        )
    mosaicCollection = ee.ImageCollection(
        mosaicImgList).copyProperties(image_collection)
    image_collection = ee.ImageCollection(
        ee.Algorithms.If(imgDates.length().gt(distinctDates.length()), 
            mosaicCollection, image_collection)
            )

    # Clip the images.
    if clip:
        image_collection = image_collection.map(
            lambda image: ee.Image(image).clip(aoi)
            )

    # Rescale the spectral bands.
    def rescaleSpectralBands(image):
        finalImage = image.multiply(
            PRODUCT_SPECS[productID]["scalingFactor"]
            ).add(PRODUCT_SPECS[productID]["offset"]).copyProperties(image)
        return finalImage

    if (PRODUCT_SPECS[productID]["scalingFactor"] 
        and PRODUCT_SPECS[productID]["offset"]):
        image_collection = image_collection.map(rescaleSpectralBands)

    # Reusable functions:

    # Set the number of unmasked pixels as an image property.
    def nSelecPixels(image):
        scale = image.select(refBand).projection().nominalScale()
        nSelecPixels = image.select(
            refBand).reduceRegion(
                ee.Reducer.count(), aoi
                ).values().getNumber(0)
        return image.set("n_selected_pixels", nSelecPixels)

    # minNDVI clustering: select the cluster with the lowest NDVI.
    def minNDVI(image):

        nClusters = 20
        targetBands = [bands["red"], bands["NIR"]]
        redNIRimage = ee.Image(image).select(targetBands)
        ndviImage = redNIRimage.normalizedDifference([bands["NIR"], bands["red"]])

        # Make the training dataset for the clusterer.
        trainingData = redNIRimage.sample()
        clusterer = ee.Clusterer.wekaCascadeKMeans(2, nClusters).train(trainingData)
        resultImage = redNIRimage.cluster(clusterer)

        # Update the clusters (classes).
        maxID = resultImage.reduceRegion(ee.Reducer.max(), aoi).values().getNumber(0)
        clusterIDs = ee.List.sequence(0, maxID)

        # Pick the class with the smallest NDVI.
        ndviList = clusterIDs.map(
            lambda id: ndviImage.updateMask(
                resultImage.eq(ee.Image(ee.Number(id)))
                ).reduceRegion(
                    ee.Reducer.mean(), aoi
                ).values().getNumber(0)
                )
        minNDVI = ndviList.sort().getNumber(0)
        waterClusterID = ndviList.indexOf(minNDVI)

        return image.updateMask(resultImage.eq(waterClusterID))

    # RICO algorithm.
    def rico(image):
        firstCut = image.updateMask(
                image.select(bands["NIR"]).lt(2000).And(
                    image.select(bands["NIR"]).gte(0)
                    ).And(image.select(bands["red"]).gte(0))
            )
        newRed = firstCut.select(bands["red"]).subtract(
            firstCut.select(bands["NIR"])
            ).unitScale(-500,500).rename("R")
        newGreen = firstCut.select(bands["NIR"]).unitScale(0,2000).rename("G")
        newBlue = firstCut.select(bands["NIR"]).subtract(500).unitScale(0,1500).rename("B")
        redwaterImg = newRed.addBands(newGreen).addBands(newBlue)
        hsvImg = redwaterImg.rgbToHsv()
        waterMask = hsvImg.select("hue").lt(0.08).selfMask()
        value = hsvImg.select("value").updateMask(waterMask)
        valueRef = value.reduceRegion(
            reducer=ee.Reducer.median(), geometry=aoi, bestEffort=True
            ).values().getNumber(0)
        statMask = ee.Image(ee.Algorithms.If(
            valueRef, value.gte(valueRef.multiply(0.95)).And(
                value.lte(valueRef.multiply(1.05))), waterMask
                ))
        waterMask = waterMask.updateMask(statMask)
        hsvImg = hsvImg.updateMask(statMask)
        hue = hsvImg.select("hue")
        saturation = hsvImg.select("saturation")
        trustIndex = saturation.subtract(hue)
        trustIndexRefs = trustIndex.reduceRegion(
            reducer=ee.Reducer.percentile([40,95]), geometry=aoi, 
            bestEffort=True
            ).values()
        trustIndexRef1 = trustIndexRefs.getNumber(0)
        trustIndexRef2 = trustIndexRefs.getNumber(1)
        sunglintMask = ee.Image(ee.Algorithms.If(
                    trustIndexRef1, trustIndex.gte(
                        trustIndexRef1).And(
                            trustIndex.gte(trustIndexRef2.multiply(0.8))
                            ), waterMask
                    ))
        waterMask = waterMask.updateMask(sunglintMask)
        return image.updateMask(waterMask)

    # Statistic filter to remove mixed (outlier) pixels.
    def mod3rStatFilter(image):
        redNIRRatio = image.select(
            bands["red"]).divide(image.select(bands["NIR"]).add(1)
            )
        redNIRRatioRef = redNIRRatio.reduceRegion(
            reducer=ee.Reducer.median(), geometry=aoi
            ).values().getNumber(0)
        statMask = ee.Image(ee.Algorithms.If(
            redNIRRatioRef, redNIRRatio.gte(
                redNIRRatioRef.multiply(0.95)
                ), image.mask())
            )
        return image.updateMask(statMask)

    # Function to calculate a quality flag for Modis images.
    def mod3rQualFlag(image):
        tmpImage = image
        tmpImage.set("qual_flag", 0)
        nSelecPixels = ee.Number(image.get("n_selected_pixels"))
        nValidPixels = ee.Number(image.get("n_valid_pixels"))
        nTotalPixels = ee.Number(image.get("n_total_pixels"))
        scale = image.select(refBand).projection().nominalScale()
        meanVals = image.select(
            [bands["red"], bands["NIR"]]
            ).reduceRegion(ee.Reducer.mean(), aoi).values()
        redMean = meanVals.getNumber(0)
        nirMean = meanVals.getNumber(1)
        convrad = ee.Number(math.pi / 180)

        if productID < 110 or productID in [151,152]:
            vzen = image.select("SensorZenith").reduceRegion(
                reducer=ee.Reducer.mean(), geometry=aoi, scale = scale
                ).getNumber("SensorZenith").divide(100).multiply(convrad)
            szen = image.select("SolarZenith").reduceRegion(
                reducer=ee.Reducer.mean(), geometry=aoi, scale=scale
                ).getNumber("SolarZenith").divide(100).multiply(convrad)
            solaz = image.select("SolarAzimuth").reduceRegion(
                reducer=ee.Reducer.mean(), geometry=aoi, scale=scale
                ).getNumber("SolarAzimuth").divide(100).multiply(convrad)
            senaz = image.select("SensorAzimuth").reduceRegion(
                reducer=ee.Reducer.mean(), geometry=aoi, scale=scale
                ).getNumber("SensorAzimuth").divide(100).multiply(convrad)
            delta = solaz.subtract(senaz)
            delta = ee.Number(ee.Algorithms.If(
                delta.gte(360), delta.subtract(360), delta)
                )
            delta = ee.Number(ee.Algorithms.If(
                delta.lt(0), delta.add(360), delta)
                )
            raz = delta.subtract(180).abs()

        elif productID in range(111,120):
            vzen = image.select("ViewZenith").reduceRegion(
                reducer=ee.Reducer.mean(), geometry=aoi, scale=scale
                ).getNumber("ViewZenith").divide(100).multiply(convrad)
            szen = image.select("SolarZenith").reduceRegion(
                reducer=ee.Reducer.mean(), geometry=aoi, scale=scale
                ).getNumber("SolarZenith").divide(100).multiply(convrad)
            raz = image.select("RelativeAzimuth").reduceRegion(
                reducer=ee.Reducer.mean(), geometry=aoi, scale=scale
                ).getNumber("RelativeAzimuth").divide(100).multiply(convrad)
        sunglint = vzen.cos().multiply(szen.cos()).subtract(
                vzen.sin().multiply(szen.sin()).multiply(raz.cos())
            ).acos().divide(convrad)
        sunglint = sunglint.min(ee.Number(180).subtract(sunglint))
        qual = ee.Number(1).add( \
            nValidPixels.divide(nTotalPixels).lt(0.05) \
            .Or(nSelecPixels.divide(nValidPixels).lt(0.1)) \
            .Or(nSelecPixels.lt(10)) \
        ).add( \
            vzen.divide(convrad).gte(45) \
            .Or(sunglint.lte(25)) \
        ).add( \
            nirMean.gte(1000) \
            .Or(nirMean.subtract(redMean).gte(300)) \
            .add(nirMean.gte(2000).multiply(2)) \
        )
        image = image.set(
            "vzen", vzen.divide(convrad), "sunglint", 
            sunglint, "qual_flag", qual.min(3)
            )

        image = ee.Image(ee.Algorithms.If(
            nSelecPixels.gt(0), image, tmpImage)
            )
        return image;

    # Calculates a quality flag for algorithms that distinguish 
    # the numbers of total, valid, water and selected pixels, 
    # such as the S2WP algorithms.
    def s2wpQualFlag(image):        
        nSelecPixels = ee.Number(image.get("n_selected_pixels"))
        nWaterPixels = ee.Number(image.get("n_water_pixels"))
        nValidPixels = ee.Number(image.get("n_valid_pixels"))
        nTotalPixels = ee.Number(image.get("n_total_pixels"))
        qualFlag = ee.Number(1).add( \
            nValidPixels.divide(nTotalPixels).lt(0.2) \
        ).add( \
            nSelecPixels.divide(nWaterPixels).lt(0.2) \
        ).add( \
            nSelecPixels.divide(nWaterPixels).lt(0.01) \
        ).min(3).multiply(nSelecPixels.min(1))
        return image.set("qual_flag", qualFlag)

    # Calculates a generic quality flag.
    def genericQualFlag(image):        
        nSelecPixels = ee.Number(image.get("n_selected_pixels"))
        nValidPixels = ee.Number(image.get("n_valid_pixels"))
        nTotalPixels = ee.Number(image.get("n_total_pixels"))
        qualFlag = ee.Number(1).add( \
            nValidPixels.divide(nTotalPixels).lt(0.2) \
        ).add( \
            nSelecPixels.divide(nValidPixels).lt(0.1) \
        ).add( \
            nSelecPixels.divide(nValidPixels).lt(0.01) \
        ).min(3).multiply(nSelecPixels.min(1))
        return image.set("qual_flag", qualFlag)

    # Algorithms:

    # 00 is the most simple one. It does nothing to the images.
    if algo == 0:
        pass
    # Simply removes pixels with cloud, cloud shadow or high aerosol.
    if algo == 1:
        image_collection = qaMask_collection(
            productID, image_collection
            )

    # MOD3R and its variations
    elif algo in [2, 3, 4]:
        export_vars = list(set(export_vars).union({
            "n_selected_pixels", "n_valid_pixels", 
            "n_total_pixels", "vzen", 
            "sunglint", "qual_flag"}
            )
        )

        # Set the number of total pixels and remove unlinkely water pixels.
        image_collection = image_collection.map(
            lambda image: ee.Image(image).set(
                    "n_total_pixels", ee.Image(image).select(
                        bands["red"]
                    ).reduceRegion(
                        ee.Reducer.count(), aoi
                    ).values().getNumber(0)
                ).updateMask(ee.Image(image).select(
                    bands["red"]).gte(0).And(
                        ee.Image(image).select(bands["red"]).lt(3000)
                            ).And(ee.Image(image).select(bands["NIR"]).gte(0)
                    )
                )
            )
        # Remove bad pixels (cloud, cloud shadow, high aerosol and 
        # acquisition/processing issues)
        image_collection = qaMask_collection(productID, image_collection)
        # Filter out images with too few valid pixels.
        image_collection = image_collection.map(
            lambda image: ee.Image(image).set(
                "n_valid_pixels", ee.Image(image).select(
                    bands["red"]
                    ).reduceRegion(
                        ee.Reducer.count(), aoi
                    ).values().getNumber(0)
                )
        )
        image_collection_out = ee.ImageCollection(
            image_collection.filterMetadata(
                "n_valid_pixels", "less_than", 10
                ).map(
                    lambda image: ee.Image(image).set(
                            "n_selected_pixels", 0, 
                            "qual_flag", 0
                        ).updateMask(ee.Image(0))
                    )
                )
        image_collection_in = ee.ImageCollection(
            image_collection.filterMetadata(
                "n_valid_pixels", "greater_than", 9
                )
            )

        if algo == 2:
            # MOD3R clusterer/cassifier.
            ## Run k-means with up to 5 clusters and choose the cluster 
            # which most likley represents water.
            ## For such choice, first define the cluster which probably 
            # represents soil or vegetation.
            ## Such cluster is the one with the largest difference 
            # between red and NIR.
            ## Then test every other cluster as a possible water endmember, 
            # choosing the one which yields the smaller error.
            def mod3r(image):
                nClusters = 20
                targetBands = [bands["red"], bands["NIR"]]
                redNIRimage = ee.Image(image).select(targetBands)

                # Make the training dataset for the clusterer.
                trainingData = redNIRimage.sample()
                clusterer = ee.Clusterer.wekaCascadeKMeans(
                    2, nClusters).train(trainingData)
                resultImage = redNIRimage.cluster(clusterer)

                # Update the clusters (classes).
                maxID = ee.Image(resultImage).reduceRegion(
                    ee.Reducer.max(), aoi).values().get(0)
                clusterIDs = ee.List.sequence(0, ee.Number(maxID))

                # Get the mean band values for each cluster.
                clusterBandVals = clusterIDs.map(
                    lambda id: redNIRimage.updateMask(
                        resultImage.eq(ee.Image(ee.Number(id)))
                        ).reduceRegion(ee.Reducer.mean(), aoi)
                    )

                # Get a red-NIR difference list.
                redNIRDiffList = clusterBandVals.map(
                    lambda vals: ee.Number(
                        ee.Dictionary(vals).get(bands["NIR"])
                        ).subtract(
                    ee.Number(ee.Dictionary(vals).get(bands["red"]))
                        )
                    )

                # Pick the class with the greatest difference to be the land endmember.
                greatestDiff = redNIRDiffList.sort().reverse().get(0)
                landClusterID = redNIRDiffList.indexOf(greatestDiff)
                # The other clusters are candidates for water endmembers.
                waterCandidateIDs = clusterIDs.splice(landClusterID, 1)

                # Apply, for every water candidate cluster, an unmix 
                # procedure with non-negative-values constraints.
                # Then choose as water representative the one which 
                # yielded the smaller prediction error.
                landEndmember = ee.Dictionary(clusterBandVals.get(
                        landClusterID
                    )).values(targetBands)
                landEndmember_red = ee.Number(landEndmember.get(0))
                landEndmember_nir = ee.Number(landEndmember.get(1))
                landImage = ee.Image(landEndmember_red).addBands(
                    ee.Image(landEndmember_nir)
                    ).rename(targetBands)
                minError = ee.Dictionary().set(
                        "id", ee.Number(waterCandidateIDs.get(0))
                        ).set("val", ee.Number(2147483647)
                    )

                # Function for getting the best water candidate.
                def pickWaterCluster(id, errorDict):
                    candidateWaterEndmember = ee.Dictionary(
                        clusterBandVals.get(ee.Number(id))
                        ).values(targetBands)
                    candidateWaterEndmember_red = ee.Number(
                        candidateWaterEndmember.get(0)
                        )
                    candidateWaterEndmember_nir = ee.Number(
                        candidateWaterEndmember.get(1)
                        )
                    candidateWaterImage = ee.Image(
                        candidateWaterEndmember_red
                        ).addBands(
                        ee.Image(candidateWaterEndmember_nir)
                        ).rename(targetBands)
                    otherCandidatesIDs = waterCandidateIDs.splice(
                        ee.Number(id), 1
                        )
                    def testCluster(otherID, accum):
                        maskedImage = redNIRimage.updateMask(
                            resultImage.eq(ee.Number(otherID))
                            )
                        fractions = maskedImage.unmix([
                            landEndmember, candidateWaterEndmember], 
                            True, True
                            )
                        predicted = landImage.multiply(
                            fractions.select("band_0")
                            ).add(candidateWaterImage.multiply(
                                fractions.select("band_1"))
                                )
                        return ee.Number(
                            maskedImage.subtract(
                                predicted
                            ).pow(2).reduce(
                                ee.Reducer.sum()
                            ).reduceRegion(
                                ee.Reducer.mean(), aoi
                            ).values().get(0)).add(ee.Number(accum)
                        )
                    errorSum = otherCandidatesIDs.iterate(testCluster, 0)
                    errorDict = ee.Dictionary(errorDict)
                    prevError = ee.Number(errorDict.get("val"))
                    prevID = ee.Number(errorDict.get("id"))
                    newError = ee.Algorithms.If(
                        ee.Number(errorSum).lt(prevError), errorSum, prevError
                        )
                    newID = ee.Algorithms.If(
                        ee.Number(errorSum).lt(prevError), ee.Number(id), prevID
                        )    
                    return errorDict.set(
                        "id", newID).set("val", newError)

                waterClusterID = ee.Number(
                    ee.Dictionary(
                        waterCandidateIDs.iterate(
                            pickWaterCluster, minError)).get("id")
                        )

                # Return the image with non-water clusters masked, 
                # with the clustering result as a band and with the water 
                # cluster ID as a property.
                return image.updateMask(
                    resultImage.eq(
                        ee.Image(waterClusterID))
                        )

        elif algo == 3:
            # minNDVI: a MOD3R modification. Get the lowest-NDVI cluster.
            mod3r = minNDVI

        elif algo == 4:
            # minNIR: a MOD3R modification. Get the lowest-NIR cluster.
            def mod3r(image):
                nClusters = 20
                targetBands = [bands["red"], bands["NIR"]]
                redNIRimage = ee.Image(image).select(targetBands)

                # Make the training dataset for the clusterer.
                trainingData = redNIRimage.sample()
                clusterer = ee.Clusterer.wekaCascadeKMeans(
                    2, nClusters).train(trainingData)
                resultImage = redNIRimage.cluster(clusterer)

                # Update the clusters (classes).
                maxID = resultImage.reduceRegion(ee.Reducer.max(), aoi).values().getNumber(0)
                clusterIDs = ee.List.sequence(0, maxID)

                # Pick the class with the smallest NDVI.
                nirList = clusterIDs.map(
                    lambda id: redNIRimage.select(
                        bands["NIR"]).updateMask(resultImage.eq(
                    ee.Image(ee.Number(id))
                        )
                    ).reduceRegion(
                        ee.Reducer.mean(), aoi
                        ).values().getNumber(0)
                    )
                minNIR = nirList.sort().getNumber(0)
                waterClusterID = nirList.indexOf(minNIR)

                return ee.Image(image).updateMask(
                    resultImage.eq(waterClusterID)
                    )

        # Run the modified MOD3R algorithm and set the quality flag.
        image_collection_in = image_collection_in.map(mod3r) \
            .map(mod3rStatFilter) \
            .map(nSelecPixels) \
            .map(mod3rQualFlag)

        # Reinsert the unprocessed images.
        image_collection = ee.ImageCollection(
            image_collection_in.merge(image_collection_out)
            ).copyProperties(image_collection)

    # Ventura 2018 (Açudes)
    elif algo == 5:
        # Remove bad pixels (cloud, cloud shadow, high aerosol and 
        # acquisition/processing issues)
        image_collection = qaMask_collection(productID, image_collection)
        # Remove pixels with NIR > 400.
        image_collection = ee.ImageCollection(image_collection).map(
            lambda image: ee.Image(image).updateMask(
                ee.Image(image).select(bands["NIR"]).lte(400).And(
                    ee.Image(image).select(bands["NIR"]).gte(0))
                )
            )

    # Sentinel-2 Water Processing (S2WP) algorithm version 6.
    elif algo in [6, 7, 8]:
        export_vars = list(set(export_vars).union({"n_selected_pixels"}))
        def s2wp6(image):
            blue = image.select(bands["blue"])
            green = image.select(bands["green"])
            nir = image.select(bands["NIR"])
            swir2 = image.select(bands["wl2000"])
            minSWIR = image.select(
                [bands["wl1500"],bands["wl2000"]]
                ).reduce(ee.Reducer.min())
            maxGR = image.select(
                [bands["green"], bands["red"]]
                ).reduce(ee.Reducer.max())
            blueNIRratio = blue.divide(nir)
            b1pred = blue.multiply(1.1470590).add(
                green.multiply(-0.24835489)
                ).add(38.96482)
            vis = image.select([bands["blue"], bands["green"], bands["red"]])
            maxV = vis.reduce(ee.Reducer.max())
            minV = vis.reduce(ee.Reducer.min())
            maxDiffV = maxV.subtract(minV)
            ndwi = image.normalizedDifference([bands["green"], bands["NIR"]])
            ci = image.normalizedDifference([bands["red"], bands["green"]])
            rg = image.select(bands["red"]).divide(image.select(bands["green"]))
            ndwihvt2 = maxGR.addBands(minSWIR).normalizedDifference()
            aeib2 = maxDiffV.subtract(blue)
            aeib1 = maxDiffV.subtract(b1pred)
            nirMaxVratio = nir.divide(maxV)
            predNIRmaxVratioHighR = rg.multiply(1.45589130421).exp().multiply(0.0636397716305)
            nirMaxVratioDevHighR = nirMaxVratio.subtract(predNIRmaxVratioHighR)

            image = image.updateMask(ndwihvt2.gte(0).And(minSWIR.lt(420)))
            darkAndInterW = maxDiffV.lt(250) \
                .And(nir.lt(300)) \
                .And(aeib2.gte(-450)) \
                .And(maxDiffV.gte(120) \
                .And(swir2.lt(125)) \
                .Or(ci.lt(0.08).And(swir2.lt(60)))) \
                .And(ndwihvt2.gte(0.78) \
                    .Or(aeib2.subtract(ci.polynomial(
                        [-151.17, -359.17])).abs().lt(80)))
            darkW = darkAndInterW.And(maxDiffV.lt(120))
            interW = darkAndInterW.And(maxDiffV.gte(120).And(maxDiffV.lt(250)))
            brightW = interW.Or(maxDiffV.gte(220) \
              .And(aeib2.gte(-350)) \
              .And(ndwihvt2.gte(0.4)) \
              .And(nirMaxVratioDevHighR.lt(0.5)) \
              .And(ndwi.gte(-0.1).Or(maxDiffV.gte(420).And(ci.gte(0.3).Or(maxDiffV.gte(715))))) \
              .And(ci.lt(0.23).And(aeib2.gte(
                ci.polynomial([-881.33, 5266.7])
                )).Or(ci.gte(0.23).And(aeib2.gte(ci.polynomial([519.41, -823.53]))))) 
              .And( \
                ci.lt(-0.35).And(ndwihvt2.gte(0.78).Or(aeib1.gte(-5)).Or(blueNIRratio.gte(4))) \
                .Or(ci.gte(-0.35).And(ci.lt(-0.2)).And(
                    ndwihvt2.gte(0.78).Or(aeib1.gte(-15)).Or(blueNIRratio.gte(5)))) \
                .Or(ci.gte(-0.2).And(ci.lt(0.3)).And(ndwihvt2.gte(0.78).Or(aeib1.gte(0)))) \
                .Or(ci.gte(0.3).And(aeib2.gte(220))) \
              ) \
            )
            # Bright + dark waters:
            if algo == 6:
                image = image.updateMask(darkW.Or(brightW))
            # Only bright waters:
            elif algo == 7:
                image = image.updateMask(brightW)
            # Only dark waters:
            elif algo == 8:
                image = image.updateMask(darkW)
            return image.set("n_selected_pixels", image.select(
                bands["red"]).reduceRegion(
                    ee.Reducer.count(), aoi).values().get(0))
        image_collection = image_collection.map(s2wp6)

    # Sentinel-2 Water Processing (S2WP) algorithm versions 7 and 8.
    elif algo in [9,10,12]:
        export_vars = list(set(export_vars).union({
                "n_selected_pixels", "n_valid_pixels", 
                "n_total_pixels", "n_water_pixels", 
                "qual_flag"}
                )
            )

        # Set the total number of pixels in the aoi as an image property:
        def totalPixels(image):
            scale = image.select(refBand).projection().nominalScale()       
            return image.set(
                "n_total_pixels", image.select(refBand).reduceRegion(
                    ee.Reducer.count(), aoi, scale
                    ).values().getNumber(0)
                    )
        image_collection = image_collection.map(totalPixels)

        # Mask clouds and set the number of valid (non-cloudy) pixels.
        def validPixels(image):        
            vis = image.select([
                bands["blue"], bands["green"], bands["red"]]
                )
            maxV = vis.reduce(ee.Reducer.max())
            minV = vis.reduce(ee.Reducer.min())
            maxDiffV = maxV.subtract(minV)
            atmIndex = maxDiffV.subtract(minV)            
            # Exclude cloud pixels (it will inadvertedly pick very bright pixels):
            validPixels = atmIndex.gte(-1150)
            image = image.updateMask(validPixels)
            scale = image.select(refBand).projection().nominalScale()       
            nValidPixels = image.select(refBand).reduceRegion(
                ee.Reducer.count(), aoi, scale
                ).values().getNumber(0)
            return image.set("n_valid_pixels", nValidPixels)
        image_collection = image_collection.map(validPixels)       

        # Select potential water pixels.
        def waterPixels(image):
            swir1 = image.select(bands["wl1500"])
            swir2 = image.select(bands["wl2000"])
            ndwihvt = image.select(bands["green"]).max(
                        image.select(bands["red"])
                    ).addBands(swir2).normalizedDifference()
            waterMask = ndwihvt.gte(0).And(swir1.lt(680))
            scale = image.select(refBand).projection().nominalScale()       
            nWaterPixels = waterMask.reduceRegion(
                ee.Reducer.count(), aoi, scale
                ).values().getNumber(0)
            return image.updateMask(waterMask).set("n_water_pixels", nWaterPixels)            
        image_collection = image_collection.map(waterPixels)

        # Remove border (spectrally mixed) pixels (only work for Sentinel-2).
        # "B8" in bands.values() and "B8A" in bands.values():
        if productID in [201,151,152]: 
            if productID == 201:
                def maskBorder(image):
                    smi = image.normalizedDifference(["B8","B8A"])
                    return image.updateMask(smi.abs().lt(0.2))

            else:
                def maskBorder(image):
                    smi = image.select("I3").divide(image.select("M10"))
                    return image.updateMask(smi.lte(1))

            image_collection = image_collection.map(maskBorder)

        if algo == 9:
            # More appropriate for Sentinel-2 and Landsat:
            nir_thr = 2000
            blue_thr = 2000
            ndwihvt_thr_bright = 0.2
            ndwi_thr_dark = -0.15
            maxOffset = 30

        elif algo == 10:
            # More appropriate for MODIS:
            nir_thr = 1500
            blue_thr = 800
            ndwihvt_thr_bright = 0.4
            ndwi_thr_dark = 0
            maxOffset = 0

        # Algorithm - version 7.
        def s2wp7(image):
            swir2 = image.select(bands["wl2000"])
            vnir = image.select([
                bands["blue"], bands["green"], 
                bands["red"], bands["NIR"]]
                )
            offset = vnir.reduce(ee.Reducer.min()).min(0).abs()
            blue = image.select(bands["blue"]).add(offset)
            green = image.select(bands["green"]).add(offset)
            red = image.select(bands["red"]).add(offset)
            nir = image.select(bands["NIR"]).add(offset)
            vnir_offset = blue.addBands(green).addBands(red).addBands(nir)
            vis = vnir_offset.select(
                [bands["blue"], bands["green"], 
                bands["red"]]
                )
            minV = vis.reduce(ee.Reducer.min())
            maxV = vis.reduce(ee.Reducer.max())
            maxDiffV = maxV.subtract(minV)
            ci = vnir_offset.normalizedDifference(
                [bands["red"], bands["green"]]
                )
            ndwi = vnir_offset.normalizedDifference(
                [bands["green"], bands["NIR"]]
                )
            ngbdi = vnir_offset.normalizedDifference(
                [bands["green"], bands["blue"]]
                )
            ndwihvt = green.max(red).addBands(swir2).normalizedDifference()
            # An index helpful to detect clouds (+ bright pixels), cirrus and aerosol:
            saturationIndex = maxDiffV.subtract(minV)            
            # CI-Saturation Index curves.
            curveCI_SI1 = ci.polynomial([-370, -800])
            curveCI_SI2 = -290
            curveCI_SI3 = ci.polynomial([-378.57, 1771.4])
            # A visible-spectrum-based filter which removes pixels strongly 
            # affected by aerosol, sungling and cirrus.
            saturationFilter = saturationIndex.gte(curveCI_SI1).And(
                saturationIndex.gte(curveCI_SI2)).And(
                saturationIndex.gte(curveCI_SI3))
            # CI-NDWI curves to detect sunglint and cirrus:
            curveHighR1a = ci.polynomial([0.745, 0.575])
            curveHighR1b = ci.polynomial([0.3115, -1.5926])
            curveHighR1c = ci.polynomial([0.4158, -3.0833])
            curveLowR1 = ci.polynomial([-0.3875, -2.9688])            
            # A visible & infrared filter for sunglint, cirrus and dark land pixels.
            # The filter is applied separately to low and high reflectance pixels.
            multiFilter = maxV.gte(200).And(
                            ndwihvt.gte(ndwihvt_thr_bright).And(
                                ndwi.gte(0.6).Or(
                                    ndwi.gte(curveHighR1a)).Or(
                                        ndwi.gte(curveHighR1b)
                                        ).Or(ndwi.gte(curveHighR1c)
                                             ).Or(ngbdi.gte(0.25).And(
                                                ndwihvt.gte(0.7)
                                                )))
                            ).Or(maxV.lt(250).And(
                                ndwi.gte(ndwi_thr_dark).And(
                                    ndwi.gte(curveLowR1)).Or(
                                        ndwi.gte(0.6)))
                                        )
            # "Good" water pixels:
            waterMask = saturationFilter.And(
                multiFilter).And(
                nir.lt(nir_thr)).And(
                blue.lt(blue_thr)).And(
                offset.lte(maxOffset)
                ).selfMask()
            # Filter shadow by comparing each pixel to the median of the 
            # area of interest.
            # It must be applied to a small water surface area so to 
            # avoid shadow misclassification due to heterogeneity.
            shadowFilter = waterMask
            indicator = maxV.updateMask(waterMask)
            indicator_ref = indicator.reduceRegion(
                reducer = ee.Reducer.median(), geometry = aoi, 
                bestEffort = True
                ).values().getNumber(0)
            proportionToRef = indicator.divide(indicator_ref);
            shadowFilter = ee.Image(ee.Algorithms.If(
                indicator_ref, proportionToRef.gte(0.8), shadowFilter)
                )
            waterMask = waterMask.updateMask(shadowFilter)
            return image.updateMask(waterMask)

        # Algorithm - version 8.2
        def s2wp8(image):
            # Bands and indices:
            blue = image.select(bands["blue"])
            green = image.select(bands["green"])
            red = image.select(bands["red"])
            nir = image.select(bands["NIR"])
            swir2 = image.select(bands["wl2000"])
            vis = image.select([
                bands["blue"], bands["green"], bands["red"]]
                )
            minV = vis.reduce(ee.Reducer.min())
            maxV = vis.reduce(ee.Reducer.max())
            maxDiffV = maxV.subtract(minV)
            ci = image.normalizedDifference([bands["red"],bands["green"]])
            ndwihvt = green.max(red).addBands(swir2).normalizedDifference()
            # Remove negative-reflectance pixels.
            ndwihvt = ndwihvt.updateMask(minV.gte(0).And(nir.gte(0)))
            # Atmospheric Index (for detection of cloud, cirrus and aerosol).
            atmIndex2 = green.subtract(blue.multiply(2))
            # Filter pixels affected by glint, cirrus or aerosol.
            atm2ndwihvtMask = atmIndex2.gte(ndwihvt.multiply(-500).add(100))
            # "Good" water pixels:
            waterMask = atm2ndwihvtMask.And(ndwihvt.gte(0.6)).selfMask()
            # Filter shaded pixels statistically. For it to work 
            # properly, the water must be homogeneous.
            shadowFilter = waterMask
            indicator = maxV.updateMask(waterMask)
            indicator_ref = indicator.reduceRegion(
                    reducer = ee.Reducer.median(), 
                    geometry = aoi, bestEffort = True
                ).values().getNumber(0)
            proportionToRef = indicator.divide(indicator_ref)
            shadowFilter = ee.Image(ee.Algorithms.If(
                indicator_ref, proportionToRef.gte(0.5), shadowFilter)
                )
            # Final mask:
            waterMask = waterMask.updateMask(shadowFilter)
            return image.updateMask(waterMask)

        if algo in [9,10]:
            image_collection = image_collection.map(s2wp7)

        elif algo == 12:
            image_collection = image_collection.map(s2wp8)

        # Set the final number of pixels as an image property:
        def selecPixels(image):
            scale = image.select(refBand).projection().nominalScale()
            return image.set(
                "n_selected_pixels", image.select(refBand)
                    .reduceRegion(
                        ee.Reducer.count(), aoi, scale
                        ).values().getNumber(0)
                        )
        image_collection = image_collection.map(selecPixels)
        image_collection = image_collection.map(s2wpQualFlag)         # Quality flag.

    # RICO (Red In Cyan Out)
    elif algo == 11:
        if(productID < 200 and not productID in [103,104,113,114]):
            export_vars = list(set(export_vars).union({
                "n_selected_pixels", "n_valid_pixels", 
                "n_total_pixels", "vzen", 
                "sunglint", "qual_flag"}
                    )
                )
        else:
            export_vars = list(set(export_vars).union({
                    "n_selected_pixels", "n_valid_pixels", 
                    "n_total_pixels", "qual_flag"}
                    )
                )
        # Set the number of total pixels.
        image_collection = image_collection.map(
            lambda image: image.set(
            "n_total_pixels", image.select(refBand).reduceRegion(
                ee.Reducer.count(), aoi).values().getNumber(0)
                )
            )
        # Mask bad pixels.
        image_collection = qaMask_collection(productID, image_collection)
        # Set the number of valid (remainging) pixels.
        image_collection = image_collection.map(
            lambda image: image.set(
                "n_valid_pixels", image.select(refBand).reduceRegion(
                ee.Reducer.count(), aoi).values().getNumber(0)
                )
            )
        # Apply the algorithm.
        image_collection = image_collection.map(rico).map(nSelecPixels)
        # Filter images with no good pixels.
        image_collection_out = ee.ImageCollection(
            image_collection.filterMetadata(
                "n_selected_pixels", "less_than", 1
                ).map(lambda image: ee.Image(image).set(
                    "n_selected_pixels", 0, 
                    "qual_flag", 0).updateMask(ee.Image(0))
                )
            )
        image_collection_in = ee.ImageCollection(
            image_collection.filterMetadata("n_selected_pixels", "greater_than", 0)
            )
        # Quality flag:
        if(productID < 200 and not productID in [103,104,113,114]):
            image_collection_in = image_collection_in.map(mod3rQualFlag)

        else:
            image_collection_in = image_collection_in.map(
                genericQualFlag
                )
        # Reinsert the unprocessed images.
        image_collection = ee.ImageCollection(
            image_collection_in.merge(image_collection_out)
            ).copyProperties(image_collection)

    # minNDVI + Wang et al. 2016
    elif algo == 13:
        if(productID < 200 and not productID in [103,104,113,114]):
            export_vars = list(set(export_vars).union({
                "n_selected_pixels", "n_valid_pixels", 
                "n_total_pixels", "vzen","sunglint", 
                "qual_flag"}
                )
            )

        else:
            export_vars = list(set(export_vars).union({
                "n_selected_pixels", "n_valid_pixels", 
                "n_total_pixels", "qual_flag"}
                ))        

        # Set the number of total pixels and remove unlinkely water pixels.
        image_collection = image_collection.map( \
            lambda image: ee.Image(image) \
                .set("n_total_pixels", ee.Image(image).select(refBand)
                     .reduceRegion(ee.Reducer.count(), aoi)
                     .values().getNumber(0)) \
                .updateMask( \
                    ee.Image(image).select(bands["red"]).gte(0) \
                    .And(ee.Image(image).select(bands["red"]).lt(3000)) \
                    .And(ee.Image(image).select(bands["NIR"]).gte(0)) \
                ) \
        )
        # Remove bad pixels (cloud, cloud shadow, high aerosol and acquisition/processing issues)
        image_collection = qaMask_collection(productID, image_collection)
        # Filter out images with too few valid pixels.
        image_collection = image_collection.map(
            lambda image: ee.Image(image) \
                .set("n_valid_pixels", ee.Image(image)
                     .select(refBand).reduceRegion(
                        ee.Reducer.count(), aoi).values().getNumber(0)
                        )
        )
        image_collection_out = ee.ImageCollection(
            image_collection.filterMetadata(
                "n_valid_pixels", "less_than", 10).map(
                    lambda image: ee.Image(image).set(
                        "n_selected_pixels", 0, "qual_flag", 0
                        ).updateMask(ee.Image(0)))
                        )
        image_collection_in = ee.ImageCollection(
            image_collection.filterMetadata(
                "n_valid_pixels", "greater_than", 9)
            )

        # Clustering.
        image_collection_in = image_collection_in.map(minNDVI)

        def wang2016(image):
            allBands = image.bandNames()
            noCorrBands = allBands.removeAll(ee.List(spectralBands))
            image = image.updateMask(image.select(spectralBands)
                                     .reduce(ee.Reducer.min()).gte(0)) # Mask negative pixels
            minIR = image.select(irBands).reduce(ee.Reducer.min())
            vis = image.select([bands["blue"], bands["green"], bands["red"]])
            minV = vis.reduce(ee.Reducer.min())
            maxV = vis.reduce(ee.Reducer.max())
            image = image.updateMask(minV.gte(minIR))
            corrImage = image.select(
                spectralBands).subtract(minIR).rename(spectralBands)
            finalImage = ee.Image(corrImage.addBands(
                image.select(noCorrBands)).copyProperties(image)
                )
            return finalImage
        image_collection_in = image_collection_in.map(wang2016).map(nSelecPixels)

        # Update the separate collections:
        image_collection_out = ee.ImageCollection(
            image_collection_out.merge(ee.ImageCollection(
                image_collection_in.filterMetadata(
                    "n_selected_pixels", "less_than", 1)
                    ).map(
                lambda image: ee.Image(image).set(
                    "n_selected_pixels", 0, "qual_flag", 0).updateMask(
                        ee.Image(0))
                        ).copyProperties(image_collection)
                    )
                )
        image_collection_in = ee.ImageCollection(
            image_collection_in.filterMetadata(
            "n_selected_pixels", "greater_than", 0)
            )     

        # Quality flag:
        if(productID < 200 and not productID in [103,104,113,114]):
            image_collection_in = image_collection_in.map(
                mod3rQualFlag
                )

        else:
            image_collection_in = image_collection_in.map(
                genericQualFlag
                )

        # Reinsert the unprocessed images.
        image_collection = ee.ImageCollection(
            image_collection_in.merge(image_collection_out)
            ).copyProperties(image_collection)

    # GPM daily precipitation
    elif algo == 14:
        export_vars = list(set(export_vars).union(
            {"n_selected_pixels", "area"})
            )
        area = aoi.area()
        image_collection = image_collection.map(
            nSelecPixels).map(
            lambda image: ee.Image(image).set("area", ee.Number(area))
            )

    #---

    # If not already added, add the final number of pixels selected 
    # by the algorithm as an image propoerty.
    if not "n_selected_pixels" in export_vars:
        export_vars.append("n_selected_pixels")
        image_collection = image_collection.map(
            lambda image: image.set(
                "n_selected_pixels", image.select(refBand).reduceRegion(
                    ee.Reducer.count(), aoi, 
                    image.select(
                    refBand).projection().nominalScale()
                    ).values().getNumber(0)
                )
            )

listAvailableProducts()

Retorna uma lista com todos os produtos de satelites disponíveis.

Returns:

Type Description
list

Uma lista com os produtos de satélites disponíveis

Examples:

>>> listAvailableProducts()
[101, 102, 103, 104, 105, 106, 107, 111, 112, 113, 114, 115, 116, 117, 151, 152, 201, 202, 301, 302, 303, 311, 312, 313, 314, 315, 901]
Source code in geedar_lib/geedar.py
34
35
36
37
38
39
40
41
42
43
44
45
def listAvailableProducts() -> list:
    """
    Retorna uma lista com todos os produtos de satelites disponíveis.

    Returns:
        Uma lista com os produtos de satélites disponíveis

    Examples:
        >>> listAvailableProducts()
        [101, 102, 103, 104, 105, 106, 107, 111, 112, 113, 114, 115, 116, 117, 151, 152, 201, 202, 301, 302, 303, 311, 312, 313, 314, 315, 901]
    """
    return AVAILABLE_PRODUCTS

listEstimationAlgos()

Retorna a lista de algorítmos de estimação (inversão).

Returns:

Type Description
list

Uma lista com os algoritmos de processamento disponíveis.

Examples:

>>> listEstimationAlgos()
[0, 1, 2, 3, 4, 5, 10, 11, 12, 99]
Source code in geedar_lib/geedar.py
64
65
66
67
68
69
70
71
72
73
74
75
def listEstimationAlgos() -> list:
    """
    Retorna a lista de algorítmos de estimação (inversão).

    Returns:
        Uma lista com os algoritmos de processamento disponíveis.

    Examples:
        >>> listEstimationAlgos()
        [0, 1, 2, 3, 4, 5, 10, 11, 12, 99]
    """
    return ESTIMATION_ALGO_LIST

listProcessingAlgos()

Retorna uma lista com os algoritmos de processamento disponíveis.

Returns:

Type Description
list

Uma lista com os algoritmos de processamento disponíveis.

Examples:

>>> listProcessingAlgos()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Source code in geedar_lib/geedar.py
49
50
51
52
53
54
55
56
57
58
59
60
def listProcessingAlgos() -> list:
    """
    Retorna uma lista com os algoritmos de processamento disponíveis.

    Returns:
        Uma lista com os algoritmos de processamento disponíveis.

    Examples:
        >>> listProcessingAlgos()
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
    """
    return IMG_PROC_ALGO_LIST

qaMask_collection(productID, imageCollection, addBand=False)

Retorna a uma coleção de imagens baseada na definição de qualidade pixel determinada pelo usuário.

Source code in geedar_lib/geedar.py
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
def qaMask_collection(productID:int, imageCollection:ee.ImageCollection, addBand:bool = False):
    """
    Retorna a uma coleção de imagens baseada na definição de qualidade pixel
    determinada pelo usuário.
    """
    qaLayerName = PRODUCT_SPECS[productID]["qaLayer"]

    if qaLayerName == "" or qaLayerName == []:

        if addBand:
            return ee.ImageCollection(imageCollection).map(
                lambda image: image.addBands(
                    ee.Image(1).rename("qa_mask"))
                    )

        else:
            return ee.ImageCollection(imageCollection)

    # MODIS bands 1-7 (Terra and Aqua)
    if productID in range(100, 120):
        qaLayer = [qaLayerName[0], qaLayerName[0], qaLayerName[0]]
        startBit = [0, 6, 8]
        endBit = [2, 7, 9]
        testExpression = ["b(0) == 0", "b(0) < 2", "b(0) == 0"]

    # Sentinel-2 L2A
    elif productID == 201:
        qaLayer = [qaLayerName[0]]
        startBit = [0]
        endBit = [7]
        testExpression = ["b(0) >= 4 && b(0) <= 7"]

    # Sentinel-2 L1C
    elif productID == 202:
        qaLayer = [qaLayerName[0]]
        startBit = [10]
        endBit = [11]
        testExpression = ["b(0) == 0"]

    # Landsat 5 and 7 SR Collection 1
    elif productID in [301,302]:
        qaLayer = [qaLayerName[0]]
        startBit = [3]
        endBit = [5]
        testExpression = ["b(0) == 0"]

    # Landsat 8 SR Collection 1
    elif productID in [303]:
        qaLayer = [qaLayerName[0],qaLayerName[1]]
        startBit = [3,6]
        endBit = [5,7]
        testExpression = ["b(0) == 0", "b(0) <= 1"]

    # Landsat 4, 5 and 7 Level 2 Collection 2
    elif productID in [311,312,313]:
        qaLayer = [qaLayerName[0]]
        startBit = [1]
        endBit = [5]
        testExpression = ["b(0) == 0"]

    # Landsat 8 and 9 Level 2 Collection 2
    elif productID in [314,315]:
        qaLayer = [qaLayerName[0],qaLayerName[1]]
        startBit = [1,6]
        endBit = [5,7]
        testExpression = ["b(0) == 0", "b(0) <= 1"]

    # VIIRS
    elif productID in [151,152]:
        qaLayer = [qaLayerName[0],qaLayerName[1]]
        startBit = [2,3]
        endBit = [4,7]
        testExpression = ["b(0) == 0", "b(0) == 0"]

    else:

        if addBand:
            return ee.ImageCollection(imageCollection).map(
                lambda image: image.addBands(
                    ee.Image(1).rename("qa_mask"))
                    )

        else:
            return ee.ImageCollection(imageCollection)

    maskVals = []
    for i in range(len(startBit)):
        bitToInt = 0
        for j in range(startBit[i], endBit[i] + 1):
            bitToInt = bitToInt + int(math.pow(2, j))
        maskVals.append(bitToInt)

    def qaMask(image):
      mask = ee.Image(1)
      for i in range(len(maskVals)):
        mask = mask.And(image.select(qaLayer[i]).int().bitwiseAnd(
            maskVals[i]
            ).rightShift(startBit[i]).expression(testExpression[i]))
      if addBand:
        image = image.addBands(mask.rename("qa_mask"))
      return image.updateMask(mask);

    return ee.ImageCollection(imageCollection).map(qaMask)

reduction(reducer, productID, aoi=None)

Essa função reduz o valor de cada imagem previamente mascadara em uma coletação aplicando o redutor predefinido

Source code in geedar_lib/geedar.py
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
def reduction(reducer, productID, aoi=None):
    """
    Essa função reduz o valor de cada imagem previamente mascadara em
    uma coletação aplicando o redutor predefinido
    """
    global image_collection
    global ee_reducer

    # Parameters to include in the result data frame:
    paramList = ee.List(export_vars)   

    def getParamVals(image, result):
        return ee.Dictionary(result).set(
            ee.Image(image).get("img_date"), ee.Dictionary.fromLists(
                paramList, paramList.map(
                    lambda paramName: ee.Image(image).get(ee.String(paramName))
                    )
                )
            )   
    first = ee.Dictionary()
    paramDict = ee.Dictionary(
        ee.ImageCollection(image_collection).iterate(getParamVals, first))

    if reducer == 0:
        return paramDict

    else:
        if reducer == 1:
            ee_reducer = ee.Reducer.median()

        elif reducer == 2:
            ee_reducer = ee.Reducer.mean()

        elif reducer == 3:
            ee_reducer = ee.Reducer.mean().combine(
                reducer2=ee.Reducer.stdDev(),sharedInputs=True
                )

        elif reducer == 4:
            ee_reducer = ee.Reducer.minMax()

        elif reducer == 5:
            ee_reducer = ee.Reducer.count()

        elif reducer == 6:
            ee_reducer = ee.Reducer.sum()

        elif reducer == 7:
            ee_reducer = ee.Reducer.median() \
                .combine(reducer2 = ee.Reducer.mean(), sharedInputs = True) \
                .combine(reducer2 = ee.Reducer.stdDev(), sharedInputs = True) \
                .combine(reducer2 = ee.Reducer.minMax(), sharedInputs = True)

    band = PRODUCT_SPECS[productID]["scaleRefBand"]

    # Combine the dictionaries of parameters and of band values.
    def combDicts(key, subDict):
        return ee.Dictionary(
            subDict).combine(ee.Dictionary(paramDict).get(key)
                             )

    successful = False
    timeoutcounts = 0
    tileScale = 1

    for c in range(3):

        def reduce(image, result):
            scale = image.select(band).projection().nominalScale()
            return ee.Dictionary(result).set(
                ee.Image(image).get("img_date"), ee.Image(image).reduceRegion(
                    reducer=ee.Reducer(ee_reducer), geometry = aoi, 
                    scale=scale, bestEffort = True, 
                    tileScale=tileScale
                    )
                )
        #first = ee.Dictionary()
        bandDict = ee.Dictionary(
            ee.ImageCollection(image_collection).iterate(reduce, first)
            )

        try:
            result = bandDict.map(combDicts).getInfo()
            successful = True
            #if c > 0:
            #print("Successful retrieval.")
            break

        except Exception as e:
            print("(!)")
            print(e)
            if str(e) == "Computation timed out.":
                if c < 2:
                    print("Trying again...")                    
                timeoutcounts = timeoutcounts + 1
                if(timeoutcounts >= 2):
                    # On the second failure for computation timeout, process images one by one:
                    localDateList = image_collection.aggregate_array("img_date").getInfo()
                    if len(localDateList) > 1:
                        print("This time processing images one by one:")                    
                        result = ee.Dictionary()
                        for localDate in localDateList:
                            localImageCollection = image_collection.filterDate(
                                localDate, (pd.Timestamp(localDate) 
                                            + pd.Timedelta(1, "day")
                                            ).strftime("%Y-%m-%d")
                                            )
                            #first = ee.Dictionary()
                            paramDict = ee.Dictionary(
                                ee.ImageCollection(localImageCollection).iterate(getParamVals, first)
                                )
                            bandDict = ee.Dictionary(
                                ee.ImageCollection(localImageCollection).iterate(reduce, first)
                                )
                            localResult = bandDict.map(combDicts)
                            print(localDate + ": ", end = '')
                            try:
                                result = ee.Dictionary(result).combine(localResult).getInfo()
                                print("successful retrieval.")
                                successful = True
                            except:
                                print("Failed.")
                        break
            elif str(e)[:40] == "Output of image computation is too large":
                if c < 2:
                    print("Trying with a different tileScale parameter: " 
                          + str(tileScale) + "...")
                    tileScale = tileScale * 2
                else:
                    print("Failed.")
            else:
                if c < 2:
                    print("Trying again in 30 seconds...")
                    sleep(30)
                else:
                    print("Failed.")

    if not successful:
        return

    reducedBands = list({*bands.values()}) + export_bands
    sufix = REDUCTION_SPECS[reducer]["sufix"][0]

    if len(REDUCTION_SPECS[reducer]["sufix"]) == 1:
        for k1 in result:
            for k2 in [*result[k1]]:
                if k2 in reducedBands:
                    result[k1][k2 + "_" + sufix] = result[k1].pop(k2)
    #print("Successful retrieval.")
    return result

specificDatesRetrieval(date_col=0, id_col=1, lat_col=2, long_col=3, running_mode=1, input_dir='', aoi_mode='kml', append_mode=False, max_n_proc_pixels=25000, estimation_algos=[0] * 6, reducers=[1] * 6, img_proc_algos=[10, 10, 9, 9, 9, 9], aoi_radius=1000, product_ids=[101, 102, 301, 302, 303, 201], processing_codes=[10110001, 10210001, 30109001, 30209001, 30309001, 20109001])

Recupera dados no modo específico de datas

Source code in geedar_lib/geedar.py
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
def specificDatesRetrieval(
        date_col:int = 0, 
        id_col:int = 1, 
        lat_col:int = 2, 
        long_col:int = 3,  
        running_mode:int = 1,
        input_dir:str = "", 
        aoi_mode:str = 'kml',
        append_mode:bool = False,
        max_n_proc_pixels:int = 25000,
        estimation_algos:list = [0]*6,
        reducers:list = [1]*6,
        img_proc_algos:list = [10,10,
                               9,9,9,9], 
        aoi_radius:int = 1000, 
        product_ids:list = [101,102,301,
                            302,303,201], 
        processing_codes:list = [10110001,10210001,30109001,
                                                  30209001,30309001,20109001]
        ):
    """
    Recupera dados no modo específico de datas
    """

    #global image_collection

    global aoi, export_bands, export_vars
    global input_df
    global time_window

    nProcCodes = len(processing_codes)
    export_bands = []
    export_vars = []

    if running_mode == 2:
        time_window = 0
        print("Converting the date-range format to the specific-dates format...")
        toSpecificDatesDF()

    print("Checking data in the input file...")

    # Data frame attributes:
    colnames = [c.lower() for c in [*input_df.columns]]
    nrows = input_df.shape[0]
    ncols = input_df.shape[1]

    # Check if the data frame has enough rows and columns:
    if nrows < 1:
        print("(!)")
        raise Exception(
            "The input CSV file must have a header row and at least one data row.")

    if ncols < 3 and aoi_mode != "kml":
        print("(!)")
        raise Exception(
            "The input CSV file must have a header and at least three columns "
            + "(date, lat, long), unless you are defining your sites trough KML "
            + "files (option -k), in which case the minimum required columns are"
            + " 'date' and 'id'.")

    if ncols < 2 and aoi_mode == "kml":
        print("(!)")
        raise Exception(
            "If you choose to define the regions of interest trough KML files,"
            + "the input CSV file must include, at least, the columns 'date'"
            +" and 'id'. The KML files' names must be equal to the corresponding"
            +" 'id' plus the extension '.kml' and the files must be in the same "
            + "folder as the CSV file.")

    # Update, if possible, the index of the "date" column.
    try:
        date_col = colnames.index("date")
    except ValueError:
        pass

    # Check the date values:
    try:
        pdDates = pd.to_datetime(input_df.iloc[:,date_col])
        input_df.iloc[:, date_col] = pd.Series(pdDates).dt.date
    except:
        print("(!)")
        raise Exception(
            "The date column in the input file must have valid date values "
            + "in the format yyyy-mm-dd.")

    # Update, if possible, the index of the (site) "id" column.
    try:
        id_col = colnames.index("id")
    except ValueError:
        if ncols < 4 and aoi_mode != "kml":
            id_col = -1
            lat_col = lat_col - 1
            long_col = long_col - 1
    else:
        if ncols < 4 and aoi_mode != "kml":
            print("(!)")
            raise Exception(
                "The input CSV file must include, at least, the columns "
                + " 'date', 'lat' and 'long', unless you define your sites "
                + "of interest through kml files (option -k), in which case the"
                + "'date' and 'id' columns are enough.") 

    # Update, if possible, the index of the lat column.
    try:
        lat_col = colnames.index("lat")
    except ValueError:
        if aoi_mode == "kml":
            lat_col = -1

    # Update, if possible, the index of the long column.
    try:
        long_col = colnames.index("long")
    except ValueError:
        if aoi_mode == "kml":
            long_col = -1

    # Unknown id column?
    if id_col == date_col or id_col == lat_col or id_col == long_col:
        if aoi_mode == "kml":
            print("(!)")
            raise Exception(
                "The column containing the sites' name could not be identified." 
                + " Please, name it as 'id'.")
        else:
            id_col = -1

    # Check the lat/long values:
    if aoi_mode != "kml":
        if (not pd.api.types.is_numeric_dtype(input_df.iloc[:, lat_col])
            ) or ((not pd.api.types.is_numeric_dtype(input_df.iloc[:, long_col]))):
            print("(!)")
            raise Exception(
                "'lat' and 'long' values in the input file must be in decimal degrees.")

    # Get the indices of the valid rows (no NaN nor None):
    if aoi_mode == "kml":
        validIDs = input_df.iloc[:,id_col].notna()
        validLats = True
        validLongs = True

    else:
        validIDs = True
        validLats = input_df.iloc[:,lat_col].notna()
        validLongs = input_df.iloc[:,long_col].notna()

    validRows = which(input_df.iloc[:,date_col].notna() 
                      & validLats & validLongs & validIDs)

    if len(validRows) < 1:
        print("(!)")
        raise Exception(
            "The input CSV file has no valid rows (rows with no missing data).")

    # Results' data frame template:
    resultDF_template = input_df.copy()
    # Add the adjacents dates according to the time window.
    nrows_result = nrows

    if time_window != 0:
        print("Expanding the input data to meet the time_window parameter (" 
              + str(time_window) + ")...")
        window_size = 1 + (time_window * 2)
        nrows_tmp = len(validRows) * window_size + (nrows - len(validRows))
        tmpDF = pd.DataFrame(index=range(nrows_tmp), columns=resultDF_template.columns)
        imgDate = pd.Series(index=range(nrows_tmp), name="img_date", dtype="float64")
        row_j = 0
        validRows_new = []

        for row_i in range(nrows):
            if row_i in validRows:
                date_j = pd.Timestamp(
                    resultDF_template.iloc[row_i, date_col]
                    ) - pd.Timedelta(time_window, "day")

                for window_i in range(window_size):
                    validRows_new.append(row_j)
                    tmpDF.iloc[row_j] = resultDF_template.iloc[row_i]
                    imgDate[row_j] = date_j.date()
                    date_j = date_j + pd.Timedelta(1, "day")
                    row_j = row_j + 1

            else:            
                tmpDF.iloc[row_j] = resultDF_template.iloc[row_i]
                row_j = row_j + 1

        tmpDF.insert(date_col + 1, "img_date", imgDate)
        ncols = ncols + 1
        date_col = date_col + 1

        if date_col <= id_col:
            id_col = id_col + 1

        if date_col <= lat_col:
            lat_col = lat_col + 1

        if date_col <= long_col:
            long_col = long_col + 1   

        resultDF_template = tmpDF.copy()        
        nrows_result = nrows_tmp
        validRows = validRows_new

    # Get the unique site IDs:
    if id_col >= 0:
        siteSeries = resultDF_template.iloc[:,id_col].astype(str)

    else:
        siteSeries = pd.Series(
            [str([*resultDF_template.iloc[:, lat_col]][i]) 
            + str([*resultDF_template.iloc[:, long_col]][i]) 
            for i in range(nrows_result)]
            )

    siteList = siteSeries.iloc[validRows].unique().tolist()

    # Result dictionary.
    resultDFs_dictio = {}

    for code_i in range(nProcCodes):
        processingCode = processing_codes[code_i]
        resultDFs_dictio[processingCode] = pd.DataFrame(
            data=None, index=range(nrows_result))

    # Data retrieval grouped by GEEDaR product and by site.
    print("Processing started at " + str(pd.Timestamp.now()) + ".")
    dataRetrieved = False

    for site in siteList:
        print("")
        print("[Site] " + str(site))
        targetRows = [i for i in which(siteSeries == site) if i in validRows]
        dateList = [*pd.to_datetime(
            resultDF_template.iloc[targetRows, date_col].sort_values()
            ).dt.strftime("%Y-%m-%d").unique()]
        aoi = None

        if aoi_mode == "kml":
            kmlFile = ""
            searchPath1 = os.path.join(input_dir, site + ".kml")
            searchPath2 = os.path.join(input_dir, "KML", site + ".kml")

            if os.path.isfile(searchPath1):
                kmlFile = searchPath1

            elif os.path.isfile(searchPath2):
                kmlFile = searchPath2

            if kmlFile == "":
                print("(!) File " 
                      + kmlFile + " was not found. The site was ignored.")

            else:
                coords = polygonFromKML(kmlFile)
                if coords != []:
                    aoi = ee.Geometry.MultiPolygon(coords)
                else:
                    print("(!) A polygon could not be extracted from the file " 
                          + kmlFile + ". The site was ignored.")

        else:                
            # Check if lat/long coordinates are the same for the site.
            lats = [*resultDF_template.iloc[targetRows, lat_col]]
            firstLat = lats[0]
            longs = [*resultDF_template.iloc[targetRows, long_col]]
            firstLong = longs[0]

            if (not all(i == firstLat for i in lats)
                ) or (not all(i == firstLong for i in longs)):
                print("(!) Coordinates were not all the same. The first pair was used.")
            # Define the region of interest.
            aoi = ee.Geometry.Point(coords = [firstLong, firstLat]).buffer(aoi_radius)

        if not aoi is None:
            # If more than one processing code was provided, run one by one.
            for code_i in range(nProcCodes):
                processingCode = processing_codes[code_i]
                productID = product_ids[code_i]
                imgProcAlgo = img_proc_algos[code_i]
                estimationAlgo = estimation_algos[code_i]
                reducer = reducers[code_i]
                print("\n(" + str(processingCode) + ")")

                if not productID in IMG_PROC_ALGO_SPECS[
                    imgProcAlgo]["applicableTo"]:
                    print("(!) The image processing algorithm #" 
                          + str(imgProcAlgo) 
                          + " is not applicable to the product " 
                          + str(productID) 
                          + ". This data demand was ignored.")
                    continue

                # Get the available dates.
                tmpDateList = getAvailableDates(productID, dateList)
                availableDates = [d for d in dateList if d in tmpDateList]                
                #if not len(availableDates) == 0:
                #    availableDates = list(set(availableDates.sort()))
                nAvailableDates = len(availableDates)
                if nAvailableDates == 0:
                    print("No available data.")
                elif append_mode:
                    # Get common band names (e.g. 'red', 'blue', etc.).
                    commonBandNames = [k for k,v in PRODUCT_SPECS[productID][
                        "commonBands"].items() if v >= 0]
                    commonBandInds = [PRODUCT_SPECS[productID][
                        "commonBands"][k] for k in commonBandNames]
                    realBandNames = [PRODUCT_SPECS[productID][
                        "bandList"][i] for i in commonBandInds]
                    #commonBandsDictio = {PRODUCT_SPECS[productID]["bandList"][v]:k for k,v in PRODUCT_SPECS[productID]["commonBands"].items() if v >= 0 and k in commonBandNames}

                # Divide the request in groups to avoid exceeding GEE capacity.
                # First, calculate the number of pixels in the region of interest.
                # Then determine the number of images which correspond to a total of 100 000 pixels.
                nPixelsInAoI = aoi.area().divide(math.pow(
                    PRODUCT_SPECS[productID]["roughScale"], 2)).getInfo()
                maxNImgs = math.ceil(max_n_proc_pixels/nPixelsInAoI)
                group_len = min(maxNImgs, IMG_PROC_ALGO_SPECS[imgProcAlgo]["nSimImgs"])
                nGroups = math.ceil(nAvailableDates / group_len)

                for g in range(nGroups):
                    dateSublist_inds = range(g * group_len, min(
                        g * group_len + group_len, nAvailableDates))
                    dateSublist = [availableDates[i] for i in dateSublist_inds]
                    print("Requesting data for days " 
                          + str(g * group_len + 1) 
                          + "-" + str(min(g * group_len + group_len, nAvailableDates)) 
                          + "/" + str(nAvailableDates) + "...")
                    # Image processing, parameter estimation and reduction.
                    imageProcessing(imgProcAlgo, productID, dateSublist)
                    estimation(estimationAlgo, productID)
                    result = reduction(reducer, productID)

                    if result is None:
                        print("(!) Failed to retrieve data.")

                    elif result == {}:
                        print("No data retrieved.")

                    else:
                        dataRetrieved = True
                        # Save the retrieved data in the result data frame.

                        for date in [*result]:
                            sameDateRows = [i for i in which(
                                resultDF_template.iloc[:,date_col
                                                       ].astype("str") == date) if i in targetRows]

                            for band in [*result[date]]:
                                colNames = []
                                if append_mode:
                                    for i in range(len(commonBandNames)):
                                        if realBandNames[i] + "_" in band:
                                            colNames.append(band.replace(
                                                realBandNames[i], commonBandNames[i]))
                                elif nProcCodes > 1:
                                    colNames = [str(processingCode) + "_" + band]
                                if len(colNames) == 0:
                                    colNames = [band]
                                for row_i in sameDateRows:
                                    for colName in colNames:
                                        resultDFs_dictio[processingCode].loc[row_i, colName] = result[date][band]
                        print("Data successfully retrieved.")

    print("Processing finished at " + str(pd.Timestamp.now()) + ".")

    if dataRetrieved:
        print("Consolidating results...")
        resultDF_template.reset_index(inplace = True, drop = True)

        if append_mode:
            # Get all column names.
            cols = []
            for k in resultDFs_dictio:
                cols.extend([*resultDFs_dictio[k].columns])
            cols = set(cols)
            commonBandNames = [*PRODUCT_SPECS[101]["commonBands"].keys()]
            # Reorder columns.

            for k in resultDFs_dictio:
                tmpDF = pd.DataFrame()
                for col in cols:
                    wosuffix = col.split("_")[0]                    
                    if not wosuffix in commonBandNames:
                        if col in [*resultDFs_dictio[k].columns]:
                            tmpDF[col] = resultDFs_dictio[k][col]
                        else:
                            tmpDF[col] = math.nan
                tmpDF = tmpDF.reindex(sorted(tmpDF.columns), axis=1)

                for band in commonBandNames:
                    matches = [col for col in cols if (band + "_") in col]
                    for col in matches:
                        if col in [*resultDFs_dictio[k].columns]:
                            tmpDF[col] = resultDFs_dictio[k][col]
                        else:
                            tmpDF[col] = math.nan
                dataColNames = tmpDF.columns
                resultDFs_dictio[k] = tmpDF
                prodID = int(str(k)[0:3])
                sensor = PRODUCT_SPECS[prodID]["sensor"]
                resultDFs_dictio[k] = pd.concat([
                    pd.DataFrame({"ProcCode": [k] * nrows_result, 
                                  "Source": [sensor] * nrows_result}), 
                                  resultDFs_dictio[k]], axis = 1, sort = False)
                resultDFs_dictio[k] = pd.concat([
                    resultDF_template, resultDFs_dictio[k]], 
                    axis = 1, sort = False)

                if(running_mode == 2):
                    resultDFs_dictio[k].dropna(
                        subset = dataColNames, how = "all", inplace = True)
            resultDF = pd.concat([*resultDFs_dictio.values()], sort = False)
        else:
            dataDF = pd.concat([*resultDFs_dictio.values()], axis = 1, sort = False)
            resultDF = pd.concat([resultDF_template, dataDF], axis = 1, sort = False)
            # Remove empty rows (if in running mode 2):
            if(running_mode == 2):
                resultDF.dropna(subset = dataDF.columns, how = "all", inplace = True)
    else:
        resultDF = None

    return resultDF

toSpecificDatesDF(input_df)

Essa função converte uma série temporal em datas específicas em um data frame

Source code in geedar_lib/geedar.py
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
def toSpecificDatesDF(input_df):
    """
    Essa função converte uma série temporal em datas específicas em um data frame
    """
    product_ids = [101,102,301,302,303,201]
    colnames = [c.lower() for c in [*input_df.columns]]

    for i in colnames:
        print(i, type(i))


    if (all(col in colnames for col in ["lat", "long", 
                                        "start_date", "end_date"]
        ) or all(col in colnames for col in ["id", "start_date", 
                                             "end_date"])
        ):
        print("(!)")
        raise Exception(
            "The input CSV file should have the columns 'start_date', 'end_date' and 'id' or 'lat' and 'long'."
            )

    startDate_col = colnames.index("start_date")
    endDate_col = colnames.index("end_date")

    exportColumns = []

    # ID:
    try:
        id_col = colnames.index("id")
    except:
        pass
    else:
        exportColumns.append(id_col)

    # Lat/Long:
    try:
        lat_col = colnames.index("lat")
        long_col = colnames.index("long")
    except:
        pass
    else:
        exportColumns.extend([lat_col, long_col])

    nrows = input_df.shape[0]
    tmpList = []

    for row_i in range(nrows):
        nDates = 0
        try:
            # Get the optimal start date, discarding the dates of the period 
            # before the beginning of the sensor operation.
            userStartDateStr = input_df.iloc[row_i, startDate_col]

            if (not isinstance(userStartDateStr, str)
                ) or (userStartDateStr.lower() == "auto"
                      ) or (userStartDateStr.replace(" ", "") == ""):
                userStartDateStr = "1960-01-01"

            userStartDate = pd.to_datetime(userStartDateStr).date()
            earliestSensorDate = pd.to_datetime('today').date()
            for prodID in product_ids:
                collectionStartDate = pd.to_datetime(
                        PRODUCT_SPECS[prodID]["startDate"]
                    ).date()
                earliestSensorDate = min(earliestSensorDate, collectionStartDate)
            optimalStartDate = max(userStartDate, earliestSensorDate)
            dates = [*pd.Series(pd.date_range(
                optimalStartDate, pd.to_datetime(input_df.iloc[row_i, endDate_col])
                )).astype("str")]            
            nDates = len(dates)
        except:
            pass

        if not nDates > 0:
            print("(!) Could not interpret the date range defined by 'start_date' and 'end_date' in row #" 
                  + str(row_i + 1) + " of the input CSV file. The row was ignored.")
            continue

        tmpDF = pd.DataFrame({"date": dates})
        for c in exportColumns:
            tmpDF[input_df.columns[c]] = input_df.iloc[row_i, c]
        tmpList.append(tmpDF)

    input_df = pd.concat(tmpList)