io
BackgroundBlockWriter
Bases: BackgroundWriter
Class to write data to multiple files in the background using gdal bindings.
Source code in src/dolphin/io/_writers.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
write(data, filename, row_start, col_start, band=None)
Write out an ndarray to a subset of the pre-made filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ArrayLike
|
2D or 3D data array to save. |
required |
filename
|
Filename
|
list of output files to save to, or (if cur_block is 2D) a single file. |
required |
row_start
|
int
|
Row index to start writing at. |
required |
col_start
|
int
|
Column index to start writing at. |
required |
band
|
int
|
Band index in the file to write. Defaults to None, which uses first band, or whole raster for 3D data. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If length of |
Source code in src/dolphin/io/_writers.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
BackgroundRasterWriter
Bases: BackgroundWriter, DatasetWriter
Class to write data to files in a background thread.
Source code in src/dolphin/io/_writers.py
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
closed
property
bool : True if the dataset is closed.
close()
Close the underlying dataset and stop the background thread.
Source code in src/dolphin/io/_writers.py
393 394 395 396 | |
write(key, value)
Write out an ndarray to a subset of the pre-made filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
tuple[Index, ...]
|
The key of the data to write. |
required |
value
|
ndarray
|
The block of data to write. |
required |
Source code in src/dolphin/io/_writers.py
376 377 378 379 380 381 382 383 384 385 386 387 388 | |
BackgroundReader
Bases: BackgroundWorker
Base class for reading data in a background thread (pre-fetching).
After instantiating an object, a client sends it data selection parameters
(slices, indices, etc.) via the queue_read method and retrieves the result
with the get_data method. In order to get useful concurrency, that
usually means you'll want to queue the read for the next data block before
starting work on the current block. The reader remains active until
notify_finished is called and all blocks have been retrieved. Subclasses
must define the read method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nq
|
int
|
Number of read results that can be stored before blocking, <= 0 for unbounded. Default is 1. |
1
|
timeout
|
float
|
Interval in seconds used to check for finished notification once write queue is empty. |
_DEFAULT_TIMEOUT
|
Source code in src/dolphin/io/_background.py
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 | |
get_data()
Retrieve the least-recently read chunk of data.
Blocks until a result is available.
Same output interface as read.
Source code in src/dolphin/io/_background.py
250 251 252 253 254 255 256 | |
queue_read(*args, **kw)
Add selection parameters (slices, etc.) to the read queue to be processed.
Same input interface as read.
Source code in src/dolphin/io/_background.py
242 243 244 245 246 247 | |
read(*args, **kw)
abstractmethod
User-defined method for reading a chunk of data.
Source code in src/dolphin/io/_background.py
262 263 264 | |
BackgroundStackWriter
Bases: BackgroundWriter, DatasetStackWriter
Class to write 3D data to multiple files in a background thread.
Will create/overwrite the files in file_list if they exist.
Source code in src/dolphin/io/_writers.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 | |
closed
property
bool : True if the dataset is closed.
close()
Close the underlying dataset and stop the background thread.
Source code in src/dolphin/io/_writers.py
509 510 511 | |
write(data, row_start, col_start, band=None)
Write out an ndarray to a subset of the pre-made filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ArrayLike
|
3D data array to save. |
required |
row_start
|
int
|
Row index to start writing at. |
required |
col_start
|
int
|
Column index to start writing at. |
required |
band
|
int
|
Band index in the file to write. Defaults to None, which uses first band, or whole raster for 3D data. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If length of |
Source code in src/dolphin/io/_writers.py
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 | |
BackgroundWorker
Bases: ABC
Base class for doing work in a background thread.
After instantiating an object, a client sends it work with the queue_work
method and retrieves the result with the get_result method (hopefully
after doing something else useful in between). The worker remains active
until notify_finished is called. Subclasses must define the process
method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_work_queue
|
int
|
Max number of work items to queue before blocking, <= 0 for unbounded. |
0
|
num_results_queue
|
int
|
Max number of results to generate before blocking, <= 0 for unbounded. |
0
|
store_results
|
bool
|
Whether to store return values of |
True
|
timeout
|
float
|
Interval in seconds used to check for finished notification once work queue is empty. |
_DEFAULT_TIMEOUT
|
Notes
The usual caveats about Python threading apply. It's typically a poor choice for concurrency unless the global interpreter lock (GIL) has been released, which can happen in IO calls and compiled extensions.
Source code in src/dolphin/io/_background.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
get_result()
Get the least-recent value from the result queue.
Blocks until a result is available.
Same output interface as process.
Source code in src/dolphin/io/_background.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
notify_finished(timeout=None)
Signal that all work has finished.
Indicate that no more work will be added to the queue, and block until
all work has been processed.
If store_results=True also block until all results have been retrieved.
Source code in src/dolphin/io/_background.py
143 144 145 146 147 148 149 150 151 152 153 | |
process(*args, **kw)
abstractmethod
User-defined task to operate in background thread.
Source code in src/dolphin/io/_background.py
109 110 111 | |
queue_work(*args, **kw)
Add a job to the work queue to be executed.
Blocks if work queue is full.
Same input interface as process.
Source code in src/dolphin/io/_background.py
113 114 115 116 117 118 119 120 121 122 | |
BackgroundWriter
Bases: BackgroundWorker
Base class for writing data in a background thread.
After instantiating an object, a client sends it data with the queue_write
method. The writer remains active until notify_finished is called.
Subclasses must define the write method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nq
|
int
|
Number of write jobs that can be queued before blocking, <= 0 for unbounded. Default is 1. |
1
|
timeout
|
float
|
Interval in seconds used to check for finished notification once write queue is empty. |
_DEFAULT_TIMEOUT
|
Source code in src/dolphin/io/_background.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
num_queued
property
Number of items waiting in the queue to be written.
queue_write(*args, **kw)
Add data to the queue to be written.
Blocks if write queue is full.
Same interfaces as write.
Source code in src/dolphin/io/_background.py
187 188 189 190 191 192 193 | |
write(*args, **kw)
abstractmethod
User-defined method for writing data.
Source code in src/dolphin/io/_background.py
204 205 206 | |
BinaryReader
dataclass
Bases: DatasetReader
A flat binary file for storing array data.
See Also
HDF5Dataset RasterReader
Notes
This class does not store an open file object. Instead, the file is opened on-demand for reading or writing and closed immediately after each read/write operation. This allows multiple spawned processes to write to the file in coordination (as long as a suitable mutex is used to guard file access.)
Source code in src/dolphin/io/_readers.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
dtype
instance-attribute
numpy.dtype : Data-type of the array's elements.
filename
instance-attribute
pathlib.Path : The file path.
ndim
property
int : Number of array dimensions.
nodata = None
class-attribute
instance-attribute
Optional[float] : Value to use for nodata pixels.
shape
instance-attribute
tuple of int : Tuple of array dimensions.
from_gdal(filename, band=1, nodata=None)
classmethod
Create a BinaryReader from a GDAL-readable file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to read. |
required |
band
|
int
|
Band to read from the file, by default 1 |
1
|
nodata
|
float
|
Value to use for nodata pixels, by default None If None passed, will search for a nodata value in the file. |
None
|
Returns:
| Type | Description |
|---|---|
BinaryReader
|
The BinaryReader object. |
Source code in src/dolphin/io/_readers.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
BinaryStackReader
dataclass
Bases: BaseStackReader
Source code in src/dolphin/io/_readers.py
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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
from_file_list(file_list, shape_2d, dtype)
classmethod
Create a BinaryStackReader from a list of files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
Iterable[Filename]
|
Iterable of paths to the files to read. |
required |
shape_2d
|
tuple[int, int]
|
Shape of each file. |
required |
dtype
|
dtype
|
Data type of each file. |
required |
Returns:
| Type | Description |
|---|---|
BinaryStackReader
|
The BinaryStackReader object. |
Source code in src/dolphin/io/_readers.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
from_gdal(file_list, band=1, num_threads=1, nodata=None)
classmethod
Create a BinaryStackReader from a list of GDAL-readable files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
Sequence[Filename]
|
List of paths to the files to read. |
required |
band
|
int
|
Band to read from the file, by default 1 |
1
|
num_threads
|
int, optional (default 1)
|
Number of threads to use for reading. |
1
|
nodata
|
float
|
Manually set value to use for nodata pixels, by default None If None passed, will search for a nodata value in the file. |
None
|
Returns:
| Type | Description |
|---|---|
BinaryStackReader
|
The BinaryStackReader object. |
Source code in src/dolphin/io/_readers.py
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
BlockIndices
dataclass
Class holding slices for 2D array access.
Source code in src/dolphin/io/_blocks.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
BlockProcessor
Bases: Protocol
Protocol for a block-wise processing function.
Reads a block of data from each reader, processes it, and returns the result as an array-like object.
Source code in src/dolphin/io/_process.py
19 20 21 22 23 24 25 26 27 28 29 30 31 | |
DatasetReader
Bases: Protocol
An array-like interface for reading input datasets.
DatasetReader defines the abstract interface that types must conform to in order
to be read by functions which iterate in blocks over the input data.
Such objects must export NumPy-like dtype, shape, and ndim attributes,
and must support NumPy-style slice-based indexing.
Note that this protocol allows objects to be passed to dask.array.from_array
which needs .shape, .ndim, .dtype and support numpy-style slicing.
Source code in src/dolphin/io/_readers.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
dtype
instance-attribute
numpy.dtype : Data-type of the array's elements.
ndim
instance-attribute
int : Number of array dimensions.
shape
instance-attribute
tuple of int : Tuple of array dimensions.
__getitem__(key)
Read a block of data.
Source code in src/dolphin/io/_readers.py
78 79 80 | |
DatasetStackWriter
Bases: Protocol
An array-like interface for writing output datasets.
DatasetWriter defines the abstract interface that types must conform to in order
to be used by functions which write outputs in blocks.
Such objects must export NumPy-like dtype, shape, and ndim attributes,
and must support NumPy-style slice-based indexing for setting data..
Source code in src/dolphin/io/_writers.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
dtype
instance-attribute
numpy.dtype : Data-type of the array's elements.
ndim = 3
class-attribute
instance-attribute
int : Number of array dimensions.
shape
instance-attribute
tuple of int : Tuple of array dimensions.
__setitem__(key, value)
Write a block of data.
Source code in src/dolphin/io/_writers.py
140 141 142 | |
DatasetWriter
Bases: Protocol
An array-like interface for writing output datasets.
DatasetWriter defines the abstract interface that types must conform to in order
to be used by functions which write outputs in blocks.
Such objects must export NumPy-like dtype, shape, and ndim attributes,
and must support NumPy-style slice-based indexing for setting data..
Source code in src/dolphin/io/_writers.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
dtype
instance-attribute
numpy.dtype : Data-type of the array's elements.
ndim
instance-attribute
int : Number of array dimensions.
shape
instance-attribute
tuple of int : Tuple of array dimensions.
__setitem__(key, value)
Write a block of data.
Source code in src/dolphin/io/_writers.py
116 117 118 | |
EagerLoader
Bases: BackgroundReader
Class to pre-fetch data chunks in a background thread.
Source code in src/dolphin/io/_readers.py
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 | |
HDF5Reader
dataclass
Bases: DatasetReader
A Dataset in an HDF5 file.
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
Path | str
|
Location of HDF5 file. |
dset_name |
str
|
Path to the dataset within the file. |
chunks |
(tuple[int, ...], optional)
|
Chunk shape of the dataset, or None if file is unchunked. |
keep_open |
bool, optional (default False)
|
If True, keep the HDF5 file handle open for faster reading. |
See Also
BinaryReader RasterReader
Notes
If keep_open=True, this class does not store an open file object.
Otherwise, the file is opened on-demand for reading or writing and closed
immediately after each read/write operation.
If passing the HDF5Reader to multiple spawned processes, it is recommended
to set keep_open=False .
Source code in src/dolphin/io/_readers.py
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
dset_name
instance-attribute
str : The path to the dataset within the file.
filename
instance-attribute
pathlib.Path : The file path.
keep_open = False
class-attribute
instance-attribute
bool : If True, keep the HDF5 file handle open for faster reading.
keepdims = True
class-attribute
instance-attribute
bool : Maintain the dimension of the point array. If set to False, will
skip squeeze on outputs with one dimension size of 1. Default is True.
ndim
property
Int : Number of array dimensions.
nodata = None
class-attribute
instance-attribute
Optional[float] : Value to use for nodata pixels.
If None, looks for _FillValue or missing_value attributes on the dataset.
close()
Close the underlying HDF5 file handle if it was kept open.
Source code in src/dolphin/io/_readers.py
273 274 275 276 | |
HDF5StackReader
dataclass
Bases: BaseStackReader
A stack of datasets in an HDF5 file.
See Also
BinaryStackReader StackReader
Notes
If keep_open=True, this class stores an open file object.
Otherwise, the file is opened on-demand for reading or writing and closed
immediately after each read/write operation.
If passing the HDF5StackReader to multiple spawned processes, it is recommended
to set keep_open=False.
Source code in src/dolphin/io/_readers.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
from_file_list(file_list, dset_names, keep_open=False, num_threads=1, nodata=None)
classmethod
Create a HDF5StackReader from a list of files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
Iterable[Filename]
|
Iterable of paths to the files to read. |
required |
dset_names
|
str | Sequence[str]
|
Name of the dataset to read from each file. If a single string, will be used for all files. |
required |
keep_open
|
bool, optional (default False)
|
If True, keep the HDF5 file handles open for faster reading. |
False
|
num_threads
|
int, optional (default 1)
|
Number of threads to use for reading. |
1
|
nodata
|
float
|
Manually set value to use for nodata pixels, by default None If None passed, will search for a nodata value in the file. |
None
|
Returns:
| Type | Description |
|---|---|
HDF5StackReader
|
The HDF5StackReader object. |
Source code in src/dolphin/io/_readers.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
RasterReader
dataclass
Bases: DatasetReader
A single raster band of a GDAL-compatible dataset.
See Also
BinaryReader HDF5
Notes
If keep_open=True, this class does not store an open file object.
Otherwise, the file is opened on-demand for reading or writing and closed
immediately after each read/write operation.
If passing the RasterReader to multiple spawned processes, it is recommended
to set keep_open=False .
Source code in src/dolphin/io/_readers.py
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
band
instance-attribute
int : Band index (1-based).
chunks = None
class-attribute
instance-attribute
Optional[tuple[int, int]] : Chunk shape of the dataset, or None if unchunked.
crs
instance-attribute
rio.crs.CRS : The dataset's coordinate reference system.
driver
instance-attribute
str : Raster format driver name.
filename
instance-attribute
Filename : The file path.
keep_open = False
class-attribute
instance-attribute
bool : If True, keep the rasterio file handle open for faster reading.
keepdims = True
class-attribute
instance-attribute
bool : Maintain the dimension of the point array. If set to False, will
skip squeeze on outputs with one dimension size of 1. Default is True.
ndim
property
Int : Number of array dimensions.
nodata = None
class-attribute
instance-attribute
Optional[float] : Value to use for nodata pixels.
transform
instance-attribute
rasterio.transform.Affine : The dataset's georeferencing transformation matrix.
This transform maps pixel row/column coordinates to coordinates in the dataset's coordinate reference system.
close()
Close the underlying rasterio dataset if it was kept open.
Source code in src/dolphin/io/_readers.py
396 397 398 399 | |
RasterStackReader
dataclass
Bases: BaseStackReader
A stack of datasets for any GDAL-readable rasters.
See Also
BinaryStackReader HDF5StackReader
Notes
If keep_open=True, this class stores an open file object.
Otherwise, the file is opened on-demand for reading or writing and closed
immediately after each read/write operation.
Source code in src/dolphin/io/_readers.py
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | |
from_file_list(file_list, bands=1, keepdims=True, keep_open=False, num_threads=1, nodata=None)
classmethod
Create a RasterStackReader from a list of files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_list
|
Iterable[Filename]
|
Iterable of paths to the files to read. |
required |
bands
|
int | Sequence[int]
|
Band to read from each file. If a single int, will be used for all files. Default = 1. |
1
|
keepdims
|
bool
|
Maintain the dimension of the point array. If set to False, will
skip |
True
|
keep_open
|
bool, optional (default False)
|
If True, keep the rasterio file handles open for faster reading. |
False
|
num_threads
|
int, optional (default 1)
|
Number of threads to use for reading. |
1
|
nodata
|
float
|
Manually set value to use for nodata pixels, by default None |
None
|
Returns:
| Type | Description |
|---|---|
RasterStackReader
|
The RasterStackReader object. |
Source code in src/dolphin/io/_readers.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | |
RasterWriter
dataclass
Bases: DatasetWriter, AbstractContextManager['RasterWriter']
A single raster band in a GDAL-compatible dataset containing one or more bands.
Raster provides a convenient interface for using SNAPHU to unwrap ground-projected
interferograms in raster formats supported by the Geospatial Data Abstraction
Library (GDAL). It acts as a thin wrapper around a Rasterio dataset and a band
index, providing NumPy-like access to the underlying raster data.
Data access is performed lazily -- the raster contents are not stored in memory unless/until they are explicitly accessed by an indexing operation.
Raster objects must be closed after use in order to ensure that any data written
to them is flushed to disk and any associated file objects are closed. The Raster
class implements Python's context manager protocol, which can be used to reliably
ensure that the raster is closed upon exiting the context manager.
Source code in src/dolphin/io/_writers.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 285 286 287 288 289 290 291 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 | |
band = 1
class-attribute
instance-attribute
int : Band index in the file to write.
closed
property
bool : True if the dataset is closed.
filename
instance-attribute
str or Path : Path to the file to write.
height
property
int : The number of rows in the raster.
keep_bits = None
class-attribute
instance-attribute
int : For floating point rasters, the number of mantissa bits to keep.
width
property
int : The number of columns in the raster.
close()
Close the underlying dataset.
Has no effect if the dataset is already closed.
Source code in src/dolphin/io/_writers.py
298 299 300 301 302 303 304 | |
create(fp, width=None, height=None, dtype=None, driver=None, crs=None, transform=None, *, like_filename=None, keep_bits=None, **kwargs)
classmethod
Create a new single-band raster dataset.
If another raster is passed via the like argument, the new dataset will
inherit the shape, data-type, driver, coordinate reference system (CRS), and
geotransform of the reference raster. Driver-specific dataset creation options
such as chunk size and compression flags may also be inherited.
All other arguments take precedence over like and may be used to override
attributes of the reference raster when creating the new raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
str or path - like
|
File system path or URL of the local or remote dataset. |
required |
width
|
int or None
|
The numbers of columns and rows of the raster dataset. Required if |
None
|
height
|
int or None
|
The numbers of columns and rows of the raster dataset. Required if |
None
|
dtype
|
data - type or None
|
Data-type of the raster dataset's elements. Must be convertible to a
|
None
|
driver
|
str or None
|
Raster format driver name. If None, the method will attempt to infer the driver from the file extension. Defaults to None. |
None
|
crs
|
str, dict, rasterio.crs.CRS, or None; optional
|
The coordinate reference system. If None, the CRS of |
None
|
transform
|
Affine or None
|
Affine transformation mapping the pixel space to geographic space. If None,
the geotransform of |
None
|
like_filename
|
Raster or None
|
An optional reference raster. If not None, the new raster will be created with the same metadata (shape, data-type, driver, CRS/geotransform, etc) as the reference raster. All other arguments will override the corresponding attribute of the reference raster. Defaults to None. |
None
|
keep_bits
|
int
|
Number of bits to preserve in mantissa. Defaults to None. Lower numbers will truncate the mantissa more and enable more compression. |
None
|
**kwargs
|
dict
|
Additional driver-specific creation options passed to |
{}
|
Source code in src/dolphin/io/_writers.py
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 | |
S3Path
Bases: GeneralPath
A convenience class to handle paths on S3.
This class relies on pathlib.Path for operations using urllib to parse the url.
If passing a url with a trailing slash, the slash will be preserved when converting back to string.
Note that pure path manipulation functions do not require boto3,
but functions which interact with S3 (e.g. exists(), .read_text()) do.
Attributes:
| Name | Type | Description |
|---|---|---|
bucket |
str
|
Name of bucket in the url |
path |
Path
|
The URL path after s3:// |
key |
str
|
Alias of |
Examples:
>>> from dolphin.io import S3Path
>>> s3_path = S3Path("s3://bucket/path/to/file.txt")
>>> str(s3_path)
's3://bucket/path/to/file.txt'
>>> s3_path.parent
S3Path("s3://bucket/path/to/")
>>> str(s3_path.parent)
's3://bucket/path/to/'
Source code in src/dolphin/io/_paths.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
key
property
Name of key/prefix within the bucket with leading slash removed.
__init__(s3_url, unsigned=False)
Create an S3Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s3_url
|
str or S3Path
|
The S3 url to parse. |
required |
unsigned
|
bool
|
If True, disable signing requests to S3. |
False
|
Source code in src/dolphin/io/_paths.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
exists()
Whether this path exists on S3.
Source code in src/dolphin/io/_paths.py
137 138 139 140 141 142 143 144 145 | |
from_bucket_key(bucket, key)
classmethod
Create a S3Path from the bucket name and key/prefix.
Matches API of some Boto3 functions which use this format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket
|
str
|
Name of S3 bucket. |
required |
key
|
str
|
S3 url of path after the bucket. |
required |
Source code in src/dolphin/io/_paths.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
read_bytes()
Download/read the S3 file as bytes.
Source code in src/dolphin/io/_paths.py
151 152 153 | |
read_text()
Download/read the S3 file as text.
Source code in src/dolphin/io/_paths.py
147 148 149 | |
resolve()
Resolve the path to an absolute path- S3 paths are always absolute.
Source code in src/dolphin/io/_paths.py
123 124 125 | |
to_gdal()
Convert this S3Path to a GDAL URL.
Source code in src/dolphin/io/_paths.py
189 190 191 | |
StackReader
Bases: DatasetReader, Protocol
An array-like interface for reading a 3D stack of input datasets.
StackReader defines the abstract interface that types must conform to in order
to be valid inputs to be read in functions like dolphin.ps.create_ps.
It is a specialization of [DatasetReader][] that requires a 3D shape.
Source code in src/dolphin/io/_readers.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
ndim = 3
class-attribute
instance-attribute
int : Number of array dimensions.
shape
instance-attribute
tuple of int : Tuple of array dimensions.
__len__()
Int : Number of images in the stack.
Source code in src/dolphin/io/_readers.py
98 99 100 | |
StridedBlockManager
dataclass
Class to handle slicing/trimming overlapping blocks with strides.
Source code in src/dolphin/io/_blocks.py
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 285 286 287 288 289 290 291 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 | |
arr_shape
instance-attribute
(row, col) of the full-res 2D image
block_shape
instance-attribute
(row, col) size of each input block to operate on at one time
half_window = field(default_factory=(lambda: HalfWindow(0, 0)))
class-attribute
instance-attribute
For multi-looking iterations, size of the (full-res) half window
in y/row and x/column direction.
Used to find overlaps between blocks and start_offset/end_margin for
iter_blocks.
input_padding_shape
property
Amount of extra padding the input blocks need.
Depends on the window size, and the strides.
output_margin
property
The output margins that we ignore while iterating.
Depends on the half window and strides.
The half_window is in full-res (input) coordinates (which would be the
amount to skip with no striding), so the output margin size is smaller
strides = field(default_factory=(lambda: Strides(1, 1)))
class-attribute
instance-attribute
Decimation/downsampling factor in y/row and x/column direction
get_trimming_block()
Compute the slices which trim output nodata values.
When the BlockIndex gets dilated (using strides) and padded (using
half_window), the result will have nodata around the edges.
The size of the nodata pixels in the full-res block is just
(half_window['y'], half_window['x'])
In the output (strided) coordinates, the number of nodata pixels is
shrunk by how many strides are taken.
Note that this is independent of which block we're on; the number of nodata pixels on the border is always the same.
Source code in src/dolphin/io/_blocks.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
iter_blocks()
Iterate over the input/output block indices.
Yields:
| Name | Type | Description |
|---|---|---|
output_block |
BlockIndices
|
The current slices for the output raster |
out_trim |
BlockIndices
|
Slices to use on a processed output block to remove nodata border pixels.
These may be relative (e.g. slice(1, -1)), not absolute like |
input_block |
BlockIndices
|
Slices used to load the full-res input data |
input_no_padding |
BlockIndices
|
Slices which point to the position within the full-res data without padding |
input_trim |
BlockIndices
|
Slices to use on full-res input block to remove padding from half-window. |
Source code in src/dolphin/io/_blocks.py
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 | |
VRTStack
Bases: StackReader
Class for creating a virtual stack of raster files.
Attributes:
| Name | Type | Description |
|---|---|---|
file_list |
list[Filename]
|
Paths or GDAL-compatible strings (NETCDF:...) for paths to files. |
outfile |
(Path, optional(default=Path('slc_stack.vrt')))
|
Name of output file to write |
dates |
list[list[DateOrDatetime]]
|
list, where each entry is all dates matched from the corresponding file
in |
use_abs_path |
(bool, optional(default=True))
|
Write the filepaths of the SLCs in the VRT as "relative=0" |
subdataset |
(str, optional)
|
Subdataset to use from the files in |
sort_files |
(bool, optional(default=True))
|
Sort the files in |
file_date_fmt |
(str, optional(default='%Y%m%d'))
|
Format string for parsing the dates from the filenames. Passed to [opera_utils.get_dates][]. |
Source code in src/dolphin/io/_readers.py
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 | |
shape
property
Get the 3D shape of the stack.
from_vrt_file(vrt_file, new_outfile=None, **kwargs)
classmethod
Create a new VRTStack using an existing VRT file.
Source code in src/dolphin/io/_readers.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 | |
read_stack(band=None, subsample_factor=1, rows=None, cols=None, masked=None, keepdims=True)
Read in the SLC stack.
Source code in src/dolphin/io/_readers.py
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 | |
copy_projection(src_file, dst_file)
Copy projection/geotransform from src_file to dst_file.
Source code in src/dolphin/io/_core.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
format_nc_filename(filename, ds_name=None)
Format an HDF5/NetCDF filename with dataset for reading using GDAL.
If filename is already formatted, or if filename is not an HDF5/NetCDF
file (based on the file extension), it is returned unchanged.
The driver prefix is chosen by filename:
.nc→NETCDF:"file":"//ds".h5whose granule prefix isNISAR_→HDF5:"file":"//ds"(NISAR raw HDF5; no CF metadata, and GDAL's NETCDF driver refuses it with "No such file or directory")- every other
.h5→NETCDF:"file":"//ds"(OPERA CSLCs, COMPASS CSLCs + static_layers, etc. ship CF-1.8- compliant HDF5 and depend on the NETCDF driver to read the grid mapping; the bare HDF5 driver opens the data but reports an identity geotransform)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or PathLike
|
Filename to format. |
required |
ds_name
|
str
|
Dataset name to use. If not provided for a .h5 or .nc file, an error is raised. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted filename. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/dolphin/io/_core.py
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
get_gtiff_options(max_error=None, compression_type='lzw', chunk_size=256, predictor=None, zlevel=1, use_16_bits=False)
Generate GTiff creation options for GDAL translate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_error
|
float
|
Maximum compression error. |
None
|
compression_type
|
str
|
Compression type to use (default is "lzw"). |
'lzw'
|
chunk_size
|
int
|
Size of the chunks for blockxsize and blockysize (default is 256). |
256
|
predictor
|
int or None
|
Predictor type to use (default is 3). Use None to omit the predictor. |
None
|
zlevel
|
int or None
|
Compression level for the 'deflate' and 'zstd' compression types (default is 1). Use None to omit the zlevel. |
1
|
use_16_bits
|
bool
|
If True, sets |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, str] | list[str]
|
List of GTiff creation options formatted for GDAL (if |
Source code in src/dolphin/io/_utils.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
get_raster_bounds(filename=None, ds=None)
Get the (left, bottom, right, top) bounds of the image.
Source code in src/dolphin/io/_core.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
get_raster_chunk_size(filename)
Get size the raster's chunks on disk.
This is called blockXsize, blockYsize by GDAL.
Source code in src/dolphin/io/_core.py
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 | |
get_raster_crs(filename)
Get the CRS from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
Returns:
| Type | Description |
|---|---|
CRS
|
CRS. |
Source code in src/dolphin/io/_core.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
get_raster_description(filename, band=1)
Get description of a raster band.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
band
|
int
|
Band to get description for. Default is 1. |
1
|
Source code in src/dolphin/io/_core.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 | |
get_raster_driver(filename)
Get the GDAL driver ShortName from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Driver name. |
Source code in src/dolphin/io/_core.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
get_raster_dtype(filename)
Get the data type from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
Returns:
| Type | Description |
|---|---|
dtype
|
Data type. |
Source code in src/dolphin/io/_core.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
get_raster_gt(filename)
Get the geotransform from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
6 floats representing a GDAL Geotransform. |
Source code in src/dolphin/io/_core.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
get_raster_metadata(filename, domain='')
Get metadata from a raster file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
domain
|
str
|
Domain to get metadata for. Default is "" (all domains). |
''
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary of metadata. |
Source code in src/dolphin/io/_core.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | |
get_raster_nodata(filename, band=1)
Get the nodata value from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
band
|
int
|
Band to get nodata value for, by default 1. |
1
|
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Nodata value, or None if not found. |
Source code in src/dolphin/io/_core.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
get_raster_units(filename, band=1)
Get units of a raster band.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
band
|
int
|
Band to get units for. Default is 1. |
1
|
Source code in src/dolphin/io/_core.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
get_raster_xysize(filename)
Get the xsize/ysize of a GDAL-readable raster.
Source code in src/dolphin/io/_core.py
402 403 404 405 406 407 | |
iter_blocks(arr_shape, block_shape, overlaps=(0, 0), start_offsets=(0, 0), end_margin=(0, 0))
Create a generator to get indexes for accessing blocks of a raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr_shape
|
tuple[int, int]
|
(num_rows, num_cols), full size of array to access |
required |
block_shape
|
tuple[int, int]
|
(height, width), size of blocks to load |
required |
overlaps
|
tuple[int, int]
|
(row_overlap, col_overlap), number of pixels to re-include from the previous block after sliding |
= (0, 0)
|
start_offsets
|
tuple[int, int]
|
Offsets from top left to start reading from |
= (0, 0)
|
end_margin
|
tuple[int, int]
|
Margin to avoid at the bottom/right of array |
= (0, 0)
|
Yields:
| Type | Description |
|---|---|
BlockIndices
|
Iterator of BlockIndices, which can be unpacked into (slice(row_start, row_stop), slice(col_start, col_stop)) |
Examples:
>>> list(iter_blocks((180, 250), (100, 100)))
[BlockIndices(row_start=0, row_stop=100, col_start=0, col_stop=100), BlockIndices(row_start=0, row_stop=100, col_start=100, col_stop=200), BlockIndices(row_start=0, row_stop=100, col_start=200, col_stop=250), BlockIndices(row_start=100, row_stop=180, col_start=0, col_stop=100), BlockIndices(row_start=100, row_stop=180, col_start=100, col_stop=200), BlockIndices(row_start=100, row_stop=180, col_start=200, col_stop=250)]
>>> list(map(tuple, iter_blocks((180, 250), (100, 100), overlaps=(10, 10))))
[(slice(0, 100, None), slice(0, 100, None)), (slice(0, 100, None), slice(90, 190, None)), (slice(0, 100, None), slice(180, 250, None)), (slice(90, 180, None), slice(0, 100, None)), (slice(90, 180, None), slice(90, 190, None)), (slice(90, 180, None), slice(180, 250, None))]
Source code in src/dolphin/io/_blocks.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
load_gdal(filename, *, band=None, subsample_factor=1, overview=None, rows=None, cols=None, masked=False)
Load a gdal file into a numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or Path
|
Path to the file to load. |
required |
band
|
int
|
Band to load. If None, load all bands as 3D array. |
None
|
subsample_factor
|
int or tuple[int, int]
|
Subsample the data by this factor. Default is 1 (no subsampling). Uses nearest neighbor resampling. |
1
|
overview
|
Optional[int]
|
If passed, will load an overview of the file. Raster must have existing overviews, or ValueError is raised. |
None
|
rows
|
slice
|
Rows to load. Default is None (load all rows). |
None
|
cols
|
slice
|
Columns to load. Default is None (load all columns). |
None
|
masked
|
bool
|
If True, return a masked array using the raster's |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
arr |
ndarray or MaskedArray
|
Array of shape (bands, y, x) or (y, x) if |
Source code in src/dolphin/io/_core.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 | |
process_blocks(readers, writer, func, block_shape=(512, 512), overlaps=(0, 0), num_threads=5)
Perform block-wise processing over blocks in readers, writing to writer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
readers
|
Sequence[StackReader]
|
Sequence of input readers to read data from. |
required |
writer
|
DatasetWriter
|
Output writer to write data to. |
required |
func
|
BlockProcessor
|
Function to process each block. |
required |
block_shape
|
tuple[int, int]
|
Shape of each block to process. |
(512, 512)
|
overlaps
|
tuple[int, int]
|
Amount of overlap between blocks in (row, col) directions. By default (0, 0). |
(0, 0)
|
num_threads
|
int
|
Number of threads to use, by default 5. |
5
|
Source code in src/dolphin/io/_process.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
repack_raster(raster_path, output_dir=None, keep_bits=None, block_shape=(1024, 1024), **output_options)
Repack a single raster file with GDAL Translate using provided options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster_path
|
Path
|
Path to the input raster file. |
required |
output_dir
|
Path
|
Directory to save the repacked rasters or None for in-place repacking. |
None
|
keep_bits
|
int
|
Number of bits to preserve in mantissa. Defaults to None. Lower numbers will truncate the mantissa more and enable more compression. |
None
|
block_shape
|
int | tuple[int, int]
|
Size of blocks to read in at one time. |
(1024, 1024)
|
**output_options
|
Keyword args passed to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
output_path |
Path
|
Path to newly created file.
If |
Source code in src/dolphin/io/_utils.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
repack_rasters(raster_files, output_dir=None, num_threads=4, keep_bits=None, block_shape=(1024, 1024), **output_options)
Recreate and compress a list of raster files.
Useful for rasters which were created in block and lost the full effect of compression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster_files
|
List[Path]
|
List of paths to the input raster files. |
required |
output_dir
|
Path
|
Directory to save the processed rasters or None for in-place processing. |
None
|
num_threads
|
int
|
Number of threads to use (default is 4). |
4
|
keep_bits
|
int
|
Number of bits to preserve in mantissa. Defaults to None. Lower numbers will truncate the mantissa more and enable more compression. |
None
|
block_shape
|
int | tuple[int, int]
|
Size of blocks to read in at one time. |
(1024, 1024)
|
**output_options
|
Creation options to pass to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
output_path |
Path
|
Path to newly created file.
If |
Source code in src/dolphin/io/_utils.py
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 | |
round_mantissa(z, keep_bits=10, chunk_rows=1024)
Zero out mantissa bits of elements of array in place.
Drops a specified number of bits from the floating point mantissa, leaving an array more amenable to compression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
ndarray
|
Real or complex array whose mantissas are to be zeroed out |
required |
keep_bits
|
int
|
Number of bits to preserve in mantissa. Lower numbers will truncate the mantissa more and enable more compression. Default is 10. |
10
|
chunk_rows
|
int
|
Number of rows to process at a time to limit memory usage. Default is 1024. |
1024
|
References
https://numcodecs.readthedocs.io/en/v0.12.1/_modules/numcodecs/bitround.html
Source code in src/dolphin/io/_utils.py
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 285 286 287 288 | |
set_raster_description(filename, description, band=1)
Set description on a raster band.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
description
|
str
|
Description to set. |
required |
band
|
int
|
Band to set description for. Default is 1. |
1
|
Source code in src/dolphin/io/_core.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
set_raster_metadata(filename, metadata, domain='')
Set metadata on a raster file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
metadata
|
dict
|
Dictionary of metadata to set. |
required |
domain
|
str
|
Domain to set metadata for. Default is "" (all domains). |
''
|
Source code in src/dolphin/io/_core.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |
set_raster_nodata(filename, nodata, band=None)
Set the nodata value for a raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
nodata
|
float
|
The nodata value to set. |
required |
band
|
int
|
The band to set the nodata value for, by default None (sets the nodata value for all bands). |
None
|
Source code in src/dolphin/io/_core.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
set_raster_units(filename, units, band=None)
Set units on a raster band.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Filename
|
Path to the file to load. |
required |
units
|
str
|
Units to set. |
required |
band
|
int
|
Band to set units for. Default is None, which sets for all bands. |
None
|
Source code in src/dolphin/io/_core.py
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | |
write_arr(*, arr, output_name, like_filename=None, driver='GTiff', options=None, nbands=None, shape=None, dtype=None, geotransform=None, strides=None, projection=None, nodata=None, units=None, description=None)
Save an array to output_name.
If like_filename if provided, copies the projection/nodata.
Options can be overridden by passing driver/nbands/dtype.
If arr is None, create an empty file with the same x/y shape as like_filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ArrayLike
|
Array to save. If None, create an empty file. |
required |
output_name
|
str or Path
|
Path to save the file to. |
required |
like_filename
|
str or Path
|
Path to a file to copy raster shape/metadata from. |
None
|
driver
|
str
|
GDAL driver to use. Default is "GTiff". |
'GTiff'
|
options
|
list
|
list of options to pass to the driver. Default is DEFAULT_TIFF_OPTIONS. |
None
|
nbands
|
int
|
Number of bands to save. Default is 1. |
None
|
shape
|
tuple
|
(rows, cols) of desired output file.
Overrides the shape of the output file, if using |
None
|
dtype
|
DTypeLike
|
Data type to save. Default is |
None
|
geotransform
|
list
|
Geotransform to save. Default is the geotransform of like_filename. See https://gdal.org/tutorials/geotransforms_tut.html . |
None
|
strides
|
dict
|
If using |
None
|
projection
|
str or int
|
Projection to save. Default is the projection of like_filename.
Possible values are anything parse-able by |
None
|
nodata
|
float
|
Nodata value to save.
Default is the nodata of band 1 of |
None
|
units
|
str
|
Units of the data. Default is None. Value is stored in the metadata as "units". |
None
|
description
|
str
|
Description of the raster bands stored in the metadata. |
None
|
Source code in src/dolphin/io/_core.py
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 | |
write_block(cur_block, filename, row_start, col_start, band=None, dset=None)
Write out an ndarray to a subset of the pre-made filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cur_block
|
ArrayLike
|
2D or 3D data array |
required |
filename
|
Filename
|
list of output files to save to, or (if cur_block is 2D) a single file. |
required |
row_start
|
int
|
Row index to start writing at. |
required |
col_start
|
int
|
Column index to start writing at. |
required |
band
|
int
|
Raster band to write to within |
None
|
dset
|
str
|
(For writing to HDF5/NetCDF files) The name of the string dataset
within |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If length of |
Source code in src/dolphin/io/_core.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 | |