EnMAP¶
This notebook requires the cloudsen12_models
package
pip install georeader-spaceml cloudsen12_models fsspec
from georeader.readers import enmap
from cloudsen12_models import cloudsen12
from georeader import plot
xml_file = "tempEnMAP/ENMAP01-____L1B-DT0000074101_20240511T080843Z_001_V010402_20240514T093550Z/ENMAP01-____L1B-DT0000074101_20240511T080843Z_001_V010402_20240514T093550Z-METADATA.XML"
enmap_reader = enmap.EnMAP(xml_file,by_folder=False)
enmap_reader
%%time
rgb = enmap_reader.load_rgb(apply_rpcs=False)
plot.show(rgb)
from georeader.readers import S2_SAFE_reader
srf = S2_SAFE_reader.read_srf("S2A")
srf
%%time
# Load swir in enmap_reader.units (i.e. radiance units)
swir = enmap_reader.load_product("SPECTRAL_IMAGE_SWIR")
from georeader import reflectance
bands_s2_swir = ["B09","B10","B11","B12"]
s2bandsswir = reflectance.transform_to_srf(swir,
srf[bands_s2_swir],
wavelengths_hyperspectral=enmap_reader.wl_center["swir"],
as_reflectance=True,
observation_date_corr_factor=enmap_reader.observation_date_correction_factor,
verbose=True,
units=enmap_reader.units)
s2bandsswir
import matplotlib.pyplot as plt
fig, ax =plt.subplots(2,2,figsize=(12,5), tight_layout=True)
ax =ax.flatten()
for i,b in enumerate(bands_s2_swir):
ax[i].hist(s2bandsswir.values[i].ravel())
ax[i].set_title(b)
vnir = enmap_reader.load_product("SPECTRAL_IMAGE_VNIR")
bands_s2_vnir = ["B01","B02","B03","B04","B05","B06","B07","B08","B8A"]
s2bandsvnir = reflectance.transform_to_srf(vnir,
srf[bands_s2_vnir],
wavelengths_hyperspectral=enmap_reader.wl_center["vnir"],
as_reflectance=True,
observation_date_corr_factor=enmap_reader.observation_date_correction_factor,
verbose=True,
extrapolate=True,
units=enmap_reader.units)
s2bandsvnir
fig, ax =plt.subplots(4,2,figsize=(12,10), tight_layout=True)
ax =ax.flatten()
for i,b in enumerate(bands_s2_vnir[:-1]):
ax[i].hist(s2bandsvnir.values[i].ravel())
ax[i].set_title(b)
rgb_s2 = s2bandsvnir.isel({"band": [3,2,1]})
plot.show(rgb_s2)
from georeader.geotensor import GeoTensor
import numpy as np
s2_image = GeoTensor(np.concatenate([s2bandsvnir.values, s2bandsswir.values],axis=0),
transform=s2bandsswir.transform, crs=s2bandsswir.crs,
fill_value_default=s2bandsswir.fill_value_default)
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(s2_image.isel({"band": [S2_SAFE_reader.BANDS_S2_L1C.index(b) for b in model_4bands.bands]}))
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, ax=ax[1])
model = cloudsen12.load_model_by_name(name="UNetMobV2_V2", weights_folder="cloudsen12_models")
cloudmask = model.predict(s2_image)
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, ax=ax[1])