PRISMA cloud detection
Run CloudSEN12 models in PRISMA¶
- Authors: Gonzalo Mateo-García, Manuel Montesino Martin
This tutorial shows how to run CloudSEN12
cloud detection models in a PRISMA image. It requires the cloudsen12_models
package.
pip install cloudsen12_models georeader-spaceml
from georeader.readers import prisma
from cloudsen12_models import cloudsen12
from georeader import plot
file = "rasters/PRS_L1_STD_OFFL_20241109073054_20241109073059_0001.he5"
prisma_reader = prisma.PRISMA(file)
prisma_reader
from georeader.readers import S2_SAFE_reader
srf = S2_SAFE_reader.read_srf("S2A")
srf
%%time
import numpy as np
# Load VNIR-SWIR in prisma units (i.e. radiance units)
vnir = prisma_reader.load_raw(swir_flag=False); print(vnir.shape)
swir = prisma_reader.load_raw(swir_flag=True); print(swir.shape)
vnir_swir = np.moveaxis(np.concatenate((vnir, swir), axis = 2), 2, 0)
# Load VNIR-SWIR central wavelengths
vnir_wvl = prisma_reader.wavelength_vnir[0]
swir_wvl = prisma_reader.wavelength_swir[0]
vnir_swir_wvl = np.concatenate((vnir_wvl, swir_wvl))
from georeader import reflectance
s2bands = reflectance.transform_to_srf(vnir_swir,
srf,
wavelengths_hyperspectral=vnir_swir_wvl,
as_reflectance=True,
observation_date_corr_factor=prisma_reader.observation_date_correction_factor,
verbose=True,
units=prisma_reader.units)
type(s2bands)
import matplotlib.pyplot as plt
bands_s2 = ["B01","B02","B03","B04","B05","B06","B07","B08","B8A", "B09","B10","B11","B12"]
fig, ax =plt.subplots(7,2,figsize=(10,10), tight_layout=True)
ax =ax.flatten()
for i,b in enumerate(bands_s2):
ax[i].hist(s2bands[i].ravel())
ax[i].set_title(b)
s2bands.shape
np.moveaxis(s2bands, 0, 2).astype(np.float32).shape
from georeader import griddata
from georeader.geotensor import GeoTensor
s2_image = griddata.read_to_crs(np.moveaxis(s2bands, 0, 2).astype(np.float32),
lons=prisma_reader.lons, lats=prisma_reader.lats,
resolution_dst=30)
s2_image
swir_nir_red = (s2_image.isel({"band": [S2_SAFE_reader.BANDS_S2_L1C.index(b) for b in ["B11", "B08", "B04"]]}) / .45).clip(0,1)
plot.show(swir_nir_red)
model_4bands = cloudsen12.load_model_by_name(name="dtacs4bands", weights_folder="cloudsen12_models")
cloudmask = model_4bands.predict(s2bands[[S2_SAFE_reader.BANDS_S2_L1C.index(b) for b in model_4bands.bands]])
cloudmask_geotensor = griddata.read_to_crs(cloudmask,
lons=prisma_reader.lons, lats=prisma_reader.lats,
resolution_dst=30, method="nearest")
invalids = np.all(swir_nir_red.values == 0,axis=0)
cloudmask_geotensor.values[invalids] = np.nan
# swir_nir_red.values[:, invalids] = np.nan
cloudsen12.plot_cloudSEN12mask(cloudmask_geotensor, ax=ax[1])
fig, ax = plt.subplots(1,2,figsize=(14,5),sharey=True, tight_layout=True)
plot.show(swir_nir_red,ax=ax[0])
cloudsen12.plot_cloudSEN12mask(cloudmask_geotensor, ax=ax[1])
Licence¶
The cloudsen12_models
package is published under a GNU Lesser GPL v3 licence
The CloudSEN12 database and all pre-trained models are released under a Creative Commons non-commercial licence. For using the models in comercial pipelines written consent by the authors must be provided.
This notebook is released under a Creative Commons non-commercial licence.
If you find this work useful please cite:
@article{aybar_cloudsen12_2024,
title = {{CloudSEN12}+: {The} largest dataset of expert-labeled pixels for cloud and cloud shadow detection in {Sentinel}-2},
issn = {2352-3409},
url = {https://www.sciencedirect.com/science/article/pii/S2352340924008163},
doi = {10.1016/j.dib.2024.110852},
journal = {Data in Brief},
author = {Aybar, Cesar and Bautista, Lesly and Montero, David and Contreras, Julio and Ayala, Daryl and Prudencio, Fernando and Loja, Jhomira and Ysuhuaylas, Luis and Herrera, Fernando and Gonzales, Karen and Valladares, Jeanett and Flores, Lucy A. and Mamani, Evelin and Quiñonez, Maria and Fajardo, Rai and Espinoza, Wendy and Limas, Antonio and Yali, Roy and Alcántara, Alejandro and Leyva, Martin and Loayza-Muro, Rau´l and Willems, Bram and Mateo-García, Gonzalo and Gómez-Chova, Luis},
month = aug,
year = {2024},
pages = {110852},
}