#include "RadianceConverter.h"
RadConverter::RadConverter(QObject* parent /*= nullptr*/)
{
#ifdef _DEBUG
	m_qstrCalFilePath = "E:/WorkSpace/TowerOptoSifAndSpectral/Cal";
#else
	m_qstrCalFilePath = "/home/data/Cal";
#endif // DEBUG
}

RadConverter::~RadConverter()
{
}

int RadConverter::LoadCalibrationFrames(RunTimeGrabberParams struGrabberRTParams, vector<vector<CalFrame>> &struAllCalFrame)
{
	/////param
	int iScanPoints = struGrabberRTParams.apsParams.iTotalPosition - 1;
	int iDevices = struGrabberRTParams.fscParams.ucDeviceNumber;
	//int iScanPoints = 4;
	//int iDevices = 1;
	/////check dir
	QDir qdirPath(m_qstrCalFilePath);
	if (!qdirPath.exists())
	{
		qDebug() << "Calibration File not exist";
		return -1;
	}
	/////check quantities
	QStringList qslFilter;
	qslFilter << "*.dat";
	qdirPath.setNameFilters(qslFilter);
	QFileInfoList qfiInfo = qdirPath.entryInfoList(qslFilter);
	if (qfiInfo.size()<iScanPoints * iDevices)
	{
		qDebug() << "Calibration Files quantities not match";
		return -2;
	}

	/////read file
	QString qstrFilePath;
	vector<CalFrame> vecOneDev;
	CalFrame OneFile;
	for (int j=0;j<iDevices;j++)
	{
		vecOneDev.clear();
		for (int i = 0; i < iScanPoints; i++)
		{
			qstrFilePath = m_qstrCalFilePath+QString("/FS%1_").arg(j + 1)+ QString("P%1").arg(i + 1)+".dat";
			QFile qfCalFile(qstrFilePath);
			bool bRes = qfCalFile.open(QFile::ReadOnly);

			using namespace ZZ_MISCDEF;
			qfCalFile.read((char *)&OneFile.uiExposureTimeInMS,sizeof(ZZ_U32));//U32
			qfCalFile.read((char*)&OneFile.fTemperature, sizeof(float));
			qfCalFile.read((char*)&OneFile.iPixels,sizeof(int));
			qfCalFile.read((char*)OneFile.fWaveLength, sizeof(float)*OneFile.iPixels);
			qfCalFile.read((char*)OneFile.dCal_Gain, sizeof(double)*OneFile.iPixels);
			qfCalFile.read((char*)OneFile.dCal_Offset, sizeof(double)*OneFile.iPixels);

			vecOneDev.push_back(OneFile);
		}
		struAllCalFrame.push_back(vecOneDev);
	}



	return 0;
}

// int RadConverter::LoadQEProLinearParams(RunTimeGrabberParams struGrabberRTParams, vector<vector<double>>& vecQEPLP)
// {
// 	int iQEPDevices = 0;
// 	return 0;
// }
