dataset.preprocessing.cog_conversion¶
cog_conversion ¶
HiRISE preprocessing utilities.
Provides:
-
:func:
jp2_to_cog— Convert a single JP2 to a Cloud-Optimized GeoTIFF (COG) sidecar. COGs use internal 512×512 tiling so rasterio can decompress only the tiles that overlap a small query window, giving 10–100× faster random-access reads compared to JPEG2000 during ML training. -
:func:
convert_all— Batch-convert all JP2 files under a root directory using a process pool.
CLI usage::
# Convert all JP2s to COG GeoTIFFs (run once before training)
uv run python -m src.preprocessing --root /scratch/mars_hirise --workers 4
# Overwrite existing .tif sidecars
uv run python -m src.preprocessing --root /scratch/mars_hirise --workers 4 --overwrite
jp2_to_cog ¶
Convert a single HiRISE JP2 to a Cloud-Optimized GeoTIFF sidecar.
The COG is written alongside the source JP2 with the same stem and a
.tif extension. :meth:~temp.MarsHiRISE.prefer_cog will
automatically use it when it exists, bypassing the slower JP2 path.
The conversion proceeds in two passes:
- Write a temporary intermediate GeoTIFF so that overviews can be built on a writeable dataset (rasterio requires this).
- Copy the intermediate file to the final COG path with
copy_src_overviews=Trueto embed the overviews efficiently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jp2_path
|
Path
|
Path to the source JPEG2000 file. |
required |
overwrite
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the output COG on success, or |
Source code in src/dataset/preprocessing/cog_conversion.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
img_to_cog ¶
Convert a single HiRISE DTM .IMG (PDS3 float32) to a Cloud-Optimized GeoTIFF.
DTM .IMG files are flat binary rasters with attached PDS3 labels.
Unlike JP2 orthoimages, they are not compressed, so the conversion is
mainly about adding internal 512×512 tiling and overviews for fast
random-access reads during ML training.
Float32 elevation data uses predictor=3 (floating-point differencing)
for better deflate compression. The nodata sentinel
-3.4028226550889045e+38 is preserved in the output GeoTIFF metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_path
|
Path
|
Path to the PDS3 |
required |
overwrite
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the output COG on success, or |
Source code in src/dataset/preprocessing/cog_conversion.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
convert_all ¶
convert_all(root: Path, workers: int = 4, overwrite: bool = False, skip_jp2: bool = False, skip_dtm: bool = False) -> dict[str, int]
Convert all JP2 and DTM .IMG files under root to COG GeoTIFF sidecars.
Uses a :class:~concurrent.futures.ProcessPoolExecutor to parallelise
the CPU-bound conversion. Each worker calls :func:jp2_to_cog or
:func:img_to_cog.
.. note::
Large JP2 files (up to 2.5 GB) are fully decompressed in memory during
the intermediate write step. The actual worker count is automatically
capped by :func:_safe_worker_count based on available RAM and the
estimated decompressed size of the JP2 files found under root; the
workers argument is therefore treated as an upper bound.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Path
|
Dataset root directory containing JP2 and/or IMG files. |
required |
workers
|
int
|
Number of parallel conversion processes. |
4
|
overwrite
|
bool
|
Re-convert files that already have a |
False
|
skip_jp2
|
bool
|
Skip JP2 orthoimage conversion (only process DTM .IMG). |
False
|
skip_dtm
|
bool
|
Skip DTM .IMG conversion (only process JP2 orthoimages). |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dict with keys |
Source code in src/dataset/preprocessing/cog_conversion.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |