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