r310: plotfile updates
[ctsim.git] / libctsupport / plotfile.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         plotfile.cpp
5 **      Purpose:      plotfile class
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: Dec 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: plotfile.cpp,v 1.4 2000/12/21 03:40:58 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29 #include <ctime>
30
31
32 ///////////////////////////////////////////////////////////////////////////
33 // CLASS IMPLEMENTATION
34 //
35 //     Name: PlotFile
36 //     Purpose: Plot File storage
37 ///////////////////////////////////////////////////////////////////////////
38
39
40 PlotFile::PlotFile (int nCurves, int nRecords)
41 {
42     initHeaders ();
43     setCurveSize (nCurves, nRecords);
44 }
45
46 PlotFile::PlotFile ()
47 {
48   initHeaders ();
49 }
50
51 PlotFile::~PlotFile ()
52 {
53 }
54
55 void
56 PlotFile::initHeaders ()
57 {
58     m_iNumColumns = 0;
59     m_iNumRecords = 0;
60     m_strTitle = "";
61     m_strXLabel = "";
62     m_strYLabel = "";
63     m_strDate = "";
64     m_vecStrDescriptions.clear();
65 }
66
67 void
68 PlotFile::setCurveSize (int nCols, int nRecords)
69 {
70     m_iNumColumns = nCols;
71     m_iNumRecords = nRecords;
72     m_vecCurves.clear();
73     m_vecCurves.reserve (m_iNumColumns * m_iNumRecords);
74 }
75 \r
76 // Storage format\r
77 //   a Column's records are stored sequentially. It begins at iCol * m_iNumRecords\r
78
79 bool
80 PlotFile::addColumn (int iCol, const double* const pdColData)
81 {
82   if (iCol < 0 || iCol >= m_iNumColumns) {
83     sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
84     return (false);
85   }
86
87   for (int iRec = 0; iRec < m_iNumRecords; iRec++)
88     m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
89
90   return true;
91 }
92
93 bool\r
94 PlotFile::addColumn (int iCol, const float* const pdColData)\r
95 {\r
96   if (iCol < 0 || iCol >= m_iNumColumns) {\r
97     sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);\r
98     return (false);\r
99   }\r
100 \r
101   for (int iRec = 0; iRec < m_iNumRecords; iRec++)\r
102     m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];\r
103 \r
104   return true;\r
105 }\r
106 \r
107 void\r
108 PlotFile::getColumn (int iCol, double* pdColData) const\r
109 {\r
110   if (iCol < 0 || iCol >= m_iNumColumns) {\r
111     sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);\r
112     return;\r
113   }\r
114 \r
115   for (int iRec = 0; iRec < m_iNumRecords; iRec++)\r
116     pdColData[iRec] = m_vecCurves[ iRec + (iCol * m_iNumRecords) ];\r
117 \r
118 }\r
119 \r
120 bool\r
121 PlotFile::getMinMax (int iStartingCol, double& dMin, double& dMax) const\r
122 {\r
123         if (iStartingCol >= m_iNumColumns) {\r
124                 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");\r
125                 return false;\r
126         }\r
127 \r
128         int iOffset = iStartingCol * m_iNumRecords;\r
129         dMin = m_vecCurves[ 0 + iOffset ];\r
130         dMax = dMin;\r
131 \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
136                         if (dVal < dMin)\r
137                                 dMin = dVal;\r
138                         else if (dVal > dMax)\r
139                                 dMax = dVal;\r
140                 }\r
141         }\r
142 \r
143         return true;\r
144 }\r
145 \r
146 bool \r
147 PlotFile::statistics (int iStartingCol, double& min, double& max, double& mean, double& mode, double& median, double &stddev) const\r
148 {\r
149         if (iStartingCol >= m_iNumColumns) {\r
150                 sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");\r
151                 return false;\r
152         }\r
153 \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
158 \r
159         int iVec = 0;\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
164         }\r
165 \r
166         vectorNumericStatistics (vec, iNPoints, min, max, mean, mode, median, stddev);\r
167 \r
168         return true;\r
169 }\r
170 \r
171 bool
172 PlotFile::fileWrite (const char* const filename)
173 {
174     m_strFilename = filename;
175
176     fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc);
177     if (fs.fail()) {
178         sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str());
179       return false;
180     }
181
182     if (! headerWrite(fs) || ! columnsWrite (fs))
183         return false;
184     
185     return true;
186 }
187
188 bool
189 PlotFile::fileRead (const char* const filename)
190 {
191     m_strFilename = filename;
192
193 #ifdef MSVC
194     fstream fs (m_strFilename.c_str(), std::ios::in);
195 #else
196     fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate);
197 #endif
198
199     if (fs.fail()) {
200       sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str());
201       return false;
202     }
203
204     if (! headerRead(fs))
205       return false;
206     
207     setCurveSize (m_iNumColumns, m_iNumRecords);
208     
209     if (! columnsRead(fs))
210       return false;;
211     
212     return true;
213 }
214
215 bool
216 PlotFile::headerRead (std::iostream& fs)
217 {
218   if (! fs) {
219     sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
220     return false;
221   }
222
223   fs.seekg (0);
224
225   initHeaders();
226   bool bInHeaders = true;
227 //  while (bInHeaders) {
228   //}
229
230   return ! fs.fail();
231 }
232
233
234 bool
235 PlotFile::headerWrite (std::iostream& fs)
236 {
237   if (! fs) {
238     sys_error (ERR_WARNING, "Tried to write header with ! fs");
239     return false;
240   }
241
242   fs.seekp (0);
243   fs << "<plotfile>\n";\r
244   fs << "<header>\n";
245   
246   if (! m_strDate.empty())
247     fs << "<date>" << m_strDate << "</date>\n";
248
249   if (! m_strTitle.empty())
250     fs << "<title>" << m_strTitle << "</title>\n";
251
252   if (! m_strXLabel.empty())
253     fs << "<xlabel>" << m_strXLabel << "</xlabel>\n";
254
255   if (! m_strYLabel.empty())
256     fs << "<ylabel>" << m_strYLabel << "</ylabel>\n";
257
258   int iNDesc = m_vecStrDescriptions.size();
259   for (int i = 0; i < iNDesc; i++)
260     fs << "<description>" << m_vecStrDescriptions[i] << "</description>\n";
261
262   fs << "</header>\n";
263
264   return ! fs.fail();
265 }
266
267
268 bool
269 PlotFile::columnsWrite (std::iostream& fs)
270 {
271   if (! fs) {
272     sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
273     return false;
274   }
275
276   fs << "<datapoints>\n";
277
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)] << " ";
282     fs << "\n";
283   }
284
285   fs << "</datapoints>\n";
286 \r
287   fs << "</plotfile>\n";\r
288
289   return ! fs.fail();
290 }
291
292
293 bool
294 PlotFile::columnsRead (std::iostream& fs)
295 {
296   if (! fs) {
297     sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
298     return false;
299   }
300
301   return ! fs.fail();
302
303   if (m_iNumColumns == 0 || m_iNumRecords == 0) {
304     sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
305     return false;
306   }
307   
308   return true;
309 }
310
311
312 void
313 PlotFile::printHeaders (std::ostream& os) const
314 {
315 }