#include "QtSimulationGeoSARSigma0Dialog.h"
#include "ui_QtSimulationGeoSARSigma0Dialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include "BaseTool.h"
#include "SigmaDatabase.h"
#include <ImageOperatorBase.h>


QtSimulationGeoSARSigma0Dialog::QtSimulationGeoSARSigma0Dialog(QWidget *parent)
	: QDialog(parent), ui(new Ui::QtSimulationGeoSARSigma0DialogClass)
{
	ui->setupUi(this);
	// 连接信号和槽
	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onbtnaccepted()));
	connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(onbtnrejected()));
	connect(ui->InputIncAngleRasterBtn, SIGNAL(clicked(bool)), this, SLOT(onpushButtonIncAngleRasterClicked(bool)));
	connect(ui->InputLandClsRasterBtn, SIGNAL(clicked(bool)), this, SLOT(onpushButtonLandClsRasterClicked(bool)));
	connect(ui->InputClsWeightRasterBtn, SIGNAL(clicked(bool)), this, SLOT(onpushButtonClsWeightRasterClicked(bool)));
	connect(ui->OutputTerrianRasterBtn, SIGNAL(clicked(bool)), this, SLOT(onpushButtonOutputTerrianRasterClicked(bool)));
	connect(ui->OutputClsSARRasterBtn, SIGNAL(clicked(bool)), this, SLOT(onpushButtonOutputClsSARRasterClicked(bool)));

}

QtSimulationGeoSARSigma0Dialog::~QtSimulationGeoSARSigma0Dialog()
{}

