555d22eed1b9c771fdfb92d2e2c37fef87c5094a
[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.27 2001/01/02 16:02:13 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 #ifdef MSVC
31 typedef long off_t;
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];
112   snprintf (szDate, sizeof(szDate), "%2d/%02d/%4d %02d:%02d:%02d",
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 void
158 Array2dFileLabel::printBrief (std::ostream& os) const
159 {
160   if (m_labelType == L_HISTORY) {
161     os << "History (";
162     if (m_calcTime > 0)
163       os << m_calcTime << " secs, ";
164     os << getDateString() << "): " << m_strLabel << std::endl;
165   } else if (m_labelType == L_USER) {
166     os << "Note (" <<  getDateString() << "): " << m_strLabel << std::endl;
167   } else {
168     os << "Unknown (" << getDateString() << "): " << m_strLabel << std::endl;
169   }
170 }
171
172 void
173 Array2dFileLabel::printBrief (std::ostringstream& os) const
174 {
175   if (m_labelType == L_HISTORY) {
176     os << "History (";
177     if (m_calcTime > 0)
178       os << m_calcTime << " secs, ";
179     os << getDateString().c_str() << "): " << m_strLabel.c_str() << "\n";
180   } else if (m_labelType == L_USER) {
181     os << "Note (" <<  getDateString() << "): " << m_strLabel << "\n";
182   } else {
183     os << "Unknown (" << getDateString() << "): " << m_strLabel << "\n";
184   }
185 }
186
187
188 ///////////////////////////////////////////////////////////////////////////
189 // CLASS IMPLEMENTATION
190 //
191 //     Name: Array2dFile
192 //     Purpose: Array2dFiles
193 ///////////////////////////////////////////////////////////////////////////
194
195
196 Array2dFile::Array2dFile (int x, int y, int pixelSize, int pixelFormat, int dataType)
197 {
198   init();
199   setArraySize (x, y, pixelSize, pixelFormat, dataType);
200 }
201
202 Array2dFile::~Array2dFile (void)
203 {
204   freeArrays ();
205   for (labelIterator l = m_labels.begin(); l != m_labels.end(); l++)
206     delete *l;
207 }
208
209 Array2dFile::Array2dFile (void)
210 {
211   init();
212 }
213
214 void
215 Array2dFile::init (void)
216 {
217   m_pixelSize = 0;
218   m_pixelFormat = PIXEL_INVALID;
219   m_arrayData = NULL;
220   m_imaginaryArrayData = NULL;
221   m_dataType = DATA_TYPE_INVALID;
222   m_nx = 0;
223   m_ny = 0;
224   m_headersize = 0;
225   m_axisIncrementKnown = false;
226   m_axisIncrementX = m_axisIncrementY = 0;
227   m_axisExtentKnown = false;
228   m_minX = m_maxX = m_minY = m_maxY = 0;
229   m_offsetPV = 0;
230   m_scalePV = 1;
231 }
232
233
234 void
235 Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat, int dataType)
236 {
237   m_pixelSize = pixelSize;
238   m_pixelFormat = pixelFormat;
239   m_dataType = dataType;
240   setArraySize (x, y);
241 }
242
243 void
244 Array2dFile::setArraySize (int x, int y)
245 {
246   m_nx = x;
247   m_ny = y;
248   allocArrays ();
249 }
250
251 bool
252 Array2dFile::reallocComplexToReal ()
253 {
254   if (m_dataType != DATA_TYPE_COMPLEX)
255     return false;
256
257   freeArray (m_imaginaryArrayData);
258   m_dataType = DATA_TYPE_REAL;
259
260   return true;
261 }
262
263
264 bool
265 Array2dFile::reallocRealToComplex ()
266 {
267   if (m_dataType != DATA_TYPE_REAL)
268     return false;
269
270   allocArray (m_imaginaryArrayData);
271   m_dataType = DATA_TYPE_COMPLEX;
272
273   return true;
274 }
275
276
277
278 void 
279 Array2dFile::allocArrays ()
280 {
281   if (m_arrayData) 
282     freeArray (m_arrayData);
283   if (m_imaginaryArrayData)
284     freeArray (m_imaginaryArrayData);
285
286   allocArray (m_arrayData);
287   if (m_dataType == DATA_TYPE_COMPLEX)
288     allocArray (m_imaginaryArrayData);
289 }
290
291 void
292 Array2dFile::allocArray (unsigned char**& rppData)
293 {
294   m_arraySize = m_nx * m_ny * m_pixelSize;
295   rppData = new unsigned char* [m_nx];
296   int columnBytes = m_ny * m_pixelSize;
297   for (unsigned int i = 0; i < m_nx; i++)
298     rppData[i] = new unsigned char [columnBytes];
299 }
300
301 void 
302 Array2dFile::freeArrays ()
303 {
304   if (m_arrayData) 
305     freeArray (m_arrayData);
306   
307   if (m_imaginaryArrayData) 
308     freeArray (m_imaginaryArrayData);
309   
310 }
311
312 void
313 Array2dFile::freeArray (unsigned char**& rppData)
314 {
315    for (unsigned int i = 0; i < m_nx; i++)
316       delete rppData[i];
317     delete rppData;
318     rppData = NULL;
319 }
320
321
322 bool
323 Array2dFile::fileWrite (const std::string& filename)
324 {
325   return fileWrite (filename.c_str());
326 }
327
328 bool
329 Array2dFile::fileWrite (const char* const filename)
330 {
331   m_filename = filename;
332   
333   frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::trunc | std::ios::binary);
334   if (fs.fail()) {
335     sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
336     return false;
337   }
338   if (! headerWrite(fs))
339     return false;
340   
341   if (! arrayDataWrite (fs))
342     return false;
343   
344   if (! labelsWrite (fs))
345     return false;
346   
347   return true;
348 }
349
350 bool
351 Array2dFile::fileRead (const std::string& filename)
352 {
353   return fileRead (filename.c_str());
354 }
355
356 bool
357 Array2dFile::fileRead (const char* const filename)
358 {
359   m_filename = filename;
360   
361 #ifdef MSVC
362   frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary);
363 #else
364   frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary | std::ios::nocreate);
365 #endif
366   if (fs.fail()) {
367     sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
368     return false;
369   }
370   
371   if (! headerRead(fs))
372     return false;
373   
374   allocArrays ();
375   
376   if (! arrayDataRead(fs))
377     return false;;
378   
379   if (! labelsRead (fs))
380     return false;
381   
382   return true;
383 }
384
385 void
386 Array2dFile::setAxisIncrement (double incX, double incY)
387 {
388   m_axisIncrementKnown = true;
389   m_axisIncrementX = incX;
390   m_axisIncrementY = incY;
391 }
392
393 void 
394 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
395 {
396   m_axisExtentKnown = true;
397   m_minX = minX;
398   m_maxY = maxX;
399   m_minX = minX;
400   m_maxY = maxY;
401 }
402
403 bool
404 Array2dFile::headerRead (frnetorderstream& fs)
405 {
406   if (! fs) {
407     sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
408     return false;
409   }
410   
411   fs.seekg (0);
412   kuint16 file_signature;
413   
414   fs.readInt16 (m_headersize);
415   fs.readInt16 (file_signature);
416   fs.readInt16 (m_pixelFormat);
417   fs.readInt16 (m_pixelSize);
418   fs.readInt16 (m_numFileLabels);
419   fs.readInt32 (m_nx);
420   fs.readInt32 (m_ny);
421   fs.readInt16 (m_dataType);
422   fs.readInt16 (m_axisIncrementKnown);
423   fs.readFloat64 (m_axisIncrementX);
424   fs.readFloat64 (m_axisIncrementY);
425   fs.readInt16 (m_axisExtentKnown);
426   fs.readFloat64 (m_minX);
427   fs.readFloat64 (m_maxX);
428   fs.readFloat64 (m_minY);
429   fs.readFloat64 (m_maxY);
430   fs.readFloat64 (m_offsetPV);
431   fs.readFloat64 (m_scalePV);
432   
433   int read_m_headersize = fs.tellg();
434   if (read_m_headersize != m_headersize) {
435     sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
436     return false;
437   }
438   if (file_signature != m_signature) {
439     sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
440     return false;
441   }
442   
443   return ! fs.fail();
444 }
445
446
447 bool
448 Array2dFile::headerWrite (frnetorderstream& fs)
449 {
450   if (! fs) {
451     sys_error (ERR_WARNING, "Tried to write header with ! fs");
452     return false;
453   }
454   
455   m_numFileLabels = m_labels.size();
456   
457   fs.seekp (0);
458   fs.writeInt16 (m_headersize);
459   fs.writeInt16 (m_signature);
460   fs.writeInt16 (m_pixelFormat);
461   fs.writeInt16 (m_pixelSize);
462   fs.writeInt16 (m_numFileLabels);
463   fs.writeInt32 (m_nx);
464   fs.writeInt32 (m_ny);
465   fs.writeInt16 (m_dataType);
466   fs.writeInt16 (m_axisIncrementKnown);
467   fs.writeFloat64 (m_axisIncrementX);
468   fs.writeFloat64 (m_axisIncrementY);
469   fs.writeInt16 (m_axisExtentKnown);
470   fs.writeFloat64 (m_minX);
471   fs.writeFloat64 (m_maxX);
472   fs.writeFloat64 (m_minY);
473   fs.writeFloat64 (m_maxY);
474   fs.writeFloat64 (m_offsetPV);
475   fs.writeFloat64 (m_scalePV);
476   
477   m_headersize = static_cast<kuint16>(fs.tellp());
478   fs.seekp (0);
479   fs.writeInt16 (m_headersize);
480   
481   return ! fs.fail();
482 }
483
484
485 bool
486 Array2dFile::arrayDataWrite (frnetorderstream& fs)
487 {
488   if (! fs) {
489     sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
490     return false;
491   }
492   
493   if (! m_arrayData) 
494     return false;
495   
496   fs.seekp (m_headersize);
497   int columnSize = m_ny * m_pixelSize;
498   for (unsigned int ix = 0; ix < m_nx; ix++) {
499     unsigned char* ptrColumn = m_arrayData[ix];
500     if (NativeBigEndian()) {
501       for (unsigned int iy = 0; iy < m_ny; iy++) {
502         ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
503         fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
504         ptrColumn += m_pixelSize;
505       }
506     } else 
507       fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
508   }
509   if (m_dataType == DATA_TYPE_COMPLEX) {
510     for (unsigned int ix = 0; ix < m_nx; ix++) {
511       unsigned char* ptrColumn = m_imaginaryArrayData[ix];
512       if (NativeBigEndian()) {
513         for (unsigned int iy = 0; iy < m_ny; iy++) {
514           ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
515           fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
516           ptrColumn += m_pixelSize;
517         }
518       } else 
519         fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
520     }
521   }
522   
523   return true;
524 }
525
526
527 bool
528 Array2dFile::arrayDataRead (frnetorderstream& fs)
529 {
530   if (! fs) {
531     sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
532     return false;
533   }
534   
535   if (! m_arrayData)
536     return false;
537   
538   fs.seekg (m_headersize);
539   int columnSize = m_ny * m_pixelSize;
540   for (unsigned int ix = 0; ix < m_nx; ix++) {
541     unsigned char* ptrColumn = m_arrayData[ix];
542     if (NativeBigEndian()) {
543       for (unsigned int iy = 0; iy < m_ny; iy++) {
544         fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
545         ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
546         ptrColumn += m_pixelSize;
547       } 
548     } else
549       fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
550   }
551   if (m_dataType == DATA_TYPE_COMPLEX) {
552     for (unsigned int ix = 0; ix < m_nx; ix++) {
553       unsigned char* ptrColumn = m_imaginaryArrayData[ix];
554       if (NativeBigEndian()) {
555         for (unsigned int iy = 0; iy < m_ny; iy++) {
556           fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
557           ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
558           ptrColumn += m_pixelSize;
559         } 
560       } else
561         fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
562     }
563   }
564   
565   return true;
566 }
567
568 bool
569 Array2dFile::labelsRead (frnetorderstream& fs)
570 {
571   off_t pos = m_headersize + m_arraySize;
572   fs.seekg (pos);
573   if (fs.fail())
574     return false;
575   
576   for (int i = 0; i < m_numFileLabels; i++) {
577     kuint16 labelType, year, month, day, hour, minute, second;
578     kfloat64 calcTime;
579     
580     fs.readInt16 (labelType);
581     fs.readInt16 (year);
582     fs.readInt16 (month);
583     fs.readInt16 (day);
584     fs.readInt16 (hour);
585     fs.readInt16 (minute);
586     fs.readInt16 (second);
587     fs.readFloat64 (calcTime);
588     
589     kuint16 strLength;
590     fs.readInt16 (strLength);
591     char* pszLabelStr = new char [strLength+1];
592     fs.read (pszLabelStr, strLength);
593     pszLabelStr[strLength] = 0;
594     
595     Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime);
596     delete pszLabelStr;
597     
598     pLabel->setDateTime (year, month, day, hour, minute, second);
599     m_labels.push_back (pLabel);
600   }
601   
602   return true;
603 }
604
605 bool
606 Array2dFile::labelsWrite (frnetorderstream& fs)
607 {
608   off_t pos = m_headersize + m_arraySize;
609   fs.seekp (pos);
610   
611   for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
612     const Array2dFileLabel& label = **l;
613     kuint16 labelType = label.getLabelType();
614     kfloat64 calcTime = label.getCalcTime();
615     const char* const labelString = label.getLabelString().c_str();
616     int year, month, day, hour, minute, second;
617     kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
618     
619     label.getDateTime (year, month, day, hour, minute, second);
620     yearBuf = year; monthBuf = month; dayBuf = day;
621     hourBuf = hour; minuteBuf = minute; secondBuf = second;
622     
623     fs.writeInt16 (labelType);
624     fs.writeInt16 (yearBuf);
625     fs.writeInt16 (monthBuf);
626     fs.writeInt16 (dayBuf);
627     fs.writeInt16 (hourBuf);
628     fs.writeInt16 (minuteBuf);
629     fs.writeInt16 (secondBuf);
630     fs.writeFloat64 (calcTime);
631     kuint16 strlength = strlen (labelString);
632     fs.writeInt16 (strlength);
633     fs.write (labelString, strlength);
634   }
635   
636   return true;
637 }
638
639 void
640 Array2dFile::labelAdd (const char* const lstr, double calc_time)
641 {
642   labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
643 }
644
645
646 void
647 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
648 {
649   Array2dFileLabel label (type, lstr, calc_time);
650   
651   labelAdd (label);
652 }
653
654
655 void
656 Array2dFile::labelAdd (const Array2dFileLabel& label)
657 {
658   Array2dFileLabel* pLabel = new Array2dFileLabel(label);
659   
660   m_labels.push_back (pLabel);
661 }
662
663 void
664 Array2dFile::labelsCopy (const Array2dFile& copyFile, const char* const pszId)
665 {
666   std::string id;
667   if (pszId)
668     id = pszId;
669   for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
670     Array2dFileLabel l (copyFile.labelGet (i));
671     std::string lstr = l.getLabelString();
672     lstr = id + lstr;
673     l.setLabelString (lstr);
674     labelAdd (l);
675   }
676 }
677
678 void 
679 Array2dFile::arrayDataClear (void)
680 {
681   if (m_arrayData) {
682     int columnSize = m_ny * m_pixelSize;
683     for (unsigned int ix = 0; ix < m_nx; ix++)
684       memset (m_arrayData[ix], 0, columnSize);
685   }
686   if (m_imaginaryArrayData) {
687     int columnSize = m_ny * m_pixelSize;
688     for (unsigned int ix = 0; ix < m_nx; ix++)
689       memset (m_arrayData[ix], 0, columnSize);
690   }
691 }
692
693 void
694 Array2dFile::printLabels (std::ostream& os) const
695 {
696   for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
697     const Array2dFileLabel& label = **l;
698     
699     label.print (os);
700     os << std::endl;
701   }
702 }
703
704 void
705 Array2dFile::printLabelsBrief (std::ostream& os) const
706 {
707   for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
708     const Array2dFileLabel& label = **l;
709     
710     label.printBrief (os);
711   }
712 }
713
714 void
715 Array2dFile::printLabelsBrief (std::ostringstream& os) const
716 {
717   for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
718     const Array2dFileLabel& label = **l;
719     
720     label.printBrief (os);
721   }
722 }
723
724
725 const Array2dFileLabel&
726 Array2dFile::labelGet (int i) const
727 {
728   return *m_labels[i];
729 }