opensr-sr1m 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Eavheang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,81 @@
1
+ Metadata-Version: 2.4
2
+ Name: opensr-sr1m
3
+ Version: 0.1.0
4
+ Summary: Sentinel-2 10 m to 1 m RGB super-resolution for points and dates
5
+ Author: Eavheang
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Eavheang/openSR
8
+ Project-URL: Issues, https://github.com/Eavheang/openSR/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Scientific/Engineering :: GIS
12
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
13
+ Requires-Python: >=3.9
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: numpy
17
+ Requires-Dist: pillow
18
+ Requires-Dist: torch
19
+ Requires-Dist: rasterio
20
+ Requires-Dist: shapely
21
+ Requires-Dist: planetary-computer
22
+ Requires-Dist: pystac-client
23
+ Requires-Dist: mlstac
24
+ Requires-Dist: sen2sr
25
+ Requires-Dist: opensr-srgan>=1.1.0
26
+ Dynamic: license-file
27
+
28
+ # opensr
29
+
30
+ Sentinel-2 L2A (Planetary Computer) 10 m to 1 m RGB super-resolution for any
31
+ set of locations and dates. The SEN2-SR model takes 10 m to 2.5 m; bicubic
32
+ resampling plus sharpening then puts it on a 1 m grid. Detail finer than
33
+ ~2.5 m is not recovered, so treat 1 m as a pixel size, not true resolution.
34
+
35
+ Each (location, date) pair becomes a 5.12 km GeoTIFF (5120 x 5120 px at 1 m);
36
+ a run folder and a zip of it are written per call.
37
+
38
+ ## Install
39
+
40
+ ```bash
41
+ pip install opensr-sr1m
42
+ # or, from a local checkout
43
+ pip install -e .
44
+ ```
45
+
46
+ ## Use
47
+
48
+ ```python
49
+ import opensr.inferutils
50
+
51
+ # one location, one date -> output_2026-11-08_Phnom_Penh.tif
52
+ opensr.inferutils.run((104.9160, 11.5564), "2026-11-08", location="Phnom Penh")
53
+
54
+ # many locations x many dates (every combination is processed),
55
+ # one name per point -> output_2026-10-14_Site_A.tif, ...
56
+ results = opensr.inferutils.run(
57
+ [(105.392986, 14.121299), (104.9160, 11.5564)],
58
+ ["2026-10-14", "2026-10-29", "2026-11-08"],
59
+ location=["Site A", "Phnom Penh"],
60
+ out_dir="/content/s2dr3_out",
61
+ )
62
+
63
+ # CSV text or a CSV file path also work (lon first, lat second, header optional)
64
+ opensr.inferutils.run("points.csv", ["2026-11-08"])
65
+ ```
66
+
67
+ File names are `output_<date>_<location>.tif`, using the requested date.
68
+ When the same name repeats for one date, later files get `(1)`, `(2)`, ...
69
+ (`output_2026-11-08_Phnom_Penh(1).tif`). Without `location` the files are
70
+ `output_<date>_run_1.tif`, `output_<date>_run_2.tif`, ...
71
+ The scene actually used can be a few days from the requested date; its ID
72
+ (which includes its acquisition time) is stored in each GeoTIFF's `source` tag.
73
+
74
+ `run` returns one `Result` per (location, date) with `path` (GeoTIFF),
75
+ `preview` (PIL image: 10 m input | 1 m output, centre 1 km) and `error`
76
+ (set when that job failed). In Jupyter or Colab, `run` also draws every
77
+ preview inline under the progress output; in a plain script it only returns
78
+ the results.
79
+
80
+ The model weights download once to `~/.cache/opensr/` on the first call.
81
+ A GPU is used when available; CPU works but is slow.
@@ -0,0 +1,54 @@
1
+ # opensr
2
+
3
+ Sentinel-2 L2A (Planetary Computer) 10 m to 1 m RGB super-resolution for any
4
+ set of locations and dates. The SEN2-SR model takes 10 m to 2.5 m; bicubic
5
+ resampling plus sharpening then puts it on a 1 m grid. Detail finer than
6
+ ~2.5 m is not recovered, so treat 1 m as a pixel size, not true resolution.
7
+
8
+ Each (location, date) pair becomes a 5.12 km GeoTIFF (5120 x 5120 px at 1 m);
9
+ a run folder and a zip of it are written per call.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ pip install opensr-sr1m
15
+ # or, from a local checkout
16
+ pip install -e .
17
+ ```
18
+
19
+ ## Use
20
+
21
+ ```python
22
+ import opensr.inferutils
23
+
24
+ # one location, one date -> output_2026-11-08_Phnom_Penh.tif
25
+ opensr.inferutils.run((104.9160, 11.5564), "2026-11-08", location="Phnom Penh")
26
+
27
+ # many locations x many dates (every combination is processed),
28
+ # one name per point -> output_2026-10-14_Site_A.tif, ...
29
+ results = opensr.inferutils.run(
30
+ [(105.392986, 14.121299), (104.9160, 11.5564)],
31
+ ["2026-10-14", "2026-10-29", "2026-11-08"],
32
+ location=["Site A", "Phnom Penh"],
33
+ out_dir="/content/s2dr3_out",
34
+ )
35
+
36
+ # CSV text or a CSV file path also work (lon first, lat second, header optional)
37
+ opensr.inferutils.run("points.csv", ["2026-11-08"])
38
+ ```
39
+
40
+ File names are `output_<date>_<location>.tif`, using the requested date.
41
+ When the same name repeats for one date, later files get `(1)`, `(2)`, ...
42
+ (`output_2026-11-08_Phnom_Penh(1).tif`). Without `location` the files are
43
+ `output_<date>_run_1.tif`, `output_<date>_run_2.tif`, ...
44
+ The scene actually used can be a few days from the requested date; its ID
45
+ (which includes its acquisition time) is stored in each GeoTIFF's `source` tag.
46
+
47
+ `run` returns one `Result` per (location, date) with `path` (GeoTIFF),
48
+ `preview` (PIL image: 10 m input | 1 m output, centre 1 km) and `error`
49
+ (set when that job failed). In Jupyter or Colab, `run` also draws every
50
+ preview inline under the progress output; in a plain script it only returns
51
+ the results.
52
+
53
+ The model weights download once to `~/.cache/opensr/` on the first call.
54
+ A GPU is used when available; CPU works but is slow.
@@ -0,0 +1,35 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "opensr-sr1m"
7
+ version = "0.1.0"
8
+ description = "Sentinel-2 10 m to 1 m RGB super-resolution for points and dates"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [{ name = "Eavheang" }]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Operating System :: OS Independent",
17
+ "Topic :: Scientific/Engineering :: GIS",
18
+ "Topic :: Scientific/Engineering :: Image Processing",
19
+ ]
20
+ dependencies = [
21
+ "numpy",
22
+ "pillow",
23
+ "torch",
24
+ "rasterio",
25
+ "shapely",
26
+ "planetary-computer",
27
+ "pystac-client",
28
+ "mlstac",
29
+ "sen2sr",
30
+ "opensr-srgan>=1.1.0",
31
+ ]
32
+
33
+ [project.urls]
34
+ Homepage = "https://github.com/Eavheang/openSR"
35
+ Issues = "https://github.com/Eavheang/openSR/issues"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1,360 @@
1
+ """Sentinel-2 10 m -> 1 m RGB super-resolution for any set of points and dates.
2
+
3
+ 10 m -> 2.5 m is the SEN2-SR model (x4); 2.5 m -> 1 m is bicubic resampling plus
4
+ sharpening. The 1 m grid is finer, but detail below ~2.5 m is not recovered.
5
+
6
+ import opensr.inferutils
7
+ results = opensr.inferutils.run([(105.392986, 14.121299)], ["2026-10-29", "2026-11-08"])
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import numbers
12
+ import os
13
+ import re
14
+ import threading
15
+ import zipfile
16
+ from concurrent.futures import ThreadPoolExecutor, as_completed
17
+ from dataclasses import dataclass, field
18
+ from datetime import datetime, timedelta
19
+ from itertools import product
20
+
21
+ # GDAL reads these when a file is opened; setdefault keeps any value the user already set.
22
+ for _k, _v in dict(GDAL_DISABLE_READDIR_ON_OPEN="EMPTY_DIR", GDAL_HTTP_MULTIPLEX="YES",
23
+ GDAL_HTTP_VERSION="2", GDAL_HTTP_MAX_RETRY="5",
24
+ GDAL_HTTP_RETRY_DELAY="2").items():
25
+ os.environ.setdefault(_k, _v)
26
+
27
+ import mlstac
28
+ import numpy as np
29
+ import planetary_computer
30
+ import rasterio
31
+ import torch
32
+ import torch.nn.functional as F
33
+ from opensr_srgan import load_inference_model
34
+ from opensr_srgan.utils.back_projection import BackProjectionConfig, back_projection
35
+ from PIL import Image
36
+ from pystac_client import Client
37
+ from rasterio.enums import Resampling
38
+ from rasterio.transform import from_origin
39
+ from rasterio.warp import transform as warp_transform
40
+ from rasterio.windows import from_bounds
41
+ from shapely.geometry import Point, shape
42
+
43
+ PATCH = 512 # 10 m px per side = 5.12 km box
44
+ SEARCH_DAYS = 10 # no pass on the exact date -> nearest within +/- this
45
+ STRETCH = 0.3 # reflectance shown as white in RGB
46
+ SCALE = 4 # model: 10 m -> 2.5 m
47
+ OUT_RES = 1.0 # final pixel size (m); 2.5 m -> 1 m by resampling
48
+ TILE, OVERLAP = 128, 32 # model tile (10 m px), feathered overlap
49
+ BANDS = ["B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12"]
50
+ RGB = [2, 1, 0] # B04, B03, B02
51
+ STAC = "https://planetarycomputer.microsoft.com/api/stac/v1"
52
+ WEIGHTS_DIR = os.path.expanduser("~/.cache/opensr/SEN2SRLite")
53
+ WEIGHTS_URL = "https://huggingface.co/tacofoundation/sen2sr/resolve/main/SEN2SRLite/main/mlm.json"
54
+ DEV = "cuda" if torch.cuda.is_available() else "cpu"
55
+
56
+
57
+ @dataclass
58
+ class Result:
59
+ index: int # 1-based location index
60
+ lon: float
61
+ lat: float
62
+ date: str # requested date (the scene may be up to SEARCH_DAYS away)
63
+ path: str | None = None # 1 m RGB GeoTIFF; None if the job failed
64
+ preview: Image.Image | None = field(default=None, repr=False) # centre 1 km, 10 m input | 1 m output
65
+ error: str | None = None
66
+
67
+
68
+ # ---------------- Inputs ----------------
69
+ def _parse_line(line):
70
+ try:
71
+ return tuple(float(v) for v in re.split(r"[\s,;]+", line.strip())[:2])
72
+ except ValueError: # header / blank line
73
+ return None
74
+
75
+
76
+ def _points(lonlat):
77
+ """(lon, lat) | [(lon, lat), ...] | CSV text or CSV file path -> [(lon, lat), ...]"""
78
+ if isinstance(lonlat, str):
79
+ text = open(lonlat).read() if os.path.isfile(lonlat) else lonlat
80
+ pts = [p for p in map(_parse_line, text.splitlines()) if p and len(p) == 2]
81
+ elif isinstance(lonlat[0], numbers.Real):
82
+ pts = [lonlat]
83
+ else:
84
+ pts = list(lonlat)
85
+ if not pts:
86
+ raise ValueError("no lon/lat points given")
87
+ out = []
88
+ for lon, lat in pts:
89
+ lon, lat = float(lon), float(lat)
90
+ if not (-180 <= lon <= 180 and -90 <= lat <= 90):
91
+ raise ValueError(f"({lon}, {lat}) is not a valid (lon, lat); lon comes first")
92
+ out.append((lon, lat))
93
+ return out
94
+
95
+
96
+ def _dates(date):
97
+ """'YYYY-MM-DD' | datetime.date | list of either -> ['YYYY-MM-DD', ...]"""
98
+ dates = [str(d)[:10] for d in (date if isinstance(date, (list, tuple)) else [date])]
99
+ for d in dates:
100
+ datetime.strptime(d, "%Y-%m-%d") # fail before any download, not inside a worker
101
+ return dates
102
+
103
+
104
+ def _locations(location, n):
105
+ """None | name | list of n names (None allowed) -> n file-safe names or None."""
106
+ names = location if isinstance(location, (list, tuple)) else [location] * n
107
+ if len(names) != n:
108
+ raise ValueError(f"got {len(names)} location names for {n} points")
109
+ # Keep any script (e.g. Khmer); only drop whitespace and characters paths can't hold.
110
+ return [re.sub(r'[\s<>:"/\\|?*\x00-\x1f]+', "_", str(s)).strip("_.") or None if s else None
111
+ for s in names]
112
+
113
+
114
+ def _file_stems(jobs, names):
115
+ """output_<date>_<name>, then (1), (2)... on repeats; output_<date>_run_<n> if unnamed.
116
+
117
+ Takes the first free candidate, compared case-insensitively (macOS/Windows), so a
118
+ user name like "run_1" or "Site(1)" can never overwrite another job's file.
119
+ """
120
+ used, stems = set(), []
121
+ for r in jobs:
122
+ name = names[r.index - 1]
123
+ base = f"output_{r.date}_{name or 'run'}"
124
+ suffix, k = ("({})", 0) if name else ("_{}", 1)
125
+ stem = base + suffix.format(k) if k else base
126
+ while stem.lower() in used:
127
+ k += 1
128
+ stem = base + suffix.format(k)
129
+ used.add(stem.lower())
130
+ stems.append(stem)
131
+ return stems
132
+
133
+
134
+ # ---------------- Model ----------------
135
+ class _ESRGAN_RGBN(torch.nn.Module):
136
+ """SEN2-SR ESRGAN (EMA weights, expects 0-1 reflectance) + back-projection."""
137
+ def __init__(self, g):
138
+ super().__init__()
139
+ self.g = g
140
+
141
+ def forward(self, x):
142
+ return back_projection(self.g(x), x, BackProjectionConfig())
143
+
144
+
145
+ _model = None
146
+ _gpu = threading.Lock() # one job on the GPU at a time; downloads run in parallel
147
+ _batch = 36 # tiles per forward pass; halved on out-of-memory
148
+
149
+
150
+ def _load_model():
151
+ """Load once per process, on the first run() call (not at import)."""
152
+ global _model
153
+ if _model is None:
154
+ torch.backends.cudnn.benchmark = True
155
+ if not os.path.exists(f"{WEIGHTS_DIR}/mlm.json"):
156
+ mlstac.download(file=WEIGHTS_URL, output_dir=WEIGHTS_DIR)
157
+ m = mlstac.load(WEIGHTS_DIR).compiled_model(device=DEV)
158
+ m.sr_model = _ESRGAN_RGBN(load_inference_model("SEN2-SR", map_location="cpu"))
159
+ _model = m.to(DEV).eval()
160
+ return _model
161
+
162
+
163
+ # ---------------- GPU processing ----------------
164
+ def _super_resolve(x, batch):
165
+ """(C,H,W) 10 m -> (C,4H,4W) 2.5 m, `batch` tiles per pass, feathered seams."""
166
+ C, H, W = x.shape
167
+ step, T = TILE - OVERLAP, TILE * SCALE
168
+ ys = sorted({min(y, H - TILE) for y in range(0, H - OVERLAP, step)})
169
+ xs = sorted({min(c, W - TILE) for c in range(0, W - OVERLAP, step)})
170
+ i = torch.arange(T, device=x.device) + 0.5
171
+ w = (torch.minimum(i, T - i) / (OVERLAP * SCALE)).clamp(max=1)
172
+ w = (w[:, None] * w[None])[None]
173
+ out = torch.zeros(C, H * SCALE, W * SCALE, device=x.device)
174
+ wsum = torch.zeros(1, H * SCALE, W * SCALE, device=x.device)
175
+ coords = list(product(ys, xs))
176
+ for k in range(0, len(coords), batch):
177
+ cs = coords[k:k + batch]
178
+ for p, (y, c) in zip(_model(torch.stack([x[:, y:y + TILE, c:c + TILE] for y, c in cs])), cs):
179
+ out[:, y * SCALE:y * SCALE + T, c * SCALE:c * SCALE + T] += p * w
180
+ wsum[:, y * SCALE:y * SCALE + T, c * SCALE:c * SCALE + T] += w
181
+ return out.div_(wsum).clamp_(0, 1)
182
+
183
+
184
+ def _unsharp(img, sigma=0.8, amount=0.25):
185
+ r = int(3 * sigma + 0.5)
186
+ k = torch.exp(-torch.arange(-r, r + 1, device=img.device, dtype=img.dtype) ** 2 / (2 * sigma ** 2))
187
+ k /= k.sum()
188
+ b = F.pad(img[:, None], (r, r, r, r), mode="reflect")
189
+ b = F.conv2d(F.conv2d(b, k.view(1, 1, 1, -1)), k.view(1, 1, -1, 1))[:, 0]
190
+ return img + amount * (img - b)
191
+
192
+
193
+ def _to_uint8(rgb):
194
+ return ((rgb / STRETCH).clamp(0, 1) * 255).round().to(torch.uint8)
195
+
196
+
197
+ def _gpu_products(lr):
198
+ """10 m numpy (10,H,W) -> uint8 1 m RGB (3, 10H/OUT_RES, 10W/OUT_RES) numpy."""
199
+ global _batch
200
+ x = torch.from_numpy(lr).to(DEV)
201
+ with _gpu, torch.inference_mode():
202
+ while True:
203
+ try:
204
+ sr = _super_resolve(x, _batch)
205
+ break
206
+ except torch.cuda.OutOfMemoryError:
207
+ if _batch == 1:
208
+ raise
209
+ _batch //= 2
210
+ torch.cuda.empty_cache()
211
+ rgb = _unsharp(sr[RGB]) # sharpen at the model's native 2.5 m
212
+ size = (round(lr.shape[1] * 10 / OUT_RES), round(lr.shape[2] * 10 / OUT_RES))
213
+ rgb = F.interpolate(rgb[None], size=size, mode="bicubic", align_corners=False)[0]
214
+ return _to_uint8(rgb).cpu().numpy()
215
+
216
+
217
+ # ---------------- Sentinel-2 access ----------------
218
+ def _search_date(date, points):
219
+ """One STAC search per date covering every location."""
220
+ d0 = datetime.strptime(date, "%Y-%m-%d").date()
221
+ lons, lats = zip(*points)
222
+ items = Client.open(STAC).search(
223
+ collections=["sentinel-2-l2a"],
224
+ bbox=[min(lons), min(lats), max(lons), max(lats)],
225
+ datetime=f"{d0 - timedelta(SEARCH_DAYS)}/{d0 + timedelta(SEARCH_DAYS)}",
226
+ ).item_collection()
227
+ return d0, [(i, shape(i.geometry)) for i in items]
228
+
229
+
230
+ def _find_scenes(lon, lat, scenes):
231
+ """Scenes covering the point: nearest date first, then least cloudy."""
232
+ d0, items = scenes
233
+ hits = [i for i, fp in items if fp.contains(Point(lon, lat))]
234
+ return sorted(hits, key=lambda i: (abs((i.datetime.date() - d0).days),
235
+ i.properties.get("eo:cloud_cover", 100)))
236
+
237
+
238
+ def _open_asset(item, key): # sign at read time so tokens refresh on long runs
239
+ return rasterio.open(planetary_computer.sign(item.assets[key].href))
240
+
241
+
242
+ def _patch_geo(item, lon, lat):
243
+ with _open_asset(item, "B04") as ds:
244
+ (x,), (y,) = warp_transform("EPSG:4326", ds.crs, [lon], [lat])
245
+ t, half = ds.transform, PATCH // 2 * 10
246
+ return ds.crs, t.c + (x - t.c) // 10 * 10 - half, t.f - (t.f - y) // 10 * 10 + half
247
+
248
+
249
+ def _read_window(item, key, left, top, resampling):
250
+ with _open_asset(item, key) as ds:
251
+ win = from_bounds(left, top - PATCH * 10, left + PATCH * 10, top, ds.transform)
252
+ return ds.read(1, window=win, out_shape=(PATCH, PATCH), boundless=True,
253
+ fill_value=0, resampling=resampling)
254
+
255
+
256
+ def _read_band(item, band, left, top):
257
+ dn = _read_window(item, band, left, top, Resampling.bilinear).astype("float32")
258
+ rb = item.assets[band].extra_fields.get("raster:bands", [{}])[0]
259
+ new_baseline = item.properties.get("s2:processing_baseline", "00.00") >= "04.00"
260
+ offset = rb.get("offset", -0.1 if new_baseline else 0.0)
261
+ return np.clip(np.where(dn > 0, dn * rb.get("scale", 1e-4) + offset, 0.0), 0, 1)
262
+
263
+
264
+ # ---------------- One job ----------------
265
+ def _center(chw, half):
266
+ cy, cx = chw.shape[1] // 2, chw.shape[2] // 2
267
+ return chw[:, cy - half:cy + half, cx - half:cx + half]
268
+
269
+
270
+ def _run_job(lon, lat, scenes, path):
271
+ for item in _find_scenes(lon, lat, scenes):
272
+ crs, left, top = _patch_geo(item, lon, lat)
273
+ if (_read_window(item, "SCL", left, top, Resampling.nearest) == 0).mean() < 0.2:
274
+ break # skip swath-edge scenes that are mostly empty
275
+ else:
276
+ raise RuntimeError(f"no usable Sentinel-2 scene within +/-{SEARCH_DAYS} days")
277
+
278
+ with ThreadPoolExecutor(len(BANDS)) as pool:
279
+ lr = np.stack(list(pool.map(lambda b: _read_band(item, b, left, top), BANDS)))
280
+ tci = _gpu_products(lr)
281
+
282
+ tmp = f"{path}.tmp" # write then rename: no half-written .tif (paths are unique per job)
283
+ with rasterio.open(tmp, "w", driver="GTiff", width=tci.shape[2],
284
+ height=tci.shape[1], count=3, dtype="uint8", crs=crs,
285
+ transform=from_origin(left, top, OUT_RES, OUT_RES), photometric="ycbcr",
286
+ compress="jpeg", jpeg_quality=92, tiled=True) as dst:
287
+ dst.write(tci)
288
+ dst.update_tags(lon=lon, lat=lat, date=scenes[0].isoformat(), source=item.id)
289
+ os.replace(tmp, path)
290
+
291
+ sr_crop = Image.fromarray(_center(tci, round(500 / OUT_RES)).transpose(1, 2, 0))
292
+ lr_crop = Image.fromarray(_to_uint8(torch.from_numpy(_center(lr[RGB], 50))).numpy().transpose(1, 2, 0))
293
+ prev = Image.new("RGB", (2 * sr_crop.width + 8, sr_crop.height), "white")
294
+ prev.paste(lr_crop.resize(sr_crop.size, Image.NEAREST), (0, 0))
295
+ prev.paste(sr_crop, (sr_crop.width + 8, 0))
296
+ return path, prev
297
+
298
+
299
+ # ---------------- Public API ----------------
300
+ def run(lonlat, date, out_dir="opensr_out", location=None):
301
+ """Super-resolve every (location, date) pair to a 1 m RGB GeoTIFF.
302
+
303
+ lonlat: (lon, lat), a list of (lon, lat), or CSV text / a CSV file path
304
+ with lon in the first column and lat in the second (header optional).
305
+ date: "YYYY-MM-DD" or a list of them. With no pass on that exact day,
306
+ the nearest scene within +/- SEARCH_DAYS days is used.
307
+ out_dir: each call writes to a new {out_dir}/run_<timestamp>/ plus a .zip of it.
308
+ location: name used in the file names: one name for every point, or a list
309
+ with one name per point. Files are output_<date>_<name>.tif, with
310
+ (1), (2)... added when a name repeats for the same date. Without a
311
+ name: output_<date>_run_1.tif, output_<date>_run_2.tif, ...
312
+
313
+ Returns one Result per (location, date), sorted by location then date.
314
+ Failed jobs are returned too, with `error` set and `path` None.
315
+ """
316
+ points, dates = _points(lonlat), _dates(date)
317
+ names = _locations(location, len(points))
318
+ _load_model()
319
+ run_dir = os.path.join(out_dir, f"run_{datetime.now():%Y%m%d_%H%M%S}")
320
+ os.makedirs(run_dir, exist_ok=True)
321
+
322
+ with ThreadPoolExecutor(len(dates)) as ex:
323
+ scenes = dict(zip(dates, ex.map(lambda d: _search_date(d, points), dates)))
324
+
325
+ jobs = [Result(i, lon, lat, d) for (i, (lon, lat)), d in product(enumerate(points, 1), dates)]
326
+ paths = [os.path.join(run_dir, f"{s}.tif") for s in _file_stems(jobs, names)]
327
+ with ThreadPoolExecutor(min(len(jobs), 16)) as ex:
328
+ futs = {ex.submit(_run_job, r.lon, r.lat, scenes[r.date], p): r for r, p in zip(jobs, paths)}
329
+ for n, f in enumerate(as_completed(futs), 1):
330
+ r = futs[f]
331
+ try:
332
+ r.path, r.preview = f.result()
333
+ except Exception as e:
334
+ r.error = str(e)
335
+ print(f"\nFAIL #{r.index} ({r.lon}, {r.lat}) {r.date}: {e}")
336
+ print(f"\r{n}/{len(jobs)} done", end="", flush=True)
337
+
338
+ zip_path = f"{run_dir}.zip"
339
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_STORED) as z: # tifs already compressed
340
+ for name in sorted(os.listdir(run_dir)):
341
+ z.write(os.path.join(run_dir, name), name)
342
+ ok = sum(r.error is None for r in jobs)
343
+ print(f"\rDone: {ok} ok, {len(jobs) - ok} failed. Saved: {run_dir}/ + {zip_path}")
344
+ _show_previews(jobs)
345
+ return jobs
346
+
347
+
348
+ def _show_previews(jobs):
349
+ """Draw each preview inline when running in a Jupyter/Colab kernel; no-op elsewhere."""
350
+ try:
351
+ from IPython import get_ipython
352
+ from IPython.display import display
353
+ except ImportError:
354
+ return
355
+ if getattr(get_ipython(), "kernel", None) is None: # plain script or terminal IPython
356
+ return
357
+ for r in jobs:
358
+ if r.preview:
359
+ print(f"#{r.index} ({r.lon}, {r.lat}) {r.date} {os.path.basename(r.path)}")
360
+ display(r.preview)
@@ -0,0 +1,81 @@
1
+ Metadata-Version: 2.4
2
+ Name: opensr-sr1m
3
+ Version: 0.1.0
4
+ Summary: Sentinel-2 10 m to 1 m RGB super-resolution for points and dates
5
+ Author: Eavheang
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/Eavheang/openSR
8
+ Project-URL: Issues, https://github.com/Eavheang/openSR/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Scientific/Engineering :: GIS
12
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
13
+ Requires-Python: >=3.9
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: numpy
17
+ Requires-Dist: pillow
18
+ Requires-Dist: torch
19
+ Requires-Dist: rasterio
20
+ Requires-Dist: shapely
21
+ Requires-Dist: planetary-computer
22
+ Requires-Dist: pystac-client
23
+ Requires-Dist: mlstac
24
+ Requires-Dist: sen2sr
25
+ Requires-Dist: opensr-srgan>=1.1.0
26
+ Dynamic: license-file
27
+
28
+ # opensr
29
+
30
+ Sentinel-2 L2A (Planetary Computer) 10 m to 1 m RGB super-resolution for any
31
+ set of locations and dates. The SEN2-SR model takes 10 m to 2.5 m; bicubic
32
+ resampling plus sharpening then puts it on a 1 m grid. Detail finer than
33
+ ~2.5 m is not recovered, so treat 1 m as a pixel size, not true resolution.
34
+
35
+ Each (location, date) pair becomes a 5.12 km GeoTIFF (5120 x 5120 px at 1 m);
36
+ a run folder and a zip of it are written per call.
37
+
38
+ ## Install
39
+
40
+ ```bash
41
+ pip install opensr-sr1m
42
+ # or, from a local checkout
43
+ pip install -e .
44
+ ```
45
+
46
+ ## Use
47
+
48
+ ```python
49
+ import opensr.inferutils
50
+
51
+ # one location, one date -> output_2026-11-08_Phnom_Penh.tif
52
+ opensr.inferutils.run((104.9160, 11.5564), "2026-11-08", location="Phnom Penh")
53
+
54
+ # many locations x many dates (every combination is processed),
55
+ # one name per point -> output_2026-10-14_Site_A.tif, ...
56
+ results = opensr.inferutils.run(
57
+ [(105.392986, 14.121299), (104.9160, 11.5564)],
58
+ ["2026-10-14", "2026-10-29", "2026-11-08"],
59
+ location=["Site A", "Phnom Penh"],
60
+ out_dir="/content/s2dr3_out",
61
+ )
62
+
63
+ # CSV text or a CSV file path also work (lon first, lat second, header optional)
64
+ opensr.inferutils.run("points.csv", ["2026-11-08"])
65
+ ```
66
+
67
+ File names are `output_<date>_<location>.tif`, using the requested date.
68
+ When the same name repeats for one date, later files get `(1)`, `(2)`, ...
69
+ (`output_2026-11-08_Phnom_Penh(1).tif`). Without `location` the files are
70
+ `output_<date>_run_1.tif`, `output_<date>_run_2.tif`, ...
71
+ The scene actually used can be a few days from the requested date; its ID
72
+ (which includes its acquisition time) is stored in each GeoTIFF's `source` tag.
73
+
74
+ `run` returns one `Result` per (location, date) with `path` (GeoTIFF),
75
+ `preview` (PIL image: 10 m input | 1 m output, centre 1 km) and `error`
76
+ (set when that job failed). In Jupyter or Colab, `run` also draws every
77
+ preview inline under the progress output; in a plain script it only returns
78
+ the results.
79
+
80
+ The model weights download once to `~/.cache/opensr/` on the first call.
81
+ A GPU is used when available; CPU works but is slow.
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/opensr/__init__.py
5
+ src/opensr/inferutils.py
6
+ src/opensr_sr1m.egg-info/PKG-INFO
7
+ src/opensr_sr1m.egg-info/SOURCES.txt
8
+ src/opensr_sr1m.egg-info/dependency_links.txt
9
+ src/opensr_sr1m.egg-info/requires.txt
10
+ src/opensr_sr1m.egg-info/top_level.txt
@@ -0,0 +1,10 @@
1
+ numpy
2
+ pillow
3
+ torch
4
+ rasterio
5
+ shapely
6
+ planetary-computer
7
+ pystac-client
8
+ mlstac
9
+ sen2sr
10
+ opensr-srgan>=1.1.0