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.2 2000/12/20 14:39:09 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::fileWrite (const char* const filename)
123 m_strFilename = filename;
125 fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc);
127 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str());
131 if (! headerWrite(fs) || ! columnsWrite (fs))
138 PlotFile::fileRead (const char* const filename)
140 m_strFilename = filename;
143 fstream fs (m_strFilename.c_str(), std::ios::in);
145 fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate);
149 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str());
153 if (! headerRead(fs))
156 setCurveSize (m_iNumColumns, m_iNumRecords);
158 if (! columnsRead(fs))
165 PlotFile::headerRead (std::iostream& fs)
168 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
175 bool bInHeaders = true;
176 // while (bInHeaders) {
184 PlotFile::headerWrite (std::iostream& fs)
187 sys_error (ERR_WARNING, "Tried to write header with ! fs");
192 fs << "<plotfile>\n";
\r
195 if (! m_strDate.empty())
196 fs << "<date>" << m_strDate << "</date>\n";
198 if (! m_strTitle.empty())
199 fs << "<title>" << m_strTitle << "</title>\n";
201 if (! m_strXLabel.empty())
202 fs << "<xlabel>" << m_strXLabel << "</xlabel>\n";
204 if (! m_strYLabel.empty())
205 fs << "<ylabel>" << m_strYLabel << "</ylabel>\n";
207 int iNDesc = m_vecStrDescriptions.size();
208 for (int i = 0; i < iNDesc; i++)
209 fs << "<description>" << m_vecStrDescriptions[i] << "</description>\n";
218 PlotFile::columnsWrite (std::iostream& fs)
221 sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
225 fs << "<datapoints>\n";
227 int iStride = m_iNumRecords;
228 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
229 for (int iCol = 0; iCol < m_iNumColumns; iCol++)
230 fs << m_vecCurves [iRec + (iCol * iStride)] << " ";
234 fs << "</datapoints>\n";
236 fs << "</plotfile>\n";
\r
243 PlotFile::columnsRead (std::iostream& fs)
246 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
252 if (m_iNumColumns == 0 || m_iNumRecords == 0) {
253 sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
262 PlotFile::printHeaders (std::ostream& os) const