|
37 | 37 | from shapely.ops import transform |
38 | 38 | from tqdm import tqdm |
39 | 39 |
|
| 40 | +# TODO TEMP |
| 41 | +from pathlib import Path |
| 42 | +BASE_DIR = Path("efast_results") |
| 43 | +# TODO END TEMP |
| 44 | + |
| 45 | + |
40 | 46 | # Mapping of Sentinel-2 bands names to bands ids |
41 | 47 | BANDS_IDS = { |
42 | 48 | "B02": "1", |
@@ -121,6 +127,13 @@ def extract_mask_s2_bands( |
121 | 127 | with rasterio.open(out_path, "w", **profile) as dst: |
122 | 128 | dst.write(s2_image) |
123 | 129 |
|
| 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 | + |
124 | 137 |
|
125 | 138 | def distance_to_clouds(dir_s2, ratio=30, tolerance_percentage=0.05): |
126 | 139 | """ |
@@ -203,7 +216,36 @@ def distance_to_clouds(dir_s2, ratio=30, tolerance_percentage=0.05): |
203 | 216 | out_path = re.sub("_[A-Z]*\.tif", "_DIST_CLOUD.tif", str(sen2_path)) |
204 | 217 | with rasterio.open(out_path, "w", **s2_profile) as dst: |
205 | 218 | 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]) |
206 | 225 |
|
| 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 | + |
207 | 249 |
|
208 | 250 | def get_wkt_footprint(dir_s2, crs="EPSG:4326"): |
209 | 251 | """ |
|
0 commit comments