Skip to content

Commit

Permalink
Added processing level to S2
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinBabe committed Mar 8, 2024
1 parent 5b775ea commit 03aa686
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 113 deletions.
6 changes: 3 additions & 3 deletions examples/extraction_pipelines/S2_extraction_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@
" backend_context = BackendContext(backend)\n",
"\n",
" fetch_type = FetchType.POLYGON\n",
" bands_to_download = ['S2-B01', 'S2-B02', 'S2-B03', 'S2-B04', 'S2-B05', 'S2-B06', 'S2-B07', 'S2-B08', 'S2-B8A', 'S2-B09', 'S2-B11', 'S2-B12', 'S2-SCL']\n",
" bands_to_download = ['S2-L2A-B01', 'S2-L2A-B02', 'S2-L2A-B03', 'S2-L2A-B04', 'S2-L2A-B05', 'S2-L2A-B06', 'S2-L2A-B07', 'S2-L2A-B08', 'S2-L2A-B8A', 'S2-L2A-B09', 'S2-L2A-B11', 'S2-L2A-B12', 'S2-L2A-SCL']\n",
"\n",
" # Create the job to extract S2\n",
" extraction_parameters = {\n",
Expand All @@ -481,13 +481,13 @@
" scl_dilated_mask = cube.process(\n",
" \"to_scl_dilation_mask\",\n",
" data=cube,\n",
" scl_band_name=\"S2-SCL\",\n",
" scl_band_name=\"S2-L2A-SCL\",\n",
" kernel1_size=17, # 17px dilation on a 20m layer\n",
" kernel2_size=77, # 77px dilation on a 20m layer\n",
" mask1_values=[2, 4, 5, 6, 7],\n",
" mask2_values=[3, 8, 9, 10, 11],\n",
" erosion_kernel_size=3\n",
" ).rename_labels(\"bands\", [\"S2-SCL_DILATED_MASK\"])\n",
" ).rename_labels(\"bands\", [\"S2-L2A-SCL_DILATED_MASK\"])\n",
"\n",
" cube = cube.merge_cubes(scl_dilated_mask)\n",
" cube = cube.linear_scale_range(0, 65534, 0, 65534)\n",
Expand Down
30 changes: 15 additions & 15 deletions examples/feature_extractors/quantile_feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def output_labels(self) -> list:

def execute(self, inarr: xr.DataArray) -> xr.DataArray:
# compute indices
b03 = inarr.sel(bands="S2-B03")
b04 = inarr.sel(bands="S2-B04")
b05 = inarr.sel(bands="S2-B05")
b06 = inarr.sel(bands="S2-B06")
b08 = inarr.sel(bands="S2-B08")
b11 = inarr.sel(bands="S2-B11")
b12 = inarr.sel(bands="S2-B12")
b03 = inarr.sel(bands="S2-L2A-B03")
b04 = inarr.sel(bands="S2-L2A-B04")
b05 = inarr.sel(bands="S2-L2A-B05")
b06 = inarr.sel(bands="S2-L2A-B06")
b08 = inarr.sel(bands="S2-L2A-B08")
b11 = inarr.sel(bands="S2-L2A-B11")
b12 = inarr.sel(bands="S2-L2A-B12")

ndvi = (b08 - b04) / (b08 + b04)
ndwi = (b03 - b08) / (b03 + b08)
Expand Down Expand Up @@ -100,14 +100,14 @@ def execute(self, inarr: xr.DataArray) -> xr.DataArray:

# The bands that you can extract are defined in the code openeo_gfmap.fetching.s2.BASE_SENTINEL2_L2A_MAPPING
bands = [
"S2-B03",
"S2-B04",
"S2-B05",
"S2-B06",
"S2-B08",
"S2-B11",
"S2-B12",
"S2-SCL",
"S2-L2A-B03",
"S2-L2A-B04",
"S2-L2A-B05",
"S2-L2A-B06",
"S2-L2A-B08",
"S2-L2A-B11",
"S2-L2A-B12",
"S2-L2A-SCL",
]

# Use the base feching
Expand Down
60 changes: 30 additions & 30 deletions src/openeo_gfmap/fetching/s2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,39 @@
from .fetching import CollectionFetcher, FetchType

