File: plotfile.cpp

package info (click to toggle)
ctsim 6.0.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,868 kB
  • sloc: cpp: 26,967; sh: 7,782; ansic: 1,256; perl: 296; makefile: 148
file content (393 lines) | stat: -rw-r--r-- 9,834 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*****************************************************************************
** FILE IDENTIFICATION
**
**      Name:         plotfile.cpp
**      Purpose:      plotfile class
**      Programmer:   Kevin Rosenberg
**      Date Started: Dec 2000
**
**  This is part of the CTSim program
**  Copyright (c) 1983-2009 Kevin Rosenberg
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License (version 2) as
**  published by the Free Software Foundation.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

#include "ct.h"
#include <ctime>


///////////////////////////////////////////////////////////////////////////
// CLASS IMPLEMENTATION
//
//     Name: PlotFile
//     Purpose: Plot File storage
///////////////////////////////////////////////////////////////////////////


PlotFile::PlotFile (int nCurves, int nRecords)
{
  initHeaders ();
  setCurveSize (nCurves, nRecords);
}

PlotFile::PlotFile ()
{
  initHeaders ();
}

PlotFile::~PlotFile ()
{
}

void
PlotFile::initHeaders ()
{
  m_iNumColumns = 0;
  m_iNumRecords = 0;
  time_t currentTime = time (NULL);
  m_strDate = ctime (&currentTime);
  m_vecStrDescriptions.clear();
  m_vecStrEzsetCommands.clear();
}

void
PlotFile::setCurveSize (int nCols, int nRecords, bool bScatterPlot)
{
  m_iNumColumns = nCols;
  m_iNumRecords = nRecords;
  m_bScatterPlot = bScatterPlot;
  m_vecCurves.clear();
  m_vecCurves.reserve (m_iNumColumns * m_iNumRecords);
}

// Storage format
//   a Column's records are stored sequentially. It begins at iCol * m_iNumRecords
bool
PlotFile::addColumn (int iCol, const double* const pdColData)
{
  if (iCol < 0 || iCol >= m_iNumColumns) {
    sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
    return (false);
  }

  for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
    m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];
#if 0
    sys_error (ERR_TRACE, "Storing m_vecCurves[%d] = %f",
               iRec + (iCol * m_iNumRecords),
               pdColData [iRec]);
#endif
  }
  return true;
}

bool
PlotFile::addColumn (int iCol, const float* const pdColData)
{
  if (iCol < 0 || iCol >= m_iNumColumns) {
    sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
    return (false);
  }

  for (int iRec = 0; iRec < m_iNumRecords; iRec++)
    m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = pdColData [iRec];

  return true;
}

void
PlotFile::getColumn (int iCol, double* pdColData) const
{
  if (iCol < 0 || iCol >= m_iNumColumns) {
    sys_error (ERR_SEVERE, "Illegal column number %d [PlotFile::addColumn]", iCol);
    return;
  }

  for (int iRec = 0; iRec < m_iNumRecords; iRec++)
    pdColData[iRec] = m_vecCurves[ iRec + (iCol * m_iNumRecords) ];

}

bool
PlotFile::getMinMax (int iStartingCol, double& dMin, double& dMax) const
{
  if (iStartingCol >= m_iNumColumns) {
    sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
    return false;
  }

  int iOffset = iStartingCol * m_iNumRecords;
  dMin = m_vecCurves[ 0 + iOffset ];
  dMax = dMin;

  for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) {
    int iOffset = iCol * m_iNumRecords;
    for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
      double dVal = m_vecCurves[ iRec + iOffset ];
      if (dVal < dMin)
        dMin = dVal;
      else if (dVal > dMax)
        dMax = dVal;
    }
  }

  //  sys_error (ERR_TRACE, "dMin=%f, dMax=%f", dMin, dMax);
  return true;
}

bool
PlotFile::statistics (int iStartingCol, double& min, double& max, double& mean, double& mode, double& median, double &stddev) const
{
  if (iStartingCol >= m_iNumColumns) {
    sys_error (ERR_WARNING, "iStartingCol >= iNumColumns");
    return false;
  }

  int iNPoints = (m_iNumColumns - iStartingCol) * m_iNumRecords;
  std::vector<double> vec;
  vec.resize (iNPoints);

  int iVec = 0;
  for (int iCol = iStartingCol; iCol < m_iNumColumns; iCol++) {
    int iOffset = iCol * m_iNumRecords;
    for (int iRec = 0; iRec < m_iNumRecords; iRec++)
      vec[iVec++] = m_vecCurves[ iRec + iOffset ];
  }

  vectorNumericStatistics (vec, iNPoints, min, max, mean, mode, median, stddev);

  return true;
}

bool
PlotFile::fileWrite (const char* const filename)
{
  m_strFilename = filename;

  fstream fs (m_strFilename.c_str(), std::ios::out | std::ios::trunc);
  if (fs.fail()) {
    sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_strFilename.c_str());
    return false;
  }

  if (! headerWrite(fs) || ! columnsWrite (fs))
    return false;

  return true;
}

bool
PlotFile::fileRead (const char* const filename)
{
  m_strFilename = filename;

#ifdef MSVC
  fstream fs (m_strFilename.c_str(), std::ios::in);
#else
  fstream fs (m_strFilename.c_str(), std::ios::in); // | std::ios::nocreate);
#endif

  if (fs.fail()) {
    sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_strFilename.c_str());
    return false;
  }

  if (! headerRead(fs))
    return false;

  setCurveSize (m_iNumColumns, m_iNumRecords);

  if (! columnsRead(fs))
    return false;;

  return true;
}