void QtSimulationGeoSARSigma0Dialog::onpushButtonIncAngleRasterClicked(bool)
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->InputIncAngleRasterLineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtSimulationGeoSARSigma0Dialog::onpushButtonLandClsRasterClicked(bool)
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->InputLandClsRasterLineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtSimulationGeoSARSigma0Dialog::onpushButtonClsWeightRasterClicked(bool)
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->InputClsWeightRasterLineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtSimulationGeoSARSigma0Dialog::onpushButtonOutputTerrianRasterClicked(bool)
{
	QString fileName = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->OutputTerrianRasterLineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtSimulationGeoSARSigma0Dialog::onpushButtonOutputClsSARRasterClicked(bool)
{
	QString fileName = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->OutputClsSARRasterLineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}


void QtSimulationGeoSARSigma0Dialog::onbtnaccepted()
{
	// 参数文件
	QString IncAngleRaster = this->ui->InputIncAngleRasterLineEdit->text(); // 输入入射角文件
	QString LandClsRaster = this->ui->InputLandClsRasterLineEdit->text(); // 输入地表覆盖分类文件
	QString ClsWeightRaster = this->ui->InputClsWeightRasterLineEdit->text();// 输入 SigmaDatabase 权重文件
	QString OutputTerrianRaster = this->ui->OutputTerrianRasterLineEdit->text(); // 输出入射角仿真SAR文件
	//QString OutputClsSARRaster = this->ui->OutputClsSARRasterLineEdit->text();// 输出地表覆盖分类仿真SAR文件


	gdalImage incAngleImg(IncAngleRaster);
	gdalImage landImg(LandClsRaster);
	gdalImage weightImg(ClsWeightRaster);
	gdalImage OutputTerrian=CreategdalImage(OutputTerrianRaster, incAngleImg.height, incAngleImg.width, 1, incAngleImg.gt, 4326,  GDT_Float32,   true,  true);



    // 分块加载运行 incAngleImg landImg weightImg，处理，每次处理100行
    int blockSize = 100;
    int numRows = incAngleImg.height;
    int numCols = incAngleImg.width;

	double calibration = 0;
	bool flag = false;
    for (int row = 0; row < numRows; row += blockSize) {
        int numRowsToProcess = std::min(blockSize, numRows - row);

        Eigen::MatrixXd incAngleData = incAngleImg.getData(row, 0, numRowsToProcess, numCols, 1);
        Eigen::MatrixXd landData = landImg.getData(row, 0, numRowsToProcess, numCols, 1);
        Eigen::MatrixXd weightData = weightImg.getData(row, 0, numRowsToProcess, numCols, 1);
		
		//qDebug() << "(0,0)" << incAngleData(0, 2);
		 
		for (int i = 0; i < incAngleData.rows(); i++) {
			for (int j = 0; j < incAngleData.cols(); j++) {
				double weight = weightData(i, j);
				double incAngle = incAngleData(i, j);
				double weightSIgma = weight* sin(38 * d2r) / sin(incAngle * d2r); 
				
				double muhsigmaAmp=calculate_MuhlemanSigma(incAngle);
				if ((landData(i, j) == 10|| std::abs(landData(i, j) - 10)<10||
					landData(i, j) == 30 || std::abs(landData(i, j) - 30) < 10 ||
					landData(i, j) == 90 || std::abs(landData(i, j) -90) < 10 
					)&&!isnan(incAngle)&&!isinf(incAngle)) {
					//qDebug() << "muhsigmaAmp:" << muhsigmaAmp << "weightSIgma:" << weightSIgma << "weight:" << weight << "incAngle:" << incAngle;
					calibration= (10 * log10(muhsigmaAmp))/ weightSIgma ;
					if (!isinf(calibration) && !isnan(calibration))
					{
						flag = true;
						break;
					}
				}
				else {
					//alibration = weightSIgma / (10 * log10(muhsigmaAmp));
				}
			 
			}
		}

		if (flag) {
			break;
		}

    }



	for (int row = 0; row < numRows; row += blockSize) {
		int numRowsToProcess = std::min(blockSize, numRows - row);

		Eigen::MatrixXd incAngleData = incAngleImg.getData(row, 0, numRowsToProcess, numCols, 1);
		Eigen::MatrixXd landData = landImg.getData(row, 0, numRowsToProcess, numCols, 1);
		Eigen::MatrixXd weightData = weightImg.getData(row, 0, numRowsToProcess, numCols, 1);
		Eigen::MatrixXd OutputTerrianData = OutputTerrian.getData(row, 0, numRowsToProcess, numCols, 1);

		for (int i = 0; i < incAngleData.rows(); i++) {
			for (int j = 0; j < incAngleData.cols(); j++) {
				double weight = weightData(i, j);
				double incAngle = incAngleData(i, j);
				double weightSIgma = weight * sin(38 * d2r) / sin(incAngle * d2r)* calibration;
				double amp = std::pow(10, weightSIgma / 20);
				OutputTerrianData(i, j) = amp;
			}
		}

		OutputTerrian.saveImage(OutputTerrianData, row, 0, 1);
	}

 

	//// 创建输出文件，其中输出文件的大小和输入文件的大小一致，复制输入文件到输出文件，并把文件置零
	//gdalImage IncAngle(IncAngleRaster);
	// 

	//// 调用createImage函数创建输出文件 函数定义
	//gdalImage OutputTerrian=CreategdalImage(OutputTerrianRaster, IncAngle.height, IncAngle.width, 1, IncAngle.gt, 4326,  GDT_Float32,   true,  true);
 //
	//Eigen::MatrixXd IncAngleData = IncAngle.getData(0, 0, IncAngle.height, IncAngle.width,1);
	//Eigen::MatrixXd SigmaSARData = MuhlemanSigmaArray(IncAngleData);
	//
	//OutputTerrian.saveImage(SigmaSARData, 0, 0, 1);



	//if (QFile(LandClsRaster).exists()) {

	//	QFile::copy(OutputTerrianRaster, OutputClsSARRaster);
	//	gdalImage OutputClsSAR(OutputClsSARRaster);
	//	gdalImage LandCls(LandClsRaster);
	//	SigmaDatabase sigmads;
	//	sigmads.readParamsFromFile(ClsWeightRaster.toStdString());
	//	Eigen::MatrixXd LandClsData = LandCls.getData(0, 0, LandCls.height, LandCls.width,1);

	//	Eigen::MatrixXd SigmaSARDataCls = SigmaSARData;
	//	for (long i = 0; i < LandClsData.rows(); i++)
	//	{
	//		for (long j = 0; j < LandClsData.cols(); j++)
	//		{
	//			double weight = sigmads.getAmpHH(LandClsData(i, j), IncAngleData(i, j) * d2r);
	//			SigmaSARDataCls(i, j) = weight;
	//		}
	//	}
	//	OutputClsSAR.saveImage(SigmaSARDataCls, 0, 0, 1);
	//}
	//else {}




	QMessageBox::information(nullptr, u8"提示", u8"completed!!!");

}

void QtSimulationGeoSARSigma0Dialog::onbtnrejected()
{
	this->close();
}
