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
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"
36 const kuint16 Array2dFile::m_signature = ('I'*256+'F');
38 ///////////////////////////////////////////////////////////////////////////
39 // CLASS IMPLEMENTATION
41 // Name: Array2dFileLabel
42 // Purpose: Labels for Array2dFiles
43 ///////////////////////////////////////////////////////////////////////////
46 Array2dFileLabel::init (void)
49 m_labelType = L_EMPTY;
51 tm* lt = localtime(&t);
56 m_minute = lt->tm_min;
57 m_second = lt->tm_sec;
60 Array2dFileLabel::Array2dFileLabel()
65 Array2dFileLabel::Array2dFileLabel(const char* const str, double ctime)
74 Array2dFileLabel::Array2dFileLabel(const int type, const char* const str, double ctime)
83 Array2dFileLabel::~Array2dFileLabel()
88 Array2dFileLabel::setDateTime (int year, int month, int day, int hour, int minute, int second)
99 Array2dFileLabel::getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const
110 Array2dFileLabel::getDateString (void) const
113 snprintf (szDate, sizeof(szDate), "%2d/%02d/%4d %02d:%02d:%02d",
114 m_month + 1, m_day, m_year + 1900, m_hour, m_minute, m_second);
120 Array2dFileLabel::Array2dFileLabel (const Array2dFileLabel& rhs)
122 m_calcTime = rhs.m_calcTime;
123 m_labelType = rhs.m_labelType;
124 m_strLabel = rhs.m_strLabel;
125 m_year = rhs.m_year; m_month = rhs.m_month; m_day = rhs.m_day;
126 m_hour = rhs.m_hour; m_minute = rhs.m_minute; m_second = rhs.m_second;
130 Array2dFileLabel::operator= (const Array2dFileLabel& rhs)
132 m_calcTime = rhs.m_calcTime;
133 m_labelType = rhs.m_labelType;
134 m_strLabel = rhs.m_strLabel;
135 m_year = rhs.m_year; m_month = rhs.m_month; m_day = rhs.m_day;
136 m_hour = rhs.m_hour; m_minute = rhs.m_minute; m_second = rhs.m_second;
142 Array2dFileLabel::print (std::ostream& os) const
144 if (m_labelType == L_HISTORY) {
145 os << "History: " << std::endl;
146 os << " " << m_strLabel << std::endl;
147 os << " calc time = " << m_calcTime << " secs" << std::endl;
148 os << " Timestamp = " << getDateString() << std::endl;
149 } else if (m_labelType == L_USER) {
150 os << "Note: " << m_strLabel << std::endl;
151 os << " Timestamp = %s" << getDateString() << std::endl;
153 os << "Unknown (" << m_labelType << "): " << m_strLabel << std::endl;
154 os << " Timestamp = %s" << getDateString() << std::endl;
159 Array2dFileLabel::printBrief (std::ostream& os) const
161 if (m_labelType == L_HISTORY) {
164 os << m_calcTime << " secs, ";
165 os << getDateString() << "): " << m_strLabel << std::endl;
166 } else if (m_labelType == L_USER) {
167 os << "Note (" << getDateString() << "): " << m_strLabel << std::endl;
169 os << "Unknown (" << getDateString() << "): " << m_strLabel << std::endl;
174 Array2dFileLabel::printBrief (std::ostringstream& os) const
176 if (m_labelType == L_HISTORY) {
179 os << m_calcTime << " secs, ";
180 os << getDateString().c_str() << "): " << m_strLabel.c_str() << "\n";
181 } else if (m_labelType == L_USER) {
182 os << "Note (" << getDateString() << "): " << m_strLabel << "\n";
184 os << "Unknown (" << getDateString() << "): " << m_strLabel << "\n";
189 ///////////////////////////////////////////////////////////////////////////
190 // CLASS IMPLEMENTATION
193 // Purpose: Array2dFiles
194 ///////////////////////////////////////////////////////////////////////////
197 Array2dFile::Array2dFile (int x, int y, int pixelSize, int pixelFormat, int dataType)
200 setArraySize (x, y, pixelSize, pixelFormat, dataType);
203 Array2dFile::~Array2dFile (void)
206 for (labelIterator l = m_labels.begin(); l != m_labels.end(); l++)
210 Array2dFile::Array2dFile (void)
216 Array2dFile::init (void)
219 m_pixelFormat = PIXEL_INVALID;
221 m_imaginaryArrayData = NULL;
222 m_dataType = DATA_TYPE_INVALID;
226 m_axisIncrementKnown = false;
227 m_axisIncrementX = m_axisIncrementY = 0;
228 m_axisExtentKnown = false;
229 m_minX = m_maxX = m_minY = m_maxY = 0;
236 Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat, int dataType)
238 m_pixelSize = pixelSize;
239 m_pixelFormat = pixelFormat;
240 m_dataType = dataType;
245 Array2dFile::setArraySize (int x, int y)
253 Array2dFile::reallocComplexToReal ()
255 if (m_dataType != DATA_TYPE_COMPLEX)
258 freeArray (m_imaginaryArrayData);
259 m_dataType = DATA_TYPE_REAL;
266 Array2dFile::reallocRealToComplex ()
268 if (m_dataType != DATA_TYPE_REAL)
271 allocArray (m_imaginaryArrayData);
272 m_dataType = DATA_TYPE_COMPLEX;
280 Array2dFile::allocArrays ()
283 freeArray (m_arrayData);
284 if (m_imaginaryArrayData)
285 freeArray (m_imaginaryArrayData);
287 allocArray (m_arrayData);
288 if (m_dataType == DATA_TYPE_COMPLEX)
289 allocArray (m_imaginaryArrayData);
293 Array2dFile::allocArray (unsigned char**& rppData)
295 m_arraySize = m_nx * m_ny * m_pixelSize;
296 rppData = new unsigned char* [m_nx];
297 int columnBytes = m_ny * m_pixelSize;
298 for (unsigned int i = 0; i < m_nx; i++)
299 rppData[i] = new unsigned char [columnBytes];
303 Array2dFile::freeArrays ()
306 freeArray (m_arrayData);
308 if (m_imaginaryArrayData)
309 freeArray (m_imaginaryArrayData);
314 Array2dFile::freeArray (unsigned char**& rppData)
316 for (unsigned int i = 0; i < m_nx; i++)
324 Array2dFile::fileWrite (const std::string& filename)
326 return fileWrite (filename.c_str());
330 Array2dFile::fileWrite (const char* const filename)
332 m_filename = filename;
334 frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::trunc | std::ios::binary);
336 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
339 if (! headerWrite(fs))
342 if (! arrayDataWrite (fs))
345 if (! labelsWrite (fs))
352 Array2dFile::fileRead (const std::string& filename)
354 return fileRead (filename.c_str());
358 Array2dFile::fileRead (const char* const filename)
360 m_filename = filename;
362 frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary);
365 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
369 if (! headerRead(fs))
374 if (! arrayDataRead(fs))
377 if (! labelsRead (fs))
384 Array2dFile::setAxisIncrement (double incX, double incY)
386 m_axisIncrementKnown = true;
387 m_axisIncrementX = incX;
388 m_axisIncrementY = incY;
392 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
394 m_axisExtentKnown = true;
402 Array2dFile::headerRead (frnetorderstream& fs)
405 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
410 kuint16 file_signature;
412 fs.readInt16 (m_headersize);
413 fs.readInt16 (file_signature);
414 fs.readInt16 (m_pixelFormat);
415 fs.readInt16 (m_pixelSize);
416 fs.readInt16 (m_numFileLabels);
419 fs.readInt16 (m_dataType);
420 fs.readInt16 (m_axisIncrementKnown);
421 fs.readFloat64 (m_axisIncrementX);
422 fs.readFloat64 (m_axisIncrementY);
423 fs.readInt16 (m_axisExtentKnown);
424 fs.readFloat64 (m_minX);
425 fs.readFloat64 (m_maxX);
426 fs.readFloat64 (m_minY);
427 fs.readFloat64 (m_maxY);
428 fs.readFloat64 (m_offsetPV);
429 fs.readFloat64 (m_scalePV);
431 int read_m_headersize = fs.tellg();
432 if (read_m_headersize != m_headersize) {
433 sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
436 if (file_signature != m_signature) {
437 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
446 Array2dFile::headerWrite (frnetorderstream& fs)
449 sys_error (ERR_WARNING, "Tried to write header with ! fs");
453 m_numFileLabels = m_labels.size();
456 fs.writeInt16 (m_headersize);
457 fs.writeInt16 (m_signature);
458 fs.writeInt16 (m_pixelFormat);
459 fs.writeInt16 (m_pixelSize);
460 fs.writeInt16 (m_numFileLabels);
461 fs.writeInt32 (m_nx);
462 fs.writeInt32 (m_ny);
463 fs.writeInt16 (m_dataType);
464 fs.writeInt16 (m_axisIncrementKnown);
465 fs.writeFloat64 (m_axisIncrementX);
466 fs.writeFloat64 (m_axisIncrementY);
467 fs.writeInt16 (m_axisExtentKnown);
468 fs.writeFloat64 (m_minX);
469 fs.writeFloat64 (m_maxX);
470 fs.writeFloat64 (m_minY);
471 fs.writeFloat64 (m_maxY);
472 fs.writeFloat64 (m_offsetPV);
473 fs.writeFloat64 (m_scalePV);
475 m_headersize = static_cast<kuint16>(fs.tellp());
477 fs.writeInt16 (m_headersize);
484 Array2dFile::arrayDataWrite (frnetorderstream& fs)
487 sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
494 fs.seekp (m_headersize);
495 int columnSize = m_ny * m_pixelSize;
496 for (unsigned int ix = 0; ix < m_nx; ix++) {
497 unsigned char* ptrColumn = m_arrayData[ix];
498 if (NativeBigEndian()) {
499 for (unsigned int iy = 0; iy < m_ny; iy++) {
500 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
501 fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
502 ptrColumn += m_pixelSize;
505 fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
507 if (m_dataType == DATA_TYPE_COMPLEX) {
508 for (unsigned int ix = 0; ix < m_nx; ix++) {
509 unsigned char* ptrColumn = m_imaginaryArrayData[ix];
510 if (NativeBigEndian()) {
511 for (unsigned int iy = 0; iy < m_ny; iy++) {
512 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
513 fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
514 ptrColumn += m_pixelSize;
517 fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
526 Array2dFile::arrayDataRead (frnetorderstream& fs)
529 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
536 fs.seekg (m_headersize);
537 int columnSize = m_ny * m_pixelSize;
538 for (unsigned int ix = 0; ix < m_nx; ix++) {
539 unsigned char* ptrColumn = m_arrayData[ix];
540 if (NativeBigEndian()) {
541 for (unsigned int iy = 0; iy < m_ny; iy++) {
542 fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
543 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
544 ptrColumn += m_pixelSize;
547 fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
549 if (m_dataType == DATA_TYPE_COMPLEX) {
550 for (unsigned int ix = 0; ix < m_nx; ix++) {
551 unsigned char* ptrColumn = m_imaginaryArrayData[ix];
552 if (NativeBigEndian()) {
553 for (unsigned int iy = 0; iy < m_ny; iy++) {
554 fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
555 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
556 ptrColumn += m_pixelSize;
559 fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
567 Array2dFile::labelsRead (frnetorderstream& fs)
569 off_t pos = m_headersize + m_arraySize;
574 for (int i = 0; i < m_numFileLabels; i++) {
575 kuint16 labelType, year, month, day, hour, minute, second;
578 fs.readInt16 (labelType);
580 fs.readInt16 (month);
583 fs.readInt16 (minute);
584 fs.readInt16 (second);
585 fs.readFloat64 (calcTime);
588 fs.readInt16 (strLength);
589 char* pszLabelStr = new char [strLength+1];
590 fs.read (pszLabelStr, strLength);
591 pszLabelStr[strLength] = 0;
593 Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime);
596 pLabel->setDateTime (year, month, day, hour, minute, second);
597 m_labels.push_back (pLabel);
604 Array2dFile::labelsWrite (frnetorderstream& fs)
606 off_t pos = m_headersize + m_arraySize;
609 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
610 const Array2dFileLabel& label = **l;
611 kuint16 labelType = label.getLabelType();
612 kfloat64 calcTime = label.getCalcTime();
613 const char* const labelString = label.getLabelString().c_str();
614 int year, month, day, hour, minute, second;
615 kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
617 label.getDateTime (year, month, day, hour, minute, second);
618 yearBuf = year; monthBuf = month; dayBuf = day;
619 hourBuf = hour; minuteBuf = minute; secondBuf = second;
621 fs.writeInt16 (labelType);
622 fs.writeInt16 (yearBuf);
623 fs.writeInt16 (monthBuf);
624 fs.writeInt16 (dayBuf);
625 fs.writeInt16 (hourBuf);
626 fs.writeInt16 (minuteBuf);
627 fs.writeInt16 (secondBuf);
628 fs.writeFloat64 (calcTime);
629 kuint16 strlength = strlen (labelString);
630 fs.writeInt16 (strlength);
631 fs.write (labelString, strlength);
638 Array2dFile::labelAdd (const char* const lstr, double calc_time)
640 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
645 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
647 Array2dFileLabel label (type, lstr, calc_time);
654 Array2dFile::labelAdd (const Array2dFileLabel& label)
656 Array2dFileLabel* pLabel = new Array2dFileLabel(label);
658 m_labels.push_back (pLabel);
662 Array2dFile::labelsCopy (const Array2dFile& copyFile, const char* const pszId)
667 for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
668 Array2dFileLabel l (copyFile.labelGet (i));
669 std::string lstr = l.getLabelString();
671 l.setLabelString (lstr);
677 Array2dFile::arrayDataClear (void)
680 int columnSize = m_ny * m_pixelSize;
681 for (unsigned int ix = 0; ix < m_nx; ix++)
682 memset (m_arrayData[ix], 0, columnSize);
684 if (m_imaginaryArrayData) {
685 int columnSize = m_ny * m_pixelSize;
686 for (unsigned int ix = 0; ix < m_nx; ix++)
687 memset (m_arrayData[ix], 0, columnSize);
692 Array2dFile::printLabels (std::ostream& os) const
694 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
695 const Array2dFileLabel& label = **l;
703 Array2dFile::printLabelsBrief (std::ostream& os) const
705 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
706 const Array2dFileLabel& label = **l;
708 label.printBrief (os);
713 Array2dFile::printLabelsBrief (std::ostringstream& os) const
715 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
716 const Array2dFileLabel& label = **l;
718 label.printBrief (os);
723 const Array2dFileLabel&
724 Array2dFile::labelGet (int i) const