bool
PlotFile::headerRead (std::iostream& fs)
{
  if (! fs) {
    sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
    return false;
  }

  initHeaders();
  fs.seekg (0);
  bool bFinishedHeaders = false;

  fs >> m_iNumColumns;
  fs >> m_iNumRecords;

  if (fs.fail() || m_iNumColumns == 0 || m_iNumRecords == 0)
    return false;

  while (! bFinishedHeaders && ! fs.eof() && ! fs.fail()) {
    char line[1024];
    fs.getline (line, sizeof(line));
    int iSP = 0;
    while (line[iSP] == ' ')
      iSP++;
    if (line[iSP] == '\n' || ! line[iSP])
      ;
    else if (line[iSP] == '#') {
      iSP++;
      while (line[iSP] == ' ')
        iSP++;
      if (line[iSP] == '\n' || ! line[iSP])
        ;
      else
        addDescription (&line[iSP]);
    } else if (strstr (&line[iSP], "<datapoints>") != NULL) {
         bFinishedHeaders = true;
    } else
      addEzsetCommand (&line[iSP]);
  }

  return ! fs.fail();
}


bool
PlotFile::headerWrite (std::iostream& fs)
{
  if (! fs) {
    sys_error (ERR_WARNING, "Tried to write header with ! fs");
    return false;
  }

  fs.seekp (0);
  fs << m_iNumColumns << " " << m_iNumRecords << "\n";

  unsigned int i;
  for (i = 0; i < m_vecStrEzsetCommands.size(); i++)
      fs << m_vecStrEzsetCommands[i] << "\n";

  for (i = 0; i < m_vecStrDescriptions.size(); i++)
      fs << "# " << m_vecStrDescriptions[i] << "\n";

  if (! m_strDate.empty())
    fs << "# Date: " << m_strDate << "\n";

  return ! fs.fail();
}


bool
PlotFile::columnsWrite (std::iostream& fs)
{
  if (! fs) {
    sys_error (ERR_WARNING, "Tried to columnWrite with !fs");
    return false;
  }

  fs << "<datapoints>\n";

  int iStride = m_iNumRecords;
  for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
    for (int iCol = 0; iCol < m_iNumColumns; iCol++)
      fs << m_vecCurves [iRec + (iCol * iStride)] << " ";
    fs << "\n";
  }

  fs << "</datapoints>\n";

  fs << "</plotfile>\n";

  return ! fs.fail();
}


bool
PlotFile::columnsRead (std::iostream& fs)
{
  if (! fs) {
    sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
    return false;
  }

  if (m_iNumColumns == 0 || m_iNumRecords == 0) {
    sys_error (ERR_WARNING, "Called PlotFile::columnsRead with 0 columns or records");
    return false;
  }

  bool bTerminateEarly = false;
  for (int iRec = 0; iRec < m_iNumRecords; iRec++) {
    for (int iCol = 0; iCol < m_iNumColumns; iCol++) {
      if (fs.eof()) {
        bTerminateEarly = true;
        break;
      }
      if (fs.fail())
        break;
      double d;
      fs >> d;
      m_vecCurves[ iRec + (iCol * m_iNumRecords) ] = d;
    }
  }

  return ! (bTerminateEarly || fs.fail());
}


void
PlotFile::printHeaders (std::ostream& os) const
{
  os << "EZSet Commands\n";
  for (unsigned int iEZ = 0; iEZ < m_vecStrEzsetCommands.size(); iEZ++)
    os << m_vecStrEzsetCommands[iEZ] << "\n";

  os << "Descriptions\n";
  for (unsigned int iDesc = 0; iDesc < m_vecStrDescriptions.size(); iDesc++)
    os << m_vecStrDescriptions[iDesc] << "\n";
}

void
PlotFile::printHeaders (std::ostringstream& os) const
{
  os << "EZSet Commands\n";
  for (unsigned int iEZ = 0; iEZ < m_vecStrEzsetCommands.size(); iEZ++)
    os << m_vecStrEzsetCommands[iEZ] << "\n";

  os << "Descriptions\n";
  for (unsigned int iDesc = 0; iDesc < m_vecStrDescriptions.size(); iDesc++)
    os << m_vecStrDescriptions[iDesc] << "\n";
}

void
PlotFile::printHeadersBrief (std::ostream& os) const
{
  os << "EZSet Commands\n";
  for (unsigned int iEZ = 0; iEZ < m_vecStrEzsetCommands.size(); iEZ++)
    os << m_vecStrEzsetCommands[iEZ] << "; ";
  if (m_vecStrEzsetCommands.size() > 0)
    os << "\n";

  os << "Descriptions\n";
  for (unsigned int iDesc = 0; iDesc < m_vecStrDescriptions.size(); iDesc++)
    os << m_vecStrDescriptions[iDesc] << "\n";
}

void
PlotFile::printHeadersBrief (std::ostringstream& os) const
{
  os << "EZSet Commands\n";
  for (unsigned int iEZ = 0; iEZ < m_vecStrEzsetCommands.size(); iEZ++)
    os << m_vecStrEzsetCommands[iEZ] << "; ";
  if (m_vecStrEzsetCommands.size() > 0)
    os << "\n";

  os << "Descriptions\n";
  for (unsigned int iDesc = 0; iDesc < m_vecStrDescriptions.size(); iDesc++)
    os << m_vecStrDescriptions[iDesc] << "\n";
}