2022-01-14 14:26:03 +08:00
# 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 ; //<2F> <> Ĭ<EFBFBD> <C4AC> ֵ
int averageTimes ;
int position ;
QString calFileOutputDirectory ; //<2F> <> Ĭ<EFBFBD> <C4AC> ֵ
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 " ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> в<EFBFBD> <D0B2> <EFBFBD>
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 ( ) ;
}
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ƕ<EFBFBD> <C7B6> <EFBFBD>
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 ( ) ;
}
//<2F> <> <EFBFBD> ӹ<EFBFBD> <D3B9> <EFBFBD> <EFBFBD> <EFBFBD>
QString SN ;
QString pixelCount ;
QString wavelengthInfo ;
logout ( " <br><b style= \" color:red \" >Connectting the fiber spectrometer!</b> " ) ;
m_FiberSpectrometer - > connectFiberSpectrometer ( SN , pixelCount , wavelengthInfo ) ;
//<2F> Զ<EFBFBD> <D4B6> ع<EFBFBD>
logout ( " <br><b style= \" color:red \" >AutoExpose!</b> " ) ;
2022-01-19 14:55:01 +08:00
m_FiberSpectrometer - > autoExpose ( ) ;
2022-01-14 14:26:03 +08:00
//<2F> <> <EFBFBD> <EFBFBD> sleep<65> <70> <EFBFBD> ȴ<EFBFBD> <C8B4> رտ<D8B1> <D5BF> <EFBFBD>
logout ( " <br><b style= \" color:red \" >Wait for close the lamp!</b> " ) ;
QThread : : sleep ( query . sleepTimeinSecond ) ;
//<2F> ɼ<EFBFBD> <C9BC> <EFBFBD> ֡
logout ( " <br><b style= \" color:red \" >Record dark frame!</b> " ) ;
m_FiberSpectrometer - > recordDark ( query . calFileOutputDirectory ) ;
//<2F> <> <EFBFBD> <EFBFBD> sleep<65> <70> <EFBFBD> ȴ<EFBFBD> <C8B4> <EFBFBD> <F2BFAABF> <EFBFBD>
logout ( " <br><b style= \" color:red \" >Wait for open the lamp!</b> " ) ;
QThread : : sleep ( query . sleepTimeinSecond ) ;
//<2F> ɼ<EFBFBD> <C9BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
logout ( " <br><b style= \" color:red \" >Record integrating sphere frame!</b> " ) ;
m_FiberSpectrometer - > recordTarget ( query . averageTimes , query . calFileOutputDirectory ) ;
//<> <D7BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD>
DeviceAttribute deviceAttribute ;
DeviceInfo deviceInfo ;
m_FiberSpectrometer - > getDeviceAttribute ( deviceAttribute ) ;
m_FiberSpectrometer - > getDeviceInfo ( deviceInfo ) ;
CalibrationAlgorithm * m_CalibrationAlgorithm = new CalibrationAlgorithm ( ) ;
m_CalibrationAlgorithm - > readFile ( query . standardLightFilePath , deviceAttribute , deviceInfo ) ; //
//<2F> <> <EFBFBD> ɷ<EFBFBD> <C9B7> 䶨<EFBFBD> <E4B6A8> <EFBFBD> ļ<EFBFBD>
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 ) ;
//<2F> <> <EFBFBD> Ʒ <EFBFBD> <C6B7> 䶨<EFBFBD> <E4B6A8> <EFBFBD> ļ<EFBFBD>
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 ) ;
//<2F> Ͽ<EFBFBD> <CFBF> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
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 " ) ; //<2F> <> <EFBFBD> <EFBFBD> Ĭ<EFBFBD> ϲ <EFBFBD> <CFB2> <EFBFBD>
parser . addOption ( sleepTimeinSecond ) ;
QCommandLineOption averageTimes ( " a " , " Average times. " , " average_times " ) ;
averageTimes . setDefaultValue ( " 5 " ) ; //<2F> <> <EFBFBD> <EFBFBD> Ĭ<EFBFBD> ϲ <EFBFBD> <CFB2> <EFBFBD>
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 ) ;
//<2F> <> <EFBFBD> <D7BC> <EFBFBD> ļ<EFBFBD>
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 ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> ·<EFBFBD> <C2B7>
// 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 ) ; //<2F> <> <EFBFBD> <EFBFBD> Ĭ<EFBFBD> ϲ <EFBFBD> <CFB2> <EFBFBD> <EFBFBD> <EFBFBD> QCoreApplication::applicationDirPath()<29> <> standardLightFile
parser . addOption ( calFileOutputDirectory ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD>
// 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 //Ĭ<> ϲ <EFBFBD> <CFB2> <EFBFBD>
{
* errorMessage = " No deviceType set. " ;
return CommandLineError ;
}
if ( parser . isSet ( serialPort ) )
{
const QString serialPortTmp = parser . value ( serialPort ) ;
query - > serialPort = serialPortTmp ;
}
else //Ĭ<> ϲ <EFBFBD> <CFB2> <EFBFBD>
{
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 //Ĭ<> ϲ <EFBFBD> <CFB2> <EFBFBD>
{
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 //Ĭ<> ϲ <EFBFBD> <CFB2> <EFBFBD>
{
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 ) ) //û<> <C3BB> <EFBFBD> <EFBFBD> <EFBFBD> ö<EFBFBD> <C3B6> 걣<EFBFBD> <EAB1A3> <EFBFBD> ļ<EFBFBD> ·<EFBFBD> <C2B7>
{
* 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 ;
//<2F> ж϶<D0B6> <CFB6> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> Ƿ<EFBFBD> <C7B7> <EFBFBD> <EFBFBD> <EFBFBD>
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 ) ) //<2F> <> <EFBFBD> 걣<EFBFBD> <EAB1A3> <EFBFBD> ļ<EFBFBD> ·<EFBFBD> <C2B7>
{
query - > calFileOutputDirectory = parser . value ( calFileOutputDirectory ) ;
createDirectory ( query - > calFileOutputDirectory ) ; //<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> в<EFBFBD> <D0B2> <EFBFBD> <EFBFBD> <EFBFBD> <20> <EFBFBD>
}
else //Ĭ<> ϲ <EFBFBD> <CFB2> <EFBFBD>
{
QStringList tmp = calFileOutputDirectory . defaultValues ( ) ;
QString directory = tmp [ 0 ] ;
createDirectory ( directory ) ; //<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> в<EFBFBD> <D0B2> <EFBFBD> <EFBFBD> <EFBFBD> <20> <EFBFBD>
query - > calFileOutputDirectory = directory ;
}
if ( parser . isSet ( calFileOutputName ) ) //-------
{
QString calFileOutputNameTmp = QDir : : cleanPath ( query - > calFileOutputDirectory + QDir : : separator ( ) + parser . value ( calFileOutputName ) ) ;
query - > calFileOutputName = calFileOutputNameTmp ;
}
else //Ĭ<> ϲ <EFBFBD> <CFB2> <EFBFBD>
{
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 ) ; //ֻ<> <D6BB> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> Ŀ¼<C4BF> <C2BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> 뱣֤<EBB1A3> ϼ<EFBFBD> Ŀ¼<C4BF> <C2BC> <EFBFBD> <EFBFBD>
return ;
}
}
bool isFileExist ( QString fullFileName )
{
QFileInfo fileInfo ( fullFileName ) ;
if ( fileInfo . isFile ( ) )
{
return true ;
}
return false ;
}