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.1 2000/12/19 21:37:10 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::fileWrite (const char* const filename)
110 m_strFilename = filename;
112 fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc);
114 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str());
118 if (! headerWrite(fs) || ! columnsWrite (fs))
125 PlotFile::fileRead (const char* const filename)
127 m_strFilename = filename;
130 fstream fs (m_strFilename.c_str(), std::ios::in);
132 fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate);
136 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str());
140 if (! headerRead(fs))
143 setCurveSize (m_iNumColumns, m_iNumRecords);
145 if (! columnsRead(fs))
152 PlotFile::headerRead (std::iostream& fs)
155 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
162 bool bInHeaders = true;
163 // while (bInHeaders) {
171 PlotFile::headerWrite (std::iostream& fs)
174 sys_error (ERR_WARNING, "Tried to write header with ! fs");
179 fs << "<plotfile>\n";
\r
182 if (! m_strDate.empty())
183 fs << "<date>" << m_strDate << "</date>\n";
185 if (! m_strTitle.empty())
186 fs << "<title>" << m_strTitle << "</title>\n";
188 if (! m_strXLabel.empty())
189 fs << "<xlabel>" << m_strXLabel << "</xlabel>\n";
191 if (! m_strYLabel.empty())
192 fs << "<ylabel>" << m_strYLabel << "</ylabel>\n";
194 int iNDesc = m_vecStrDescriptions.size();
195 for (int i = 0; i < iNDesc; i++)
196 fs << "<description>" << m_vecStrDescriptions[i] << "</description>\n";
205 PlotFile::columnsWrite (std::iostream& fs)
208 sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
212 fs << "<datapoints>\n";
214 int iStride = m_iNumRecords;
215 for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
216 for (int iCol = 0; iCol < m_iNumColumns; iCol++)
217 fs << m_vecCurves [iRec + (iCol * iStride)] << " ";
221 fs << "</datapoints>\n";
223 fs << "</plotfile>\n";
\r
230 PlotFile::columnsRead (std::iostream& fs)
233 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
239 if (m_iNumColumns == 0 || m_iNumRecords == 0) {
240 sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
249 PlotFile::printHeaders (std::ostream& os) const