def statistics_func(raster_folder, raster_filename, output_folder, statistical_functions, statistical_args): start_time = datetime.now() input_raster = os.path.join(raster_folder, raster_filename) hdr_file = "" # input_raster + ".hdr" # only used for ENVI stacks outname = os.path.join(output_folder, raster_filename) if outname.find(".tif") != -1: outname = outname[0:len(outname) - 4] # arr: full size numpy array 3D XxYxZ 200x300x100 arr = preprocessing.rio_array(input_raster, hdr_file=hdr_file) # activate to get list of dates from .hdr file (.hdr file needs to be specified above) # dates = arr[1] for i, func in enumerate(statistical_functions): # creating results with calling wanted algorithm in parallel_apply_along_axis for quick runtime result = apply_along_axis.parallel_apply_along_axis(func1d=func, arr=arr[0], axis=0, cores=mp.cpu_count(), **statistical_args[i]) # selecting dtype based on result dtype = type(result[0][0]) func_name_end = str(func).find(" at") func_name_start = 10 func_name = str(func)[func_name_start:func_name_end] # exporting result to new raster export_arr.functions_out_array(outname=outname + "_" + func_name + str(i), arr=result, input_file=input_raster, dtype=dtype) # print time to this point statistics_time = datetime.now() print("breakpoint-time = ", statistics_time - start_time, "Hr:min:sec")
def filter_func(raster_folder, raster_filename, output_folder, filter_functions, filter_args): start_time = datetime.now() input_raster = os.path.join(raster_folder, raster_filename) hdr_file = "" # only used for ENVI stacks outname = os.path.join(output_folder, raster_filename) if outname.find(".tif") != -1: outname = outname[0:len(outname) - 4] # arr: full size numpy array 3D XxYxZ 200x300x100 arr = preprocessing.rio_array(input_raster, hdr_file=hdr_file) # activate to get list of dates from .hdr file (.hdr file needs to be specified above) # dates = arr[1] for i, func in enumerate(filter_functions): kernel_size = str(filter_args[i]['kernel']) filtered_arr = apply_along_axis.parallel_apply_along_axis(func1d=func, arr=arr[0], axis=0, cores=mp.cpu_count(), **filter_args[i]) filtered_arr = np.rollaxis(filtered_arr, 2) filtered_arr = np.rollaxis(filtered_arr, 1) filtered_arr = np.rollaxis(filtered_arr, 2) dtype = type(filtered_arr[0][0][0]) func_name_end = str(func).find(" at") func_name_start = 10 func_name = str(func)[func_name_start:func_name_end] # exporting result to new raster export_arr.functions_out_array(outname=outname + "_" + func_name + str(kernel_size), arr=filtered_arr, input_file=input_raster, dtype=dtype) # print time to this point filter_time = datetime.now() print("filter-time = ", filter_time - start_time, "Hr:min:sec")
def subset_processed_data(): """ function to subset raster data using polygons from a shapefile and export the subset to a new folder all paths are given in user_data.py """ import rasterio as rio import rasterio.mask import numpy as np ### install BanDiTS using: python3.6 -m pip install git+https://github.com/marlinmm/BanDiTS.git ### from BanDiTS.export_arr import functions_out_array import shutil subset_path = os.path.join(Paths.sen_processed_path, "subset/") if os.path.exists(subset_path): shutil.rmtree(subset_path) os.mkdir(subset_path) processed_file_list = extract_files_to_list(Paths.sen_processed_path, datatype=".tif") shapefile = import_polygons() for k, tifs in enumerate(processed_file_list): src1 = rio.open(processed_file_list[k]) out_image1, out_transform1 = rio.mask.mask(src1, [shapefile[0][0]], all_touched=1, crop=True, nodata=np.nan) ras_meta1 = src1.profile ras_meta1.update({ "driver": "GTiff", "height": out_image1.shape[1], "width": out_image1.shape[2], "transform": out_transform1, "nodata": -9999 }) tmp1 = processed_file_list[k].index("_dir") tmp2 = len(processed_file_list[k]) print(subset_path + processed_file_list[k][tmp1 + 5:tmp2 - 4]) functions_out_array(outname=subset_path + processed_file_list[k][tmp1 + 5:tmp2 - 4] + "_subset.tif", arr=out_image1, input_file=processed_file_list[k], dtype=np.float32, ras_meta1=ras_meta1)
def main(): ################################### INPUT ######################################## # Input Folder Marlin: raster_folder = "C:/Users/marli/Desktop/GEO402_Testdaten/Input_Files/Raster/" # Input Folder Jonas: # raster_folder = "C:/Users/jz199/Documents/Studium/Master/1. Semester\Vorlesungsmitschriften/GEO402 - Ableitung von Landoberflächenparametern/Subset/" # Input file name raster_filename = "S1_A_VH_agulhas_full_study_site_50m" # raster_filename = "SubsetVH.tif" ################################### OUTPUT ######################################## # Output Folder Marlin: output_folder = "C:/Users/marli/Desktop/GEO402_Testdaten/AAA_output/" # Output Folder Jonas: # output_folder = "C:/Users/jz199/Documents/Studium/Master/1. Semester\Vorlesungsmitschriften/GEO419 - Pythonprogrammierung Habermeyer/GEO402_Output/" # Output File Name: # output_file = raster_filename[0:len(raster_filename)-4] + "_median_filtered3.tif" output_file = raster_filename + "_median_test2.tif" #simple_edge_detection ###################### NO USER INPUT BEYOND THIS POINT ############################### input_raster = raster_folder + raster_filename hdr_file = input_raster + ".hdr" outname = output_folder + output_file # arr: full size numpy array 3D XxYxZ 200x300x100 arr = preprocessing.rio_array(input_raster, hdr_file=hdr_file) dates = arr[1] #print(dates) # creating filtered array depending on set filter function filtered_arr = apply_along_axis.parallel_apply_along_axis(func1d=filter_functions.median_filter11, arr=arr[0], axis=0, cores=mp.cpu_count()) filtered_arr = np.rollaxis(filtered_arr, 2) filtered_arr = np.rollaxis(filtered_arr, 1) filtered_arr = np.rollaxis(filtered_arr, 2) dtype = type(filtered_arr[0][0][0]) #print(type(dtype) #if type(dtype) == np.float64: # print("lalala") # --> change float64 to float32 export_arr.functions_out_array(outname=outname, arr=filtered_arr, dates=dates, input_file=input_raster, dtype=dtype) # creating results with calling wanted algorithm in parallel_apply_along_axis for quick runtime #result = apply_along_axis.parallel_apply_along_axis(func1d=function.find_peaks, arr=arr[0], axis=0, cores=mp.cpu_count()) # selecting dtype based on result dtype = np.int32 #type(result[0][0]) # dtype = type(filtered_arr[0][0]) # float64 to float32 # exporting result to new raster #export_arr.functions_out_array(outname=outname, arr=result, dates=dates, input_file=input_raster, dtype=dtype) # export_arr.functions_out_array(outname=outname, arr=filtered_arr, input_file=input_raster, dtype=dtype) end_time = datetime.now() print("end-time = ", end_time-start_time, "Hr:min:sec")