Skip to content
Snippets Groups Projects
Commit 886556f3 authored by pascal.mouquet_ird.fr's avatar pascal.mouquet_ird.fr
Browse files

Update NDWI processing

parent 9a73b1e5
No related branches found
No related tags found
No related merge requests found
...@@ -14,7 +14,7 @@ import os ...@@ -14,7 +14,7 @@ import os
from typing import Union, List from typing import Union, List
from .indices_functions import (create_raw_ndvi, create_raw_ndwi, from .indices_functions import (create_raw_ndvi, create_raw_ndwi,
create_masked_indice, ndvi_tiff_2_jp2) create_masked_indice, index_tiff_2_jp2)
from .colormap import matplotlib_colormap_to_rgb, create_colormap from .colormap import matplotlib_colormap_to_rgb, create_colormap
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -102,7 +102,7 @@ class Ndvi(Indice): ...@@ -102,7 +102,7 @@ class Ndvi(Indice):
create_raw_ndvi(nir_path=self.l2a_product.b08_10m, create_raw_ndvi(nir_path=self.l2a_product.b08_10m,
vir_path=self.l2a_product.b04_10m, vir_path=self.l2a_product.b04_10m,
out_path=(out_path / self.indice_raw)) out_path=(out_path / self.indice_raw))
ndvi_tiff_2_jp2(img_path=(out_path / self.indice_raw), index_tiff_2_jp2(img_path=(out_path / self.indice_raw),
out_path=(out_path / self.indice_filename)) out_path=(out_path / self.indice_filename))
if nodata_clouds: if nodata_clouds:
if self.l2a_product.user_cloud_mask is None: if self.l2a_product.user_cloud_mask is None:
...@@ -120,7 +120,7 @@ class Ndvi(Indice): ...@@ -120,7 +120,7 @@ class Ndvi(Indice):
create_masked_indice(indice_path=ndvi_name, create_masked_indice(indice_path=ndvi_name,
cloud_mask_path=self.l2a_product.user_cloud_mask, cloud_mask_path=self.l2a_product.user_cloud_mask,
out_path=(out_path / masked_indice_raw)) out_path=(out_path / masked_indice_raw))
ndvi_tiff_2_jp2(img_path=(out_path / masked_indice_raw), index_tiff_2_jp2(img_path=(out_path / masked_indice_raw),
out_path=(out_path / masked_indice_filename)) out_path=(out_path / masked_indice_filename))
os.remove(str(out_path / masked_indice_raw)) os.remove(str(out_path / masked_indice_raw))
...@@ -148,7 +148,8 @@ class NdwiGao(Indice): ...@@ -148,7 +148,8 @@ class NdwiGao(Indice):
""" """
name = "NDWI_GAO" name = "NDWI_GAO"
filename_template = "{product_identifier}_NDWIg{ext}" filename_template = "{product_identifier}_NDWIg{ext}"
ext = ".tif" ext = ".jp2"
ext_raw = ".tif"
colormap = cm.RdYlBu colormap = cm.RdYlBu
# colormap = get_tsv_colormap("../../share/data/RdYlGn.lut") # colormap = get_tsv_colormap("../../share/data/RdYlGn.lut")
...@@ -162,37 +163,49 @@ class NdwiGao(Indice): ...@@ -162,37 +163,49 @@ class NdwiGao(Indice):
self.out_path = None self.out_path = None
# filenames # filenames
self.indice_stem = self.filename_template.format(product_identifier=self.l2a_product.identifier, self.indice_stem = self.filename_template.format(product_identifier=self.l2a_product.identifier, ext="")
ext="")
self.indice_filename = self.indice_stem + self.ext self.indice_filename = self.indice_stem + self.ext
self.indice_raw = self.indice_stem + self.ext_raw
def process_indice( def process_indice(
self, self,
out_path: pathlib.PosixPath, out_path: pathlib.PosixPath,
reprocess: bool = False, nodata_clouds: bool = False, quicklook: bool = False reprocess: bool = False,
nodata_clouds: bool = False,
quicklook: bool = False
) -> None: ) -> None:
""" process """ """ process """
self.out_path = out_path self.out_path = out_path
if (self.out_path / self.indice_filename).exists() and not reprocess: if (out_path / self.indice_filename).exists() and not reprocess:
logger.info("{} already exists".format(self.indice_filename)) logger.info("{} already exists".format(self.indice_filename))
else: else:
create_raw_ndwi(nir_path=self.l2a_product.b08_10m, create_raw_ndwi(nir_path=self.l2a_product.b08_10m,
swir_path=self.l2a_product.b11_20m, swir_path=self.l2a_product.b11_20m,
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)) out_path=(out_path / self.indice_filename))
if nodata_clouds: if nodata_clouds:
if self.l2a_product.user_cloud_mask is None: if self.l2a_product.user_cloud_mask is None:
raise ValueError("Cloud mask does not exist") raise ValueError("Cloud mask does not exist")
masked_indice_filename = self.indice_stem + "_MASKED" + self.ext masked_indice_filename = self.indice_stem + "_MASKED" + self.ext
masked_indice_raw = self.indice_stem + "_MASKED" + self.ext_raw
if (self.out_path / masked_indice_filename).exists() and not reprocess: if (out_path / masked_indice_filename).exists() and not reprocess:
logger.info("{} already exists".format(masked_indice_filename)) logger.info("{} already exists".format(masked_indice_filename))
else: else:
create_masked_indice(indice_path=(self.out_path / self.indice_filename), if (out_path / self.indice_raw).exists():
ndwi_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, cloud_mask_path=self.l2a_product.user_cloud_mask,
out_path=(self.out_path / masked_indice_filename)) 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))
if quicklook: if quicklook:
cmap = matplotlib_colormap_to_rgb(self.colormap, revers=False) cmap = matplotlib_colormap_to_rgb(self.colormap, revers=False)
...@@ -200,9 +213,10 @@ class NdwiGao(Indice): ...@@ -200,9 +213,10 @@ class NdwiGao(Indice):
if (self.out_path / quicklook_filename).exists() and not reprocess: if (self.out_path / quicklook_filename).exists() and not reprocess:
logger.info("{} already exists".format(quicklook_filename)) logger.info("{} already exists".format(quicklook_filename))
else: else:
logger.info("creating quicklook")
create_colormap(raster=(self.out_path / self.indice_filename), create_colormap(raster=(self.out_path / self.indice_filename),
cloud_mask=self.l2a_product.user_cloud_mask, cloud_mask=self.l2a_product.user_cloud_mask,
lut_dict=cmap, lut_dict=cmap, clouds_color="white",
out_path=(self.out_path / quicklook_filename)) out_path=(self.out_path / quicklook_filename))
......
...@@ -57,13 +57,13 @@ def create_raw_ndwi(nir_path: Union[str, pathlib.PosixPath], ...@@ -57,13 +57,13 @@ def create_raw_ndwi(nir_path: Union[str, pathlib.PosixPath],
swir_path: Union[str, pathlib.PosixPath], swir_path: Union[str, pathlib.PosixPath],
out_path: Union[str, pathlib.PosixPath]="./raw_ndwi.tif") -> pathlib.PosixPath: out_path: Union[str, pathlib.PosixPath]="./raw_ndwi.tif") -> pathlib.PosixPath:
""" """
Creates a NDWI raster from NIR and VIR rasters. Creates a NDWI raster from NIR and SWIR rasters.
:param nir_path: path to the NIR raster. :param nir_path: path to the NIR raster.
:param swir_path: path to the SWIR raster. :param swir_path: path to the SWIR raster.
:param out_path: path to the output raster. :param out_path: path to the output raster.
""" """
logger.info("creating raw NDWI (int16)") logger.info("creating raw NDWI (tiff - int16)")
with rasterio.open(str(nir_path)) as nir_src, \ with rasterio.open(str(nir_path)) as nir_src, \
rasterio.open(str(swir_path)) as swir_src: rasterio.open(str(swir_path)) as swir_src:
...@@ -78,9 +78,9 @@ def create_raw_ndwi(nir_path: Union[str, pathlib.PosixPath], ...@@ -78,9 +78,9 @@ def create_raw_ndwi(nir_path: Union[str, pathlib.PosixPath],
swir_reproj = np.empty(nir.shape, dtype=np.float32) swir_reproj = np.empty(nir.shape, dtype=np.float32)
reproject(source=swir, reproject(source=swir,
destination=swir_reproj, destination=swir_reproj,
src_transform=swir_src.affine, src_transform=swir_src.transform,
src_crs=swir_src.crs, src_crs=swir_src.crs,
dst_transform=nir_src.affine, dst_transform=nir_src.transform,
dst_crs=nir_src.crs, dst_crs=nir_src.crs,
resampling=Resampling.nearest) resampling=Resampling.nearest)
...@@ -94,11 +94,11 @@ def create_raw_ndwi(nir_path: Union[str, pathlib.PosixPath], ...@@ -94,11 +94,11 @@ def create_raw_ndwi(nir_path: Union[str, pathlib.PosixPath],
tiled=False, tiled=False,
dtype=np.int16, dtype=np.int16,
nodata=32767, nodata=32767,
transform=nir_src.affine) transform=nir_src.transform)
nir_profile.pop('tiled', None)
with rasterio.open(str(out_path), "w", **nir_profile) as dst: with rasterio.Env(GDAL_CACHEMAX=512) as env:
dst.write(ndwi_masked, 1) with rasterio.open(str(out_path), "w", **nir_profile) as dst:
dst.write(ndwi_masked, 1)
return Path(str(out_path)).absolute return Path(str(out_path)).absolute
...@@ -150,16 +150,13 @@ def create_masked_indice( ...@@ -150,16 +150,13 @@ def create_masked_indice(
return str(Path(str(out_path)).absolute) return str(Path(str(out_path)).absolute)
def ndvi_tiff_2_jp2(img_path: Union[str, pathlib.PosixPath], def index_tiff_2_jp2(img_path: Union[str, pathlib.PosixPath],
out_path: Union[str, pathlib.PosixPath]="./ndvi_2_jp2.jp2") -> pathlib.PosixPath: out_path: Union[str, pathlib.PosixPath]="./indice_2_jp2.jp2") -> pathlib.PosixPath:
""" """
Convert a NDVI file from TIF to JP2. Convert a indice file from TIF to JP2.
:param nir_path: path to the NIR raster.
:param vir_path: path to the VIR raster.
:param out_path: path to the output raster. :param out_path: path to the output raster.
""" """
logger.info("converting raw NDWI to JPEG2000") logger.info("converting raw indice to JPEG2000")
src_ds = gdal.Open(str(img_path)) src_ds = gdal.Open(str(img_path))
driver = gdal.GetDriverByName("JP2OpenJPEG") driver = gdal.GetDriverByName("JP2OpenJPEG")
dst_ds = driver.CreateCopy(str(out_path), src_ds, options=['NBITS=15', 'CODEC=JP2', 'QUALITY=20']) dst_ds = driver.CreateCopy(str(out_path), src_ds, options=['NBITS=15', 'CODEC=JP2', 'QUALITY=20'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment