1 /*****************************************************************************
4 ** Name: array2dfile.cpp
5 ** Purpose: 2-dimension array file class
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: June 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: array2dfile.cpp,v 1.29 2002/05/28 18:43:16 kevin Exp $
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.
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.
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 ******************************************************************************/
28 #include "array2dfile.h"
35 const kuint16 Array2dFile::m_signature = ('I'*256+'F');
37 ///////////////////////////////////////////////////////////////////////////
38 // CLASS IMPLEMENTATION
40 // Name: Array2dFileLabel
41 // Purpose: Labels for Array2dFiles
42 ///////////////////////////////////////////////////////////////////////////
45 Array2dFileLabel::init (void)
48 m_labelType = L_EMPTY;
50 tm* lt = localtime(&t);
55 m_minute = lt->tm_min;
56 m_second = lt->tm_sec;
59 Array2dFileLabel::Array2dFileLabel()
64 Array2dFileLabel::Array2dFileLabel(const char* const str, double ctime)
73 Array2dFileLabel::Array2dFileLabel(const int type, const char* const str, double ctime)
82 Array2dFileLabel::~Array2dFileLabel()
87 Array2dFileLabel::setDateTime (int year, int month, int day, int hour, int minute, int second)
98 Array2dFileLabel::getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const
109 Array2dFileLabel::getDateString (void) const
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);
119 Array2dFileLabel::Array2dFileLabel (const Array2dFileLabel& rhs)
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;
129 Array2dFileLabel::operator= (const Array2dFileLabel& rhs)
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;
141 Array2dFileLabel::print (std::ostream& os) const
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;
152 os << "Unknown (" << m_labelType << "): " << m_strLabel << std::endl;
153 os << " Timestamp = %s" << getDateString() << std::endl;
158 Array2dFileLabel::printBrief (std::ostream& os) const
160 if (m_labelType == L_HISTORY) {
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;
168 os << "Unknown (" << getDateString() << "): " << m_strLabel << std::endl;
173 Array2dFileLabel::printBrief (std::ostringstream& os) const
175 if (m_labelType == L_HISTORY) {
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";
183 os << "Unknown (" << getDateString() << "): " << m_strLabel << "\n";
188 ///////////////////////////////////////////////////////////////////////////
189 // CLASS IMPLEMENTATION
192 // Purpose: Array2dFiles
193 ///////////////////////////////////////////////////////////////////////////
196 Array2dFile::Array2dFile (int x, int y, int pixelSize, int pixelFormat, int dataType)
199 setArraySize (x, y, pixelSize, pixelFormat, dataType);
202 Array2dFile::~Array2dFile (void)
205 for (labelIterator l = m_labels.begin(); l != m_labels.end(); l++)
209 Array2dFile::Array2dFile (void)
215 Array2dFile::init (void)
218 m_pixelFormat = PIXEL_INVALID;
220 m_imaginaryArrayData = NULL;
221 m_dataType = DATA_TYPE_INVALID;
225 m_axisIncrementKnown = false;
226 m_axisIncrementX = m_axisIncrementY = 0;
227 m_axisExtentKnown = false;
228 m_minX = m_maxX = m_minY = m_maxY = 0;
235 Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat, int dataType)
237 m_pixelSize = pixelSize;
238 m_pixelFormat = pixelFormat;
239 m_dataType = dataType;
244 Array2dFile::setArraySize (int x, int y)
252 Array2dFile::reallocComplexToReal ()
254 if (m_dataType != DATA_TYPE_COMPLEX)
257 freeArray (m_imaginaryArrayData);
258 m_dataType = DATA_TYPE_REAL;
265 Array2dFile::reallocRealToComplex ()
267 if (m_dataType != DATA_TYPE_REAL)
270 allocArray (m_imaginaryArrayData);
271 m_dataType = DATA_TYPE_COMPLEX;
279 Array2dFile::allocArrays ()
282 freeArray (m_arrayData);
283 if (m_imaginaryArrayData)
284 freeArray (m_imaginaryArrayData);
286 allocArray (m_arrayData);
287 if (m_dataType == DATA_TYPE_COMPLEX)
288 allocArray (m_imaginaryArrayData);
292 Array2dFile::allocArray (unsigned char**& rppData)
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];
302 Array2dFile::freeArrays ()
305 freeArray (m_arrayData);
307 if (m_imaginaryArrayData)
308 freeArray (m_imaginaryArrayData);
313 Array2dFile::freeArray (unsigned char**& rppData)
315 for (unsigned int i = 0; i < m_nx; i++)
323 Array2dFile::fileWrite (const std::string& filename)
325 return fileWrite (filename.c_str());
329 Array2dFile::fileWrite (const char* const filename)
331 m_filename = filename;
333 frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::trunc | std::ios::binary);
335 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
338 if (! headerWrite(fs))
341 if (! arrayDataWrite (fs))
344 if (! labelsWrite (fs))
351 Array2dFile::fileRead (const std::string& filename)
353 return fileRead (filename.c_str());
357 Array2dFile::fileRead (const char* const filename)
359 m_filename = filename;
361 frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary);
364 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
368 if (! headerRead(fs))
373 if (! arrayDataRead(fs))
376 if (! labelsRead (fs))
383 Array2dFile::setAxisIncrement (double incX, double incY)
385 m_axisIncrementKnown = true;
386 m_axisIncrementX = incX;
387 m_axisIncrementY = incY;
391 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
393 m_axisExtentKnown = true;
401 Array2dFile::headerRead (frnetorderstream& fs)
404 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
409 kuint16 file_signature;
411 fs.readInt16 (m_headersize);
412 fs.readInt16 (file_signature);
413 fs.readInt16 (m_pixelFormat);
414 fs.readInt16 (m_pixelSize);
415 fs.readInt16 (m_numFileLabels);
418 fs.readInt16 (m_dataType);
419 fs.readInt16 (m_axisIncrementKnown);
420 fs.readFloat64 (m_axisIncrementX);
421 fs.readFloat64 (m_axisIncrementY);
422 fs.readInt16 (m_axisExtentKnown);
423 fs.readFloat64 (m_minX);
424 fs.readFloat64 (m_maxX);
425 fs.readFloat64 (m_minY);
426 fs.readFloat64 (m_maxY);
427 fs.readFloat64 (m_offsetPV);
428 fs.readFloat64 (m_scalePV);
430 int read_m_headersize = fs.tellg();
431 if (read_m_headersize != m_headersize) {
432 sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
435 if (file_signature != m_signature) {
436 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
445 Array2dFile::headerWrite (frnetorderstream& fs)
448 sys_error (ERR_WARNING, "Tried to write header with ! fs");
452 m_numFileLabels = m_labels.size();
455 fs.writeInt16 (m_headersize);
456 fs.writeInt16 (m_signature);
457 fs.writeInt16 (m_pixelFormat);
458 fs.writeInt16 (m_pixelSize);
459 fs.writeInt16 (m_numFileLabels);
460 fs.writeInt32 (m_nx);
461 fs.writeInt32 (m_ny);
462 fs.writeInt16 (m_dataType);
463 fs.writeInt16 (m_axisIncrementKnown);
464 fs.writeFloat64 (m_axisIncrementX);
465 fs.writeFloat64 (m_axisIncrementY);
466 fs.writeInt16 (m_axisExtentKnown);
467 fs.writeFloat64 (m_minX);
468 fs.writeFloat64 (m_maxX);
469 fs.writeFloat64 (m_minY);
470 fs.writeFloat64 (m_maxY);
471 fs.writeFloat64 (m_offsetPV);
472 fs.writeFloat64 (m_scalePV);
474 m_headersize = static_cast<kuint16>(fs.tellp());
476 fs.writeInt16 (m_headersize);
483 Array2dFile::arrayDataWrite (frnetorderstream& fs)
486 sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
493 fs.seekp (m_headersize);
494 int columnSize = m_ny * m_pixelSize;
495 for (unsigned int ix = 0; ix < m_nx; ix++) {
496 unsigned char* ptrColumn = m_arrayData[ix];
497 if (NativeBigEndian()) {
498 for (unsigned int iy = 0; iy < m_ny; iy++) {
499 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
500 fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
501 ptrColumn += m_pixelSize;
504 fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
506 if (m_dataType == DATA_TYPE_COMPLEX) {
507 for (unsigned int ix = 0; ix < m_nx; ix++) {
508 unsigned char* ptrColumn = m_imaginaryArrayData[ix];
509 if (NativeBigEndian()) {
510 for (unsigned int iy = 0; iy < m_ny; iy++) {
511 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
512 fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
513 ptrColumn += m_pixelSize;
516 fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
525 Array2dFile::arrayDataRead (frnetorderstream& fs)
528 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
535 fs.seekg (m_headersize);
536 int columnSize = m_ny * m_pixelSize;
537 for (unsigned int ix = 0; ix < m_nx; ix++) {
538 unsigned char* ptrColumn = m_arrayData[ix];
539 if (NativeBigEndian()) {
540 for (unsigned int iy = 0; iy < m_ny; iy++) {
541 fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
542 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
543 ptrColumn += m_pixelSize;
546 fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
548 if (m_dataType == DATA_TYPE_COMPLEX) {
549 for (unsigned int ix = 0; ix < m_nx; ix++) {
550 unsigned char* ptrColumn = m_imaginaryArrayData[ix];
551 if (NativeBigEndian()) {
552 for (unsigned int iy = 0; iy < m_ny; iy++) {
553 fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
554 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
555 ptrColumn += m_pixelSize;
558 fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
566 Array2dFile::labelsRead (frnetorderstream& fs)
568 off_t pos = m_headersize + m_arraySize;
573 for (int i = 0; i < m_numFileLabels; i++) {
574 kuint16 labelType, year, month, day, hour, minute, second;
577 fs.readInt16 (labelType);
579 fs.readInt16 (month);
582 fs.readInt16 (minute);
583 fs.readInt16 (second);
584 fs.readFloat64 (calcTime);
587 fs.readInt16 (strLength);
588 char* pszLabelStr = new char [strLength+1];
589 fs.read (pszLabelStr, strLength);
590 pszLabelStr[strLength] = 0;
592 Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime);
595 pLabel->setDateTime (year, month, day, hour, minute, second);
596 m_labels.push_back (pLabel);
603 Array2dFile::labelsWrite (frnetorderstream& fs)
605 off_t pos = m_headersize + m_arraySize;
608 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
609 const Array2dFileLabel& label = **l;
610 kuint16 labelType = label.getLabelType();
611 kfloat64 calcTime = label.getCalcTime();
612 const char* const labelString = label.getLabelString().c_str();
613 int year, month, day, hour, minute, second;
614 kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
616 label.getDateTime (year, month, day, hour, minute, second);
617 yearBuf = year; monthBuf = month; dayBuf = day;
618 hourBuf = hour; minuteBuf = minute; secondBuf = second;
620 fs.writeInt16 (labelType);
621 fs.writeInt16 (yearBuf);
622 fs.writeInt16 (monthBuf);
623 fs.writeInt16 (dayBuf);
624 fs.writeInt16 (hourBuf);
625 fs.writeInt16 (minuteBuf);
626 fs.writeInt16 (secondBuf);
627 fs.writeFloat64 (calcTime);
628 kuint16 strlength = strlen (labelString);
629 fs.writeInt16 (strlength);
630 fs.write (labelString, strlength);
637 Array2dFile::labelAdd (const char* const lstr, double calc_time)
639 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
644 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
646 Array2dFileLabel label (type, lstr, calc_time);
653 Array2dFile::labelAdd (const Array2dFileLabel& label)
655 Array2dFileLabel* pLabel = new Array2dFileLabel(label);
657 m_labels.push_back (pLabel);
661 Array2dFile::labelsCopy (const Array2dFile& copyFile, const char* const pszId)
666 for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
667 Array2dFileLabel l (copyFile.labelGet (i));
668 std::string lstr = l.getLabelString();
670 l.setLabelString (lstr);
676 Array2dFile::arrayDataClear (void)
679 int columnSize = m_ny * m_pixelSize;
680 for (unsigned int ix = 0; ix < m_nx; ix++)
681 memset (m_arrayData[ix], 0, columnSize);
683 if (m_imaginaryArrayData) {
684 int columnSize = m_ny * m_pixelSize;
685 for (unsigned int ix = 0; ix < m_nx; ix++)
686 memset (m_arrayData[ix], 0, columnSize);
691 Array2dFile::printLabels (std::ostream& os) const
693 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
694 const Array2dFileLabel& label = **l;
702 Array2dFile::printLabelsBrief (std::ostream& os) const
704 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
705 const Array2dFileLabel& label = **l;
707 label.printBrief (os);
712 Array2dFile::printLabelsBrief (std::ostringstream& os) const
714 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
715 const Array2dFileLabel& label = **l;
717 label.printBrief (os);
722 const Array2dFileLabel&
723 Array2dFile::labelGet (int i) const