Ejemplo n.º 1
0
def generate_fake_l8_old_data(root_directory: str,
                              tile_name: str,
                              dates: List[str],
                              res: Optional[float] = 30.0):
    """
    Parameters
    ----------
    root_directory : string
        path to generate Sentinel-2 dates
    tile_name : string
        THEIA tile name (ex:T31TCJ)
    dates : list
        list of strings reprensentig dates format : YYYYMMDD
    """

    tile_dir = os.path.join(root_directory, tile_name)
    ensure_dir(tile_dir)

    band_of_interest = ["B1", "B2", "B3", "B4", "B5", "B6", "B7"]
    masks_of_interest = ["DIV", "BINARY_MASK", "NUA", "SAT"]

    origin_x = 566377
    origin_y = 6284029
    array_name = "iota2_binary"
    for date in dates:
        date_dir = os.path.join(
            tile_dir, (f"LANDSAT8_OLITIRS_XS_{date}_N2A_{tile_name}"))
        mask_date_dir = os.path.join(date_dir, "MASK")
        ensure_dir(date_dir)
        ensure_dir(mask_date_dir)
        all_bands = []
        for cpt, mask in enumerate(masks_of_interest):
            new_mask = os.path.join(mask_date_dir,
                                    (f"LANDSAT8_OLITIRS_XS_{date}_N2A"
                                     f"_{tile_name}_{mask}.TIF"))

            array_to_raster(fun_array(array_name) * cpt % 2,
                            new_mask,
                            pixel_size=res,
                            origin_x=origin_x,
                            origin_y=origin_y)
        for band in band_of_interest:
            new_band = os.path.join(date_dir,
                                    (f"LANDSAT8_OLITIRS_XS_{date}_N2A"
                                     f"_{tile_name}_{band}.TIF"))
            all_bands.append(new_band)
            array = fun_array(array_name)
            random_array = []
            for val in array:
                val_tmp = []
                for pix_val in val:
                    val_tmp.append(pix_val * random.random() * 1000)
                random_array.append(val_tmp)

            array_to_raster(np.array(random_array),
                            new_band,
                            pixel_size=res,
                            origin_x=origin_x,
                            origin_y=origin_y)
            stack_date = os.path.join(date_dir, (f"LANDSAT8_OLITIRS_XS_{date}_"
                                                 "N2A_ORTHO_SURF_CORR"
                                                 f"_PENTE_{tile_name}.TIF"))
            stack_app = CreateConcatenateImagesApplication({
                "il": all_bands,
                "out": stack_date
            })
            stack_app.ExecuteAndWriteOutput()
