def main(cmdargs):
    scn_csv = cmdargs.scn_list
    outdir = cmdargs.outdir

    repo_ch_list = ["GOOGLE", "AWS"]
    landsat_obj_dict = {}
    for repo in repo_ch_list:
        if repo == "GOOGLE":
            scft_id_list = ["LANDSAT_5", "LANDSAT_7", "LANDSAT_8"]
        elif repo == "AWS":
            scft_id_list = ["LANDSAT_8"]
        else:
            scft_id_list = []
        for scft in scft_id_list:
            landsat_obj_dict[(repo, scft)] = Landsat(scft, repo)

    n_good = 0
    n_bad = 0

    chunksize = int(1e3)
    for scn_df in pd.read_csv(scn_csv, usecols=[0, 1], chunksize=chunksize):
        for idx, row in enumerate(scn_df.itertuples()):
            scft_id = scnIdToSpacecraftId(row[1])
            if scft_id is None:
                logger.error(
                    ("Scene ID {0:s} cannot be parsed to " +
                     "Landsat spacecraft ID, and will be skipped.").format(
                         row[1]))
                n_bad += 1
                continue
            repo_ch = indexUrlToBucketId(row[2])
            if repo_ch is None:
                logger.error(("Scene index url {0:s} cannot be parsed " +
                              "to public data repository channel name, " +
                              "and will be skipped.").format(row[2]))
                n_bad += 1
                continue

            landsat_obj = landsat_obj_dict[(repo_ch, scft_id)]
            if landsat_obj.addScene(row[1], row[2]) is None:
                logger.error(
                    "Adding scene {0:s} to the download queue failed, and will be skipped."
                    .format(row[1]))
                n_bad += 1
                continue
            logger.info("Saving scene {0:s} started.".format(row[1]))
            landsat_obj.saveToDir(outdir)
            landsat_obj.clearScenes()
            n_good += 1

    logger.info("{0:d} scenes saved to {1:s}".format(n_good, outdir))
    if n_bad > 0:
        logger.warning("{0:d} scenes failed to download.".format(n_bad))
def main():
    """ Tests de la classe et de ses méthodes.
    """

    # secteur3
    b1 = r'data/CU_LC08.001_SRB1_doy2020133_aid0001.tif'
    b2 = r'data/CU_LC08.001_SRB2_doy2020133_aid0001.tif'
    b3 = r'data/CU_LC08.001_SRB3_doy2020133_aid0001.tif'
    b4 = r'data/CU_LC08.001_SRB4_doy2020133_aid0001.tif'
    b5 = r'data/CU_LC08.001_SRB5_doy2020133_aid0001.tif'
    b6 = r'data/CU_LC08.001_SRB6_doy2020133_aid0001.tif'
    b7 = r'data/CU_LC08.001_SRB7_doy2020133_aid0001.tif'
    qa = r'data/CU_LC08.001_PIXELQA_doy2020133_aid0001.tif'

    landsat = Landsat(b1, b2, b3, b4, b5, b6, b7, qa, "appeears")

    lst = r'data/MOD11A1.006_LST_Day_1km_doy2020133_aid0001.tif'
    qa = r'data/MOD11A1.006_QC_Day_doy2020133_aid0001.tif'
    modis = Modis(lst, qa)

    # reprojection de l'image MODIS de départ en UTM18
    modis.reprojectModisSystem('EPSG:32618', '-9999.0', '1000.0', 'average')

    mnt = r'data/ASTGTM_NC.003_ASTER_GDEM_DEM_doy2000061_aid0001.tif'
    qa = r'data/ASTGTM_NUMNC.003_ASTER_GDEM_NUM_doy2000061_aid0001.tif'
    aster = Aster(mnt, qa)

    secteur3 = Secteur(modis, landsat, aster)
    secteur3.prepareData(train_model=True)

    rfr = ReductionEchelle(secteur3)

    # tests de combinaisons de prédicteurs
    #predictors = ['NDVI', 'NDWI', 'NDBI', 'MNDWI', 'SAVI', 'Albedo', 'BSI', 'UI', 'EVI', 'IBI', 'MNT']
    #predictors = ['MNT', 'B7', 'B6', 'B1', 'B2', 'BSI', 'MNDWI']  ## Not bad!
    predictors = ['NDVI', 'MNT']
    #predictors = ['MNT', 'Albedo', 'MNDWI', 'BSI', 'B1', 'B2']
    #predictors = ['NDVI', 'NDWI', 'MNT']
    rfr.applyDownscaling(predictors,
                         outputFile=r'data3/MODIS_predit_100m.tif',
                         residualCorrection=True,
                         outputFile_withResidualCorrection=
                         r'data/MODIS_predit_100m_avec_residus.tif')
Esempio n. 3
0
def main():
    """ Tests de la classe et de ses méthodes.
    """

    # secteur1
    b1 = r'27juin2019/CU_LC08.001_SRB1_doy2019178_aid0001.tif'
    b2 = r'27juin2019/CU_LC08.001_SRB2_doy2019178_aid0001.tif'
    b3 = r'27juin2019/CU_LC08.001_SRB3_doy2019178_aid0001.tif'
    b4 = r'27juin2019/CU_LC08.001_SRB4_doy2019178_aid0001.tif'
    b5 = r'27juin2019/CU_LC08.001_SRB5_doy2019178_aid0001.tif'
    b6 = r'27juin2019/CU_LC08.001_SRB6_doy2019178_aid0001.tif'
    b7 = r'27juin2019/CU_LC08.001_SRB7_doy2019178_aid0001.tif'
    qa = r'27juin2019/CU_LC08.001_PIXELQA_doy2019178_aid0001.tif'
    landsat = Landsat(b1, b2, b3, b4, b5, b6, b7, qa)

    lst = r'27juin2019/MOD11A1.006_LST_Day_1km_doy2019178_aid0001.tif'
    qa = r'27juin2019/MOD11A1.006_QC_Day_doy2019178_aid0001.tif'
    # test)
    modis = Modis(lst, qa)

    mnt = r'data/ASTGTM_NC.003_ASTER_GDEM_DEM_doy2000061_aid0001.tif'
    qa = r'data/ASTGTM_NUMNC.003_ASTER_GDEM_NUM_doy2000061_aid0001.tif'  # aussi un test (ne semble pas valide)

    aster = Aster(mnt, qa)

    rfr = Secteur(modis, landsat, aster)
    rfr.prepareData()

    predictors = ['NDVI', 'NDWI', 'NDBI', 'B1']
    df = rfr.getDf(predictors)

    print(df)
    print(df.drop('LST', axis=1))
    print(df['LST'])

    a = df['LST']
    print(a.ravel())
Esempio n. 4
0
            t_high, r'secteur3/PBIM_100m_result.tif')


