Nadir BRDF Adjusted Reflectance (NBAR) for Sentinel-2 in Python
ValueError
events when not all bands include angle values.nbar_stac
when not all bands include angle values.np.nanmean
of detector angles.tqdm
progress bar and set leave=False
.get_all_items()
by item_collection()
.cubo>=2023.7.0
.You can use sen2nbar
to convert complete images via SAFE:
from sen2nbar.nbar import nbar_SAFE
# Converted images are saved inside the SAFE path
nbar_SAFE("S2A_MSIL2A_20230223T075931_N0509_R035_T35HLC_20230223T120656.SAFE")
Note
Note that
sen2nbar
automatically shifts the DN of images with a processing baseline >= 04.00. This includes data cubes obtained viastackstac
orcubo
.
Or, if you are using STAC and retrieving images via stackstac
:
import pystac_client
import stackstac
import planetary_computer as pc
from sen2nbar.nbar import nbar_stackstac
# Important infor for later
endpoint = "https://planetarycomputer.microsoft.com/api/stac/v1"
collection = "sentinel-2-l2a"
bounds = (-148.565368, 60.800723, -147.443389, 61.183638)
# Open the STAC
catalog = pystac_client.Client.open(endpoint, modifier=pc.sign_inplace)
# Define your area
area_of_interest = {
"type": "Polygon",
"coordinates": [
[
[bounds[0], bounds[1]],
[bounds[2], bounds[1]],
[bounds[2], bounds[3]],
[bounds[0], bounds[3]],
[bounds[0], bounds[1]],
]
],
}
# Search the items
items = catalog.search(
collections=[collection],
intersects=area_of_interest,
datetime="2019-06-01/2019-08-01",
query={"eo:cloud_cover": {"lt": 10}},
).get_all_items()
# Retrieve all items as a xr.DataArray
stack = stackstac.stack(
items,
assets=["B05","B06","B07"], # Red Edge here, but you can use more!
bounds_latlon=bounds,
resolution=20
)
# Convert it to NBAR!
da = nbar_stackstac(
stack,
stac=endpoint,
collection=collection
)
Warning
These examples are done using
Planetary Computer
. If you are using data cubes retrieved via STAC (e.g., by usingstackstac
orcubo
), we recommend you to use this provider. The providerElement84
is not supported at the moment.
And going deeper, if you are using cubo
:
import cubo
import xarray as xr
from sen2nbar.nbar import nbar_cubo
# Get your cube
da = cubo.create(
lat=47.84815,
lon=13.37949,
collection="sentinel-2-l2a",
bands=["B02","B03","B04"], # RGB here, but you can add more bands!
start_date="2020-01-01",
end_date="2021-01-01",
edge_size=64,
resolution=10,
query={"eo:cloud_cover": {"lt": 3}}
)
# Convert it to NBAR (This a xr.DataArray)
da = nbar_cubo(da)