|
| 1 | +import os |
| 2 | +import isce3 |
| 3 | +import h5py |
| 4 | +import numpy as np |
| 5 | +from osgeo import gdal |
| 6 | +from dataclasses import dataclass |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class BlockParam: |
| 10 | + ''' |
| 11 | + Class for block specific parameters |
| 12 | + Facilitate block parameters exchange between functions |
| 13 | + ''' |
| 14 | + # Length of current block to filter; padding not included |
| 15 | + block_length: int |
| 16 | + |
| 17 | + # First line to write to for current block |
| 18 | + write_start_line: int |
| 19 | + |
| 20 | + # First line to read from dataset for current block |
| 21 | + read_start_line: int |
| 22 | + |
| 23 | + # Number of lines to read from dataset for current block |
| 24 | + read_length: int |
| 25 | + |
| 26 | + # Padding to be applied to read in current block. First tuple is padding to |
| 27 | + # be applied to top/bottom (along length). Second tuple is padding to be |
| 28 | + # applied to left/right (along width). Values in second tuple do not change; |
| 29 | + # included in class so one less value is passed between functions. |
| 30 | + block_pad: tuple |
| 31 | + |
| 32 | + # Width of current block. Value does not change per block; included to |
| 33 | + # in class so one less value is to be passed between functions. |
| 34 | + data_width: int |
| 35 | + |
| 36 | +def np2gdal_dtype(np_dtype): |
| 37 | + dict_np2gdal = { |
| 38 | + np.byte: gdal.GDT_Byte, |
| 39 | + np.ushort: gdal.GDT_UInt16, |
| 40 | + np.short: gdal.GDT_Int16, |
| 41 | + np.uintc: gdal.GDT_UInt32, |
| 42 | + np.intc: gdal.GDT_Int32, |
| 43 | + np.float32: gdal.GDT_Float32, |
| 44 | + np.float64: gdal.GDT_Float64, |
| 45 | + np.complex64: gdal.GDT_CFloat32, |
| 46 | + np.complex128: gdal.GDT_CFloat64} |
| 47 | + if np_dtype not in dict_np2gdal: |
| 48 | + # throw unsupported error |
| 49 | + pass |
| 50 | + else: |
| 51 | + return dict_np2gdal[int_dtype] |
| 52 | + |
| 53 | +def block_param_generator(lines_per_block, data_shape, pad_shape): |
| 54 | + ''' Generator for block specific parameter class. |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + lines_per_block: int |
| 59 | + Lines to be processed per block (in batch). |
| 60 | + data_shape: tuple(int, int) |
| 61 | + Length and width of input raster. |
| 62 | + pad_shape: tuple(int, int) |
| 63 | + Padding for the length and width of block to be filtered. |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + _: BlockParam |
| 68 | + BlockParam object for current block |
| 69 | + ''' |
| 70 | + data_length, data_width = data_shape |
| 71 | + pad_length, pad_width = pad_shape |
| 72 | + |
| 73 | + # Calculate number of blocks to break raster into |
| 74 | + num_blocks = int(np.ceil(data_length / lines_per_block)) |
| 75 | + |
| 76 | + for block in range(num_blocks): |
| 77 | + start_line = block * lines_per_block |
| 78 | + |
| 79 | + # Discriminate between first, last, and middle blocks |
| 80 | + first_block = block == 0 |
| 81 | + last_block = block == num_blocks - 1 or num_blocks == 1 |
| 82 | + middle_block = not first_block and not last_block |
| 83 | + |
| 84 | + # Determine block size; Last block uses leftover lines |
| 85 | + block_length = data_length - start_line if last_block else lines_per_block |
| 86 | + |
| 87 | + # Determine padding along length. Full padding for middle blocks |
| 88 | + # Half padding for start and end blocks |
| 89 | + read_length_pad = pad_length if middle_block else pad_length // 2 |
| 90 | + |
| 91 | + # Determine 1st line of output |
| 92 | + write_start_line = block * lines_per_block |
| 93 | + |
| 94 | + # Determine 1st dataset line to read. Subtract half padding length |
| 95 | + # to account for additional lines to be read. |
| 96 | + read_start_line = block * lines_per_block - pad_length // 2 |
| 97 | + |
| 98 | + # If applicable, save negative start line as deficit to account for later |
| 99 | + read_start_line, start_line_deficit = ( |
| 100 | + 0, read_start_line) if read_start_line < 0 else ( |
| 101 | + read_start_line, 0) |
| 102 | + |
| 103 | + # Initial guess at number lines to read; accounting for negative start at the end |
| 104 | + read_length = block_length + read_length_pad |
| 105 | + if not first_block: |
| 106 | + read_length -= abs(start_line_deficit) |
| 107 | + |
| 108 | + # Check for over-reading and adjust lines read as needed |
| 109 | + end_line_deficit = min( |
| 110 | + data_length - read_start_line - read_length, 0) |
| 111 | + read_length -= abs(end_line_deficit) |
| 112 | + |
| 113 | + # Determine block padding in length |
| 114 | + if first_block: |
| 115 | + # Only the top part of the block should be padded. If end_deficit_line=0 |
| 116 | + # we have a sufficient number of lines to be read in the subsequent block |
| 117 | + top_pad = pad_length // 2 |
| 118 | + bottom_pad = abs(end_line_deficit) |
| 119 | + elif last_block: |
| 120 | + # Only the bottom part of the block should be padded |
| 121 | + top_pad = abs( |
| 122 | + start_line_deficit) if start_line_deficit < 0 else 0 |
| 123 | + bottom_pad = pad_length // 2 |
| 124 | + else: |
| 125 | + # Top and bottom should be added taking into account line deficit |
| 126 | + top_pad = abs( |
| 127 | + start_line_deficit) if start_line_deficit < 0 else 0 |
| 128 | + bottom_pad = abs(end_line_deficit) |
| 129 | + |
| 130 | + block_pad = ((top_pad, bottom_pad), |
| 131 | + (pad_width // 2, pad_width // 2)) |
| 132 | + |
| 133 | + yield BlockParam(block_length, write_start_line, read_start_line, read_length, block_pad, data_width) |
| 134 | + |
| 135 | + return |
| 136 | + |
| 137 | +def get_raster_info(raster): |
| 138 | + ''' Determine raster shape based on raster |
| 139 | + type (h5py.Dataset or GDAL-friendly raster). |
| 140 | +
|
| 141 | + Parameters |
| 142 | + ---------- |
| 143 | + raster: h5py.Dataset or str |
| 144 | + Raster whose size is to be determined. String value represents |
| 145 | + filepath for GDAL rasters. |
| 146 | +
|
| 147 | + Returns |
| 148 | + ------- |
| 149 | + data_width: int |
| 150 | + Width of raster. |
| 151 | + data_length: int |
| 152 | + Length of raster. |
| 153 | + ''' |
| 154 | + if isinstance(raster, h5py.Dataset): |
| 155 | + return raster.shape, raster.dtype |
| 156 | + else: |
| 157 | + # Open input data using GDAL to get raster length |
| 158 | + ds = gdal.Open(raster, gdal.GA_ReadOnly) |
| 159 | + data_length = ds.RasterYSize |
| 160 | + data_width = ds.RasterXSize |
| 161 | + data_type = ds.GetRasterBand(1).DataType |
| 162 | + return (data_length, data_width), data_type |
| 163 | + |
| 164 | +def get_raster_block(raster, block_param): |
| 165 | + ''' Get a block of data from raster. |
| 166 | + Raster can be a HDF5 file or a GDAL-friendly raster |
| 167 | +
|
| 168 | + Parameters |
| 169 | + ---------- |
| 170 | + raster: h5py.Dataset or str |
| 171 | + Raster where a block is to be read from. String value represents a |
| 172 | + filepath for GDAL rasters. |
| 173 | + block_param: BlockParam |
| 174 | + Object specifying size of block and where to read from raster, |
| 175 | + and amount of padding for the read array |
| 176 | +
|
| 177 | + Returns |
| 178 | + ------- |
| 179 | + data_block: np.ndarray |
| 180 | + Block read from raster with shape specified in block_param. |
| 181 | + ''' |
| 182 | + if isinstance(raster, h5py.Dataset): |
| 183 | + data_block = np.empty((block_param.read_length, block_param.data_width), |
| 184 | + dtype=raster.dtype) |
| 185 | + raster.read_direct(data_block, np.s_[block_param.read_start_line: |
| 186 | + block_param.read_start_line + block_param.read_length, :]) |
| 187 | + else: |
| 188 | + # Open input data using GDAL to get raster length |
| 189 | + ds_data = gdal.Open(raster, gdal.GA_Update) |
| 190 | + data_block = ds_data.GetRasterBand(1).ReadAsArray(0, |
| 191 | + block_param.read_start_line, |
| 192 | + block_param.data_width, |
| 193 | + block_param.read_length) |
| 194 | + |
| 195 | + # Pad igram_block with zeros according to pad_length/pad_width |
| 196 | + data_block = np.pad(data_block, block_param.block_pad, |
| 197 | + mode='constant', constant_values=0) |
| 198 | + |
| 199 | + return data_block |
| 200 | + |
| 201 | +def write_raster_block(out_raster, data, block_param): |
| 202 | + ''' Write processed block to out_raster. |
| 203 | +
|
| 204 | + Parameters |
| 205 | + ---------- |
| 206 | + out_raster: h5py.Dataset or str |
| 207 | + Raster where data (i.e., filtered data) needs to be written. |
| 208 | + String value represents filepath for GDAL rasters. |
| 209 | + data: np.ndarray |
| 210 | + Filtered data to write to out_raster. |
| 211 | + block_param: BlockParam |
| 212 | + Object specifying where and how much to write to out_raster. |
| 213 | + ''' |
| 214 | + if isinstance(out_raster, h5py.Dataset): |
| 215 | + out_raster.write_direct(data, |
| 216 | + dest_sel=np.s_[ |
| 217 | + block_param.write_start_line:block_param.write_start_line + block_param.block_length, |
| 218 | + :]) |
| 219 | + else: |
| 220 | + ds_data = gdal.Open(out_raster, gdal.GA_Update) |
| 221 | + ds_data.GetRasterBand(1).WriteArray(data, xoff=0, yoff=block_param.write_start_line) |
| 222 | + |
| 223 | + |
| 224 | +def filter_data(input_data, lines_per_block, |
| 225 | + kernel_rows, kernel_cols, output_data=None, mask_path=None): |
| 226 | + ''' Filter data using two separable 1D kernels. |
| 227 | +
|
| 228 | + Parameters |
| 229 | + ---------- |
| 230 | + input_data: str |
| 231 | + File path to input data raster (GDAL-friendly) |
| 232 | + lines_per_block: int |
| 233 | + Number of lines to process in batch |
| 234 | + kernel_rows: float array |
| 235 | + 1D kernel along rows direction |
| 236 | + kernel_cols: float array |
| 237 | + 1D kernel along columns direction |
| 238 | + output_data: h5py.Dataset or str |
| 239 | + Raster where a block needs to be written to. String value represents |
| 240 | + file path for GDAL rasters. If not provided, input_data is overwritten |
| 241 | + with the output filtered data |
| 242 | + mask_path: str |
| 243 | + Filepath to the mask to use during filtering |
| 244 | +
|
| 245 | + Returns |
| 246 | + ------- |
| 247 | + ''' |
| 248 | + |
| 249 | + data_shape, data_type = get_raster_info(input_data) |
| 250 | + data_length, data_width = data_shape |
| 251 | + |
| 252 | + # Determine the amount of padding |
| 253 | + pad_length = 2 * (len(kernel_rows) // 2) |
| 254 | + pad_width = 2 * (kernel_cols.shape[1] // 2) |
| 255 | + pad_shape = (pad_length, pad_width) |
| 256 | + |
| 257 | + # Determine number of blocks to process |
| 258 | + lines_per_block = min(data_length, |
| 259 | + lines_per_block) |
| 260 | + |
| 261 | + # Start block processing |
| 262 | + block_params = block_param_generator(lines_per_block, data_shape, pad_shape) |
| 263 | + for block_param in block_params: |
| 264 | + # Read a block of data. If hdf5_dset is set, read a block of data |
| 265 | + # directly from the hdf5 file. Otherwise, use gdal to read block of data |
| 266 | + data_block = get_raster_block(input_data, block_param) |
| 267 | + |
| 268 | + # Get if filtering needs to be performed with or without a mask |
| 269 | + if mask_path is not None: |
| 270 | + # Use gdal to extract a mask block, pad the mask (mask need to be same shape as input) |
| 271 | + ds_mask = gdal.Open(mask_path, |
| 272 | + gdal.GA_ReadOnly) |
| 273 | + mask_block = ds_mask.GetRasterBand(1).ReadAsArray(0, |
| 274 | + block_param.read_start_line, |
| 275 | + block_param.data_width, |
| 276 | + block_param.read_length) |
| 277 | + mask_block = np.pad(mask_block, block_param.block_pad, |
| 278 | + mode='constant', constant_values=0) |
| 279 | + filt_data_block = isce3.signal.convolve2D(data_block, |
| 280 | + mask_block, |
| 281 | + kernel_cols, |
| 282 | + kernel_rows, |
| 283 | + False) |
| 284 | + else: |
| 285 | + filt_data_block = isce3.signal.convolve2D(data_block, |
| 286 | + kernel_cols, |
| 287 | + kernel_rows, |
| 288 | + False) |
| 289 | + # If no value provided for output_data, then overwrite existing |
| 290 | + # input with filtered output |
| 291 | + # Otherwise write filtered output to output_data |
| 292 | + out_raster = input_data if output_data is None else output_data |
| 293 | + |
| 294 | + # If writing to GDAL raster, prepare file |
| 295 | + if not isinstance(out_raster, h5py.Dataset) and not os.path.isfile(out_raster): |
| 296 | + raster = isce3.io.Raster(path=out_raster, width=data_width, |
| 297 | + length=data_length, num_bands=1, |
| 298 | + dtype=data_type, driver_name='GTiff') |
| 299 | + del raster |
| 300 | + |
| 301 | + write_raster_block(out_raster, filt_data_block, block_param) |
0 commit comments