From 1d532c5c6976efd93d2b4d7cbfb23b28b0dd0a7e Mon Sep 17 00:00:00 2001 From: Impact <pascal.mouquet@ird.fr> Date: Mon, 9 Nov 2020 16:27:55 +0400 Subject: [PATCH] updated indices functions to use cm001 as default, utilization of other cloud masks not implemented yet --- sen2chain/indices.py | 160 ++++++++++++++++++------------------------ sen2chain/products.py | 2 + 2 files changed, 71 insertions(+), 91 deletions(-) diff --git a/sen2chain/indices.py b/sen2chain/indices.py index 4775d8b..41cf778 100644 --- a/sen2chain/indices.py +++ b/sen2chain/indices.py @@ -107,8 +107,6 @@ class Ndvi(Indice): out_path=(out_path / self.indice_filename)) if nodata_clouds: - - #~ cm_product = NewCloudMaskProduct(l2a_identifier = self.l2a_product.identifier, version = cm_version) if not self.cm_product.path.exists(): raise ValueError("Cloud mask does not exist") masked_indice_filename = self.indice_stem + "_" + self.cm_product.suffix + self.ext @@ -168,7 +166,6 @@ class Ndvi(Indice): #~ IndiceProduct(identifier = quicklook_filename).init_md() -# A updater cm_product_object class NdwiMcf(Indice): """ NDWI(McFeeters) = (GREEN-NIR) / (GREEN+NIR) @@ -184,11 +181,12 @@ class NdwiMcf(Indice): colormap = cm.colors.LinearSegmentedColormap.from_list("", ["green", "white", "blue"]) - def __init__(self, l2a_product_object): - if l2a_product_object is None: - raise ValueError("A L2aProduct object must be provided") + def __init__(self, l2a_product_object, cm_product_object): + if (l2a_product_object or cm_product_object) is None: + raise ValueError("A L2aProduct and NewCloudMask objects must be provided") else: self.l2a_product = l2a_product_object + self.cm_product = cm_product_object # output path self.out_path = None @@ -207,7 +205,6 @@ class NdwiMcf(Indice): ) -> None: """ process """ self.out_path = out_path - if (out_path / self.indice_filename).exists() and not reprocess: logger.info("{} already exists".format(self.indice_filename)) else: @@ -218,20 +215,20 @@ class NdwiMcf(Indice): out_path=(out_path / self.indice_filename)) if nodata_clouds: - if self.l2a_product.user_cloud_mask is None: + if not self.cm_product.path.exists(): raise ValueError("Cloud mask does not exist") - masked_indice_filename = self.indice_stem + "_MASKED" + self.ext - masked_indice_raw = self.indice_stem + "_MASKED" + self.ext_raw - + masked_indice_filename = self.indice_stem + "_" + self.cm_product.suffix + self.ext + masked_indice_raw = self.indice_stem + "_" + self.cm_product.suffix + self.ext_raw + if (out_path / masked_indice_filename).exists() and not reprocess: logger.info("{} already exists".format(masked_indice_filename)) else: if (out_path / self.indice_raw).exists(): - ndwimcf_name = (out_path / self.indice_raw) + ndwimcf_path = (out_path / self.indice_raw) else: - ndwimcf_name = (out_path / self.indice_filename) - create_masked_indice(indice_path=ndwimcf_name, - cloud_mask_path=self.l2a_product.user_cloud_mask, + ndwimcf_path = (out_path / self.indice_filename) + create_masked_indice(indice_path=ndwimcf_path, + cloud_mask_path=self.cm_product.path, out_path=(out_path / masked_indice_raw)) index_tiff_2_jp2(img_path=(out_path / masked_indice_raw), out_path=(out_path / masked_indice_filename)) @@ -246,22 +243,18 @@ class NdwiMcf(Indice): if quicklook: cmap = matplotlib_colormap_to_rgb(self.colormap, revers=False) - quicklook_filename = self.indice_stem + "_QUICKLOOK.tif" + quicklook_filename = self.indice_stem + "_" + self.cm_product.suffix + "_QL.tif" + if (self.out_path / quicklook_filename).exists() and not reprocess: logger.info("{} already exists".format(quicklook_filename)) else: logger.info("creating quicklook") -# create_colormap(raster=(self.out_path / self.indice_filename), -# cloud_mask=self.l2a_product.user_cloud_mask, -# lut_dict=cmap, clouds_color="white", -# out_path=(self.out_path / quicklook_filename)) create_rvb(raster=(self.out_path / self.indice_filename), - cloud_mask=self.l2a_product.user_cloud_mask, + cloud_mask=self.cm_product.path, lut_dict=cmap, clouds_color="white", out_path=(self.out_path / quicklook_filename)) -# A updater cm_product_object class NdwiGao(Indice): """ NDWI(Gao) = (NIR-SWIR) / (NIR+SWIR) @@ -275,11 +268,13 @@ class NdwiGao(Indice): ext_raw = ".tif" colormap = cm.RdYlBu - def __init__(self, l2a_product_object): - if l2a_product_object is None: - raise ValueError("A L2aProduct object must be provided") + def __init__(self, l2a_product_object, cm_product_object): + if (l2a_product_object or cm_product_object) is None: + raise ValueError("A L2aProduct and NewCloudMask objects must be provided") else: self.l2a_product = l2a_product_object + self.cm_product = cm_product_object + # output path self.out_path = None @@ -309,20 +304,20 @@ class NdwiGao(Indice): out_path=(out_path / self.indice_filename)) if nodata_clouds: - if self.l2a_product.user_cloud_mask is None: + if not self.cm_product.path.exists(): raise ValueError("Cloud mask does not exist") - masked_indice_filename = self.indice_stem + "_MASKED" + self.ext - masked_indice_raw = self.indice_stem + "_MASKED" + self.ext_raw - + masked_indice_filename = self.indice_stem + "_" + self.cm_product.suffix + self.ext + masked_indice_raw = self.indice_stem + "_" + self.cm_product.suffix + self.ext_raw + if (out_path / masked_indice_filename).exists() and not reprocess: logger.info("{} already exists".format(masked_indice_filename)) else: if (out_path / self.indice_raw).exists(): - ndwi_name = (out_path / self.indice_raw) + ndwigao_name = (out_path / self.indice_raw) else: - ndwi_name = (out_path / self.indice_filename) - create_masked_indice(indice_path=ndwi_name, - cloud_mask_path=self.l2a_product.user_cloud_mask, + ndwigao_name = (out_path / self.indice_filename) + create_masked_indice(indice_path=ndwigao_name, + cloud_mask_path=self.cm_product.path, out_path=(out_path / masked_indice_raw)) index_tiff_2_jp2(img_path=(out_path / masked_indice_raw), out_path=(out_path / masked_indice_filename)) @@ -347,12 +342,11 @@ class NdwiGao(Indice): # lut_dict=cmap, clouds_color="white", # out_path=(self.out_path / quicklook_filename)) create_rvb(raster=(self.out_path / self.indice_filename), - cloud_mask=self.l2a_product.user_cloud_mask, + cloud_mask=self.cm_product.path, lut_dict=cmap, clouds_color="white", out_path=(self.out_path / quicklook_filename)) -# A updater cm_product_object class Mndwi(Indice): """ MNDWI = (GREEN-SWIR) / (GREEN+SWIR) @@ -366,11 +360,12 @@ class Mndwi(Indice): ext_raw = ".tif" colormap = cm.BrBG - def __init__(self, l2a_product_object): - if l2a_product_object is None: - raise ValueError("A L2aProduct object must be provided") + def __init__(self, l2a_product_object, cm_product_object): + if (l2a_product_object or cm_product_object) is None: + raise ValueError("A L2aProduct and NewCloudMask objects must be provided") else: self.l2a_product = l2a_product_object + self.cm_product = cm_product_object # output path self.out_path = None @@ -400,10 +395,10 @@ class Mndwi(Indice): out_path=(out_path / self.indice_filename)) if nodata_clouds: - if self.l2a_product.user_cloud_mask is None: + if not self.cm_product.path.exists(): raise ValueError("Cloud mask does not exist") - masked_indice_filename = self.indice_stem + "_MASKED" + self.ext - masked_indice_raw = self.indice_stem + "_MASKED" + self.ext_raw + masked_indice_filename = self.indice_stem + "_" + self.cm_product.suffix + self.ext + masked_indice_raw = self.indice_stem + "_" + self.cm_product.suffix + self.ext_raw if (out_path / masked_indice_filename).exists() and not reprocess: logger.info("{} already exists".format(masked_indice_filename)) @@ -413,7 +408,7 @@ class Mndwi(Indice): else: mndwi_name = (out_path / self.indice_filename) create_masked_indice(indice_path=mndwi_name, - cloud_mask_path=self.l2a_product.user_cloud_mask, + cloud_mask_path=self.cm_product.path, out_path=(out_path / masked_indice_raw)) index_tiff_2_jp2(img_path=(out_path / masked_indice_raw), out_path=(out_path / masked_indice_filename)) @@ -428,13 +423,13 @@ class Mndwi(Indice): if quicklook: cmap = matplotlib_colormap_to_rgb(self.colormap, revers=False) - quicklook_filename = self.indice_stem + "_QUICKLOOK.tif" + quicklook_filename = self.indice_stem + "_" + self.cm_product.suffix + "_QL.tif" if (self.out_path / quicklook_filename).exists() and not reprocess: logger.info("{} already exists".format(quicklook_filename)) else: logger.info("creating quicklook") create_rvb(raster=(self.out_path / self.indice_filename), - cloud_mask=self.l2a_product.user_cloud_mask, + cloud_mask=self.cm_product.path, lut_dict=cmap, clouds_color="white", out_path=(self.out_path / quicklook_filename)) @@ -537,12 +532,6 @@ class IRECI(Indice): colormap = cm.Spectral -############################## -######## New Indices ######### -############################## - - -# A updater cm_product_object class BIGR(Indice): """ Brightness Index Green Red = ( (GREEN² + RED²)/2 ) ^ 0.5 @@ -555,14 +544,13 @@ class BIGR(Indice): ext = ".jp2" ext_raw = ".tif" colormap = cm.pink - #~ colormap = cm.colors.LinearSegmentedColormap.from_list("", ["black", "white"]) - - def __init__(self, l2a_product_object): - if l2a_product_object is None: - raise ValueError("A L2aProduct object must be provided") + def __init__(self, l2a_product_object, cm_product_object): + if (l2a_product_object or cm_product_object) is None: + raise ValueError("A L2aProduct and NewCloudMask objects must be provided") else: self.l2a_product = l2a_product_object + self.cm_product = cm_product_object # output path self.out_path = None @@ -592,10 +580,10 @@ class BIGR(Indice): out_path=(out_path / self.indice_filename)) if nodata_clouds: - if self.l2a_product.user_cloud_mask is None: + if not self.cm_product.path.exists(): raise ValueError("Cloud mask does not exist") - masked_indice_filename = self.indice_stem + "_MASKED" + self.ext - masked_indice_raw = self.indice_stem + "_MASKED" + self.ext_raw + masked_indice_filename = self.indice_stem + "_" + self.cm_product.suffix + self.ext + masked_indice_raw = self.indice_stem + "_" + self.cm_product.suffix + self.ext_raw if (out_path / masked_indice_filename).exists() and not reprocess: logger.info("{} already exists".format(masked_indice_filename)) @@ -605,7 +593,7 @@ class BIGR(Indice): else: bigr_name = (out_path / self.indice_filename) create_masked_indice(indice_path=bigr_name, - cloud_mask_path=self.l2a_product.user_cloud_mask, + cloud_mask_path=self.cm_product.path, out_path=(out_path / masked_indice_raw)) index_tiff_2_jp2(img_path=(out_path / masked_indice_raw), out_path=(out_path / masked_indice_filename)) @@ -620,19 +608,18 @@ class BIGR(Indice): if quicklook: cmap = matplotlib_colormap_to_rgb(self.colormap, revers=False) - quicklook_filename = self.indice_stem + "_QUICKLOOK.tif" + quicklook_filename = self.indice_stem + "_" + self.cm_product.suffix + "_QL.tif" if (self.out_path / quicklook_filename).exists() and not reprocess: logger.info("{} already exists".format(quicklook_filename)) else: logger.info("creating quicklook") create_rvb(raster=(self.out_path / self.indice_filename), - cloud_mask=self.l2a_product.user_cloud_mask, + cloud_mask=self.cm_product.path, lut_dict=cmap, clouds_color="white", out_path=(self.out_path / quicklook_filename), stretch=(0,2500)) -# A updater cm_product_object class BIRNIR(Indice): """ Brightness Index Red Near InfraRed = ( (RED² + NIR²)/2 ) ^ 0.5 @@ -645,14 +632,13 @@ class BIRNIR(Indice): ext = ".jp2" ext_raw = ".tif" colormap = cm.afmhot - #~ colormap = cm.colors.LinearSegmentedColormap.from_list("", ["black", "white"]) - - def __init__(self, l2a_product_object): - if l2a_product_object is None: - raise ValueError("A L2aProduct object must be provided") + def __init__(self, l2a_product_object, cm_product_object): + if (l2a_product_object or cm_product_object) is None: + raise ValueError("A L2aProduct and NewCloudMask objects must be provided") else: self.l2a_product = l2a_product_object + self.cm_product = cm_product_object # output path self.out_path = None @@ -682,10 +668,10 @@ class BIRNIR(Indice): out_path=(out_path / self.indice_filename)) if nodata_clouds: - if self.l2a_product.user_cloud_mask is None: + if not self.cm_product.path.exists(): raise ValueError("Cloud mask does not exist") - masked_indice_filename = self.indice_stem + "_MASKED" + self.ext - masked_indice_raw = self.indice_stem + "_MASKED" + self.ext_raw + masked_indice_filename = self.indice_stem + "_" + self.cm_product.suffix + self.ext + masked_indice_raw = self.indice_stem + "_" + self.cm_product.suffix + self.ext_raw if (out_path / masked_indice_filename).exists() and not reprocess: logger.info("{} already exists".format(masked_indice_filename)) @@ -695,7 +681,7 @@ class BIRNIR(Indice): else: birnir_name = (out_path / self.indice_filename) create_masked_indice(indice_path=birnir_name, - cloud_mask_path=self.l2a_product.user_cloud_mask, + cloud_mask_path=self.cm_product.path, out_path=(out_path / masked_indice_raw)) index_tiff_2_jp2(img_path=(out_path / masked_indice_raw), out_path=(out_path / masked_indice_filename)) @@ -710,19 +696,18 @@ class BIRNIR(Indice): if quicklook: cmap = matplotlib_colormap_to_rgb(self.colormap, revers=False) - quicklook_filename = self.indice_stem + "_QUICKLOOK.tif" + quicklook_filename = self.indice_stem + "_" + self.cm_product.suffix + "_QL.tif" if (self.out_path / quicklook_filename).exists() and not reprocess: logger.info("{} already exists".format(quicklook_filename)) else: logger.info("creating quicklook") create_rvb(raster=(self.out_path / self.indice_filename), - cloud_mask=self.l2a_product.user_cloud_mask, + cloud_mask=self.cm_product.path, lut_dict=cmap, clouds_color="white", out_path=(self.out_path / quicklook_filename), stretch=(0,5000)) -# A updater cm_product_object class BIBG(Indice): """ Brightness Index Blue Green = ( (BLUE² + GREEN²)/2 ) ^ 0.5 @@ -735,14 +720,13 @@ class BIBG(Indice): ext = ".jp2" ext_raw = ".tif" colormap = cm.bone - #~ colormap = cm.colors.LinearSegmentedColormap.from_list("", ["black", "white"]) - - def __init__(self, l2a_product_object): - if l2a_product_object is None: - raise ValueError("A L2aProduct object must be provided") + def __init__(self, l2a_product_object, cm_product_object): + if (l2a_product_object or cm_product_object) is None: + raise ValueError("A L2aProduct and NewCloudMask objects must be provided") else: self.l2a_product = l2a_product_object + self.cm_product = cm_product_object # output path self.out_path = None @@ -770,13 +754,11 @@ class BIBG(Indice): out_path=(out_path / self.indice_raw)) index_tiff_2_jp2(img_path=(out_path / self.indice_raw), out_path=(out_path / self.indice_filename)) - if nodata_clouds: - if self.l2a_product.user_cloud_mask is None: + if not self.cm_product.path.exists(): raise ValueError("Cloud mask does not exist") - masked_indice_filename = self.indice_stem + "_MASKED" + self.ext - masked_indice_raw = self.indice_stem + "_MASKED" + self.ext_raw - + masked_indice_filename = self.indice_stem + "_" + self.cm_product.suffix + self.ext + masked_indice_raw = self.indice_stem + "_" + self.cm_product.suffix + self.ext_raw if (out_path / masked_indice_filename).exists() and not reprocess: logger.info("{} already exists".format(masked_indice_filename)) else: @@ -785,34 +767,30 @@ class BIBG(Indice): else: bibg_name = (out_path / self.indice_filename) create_masked_indice(indice_path=bibg_name, - cloud_mask_path=self.l2a_product.user_cloud_mask, + cloud_mask_path=self.cm_product.path, out_path=(out_path / masked_indice_raw)) index_tiff_2_jp2(img_path=(out_path / masked_indice_raw), out_path=(out_path / masked_indice_filename)) os.remove(str(out_path / masked_indice_raw)) - try: os.remove(str(out_path / self.indice_raw)) logger.info("Removing {}".format(self.indice_raw)) except: pass - if quicklook: cmap = matplotlib_colormap_to_rgb(self.colormap, revers=False) - - quicklook_filename = self.indice_stem + "_QUICKLOOK.tif" + quicklook_filename = self.indice_stem + "_" + self.cm_product.suffix + "_QL.tif" if (self.out_path / quicklook_filename).exists() and not reprocess: logger.info("{} already exists".format(quicklook_filename)) else: logger.info("creating quicklook") create_rvb(raster=(self.out_path / self.indice_filename), - cloud_mask=self.l2a_product.user_cloud_mask, + cloud_mask=self.cm_product.path, lut_dict=cmap, clouds_color="white", out_path=(self.out_path / quicklook_filename), stretch=(0,2500)) - class IndicesCollectionMeta(type): """Adds special methods to IndicesCollection class. diff --git a/sen2chain/products.py b/sen2chain/products.py index eec6c5c..00aaca7 100755 --- a/sen2chain/products.py +++ b/sen2chain/products.py @@ -650,10 +650,12 @@ class L2aProduct(Product): else: indice_path = Path(out_path) / (self.identifier + "_INDICES") / indice.upper() indice_path.mkdir(parents=True, exist_ok=True) + indice_obj = indice_cls(self, NewCloudMaskProduct(l2a_identifier = self.identifier, cm_version = cm_version, probability = probability, iterations = iterations)) + indice_obj.process_indice(out_path = indice_path, nodata_clouds = nodata_clouds, quicklook = quicklook, -- GitLab