BASE_SENTINEL2_L2A_MAPPING = {
"B01": "S2-B01",
"B02": "S2-B02",
"B03": "S2-B03",
"B04": "S2-B04",
"B05": "S2-B05",
"B06": "S2-B06",
"B07": "S2-B07",
"B08": "S2-B08",
"B8A": "S2-B8A",
"B09": "S2-B09",
"B11": "S2-B11",
"B12": "S2-B12",
"AOT": "S2-AOT",
"SCL": "S2-SCL",
"SNW": "S2-SNW",
"B01": "S2-L2A-B01",
"B02": "S2-L2A-B02",
"B03": "S2-L2A-B03",
"B04": "S2-L2A-B04",
"B05": "S2-L2A-B05",
"B06": "S2-L2A-B06",
"B07": "S2-L2A-B07",
"B08": "S2-L2A-B08",
"B8A": "S2-L2A-B8A",
"B09": "S2-L2A-B09",
"B11": "S2-L2A-B11",
"B12": "S2-L2A-B12",
"AOT": "S2-L2A-AOT",
"SCL": "S2-L2A-SCL",
"SNW": "S2-L2A-SNW",
}

ELEMENT84_SENTINEL2_L2A_MAPPING = {
"coastal": "S2-B01",
"blue": "S2-B02",
"green": "S2-B03",
"red": "S2-B04",
"rededge1": "S2-B05",
"rededge2": "S2-B06",
"rededge3": "S2-B07",
"nir": "S2-B08",
"nir08": "S2-B8A",
"nir09": "S2-B09",
"cirrus": "S2-B10",
"swir16": "S2-B11",
"swir22": "S2-B12",
"scl": "S2-SCL",
"aot": "S2-AOT",
"coastal": "S2-L2A-B01",
"blue": "S2-L2A-B02",
"green": "S2-L2A-B03",
"red": "S2-L2A-B04",
"rededge1": "S2-L2A-B05",
"rededge2": "S2-L2A-B06",
"rededge3": "S2-L2A-B07",
"nir": "S2-L2A-B08",
"nir08": "S2-L2A-B8A",
"nir09": "S2-L2A-B09",
"cirrus": "S2-L2A-B10",
"swir16": "S2-L2A-B11",
"swir22": "S2-L2A-B12",
"scl": "S2-L2A-SCL",
"aot": "S2-L2A-AOT",
}


Expand Down
10 changes: 5 additions & 5 deletions src/openeo_gfmap/preprocessing/cloudmasking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import openeo
from openeo.processes import if_, is_nan

SCL_HARMONIZED_NAME: str = "S2-SCL"
BAPSCORE_HARMONIZED_NAME: str = "S2-BAPSCORE"
SCL_HARMONIZED_NAME: str = "S2-L2A-SCL"
BAPSCORE_HARMONIZED_NAME: str = "S2-L2A-BAPSCORE"


def mask_scl_dilation(cube: openeo.DataCube, **params: dict) -> openeo.DataCube:
Expand Down Expand Up @@ -91,7 +91,7 @@ def get_bap_score(cube: openeo.DataCube, **params: dict) -> openeo.DataCube:
Returns
-------
openeo.DataCube
A 4D datacube containing the BAP score as name 'S2-BAPSCORE'.
A 4D datacube containing the BAP score as name 'S2-L2A-BAPSCORE'.
"""
udf_path = Path(__file__).parent / "udf_score.py"

Expand Down Expand Up @@ -159,7 +159,7 @@ def get_bap_mask(cube: openeo.DataCube, period: Union[str, list], **params: dict
openeo.DataCube
The datacube with the BAP mask applied.
"""
# Checks if the S2-SCL band is present in the datacube
# Checks if the S2-L2A-SCL band is present in the datacube
assert (
SCL_HARMONIZED_NAME in cube.metadata.band_names
), f"The {SCL_HARMONIZED_NAME} band is not present in the datacube."
Expand Down Expand Up @@ -196,7 +196,7 @@ def max_score_selection(score):
f"'period' must be a string or a list of dates (in YYYY-mm-dd format), got {period}."
)

return rank_mask.rename_labels("bands", ["S2-BAPMASK"])
return rank_mask.rename_labels("bands", ["S2-L2A-BAPMASK"])


def bap_masking(cube: openeo.DataCube, period: Union[str, list], **params: dict):
Expand Down
2 changes: 1 addition & 1 deletion src/openeo_gfmap/preprocessing/udf_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def apply_datacube(cube: XarrayDataCube, context: dict) -> XarrayDataCube:
intervals = context.get("intervals", None)
array = cube.get_array().transpose("t", "bands", "y", "x")

bap_score = array.sel(bands="S2-BAPSCORE")
bap_score = array.sel(bands="S2-L2A-BAPSCORE")

