mirror of
http://172.16.0.230/r/SIF/TowerOptoSifAndSpectral.git
synced 2025-10-18 19:39:43 +08:00
445 lines
14 KiB
C++
445 lines
14 KiB
C++
#include <QtCore/QCoreApplication>
|
||
#include <QTextStream>
|
||
#include <QCommandLineParser>
|
||
#include <QDir>
|
||
|
||
#include <iostream>
|
||
|
||
#include "Header_Files/oceanOpticsFiberImager.h"
|
||
#include "Header_Files/atpFiberImager.h"
|
||
#include "Header_Files/calibration.h"
|
||
|
||
enum CommandLineParseResult
|
||
{
|
||
CommandLineOk,
|
||
CommandLineError,
|
||
CommandLineVersionRequested,
|
||
CommandLineHelpRequested
|
||
};
|
||
|
||
enum DeviceType
|
||
{
|
||
OPTOSKY,
|
||
OceanOptics,
|
||
UnknownDevice
|
||
};
|
||
|
||
struct TcQuery
|
||
{
|
||
DeviceType deviceType;
|
||
QString serialPort;
|
||
|
||
int sleepTimeinSecond;//有默认值
|
||
int averageTimes;
|
||
int position;
|
||
|
||
QString calFileOutputDirectory;//有默认值
|
||
QString calFileOutputName;
|
||
QString standardLightFilePath;
|
||
|
||
bool justRecord;
|
||
};
|
||
|
||
CommandLineParseResult parseCommandLine2(QCommandLineParser &parser, TcQuery *query, QString *errorMessage);
|
||
bool copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist);
|
||
void logout(QString str);
|
||
void createDirectory(QString fullPath);
|
||
bool isFileExist(QString fullFileName);
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
QCoreApplication a(argc, argv);
|
||
QCoreApplication::setApplicationName("Ocean optics radiance calibration software");
|
||
QCoreApplication::setApplicationVersion("1.0");
|
||
|
||
// 解析命令行参数
|
||
QCommandLineParser parser;
|
||
parser.setApplicationDescription("This software is used for doing radiance calibration for ocean optics fiber imager.");
|
||
TcQuery query;
|
||
QString errorMessage;
|
||
|
||
switch (parseCommandLine2(parser, &query, &errorMessage))
|
||
{
|
||
case CommandLineOk:
|
||
break;
|
||
case CommandLineError:
|
||
fputs(qPrintable(errorMessage), stderr);
|
||
fputs("\n\n", stderr);
|
||
fputs(qPrintable(parser.helpText()), stderr);
|
||
return 1;
|
||
case CommandLineVersionRequested:
|
||
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
|
||
qPrintable(QCoreApplication::applicationVersion()));
|
||
return 0;
|
||
case CommandLineHelpRequested:
|
||
parser.showHelp();
|
||
Q_UNREACHABLE();
|
||
}
|
||
|
||
|
||
//创建光谱仪对象
|
||
FiberSpectrometerOperationBase * m_FiberSpectrometer;
|
||
switch (query.deviceType)
|
||
{
|
||
case OPTOSKY:
|
||
m_FiberSpectrometer = new ATPFiberImager(false,"ttyUSB0","ocean_optics");
|
||
break;
|
||
case OceanOptics:
|
||
m_FiberSpectrometer = new OceanOpticsFiberImager();
|
||
break;
|
||
case UnknownDevice:
|
||
parser.showHelp();
|
||
Q_UNREACHABLE();
|
||
}
|
||
|
||
//连接光谱仪
|
||
QString SN;
|
||
QString pixelCount;
|
||
QString wavelengthInfo;
|
||
logout("<br><b style=\"color:red\">Connectting the fiber spectrometer!</b>");
|
||
m_FiberSpectrometer->connectFiberSpectrometer(SN, pixelCount, wavelengthInfo);
|
||
|
||
//自动曝光
|
||
logout("<br><b style=\"color:red\">AutoExpose!</b>");
|
||
m_FiberSpectrometer->autoExpose();
|
||
|
||
|
||
//程序sleep:等待关闭快门
|
||
logout("<br><b style=\"color:red\">Wait for close the lamp!</b>");
|
||
QThread::sleep(query.sleepTimeinSecond);
|
||
|
||
//采集暗帧
|
||
logout("<br><b style=\"color:red\">Record dark frame!</b>");
|
||
m_FiberSpectrometer->recordDark(query.calFileOutputDirectory);
|
||
|
||
|
||
//程序sleep:等待打开快门
|
||
logout("<br><b style=\"color:red\">Wait for open the lamp!</b>");
|
||
QThread::sleep(query.sleepTimeinSecond);
|
||
|
||
//采集积分球光谱
|
||
logout("<br><b style=\"color:red\">Record integrating sphere frame!</b>");
|
||
m_FiberSpectrometer->recordTarget(query.averageTimes, query.calFileOutputDirectory);
|
||
|
||
//准备定标文件数据
|
||
DeviceAttribute deviceAttribute;
|
||
DeviceInfo deviceInfo;
|
||
m_FiberSpectrometer->getDeviceAttribute(deviceAttribute);
|
||
m_FiberSpectrometer->getDeviceInfo(deviceInfo);
|
||
|
||
CalibrationAlgorithm * m_CalibrationAlgorithm = new CalibrationAlgorithm();
|
||
m_CalibrationAlgorithm->readFile(query.standardLightFilePath, deviceAttribute, deviceInfo);//
|
||
|
||
//生成辐射定标文件
|
||
if (query.calFileOutputName.isEmpty())//query->calFileOutputName==""
|
||
{
|
||
QDateTime curDateTime = QDateTime::currentDateTime();
|
||
QString currentTime = curDateTime.toString("yyyy_MM_dd_hh_mm_ss");
|
||
QString calFileName = QDir::cleanPath(query.calFileOutputDirectory + QDir::separator() + currentTime + "_" + QString::fromStdString(deviceInfo.strSN) + ".dat");
|
||
|
||
query.calFileOutputName=calFileName;
|
||
}
|
||
logout("<br><b style=\"color:red\">Produce calibration file!</b>");
|
||
m_CalibrationAlgorithm->produceCalfile(query.calFileOutputName, deviceAttribute, m_FiberSpectrometer->m_IntegratingSphereData, m_FiberSpectrometer->m_DarkData);
|
||
|
||
//复制辐射定标文件
|
||
QDateTime curDateTime = QDateTime::currentDateTime();
|
||
QString currentTime = curDateTime.toString("yyyy_MM_dd_hh_mm_ss");
|
||
QString destName = QDir::cleanPath(query.calFileOutputDirectory + QDir::separator() + currentTime + "_" + QString::fromStdString(deviceInfo.strSN) + "_" +QString::number(query.position) + ".dat");
|
||
copyFileToPath(query.calFileOutputName,destName,true);
|
||
|
||
//断开光谱仪
|
||
m_FiberSpectrometer->disconnectFiberSpectrometer();
|
||
//return a.exec();
|
||
}
|
||
|
||
CommandLineParseResult parseCommandLine2(QCommandLineParser &parser, TcQuery *query, QString *errorMessage)
|
||
{
|
||
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
|
||
|
||
QCommandLineOption deviceType("deviceType", "Device type. Options are OPTOSKY and OceanOptics", "deviceType");
|
||
parser.addOption(deviceType);
|
||
|
||
QCommandLineOption serialPort("serialPort", "Serial port.", "serialPort");
|
||
parser.addOption(serialPort);
|
||
|
||
QCommandLineOption sleepTimeinSecond("t", "The time app sleep.", "sleepTimeinSecond");
|
||
sleepTimeinSecond.setDefaultValue("30");//设置默认参数
|
||
parser.addOption(sleepTimeinSecond);
|
||
|
||
QCommandLineOption averageTimes("a", "Average times.", "average_times");
|
||
averageTimes.setDefaultValue("5");//设置默认参数
|
||
parser.addOption(averageTimes);
|
||
|
||
QCommandLineOption position("position", "Position.", "position");
|
||
parser.addOption(position);
|
||
|
||
// parser.addPositionalArgument("name", "The name to look up.");//????????????????????????????????????????????????????????????????????????????
|
||
QCommandLineOption helpOption = parser.addHelpOption();//Adds the help option (-h, --help and -? on Windows) This option is handled automatically by QCommandLineParser.
|
||
QCommandLineOption versionOption = parser.addVersionOption();//This option is handled automatically by QCommandLineParser.
|
||
|
||
|
||
//// A boolean option with a single name (-p)
|
||
//QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy"));
|
||
//parser.addOption(showProgressOption);
|
||
|
||
// A boolean option with multiple names (-r, --record)
|
||
QCommandLineOption recordOption(QStringList() << "f" << "record",
|
||
QCoreApplication::translate("main", "Just record one spectral."));
|
||
parser.addOption(recordOption);
|
||
|
||
|
||
//标准灯文件
|
||
QCommandLineOption standardLightFilePath(QStringList() << "slfp" << "standard-light-file-path",
|
||
QCoreApplication::translate("main", "set standard light file."),
|
||
QCoreApplication::translate("main", "file"));
|
||
parser.addOption(standardLightFilePath);
|
||
|
||
QCommandLineOption standardLightFileSelector(QStringList() << "slfs" << "standard-light-file-selector",
|
||
QCoreApplication::translate("main", "select standard light file."),
|
||
QCoreApplication::translate("main", "file"));
|
||
parser.addOption(standardLightFileSelector);
|
||
|
||
|
||
//定标文件输出路径
|
||
// An option with a value
|
||
QCommandLineOption calFileOutputDirectory(QStringList() << "cfod" << "calibration-file-output-directory",
|
||
QCoreApplication::translate("main", "Save cal file into <directory>."),
|
||
QCoreApplication::translate("main", "directory"));
|
||
// QString tmpPath1 = QDir::cleanPath(QDir::rootPath() + QDir::separator()+"calFile");
|
||
QString tmpPath1 = "/home/data/Cal/";
|
||
calFileOutputDirectory.setDefaultValue(tmpPath1);//设置默认参数,QCoreApplication::applicationDirPath(),standardLightFile
|
||
parser.addOption(calFileOutputDirectory);
|
||
|
||
//定标文件输出文件名
|
||
// An option with a value
|
||
QCommandLineOption calFileOutputName(QStringList() << "cfon" << "calibration-file-output-name",
|
||
QCoreApplication::translate("main", "Cal file name."),
|
||
QCoreApplication::translate("main", "fileName"));
|
||
parser.addOption(calFileOutputName);
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
if (!parser.parse(QCoreApplication::arguments()))//Process the actual command line arguments given by the user
|
||
{
|
||
*errorMessage = parser.errorText();
|
||
return CommandLineError;
|
||
}
|
||
|
||
if (parser.isSet(versionOption))
|
||
return CommandLineVersionRequested;
|
||
|
||
if (parser.isSet(helpOption))
|
||
return CommandLineHelpRequested;
|
||
|
||
if (parser.isSet(deviceType))
|
||
{
|
||
const QString deviceTypeTmp = parser.value(deviceType);
|
||
|
||
if (deviceTypeTmp=="OPTOSKY")
|
||
{
|
||
query->deviceType = OPTOSKY;
|
||
}
|
||
else if(deviceTypeTmp=="OceanOptics")
|
||
{
|
||
query->deviceType = OceanOptics;
|
||
}
|
||
else
|
||
{
|
||
*errorMessage = "DeviceType set error.";
|
||
return CommandLineError;
|
||
}
|
||
|
||
}
|
||
else//默认参数
|
||
{
|
||
*errorMessage = "No deviceType set.";
|
||
return CommandLineError;
|
||
}
|
||
|
||
if (parser.isSet(serialPort))
|
||
{
|
||
const QString serialPortTmp = parser.value(serialPort);
|
||
query->serialPort = serialPortTmp;
|
||
}
|
||
else//默认参数
|
||
{
|
||
if (query->deviceType == OceanOptics)
|
||
{
|
||
;
|
||
} else if (query->deviceType == OPTOSKY)
|
||
{
|
||
*errorMessage = "No serialPort set.";
|
||
return CommandLineError;
|
||
}
|
||
}
|
||
|
||
if (parser.isSet(sleepTimeinSecond))
|
||
{
|
||
const QString timeTmp = parser.value(sleepTimeinSecond);
|
||
query->sleepTimeinSecond = timeTmp.toInt();
|
||
}
|
||
else//默认参数
|
||
{
|
||
QStringList tmp = sleepTimeinSecond.defaultValues();
|
||
query->sleepTimeinSecond = tmp[0].toInt();
|
||
}
|
||
|
||
if (parser.isSet(averageTimes))
|
||
{
|
||
const QString averageTimesTmp = parser.value(averageTimes);
|
||
|
||
string tttt=averageTimesTmp.toStdString();
|
||
|
||
query->averageTimes = averageTimesTmp.toInt();
|
||
}
|
||
else//默认参数
|
||
{
|
||
QStringList tmp = averageTimes.defaultValues();
|
||
query->averageTimes = tmp[0].toInt();
|
||
}
|
||
|
||
if (parser.isSet(position))
|
||
{
|
||
const QString positionTmp = parser.value(position);
|
||
query->position = positionTmp.toInt();
|
||
}
|
||
else
|
||
{
|
||
*errorMessage = "No position set.";
|
||
return CommandLineError;
|
||
}
|
||
|
||
query->justRecord = parser.isSet(recordOption);
|
||
|
||
|
||
if (!parser.isSet(standardLightFilePath) && !parser.isSet(standardLightFileSelector))//没有设置定标保存文件路径
|
||
{
|
||
*errorMessage = "No standard light file set.";
|
||
return CommandLineError;
|
||
}
|
||
|
||
if (parser.isSet(standardLightFileSelector))//
|
||
{
|
||
QString selector = parser.value(standardLightFileSelector);
|
||
// QString standardLightFilePath_tmp = QDir::cleanPath(QDir::rootPath() + QDir::separator() + "standardLightFile" + QDir::separator() + selector);
|
||
|
||
QString tmp = "/home/data/Setting/standardLightFile";
|
||
QString standardLightFilePath_tmp = tmp + QDir::separator() + selector;
|
||
|
||
//判断定标文件是否存在
|
||
if (!isFileExist(standardLightFilePath_tmp))
|
||
{
|
||
*errorMessage = "No standard light file set.";
|
||
return CommandLineError;
|
||
}
|
||
|
||
query->standardLightFilePath = standardLightFilePath_tmp;
|
||
}
|
||
|
||
if (parser.isSet(standardLightFilePath))//
|
||
{
|
||
query->standardLightFilePath = parser.value(standardLightFilePath);
|
||
}
|
||
|
||
|
||
if (parser.isSet(calFileOutputDirectory))//定标保存文件路径
|
||
{
|
||
query->calFileOutputDirectory = parser.value(calFileOutputDirectory);
|
||
createDirectory(query->calFileOutputDirectory);//如果文件夹不存在 则创建
|
||
}
|
||
else//默认参数
|
||
{
|
||
QStringList tmp = calFileOutputDirectory.defaultValues();
|
||
QString directory = tmp[0];
|
||
|
||
createDirectory(directory);//如果文件夹不存在 则创建
|
||
|
||
query->calFileOutputDirectory = directory;
|
||
}
|
||
|
||
if (parser.isSet(calFileOutputName))//-------
|
||
{
|
||
QString calFileOutputNameTmp = QDir::cleanPath(query->calFileOutputDirectory + QDir::separator() + parser.value(calFileOutputName));
|
||
query->calFileOutputName = calFileOutputNameTmp;
|
||
}
|
||
else//默认参数
|
||
{
|
||
query->calFileOutputName = "";
|
||
}
|
||
|
||
// const QStringList positionalArguments = parser.positionalArguments();
|
||
// if (positionalArguments.isEmpty())
|
||
// {
|
||
// *errorMessage = "Argument 'name' missing.";
|
||
// return CommandLineError;
|
||
// }
|
||
// if (positionalArguments.size() > 1)
|
||
// {
|
||
// *errorMessage = "Several 'name' arguments specified.";
|
||
// return CommandLineError;
|
||
// }
|
||
|
||
return CommandLineOk;
|
||
}
|
||
|
||
bool copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist)
|
||
{
|
||
toDir.replace("\\","/");
|
||
if (sourceDir == toDir){
|
||
return true;
|
||
}
|
||
if (!QFile::exists(sourceDir)){
|
||
return false;
|
||
}
|
||
QDir *createfile = new QDir;
|
||
bool exist = createfile->exists(toDir);
|
||
if (exist){
|
||
if(coverFileIfExist){
|
||
createfile->remove(toDir);
|
||
}
|
||
}//end if
|
||
|
||
if(!QFile::copy(sourceDir, toDir))
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void logout(QString str)
|
||
{
|
||
std::cout << str.toStdString() << "<br>";
|
||
std::fflush(stdout);
|
||
}
|
||
|
||
void createDirectory(QString fullPath)//
|
||
{
|
||
QDir dir(fullPath);
|
||
if (dir.exists())
|
||
{
|
||
return;
|
||
}
|
||
else
|
||
{
|
||
bool ok = dir.mkdir(fullPath);//只创建一级子目录,即必须保证上级目录存在
|
||
return;
|
||
}
|
||
}
|
||
|
||
bool isFileExist(QString fullFileName)
|
||
{
|
||
QFileInfo fileInfo(fullFileName);
|
||
if (fileInfo.isFile())
|
||
{
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|