r261: Use explicit std:: namespace
[ctsim.git] / libctsim / array2dfile.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         array2dfile.cpp
5 **      Purpose:      2-dimension array file class
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: array2dfile.cpp,v 1.21 2000/12/16 06:12:47 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 "array2dfile.h"
29 #include <ctime>\r
30 #ifdef MSVC\r
31 typedef long off_t;\r
32 #endif
33
34
35 const kuint16 Array2dFile::m_signature = ('I'*256+'F');
36
37 ///////////////////////////////////////////////////////////////////////////
38 // CLASS IMPLEMENTATION
39 //
40 //     Name: Array2dFileLabel
41 //     Purpose: Labels for Array2dFiles
42 ///////////////////////////////////////////////////////////////////////////
43
44 void
45 Array2dFileLabel::init (void)
46 {
47     m_calcTime = 0;
48     m_labelType = L_EMPTY;
49     time_t t = time(0);
50     tm* lt = localtime(&t);
51     m_year = lt->tm_year;
52     m_month = lt->tm_mon;
53     m_day = lt->tm_mday;
54     m_hour = lt->tm_hour;
55     m_minute = lt->tm_min;
56     m_second = lt->tm_sec;
57 }
58
59 Array2dFileLabel::Array2dFileLabel() 
60 {
61     init();
62 }
63
64 Array2dFileLabel::Array2dFileLabel(const char* const str, double ctime)
65     : m_strLabel (str)
66 {
67     init();
68
69     m_labelType = L_USER;
70     m_calcTime = ctime;
71 }
72
73 Array2dFileLabel::Array2dFileLabel(const int type, const char* const str, double ctime)
74   :  m_strLabel (str)
75 {
76     init();
77
78     m_labelType = type;
79     m_calcTime = ctime;
80 }
81
82 Array2dFileLabel::~Array2dFileLabel()
83 {
84 }
85
86 void 
87 Array2dFileLabel::setDateTime (int year, int month, int day, int hour, int minute, int second)
88 {
89   m_year = year;
90   m_month = month;
91   m_day = day;
92   m_hour = hour;
93   m_minute = minute;
94   m_second = second;
95 }
96
97 void 
98 Array2dFileLabel::getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const
99 {
100   year = m_year;
101   month = m_month;
102   day = m_day;
103   hour = m_hour;
104   minute = m_minute;
105   second = m_second;
106 }
107
108 const std::string& 
109 Array2dFileLabel::getDateString (void) const
110 {
111   char szDate [128];\r
112   snprintf (szDate, sizeof(szDate), "%2d/%02d/%4d %02d:%02d:%02d",\r
113           m_month + 1, m_day, m_year + 1900, m_hour, m_minute, m_second);
114   m_strDate = szDate;
115   return m_strDate;
116 }
117
118
119 Array2dFileLabel::Array2dFileLabel (const Array2dFileLabel& rhs)
120 {
121     m_calcTime = rhs.m_calcTime;
122     m_labelType = rhs.m_labelType;
123     m_strLabel = rhs.m_strLabel;
124     m_year = rhs.m_year; m_month = rhs.m_month; m_day = rhs.m_day;
125     m_hour = rhs.m_hour; m_minute = rhs.m_minute; m_second = rhs.m_second;
126 }
127
128 Array2dFileLabel&
129 Array2dFileLabel::operator= (const Array2dFileLabel& rhs)
130 {
131     m_calcTime = rhs.m_calcTime;
132     m_labelType = rhs.m_labelType;
133     m_strLabel = rhs.m_strLabel;
134     m_year = rhs.m_year; m_month = rhs.m_month; m_day = rhs.m_day;
135     m_hour = rhs.m_hour; m_minute = rhs.m_minute; m_second = rhs.m_second;
136
137     return (*this);
138 }
139
140 void
141 Array2dFileLabel::print (std::ostream& os) const
142 {
143   if (m_labelType == L_HISTORY) {
144           os << "History: " << std::endl;
145     os << "  " << m_strLabel << std::endl;
146     os << "  calc time = " << m_calcTime << " secs" << std::endl;
147     os << "  Timestamp = " << getDateString() << std::endl;
148   } else if (m_labelType == L_USER) {
149     os << "Note: " <<  m_strLabel << std::endl;
150     os << "  Timestamp = %s" << getDateString() << std::endl;
151   } else {
152     os << "Unknown (" << m_labelType << "): " <<  m_strLabel << std::endl;
153     os << "  Timestamp = %s" << getDateString() << std::endl;
154   }
155 }
156
157
158 ///////////////////////////////////////////////////////////////////////////
159 // CLASS IMPLEMENTATION
160 //
161 //     Name: Array2dFile
162 //     Purpose: Array2dFiles
163 ///////////////////////////////////////////////////////////////////////////
164
165
166 Array2dFile::Array2dFile (int x, int y, int pixelSize, int pixelFormat)
167 {
168     init();
169     setArraySize (x, y, pixelSize, pixelFormat);
170 }
171
172 Array2dFile::~Array2dFile (void)
173 {
174     freeArray ();
175     for (labelIterator l = m_labels.begin(); l != m_labels.end(); l++)
176       delete *l;
177 }
178
179 Array2dFile::Array2dFile (void)
180 {
181     init();
182 }
183
184 void
185 Array2dFile::init (void)
186 {
187   m_pixelSize = 0;
188   m_pixelFormat = PIXEL_INVALID;
189   m_arrayData = NULL;
190   m_nx = 0;
191   m_ny = 0;
192   m_headersize = 0;
193   m_axisIncrementKnown = false;
194   m_axisIncrementX = m_axisIncrementY = 0;
195   m_axisExtentKnown = false;
196   m_minX = m_maxX = m_minY = m_maxY = 0;
197   m_offsetPV = 0;
198   m_scalePV = 1;
199 }
200
201
202 void
203 Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat)
204 {
205     m_pixelSize = pixelSize;
206     m_pixelFormat = pixelFormat;
207     setArraySize (x, y);
208 }
209
210 void
211 Array2dFile::setArraySize (int x, int y)
212 {
213     m_nx = x;
214     m_ny = y;
215     allocArray ();
216 }
217
218 void 
219 Array2dFile::allocArray (void)
220 {
221     if (m_arrayData)
222         freeArray();
223
224     m_arraySize = m_nx * m_ny * m_pixelSize;
225     m_arrayData = new unsigned char* [m_nx];
226     
227     int columnBytes = m_ny * m_pixelSize;
228     for (unsigned int i = 0; i < m_nx; i++)
229         m_arrayData[i] = new unsigned char [columnBytes];
230 }
231
232 void 
233 Array2dFile::freeArray (void)
234 {
235     if (m_arrayData) {
236         for (unsigned int i = 0; i < m_nx; i++)
237             delete m_arrayData[i];
238         delete m_arrayData;
239         m_arrayData = NULL;
240     }
241 }
242
243 bool
244 Array2dFile::fileWrite (const std::string& filename)
245 {
246   return fileWrite (filename.c_str());
247 }
248
249 bool
250 Array2dFile::fileWrite (const char* const filename)
251 {
252     m_filename = filename;
253
254     frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::trunc | std::ios::binary);
255     if (fs.fail()) {
256         sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
257       return false;
258     }
259     if (! headerWrite(fs))
260         return false;
261     
262     if (! arrayDataWrite (fs))
263         return false;
264     
265     if (! labelsWrite (fs))
266         return false;
267
268     return true;
269 }
270
271 bool
272 Array2dFile::fileRead (const std::string& filename)
273 {
274   return fileRead (filename.c_str());
275 }
276
277 bool
278 Array2dFile::fileRead (const char* const filename)
279 {
280     m_filename = filename;
281
282 #ifdef MSVC\r
283     frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary);\r
284 #else\r
285     frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary | std::ios::nocreate);\r
286 #endif\r
287     if (fs.fail()) {
288       sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
289       return false;
290     }
291
292     if (! headerRead(fs))
293       return false;
294     
295     allocArray ();
296     
297     if (! arrayDataRead(fs))
298       return false;;
299
300     if (! labelsRead (fs))
301       return false;
302     
303     return true;
304 }
305
306 void
307 Array2dFile::setAxisIncrement (double incX, double incY)
308 {
309   m_axisIncrementKnown = true;
310   m_axisIncrementX = incX;
311   m_axisIncrementY = incY;
312 }
313
314 void 
315 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
316 {
317     m_axisExtentKnown = true;
318     m_minX = minX;
319     m_maxY = maxX;
320     m_minX = minX;
321     m_maxY = maxY;
322 }
323
324 bool
325 Array2dFile::headerRead (frnetorderstream& fs)
326 {
327   if (! fs) {
328     sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
329     return false;
330   }
331
332   fs.seekg (0);
333   kuint16 file_signature;
334
335   fs.readInt16 (m_headersize);
336   fs.readInt16 (file_signature);
337   fs.readInt16 (m_pixelFormat);
338   fs.readInt16 (m_pixelSize);
339   fs.readInt16 (m_numFileLabels);
340   fs.readInt32 (m_nx);
341   fs.readInt32 (m_ny);
342   fs.readInt16 (m_axisIncrementKnown);
343   fs.readFloat64 (m_axisIncrementX);
344   fs.readFloat64 (m_axisIncrementY);
345   fs.readInt16 (m_axisExtentKnown);
346   fs.readFloat64 (m_minX);
347   fs.readFloat64 (m_maxX);
348   fs.readFloat64 (m_minY);
349   fs.readFloat64 (m_maxY);
350   fs.readFloat64 (m_offsetPV);
351   fs.readFloat64 (m_scalePV);
352
353   int read_m_headersize = fs.tellg();
354   if (read_m_headersize != m_headersize) {
355     sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
356     return false;
357   }
358   if (file_signature != m_signature) {
359     sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
360     return false;
361   }
362
363   return true;
364 }
365
366
367 bool
368 Array2dFile::headerWrite (frnetorderstream& fs)
369 {
370   if (! fs) {
371     sys_error (ERR_WARNING, "Tried to write header with ! fs");
372     return false;
373   }
374
375   m_numFileLabels = m_labels.size();
376
377   fs.seekp (0);
378   fs.writeInt16 (m_headersize);
379   fs.writeInt16 (m_signature);
380   fs.writeInt16 (m_pixelFormat);
381   fs.writeInt16 (m_pixelSize);
382   fs.writeInt16 (m_numFileLabels);
383   fs.writeInt32 (m_nx);
384   fs.writeInt32 (m_ny);
385   fs.writeInt16 (m_axisIncrementKnown);
386   fs.writeFloat64 (m_axisIncrementX);
387   fs.writeFloat64 (m_axisIncrementY);
388   fs.writeInt16 (m_axisExtentKnown);
389   fs.writeFloat64 (m_minX);
390   fs.writeFloat64 (m_maxX);
391   fs.writeFloat64 (m_minY);
392   fs.writeFloat64 (m_maxY);
393   fs.writeFloat64 (m_offsetPV);
394   fs.writeFloat64 (m_scalePV);
395
396   m_headersize = static_cast<kuint16>(fs.tellp());
397   fs.seekp (0);
398   fs.writeInt16 (m_headersize);
399   
400   return true;
401 }
402
403
404 bool
405 Array2dFile::arrayDataWrite (frnetorderstream& fs)
406 {
407   if (! fs) {
408     sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
409     return false;
410   }
411
412   if (! m_arrayData) 
413       return false;
414
415   fs.seekp (m_headersize);
416   int columnSize = m_ny * m_pixelSize;
417   for (unsigned int ix = 0; ix < m_nx; ix++) {
418       unsigned char* ptrColumn = m_arrayData[ix];
419       if (NativeBigEndian()) {
420           for (unsigned int iy = 0; iy < m_ny; iy++) {
421               ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
422               fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
423               ptrColumn += m_pixelSize;
424           }
425       } else 
426           fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
427   }
428
429   return true;
430 }
431
432
433 bool
434 Array2dFile::arrayDataRead (frnetorderstream& fs)
435 {
436   if (! fs) {
437     sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
438     return false;
439   }
440
441   if (! m_arrayData)
442       return false;
443
444   fs.seekg (m_headersize);
445   int columnSize = m_ny * m_pixelSize;
446   for (unsigned int ix = 0; ix < m_nx; ix++) {
447       unsigned char* ptrColumn = m_arrayData[ix];
448       if (NativeBigEndian()) {
449           for (unsigned int iy = 0; iy < m_ny; iy++) {
450               fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
451               ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
452               ptrColumn += m_pixelSize;
453           } 
454       } else
455           fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
456   }
457
458   return true;
459 }
460
461 bool
462 Array2dFile::labelsRead (frnetorderstream& fs)
463 {
464     off_t pos = m_headersize + m_arraySize;
465     fs.seekg (pos);
466     if (fs.fail())
467         return false;
468
469     for (int i = 0; i < m_numFileLabels; i++) {
470         kuint16 labelType, year, month, day, hour, minute, second;
471         kfloat64 calcTime;
472
473         fs.readInt16 (labelType);
474         fs.readInt16 (year);
475         fs.readInt16 (month);
476         fs.readInt16 (day);
477         fs.readInt16 (hour);
478         fs.readInt16 (minute);
479         fs.readInt16 (second);
480         fs.readFloat64 (calcTime);
481         
482         kuint16 strLength;
483         fs.readInt16 (strLength);
484         char* pszLabelStr = new char [strLength+1];
485         fs.read (pszLabelStr, strLength);
486         pszLabelStr[strLength] = 0;
487
488         Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime);
489         delete pszLabelStr;\r
490 \r
491         pLabel->setDateTime (year, month, day, hour, minute, second);
492         m_labels.push_back (pLabel);\r
493     }
494
495     return true;
496 }
497
498 bool
499 Array2dFile::labelsWrite (frnetorderstream& fs)
500 {
501     off_t pos = m_headersize + m_arraySize;
502     fs.seekp (pos);
503
504     for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
505         const Array2dFileLabel& label = **l;
506         kuint16 labelType = label.getLabelType();
507         kfloat64 calcTime = label.getCalcTime();
508         const char* const labelString = label.getLabelString().c_str();
509         int year, month, day, hour, minute, second;
510         kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
511
512         label.getDateTime (year, month, day, hour, minute, second);
513         yearBuf = year; monthBuf = month; dayBuf = day;
514         hourBuf = hour; minuteBuf = minute; secondBuf = second;
515
516         fs.writeInt16 (labelType);
517         fs.writeInt16 (yearBuf);
518         fs.writeInt16 (monthBuf);
519         fs.writeInt16 (dayBuf);
520         fs.writeInt16 (hourBuf);
521         fs.writeInt16 (minuteBuf);
522         fs.writeInt16 (secondBuf);
523         fs.writeFloat64 (calcTime);
524         kuint16 strlength = strlen (labelString);
525         fs.writeInt16 (strlength);
526         fs.write (labelString, strlength);
527     }
528
529     return true;
530 }
531
532 void
533 Array2dFile::labelAdd (const char* const lstr, double calc_time)
534 {
535   labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
536 }
537
538
539 void
540 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
541 {
542   Array2dFileLabel label (type, lstr, calc_time);
543
544   labelAdd (label);
545 }
546
547
548 void
549 Array2dFile::labelAdd (const Array2dFileLabel& label)
550 {
551     Array2dFileLabel* pLabel = new Array2dFileLabel(label);
552
553     m_labels.push_back (pLabel);
554 }
555
556 void
557 Array2dFile::labelsCopy (Array2dFile& copyFile, const char* const pszId)
558 {
559         std::string id;
560     if (pszId)
561       id = pszId;
562     for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
563       Array2dFileLabel l (copyFile.labelGet (i));
564           std::string lstr = l.getLabelString();
565       lstr = id + lstr;
566       l.setLabelString (lstr);
567       labelAdd (l);
568     }
569 }
570
571 void 
572 Array2dFile::arrayDataClear (void)
573 {
574     if (m_arrayData) {
575         int columnSize = m_ny * m_pixelSize;
576         for (unsigned int ix = 0; ix < m_nx; ix++)
577             memset (m_arrayData[ix], 0, columnSize);
578     }
579 }
580
581 void
582 Array2dFile::printLabels (std::ostream& os) const
583 {
584     for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
585       const Array2dFileLabel& label = **l;
586
587       label.print (os);
588       os << std::endl;
589     }
590 }
591
592
593 const Array2dFileLabel&
594 Array2dFile::labelGet (int i) const
595 {
596   return *m_labels[i];
597 }