r307: additions for plotfile
[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.2 2000/12/20 14:39:09 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
121 PlotFile::fileWrite (const char* const filename)
122 {
123     m_strFilename = filename;
124
125     fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc);
126     if (fs.fail()) {
127         sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str());
128       return false;
129     }
130
131     if (! headerWrite(fs) || ! columnsWrite (fs))
132         return false;
133     
134     return true;
135 }
136
137 bool
138 PlotFile::fileRead (const char* const filename)
139 {
140     m_strFilename = filename;
141
142 #ifdef MSVC
143     fstream fs (m_strFilename.c_str(), std::ios::in);
144 #else
145     fstream fs (m_strFilename.c_str(), std::ios::in | std::ios::nocreate);
146 #endif
147
148     if (fs.fail()) {
149       sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str());
150       return false;
151     }
152
153     if (! headerRead(fs))
154       return false;
155     
156     setCurveSize (m_iNumColumns, m_iNumRecords);
157     
158     if (! columnsRead(fs))
159       return false;;
160     
161     return true;
162 }
163
164 bool
165 PlotFile::headerRead (std::iostream& fs)
166 {
167   if (! fs) {
168     sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
169     return false;
170   }
171
172   fs.seekg (0);
173
174   initHeaders();
175   bool bInHeaders = true;
176 //  while (bInHeaders) {
177   //}
178
179   return ! fs.fail();
180 }
181
182
183 bool
184 PlotFile::headerWrite (std::iostream& fs)
185 {
186   if (! fs) {
187     sys_error (ERR_WARNING, "Tried to write header with ! fs");
188     return false;
189   }
190
191   fs.seekp (0);
192   fs << "<plotfile>\n";\r
193   fs << "<header>\n";
194   
195   if (! m_strDate.empty())
196     fs << "<date>" << m_strDate << "</date>\n";
197
198   if (! m_strTitle.empty())
199     fs << "<title>" << m_strTitle << "</title>\n";
200
201   if (! m_strXLabel.empty())
202     fs << "<xlabel>" << m_strXLabel << "</xlabel>\n";
203
204   if (! m_strYLabel.empty())
205     fs << "<ylabel>" << m_strYLabel << "</ylabel>\n";
206
207   int iNDesc = m_vecStrDescriptions.size();
208   for (int i = 0; i < iNDesc; i++)
209     fs << "<description>" << m_vecStrDescriptions[i] << "</description>\n";
210
211   fs << "</header>\n";
212
213   return ! fs.fail();
214 }
215
216
217 bool
218 PlotFile::columnsWrite (std::iostream& fs)
219 {
220   if (! fs) {
221     sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
222     return false;
223   }
224
225   fs << "<datapoints>\n";
226
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)] << " ";
231     fs << "\n";
232   }
233
234   fs << "</datapoints>\n";
235 \r
236   fs << "</plotfile>\n";\r
237
238   return ! fs.fail();
239 }
240
241
242 bool
243 PlotFile::columnsRead (std::iostream& fs)
244 {
245   if (! fs) {
246     sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
247     return false;
248   }
249
250   return ! fs.fail();
251
252   if (m_iNumColumns == 0 || m_iNumRecords == 0) {
253     sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
254     return false;
255   }
256   
257   return true;
258 }
259
260
261 void
262 PlotFile::printHeaders (std::ostream& os) const
263 {
264 }