00001
00002
00004
00005 #include "FileWriter.h"
00006 #include "GenericFileReader1D.h"
00007 #include <fstream>
00009
00011 namespace Interpolator
00012 {
00013 FileWriter::FileWriter()
00014 {
00015
00016 }
00017
00018 FileWriter::~FileWriter()
00019 {
00020
00021 }
00022
00023 FileWriter::FileWriter(const std::string& sPath,const std::string& sFileName,long lNbDatas, double* mXdata, double* mYdata)
00024 {
00025 std::string sCompleteFilePath = sPath;
00026 sCompleteFilePath+=sFileName;
00027
00028 std::ofstream fFile(sCompleteFilePath.c_str(),ios::out);
00029
00030 for (int i=0;i<lNbDatas;i++)
00031 {
00032 fFile << mXdata[i] << "\t" << mYdata[i] << endl;
00033 }
00034
00035 fFile.close();
00036 }
00037
00038 FileWriter::FileWriter(const std::string& sCompleteFilePath,long lNbDatas, double* mXdata, double* mYdata)
00039 {
00040 std::ofstream fFile(sCompleteFilePath.c_str(),ios::out);
00041
00042 for (int i=0;i<lNbDatas;i++)
00043 {
00044 fFile << mXdata[i] << "\t" << mYdata[i] << endl;
00045 }
00046
00047 fFile.close();
00048 }
00049
00050 FileWriter::FileWriter(const std::string& sPath,
00051 const std::string& sFileName,
00052 long lIndex,
00053 std::string& sExtension,
00054 long lNbDatas, double* mXdata, double* mYdata)
00055 {
00056
00057 std::string sCompleteFilePath = sPath;
00058 sCompleteFilePath+="/";
00059 sCompleteFilePath+=sFileName;
00060 sCompleteFilePath+="_";
00061 sCompleteFilePath+=ltos(lIndex);
00062 sCompleteFilePath+=".";
00063 sCompleteFilePath+=sExtension;
00064
00065 std::ofstream fFile(sCompleteFilePath.c_str(),ios::out);
00066
00067 for (int i=0;i<lNbDatas;i++)
00068 {
00069 fFile << mXdata[i] << "\t" << mYdata[i] << endl;
00070 }
00071
00072 fFile.close();
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 FileWriter::FileWriter(const std::string& sCompleteFilePath, gsl_vector* mXdata, gsl_vector* mYdata)
00082 {
00083 std::ofstream fFile(sCompleteFilePath.c_str(),ios::out);
00084 size_t lNbDatas = mXdata->size;
00085
00086 for (unsigned int i=0;i<lNbDatas;i++)
00087 {
00088 fFile << gsl_vector_get(mXdata,i) << "\t" << gsl_vector_get(mYdata,i) << endl;
00089 }
00090
00091 fFile.close();
00092 }
00093
00094 FileWriter::FileWriter(const std::string& sPath,
00095 const std::string& sFileName,
00096 long lIndex,
00097 const std::string& sExtension,
00098 gsl_vector* mXdata, gsl_vector* mYdata)
00099 {
00100 size_t lNbDatas = mXdata->size;
00101 std::string sCompleteFilePath = sPath;
00102 sCompleteFilePath+="/";
00103 sCompleteFilePath+=sFileName;
00104 sCompleteFilePath+="_";
00105 sCompleteFilePath+=ltos(lIndex);
00106 sCompleteFilePath+=".";
00107 sCompleteFilePath+=sExtension;
00108
00109 std::ofstream fFile(sCompleteFilePath.c_str(),ios::out);
00110
00111 for (unsigned int i=0;i<lNbDatas;i++)
00112 {
00113 fFile << gsl_vector_get(mXdata,i) << "\t" << gsl_vector_get(mYdata,i) << endl;
00114 }
00115
00116 fFile.close();
00117 }
00118
00119
00120
00123 FileWriter::FileWriter( const std::string& sCompleteFilePath,
00124 double dThetaTheorical,
00125 double dThetaMechanical,
00126 double dTolerance)
00127 {
00128
00129 GenericFileReader1D* mFile = new GenericFileReader1D(sCompleteFilePath);
00130
00131 long lNbData = mFile->getNbData();
00132
00133 std::ofstream fFile(sCompleteFilePath.c_str(),ios::out);
00134 fFile.precision(20);
00135
00136
00137 long lLineToModify = FindLine(mFile,dThetaTheorical,dTolerance);
00138
00139 fFile << mFile->getNbVariables() << endl;
00140
00141 bool bAddingLine = false;
00142 bool bInsertAtBeginning = false;
00143
00144 if (lLineToModify == -1)
00145 {
00146
00147 double dFirstValue = mFile->getValue(1,0);
00148 double dLastValue = mFile->getValue(1,lNbData-1);
00149
00150 if ((dThetaTheorical > dFirstValue) && (dThetaTheorical < dLastValue))
00151 {
00152
00153 bAddingLine = false;
00154 }
00155 else
00156 {
00157 if (dFirstValue >= dThetaTheorical)
00158 {
00159 bAddingLine = true;
00160 bInsertAtBeginning = true;
00161 }
00162 else
00163 {
00164 bAddingLine = true;
00165 bInsertAtBeginning = false;
00166 }
00167 }
00168 }
00169
00170 double dOffset = dThetaMechanical - dThetaTheorical;
00171
00172
00173 if (bAddingLine)
00174 fFile << (lNbData+1) << endl;
00175 else
00176 fFile << lNbData << endl;
00177
00178 int i,j;
00179 for (i=0;i<mFile->getNbVariables();i++)
00180 {
00181 fFile << mFile->getColumnName(i) << "\t\t";
00182 }
00183 fFile << endl;
00184
00185 if (bAddingLine && bInsertAtBeginning)
00186 {
00187 fFile << RadiansToDegres(dThetaTheorical) << "\t\t";
00188 fFile << dThetaTheorical << "\t\t";
00189 fFile << dOffset << "\t\t";
00190 fFile << dThetaMechanical << "\n";
00191 }
00192
00193 for (i=0;i<mFile->getNbData();i++)
00194 {
00195 if (i == lLineToModify)
00196 {
00197 fFile << RadiansToDegres(dThetaTheorical) << "\t\t";
00198 fFile << dThetaTheorical << "\t\t";
00199 fFile << dOffset << "\t\t";
00200 fFile << dThetaMechanical << "\n";
00201 }
00202 else
00203 {
00204 for (j=0;j<mFile->getNbVariables();j++)
00205 {
00206 fFile << mFile->getValue(j,i) << "\t\t";
00207 }
00208 fFile << endl;
00209 }
00210 }
00211
00212 if ((bAddingLine) && (!bInsertAtBeginning))
00213 {
00214
00215
00216
00217 fFile << RadiansToDegres(dThetaTheorical) << "\t\t";
00218 fFile << dThetaTheorical << "\t\t";
00219 fFile << dOffset << "\t\t";
00220 fFile << dThetaMechanical << "\n";
00221 }
00222
00223 fFile.close();
00224 }
00225
00226
00227 long FileWriter::FindLine(GenericFileReader1D* mFile,double dThetaTheorical,double dTolerance)
00228 {
00229 double* mTheoricalThetaColumn = mFile->getColumn(1);
00230 long lNbData = mFile->getNbData();
00231
00232 double dRangeMin = dThetaTheorical - dTolerance;
00233 double dRangeMax = dThetaTheorical + dTolerance;
00234
00235 int i;
00236
00237 for (i=0;i<lNbData;i++)
00238 {
00239 double dValueInColumn = mTheoricalThetaColumn[i];
00240
00241 if (dRangeMin <= dValueInColumn && dValueInColumn <= dRangeMax)
00242 {
00243 return i;
00244 }
00245 }
00246 return -1;
00247 }
00248
00249
00250 }
00251
00252
00253