def apply_shdi_300(in_fn, out_fn300): in_ds = gdal.Open(in_fn) in_band = in_ds.GetRasterBand(1) in_data = in_band.ReadAsArray() slices = pb.make_slices(in_data, (21, 21)) stacked_data = np.ma.dstack(slices) rows, cols = in_band.YSize, in_band.XSize out_data = np.ones((rows, cols), np.int32) * -99 out_data[10:-10, 10:-10] = np.mean(stacked_data, 2) pb.make_raster(in_ds, out_fn300, out_data, gdal.GDT_Int32, -99) del in_ds print("shdi 300 executed")
for i in range(0, ysize, n): # Figure out how many rows can be read. Remember we want to read n + 2 # rows if possible. if i + n + 1 < ysize: rows = n + 2 else: rows = ysize - i # This makes sure we don't try to read off the top edge the first time # through. yoff = max(0, i - 1) # Read and process the data as before. in_data = in_band.ReadAsArray(0, yoff, xsize, rows) slices = pb.make_slices(in_data, (3, 3)) stacked_data = np.ma.dstack(slices) out_data = np.ones(in_data.shape, np.int32) * -99 out_data[1:-1, 1:-1] = np.mean(stacked_data, 2) # If it's the first time through, write the entire output array # starting at the first row. Otherwise, don't write the first row of # the output array because we don't want to overwrite good data from # the previous chunk. Because we're ignoring this first row, the row # offset needs to be increased. if yoff == 0: out_band.WriteArray(out_data) else: out_band.WriteArray(out_data[1:], 0, yoff + 1) # Finish up.