Skip to content

Commit 0e800d1

Browse files
committed
Added intermediate outputs for debugging
1 parent c7cfa7a commit 0e800d1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

efast/s2_processing.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
from shapely.ops import transform
3838
from tqdm import tqdm
3939

40+
# TODO TEMP
41+
from pathlib import Path
42+
BASE_DIR = Path("efast_results")
43+
# TODO END TEMP
44+
45+
4046
# Mapping of Sentinel-2 bands names to bands ids
4147
BANDS_IDS = {
4248
"B02": "1",
@@ -121,6 +127,13 @@ def extract_mask_s2_bands(
121127
with rasterio.open(out_path, "w", **profile) as dst:
122128
dst.write(s2_image)
123129

130+
# TODO TEMP
131+
BASE_DIR.mkdir(exist_ok=True)
132+
mask_profile = profile.copy()
133+
mask_profile["count"] = 1
134+
with rasterio.open(BASE_DIR / "cloud_mask_efast.tif", "w", **mask_profile) as dst:
135+
dst.write(mask[np.newaxis])
136+
124137

125138
def distance_to_clouds(dir_s2, ratio=30, tolerance_percentage=0.05):
126139
"""
@@ -203,7 +216,36 @@ def distance_to_clouds(dir_s2, ratio=30, tolerance_percentage=0.05):
203216
out_path = re.sub("_[A-Z]*\.tif", "_DIST_CLOUD.tif", str(sen2_path))
204217
with rasterio.open(out_path, "w", **s2_profile) as dst:
205218
dst.write(distance_to_cloud[np.newaxis])
219+
# TODO TEMP
220+
with rasterio.open(BASE_DIR / "dtc_efast.tif", "w", **s2_profile) as dst:
221+
dst.write(distance_to_cloud[np.newaxis])
222+
223+
with rasterio.open(BASE_DIR / "dtc_input_efast.tif", "w", **s2_profile) as dst:
224+
dst.write(mask[np.newaxis])
206225

226+
with rasterio.open(BASE_DIR / "cloud_mask_resampled_efast.tif", "w", **s2_profile) as dst:
227+
dst.write(s2_block[np.newaxis])
228+
229+
230+
231+
def distance_to_clouds_in_memory(s2_hr, ratio=30, tolerance_percentage=0.05) -> np.ndarray:
232+
"""
233+
Only the algorithm part of the `distance_to_clouds` function.
234+
"""
235+
# Check if a Sentinel-3 pixel is complete
236+
s2_block = (
237+
(s2_hr == 0)
238+
.reshape(s2_hr.shape[0] // ratio, ratio, s2_hr.shape[1] // ratio, ratio)
239+
.mean(3)
240+
.mean(1)
241+
)
242+
243+
# Distance to cloud score
244+
mask = s2_block < tolerance_percentage
245+
distance_to_cloud = sp.ndimage.distance_transform_edt(mask)
246+
distance_to_cloud = np.clip(distance_to_cloud, 0, 255)
247+
return distance_to_cloud
248+
207249

208250
def get_wkt_footprint(dir_s2, crs="EPSG:4326"):
209251
"""

0 commit comments

Comments
 (0)