X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsupport%2Fplotfile.cpp;h=ef8d26420a5dfb95aec58445d17eac3d4667c4ea;hp=b8744515cfc61ef8ac0a66e524e9dfecd1f1fed5;hb=9b2bb510160bdb56f04847f5b55ab61dd8a47976;hpb=739e435359d44546dd812fff8c86b815a214d587 diff --git a/libctsupport/plotfile.cpp b/libctsupport/plotfile.cpp index b874451..ef8d264 100644 --- a/libctsupport/plotfile.cpp +++ b/libctsupport/plotfile.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: plotfile.cpp,v 1.2 2000/12/20 14:39:09 kevin Exp $ +** $Id: plotfile.cpp,v 1.8 2001/01/02 05:34:57 kevin Exp $ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -39,8 +39,8 @@ PlotFile::PlotFile (int nCurves, int nRecords) { - initHeaders (); - setCurveSize (nCurves, nRecords); + initHeaders (); + setCurveSize (nCurves, nRecords); } PlotFile::PlotFile () @@ -55,22 +55,21 @@ PlotFile::~PlotFile () void PlotFile::initHeaders () { - m_iNumColumns = 0; - m_iNumRecords = 0; - m_strTitle = ""; - m_strXLabel = ""; - m_strYLabel = ""; - m_strDate = ""; - m_vecStrDescriptions.clear(); + m_iNumColumns = 0; + m_iNumRecords = 0; + time_t currentTime = time (NULL); + m_strDate = ctime (¤tTime); + m_vecStrDescriptions.clear(); + m_vecStrEzsetCommands.clear(); } void PlotFile::setCurveSize (int nCols, int nRecords) { - m_iNumColumns = nCols; - m_iNumRecords = nRecords; - m_vecCurves.clear(); - m_vecCurves.reserve (m_iNumColumns * m_iNumRecords); + m_iNumColumns = nCols; + m_iNumRecords = nRecords; + m_vecCurves.clear(); + m_vecCurves.reserve (m_iNumColumns * m_iNumRecords); } // Storage format @@ -83,10 +82,10 @@ PlotFile::addColumn (int iCol, const double* const pdColData) sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol); return (false); } - + for (int iRec = 0; iRec < m_iNumRecords; iRec++) m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec]; - + return true; } @@ -97,10 +96,10 @@ PlotFile::addColumn (int iCol, const float* const pdColData) sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol); return (false); } - + for (int iRec = 0; iRec < m_iNumRecords; iRec++) m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec]; - + return true; } @@ -111,54 +110,104 @@ PlotFile::getColumn (int iCol, double* pdColData) const sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol); return; } - + for (int iRec = 0; iRec < m_iNumRecords; iRec++) pdColData[iRec] = m_vecCurves[ iRec + (iCol * m_iNumRecords) ]; + +} + +bool +PlotFile::getMinMax (int iStartingCol, double& dMin, double& dMax) const +{ + if (iStartingCol >= m_iNumColumns) { + sys_error (ERR_WARNING, "iStartingCol >= iNumColumns"); + return false; + } + + int iOffset = iStartingCol * m_iNumRecords; + dMin = m_vecCurves[ 0 + iOffset ]; + dMax = dMin; + + for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) { + int iOffset = iCol * m_iNumRecords; + for (int iRec = 0; iRec < m_iNumRecords; iRec++) { + double dVal = m_vecCurves[ iRec + iOffset ]; + if (dVal < dMin) + dMin = dVal; + else if (dVal > dMax) + dMax = dVal; + } + } + + return true; +} +bool +PlotFile::statistics (int iStartingCol, double& min, double& max, double& mean, double& mode, double& median, double &stddev) const +{ + if (iStartingCol >= m_iNumColumns) { + sys_error (ERR_WARNING, "iStartingCol >= iNumColumns"); + return false; + } + + int iNPoints = (m_iNumColumns - iStartingCol) * m_iNumRecords; + std::vector vec; + vec.resize (iNPoints); + + int iVec = 0; + for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) { + int iOffset = iCol * m_iNumRecords; + for (int iRec = 0; iRec < m_iNumRecords; iRec++) + vec[iVec++] = m_vecCurves[ iRec + iOffset ]; + } + + vectorNumericStatistics (vec, iNPoints, min, max, mean, mode, median, stddev); + + return true; } bool PlotFile::fileWrite (const char* const filename) { - m_strFilename = filename; - - fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc); - if (fs.fail()) { - sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str()); - return false; - } - - if (! headerWrite(fs) || ! columnsWrite (fs)) - return false; - - return true; + m_strFilename = filename; + + fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc); + if (fs.fail()) { + sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str()); + return false; + } + + if (! headerWrite(fs) || ! columnsWrite (fs)) + return false; + + return true; } bool PlotFile::fileRead (const char* const filename) { - m_strFilename = filename; - + m_strFilename = filename; + #ifdef MSVC - fstream fs (m_strFilename.c_str(), std::ios::in); + fstream fs (m_strFilename.c_str(), std::ios::in); #else - fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate); + fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate); #endif - - if (fs.fail()) { - sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str()); - return false; - } - - if (! headerRead(fs)) - return false; - - setCurveSize (m_iNumColumns, m_iNumRecords); - - if (! columnsRead(fs)) - return false;; - - return true; + + if (fs.fail()) { + sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str()); + return false; + } + + if (! headerRead(fs)) + return false; + + setCurveSize (m_iNumColumns, m_iNumRecords); + + if (! columnsRead(fs)) + return false;; + + return true; } bool @@ -168,13 +217,38 @@ PlotFile::headerRead (std::iostream& fs) sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]"); return false; } - - fs.seekg (0); - - initHeaders(); - bool bInHeaders = true; -// while (bInHeaders) { - //} + + initHeaders(); + fs.seekg (0); + bool bFinishedHeaders = false; + + fs >> m_iNumColumns; + fs >> m_iNumRecords; + + if (fs.fail() || m_iNumColumns == 0 || m_iNumRecords == 0) + return false; + + while (! bFinishedHeaders && ! fs.eof() && ! fs.fail()) { + char line[1024]; + fs.getline (line, sizeof(line)); + int iSP = 0; + while (line[iSP] == ' ') + iSP++; + if (line[iSP] == '\n' || ! line[iSP]) + ; + else if (line[iSP] == '#') { + iSP++; + while (line[iSP] == ' ') + iSP++; + if (line[iSP] == '\n' || ! line[iSP]) + ; + else + addDescription (&line[iSP]); + } else if (strstr (&line[iSP], "") != NULL) { + bFinishedHeaders = true; + } else + addEzsetCommand (&line[iSP]); + } return ! fs.fail(); } @@ -187,29 +261,20 @@ PlotFile::headerWrite (std::iostream& fs) sys_error (ERR_WARNING, "Tried to write header with ! fs"); return false; } - - fs.seekp (0); - fs << "\n"; - fs << "
\n"; + fs.seekp (0); + fs << m_iNumColumns << " " << m_iNumRecords << "\n"; + + int i; + for (i = 0; i < m_vecStrEzsetCommands.size(); i++) + fs << m_vecStrEzsetCommands[i] << "\n"; + + for (i = 0; i < m_vecStrDescriptions.size(); i++) + fs << "# " << m_vecStrDescriptions[i] << "\n"; + if (! m_strDate.empty()) - fs << "" << m_strDate << "\n"; - - if (! m_strTitle.empty()) - fs << "" << m_strTitle << "\n"; - - if (! m_strXLabel.empty()) - fs << "" << m_strXLabel << "\n"; - - if (! m_strYLabel.empty()) - fs << "" << m_strYLabel << "\n"; - - int iNDesc = m_vecStrDescriptions.size(); - for (int i = 0; i < iNDesc; i++) - fs << "" << m_vecStrDescriptions[i] << "\n"; - - fs << "
\n"; - + fs << "# Date: " << m_strDate << "\n"; + return ! fs.fail(); } @@ -221,20 +286,20 @@ PlotFile::columnsWrite (std::iostream& fs) sys_error (ERR_WARNING, "Tried to columnWrite with !fs"); return false; } - + fs << "\n"; - + int iStride = m_iNumRecords; for (int iRec = 0; iRec < m_iNumRecords; iRec++) { for (int iCol = 0; iCol < m_iNumColumns; iCol++) fs << m_vecCurves [iRec + (iCol * iStride)] << " "; fs << "\n"; } - + fs << "\n"; - + fs << "
\n"; - + return ! fs.fail(); } @@ -246,19 +311,39 @@ PlotFile::columnsRead (std::iostream& fs) sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs"); return false; } - - return ! fs.fail(); - + if (m_iNumColumns == 0 || m_iNumRecords == 0) { sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records"); return false; } - - return true; + + bool bTerminateEarly = false; + for (int iRec = 0; iRec < m_iNumRecords; iRec++) { + for (int iCol = 0; iCol < m_iNumColumns; iCol++) { + if (fs.eof()) { + bTerminateEarly = true; + break; + } + if (fs.fail()) + break; + double d; + fs >> d; + m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = d; + } + } + + return ! (bTerminateEarly || fs.fail()); } void PlotFile::printHeaders (std::ostream& os) const -{ +{ + os << "EZSet Commands\n"; + for (unsigned int iEZ = 0; iEZ < m_vecStrEzsetCommands.size(); iEZ++) + os << m_vecStrEzsetCommands[iEZ] << "\n"; + + os << "Descriptions\n"; + for (unsigned int iDesc = 0; iDesc < m_vecStrDescriptions.size(); iDesc++) + os << m_vecStrDescriptions[iDesc] << "\n"; }