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.4 2000/12/21 03:40:58 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 ()
64 m_vecStrDescriptions.clear();
68 PlotFile::setCurveSize (int nCols, int nRecords)
70 m_iNumColumns = nCols;
71 m_iNumRecords = nRecords;
73 m_vecCurves.reserve (m_iNumColumns * m_iNumRecords);
77 // a Column's records are stored sequentially. It begins at iCol * m_iNumRecords
\r
80 PlotFile::addColumn (int iCol, const double* const pdColData)
82 if (iCol < 0 || iCol >= m_iNumColumns) {
83 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
87 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
88 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
94 PlotFile::addColumn (int iCol, const float* const pdColData)
\r
96 if (iCol < 0 || iCol >= m_iNumColumns) {
\r
97 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
\r
101 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
102 m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
\r
108 PlotFile::getColumn (int iCol, double* pdColData) const
\r
110 if (iCol < 0 || iCol >= m_iNumColumns) {
\r
111 sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
\r
115 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
116 pdColData[iRec] = m_vecCurves[ iRec + (iCol * m_iNumRecords) ];
\r
121 PlotFile::getMinMax (int iStartingCol, double& dMin, double& dMax) const
\r
123 if (iStartingCol >= m_iNumColumns) {
\r
124 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
\r
128 int iOffset = iStartingCol * m_iNumRecords;
\r
129 dMin = m_vecCurves[ 0 + iOffset ];
\r
132 for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) {
\r
133 int iOffset = iCol * m_iNumRecords;
\r
134 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
\r
135 double dVal = m_vecCurves[ iRec + iOffset ];
\r
138 else if (dVal > dMax)
\r
147 PlotFile::statistics (int iStartingCol, double& min, double& max, double& mean, double& mode, double& median, double &stddev) const
\r
149 if (iStartingCol >= m_iNumColumns) {
\r
150 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
\r
154 int iOffset = iStartingCol * m_iNumRecords;
\r
155 int iNPoints = (m_iNumColumns - iStartingCol) * m_iNumRecords;
\r
156 std::vector<double> vec;
\r
157 vec.resize (iNPoints);
\r
160 for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) {
\r
161 int iOffset = iCol * m_iNumRecords;
\r
162 for (int iRec = 0; iRec < m_iNumRecords; iRec++)
\r
163 vec[iVec++] = m_vecCurves[ iRec + iOffset ];
\r
166 vectorNumericStatistics (vec, iNPoints, min, max, mean, mode, median, stddev);
\r
172 PlotFile::fileWrite (const char* const filename)
174 m_strFilename = filename;
176 fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc);
178 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str());
182 if (! headerWrite(fs) || ! columnsWrite (fs))
189 PlotFile::fileRead (const char* const filename)
191 m_strFilename = filename;
194 fstream fs (m_strFilename.c_str(), std::ios::in);
196 fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate);
200 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str());
204 if (! headerRead(fs))
207 setCurveSize (m_iNumColumns, m_iNumRecords);
209 if (! columnsRead(fs))
216 PlotFile::headerRead (std::iostream& fs)
219 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
226 bool bInHeaders = true;
227 // while (bInHeaders) {
235 PlotFile::headerWrite (std::iostream& fs)
238 sys_error (ERR_WARNING, "Tried to write header with ! fs");
243 fs << "<plotfile>\n";
\r
246 if (! m_strDate.empty())
247 fs << "<date>" << m_strDate << "</date>\n";
249 if (! m_strTitle.empty())
250 fs << "<title>" << m_strTitle << "</title>\n";
252 if (! m_strXLabel.empty())
253 fs << "<xlabel>" << m_strXLabel << "</xlabel>\n";
255 if (! m_strYLabel.empty())
256 fs << "<ylabel>" << m_strYLabel << "</ylabel>\n";
258 int iNDesc = m_vecStrDescriptions.size();
259 for (int i = 0; i < iNDesc; i++)
260 fs << "<description>" << m_vecStrDescriptions[i] << "</description>\n";
269 PlotFile::columnsWrite (std::iostream& fs)
272 sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
276 fs << "<datapoints>\n";
278 int iStride = m_iNumRecords;
279 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
280 for (int iCol = 0; iCol < m_iNumColumns; iCol++)
281 fs << m_vecCurves [iRec + (iCol * iStride)] << " ";
285 fs << "</datapoints>\n";
287 fs << "</plotfile>\n";
\r
294 PlotFile::columnsRead (std::iostream& fs)
297 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
303 if (m_iNumColumns == 0 || m_iNumRecords == 0) {
304 sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
313 PlotFile::printHeaders (std::ostream& os) const