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.5 2000/12/23 18:12:35 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);
51 PlotFile::~PlotFile ()
56 PlotFile::initHeaders ()
61 m_vecStrDescriptions.clear();
\r
62 m_vecStrEzsetCommands.clear();
66 PlotFile::setCurveSize (int nCols, int nRecords)
68 m_iNumColumns = nCols;
69 m_iNumRecords = nRecords;
71 m_vecCurves.reserve (m_iNumColumns * m_iNumRecords);
75 // a Column's records are stored sequentially. It begins at iCol * m_iNumRecords
\r
78 PlotFile::addColumn (int iCol, const double* const pdColData)
80 if (iCol < 0 || iCol >= m_iNumColumns) {
81 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
85 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
86 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
92 PlotFile::addColumn (int iCol, const float* const pdColData)
\r
94 if (iCol < 0 || iCol >= m_iNumColumns) {
\r
95 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
\r
99 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
100 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
\r
106 PlotFile::getColumn (int iCol, double* pdColData) const
\r
108 if (iCol < 0 || iCol >= m_iNumColumns) {
\r
109 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
\r
113 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
114 pdColData[iRec] = m_vecCurves[ iRec + (iCol * m_iNumRecords) ];
\r
119 PlotFile::getMinMax (int iStartingCol, double& dMin, double& dMax) const
\r
121 if (iStartingCol >= m_iNumColumns) {
\r
122 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
\r
126 int iOffset = iStartingCol * m_iNumRecords;
\r
127 dMin = m_vecCurves[ 0 + iOffset ];
\r
130 for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) {
\r
131 int iOffset = iCol * m_iNumRecords;
\r
132 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
\r
133 double dVal = m_vecCurves[ iRec + iOffset ];
\r
136 else if (dVal > dMax)
\r
145 PlotFile::statistics (int iStartingCol, double& min, double& max, double& mean, double& mode, double& median, double &stddev) const
\r
147 if (iStartingCol >= m_iNumColumns) {
\r
148 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
\r
152 int iOffset = iStartingCol * m_iNumRecords;
\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 while (! bFinishedHeaders && ! fs.eof() && ! fs.fail()) {
\r
227 fs.getline (line, sizeof(line));
\r
228 if (strstr (line, "<datapoints>") != NULL)
\r
229 bFinishedHeaders = true;
\r
237 PlotFile::headerWrite (std::iostream& fs)
240 sys_error (ERR_WARNING, "Tried to write header with ! fs");
245 fs << "<plotfile>\n";
\r
246 fs << "<ncolumns>" << m_iNumColumns << "</ncolumns>\n";
\r
247 fs << "<nrecords>" << m_iNumRecords << "</nrecords>\n";
\r
249 int iNEzset = m_vecStrEzsetCommands.size();
\r
252 for (int i = 0; i < iNEzset; i++)
\r
253 fs << m_vecStrEzsetCommands[i] << "\n";
\r
254 fs << "</ezset>\n";
\r
257 if (! m_strDate.empty())
258 fs << "<date>" << m_strDate << "</date>\n";
260 int iNDesc = m_vecStrDescriptions.size();
\r
262 fs << "<description>\n";
263 for (int i = 0; i < iNDesc; i++)
264 fs << m_vecStrDescriptions[i] << "\n";
\r
265 fs << "</description>\n";
\r
273 PlotFile::columnsWrite (std::iostream& fs)
276 sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
280 fs << "<datapoints>\n";
282 int iStride = m_iNumRecords;
283 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
284 for (int iCol = 0; iCol < m_iNumColumns; iCol++)
285 fs << m_vecCurves [iRec + (iCol * iStride)] << " ";
289 fs << "</datapoints>\n";
291 fs << "</plotfile>\n";
\r
298 PlotFile::columnsRead (std::iostream& fs)
301 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
307 if (m_iNumColumns == 0 || m_iNumRecords == 0) {
308 sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
312 bool bTerminateEarly = false;
\r
313 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
\r
314 for (int iCol = 0; iCol < m_iNumColumns; iCol++) {
\r
316 bTerminateEarly = true;
\r
323 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = d;
\r
327 return ! (bTerminateEarly || fs.fail());
332 PlotFile::printHeaders (std::ostream& os) const