def select_maximum(score: xr.DataArray):
max_score = score.max(dim="t")
Expand Down
76 changes: 38 additions & 38 deletions src/openeo_gfmap/stac/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ def create_spatial_dimension(
BANDS_DIMENSION = pystac.extensions.datacube.AdditionalDimension(
{
"values": [
"S2-SCL",
"S2-B01",
"S2-B02",
"S2-B03",
"S2-B04",
"S2-B05",
"S2-B06",
"S2-B07",
"S2-B08",
"S2-B8A",
"S2-B09",
"S2-B10",
"S2-B11",
"S2-B12",
"S2-L2A-SCL",
"S2-L2A-B01",
"S2-L2A-B02",
"S2-L2A-B03",
"S2-L2A-B04",
"S2-L2A-B05",
"S2-L2A-B06",
"S2-L2A-B07",
"S2-L2A-B08",
"S2-L2A-B8A",
"S2-L2A-B09",
"S2-L2A-B10",
"S2-L2A-B11",
"S2-L2A-B12",
"CROPTYPE",
]
}
Expand All @@ -121,84 +121,84 @@ def create_spatial_dimension(
"type": "application/x-netcdf",
"roles": ["data"],
"proj:shape": [64, 64],
"raster:bands": [{"name": "S2-B01"}, {"name": "S2-B02"}],
"raster:bands": [{"name": "S2-L2A-B01"}, {"name": "S2-L2A-B02"}],
"cube:variables": {
"S2-B01": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B02": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B03": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B04": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B05": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B06": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B07": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B8A": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B08": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B11": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-B12": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-SCL": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B01": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B02": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B03": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B04": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B05": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B06": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B07": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B8A": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B08": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B11": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-B12": {"dimensions": ["time", "y", "x"], "type": "data"},
"S2-L2A-SCL": {"dimensions": ["time", "y", "x"], "type": "data"},
},
"eo:bands": [
{
"name": "S2-B01",
"name": "S2-L2A-B01",
"common_name": "coastal",
"center_wavelength": 0.443,
"full_width_half_max": 0.027,
},
{
"name": "S2-B02",
"name": "S2-L2A-B02",
"common_name": "blue",
"center_wavelength": 0.49,
"full_width_half_max": 0.098,
},
{
"name": "S2-B03",
"name": "S2-L2A-B03",
"common_name": "green",
"center_wavelength": 0.56,
"full_width_half_max": 0.045,
},
{
"name": "S2-B04",
"name": "S2-L2A-B04",
"common_name": "red",
"center_wavelength": 0.665,
"full_width_half_max": 0.038,
},
{
"name": "S2-B05",
"name": "S2-L2A-B05",
"common_name": "rededge",
"center_wavelength": 0.704,
"full_width_half_max": 0.019,
},
{
"name": "S2-B06",
"name": "S2-L2A-B06",
"common_name": "rededge",
"center_wavelength": 0.74,
"full_width_half_max": 0.018,
},
{
"name": "S2-B07",
"name": "S2-L2A-B07",
"common_name": "rededge",
"center_wavelength": 0.783,
"full_width_half_max": 0.028,
},
{
"name": "S2-B08",
"name": "S2-L2A-B08",
"common_name": "nir",
"center_wavelength": 0.842,
"full_width_half_max": 0.145,
},
{
"name": "S2-B8A",
"name": "S2-L2A-B8A",
"common_name": "nir08",
"center_wavelength": 0.865,
"full_width_half_max": 0.033,
},
{
"name": "S2-B11",
"name": "S2-L2A-B11",
"common_name": "swir16",
"center_wavelength": 1.61,
"full_width_half_max": 0.143,
},
{
"name": "S2-B12",
"name": "S2-L2A-B12",
"common_name": "swir22",
"center_wavelength": 2.19,
"full_width_half_max": 0.242,
Expand Down
2 changes: 1 addition & 1 deletion src/openeo_gfmap/utils/build_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def load_json(input_file: Path, bands: list) -> pd.DataFrame:
A `pandas.DataFrame` containing a combination of the band names and the
timestamps as column names.
For example, the Sentinel-2 green band on the 1st October 2020 is will
have the column name `S2-B02:2020-10-01`
have the column name `S2-L2A-B02:2020-10-01`
"""

df = pd.read_json(input_file)
Expand Down
2 changes: 1 addition & 1 deletion src/openeo_gfmap/utils/tile_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def normalize_array(inarr: xr.DataArray, percentile: float = 0.99) -> xr.DataArr
def select_optical_bands(inarr: xr.DataArray) -> xr.DataArray:
"""Filters and keep only the optical bands for a given array."""
return inarr.sel(
bands=[band for band in inarr.coords["bands"].to_numpy() if band.startswith("S2-B")]
bands=[band for band in inarr.coords["bands"].to_numpy() if band.startswith("S2-L2A-B")]
)


Expand Down
Loading

0 comments on commit 03aa686

Please sign in to comment.