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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Downloads S1/S2 patches from TAMMI + TAMMI_S1S2 mosaics.
|
| 4 |
+
Requires:
|
| 5 |
+
pip install datasets rasterio tifffile huggingface_hub scipy numpy
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import numpy as np
|
| 10 |
+
import rasterio
|
| 11 |
+
from rasterio.windows import Window
|
| 12 |
+
from scipy import interpolate
|
| 13 |
+
import tifffile as tiff
|
| 14 |
+
from datasets import load_dataset
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
TAMMI_REPO = "HichemBoussaid/TAMMI"
|
| 18 |
+
S1S2_LOCAL = "./S1S2_mosaics" # local path to TAMMI_S1S2 mosaics
|
| 19 |
+
# structure: {dept}/S1S2/
|
| 20 |
+
OUTPUT_DIR = "./patches" # where patches will be saved
|
| 21 |
+
SPLIT = "train" # "train", "val", or "test"
|
| 22 |
+
HEIGHT_S2 = 500
|
| 23 |
+
HEIGHT_S1 = 500
|
| 24 |
+
THRESHOLD = 9999
|
| 25 |
+
SHARED_MOSAIC = {"92": "75", "93": "75", "94": "75"}
|
| 26 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 27 |
+
|
| 28 |
+
# ============================================================
|
| 29 |
+
# S1 helpers
|
| 30 |
+
# ============================================================
|
| 31 |
+
def apply_threshold(image_data, threshold):
|
| 32 |
+
modulus = np.abs(image_data)
|
| 33 |
+
return np.where(modulus > threshold, threshold, modulus)
|
| 34 |
+
|
| 35 |
+
def geoTransform(image, orbit_direction, look_direction):
|
| 36 |
+
if orbit_direction == 'A':
|
| 37 |
+
image = np.flipud(image)
|
| 38 |
+
if look_direction == 'R':
|
| 39 |
+
image = np.fliplr(image)
|
| 40 |
+
return image
|
| 41 |
+
|
| 42 |
+
def get_s1_patch(s1s2_local, dept, namevh, namevv, an, rn,
|
| 43 |
+
orbit_direction, look_direction,
|
| 44 |
+
height_size=500, threshold=9999):
|
| 45 |
+
left = rn - height_size // 2
|
| 46 |
+
top = an - height_size // 2
|
| 47 |
+
|
| 48 |
+
with rasterio.open(os.path.join(s1s2_local, dept, "S1S2", namevh)) as src:
|
| 49 |
+
wvh = src.read(1, window=Window(left, top, height_size, height_size))
|
| 50 |
+
with rasterio.open(os.path.join(s1s2_local, dept, "S1S2", namevv)) as src:
|
| 51 |
+
wvv = src.read(1, window=Window(left, top, height_size, height_size))
|
| 52 |
+
with rasterio.open(os.path.join(s1s2_local, dept, "S1S2", "ratio" + namevv)) as src:
|
| 53 |
+
ratio = src.read(1, window=Window(left, top, height_size, height_size))
|
| 54 |
+
|
| 55 |
+
if threshold != 9999:
|
| 56 |
+
wvh = apply_threshold(wvh, threshold)
|
| 57 |
+
wvv = apply_threshold(wvv, threshold)
|
| 58 |
+
|
| 59 |
+
wvh = geoTransform(wvh, orbit_direction, look_direction)
|
| 60 |
+
wvv = geoTransform(wvv, orbit_direction, look_direction)
|
| 61 |
+
ratio = geoTransform(ratio, orbit_direction, look_direction)
|
| 62 |
+
|
| 63 |
+
return np.dstack((wvv, wvh, ratio)).astype(np.float32)
|
| 64 |
+
|
| 65 |
+
# ============================================================
|
| 66 |
+
# S2 helpers
|
| 67 |
+
# ============================================================
|
| 68 |
+
def upsample_patch(patch):
|
| 69 |
+
original_height, original_width = patch.shape
|
| 70 |
+
target_height = original_height * 2
|
| 71 |
+
target_width = original_width * 2
|
| 72 |
+
x = np.linspace(0, original_width - 1, original_width)
|
| 73 |
+
y = np.linspace(0, original_height - 1, original_height)
|
| 74 |
+
xnew = np.linspace(0, original_width - 1, target_width)
|
| 75 |
+
ynew = np.linspace(0, original_height - 1, target_height)
|
| 76 |
+
f = interpolate.interp2d(x, y, patch, kind='linear')
|
| 77 |
+
return f(xnew, ynew)
|
| 78 |
+
|
| 79 |
+
def get_s2_10m(path, pixx, pixy, height_size):
|
| 80 |
+
with rasterio.open(path) as dataset:
|
| 81 |
+
left = int(pixy - height_size // 2)
|
| 82 |
+
top = int(pixx - height_size // 2)
|
| 83 |
+
return dataset.read(1, window=Window(left, top, height_size, height_size))
|
| 84 |
+
|
| 85 |
+
def get_s2_20m(path, pixx, pixy, height_size):
|
| 86 |
+
with rasterio.open(path) as dataset:
|
| 87 |
+
left = int(pixy // 2 - height_size // 4)
|
| 88 |
+
top = int(pixx // 2 - height_size // 4)
|
| 89 |
+
patch = dataset.read(1, window=Window(left, top, height_size // 2, height_size // 2))
|
| 90 |
+
return upsample_patch(patch)
|
| 91 |
+
|
| 92 |
+
def get_s2_patch(s1s2_local, dept, s2_name_prefix, pixx, pixy, height_size=500):
|
| 93 |
+
"""
|
| 94 |
+
Loads all 8 S2 files ? stacks into (H, W, 10) array.
|
| 95 |
+
s2_name_prefix: s2_original_name without 'TCI.jp2'
|
| 96 |
+
e.g. 'T31UDQ_20210331T104619_'
|
| 97 |
+
"""
|
| 98 |
+
base = os.path.join(s1s2_local, dept, "S1S2", s2_name_prefix)
|
| 99 |
+
|
| 100 |
+
# TCI � 3 bands RGB, 10m
|
| 101 |
+
with rasterio.open(base + "TCI.jp2") as dataset:
|
| 102 |
+
left = int(pixy - height_size // 2)
|
| 103 |
+
top = int(pixx - height_size // 2)
|
| 104 |
+
w1 = dataset.read(1, window=Window(left, top, height_size, height_size))
|
| 105 |
+
w2 = dataset.read(2, window=Window(left, top, height_size, height_size))
|
| 106 |
+
w3 = dataset.read(3, window=Window(left, top, height_size, height_size))
|
| 107 |
+
tci = np.dstack((w1, w2, w3))
|
| 108 |
+
|
| 109 |
+
# 10m band
|
| 110 |
+
b08 = get_s2_10m(base + "B08.jp2", pixx, pixy, height_size)
|
| 111 |
+
|
| 112 |
+
# 20m bands � upsampled to match height_size
|
| 113 |
+
b05 = get_s2_20m(base + "B05.jp2", pixx, pixy, height_size)
|
| 114 |
+
b06 = get_s2_20m(base + "B06.jp2", pixx, pixy, height_size)
|
| 115 |
+
b07 = get_s2_20m(base + "B07.jp2", pixx, pixy, height_size)
|
| 116 |
+
b8a = get_s2_20m(base + "B8A.jp2", pixx, pixy, height_size)
|
| 117 |
+
b11 = get_s2_20m(base + "B11.jp2", pixx, pixy, height_size)
|
| 118 |
+
b12 = get_s2_20m(base + "B12.jp2", pixx, pixy, height_size)
|
| 119 |
+
|
| 120 |
+
# final stack: (H, W, 10)
|
| 121 |
+
return np.dstack((tci, b05, b06, b07, b08, b8a, b11, b12))
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def main():
|
| 125 |
+
print(f"Loading TAMMI {SPLIT} split...")
|
| 126 |
+
ds = load_dataset(TAMMI_REPO, split=SPLIT) # no token needed, public dataset
|
| 127 |
+
print(f" {len(ds)} images")
|
| 128 |
+
|
| 129 |
+
for i, sample in enumerate(ds):
|
| 130 |
+
dept = sample["department"]
|
| 131 |
+
mosaic_dept = SHARED_MOSAIC.get(dept, dept) # remap 92/93/94 -> 75
|
| 132 |
+
image_id = sample["image_id"]
|
| 133 |
+
s2_prefix = sample["s2_original_name"][:-7]
|
| 134 |
+
s1_vh = sample["s1_vh_name"]
|
| 135 |
+
s1_vv = sample["s1_vv_name"]
|
| 136 |
+
pixx, pixy = sample["s2_center_pos"]
|
| 137 |
+
rn, an = sample["s1_center_pos"]
|
| 138 |
+
orbit_dir = sample["orbit_direction"]
|
| 139 |
+
look_dir = sample["look_direction"]
|
| 140 |
+
|
| 141 |
+
out_dir = os.path.join(OUTPUT_DIR, dept, image_id)
|
| 142 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 143 |
+
|
| 144 |
+
s1_done = os.path.exists(os.path.join(out_dir, "S1.tif"))
|
| 145 |
+
s2_done = os.path.exists(os.path.join(out_dir, "S2.tif"))
|
| 146 |
+
|
| 147 |
+
if s1_done and s2_done:
|
| 148 |
+
print(f"[{i}] {image_id} already done, skipping")
|
| 149 |
+
continue
|
| 150 |
+
|
| 151 |
+
print(f"[{i}] Processing {image_id}...")
|
| 152 |
+
|
| 153 |
+
try:
|
| 154 |
+
if not s2_done:
|
| 155 |
+
s2 = get_s2_patch(S1S2_LOCAL, mosaic_dept, s2_prefix, pixx, pixy, HEIGHT_S2)
|
| 156 |
+
tiff.imwrite(os.path.join(out_dir, "S2.tif"), s2)
|
| 157 |
+
|
| 158 |
+
if not s1_done:
|
| 159 |
+
s1 = get_s1_patch(S1S2_LOCAL, mosaic_dept, s1_vh, s1_vv,
|
| 160 |
+
an, rn, orbit_dir, look_dir,
|
| 161 |
+
HEIGHT_S1, THRESHOLD)
|
| 162 |
+
tiff.imwrite(os.path.join(out_dir, "S1.tif"), s1)
|
| 163 |
+
|
| 164 |
+
except Exception as e:
|
| 165 |
+
print(f" ERROR on {image_id}: {e}")
|
| 166 |
+
continue
|
| 167 |
+
|
| 168 |
+
print("Done!")
|
| 169 |
+
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
main()
|