def _get_tile_data_(_filelist, _outfile, _band_order, _tile_specs, _composite_type): _vrtfile = '/vsimem/' + Handler(_outfile).basename.split('.tif')[0] + '_tmp_layerstack.vrt' _outfile = '/vsimem/' + Handler(_outfile).basename.split('.tif')[0] + '_tmp_layerstack.tif' _mraster = MultiRaster(_filelist) _layerstack_vrt = _mraster.layerstack(return_vrt=True, outfile=_outfile) _lras = Raster('_tmp_layerstack') _lras.datasource = _layerstack_vrt _lras.initialize() _lras.make_tile_grid(*_tile_specs) for _ii in range(_lras.ntiles): yield _filelist, _outfile, _band_order, _lras.tile_grid[_ii], _composite_type
def _tile_process_(args): _filelist, _outfile, _band_order, _tile_dict, _composite_type = args _tile_coords = _tile_dict['block_coords'] _xmin, _ymin = _tile_dict['first_pixel'] _x, _y, _cols, _rows = _tile_coords _outfile = '/vsimem/' + Handler(_outfile).basename.split('.tif')[0] + \ '_{}.tif'.format('_'.join([str(j) for j in _tile_coords])) _mraster = MultiRaster(_filelist) _layerstack_vrt = _mraster.layerstack(return_vrt=True, outfile=_outfile) _lras = Raster('_tmp_layerstack') _lras.datasource = _layerstack_vrt _lras.initialize() _tile_arr = _lras.get_tile(_band_order, _tile_coords).copy() if _composite_type == 'mean': _temp_arr = np.apply_along_axis(lambda x: np.mean(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif _composite_type == 'median': _temp_arr = np.apply_along_axis(lambda x: np.median(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif _composite_type == 'max': _temp_arr = np.apply_along_axis(lambda x: np.max(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif _composite_type == 'min': _temp_arr = np.apply_along_axis(lambda x: np.min(x[x != _lras.nodatavalue]) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) elif 'pctl' in _composite_type: pctl = int(_composite_type.split('_')[1]) _temp_arr = np.apply_along_axis(lambda x: np.percentile(x[x != _lras.nodatavalue], pctl) if (x[x != _lras.nodatavalue]).shape[0] > 0 else _lras.nodatavalue, 0, _tile_arr) else: _temp_arr = None _lras = None _mraster = None Handler(_outfile).file_delete() _tile_arr = None return (_y-_ymin), ((_y-_ymin) + _rows), (_x-_xmin), ((_x-_xmin) + _cols), _temp_arr
]) arr_copy = out_arr print(ker_size) print(arr_copy) plt.plot(range(len(arr)), arr_copy) plt.show() arr_copy = arr_copy.astype(dtype) if type(arr) in (list, tuple, dict, set): return arr_copy.tolist() else: return arr_copy if __name__ == '__main__': arr = [12, 4, 7, 13, 9, 8, 1, 13, 7, 2, 1, 36, 33] arr2 = moving_average(arr, n=7) print(arr) print(arr2) file1 = "D:/temp/above2017_0629_lvis2b_tif/LVIS2_ABoVE2017_0629_R1803_061571_umd_l2b.tif" outfile1 = "D:/temp/above2017_0629_lvis2b_tif/LVIS2_ABoVE2017_0629_R1803_061571_umd_l2b_clip2.tif" cutfile = "D:/temp/LVIS2_ABoVE2017_0629_R1803_061571_umd_l2b.shp" ras = Raster(file1) ras.clip(cutline_file=cutfile, outfile=outfile1, apply_mask=False)
picklefiles = ("RFalbedo_deciduous_fraction_treecover_50000_cutoff_5_deg1_20200501T185635_spring.pickle", "RFalbedo_deciduous_fraction_treecover_50000_cutoff_5_deg1_20200501T185635_summer.pickle", "RFalbedo_deciduous_fraction_treecover_50000_cutoff_5_deg1_20200501T185635_fall.pickle") picklefiles = [pickle_dir + picklefile for picklefile in picklefiles] band_name = 'spr_albedo' outfile = outdir + Handler(Handler(infile).basename).add_to_filename('_output3') Handler(outfile).file_remove_check() # raster contains three bands: 1) decid 2) tree cover 3) land extent mask. # all the bands are in integer format raster = Raster(infile) raster.initialize() raster.bnames = ['decid', 'treecover'] raster.get_stats(True) Opt.cprint(raster.shape) regressor = RFRegressor.load_from_pickle(picklefiles[0]) Opt.cprint(regressor) Opt.cprint(regressor.adjustment) out_raster = RFRegressor.regress_raster(regressor, raster,
clip=clip_) Opt.cprint(rf_regressor1) Opt.cprint(rf_regressor2) bandnames1_ = rf_regressor1.features Opt.cprint('Bands 1 : ') Opt.cprint(bandnames1_) bandnames2_ = rf_regressor2.features Opt.cprint('Bands 2 : ') Opt.cprint(bandnames2_) bandnames_all = bandnames1_ + bandnames2_ # get raster metadata ras = Raster(infile) ras.initialize() tile_size = min([ras.shape[1], ras.shape[2]]) Opt.cprint(ras.shape) Opt.cprint(ras) bandnames = ras.bnames Opt.cprint('Raster bands: "' + '", "'.join(bandnames) + '"') band_order = Sublist(bandnames).sublistfinder(bandnames_all) Opt.cprint('Band order: ' + ', '.join([str(b) for b in band_order])) # re-initialize raster ras.initialize(get_array=True, band_order=band_order)
(num_list[:, 1] >= startdate) & (num_list[:, 1] <= enddate))[0] filelist = list(all_files[i] for i in file_loc_on_list.tolist()) for file_ in filelist: Opt.cprint(file_) Opt.cprint(outfile) mraster = MultiRaster(filelist=filelist) Opt.cprint(mraster) ls_vrt = mraster.layerstack(return_vrt=True, outfile=outfile) lras = Raster('tmp_layerstack') lras.datasource = ls_vrt lras.initialize() xmin, xmax, ymin, ymax = lras.get_pixel_bounds(image_bounds, 'crs') lras.transform = (image_bounds[0], lras.transform[1], lras.transform[2], image_bounds[3], lras.transform[4], lras.transform[5]) lras.shape = [1, (ymax-ymin), (xmax-xmin)] lras.make_tile_grid(*tile_specs)
# first slice west of 0 deg lon first_slice = np.vstack([arr[channel_indx, (arr.shape[1] - row_indx - 1), cut_loc:] for row_indx in range(arr.shape[1])]) # second slice east of 0 deg lon second_slice = np.vstack([arr[channel_indx, (arr.shape[1] - row_indx - 1), :cut_loc] for row_indx in range(arr.shape[1])]) resliced_arr_list.append(np.hstack([first_slice, second_slice])) # stack all months arr = np.stack(resliced_arr_list, 0) # name output file outfile = Handler(file1).dirname + Handler().sep + 'ALBEDO_CAM5_{}_KERNEL.tif'.format(var_names[variable]) # define raster object ras = Raster(outfile, array=arr, bnames=months, dtype=GDAL_FIELD_DEF['double'], shape=arr.shape, transform=transform, crs_string=spref.ExportToWkt()) # define no data value ras.nodatavalue = data._FillValue # write raster object ras.write_to_file()