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