2999470e871e85af572312a63f941e796ab9ae0a
[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.14 2000/12/04 19:50:57 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 #ifdef MSVC\r
282     frnetorderstream fs (m_filename.c_str(), ios::out | ios::in | ios::binary);\r
283 #else\r
284     frnetorderstream fs (m_filename.c_str(), ios::out | ios::in | ios::binary | ios::nocreate);\r
285 #endif\r
286     if (fs.fail()) {
287       sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
288       return false;
289     }
290
291     if (! headerRead(fs))
292       return false;
293     
294     allocArray ();
295     
296     if (! arrayDataRead(fs))
297       return false;;
298
299     if (! labelsRead (fs))
300       return false;
301     
302     return true;
303 }
304
305 void
306 Array2dFile::setAxisIncrement (double incX, double incY)
307 {
308   m_axisIncrementKnown = true;
309   m_axisIncrementX = incX;
310   m_axisIncrementY = incY;
311 }
312
313 void 
314 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
315 {
316     m_axisExtentKnown = true;
317     m_minX = minX;
318     m_maxY = maxX;
319     m_minX = minX;
320     m_maxY = maxY;
321 }
322
323 bool
324 Array2dFile::headerRead (frnetorderstream& fs)
325 {
326   if (! fs) {
327     sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
328     return false;
329   }
330
331   fs.seekg (0);
332   kuint16 file_signature;
333
334   fs.readInt16 (m_headersize);
335   fs.readInt16 (file_signature);
336   fs.readInt16 (m_pixelFormat);
337   fs.readInt16 (m_pixelSize);
338   fs.readInt16 (m_numFileLabels);
339   fs.readInt32 (m_nx);
340   fs.readInt32 (m_ny);
341   fs.readInt16 (m_axisIncrementKnown);
342   fs.readFloat64 (m_axisIncrementX);
343   fs.readFloat64 (m_axisIncrementY);
344   fs.readInt16 (m_axisExtentKnown);
345   fs.readFloat64 (m_minX);
346   fs.readFloat64 (m_maxX);
347   fs.readFloat64 (m_minY);
348   fs.readFloat64 (m_maxY);
349   fs.readFloat64 (m_offsetPV);
350   fs.readFloat64 (m_scalePV);
351
352   int read_m_headersize = fs.tellg();
353   if (read_m_headersize != m_headersize) {
354     sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
355     return false;
356   }
357   if (file_signature != m_signature) {
358     sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
359     return false;
360   }
361
362   return true;
363 }
364
365
366 bool
367 Array2dFile::headerWrite (frnetorderstream& fs)
368 {
369   if (! fs) {
370     sys_error (ERR_WARNING, "Tried to write header with ! fs");
371     return false;
372   }
373
374   m_numFileLabels = m_labels.size();
375
376   fs.seekp (0);
377   fs.writeInt16 (m_headersize);
378   fs.writeInt16 (m_signature);
379   fs.writeInt16 (m_pixelFormat);
380   fs.writeInt16 (m_pixelSize);
381   fs.writeInt16 (m_numFileLabels);
382   fs.writeInt32 (m_nx);
383   fs.writeInt32 (m_ny);
384   fs.writeInt16 (m_axisIncrementKnown);
385   fs.writeFloat64 (m_axisIncrementX);
386   fs.writeFloat64 (m_axisIncrementY);
387   fs.writeInt16 (m_axisExtentKnown);
388   fs.writeFloat64 (m_minX);
389   fs.writeFloat64 (m_maxX);
390   fs.writeFloat64 (m_minY);
391   fs.writeFloat64 (m_maxY);
392   fs.writeFloat64 (m_offsetPV);
393   fs.writeFloat64 (m_scalePV);
394
395   m_headersize = fs.tellp();
396   fs.seekp (0);
397   fs.writeInt16 (m_headersize);
398   
399   return true;
400 }
401
402
403 bool
404 Array2dFile::arrayDataWrite (frnetorderstream& fs)
405 {
406   if (! fs) {
407     sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
408     return false;
409   }
410
411   if (! m_arrayData) 
412       return false;
413
414   fs.seekp (m_headersize);
415   int columnSize = m_ny * m_pixelSize;
416   for (unsigned int ix = 0; ix < m_nx; ix++) {
417       unsigned char* ptrColumn = m_arrayData[ix];
418       if (NativeBigEndian()) {
419           for (unsigned int iy = 0; iy < m_ny; iy++) {
420               ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
421               fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
422               ptrColumn += m_pixelSize;
423           }
424       } else 
425           fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
426   }
427
428   return true;
429 }
430
431
432 bool
433 Array2dFile::arrayDataRead (frnetorderstream& fs)
434 {
435   if (! fs) {
436     sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
437     return false;
438   }
439
440   if (! m_arrayData)
441       return false;
442
443   fs.seekg (m_headersize);
444   int columnSize = m_ny * m_pixelSize;
445   for (unsigned int ix = 0; ix < m_nx; ix++) {
446       unsigned char* ptrColumn = m_arrayData[ix];
447       if (NativeBigEndian()) {
448           for (unsigned int iy = 0; iy < m_ny; iy++) {
449               fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
450               ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
451               ptrColumn += m_pixelSize;
452           } 
453       } else
454           fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
455   }
456
457   return true;
458 }
459
460 bool
461 Array2dFile::labelsRead (frnetorderstream& fs)
462 {
463     off_t pos = m_headersize + m_arraySize;
464     fs.seekg (pos);
465     if (fs.fail())
466         return false;
467
468     for (int i = 0; i < m_numFileLabels; i++) {
469         kuint16 labelType, year, month, day, hour, minute, second;
470         kfloat64 calcTime;
471
472         fs.readInt16 (labelType);
473         fs.readInt16 (year);
474         fs.readInt16 (month);
475         fs.readInt16 (day);
476         fs.readInt16 (hour);
477         fs.readInt16 (minute);
478         fs.readInt16 (second);
479         fs.readFloat64 (calcTime);
480         
481         kuint16 strLength;
482         fs.readInt16 (strLength);
483         char* labelStr = new char [strLength+1];
484         fs.read (labelStr, strLength);
485         labelStr[strLength] = 0;
486
487         Array2dFileLabel* pLabel = new Array2dFileLabel(labelType, labelStr, calcTime);
488         delete labelStr;\r
489 \r
490         pLabel->setDateTime (year, month, day, hour, minute, second);
491         m_labels.push_back (pLabel);\r
492     }
493
494     return true;
495 }
496
497 bool
498 Array2dFile::labelsWrite (frnetorderstream& fs)
499 {
500     off_t pos = m_headersize + m_arraySize;
501     fs.seekp (pos);
502
503     for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
504         const Array2dFileLabel& label = **l;
505         kuint16 labelType = label.getLabelType();
506         kfloat64 calcTime = label.getCalcTime();
507         const char* const labelString = label.getLabelString().c_str();
508         int year, month, day, hour, minute, second;
509         kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
510
511         label.getDateTime (year, month, day, hour, minute, second);
512         yearBuf = year; monthBuf = month; dayBuf = day;
513         hourBuf = hour; minuteBuf = minute; secondBuf = second;
514
515         fs.writeInt16 (labelType);
516         fs.writeInt16 (yearBuf);
517         fs.writeInt16 (monthBuf);
518         fs.writeInt16 (dayBuf);
519         fs.writeInt16 (hourBuf);
520         fs.writeInt16 (minuteBuf);
521         fs.writeInt16 (secondBuf);
522         fs.writeFloat64 (calcTime);
523         kuint16 strlength = strlen (labelString);
524         fs.writeInt16 (strlength);
525         fs.write (labelString, strlength);
526     }
527
528     return true;
529 }
530
531 void
532 Array2dFile::labelAdd (const char* const lstr, double calc_time)
533 {
534   labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
535 }
536
537
538 void
539 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
540 {
541   Array2dFileLabel label (type, lstr, calc_time);
542
543   labelAdd (label);
544 }
545
546
547 void
548 Array2dFile::labelAdd (const Array2dFileLabel& label)
549 {
550     Array2dFileLabel* pLabel = new Array2dFileLabel(label);
551
552     m_labels.push_back (pLabel);
553 }
554
555 void
556 Array2dFile::labelsCopy (Array2dFile& copyFile, const char* const idStr)
557 {
558     string id = idStr;
559     for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
560       Array2dFileLabel l (copyFile.labelGet (i));
561       string lstr = l.getLabelString();
562       lstr = idStr + lstr;
563       l.setLabelString (lstr);
564       labelAdd (l);
565     }
566 }
567
568 void 
569 Array2dFile::arrayDataClear (void)
570 {
571     if (m_arrayData) {
572         int columnSize = m_ny * m_pixelSize;
573         for (unsigned int ix = 0; ix < m_nx; ix++)
574             memset (m_arrayData[ix], 0, columnSize);
575     }
576 }
577
578 void
579 Array2dFile::printLabels (ostream& os) const
580 {
581     for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
582       const Array2dFileLabel& label = **l;
583
584       label.print (os);
585       os << endl;
586     }
587 }
588
589
590 const Array2dFileLabel&
591 Array2dFile::labelGet (int i) const
592 {
593   return *m_labels[i];
594 }