if __name__ == "__main__":

    # load landsat image (bands 1-7 + qa)
    # secteur3
    b1 = r'secteur3/landsat8/CU_LC08.001_SRB1_doy2020133_aid0001.tif'
    b2 = r'secteur3/landsat8/CU_LC08.001_SRB2_doy2020133_aid0001.tif'
    b3 = r'secteur3/landsat8/CU_LC08.001_SRB3_doy2020133_aid0001.tif'
    b4 = r'secteur3/landsat8/CU_LC08.001_SRB4_doy2020133_aid0001.tif'
    b5 = r'secteur3/landsat8/CU_LC08.001_SRB5_doy2020133_aid0001.tif'
    b6 = r'secteur3/landsat8/CU_LC08.001_SRB6_doy2020133_aid0001.tif'
    b7 = r'secteur3/landsat8/CU_LC08.001_SRB7_doy2020133_aid0001.tif'
    qa = r'secteur3/landsat8/CU_LC08.001_PIXELQA_doy2020133_aid0001.tif'
    landsat = Landsat(b1, b2, b3, b4, b5, b6, b7, qa)

    lst = r'secteur3/modis/MOD11A1.006_LST_Day_1km_doy2020133_aid0001.tif'
    qa = r'secteur3/modis/MOD11A1.006_QC_Day_doy2020133_aid0001.tif'
    modis = Modis(lst, qa)

    # reprojection de l'image MODIS de départ en UTM18
    modis.reprojectModisSystem('EPSG:32618', '-9999.0', '1000.0', 'average')

    # subdivise en pixels de 100m
    modis.subdividePixel(10, "file",
                         modis.lst.split(".")[0] + '_subdivided_100m.tif')

    mnt = r'secteur3/aster/ASTGTM_NC.003_ASTER_GDEM_DEM_doy2000061_aid0001.tif'
    qa = r'secteur3/aster/ASTGTM_NUMNC.003_ASTER_GDEM_NUM_doy2000061_aid0001.tif'
    aster = Aster(mnt, qa)
def downscale():
    """ Permet d'effectuer la réduction d'échelle. Même principe que l'exécution à partir du fichier main pour le
        programme principal.

        On utilise MOD11_L2 et des images Landsat 8 provenant de EarthData qui ont été acquises à des heures très
        rapproches la même journée pour s'assurer d'avoir des températures de surface assez similaires (comparables).

        *** Il faut au préalable découper l'image MOD11_L2 pour qu'elle soit plus petite ou de même taille que les
            images Landsat et Aster afin que le découpage/alignement/rééchantillonnage de Landsat et Aster par rapport
            à l'image de référence Modis s'effectue correctement.
            Ce découpage peut être fait facilement dans QGIS selon l'étendue d'un shapefile ou une zone entrée
            manuellement.
        ***
    """

    # 19 mai 2020
    # données Landsat
    b1 = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_B1.TIF'
    b2 = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_B2.TIF'
    b3 = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_B3.TIF'
    b4 = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_B4.TIF'
    b5 = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_B5.TIF'
    b6 = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_B6.TIF'
    b7 = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_B7.TIF'
    qa = r'data/LC08_L1TP_014028_20200706_20200721_01_T1_BQA.TIF'
    #src = "appeears"
    src = "earthdata"
    landsat = Landsat(b1, b2, b3, b4, b5, b6, b7, qa, src)

    # données Modis
    lst = r'data/MOD11_L2.clipped_test2.tif'  # image Modis découpée
    qa = r'data/MOD11A1.006_QC_Day_doy2020133_aid0001.tif'  # bande inutile pour la validation externe
    modis = Modis(lst, qa)

    # reprojection de l'image MODIS de départ en UTM18
    modis.reprojectModisSystem('EPSG:32618', 'np.nan', '1000.0', 'average')

    # données Aster
    mnt = r'data/ASTGTM_NC.003_ASTER_GDEM_DEM_doy2000061_aid0001.tif'
    qa = r'data/ASTGTM_NUMNC.003_ASTER_GDEM_NUM_doy2000061_aid0001.tif'
    aster = Aster(mnt, qa)

    # construction de l'objet Secteur
    secteur3 = Secteur(modis, landsat, aster)
    secteur3.prepareData(train_model=True)

    # construction de l'objet ReductionEchelle à partir du secteur
    rfr = ReductionEchelle(secteur3)

    # prédicteurs

    # choix de prédicteurs sous forme de liste
    # options possibles : 'NDVI', 'NDWI', 'NDBI', 'MNDWI', 'SAVI', 'Albedo', 'BSI', 'UI', 'EVI', 'IBI', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'MNT', 'Pente', 'Orientation'
    predictors = ['NDWI', 'NDWI', 'NDBI']

    # réduction d'échelle
    rfr.applyDownscaling(
        predictors,
        outputFile=r'data/MODIS_predit_100m.tif',
        residualCorrection=True,
        outputFile_withResidualCorrection=
        r'data/MODIS_predit_100m_avec_residus.tif',
        targetResolution=100)  # validation externe seulement possible à 100m
