From b0eea2a6e6f9f598fd2c9abc7f631ca2d9fcdd17 Mon Sep 17 00:00:00 2001 From: bergamaschi Date: Wed, 29 Aug 2012 09:50:07 +0000 Subject: [PATCH] Splitted fileIO with fileIOStatic.h git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@235 951219d9-93cf-4727-9268-0efd64621fa3 --- .../slsDetector/slsDetectorActions.h | 1 - .../slsDetectorAnalysis/angCalLogClass.h | 2 + .../slsDetectorAnalysis/enCalLogClass.h | 104 +++- .../slsDetectorAnalysis/fileIO.cpp | 561 +----------------- .../slsDetectorAnalysis/fileIO.h | 187 +----- .../slsDetectorAnalysis/fileIOStatic.h | 477 +++++++++++++++ 6 files changed, 593 insertions(+), 739 deletions(-) create mode 100644 slsDetectorSoftware/slsDetectorAnalysis/fileIOStatic.h diff --git a/slsDetectorSoftware/slsDetector/slsDetectorActions.h b/slsDetectorSoftware/slsDetector/slsDetectorActions.h index 29828c43a..a3c4322a4 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorActions.h +++ b/slsDetectorSoftware/slsDetector/slsDetectorActions.h @@ -82,7 +82,6 @@ class slsDetectorActions : public virtual slsDetectorBase */ int getActionMode(int iaction); - /** set scan \param index of the scan (0,1) diff --git a/slsDetectorSoftware/slsDetectorAnalysis/angCalLogClass.h b/slsDetectorSoftware/slsDetectorAnalysis/angCalLogClass.h index b5b5f9efc..7b56af02e 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/angCalLogClass.h +++ b/slsDetectorSoftware/slsDetectorAnalysis/angCalLogClass.h @@ -22,6 +22,8 @@ class angCalLogClass { sprintf(cmd,"_%d.angcal",det->getFileIndex()); \ outfile.open(string(det->getFilePath()+string("/")+det->getFileName()+string(cmd)).c_str()); \ myDet=new slsDetectorCommand(det); \ + strcpy(cmd,"type"); \ + outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ strcpy(cmd,"nmod"); \ outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ strcpy(cmd,"angconv"); \ diff --git a/slsDetectorSoftware/slsDetectorAnalysis/enCalLogClass.h b/slsDetectorSoftware/slsDetectorAnalysis/enCalLogClass.h index ee050913d..957b33755 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/enCalLogClass.h +++ b/slsDetectorSoftware/slsDetectorAnalysis/enCalLogClass.h @@ -3,9 +3,17 @@ #include #include + + +#ifdef __CINT__ +#define MYROOT +#endif + +#ifndef MYROOT #include "slsDetectorCommand.h" #include "slsDetectorUtils.h" #include "sls_detector_defs.h" +#endif using namespace std; @@ -14,41 +22,113 @@ class enCalLogClass { public: - +#ifndef MYROOT enCalLogClass(slsDetectorUtils *det){ \ - char cmd[1000]; + char cmd[1000]; \ char *argv[2]; \ + strcpy(vars[0],"settings"); \ + strcpy(vars[1],"type"); \ + strcpy(vars[2],"nmod"); \ + strcpy(vars[3],"modulenumber"); \ argv[0]=cmd; \ sprintf(cmd,"_%d.encal",det->getFileIndex()); \ outfile.open(string(det->getFilePath()+string("/")+det->getFileName()+string(cmd)).c_str()); \ myDet=new slsDetectorCommand(det); \ - strcpy(cmd,"settings"); \ - outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ - strcpy(cmd,"nmod"); \ - outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ + strcpy(cmd,vars[0]); \ + outfile << cmd << " " << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ + strcpy(cmd,vars[1]); \ + outfile << cmd << " " << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ + strcpy(cmd,vars[2]); \ + outfile << cmd << " " << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ for (int im=0; imsetNumberOfModules(); im++) { \ - sprintf(cmd,"modulenumber:%d",im); \ - outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ - }; \ + sprintf(cmd,"%s:%d",vars[3],im); \ + outfile << cmd << " " << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ + } \ }; ~enCalLogClass(){delete myDet; outfile.close();}; +#else + enCalLogClass() { \ + strcpy(vars[0],"settings"); \ + strcpy(vars[1],"type"); \ + strcpy(vars[2],"nmod"); \ + strcpy(vars[3],"modulenumber"); \ + }; + ~enCalLogClass(){}; +#endif + int addStep(double threshold, string fname) {outfile << threshold << " " << fname << endl; return 0;}; + + // + + int readHeader(ifstream &infile, char *settings, int &nmod, int &chanspermod, int *mods ) { \ + nmod=0; strcpy(settings,"unknown"); chanspermod=0; \ + char line[1000], myvar[100], myarg[100]; \ + int dum; \ + for (int iv=0; iv<3; iv++) { \ + infile.getline(line,1000); \ + switch (iv) { \ + case 0: \ + sscanf(line,"settings %s", settings); \ + break; \ + case 1: \ + sscanf(line,"type %s", myarg); \ + if (string(myarg).find("Mythen")!=string::npos) \ + chanspermod=1280; \ + else if (string(myarg).find("Gotthard")!=string::npos) \ + chanspermod=1280; \ + else \ + chanspermod=65535; \ + break; \ + case 2: \ + sscanf(line,"nmod %d", &nmod); \ + break; \ + default: \ + ; \ + }; \ + if (infile.bad() || infile.eof()) { cout << "bad file "<< iv << endl; return -1;} \ + } \ + for (int im=0; im0 && pindex<=npos) - osfn << "_p" << pindex; - - //cout << osfn.str() << endl; - - // file index - osfn << "_" << findex; - - //cout << osfn.str() << endl; - - -#ifdef VERBOSE - cout << "created file name " << osfn.str() << endl; -#endif - - //cout << osfn.str() << endl; - //fn=oosfn.str()sfn.str(); - return osfn.str(); - -} - /* I/O */ /* generates file name without extension*/ string fileIO::createFileName() { - currentFileName=createFileName(filePath, \ + currentFileName=fileIOStatic::createFileName(filePath, \ fileName, \ getActionMask(), \ getCurrentScanVariable(0), \ @@ -68,496 +25,6 @@ string fileIO::createFileName() { } -int fileIO::getFileIndexFromFileName(string fname) { - int i; - size_t dot=fname.rfind("."); - if (dot==string::npos) - return -1; - size_t uscore=fname.rfind("_"); - if (uscore==string::npos) - return -1; - - if (sscanf( fname.substr(uscore+1,dot-uscore-1).c_str(),"%d",&i)) { - - return i; - } - //#ifdef VERBOSE - cout << "******************************** cannot parse file index" << endl; - //#endif - return 0; -} - -int fileIO::getVariablesFromFileName(string fname, int &index, int &p_index, double &sv0, double &sv1) { - - int i; - double f; - string s; - - - index=-1; - p_index=-1; - sv0=-1; - sv1=-1; - - - // size_t dot=fname.rfind("."); - //if (dot==string::npos) - // return -1; - size_t uscore=fname.rfind("_"); - if (uscore==string::npos) - return -1; - s=fname; - - //if (sscanf(s.substr(uscore+1,dot-uscore-1).c_str(),"%d",&i)) { - if (sscanf(s.substr(uscore+1,s.size()-uscore-1).c_str(),"%d",&i)) { - index=i; -#ifdef VERBOSE - cout << "******************************** file index is " << index << endl; -#endif - //return i; - s=fname.substr(0,uscore); - } - else - cout << "******************************** cannot parse file index" << endl; - -#ifdef VERBOSE - cout << s << endl; -#endif - - - uscore=s.rfind("_"); - - - - - if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"p%d",&i)) { - p_index=i; -#ifdef VERBOSE - cout << "******************************** position index is " << p_index << endl; -#endif - s=fname.substr(0,uscore); - } - else - cout << "******************************** cannot parse position index" << endl; - -#ifdef VERBOSE - cout << s << endl; -#endif - - - - - uscore=s.rfind("_"); - - - - - if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"s%lf",&f)) { - sv1=f; -#ifdef VERBOSE - cout << "******************************** scan variable 1 is " << sv1 << endl; -#endif - s=fname.substr(0,uscore); - } - else - cout << "******************************** cannot parse scan varable 1" << endl; - -#ifdef VERBOSE - cout << s << endl; - - -#endif - - uscore=s.rfind("_"); - - - - - if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"S%lf",&f)) { - sv0=f; -#ifdef VERBOSE - cout << "******************************** scan variable 0 is " << sv0 << endl; -#endif - } - else - cout << "******************************** cannot parse scan varable 0" << endl; - -#ifdef VERBOSE -#endif - - - - return index; -} - - - - - -int fileIO::writeDataFile(string fname, int nch, double *data, double *err, double *ang, char dataformat){ - - - ofstream outfile; - // int idata; - if (data==NULL) - return FAIL; - - // args|=0x10; // one line per channel! - - outfile.open (fname.c_str(),ios_base::out); - if (outfile.is_open()) - { - writeDataFile(outfile, nch, data, err, ang, dataformat, 0); - outfile.close(); - return OK; - } else { - std::cout<< "Could not open file " << fname << "for writing"<< std::endl; - return FAIL; - } -}; - - -int fileIO::writeDataFile(ofstream &outfile, int nch, double *data, double *err, double *ang, char dataformat, int offset){ - - - int idata; - if (data==NULL) - return FAIL; - - // args|=0x10; // one line per channel! - - - for (int ichan=0; ichan> ichan >> fdata; - //ich=ichan; - if (ssstr.fail() || ssstr.bad()) { - interrupt=1; - break; - } - // if (ich!=iline) - // std::cout<< "Channel number " << ichan << " does not match with line number " << iline << " " << dataformat << std::endl; - ich=iline; - if (ichan> fang >> fdata; - ich=iline; - } - if (ssstr.fail() || ssstr.bad()) { - interrupt=1; - break; - } - if (err) - ssstr >> ferr; - if (ssstr.fail() || ssstr.bad()) { - interrupt=1; - break; - } - if (ich=nch) { - interrupt=1; - break; - } - } - return iline; -}; - - - -int fileIO::readDataFile(string fname, int *data, int nch){ - - ifstream infile; - int iline=0;//ichan, idata, - //int interrupt=0; - string str; - -#ifdef VERBOSE - std::cout<< "Opening file "<< fname << std::endl; -#endif - infile.open(fname.c_str(), ios_base::in); - if (infile.is_open()) { - iline=readDataFile(infile, data, nch, 0); - infile.close(); - } else { - std::cout<< "Could not read file " << fname << std::endl; - return -1; - } - return iline; -}; - -int fileIO::readDataFile(ifstream &infile, int *data, int nch, int offset){ - - int ichan, idata, iline=0; - int interrupt=0; - string str; - - - while (infile.good() and interrupt==0) { - getline(infile,str); -#ifdef VERBOSE - std::cout<< str << std::endl; -#endif - istringstream ssstr(str); - ssstr >> ichan >> idata; - if (ssstr.fail() || ssstr.bad()) { - interrupt=1; - break; - } - // if (ichan!=iline) { -// std::cout<< " Expected channel "<< iline <<" but read channel "<< ichan << std::endl; -// interrupt=1; -// break; -// } else { - if (iline=offset) { - data[iline]=idata; - iline++; - } - } else { - interrupt=1; - break; - } - // } - } - return iline; -}; - - -int fileIO::readDataFile(string fname, short int *data, int nch){ - - ifstream infile; - int iline=0;//ichan, - //int interrupt=0; - string str; - -#ifdef VERBOSE - std::cout<< "Opening file "<< fname << std::endl; -#endif - infile.open(fname.c_str(), ios_base::in); - if (infile.is_open()) { - iline=readDataFile(infile, data, nch, 0); - infile.close(); - } else { - std::cout<< "Could not read file " << fname << std::endl; - return -1; - } - return iline; -}; - -int fileIO::readDataFile(ifstream &infile, short int *data, int nch, int offset){ - - int ichan, iline=0; - short int idata; - int interrupt=0; - string str; - while (infile.good() and interrupt==0) { - getline(infile,str); -#ifdef VERBOSE - ;//std::cout<< str << std::endl; -#endif - istringstream ssstr(str); - ssstr >> ichan >> idata; - if (ssstr.fail() || ssstr.bad()) { - interrupt=1; - break; - } - // if (ichan!=iline) { -// std::cout<< " Expected channel "<< iline <<" but read channel "<< ichan << std::endl; -// interrupt=1; -// break; -// } else { - if (iline=offset) { - data[iline]=idata; - iline++; - } - } else { - interrupt=1; - break; - } - // } -#ifdef VERBOSE - ;//std::cout<< "read " << iline <<" channels " << std::endl; -#endif - } - return iline; -} - /*writes raw data file */ @@ -566,28 +33,26 @@ int fileIO::writeDataFile(string fname, double *data, double *err, double *ang, if (nch==-1) nch=getTotalNumberOfChannels(); - return writeDataFile(fname, nch, data, err, ang, dataformat); + return fileIOStatic::writeDataFile(fname, nch, data, err, ang, dataformat); } int fileIO::writeDataFile(ofstream &outfile, double *data, double *err, double *ang, char dataformat, int nch, int offset){ if (nch==-1) nch=getTotalNumberOfChannels(); - return writeDataFile(outfile, nch, data, err, ang, dataformat, offset); + return fileIOStatic::writeDataFile(outfile, nch, data, err, ang, dataformat, offset); } - - int fileIO::writeDataFile(string fname, int *data){ - return writeDataFile(fname, getTotalNumberOfChannels(), data); + return fileIOStatic::writeDataFile(fname, getTotalNumberOfChannels(), data); } int fileIO::writeDataFile(ofstream &outfile, int *data, int offset){ - return writeDataFile(outfile, getTotalNumberOfChannels(), data, offset); + return fileIOStatic::writeDataFile(outfile, getTotalNumberOfChannels(), data, offset); } @@ -596,24 +61,24 @@ int fileIO::writeDataFile(ofstream &outfile, int *data, int offset){ int fileIO::writeDataFile(string fname, short int *data){ - return writeDataFile(fname, getTotalNumberOfChannels(), data); + return fileIOStatic::writeDataFile(fname, getTotalNumberOfChannels(), data); } int fileIO::writeDataFile(ofstream &outfile, short int *data, int offset){ - return writeDataFile(outfile, getTotalNumberOfChannels(), data, offset); + return fileIOStatic::writeDataFile(outfile, getTotalNumberOfChannels(), data, offset); } int fileIO::readDataFile(string fname, double *data, double *err, double *ang, char dataformat) { - return readDataFile(getTotalNumberOfChannels(), fname, data, err, ang, dataformat); + return fileIOStatic::readDataFile(getTotalNumberOfChannels(), fname, data, err, ang, dataformat); } int fileIO::readDataFile(ifstream &infile, double *data, double *err, double *ang, char dataformat, int offset) { - return readDataFile(getTotalNumberOfChannels(), infile, data, err, ang, dataformat, offset); + return fileIOStatic::readDataFile(getTotalNumberOfChannels(), infile, data, err, ang, dataformat, offset); } @@ -621,13 +86,13 @@ int fileIO::readDataFile(ifstream &infile, double *data, double *err, double *an int fileIO::readDataFile(string fname, int *data){ - return readDataFile(fname, data, getTotalNumberOfChannels()); + return fileIOStatic::readDataFile(fname, data, getTotalNumberOfChannels()); }; int fileIO::readDataFile(ifstream &infile, int *data, int offset){ - return readDataFile(infile, data, getTotalNumberOfChannels(), offset); + return fileIOStatic::readDataFile(infile, data, getTotalNumberOfChannels(), offset); }; @@ -636,12 +101,12 @@ int fileIO::readDataFile(ifstream &infile, int *data, int offset){ int fileIO::readDataFile(string fname, short int *data){ - return readDataFile(fname, data, getTotalNumberOfChannels()); + return fileIOStatic::readDataFile(fname, data, getTotalNumberOfChannels()); }; int fileIO::readDataFile(ifstream &infile, short int *data, int offset){ - return readDataFile(infile, data, getTotalNumberOfChannels(),offset); + return fileIOStatic::readDataFile(infile, data, getTotalNumberOfChannels(),offset); }; diff --git a/slsDetectorSoftware/slsDetectorAnalysis/fileIO.h b/slsDetectorSoftware/slsDetectorAnalysis/fileIO.h index b5adc6f1d..d423edee9 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/fileIO.h +++ b/slsDetectorSoftware/slsDetectorAnalysis/fileIO.h @@ -2,6 +2,8 @@ #define FILEIO_H #include "slsDetectorBase.h" +#include "fileIOStatic.h" + #include #include #include @@ -16,17 +18,21 @@ using namespace std; @short class handling the data file I/O flags */ -class fileIO : public virtual slsDetectorBase { - +class fileIO : public fileIOStatic, public virtual slsDetectorBase { public: /** default constructor */ - fileIO(){}; + fileIO(): fileIOStatic() {}; /** virtual destructor */ virtual ~fileIO(){}; + using fileIOStatic::readDataFile; + using fileIOStatic::writeDataFile; + using fileIOStatic::createFileName; + int getFileIndexFromFileName(string fname){return fileIOStatic::getFileIndexFromFileName(fname);}; + int getVariablesFromFileName(string fname, int &index, int &p_index, double &sv0, double &sv1){return fileIOStatic::getVariablesFromFileName(fname, index, p_index, sv0, sv1);}; /** sets the default output files path \param s file path @@ -67,53 +73,9 @@ class fileIO : public virtual slsDetectorBase { int getFileIndex() {return *fileIndex;}; - - - - - /** generates file name without extension - - always appends to file path and file name the run index. - - in case also appends the position index and the two level of scan varaibles with the specified precision - - Filenames will be of the form: filepath/filename(_Sy_sw_px)_i - where y is the scan0 variable, W is the scan 1 variable, x is the position index and i is the run index - \param filepath outdir - \param filename file root name - \param aMask action mask (scans, positions) - \param sv0 scan variable 0 - \param prec0 scan precision 0 - \param sv1 scan variable 1 - \param prec1 scan precision 1 - \param pindex position index - \param number of positions - \param findex file index - \returns file name without extension - */ - static string createFileName(char *filepath, char *filename, int aMask, double sv0, int prec0, double sv1, int prec1, int pindex, int npos, int findex); - - string createFileName(); - /** static function that returns the file index from the file name - \param fname file name - \returns file index - */ - int getFileIndexFromFileName(string fname); - - /** static function that returns the variables from the file name - \param fname file name - \param index reference to index - \param p_index reference to position index - \param sv0 reference to scan variable 0 - \param sv1 reference to scan variable 1 - \returns file index - */ - static int getVariablesFromFileName(string fname, int &index, int &p_index, double &sv0, double &sv1); - - /** @@ -242,138 +204,7 @@ class fileIO : public virtual slsDetectorBase { */ int readDataFile(ifstream &infile, short int *data, int offset=0); - /** - - writes a data file - \param fname of the file to be written - \param nch number of channels to be written - \param data array of data values - \param err array of arrors on the data. If NULL no errors will be written - \param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err) - \param dataformat format of the data: can be 'i' integer or 'f' double (default) - \returns OK or FAIL if it could not write the file or data=NULL - - */ - static int writeDataFile(string fname, int nch, double *data, double *err=NULL, double *ang=NULL, char dataformat='f'); - /** - writes a data file - \param outfile output file stream - \param nch number of channels to be written - \param data array of data values - \param err array of arrors on the data. If NULL no errors will be written - \param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err) - \param dataformat format of the data: can be 'i' integer or 'f' double (default) - \param offset start channel number - \returns OK or FAIL if it could not write the file or data=NULL - */ - static int writeDataFile(ofstream &outfile, int nch, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int offset=0); - - /** - writes a raw data file - \param fname of the file to be written - \param nch number of channels - \param data array of data values - \returns OK or FAIL if it could not write the file or data=NULL - */ - static int writeDataFile(string fname,int nch, int *data); - - /** - writes a raw data file - \param outfile output file stream - \param nch number of channels - \param data array of data values - \param offset start channel number - \returns OK or FAIL if it could not write the file or data=NULL - */ - static int writeDataFile(ofstream &outfile,int nch, int *data, int offset=0); - - - /** - - writes a short int raw data file - \param fname of the file to be written - \param nch number of channels - \param data array of data values - \returns OK or FAIL if it could not write the file or data=NULL - */ - static int writeDataFile(string fname,int nch, short int *data); - - /** - writes a short int raw data file - \param outfile output file stream - \param nch number of channels - \param data array of data values - \param offset start channel number - \returns OK or FAIL if it could not write the file or data=NULL - */ - static int writeDataFile(ofstream &outfile,int nch, short int *data, int offset=0); - - - /** - reads a data file - \param nch number of channels - \param fname of the file to be read - \param data array of data values to be filled - \param err array of arrors on the data. If NULL no errors are expected on the file - \param ang array of angular values. If NULL data are expected in the form chan-val(-err) otherwise ang-val(-err) - \param dataformat format of the data: can be 'i' integer or 'f' double (default) - \returns number of channels read or -1 if it could not read the file or data=NULL - - */ - static int readDataFile(int nch, string fname, double *data, double *err=NULL, double *ang=NULL, char dataformat='f'); - /** - reads a data file - \param nch number of channels - \param infile input file stream - \param data array of data values to be filled - \param err array of arrors on the data. If NULL no errors are expected on the file - \param ang array of angular values. If NULL data are expected in the form chan-val(-err) otherwise ang-val(-err) - \param dataformat format of the data: can be 'i' integer or 'f' double (default) - \param offset start channel number - \returns number of channels read or -1 if it could not read the file or data=NULL - - */ - static int readDataFile(int nch, ifstream &infile, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int offset=0); - - /** - reads a raw data file - \param fname of the file to be read - \param data array of data values - \param nch number of channels - \returns OK or FAIL if it could not read the file or data=NULL - */ - static int readDataFile(string fname, int *data, int nch); - - /** - reads a raw data file - \param infile input file stream - \param data array of data values - \param nch number of channels - \param offset start channel value - \returns OK or FAIL if it could not read the file or data=NULL - */ - static int readDataFile(ifstream &infile, int *data, int nch, int offset); - - /** - reads a short int rawdata file - \param name of the file to be read - \param data array of data values - \param nch number of channels - \returns OK or FAIL if it could not read the file or data=NULL - */ - static int readDataFile(string fname, short int *data, int nch); - /** - reads a short int raw data file - \param infile input file stream - \param data array of data values - \param nch number of channels - \param offset start channel value - \returns OK or FAIL if it could not read the file or data=NULL - */ - static int readDataFile(ifstream &infile, short int *data, int nch, int offset); - - protected: diff --git a/slsDetectorSoftware/slsDetectorAnalysis/fileIOStatic.h b/slsDetectorSoftware/slsDetectorAnalysis/fileIOStatic.h new file mode 100644 index 000000000..25f135624 --- /dev/null +++ b/slsDetectorSoftware/slsDetectorAnalysis/fileIOStatic.h @@ -0,0 +1,477 @@ +#ifndef FILEIO_STATIC_H +#define FILEIO_STATIC_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include "sls_detector_defs.h" + +using namespace std; +/** + @short class handling the data file I/O flags +*/ + +class fileIOStatic { + + + + + public: + /** default constructor */ + fileIOStatic(){}; + /** virtual de`structor */ + virtual ~fileIOStatic(){}; + + + + + /** generates file name without extension + + always appends to file path and file name the run index. + + in case also appends the position index and the two level of scan varaibles with the specified precision + + Filenames will be of the form: filepath/filename(_Sy_sw_px)_i + where y is the scan0 variable, W is the scan 1 variable, x is the position index and i is the run index + \param filepath outdir + \param filename file root name + \param aMask action mask (scans, positions) + \param sv0 scan variable 0 + \param prec0 scan precision 0 + \param sv1 scan variable 1 + \param prec1 scan precision 1 + \param pindex position index + \param number of positions + \param findex file index + \returns file name without extension + */ + static string createFileName(char *filepath, char *filename, int aMask, double sv0, int prec0, double sv1, int prec1, int pindex, int npos, int findex){ \ + ostringstream osfn; \ + osfn << filepath << "/" << filename; \ + if ( aMask& (1 << (slsDetectorDefs::MAX_ACTIONS))) osfn << "_S" << fixed << setprecision(prec0) << sv0; \ + if (aMask & (1 << (slsDetectorDefs::MAX_ACTIONS+1))) osfn << "_s" << fixed << setprecision(prec1) << sv1; \ + if (pindex>0 && pindex<=npos) osfn << "_p" << pindex; \ + osfn << "_" << findex; \ + return osfn.str(); \ + }; + + /** static function that returns the file index from the file name + \param fname file name + \returns file index + */ + static int getFileIndexFromFileName(string fname){ \ + int i; \ + size_t dot=fname.rfind("."); \ + if (dot==string::npos) \ + return -1; \ + size_t uscore=fname.rfind("_"); \ + if (uscore==string::npos) return -1; \ + if (sscanf( fname.substr(uscore+1,dot-uscore-1).c_str(),"%d",&i)) return i; \ + cout << "******************************** cannot parse file index" << endl; \ + return 0; \ + }; + + + + /** static function that returns the variables from the file name + \param fname file name + \param index reference to index + \param p_index reference to position index + \param sv0 reference to scan variable 0 + \param sv1 reference to scan variable 1 + \returns file index + */ + static int getVariablesFromFileName(string fname, int &index, int &p_index, double &sv0, double &sv1) { \ + int i; \ + double f; \ + string s; \ + index=-1; \ + p_index=-1; \ + sv0=-1; \ + sv1=-1; \ + size_t uscore=fname.rfind("_"); \ + if (uscore==string::npos) return -1; \ + s=fname; \ + if (sscanf(s.substr(uscore+1,s.size()-uscore-1).c_str(),"%d",&i)) { \ + index=i; \ + s=fname.substr(0,uscore); \ + } \ + else cout << "******************************** cannot parse file index" << endl; \ + uscore=s.rfind("_"); \ + if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"p%d",&i)) { \ + p_index=i; \ + s=fname.substr(0,uscore); \ + } \ + else cout << "******************************** cannot parse position index" << endl; \ + uscore=s.rfind("_"); \ + if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"s%lf",&f)) { \ + sv1=f; \ + s=fname.substr(0,uscore); \ + } \ + else cout << "******************************** cannot parse scan varable 1" << endl; \ + uscore=s.rfind("_"); \ + if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"S%lf",&f)) { \ + sv0=f; \ + } \ + else cout << "******************************** cannot parse scan varable 0" << endl; \ + return index; \ + }; + + + /** + + writes a data file + \param fname of the file to be written + \param nch number of channels to be written + \param data array of data values + \param err array of arrors on the data. If NULL no errors will be written + \param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err) + \param dataformat format of the data: can be 'i' integer or 'f' double (default) + \returns OK or FAIL if it could not write the file or data=NULL + + */ + + static int writeDataFile(string fname, int nch, double *data, double *err=NULL, double *ang=NULL, char dataformat='f'){ \ + ofstream outfile; \ + if (data==NULL) return slsDetectorDefs::FAIL; \ + outfile.open (fname.c_str(),ios_base::out); \ + if (outfile.is_open()) { \ + writeDataFile(outfile, nch, data, err, ang, dataformat, 0); \ + outfile.close(); \ + return slsDetectorDefs::OK; \ + } else { \ + std::cout<< "Could not open file " << fname << "for writing"<< std::endl; \ + return slsDetectorDefs::FAIL; \ + } \ + }; + + /** + writes a data file + \param outfile output file stream + \param nch number of channels to be written + \param data array of data values + \param err array of arrors on the data. If NULL no errors will be written + \param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err) + \param dataformat format of the data: can be 'i' integer or 'f' double (default) + \param offset start channel number + \returns OK or FAIL if it could not write the file or data=NULL + */ + static int writeDataFile(ofstream &outfile, int nch, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int offset=0){ \ + int idata; \ + if (data==NULL) \ + return slsDetectorDefs::FAIL; \ + for (int ichan=0; ichan> ichan >> fdata; \ + if (ssstr.fail() || ssstr.bad()) { \ + interrupt=1; \ + break; \ + } \ + ich=iline; \ + if (ichan> fang >> fdata; \ + ich=iline; \ + } \ + if (ssstr.fail() || ssstr.bad()) { \ + interrupt=1; \ + break; \ + } \ + if (err) \ + ssstr >> ferr; \ + if (ssstr.fail() || ssstr.bad()) { \ + interrupt=1; \ + break; \ + } \ + if (ich=nch) { \ + interrupt=1; \ + break; \ + } \ + } \ + return iline; \ + }; + + + /** + reads a raw data file + \param fname of the file to be read + \param data array of data values + \param nch number of channels + \returns OK or FAIL if it could not read the file or data=NULL + */ + static int readDataFile(string fname, int *data, int nch) { \ + ifstream infile; \ + int iline=0; \ + string str; \ + infile.open(fname.c_str(), ios_base::in); \ + if (infile.is_open()) { \ + iline=readDataFile(infile, data, nch, 0); \ + infile.close(); \ + } else { \ + std::cout<< "Could not read file " << fname << std::endl; \ + return -1; \ + } \ + return iline; \ + }; + + + /** + reads a raw data file + \param infile input file stream + \param data array of data values + \param nch number of channels + \param offset start channel value + \returns OK or FAIL if it could not read the file or data=NULL + */ + static int readDataFile(ifstream &infile, int *data, int nch, int offset) { \ + int ichan, idata, iline=0; \ + int interrupt=0; \ + string str; \ + while (infile.good() and interrupt==0) { \ + getline(infile,str); \ + istringstream ssstr(str); \ + ssstr >> ichan >> idata; \ + if (ssstr.fail() || ssstr.bad()) { \ + interrupt=1; \ + break; \ + } \ + if (iline=offset) { \ + data[iline]=idata; \ + iline++; \ + } \ + } else { \ + interrupt=1; \ + break; \ + } \ + } \ + return iline; \ + }; + + /** + reads a short int rawdata file + \param name of the file to be read + \param data array of data values + \param nch number of channels + \returns OK or FAIL if it could not read the file or data=NULL + */ + static int readDataFile(string fname, short int *data, int nch){ \ + ifstream infile; \ + int iline=0; \ + string str; \ + infile.open(fname.c_str(), ios_base::in); \ + if (infile.is_open()) { \ + iline=readDataFile(infile, data, nch, 0); \ + infile.close(); \ + } else { \ + std::cout<< "Could not read file " << fname << std::endl; \ + return -1; \ + } \ + return iline; \ + }; + + /** + reads a short int raw data file + \param infile input file stream + \param data array of data values + \param nch number of channels + \param offset start channel value + \returns OK or FAIL if it could not read the file or data=NULL + */ + static int readDataFile(ifstream &infile, short int *data, int nch, int offset) { \ + int ichan, iline=0; \ + short int idata; \ + int interrupt=0; \ + string str; \ + while (infile.good() and interrupt==0) { \ + getline(infile,str); \ + istringstream ssstr(str); \ + ssstr >> ichan >> idata; \ + if (ssstr.fail() || ssstr.bad()) { \ + interrupt=1; \ + break; \ + } \ + if (iline=offset) { \ + data[iline]=idata; \ + iline++; \ + } \ + } else { \ + interrupt=1; \ + break; \ + } \ + return iline; \ + }; \ + }; + +}; + +#endif