Splitted fileIO with fileIOStatic.h

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@235 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
bergamaschi 2012-08-29 09:50:07 +00:00
parent 852bd47bc9
commit b0eea2a6e6
6 changed files with 593 additions and 739 deletions

View File

@ -82,7 +82,6 @@ class slsDetectorActions : public virtual slsDetectorBase
*/ */
int getActionMode(int iaction); int getActionMode(int iaction);
/** /**
set scan set scan
\param index of the scan (0,1) \param index of the scan (0,1)

View File

@ -22,6 +22,8 @@ class angCalLogClass {
sprintf(cmd,"_%d.angcal",det->getFileIndex()); \ sprintf(cmd,"_%d.angcal",det->getFileIndex()); \
outfile.open(string(det->getFilePath()+string("/")+det->getFileName()+string(cmd)).c_str()); \ outfile.open(string(det->getFilePath()+string("/")+det->getFileName()+string(cmd)).c_str()); \
myDet=new slsDetectorCommand(det); \ myDet=new slsDetectorCommand(det); \
strcpy(cmd,"type"); \
outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \
strcpy(cmd,"nmod"); \ strcpy(cmd,"nmod"); \
outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \
strcpy(cmd,"angconv"); \ strcpy(cmd,"angconv"); \

View File

@ -3,9 +3,17 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#ifdef __CINT__
#define MYROOT
#endif
#ifndef MYROOT
#include "slsDetectorCommand.h" #include "slsDetectorCommand.h"
#include "slsDetectorUtils.h" #include "slsDetectorUtils.h"
#include "sls_detector_defs.h" #include "sls_detector_defs.h"
#endif
using namespace std; using namespace std;
@ -14,41 +22,113 @@ class enCalLogClass {
public: public:
#ifndef MYROOT
enCalLogClass(slsDetectorUtils *det){ \ enCalLogClass(slsDetectorUtils *det){ \
char cmd[1000]; char cmd[1000]; \
char *argv[2]; \ char *argv[2]; \
strcpy(vars[0],"settings"); \
strcpy(vars[1],"type"); \
strcpy(vars[2],"nmod"); \
strcpy(vars[3],"modulenumber"); \
argv[0]=cmd; \ argv[0]=cmd; \
sprintf(cmd,"_%d.encal",det->getFileIndex()); \ sprintf(cmd,"_%d.encal",det->getFileIndex()); \
outfile.open(string(det->getFilePath()+string("/")+det->getFileName()+string(cmd)).c_str()); \ outfile.open(string(det->getFilePath()+string("/")+det->getFileName()+string(cmd)).c_str()); \
myDet=new slsDetectorCommand(det); \ myDet=new slsDetectorCommand(det); \
strcpy(cmd,"settings"); \ strcpy(cmd,vars[0]); \
outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ outfile << cmd << " " << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \
strcpy(cmd,"nmod"); \ strcpy(cmd,vars[1]); \
outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ 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; im<det->setNumberOfModules(); im++) { \ for (int im=0; im<det->setNumberOfModules(); im++) { \
sprintf(cmd,"modulenumber:%d",im); \ sprintf(cmd,"%s:%d",vars[3],im); \
outfile << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \ outfile << cmd << " " << myDet->executeLine(1,argv,slsDetectorDefs::GET_ACTION) << endl; \
}; \ } \
}; };
~enCalLogClass(){delete myDet; outfile.close();}; ~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 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; im<nmod; im++) { \
infile.getline(line,1000); \
sscanf(line,"modulenumber:%d %x",&dum,mods+im); \
if (dum!=im) cout << "read module number "<< dum << " does not match with " << im << " as expected" << endl;
if (infile.bad() || infile.eof()) { cout << "bad file - module "<< im << endl; return -1;} \
}
\
return 0; \
};
int getStep(ifstream &infile, double &threshold, char *datafname){ \
char line[1000]; \
float v;
infile.getline(line,1000); \
if (sscanf(line,"%g %s",&v, datafname)<2) return -1; \
threshold=v; \
if (infile.bad() || infile.eof()) \
return -1; \
return 0; \
};
private: private:
#ifndef MYROOT
slsDetectorCommand *myDet; slsDetectorCommand *myDet;
#endif
ofstream outfile; ofstream outfile;
ifstream infile;
char vars[4][100];
}; };
#endif #endif

View File

@ -3,56 +3,13 @@
string fileIO::createFileName(char *filepath, char *filename, int aMask, double sv0, int prec0, double sv1, int prec1, int pindex, int npos, int findex) {
ostringstream osfn;
// string fn;
/*directory name +root file name */
osfn << filepath << "/" << filename;
// cout << osfn.str() << endl;
// scan level 0
if ( aMask& (1 << (MAX_ACTIONS)))
osfn << "_S" << fixed << setprecision(prec0) << sv0;
//cout << osfn.str() << endl;
//scan level 1
if (aMask & (1 << (MAX_ACTIONS+1)))
osfn << "_s" << fixed << setprecision(prec1) << sv1;
//cout << osfn.str() << endl;
//position
if (pindex>0 && 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 */ /* I/O */
/* generates file name without extension*/ /* generates file name without extension*/
string fileIO::createFileName() { string fileIO::createFileName() {
currentFileName=createFileName(filePath, \ currentFileName=fileIOStatic::createFileName(filePath, \
fileName, \ fileName, \
getActionMask(), \ getActionMask(), \
getCurrentScanVariable(0), \ 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<nch; ichan++) {
if (ang==NULL) {
outfile << ichan+offset << " ";
} else {
outfile << ang[ichan] << " ";
}
switch (dataformat) {
case 'f':
outfile << *(data+ichan)<< " ";
break;
case 'i':
default:
idata=(int)(*(data+ichan));
outfile << idata << " ";
}
if (err) {
outfile << *(err+ichan)<< " ";
}
// if (args&0x10) {
outfile << std::endl;
// }
}
return OK;
}
/*writes raw data file */
int fileIO::writeDataFile(string fname, int nch, int *data){
ofstream outfile;
if (data==NULL)
return FAIL;
outfile.open (fname.c_str(),ios_base::out);
if (outfile.is_open())
{
writeDataFile(outfile, nch, data, 0);
outfile.close();
return OK;
} else {
std::cout<< "Could not open file " << fname << "for writing"<< std::endl;
return FAIL;
}
};
/*writes raw data file */
int fileIO::writeDataFile(ofstream &outfile, int nch, int *data, int offset){
if (data==NULL)
return FAIL;
for (int ichan=0; ichan<nch; ichan++)
outfile << ichan+offset << " " << *(data+ichan) << std::endl;
return OK;
};
/*writes raw data file */
int fileIO::writeDataFile(string fname, int nch, short int *data){
ofstream outfile;
if (data==NULL)
return FAIL;
outfile.open (fname.c_str(),ios_base::out);
if (outfile.is_open())
{
writeDataFile(outfile, nch, data, 0);
outfile.close();
return OK;
} else {
std::cout<< "Could not open file " << fname << "for writing"<< std::endl;
return FAIL;
}
};
/*writes raw data file */
int fileIO::writeDataFile(ofstream &outfile, int nch, short int *data, int offset){
if (data==NULL)
return FAIL;
for (int ichan=0; ichan<nch; ichan++)
outfile << ichan+offset << " " << *(data+ichan) << std::endl;
return OK;
};
int fileIO::readDataFile(int nch, string fname, double *data, double *err, double *ang, char dataformat){
ifstream infile;
int iline=0;//ichan,
//int interrupt=0;
//double fdata, ferr, fang;
int maxchans;
//int ich;
string str;
maxchans=nch;
#ifdef VERBOSE
std::cout<< "Opening file "<< fname << std::endl;
#endif
infile.open(fname.c_str(), ios_base::in);
if (infile.is_open()) {
iline=readDataFile(nch, infile, data, err, ang, dataformat, 0);
infile.close();
} else {
std::cout<< "Could not read file " << fname << std::endl;
return -1;
}
return iline;
};
int fileIO::readDataFile(int nch, ifstream &infile, double *data, double *err, double *ang, char dataformat, int offset){
int ichan,iline=0;
int interrupt=0;
double fdata, ferr, fang;
int maxchans;
int ich;
string str;
maxchans=nch;
while (infile.good() and interrupt==0) {
getline(infile,str);
#ifdef VERBOSE
std::cout<< str << std::endl;
#endif
istringstream ssstr(str);
if (ang==NULL) {
ssstr >> 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<offset)
continue;
} else {
ssstr >> 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<maxchans) {
if (ang) {
ang[ich]=fang;
}
data[ich]=fdata;
if (err)
err[ich]=ferr;
iline++;
} // else {
// std::cout<< " too many lines in file: "<< iline << " instead of " << maxchans << std::endl;
// interrupt=1;
// break;
// }
if (iline>=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<nch) {
if (ichan>=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<nch) {
if (ichan>=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 */ /*writes raw data file */
@ -566,28 +33,26 @@ int fileIO::writeDataFile(string fname, double *data, double *err, double *ang,
if (nch==-1) if (nch==-1)
nch=getTotalNumberOfChannels(); 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){ int fileIO::writeDataFile(ofstream &outfile, double *data, double *err, double *ang, char dataformat, int nch, int offset){
if (nch==-1) if (nch==-1)
nch=getTotalNumberOfChannels(); 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){ 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){ 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){ 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){ 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) { 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) { 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){ 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){ 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){ 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){ int fileIO::readDataFile(ifstream &infile, short int *data, int offset){
return readDataFile(infile, data, getTotalNumberOfChannels(),offset); return fileIOStatic::readDataFile(infile, data, getTotalNumberOfChannels(),offset);
}; };

View File

@ -2,6 +2,8 @@
#define FILEIO_H #define FILEIO_H
#include "slsDetectorBase.h" #include "slsDetectorBase.h"
#include "fileIOStatic.h"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
@ -16,17 +18,21 @@ using namespace std;
@short class handling the data file I/O flags @short class handling the data file I/O flags
*/ */
class fileIO : public virtual slsDetectorBase { class fileIO : public fileIOStatic, public virtual slsDetectorBase {
public: public:
/** default constructor */ /** default constructor */
fileIO(){}; fileIO(): fileIOStatic() {};
/** virtual destructor */ /** virtual destructor */
virtual ~fileIO(){}; 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 sets the default output files path
\param s file path \param s file path
@ -67,53 +73,9 @@ class fileIO : public virtual slsDetectorBase {
int getFileIndex() {return *fileIndex;}; 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(); 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); 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: protected:

View File

@ -0,0 +1,477 @@
#ifndef FILEIO_STATIC_H
#define FILEIO_STATIC_H
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <string>
#include <sstream>
#include <queue>
#include <math.h>
#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<nch; ichan++) { \
if (ang==NULL) { \
outfile << ichan+offset << " "; \
} else { \
outfile << ang[ichan] << " "; \
} \
switch (dataformat) { \
case 'f': \
outfile << *(data+ichan)<< " "; \
break; \
case 'i': \
default: \
idata=(int)(*(data+ichan)); \
outfile << idata << " "; \
} \
if (err) { \
outfile << *(err+ichan)<< " "; \
} \
outfile << std::endl; \
} \
return slsDetectorDefs::OK; \
};
/**
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){ \
ofstream outfile; \
if (data==NULL) return slsDetectorDefs::FAIL; \
outfile.open (fname.c_str(),ios_base::out); \
if (outfile.is_open()) { \
writeDataFile(outfile, nch, data, 0); \
outfile.close(); \
return slsDetectorDefs::OK; \
} else { \
std::cout<< "Could not open file " << fname << "for writing"<< std::endl; \
return slsDetectorDefs::FAIL; \
} \
};
/**
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){ \
if (data==NULL) return slsDetectorDefs::FAIL; \
for (int ichan=0; ichan<nch; ichan++) outfile << ichan+offset << " " << *(data+ichan) << std::endl; \
return slsDetectorDefs::OK; \
};
/**
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) { \
ofstream outfile; \
if (data==NULL) return slsDetectorDefs::FAIL; \
outfile.open (fname.c_str(),ios_base::out); \
if (outfile.is_open()) { \
writeDataFile(outfile, nch, data, 0); \
outfile.close(); \
return slsDetectorDefs::OK; \
} else { \
std::cout<< "Could not open file " << fname << "for writing"<< std::endl; \
return slsDetectorDefs::FAIL; \
} \
};
/**
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){ \
if (data==NULL) return slsDetectorDefs::FAIL; \
for (int ichan=0; ichan<nch; ichan++) outfile << ichan+offset << " " << *(data+ichan) << std::endl; \
return slsDetectorDefs::OK; \
};
/**
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') { \
ifstream infile; \
int iline=0; \
int maxchans; \
string str; \
maxchans=nch; \
infile.open(fname.c_str(), ios_base::in); \
if (infile.is_open()) { \
iline=readDataFile(nch, infile, data, err, ang, dataformat, 0); \
infile.close(); \
} else { \
std::cout<< "Could not read file " << fname << std::endl; \
return -1; \
} \
return iline; \
};
/**
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){ \
int ichan,iline=0; \
int interrupt=0; \
double fdata, ferr, fang; \
int maxchans; \
int ich; \
string str; \
maxchans=nch; \
while (infile.good() and interrupt==0) { \
getline(infile,str); \
istringstream ssstr(str); \
if (ang==NULL) { \
ssstr >> ichan >> fdata; \
if (ssstr.fail() || ssstr.bad()) { \
interrupt=1; \
break; \
} \
ich=iline; \
if (ichan<offset) \
continue; \
} else { \
ssstr >> 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<maxchans) { \
if (ang) { \
ang[ich]=fang; \
} \
data[ich]=fdata; \
if (err) \
err[ich]=ferr; \
iline++; \
} \
if (iline>=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<nch) { \
if (ichan>=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<nch) { \
if (ichan>=offset) { \
data[iline]=idata; \
iline++; \
} \
} else { \
interrupt=1; \
break; \
} \
return iline; \
}; \
};
};
#endif