def main(cmdargs):
    prd_csv = cmdargs.prd_list
    spacecraft_id = "LANDSAT_{0:s}".format(cmdargs.spacecraft_id)
    repo_channel = cmdargs.channel.upper()
    outfile = cmdargs.outfile
    index_db_name = cmdargs.index_db

    if (repo_channel == "GOOGLE"):
        _colnames = dict(product_id="PRODUCT_ID",
                         scene_id="SCENE_ID",
                         c_number="COLLECTION_NUMBER",
                         sc_id="SPACECRAFT_ID",
                         wrs_path="WRS_PATH",
                         wrs_row="WRS_ROW",
                         acq_date="SENSING_TIME",
                         url="BASE_URL")
    elif (repo_channel == "AWS"):
        _colnames = dict(product_id="productId",
                         scene_id="entityId",
                         c_number=None,
                         sc_id=None,
                         wrs_path="path",
                         wrs_row="row",
                         acq_date="acquisitionDate",
                         url="download_url")
    else:
        raise RuntimeError(
            "Accessing the data in the channel {0:s} not implemented!".format(
                repo_channel))

    landsat_obj = Landsat(spacecraft_id, repo_channel, index_db_name)
    out_header = [
        "scene_id", "url", "product_id", "wrs_path", "wrs_row", "acq_date"
    ]
    n_found = 0
    chunksize = int(1e3)

    with open(outfile, "w") as out_fobj:
        out_fobj.write(",".join(out_header))
        out_fobj.write("\n")
        for prd_df in pd.read_csv(prd_csv,
                                  parse_dates=[2, 3],
                                  chunksize=chunksize):
            for idx, row in enumerate(prd_df.itertuples()):
                # for i in gc.get_objects():
                #     before[type(i)] += 1

                logger.info(
                    "Searching scenes for path = {0:d}, row = {1:d} started.".
                    format(row[1], row[2]))

                tmp = landsat_obj.searchPathRow(
                    row[1],
                    row[2],
                    start_date=row[3].strftime("%Y-%m-%d"),
                    end_date=row[4].strftime("%Y-%m-%d"))
                if len(tmp) == 0:
                    logger.warning(
                        ("No scenes found for " + "path = {0:d}, row = {1:d}" +
                         " between {2:s} and {3:s}").format(
                             row[1], row[2], row[3].strftime("%Y-%m-%d"),
                             row[4].strftime("%Y-%m-%d")))
                else:
                    tmp = tmp.loc[:, [_colnames[k] for k in out_header]]
                    tmp.columns = out_header
                    tmp.to_csv(out_fobj, index=False, header=False, mode="a")

                n_found = n_found + len(tmp)

                # for i in gc.get_objects():
                #     after[type(i)] += 1
                # diff = [(k, after[k] - before[k]) for k in after if after[k] - before[k]]
                # print idx, diff
                logger.info(
                    "Memory = {0:d} at path = {1:d}, row = {2:d}".format(
                        proc.memory_info().rss, row[1], row[2]))

    logger.info("{0:d} scenes found from {1:s}.".format(n_found, repo_channel))