Ejemplo n.º 2
0
    def get_time_series_gapFilling(self, ram=128):
        """
        Due to the SAR data, time series must be split by polarisation
        and orbit (ascending / descending)
        """
        import configparser

        from iota2.Common.FileUtils import getNbDateInTile
        from iota2.Common.OtbAppBank import getSARstack
        from iota2.Common.OtbAppBank import CreateConcatenateImagesApplication
        from iota2.Common.OtbAppBank import CreateImageTimeSeriesGapFillingApplication
        from iota2.Common.OtbAppBank import getInputParameterOutput

        (all_filtered, all_masks, interp_date_files,
         input_date_files) = getSARstack(self.s1_cfg,
                                         self.tile_name,
                                         self.all_tiles.split(" "),
                                         os.path.join(self.i2_output_path,
                                                      "features"),
                                         workingDirectory=None)
        # to be clearer
        s1_data = OrderedDict()
        s1_labels = OrderedDict()

        config = configparser.ConfigParser()
        config.read(self.s1_cfg)

        interpolation_method = "linear"
        if config.has_option("Processing", "gapFilling_interpolation"):
            interpolation_method = config.get("Processing",
                                              "gapFilling_interpolation")
        dependancies = []

        for filtered, masks, interp_dates, in_dates in zip(
                all_filtered, all_masks, interp_date_files, input_date_files):
            sar_mode = os.path.basename(
                filtered.GetParameterValue("outputstack"))
            sar_mode = "_".join(os.path.splitext(sar_mode)[0].split("_")[0:-1])
            polarisation = sar_mode.split("_")[1]
            orbit = sar_mode.split("_")[2]

            gapfilling_orbit_pol_name_masks = f"{self.gapfilling_orbit_pol_name_mask}_{orbit}_{polarisation}.tif"
            gapfilling_raster_mask = os.path.join(
                self.features_dir, "tmp", gapfilling_orbit_pol_name_masks)

            masks_stack = CreateConcatenateImagesApplication({
                "il": masks,
                "out": gapfilling_raster_mask,
                "ram": str(ram)
            })

            if self.write_outputs_flag is False:
                filtered.Execute()
                masks_stack.Execute()
            else:
                filtered_raster = filtered.GetParameterValue(
                    getInputParameterOutput(filtered))
                masks_stack_raster = masks_stack.GetParameterValue(
                    getInputParameterOutput(masks_stack))
                if not os.path.exists(masks_stack_raster):
                    masks_stack.ExecuteAndWriteOutput()
                if not os.path.exists(filtered_raster):
                    filtered.ExecuteAndWriteOutput()
                if os.path.exists(masks_stack_raster):
                    masks_stack = masks_stack_raster
                if os.path.exists(filtered_raster):
                    filtered = filtered_raster
            dependancies.append((filtered, masks_stack))
            gapfilling_orbit_pol_name = f"{self.gapfilling_orbit_pol_name}_{orbit}_{polarisation}.tif"
            gapfilling_raster = os.path.join(self.features_dir, "tmp",
                                             gapfilling_orbit_pol_name)

            gap_app = CreateImageTimeSeriesGapFillingApplication({
                "in":
                filtered,
                "mask":
                masks_stack,
                "it":
                interpolation_method,
                "id":
                in_dates,
                "od":
                interp_dates,
                "comp":
                str(1),
                "out":
                gapfilling_raster
            })
            s1_data[sar_mode] = gap_app

            sar_dates = sorted(getNbDateInTile(interp_dates,
                                               display=False,
                                               raw_dates=True),
                               key=lambda x: int(x))
            labels = [
                "{}_{}_{}_{}".format(self.__class__.name, orbit, polarisation,
                                     date).lower() for date in sar_dates
            ]
            s1_labels[sar_mode] = labels
        return (s1_data, dependancies), s1_labels
Ejemplo n.º 3
0
def generate_fake_s2_l3a_data(root_directory: str,
                              tile_name: str,
                              dates: List[str],
                              res: Optional[float] = 30.0):
    """
    Parameters
    ----------
    root_directory : string
        path to generate Sentinel-2 l3a dates
    tile_name : string
        THEIA tile name (ex:T31TCJ)
    dates : list
        list of strings reprensentig dates format : YYYYMMDD
    """

    tile_dir = os.path.join(root_directory, tile_name)
    ensure_dir(tile_dir)

    band_of_interest = [
        "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B8A", "B11", "B12"
    ]
    masks_of_interest = ["BINARY_MASK", "FLG_R1"]

    origin_x = 566377
    origin_y = 6284029
    array_name = "iota2_binary"
    for date in dates:
        date_dir = os.path.join(tile_dir,
                                ("SENTINEL2X_{}-000000-"
                                 "000_L3A_{}_D_V1-7".format(date, tile_name)))
        mask_date_dir = os.path.join(date_dir, "MASKS")
        ensure_dir(date_dir)
        ensure_dir(mask_date_dir)
        all_bands = []
        for cpt, mask in enumerate(masks_of_interest):
            new_mask = os.path.join(
                mask_date_dir,
                ("SENTINEL2X_{}-000000-000_L3A"
                 "_{}_D_V1-7_{}.tif".format(date, tile_name, mask)))

            array_to_raster(fun_array(array_name) * cpt % 2,
                            new_mask,
                            pixel_size=res,
                            origin_x=origin_x,
                            origin_y=origin_y)
        for band in band_of_interest:
            new_band = os.path.join(
                date_dir,
                ("SENTINEL2X_{}-000000-000_L3A"
                 "_{}_D_V1-7_FRC_{}.tif".format(date, tile_name, band)))
            all_bands.append(new_band)
            array = fun_array(array_name)
            random_array = []
            for val in array:
                val_tmp = []
                for pix_val in val:
                    val_tmp.append(pix_val * random.random() * 1000)
                random_array.append(val_tmp)

            array_to_raster(np.array(random_array),
                            new_band,
                            pixel_size=res,
                            origin_x=origin_x,
                            origin_y=origin_y)
            stack_date = os.path.join(
                date_dir, ("SENTINEL2X_{}-000000-000_L3A_{}_D_V1-7"
                           "_FRC_STACK.tif".format(date, tile_name)))
            stack_app = CreateConcatenateImagesApplication({
                "il": all_bands,
                "out": stack_date
            })
            stack_app.ExecuteAndWriteOutput()