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-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
26 #include "array2dfile.h"
34 const kuint16 Array2dFile::m_signature = ('I'*256+'F');
36 ///////////////////////////////////////////////////////////////////////////
37 // CLASS IMPLEMENTATION
39 // Name: Array2dFileLabel
40 // Purpose: Labels for Array2dFiles
41 ///////////////////////////////////////////////////////////////////////////
44 Array2dFileLabel::init (void)
47 m_labelType = L_EMPTY;
49 tm* lt = localtime(&t);
54 m_minute = lt->tm_min;
55 m_second = lt->tm_sec;
58 Array2dFileLabel::Array2dFileLabel()
63 Array2dFileLabel::Array2dFileLabel(const char* const str, double ctime)
72 Array2dFileLabel::Array2dFileLabel(const int type, const char* const str, double ctime)
81 Array2dFileLabel::~Array2dFileLabel()
86 Array2dFileLabel::setDateTime (int year, int month, int day, int hour, int minute, int second)
97 Array2dFileLabel::getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const
108 Array2dFileLabel::getDateString (void) const
111 snprintf (szDate, sizeof(szDate), "%2d/%02d/%4d %02d:%02d:%02d",
112 m_month + 1, m_day, m_year + 1900, m_hour, m_minute, m_second);
118 Array2dFileLabel::Array2dFileLabel (const Array2dFileLabel& rhs)
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;
128 Array2dFileLabel::operator= (const Array2dFileLabel& rhs)
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;
140 Array2dFileLabel::print (std::ostream& os) const
142 if (m_labelType == L_HISTORY) {
143 os << "History: " << std::endl;
144 os << " " << m_strLabel << std::endl;
145 os << " calc time = " << m_calcTime << " secs" << std::endl;
146 os << " Timestamp = " << getDateString() << std::endl;
147 } else if (m_labelType == L_USER) {
148 os << "Note: " << m_strLabel << std::endl;
149 os << " Timestamp = %s" << getDateString() << std::endl;
151 os << "Unknown (" << m_labelType << "): " << m_strLabel << std::endl;
152 os << " Timestamp = %s" << getDateString() << std::endl;
157 Array2dFileLabel::printBrief (std::ostream& os) const
159 if (m_labelType == L_HISTORY) {
162 os << m_calcTime << " secs, ";
163 os << getDateString() << "): " << m_strLabel << std::endl;
164 } else if (m_labelType == L_USER) {
165 os << "Note (" << getDateString() << "): " << m_strLabel << std::endl;
167 os << "Unknown (" << getDateString() << "): " << m_strLabel << std::endl;
172 Array2dFileLabel::printBrief (std::ostringstream& os) const
174 if (m_labelType == L_HISTORY) {
177 os << m_calcTime << " secs, ";
178 os << getDateString().c_str() << "): " << m_strLabel.c_str() << "\n";
179 } else if (m_labelType == L_USER) {
180 os << "Note (" << getDateString() << "): " << m_strLabel << "\n";
182 os << "Unknown (" << getDateString() << "): " << m_strLabel << "\n";
187 ///////////////////////////////////////////////////////////////////////////
188 // CLASS IMPLEMENTATION
191 // Purpose: Array2dFiles
192 ///////////////////////////////////////////////////////////////////////////
195 Array2dFile::Array2dFile (int x, int y, int pixelSize, int pixelFormat, int dataType)
198 setArraySize (x, y, pixelSize, pixelFormat, dataType);
201 Array2dFile::~Array2dFile (void)
204 for (labelIterator l = m_labels.begin(); l != m_labels.end(); l++)
208 Array2dFile::Array2dFile (void)
214 Array2dFile::init (void)
217 m_pixelFormat = PIXEL_INVALID;
219 m_imaginaryArrayData = NULL;
220 m_dataType = DATA_TYPE_INVALID;
224 m_axisIncrementKnown = false;
225 m_axisIncrementX = m_axisIncrementY = 0;
226 m_axisExtentKnown = false;
227 m_minX = m_maxX = m_minY = m_maxY = 0;
234 Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat, int dataType)
236 m_pixelSize = pixelSize;
237 m_pixelFormat = pixelFormat;
238 m_dataType = dataType;
243 Array2dFile::setArraySize (int x, int y)
251 Array2dFile::reallocComplexToReal ()
253 if (m_dataType != DATA_TYPE_COMPLEX)
256 freeArray (m_imaginaryArrayData);
257 m_dataType = DATA_TYPE_REAL;
264 Array2dFile::reallocRealToComplex ()
266 if (m_dataType != DATA_TYPE_REAL)
269 allocArray (m_imaginaryArrayData);
270 m_dataType = DATA_TYPE_COMPLEX;
278 Array2dFile::allocArrays ()
281 freeArray (m_arrayData);
282 if (m_imaginaryArrayData)
283 freeArray (m_imaginaryArrayData);
285 allocArray (m_arrayData);
286 if (m_dataType == DATA_TYPE_COMPLEX)
287 allocArray (m_imaginaryArrayData);
291 Array2dFile::allocArray (unsigned char**& rppData)
293 m_arraySize = m_nx * m_ny * m_pixelSize;
294 rppData = new unsigned char* [m_nx];
295 int columnBytes = m_ny * m_pixelSize;
296 for (unsigned int i = 0; i < m_nx; i++)
297 rppData[i] = new unsigned char [columnBytes];
301 Array2dFile::freeArrays ()
304 freeArray (m_arrayData);
306 if (m_imaginaryArrayData)
307 freeArray (m_imaginaryArrayData);
312 Array2dFile::freeArray (unsigned char**& rppData)
314 for (unsigned int i = 0; i < m_nx; i++)
322 Array2dFile::fileWrite (const std::string& filename)
324 return fileWrite (filename.c_str());
328 Array2dFile::fileWrite (const char* const filename)
330 m_filename = filename;
332 frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::trunc | std::ios::binary);
334 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
337 if (! headerWrite(fs))
340 if (! arrayDataWrite (fs))
343 if (! labelsWrite (fs))
350 Array2dFile::fileRead (const std::string& filename)
352 return fileRead (filename.c_str());
356 Array2dFile::fileRead (const char* const filename)
358 m_filename = filename;
360 frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary);
363 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
367 if (! headerRead(fs))
372 if (! arrayDataRead(fs))
375 if (! labelsRead (fs))
382 Array2dFile::setAxisIncrement (double incX, double incY)
384 m_axisIncrementKnown = true;
385 m_axisIncrementX = incX;
386 m_axisIncrementY = incY;
390 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
392 m_axisExtentKnown = true;
400 Array2dFile::headerRead (frnetorderstream& fs)
403 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
408 kuint16 file_signature;
410 fs.readInt16 (m_headersize);
411 fs.readInt16 (file_signature);
412 fs.readInt16 (m_pixelFormat);
413 fs.readInt16 (m_pixelSize);
414 fs.readInt16 (m_numFileLabels);
417 fs.readInt16 (m_dataType);
418 fs.readInt16 (m_axisIncrementKnown);
419 fs.readFloat64 (m_axisIncrementX);
420 fs.readFloat64 (m_axisIncrementY);
421 fs.readInt16 (m_axisExtentKnown);
422 fs.readFloat64 (m_minX);
423 fs.readFloat64 (m_maxX);
424 fs.readFloat64 (m_minY);
425 fs.readFloat64 (m_maxY);
426 fs.readFloat64 (m_offsetPV);
427 fs.readFloat64 (m_scalePV);
429 int read_m_headersize = fs.tellg();
430 if (read_m_headersize != m_headersize) {
431 sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
434 if (file_signature != m_signature) {
435 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
444 Array2dFile::headerWrite (frnetorderstream& fs)
447 sys_error (ERR_WARNING, "Tried to write header with ! fs");
451 m_numFileLabels = m_labels.size();
454 fs.writeInt16 (m_headersize);
455 fs.writeInt16 (m_signature);
456 fs.writeInt16 (m_pixelFormat);
457 fs.writeInt16 (m_pixelSize);
458 fs.writeInt16 (m_numFileLabels);
459 fs.writeInt32 (m_nx);
460 fs.writeInt32 (m_ny);
461 fs.writeInt16 (m_dataType);
462 fs.writeInt16 (m_axisIncrementKnown);
463 fs.writeFloat64 (m_axisIncrementX);
464 fs.writeFloat64 (m_axisIncrementY);
465 fs.writeInt16 (m_axisExtentKnown);
466 fs.writeFloat64 (m_minX);
467 fs.writeFloat64 (m_maxX);
468 fs.writeFloat64 (m_minY);
469 fs.writeFloat64 (m_maxY);
470 fs.writeFloat64 (m_offsetPV);
471 fs.writeFloat64 (m_scalePV);
473 m_headersize = static_cast<kuint16>(fs.tellp());
475 fs.writeInt16 (m_headersize);
482 Array2dFile::arrayDataWrite (frnetorderstream& fs)
485 sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
492 fs.seekp (m_headersize);
493 int columnSize = m_ny * m_pixelSize;
494 for (unsigned int ix = 0; ix < m_nx; ix++) {
495 unsigned char* ptrColumn = m_arrayData[ix];
496 if (NativeBigEndian()) {
497 for (unsigned int iy = 0; iy < m_ny; iy++) {
498 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
499 fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
500 ptrColumn += m_pixelSize;
503 fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
505 if (m_dataType == DATA_TYPE_COMPLEX) {
506 for (unsigned int ix = 0; ix < m_nx; ix++) {
507 unsigned char* ptrColumn = m_imaginaryArrayData[ix];
508 if (NativeBigEndian()) {
509 for (unsigned int iy = 0; iy < m_ny; iy++) {
510 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
511 fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
512 ptrColumn += m_pixelSize;
515 fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
524 Array2dFile::arrayDataRead (frnetorderstream& fs)
527 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
534 fs.seekg (m_headersize);
535 int columnSize = m_ny * m_pixelSize;
536 for (unsigned int ix = 0; ix < m_nx; ix++) {
537 unsigned char* ptrColumn = m_arrayData[ix];
538 if (NativeBigEndian()) {
539 for (unsigned int iy = 0; iy < m_ny; iy++) {
540 fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
541 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
542 ptrColumn += m_pixelSize;
545 fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
547 if (m_dataType == DATA_TYPE_COMPLEX) {
548 for (unsigned int ix = 0; ix < m_nx; ix++) {
549 unsigned char* ptrColumn = m_imaginaryArrayData[ix];
550 if (NativeBigEndian()) {
551 for (unsigned int iy = 0; iy < m_ny; iy++) {
552 fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
553 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
554 ptrColumn += m_pixelSize;
557 fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
565 Array2dFile::labelsRead (frnetorderstream& fs)
567 off_t pos = m_headersize + m_arraySize;
572 for (int i = 0; i < m_numFileLabels; i++) {
573 kuint16 labelType, year, month, day, hour, minute, second;
576 fs.readInt16 (labelType);
578 fs.readInt16 (month);
581 fs.readInt16 (minute);
582 fs.readInt16 (second);
583 fs.readFloat64 (calcTime);
586 fs.readInt16 (strLength);
587 char* pszLabelStr = new char [strLength+1];
588 fs.read (pszLabelStr, strLength);
589 pszLabelStr[strLength] = 0;
591 Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime);
594 pLabel->setDateTime (year, month, day, hour, minute, second);
595 m_labels.push_back (pLabel);
602 Array2dFile::labelsWrite (frnetorderstream& fs)
604 off_t pos = m_headersize + m_arraySize;
607 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
608 const Array2dFileLabel& label = **l;
609 kuint16 labelType = label.getLabelType();
610 kfloat64 calcTime = label.getCalcTime();
611 const char* const labelString = label.getLabelString().c_str();
612 int year, month, day, hour, minute, second;
613 kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
615 label.getDateTime (year, month, day, hour, minute, second);
616 yearBuf = year; monthBuf = month; dayBuf = day;
617 hourBuf = hour; minuteBuf = minute; secondBuf = second;
619 fs.writeInt16 (labelType);
620 fs.writeInt16 (yearBuf);
621 fs.writeInt16 (monthBuf);
622 fs.writeInt16 (dayBuf);
623 fs.writeInt16 (hourBuf);
624 fs.writeInt16 (minuteBuf);
625 fs.writeInt16 (secondBuf);
626 fs.writeFloat64 (calcTime);
627 kuint16 strlength = strlen (labelString);
628 fs.writeInt16 (strlength);
629 fs.write (labelString, strlength);
636 Array2dFile::labelAdd (const char* const lstr, double calc_time)
638 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
643 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
645 Array2dFileLabel label (type, lstr, calc_time);
652 Array2dFile::labelAdd (const Array2dFileLabel& label)
654 Array2dFileLabel* pLabel = new Array2dFileLabel(label);
656 m_labels.push_back (pLabel);
660 Array2dFile::labelsCopy (const Array2dFile& copyFile, const char* const pszId)
665 for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
666 Array2dFileLabel l (copyFile.labelGet (i));
667 std::string lstr = l.getLabelString();
669 l.setLabelString (lstr);
675 Array2dFile::arrayDataClear (void)
678 int columnSize = m_ny * m_pixelSize;
679 for (unsigned int ix = 0; ix < m_nx; ix++)
680 memset (m_arrayData[ix], 0, columnSize);
682 if (m_imaginaryArrayData) {
683 int columnSize = m_ny * m_pixelSize;
684 for (unsigned int ix = 0; ix < m_nx; ix++)
685 memset (m_arrayData[ix], 0, columnSize);
690 Array2dFile::printLabels (std::ostream& os) const
692 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
693 const Array2dFileLabel& label = **l;
701 Array2dFile::printLabelsBrief (std::ostream& os) const
703 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
704 const Array2dFileLabel& label = **l;
706 label.printBrief (os);
711 Array2dFile::printLabelsBrief (std::ostringstream& os) const
713 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
714 const Array2dFileLabel& label = **l;
716 label.printBrief (os);
721 const Array2dFileLabel&
722 Array2dFile::labelGet (int i) const