masking
MaskConvention
Bases: IntEnum
Enum for masking conventions to indicate the nodata value as 0 or 1.
In numpy, 1 indicates nodata https://numpy.org/doc/stable/reference/maskedarray.generic.html#what-is-a-masked-array
In SNAPHU (and isce2, and some water masks), the binary mask you pass has the opposite convention, where 0s mean the pixel is invalid (nodata).
Source code in src/dolphin/masking.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
get_nodata_value()
Return the nodata value for this convention.
Source code in src/dolphin/masking.py
45 46 47 | |
MaskingError
Bases: ValueError
Exception indicating the mask was improperly built, no valid pixels remain.
Source code in src/dolphin/masking.py
24 25 | |
combine_mask_files(mask_files, output_file, dtype='uint8', output_convention=MaskConvention.ZERO_IS_NODATA, input_conventions=None, combine_method='any', raise_on_empty=True)
Combine multiple mask files into a single mask file.
All mask_files must be the same size and projected on the same grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_files
|
list of Path or str
|
list of mask files to combine. |
required |
output_file
|
PathOrStr
|
Path to the combined output file. |
required |
dtype
|
str
|
Data type of output file. Default is uint8. |
'uint8'
|
output_convention
|
MaskConvention
|
Convention to use for output mask. Default is SNAPHU, where 0 indicates invalid pixels. |
ZERO_IS_NODATA
|
input_conventions
|
list of MaskConvention
|
Convention to use for each input mask. Default is None, where it is assumed all masks use the "0 is invalid" convention. |
None
|
combine_method
|
str
|
Logical operation to use to combine masks. Default is 'any', which means the output is masked where any of the input masks indicated a masked pixel (the masked region grows larger). If 'all', the only pixels masked are those in which all input masks indicated a masked pixel (the masked region shrinks). |
= 'any', choices = ['any', 'all']
|
raise_on_empty
|
bool
|
If True, raises a |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/dolphin/masking.py
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 | |
create_bounds_mask(bounds, bounds_wkt, output_filename, like_filename, bounds_epsg=4326, overwrite=False)
Create a boolean raster mask where 1 is inside the given bounds and 0 is outside.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
tuple
|
(min x, min y, max x, max y) of the area of interest |
required |
bounds_wkt
|
tuple
|
Well known text (WKT) string describing Polygon of the area of interest. Alternative to bounds. Cannot pass both bounds and bounds_wkt. |
required |
like_filename
|
Filename
|
Reference file to copy the shape, extent, and projection. |
required |
output_filename
|
Filename
|
Output filename for the mask |
required |
bounds_epsg
|
int
|
EPSG code of the coordinate system of the bounds. Default is 4326 (lat/lon coordinates for the bounds). |
4326
|
overwrite
|
bool
|
Overwrite the output file if it already exists, by default False |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither bounds nor bounds_wkt, or both bounds and bounds_wkt, is passed. |
Source code in src/dolphin/masking.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 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 | |
load_mask_as_numpy(mask_file)
Load mask_file as a lazy block-reading boolean array.
The returned object supports arr[row_slice, col_slice] and .all()
so it can be used anywhere the previous full-array return was used, while
avoiding loading the entire raster into memory at once.
The mask file is expected to use 0 for invalid data and 1 for good data. The output follows the numpy masking convention where True indicates nodata and False indicates valid data (i.e. the values are inverted).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_file
|
PathOrStr
|
Path to the mask file. |
required |
Returns:
| Type | Description |
|---|---|
_LazyMask
|
A lazy array-like where True values indicate nodata (invalid) pixels and False values indicate valid data pixels. |
Source code in src/dolphin/masking.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |