#include "WindInverseFunction.h"
#include "ImageOperatorBase.h"
#include "GPUBaseTool.h"
#include "GPUTool.cuh"
#include "WindInverserToolGPU.cuh"
void   windSpeedInversionProcess(QString in_wind_dir_path, QString in_inc_angle_path, QString in_sar_sigma0_path, QString out_wind_speed_path)
{
	gdalImage in_wind_dir_img(in_wind_dir_path);
	gdalImage in_inc_angle_img(in_inc_angle_path);
	gdalImage in_sar_sigma0_img(in_sar_sigma0_path);
	gdalImage out_wind_speed_img= CreategdalImageFloat(out_wind_speed_path, in_sar_sigma0_img.height, in_sar_sigma0_img.width,1,true,true);


	// 分块处理与计算
	
	//readDataArr(gdalImage & imgds, long start_row, long start_col, long& rows_count, long& cols_count, int band_ids, GDALREADARRCOPYMETHOD method)

	int64_t blocklines = Memory1MB / in_sar_sigma0_img.width/sizeof(float) * 500;
	
	float* in_wind_dir_U_arr_cuda = (float*)mallocCUDADevice(blocklines * in_sar_sigma0_img.width * sizeof(float));
	float* in_wind_dir_V_arr_cuda = (float*)mallocCUDADevice(blocklines * in_sar_sigma0_img.width * sizeof(float));
	float* in_inc_angle_arr_cuda  = (float*)mallocCUDADevice(blocklines * in_sar_sigma0_img.width * sizeof(float));
	float* in_sar_sigma0_arr_cuda = (float*)mallocCUDADevice(blocklines * in_sar_sigma0_img.width * sizeof(float));
	float* out_wind_speed_arr_cuda = (float*)mallocCUDADevice(blocklines * in_sar_sigma0_img.width * sizeof(float));





	for (int64_t start_rid = 0; start_rid < in_sar_sigma0_img.height; start_rid = start_rid + blocklines) {
		int64_t temp_block_line = start_rid + blocklines < in_sar_sigma0_img.height ? blocklines : in_sar_sigma0_img.height - start_rid;
		int64_t read_row_count = temp_block_line;
		int64_t read_cols_count = in_sar_sigma0_img.width;
		std::shared_ptr<float> in_wind_dir_U_arr = readDataArr<float>(in_wind_dir_img, start_rid, 0, read_row_count, read_cols_count, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
		std::shared_ptr<float> in_wind_dir_V_arr = readDataArr<float>(in_wind_dir_img, start_rid, 0, read_row_count, read_cols_count, 2, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
		std::shared_ptr<float> in_inc_angle_arr_arr = readDataArr<float>(in_inc_angle_img, start_rid, 0, read_row_count, read_cols_count, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
		std::shared_ptr<float> in_sar_sigma0_arr_arr = readDataArr<float>(in_sar_sigma0_img, start_rid, 0, read_row_count, read_cols_count, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
		std::shared_ptr<float> out_wind_speed_arr_arr = readDataArr<float>(out_wind_speed_img, start_rid, 0, read_row_count, read_cols_count, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);

		// 复制移动结果
		HostToDevice(in_wind_dir_U_arr.get(), in_wind_dir_U_arr_cuda, read_row_count * read_cols_count * sizeof(float));
		HostToDevice(in_wind_dir_V_arr.get(), in_wind_dir_V_arr_cuda, read_row_count * read_cols_count * sizeof(float));
		HostToDevice(in_inc_angle_arr_arr.get(), in_inc_angle_arr_cuda, read_row_count * read_cols_count * sizeof(float));
		HostToDevice(in_sar_sigma0_arr_arr.get(), in_sar_sigma0_arr_cuda, read_row_count * read_cols_count * sizeof(float));

		int64_t pixelCount = read_row_count * read_cols_count;
		WindSpeedInverseredFunc(in_wind_dir_U_arr_cuda, in_wind_dir_V_arr_cuda, in_inc_angle_arr_cuda, in_sar_sigma0_arr_cuda, out_wind_speed_arr_cuda, pixelCount);
		DeviceToHost(out_wind_speed_arr_arr.get(), out_wind_speed_arr_cuda, read_row_count * read_cols_count * sizeof(float));

		out_wind_speed_img.saveImage(out_wind_speed_arr_arr, start_rid, 0, read_row_count, read_cols_count, 1);
		std::cout << u8"process: " << (start_rid + temp_block_line) * 100.0 / in_sar_sigma0_img.height << u8" precent " << std::endl;
	}



	FreeCUDADevice(in_wind_dir_U_arr_cuda);
	FreeCUDADevice(in_wind_dir_V_arr_cuda);
	FreeCUDADevice(in_inc_angle_arr_cuda);
	FreeCUDADevice(in_sar_sigma0_arr_cuda);
	FreeCUDADevice(out_wind_speed_arr_cuda);


	return ;
}
