1 /*****************************************************************************
5 ** Purpose: plotfile class
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: Dec 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: plotfile.cpp,v 1.8 2001/01/02 05:34:57 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
32 ///////////////////////////////////////////////////////////////////////////
33 // CLASS IMPLEMENTATION
36 // Purpose: Plot File storage
37 ///////////////////////////////////////////////////////////////////////////
40 PlotFile::PlotFile (int nCurves, int nRecords)
43 setCurveSize (nCurves, nRecords);
\r
51 PlotFile::~PlotFile ()
56 PlotFile::initHeaders ()
60 time_t currentTime = time (NULL);
\r
61 m_strDate = ctime (¤tTime);
\r
62 m_vecStrDescriptions.clear();
\r
63 m_vecStrEzsetCommands.clear();
67 PlotFile::setCurveSize (int nCols, int nRecords)
69 m_iNumColumns = nCols;
70 m_iNumRecords = nRecords;
72 m_vecCurves.reserve (m_iNumColumns * m_iNumRecords);
76 // a Column's records are stored sequentially. It begins at iCol * m_iNumRecords
\r
79 PlotFile::addColumn (int iCol, const double* const pdColData)
81 if (iCol < 0 || iCol >= m_iNumColumns) {
82 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
86 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
87 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
93 PlotFile::addColumn (int iCol, const float* const pdColData)
\r
95 if (iCol < 0 || iCol >= m_iNumColumns) {
\r
96 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
\r
100 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
101 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
\r
107 PlotFile::getColumn (int iCol, double* pdColData) const
\r
109 if (iCol < 0 || iCol >= m_iNumColumns) {
\r
110 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
\r
114 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
115 pdColData[iRec] = m_vecCurves[ iRec + (iCol * m_iNumRecords) ];
\r
120 PlotFile::getMinMax (int iStartingCol, double& dMin, double& dMax) const
\r
122 if (iStartingCol >= m_iNumColumns) {
\r
123 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
\r
127 int iOffset = iStartingCol * m_iNumRecords;
\r
128 dMin = m_vecCurves[ 0 + iOffset ];
\r
131 for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) {
\r
132 int iOffset = iCol * m_iNumRecords;
\r
133 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
\r
134 double dVal = m_vecCurves[ iRec + iOffset ];
\r
137 else if (dVal > dMax)
\r
146 PlotFile::statistics (int iStartingCol, double& min, double& max, double& mean, double& mode, double& median, double &stddev) const
\r
148 if (iStartingCol >= m_iNumColumns) {
\r
149 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
\r
153 int iNPoints = (m_iNumColumns - iStartingCol) * m_iNumRecords;
\r
154 std::vector<double> vec;
\r
155 vec.resize (iNPoints);
\r
158 for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) {
\r
159 int iOffset = iCol * m_iNumRecords;
\r
160 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
161 vec[iVec++] = m_vecCurves[ iRec + iOffset ];
\r
164 vectorNumericStatistics (vec, iNPoints, min, max, mean, mode, median, stddev);
\r
170 PlotFile::fileWrite (const char* const filename)
172 m_strFilename = filename;
174 fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc);
176 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str());
180 if (! headerWrite(fs) || ! columnsWrite (fs))
187 PlotFile::fileRead (const char* const filename)
189 m_strFilename = filename;
192 fstream fs (m_strFilename.c_str(), std::ios::in);
194 fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate);
198 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str());
202 if (! headerRead(fs))
205 setCurveSize (m_iNumColumns, m_iNumRecords);
207 if (! columnsRead(fs))
214 PlotFile::headerRead (std::iostream& fs)
217 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
223 bool bFinishedHeaders = false;
\r
225 fs >> m_iNumColumns;
\r
226 fs >> m_iNumRecords;
\r
228 if (fs.fail() || m_iNumColumns == 0 || m_iNumRecords == 0)
\r
231 while (! bFinishedHeaders && ! fs.eof() && ! fs.fail()) {
\r
233 fs.getline (line, sizeof(line));
\r
235 while (line[iSP] == ' ')
\r
237 if (line[iSP] == '\n' || ! line[iSP])
\r
239 else if (line[iSP] == '#') {
\r
241 while (line[iSP] == ' ')
\r
243 if (line[iSP] == '\n' || ! line[iSP])
\r
246 addDescription (&line[iSP]);
\r
247 } else if (strstr (&line[iSP], "<datapoints>") != NULL) {
\r
248 bFinishedHeaders = true;
\r
250 addEzsetCommand (&line[iSP]);
\r
258 PlotFile::headerWrite (std::iostream& fs)
261 sys_error (ERR_WARNING, "Tried to write header with ! fs");
266 fs << m_iNumColumns << " " << m_iNumRecords << "\n";
\r
269 for (i = 0; i < m_vecStrEzsetCommands.size(); i++)
\r
270 fs << m_vecStrEzsetCommands[i] << "\n";
\r
272 for (i = 0; i < m_vecStrDescriptions.size(); i++)
\r
273 fs << "# " << m_vecStrDescriptions[i] << "\n";
\r
275 if (! m_strDate.empty())
276 fs << "# Date: " << m_strDate << "\n";
283 PlotFile::columnsWrite (std::iostream& fs)
286 sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
290 fs << "<datapoints>\n";
292 int iStride = m_iNumRecords;
293 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
294 for (int iCol = 0; iCol < m_iNumColumns; iCol++)
295 fs << m_vecCurves [iRec + (iCol * iStride)] << " ";
299 fs << "</datapoints>\n";
301 fs << "</plotfile>\n";
\r
308 PlotFile::columnsRead (std::iostream& fs)
311 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
315 if (m_iNumColumns == 0 || m_iNumRecords == 0) {
316 sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
320 bool bTerminateEarly = false;
\r
321 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
\r
322 for (int iCol = 0; iCol < m_iNumColumns; iCol++) {
\r
324 bTerminateEarly = true;
\r
331 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = d;
\r
335 return ! (bTerminateEarly || fs.fail());
340 PlotFile::printHeaders (std::ostream& os) const
342 os << "EZSet Commands\n";
\r
343 for (unsigned int iEZ = 0; iEZ < m_vecStrEzsetCommands.size(); iEZ++)
\r
344 os << m_vecStrEzsetCommands[iEZ] << "\n";
\r
346 os << "Descriptions\n";
\r
347 for (unsigned int iDesc = 0; iDesc < m_vecStrDescriptions.size(); iDesc++)
\r
348 os << m_vecStrDescriptions[iDesc] << "\n";