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