def main():
    """ Tests de la classe et de ses méthodes.
        ****** Exécuter ce fichier pour effectuer le downscaling ******
    """

    # secteur1
    """
    b1 = r'secteur/CU_LC08.001_SRB1_doy2020229_aid0001.tif'
    b2 = r'secteur/CU_LC08.001_SRB2_doy2020229_aid0001.tif'
    b3 = r'secteur/CU_LC08.001_SRB3_doy2020229_aid0001.tif'
    b4 = r'secteur/CU_LC08.001_SRB4_doy2020229_aid0001.tif'
    b5 = r'secteur/CU_LC08.001_SRB5_doy2020229_aid0001.tif'
    b6 = r'secteur/CU_LC08.001_SRB6_doy2020229_aid0001.tif'
    b7 = r'secteur/CU_LC08.001_SRB7_doy2020229_aid0001.tif'
    qa = r'secteur/CU_LC08.001_PIXELQA_doy2020229_aid0001.tif'
    landsat = Landsat(b1, b2, b3, b4, b5, b6, b7, qa)

    lst = r'secteur/MOD11A1.006_LST_Day_1km_doy2020221_aid0001.tif'
    qa = r'secteur/MOD11A1.006_QC_Day_doy2020229_aid0001.tif'  # pas la bonne image, mais juste pour un test, vu que je
                                                               # trouve pas la bonne image (QA n'est pas utilisé dans le
                                                               # test)
    modis = Modis(lst, qa)

    mnt = r'secteur/ASTGTM_NC.003_ASTER_GDEM_DEM_doy2000061_aid0001.tif'
    qa = r'secteur/ASTGTM_NUMNC.003_ASTER_GDEM_NUM_doy2000061_aid0001.tif'  # aussi un test (ne semble pas valide)
    aster = Aster(mnt, qa)

    secteur1 = Secteur(modis, landsat, aster)
    secteur1.prepareData()

    rfr = ReductionEchelle(secteur1)
    rfr.applyDownscaling()
    """

    # *********** TEST FONCTIONNEL **************

    # secteur3
    b1 = r'secteur3/landsat8/CU_LC08.001_SRB1_doy2020133_aid0001.tif'
    b2 = r'secteur3/landsat8/CU_LC08.001_SRB2_doy2020133_aid0001.tif'
    b3 = r'secteur3/landsat8/CU_LC08.001_SRB3_doy2020133_aid0001.tif'
    b4 = r'secteur3/landsat8/CU_LC08.001_SRB4_doy2020133_aid0001.tif'
    b5 = r'secteur3/landsat8/CU_LC08.001_SRB5_doy2020133_aid0001.tif'
    b6 = r'secteur3/landsat8/CU_LC08.001_SRB6_doy2020133_aid0001.tif'
    b7 = r'secteur3/landsat8/CU_LC08.001_SRB7_doy2020133_aid0001.tif'
    qa = r'secteur3/landsat8/CU_LC08.001_PIXELQA_doy2020133_aid0001.tif'
    landsat = Landsat(b1, b2, b3, b4, b5, b6, b7, qa)

    lst = r'secteur3/modis/MOD11A1.006_LST_Day_1km_doy2020133_aid0001.tif'
    qa = r'secteur3/modis/MOD11A1.006_QC_Day_doy2020133_aid0001.tif'
    modis = Modis(lst, qa)

    # reprojection de l'image MODIS de départ en UTM18
    modis.reprojectModisSystem('EPSG:32618', '-9999.0', '1000.0', 'average')

    mnt = r'secteur3/aster/ASTGTM_NC.003_ASTER_GDEM_DEM_doy2000061_aid0001.tif'
    qa = r'secteur3/aster/ASTGTM_NUMNC.003_ASTER_GDEM_NUM_doy2000061_aid0001.tif'
    aster = Aster(mnt, qa)

    secteur3 = Secteur(modis, landsat, aster)
    secteur3.prepareData(train_model=True)

    rfr = ReductionEchelle(secteur3)

    # options fournies:
    #predictors = ['NDVI', 'NDWI', 'NDBI', 'MNDWI', 'SAVI', 'Albedo', 'BSI', 'UI', 'EVI', 'IBI', 'B1', 'B2', 'B3',
    #              'B4', 'B5', 'B6', 'B7', 'MNT', 'Pente']

    #predictors = ['NDVI', 'NDWI', 'NDBI', 'MNDWI', 'SAVI', 'Albedo', 'BSI', 'UI', 'EVI', 'IBI', 'MNT']
    #predictors = ['MNT', 'B7', 'B6', 'B1', 'B2', 'BSI', 'MNDWI']  ## Not bad!
    predictors = ['NDVI', 'MNT']
    #predictors = ['MNT', 'Albedo', 'MNDWI', 'BSI', 'B1', 'B2']
    #predictors = ['NDVI', 'NDWI', 'MNT']
    rfr.applyDownscaling(predictors,
                         outputFile=r'secteur3/MODIS_predit_100m.tif',
                         residualCorrection=True,
                         outputFile_withResidualCorrection=
                         r'secteur3/MODIS_predit_100m_avec_residus.tif')
