Skip to content

Commit 4133679

Browse files
authored
Merge pull request #269 from raphaelrpl/patch-landsat-0.8
Fix bug related package install and landsat C2 L2 cubes (confidence)
2 parents a584c5c + 14ff754 commit 4133679

File tree

9 files changed

+76
-25
lines changed

9 files changed

+76
-25
lines changed

CHANGES.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Changes
2121
=======
2222

2323

24+
Version 0.8.6 (2023-07-14)
25+
--------------------------
26+
27+
- Fix generation of data cubes for Landsat program: confidence mask
28+
- Improve cube builder forms for extra parameters
29+
- Customize number of GDAL parallel threads to generate COGs
30+
31+
2432
Version 0.8.5 (2023-03-08)
2533
--------------------------
2634

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ LABEL "org.brazildatacube.description"="Docker image for Data Cube Builder appli
2727
LABEL "org.brazildatacube.git_commit"="${GIT_COMMIT}"
2828

2929
# Build arguments
30-
ARG CUBE_BUILDER_VERSION="0.8.5"
30+
ARG CUBE_BUILDER_VERSION="0.8.6"
3131
ARG CUBE_BUILDER_INSTALL_PATH="/opt/cube-builder/${CUBE_BUILDER_VERSION}"
3232

3333
ADD . ${CUBE_BUILDER_INSTALL_PATH}
3434

3535
WORKDIR ${CUBE_BUILDER_INSTALL_PATH}
3636

37-
RUN python3 -m pip install pip --upgrade "setuptools<67" wheel && \
37+
RUN python3 -m pip install -U pip "setuptools<67" wheel && \
3838
python3 -m pip install -e .[rabbitmq] && \
3939
python3 -m pip install gunicorn
4040

cube_builder/celery/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from celery import Celery
2828
from flask import Flask
2929

30-
from cube_builder.config import Config
30+
from ..config import Config
3131
from ..constants import to_bool
3232

3333
CELERY_TASKS = [

cube_builder/config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import os
2121

22-
from .version import __version__
2322
from .constants import to_bool
23+
from .version import __version__
2424

2525
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
2626

@@ -72,6 +72,13 @@ class Config:
7272
)
7373
"""The database URI that should be used for the database connection.
7474
Defaults to ``'postgresql://postgres:postgres@localhost:5432/bdc_catalog'``."""
75+
SQLALCHEMY_ENGINE_OPTIONS = {
76+
"pool_size": int(os.getenv("SQLALCHEMY_ENGINE_POOL_SIZE", 2)),
77+
"max_overflow": int(os.getenv("SQLALCHEMY_ENGINE_MAX_OVERFLOW", 5)),
78+
"poolclass": os.getenv("SQLALCHEMY_ENGINE_POOL_CLASS"),
79+
"pool_recycle": int(os.getenv("SQLALCHEMY_ENGINE_POOL_RECYCLE", -1)),
80+
}
81+
7582
STAC_URL = os.environ.get('STAC_URL', 'https://brazildatacube.dpi.inpe.br/stac/')
7683
MAX_THREADS_IMAGE_VALIDATOR = int(os.environ.get('MAX_THREADS_IMAGE_VALIDATOR', os.cpu_count()))
7784
# rasterio

cube_builder/forms.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ class CustomMaskDefinition(Schema):
108108
bits = fields.Boolean(required=False, allow_none=False, dump_default=False)
109109

110110

111+
class CustomScaleSchema(Schema):
112+
"""Represent the Band attribute scale factor and scale mult."""
113+
114+
add = fields.Float(required=True, allow_none=False, allow_nan=False)
115+
mult = fields.Float(required=True, allow_none=False, allow_nan=False)
116+
117+
111118
class CubeParametersSchema(Schema):
112119
"""Represent the data cube parameters used to be attached to the cube execution."""
113120

@@ -116,6 +123,15 @@ class CubeParametersSchema(Schema):
116123
histogram_matching = fields.Bool(required=False, allow_none=False)
117124
no_post_process = fields.Bool(required=False, allow_none=False)
118125
band_map = fields.Dict(required=False, allow_none=False)
126+
channel_limits = fields.List(
127+
fields.List(fields.Float, many=True, validate=fields.Length(equal=2)),
128+
required=False,
129+
allow_none=False,
130+
validate=fields.Length(min=1, max=3)
131+
)
132+
combined = fields.Boolean(required=False, allow_none=False)
133+
resampling = fields.String(required=False, allow_none=False, default="nearest")
134+
scale = fields.Nested(CustomScaleSchema, required=False, allow_none=False)
119135

120136

121137
class DataCubeForm(Schema):

cube_builder/utils/image.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import rasterio.features
3131
import rasterio.warp
3232
import shapely.geometry
33-
from rasterio._warp import Affine
33+
from rasterio.warp import Affine, Resampling
3434
from rio_cogeo.cogeo import cog_translate
3535
from rio_cogeo.profiles import cog_profiles
3636
from sqlalchemy.engine.result import ResultProxy, RowProxy
@@ -354,9 +354,13 @@ def generate_cogs(input_data_set_path, file_path, profile='deflate', block_size=
354354
output_profile["blockxsize"] = block_size
355355
output_profile["blockysize"] = block_size
356356

357+
threads = os.getenv("GDAL_NUM_THREADS", "2")
358+
if threads.isnumeric():
359+
threads = int(threads)
360+
357361
# Dataset Open option (see gdalwarp `-oo` option)
358362
config = dict(
359-
GDAL_NUM_THREADS="ALL_CPUS",
363+
GDAL_NUM_THREADS=threads,
360364
GDAL_TIFF_INTERNAL_MASK=True,
361365
GDAL_TIFF_OVR_BLOCKSIZE="128",
362366
)
@@ -730,3 +734,19 @@ def linear_raster_scale(array: ArrayType,
730734
data = data * (output_range[1] - output_range[0]) + output_range[0]
731735

732736
return data
737+
738+
739+
def get_resample_method(name: str) -> Resampling:
740+
"""Retrieve a resampling method from name.
741+
742+
Note:
743+
This method uses ``rasterio.warp.Resampling``.
744+
745+
Args:
746+
name: The resampling name
747+
"""
748+
supported = {entry.name: entry for entry in list(Resampling)}
749+
lowered = name.lower()
750+
if lowered not in supported:
751+
raise RuntimeError(f"Invalid resampling method. Use one of {supported.keys()}")
752+
return supported[lowered]

cube_builder/utils/processing.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
TOTAL_OBSERVATION_NAME)
5151
# Builder
5252
from . import get_srid_column
53-
from .image import (SmartDataSet, generate_cogs, linear_raster_scale, raster_convexhull, raster_extent, rescale,
54-
save_as_cog)
53+
from .image import (SmartDataSet, generate_cogs, get_resample_method, linear_raster_scale, raster_convexhull,
54+
raster_extent, rescale, save_as_cog)
5555
from .index_generator import generate_band_indexes
5656
from .strings import StringFormatter
5757

@@ -236,7 +236,8 @@ def merge(merge_file: str, mask: dict, assets: List[dict], band: str,
236236

237237
if quality_band == band:
238238
source_nodata = nodata = float(mask['nodata'])
239-
# Only apply bilinear (change pixel values) for band values
239+
elif "resampling" in kwargs:
240+
resampling = get_resample_method(kwargs["resampling"])
240241
elif (mask and mask.get('saturated_band') != band) or quality_band is None:
241242
resampling = Resampling.bilinear
242243

@@ -796,6 +797,16 @@ def blend(activity, band_map, quality_band, build_clear_observation=False, block
796797
saturated = radsat_extract_bits(saturated, 1, 7).astype(numpy.bool_)
797798
masked.mask[saturated] = True
798799

800+
# Get current observation file name
801+
file_path = bandlist[order].name
802+
file_date = datetime.strptime(merges_band_map[file_path], '%Y-%m-%d')
803+
day_of_year = file_date.timetuple().tm_yday
804+
805+
if build_clear_observation and is_combined_collection:
806+
datasource_block = provenance_merge_map[file_date.strftime('%Y-%m-%d')].dataset.read(1, window=window)
807+
if mask_values['bits']:
808+
confidence.oli = numpy.isin(datasource_block, index_landsat_oli)
809+
799810
if mask_values['bits']:
800811
matched = get_qa_mask(masked,
801812
clear_data=clear_values,
@@ -828,16 +839,6 @@ def blend(activity, band_map, quality_band, build_clear_observation=False, block
828839

829840
stack_total_observation[window.row_off: row_offset, window.col_off: col_offset] += copy_mask.astype(numpy.uint8)
830841

831-
# Get current observation file name
832-
file_path = bandlist[order].name
833-
file_date = datetime.strptime(merges_band_map[file_path], '%Y-%m-%d')
834-
day_of_year = file_date.timetuple().tm_yday
835-
836-
if build_clear_observation and is_combined_collection:
837-
datasource_block = provenance_merge_map[file_date.strftime('%Y-%m-%d')].dataset.read(1, window=window)
838-
if mask_values['bits']:
839-
confidence.oli = datasource_block == index_landsat_oli
840-
841842
# Find all no data in destination STACK image
842843
stack_raster_where_nodata = numpy.where(
843844
stack_raster[window.row_off: row_offset, window.col_off: col_offset] == nodata
@@ -848,9 +849,6 @@ def blend(activity, band_map, quality_band, build_clear_observation=False, block
848849
stack_raster[window.row_off: row_offset,
849850
window.col_off: col_offset].shape)
850851

851-
if build_clear_observation and is_combined_collection:
852-
datasource_block = provenance_merge_map[file_date.strftime('%Y-%m-%d')].dataset.read(1, window=window)
853-
854852
# Find all valid/cloud in destination STACK image
855853
raster_where_data = numpy.where(raster != nodata)
856854
raster_data_pos = numpy.ravel_multi_index(raster_where_data, raster.shape)
@@ -952,7 +950,7 @@ def blend(activity, band_map, quality_band, build_clear_observation=False, block
952950
provenance_file = build_cube_path(datacube, period, tile_id, version=version,
953951
band=PROVENANCE_NAME, composed=True, **kwargs)
954952
provenance_profile = profile.copy()
955-
provenance_profile.pop('nodata', -1)
953+
provenance_profile['nodata'] = PROVENANCE_ATTRIBUTES['nodata']
956954
provenance_profile['dtype'] = PROVENANCE_ATTRIBUTES['data_type']
957955

958956
save_as_cog(str(provenance_file), provenance_array, block_size=block_size, **provenance_profile)

cube_builder/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
"""
2424

2525

26-
__version__ = '0.8.5'
26+
__version__ = '0.8.6'

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
'SQLAlchemy-Utils>=0.34.2,<1',
7878
'pystac-client>=0.5',
7979
'MarkupSafe==2.0.1',
80-
'bdc-auth-client @ git+https://github.com/brazil-data-cube/bdc-auth-client.git@v0.2.1#egg=bdc-auth-client'
80+
'bdc-auth-client @ git+https://github.com/brazil-data-cube/bdc-auth-client.git@v0.2.1#egg=bdc-auth-client',
81+
# 3rdparty deps
82+
'pydantic<2',
8183
]
8284

8385
packages = find_packages()

0 commit comments

Comments
 (0)