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