Esempio n. 8
0
def main():
    #######################################################################
    ###################### paramètres du programme ########################
    #######################################################################

    # Instructions :
    # - Les données doivent tous être directement dans le même dossiers
    # - Modifier seulement les paramètres dans l'entête de la fonction main()

    # données Landsat
    b1 = r'data/CU_LC08.001_SRB1_doy2020133_aid0001.tif'
    b2 = r'data/CU_LC08.001_SRB2_doy2020133_aid0001.tif'
    b3 = r'data/CU_LC08.001_SRB3_doy2020133_aid0001.tif'
    b4 = r'data/CU_LC08.001_SRB4_doy2020133_aid0001.tif'
    b5 = r'data/CU_LC08.001_SRB5_doy2020133_aid0001.tif'
    b6 = r'data/CU_LC08.001_SRB6_doy2020133_aid0001.tif'
    b7 = r'data/CU_LC08.001_SRB7_doy2020133_aid0001.tif'
    qa = r'data/CU_LC08.001_PIXELQA_doy2020133_aid0001.tif'
    # source de données
    #options possibles : "appeears", "earthdata"
    src = "appeears"

    # données Modis
    lst = r'data/MOD11A1.006_LST_Day_1km_doy2020133_aid0001.tif'
    qc = r'data/MOD11A1.006_QC_Day_doy2020133_aid0001.tif'

    # données Aster
    dem = r'data/ASTGTM_NC.003_ASTER_GDEM_DEM_doy2000061_aid0001.tif'
    num = r'data/ASTGTM_NUMNC.003_ASTER_GDEM_NUM_doy2000061_aid0001.tif'

    # predicteurs sous forme de liste
    # options possibles : 'NDVI', 'NDWI', 'NDBI', 'MNDWI', 'SAVI', 'Albedo', 'BSI', 'UI', 'EVI', 'IBI', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'MNT', 'Pente'
    predictors = ['NDWI', 'Pente', 'Orientation']

    # paramètre pour la résolution à laquelle on veut effectuer la réduction d'échelle
    # options possibles : 30, 100
    target_resolution = 100

    # paramètre pour la résolution à laquelle on veut effectuer la réduction d'échelle
    # options possibles : 30, 100
    target_resolution = 30

    # paramètre pour l'application des résidus
    residualCorrection = True

    # paramètre pour la suppresion des fichiers temporaire
    supprimer_les_fichiers_temporaires = True

    #######################################################################
    ######################### fin des paramètres ##########################
    #######################################################################

    # ne plus modifié

    landsat = Landsat(b1, b2, b3, b4, b5, b6, b7, qa, src)
    modis = Modis(lst, qc)
    aster = Aster(dem, num)
    # reprojection de l'image MODIS de départ en UTM18
    modis.reprojectModisSystem('EPSG:32618', '-9999.0', '1000.0', 'average')

    secteur = Secteur(modis, landsat, aster)
    secteur.prepareData(train_model=True)

    rfr = ReductionEchelle(secteur)

    rfr.applyDownscaling(predictors,
                         outputFile=r'data/MODIS_predit_30m.tif',
                         residualCorrection=residualCorrection,
                         outputFile_withResidualCorrection=
                         r'data/MODIS_predit_30m_avec_residus.tif',
                         targetResolution=target_resolution)

    if supprimer_les_fichiers_temporaires:
        delete_temp()