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-2000 Kevin Rosenberg
12 ** $Id: array2dfile.cpp,v 1.17 2000/12/14 20:59:30 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"
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",
\r
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 (ostream& os) const
144 if (m_labelType == L_HISTORY) {
145 os << "History: " << endl;
146 os << " " << m_strLabel << endl;
147 os << " calc time = " << m_calcTime << " secs" << endl;
148 os << " Timestamp = " << getDateString() << endl;
149 } else if (m_labelType == L_USER) {
150 os << "Note: " << m_strLabel << endl;
151 os << " Timestamp = %s" << getDateString() << endl;
153 os << "Unknown (" << m_labelType << "): " << m_strLabel << endl;
154 os << " Timestamp = %s" << getDateString() << endl;
159 ///////////////////////////////////////////////////////////////////////////
160 // CLASS IMPLEMENTATION
163 // Purpose: Array2dFiles
164 ///////////////////////////////////////////////////////////////////////////
167 Array2dFile::Array2dFile (int x, int y, int pixelSize, int pixelFormat)
170 setArraySize (x, y, pixelSize, pixelFormat);
173 Array2dFile::~Array2dFile (void)
176 for (labelIterator l = m_labels.begin(); l != m_labels.end(); l++)
180 Array2dFile::Array2dFile (void)
186 Array2dFile::init (void)
189 m_pixelFormat = PIXEL_INVALID;
194 m_axisIncrementKnown = false;
195 m_axisIncrementX = m_axisIncrementY = 0;
196 m_axisExtentKnown = false;
197 m_minX = m_maxX = m_minY = m_maxY = 0;
204 Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat)
206 m_pixelSize = pixelSize;
207 m_pixelFormat = pixelFormat;
212 Array2dFile::setArraySize (int x, int y)
220 Array2dFile::allocArray (void)
225 m_arraySize = m_nx * m_ny * m_pixelSize;
226 m_arrayData = new unsigned char* [m_nx];
228 int columnBytes = m_ny * m_pixelSize;
229 for (unsigned int i = 0; i < m_nx; i++)
230 m_arrayData[i] = new unsigned char [columnBytes];
234 Array2dFile::freeArray (void)
237 for (unsigned int i = 0; i < m_nx; i++)
238 delete m_arrayData[i];
245 Array2dFile::fileWrite (const string& filename)
247 return fileWrite (filename.c_str());
251 Array2dFile::fileWrite (const char* const filename)
253 m_filename = filename;
255 frnetorderstream fs (m_filename.c_str(), ios::out | ios::in | ios::trunc | ios::binary);
257 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
260 if (! headerWrite(fs))
263 if (! arrayDataWrite (fs))
266 if (! labelsWrite (fs))
273 Array2dFile::fileRead (const string& filename)
275 return fileRead (filename.c_str());
279 Array2dFile::fileRead (const char* const filename)
281 m_filename = filename;
284 frnetorderstream fs (m_filename.c_str(), ios::out | ios::in | ios::binary);
\r
286 frnetorderstream fs (m_filename.c_str(), ios::out | ios::in | ios::binary | ios::nocreate);
\r
289 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
293 if (! headerRead(fs))
298 if (! arrayDataRead(fs))
301 if (! labelsRead (fs))
308 Array2dFile::setAxisIncrement (double incX, double incY)
310 m_axisIncrementKnown = true;
311 m_axisIncrementX = incX;
312 m_axisIncrementY = incY;
316 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
318 m_axisExtentKnown = true;
326 Array2dFile::headerRead (frnetorderstream& fs)
329 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
334 kuint16 file_signature;
336 fs.readInt16 (m_headersize);
337 fs.readInt16 (file_signature);
338 fs.readInt16 (m_pixelFormat);
339 fs.readInt16 (m_pixelSize);
340 fs.readInt16 (m_numFileLabels);
343 fs.readInt16 (m_axisIncrementKnown);
344 fs.readFloat64 (m_axisIncrementX);
345 fs.readFloat64 (m_axisIncrementY);
346 fs.readInt16 (m_axisExtentKnown);
347 fs.readFloat64 (m_minX);
348 fs.readFloat64 (m_maxX);
349 fs.readFloat64 (m_minY);
350 fs.readFloat64 (m_maxY);
351 fs.readFloat64 (m_offsetPV);
352 fs.readFloat64 (m_scalePV);
354 int read_m_headersize = fs.tellg();
355 if (read_m_headersize != m_headersize) {
356 sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
359 if (file_signature != m_signature) {
360 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
369 Array2dFile::headerWrite (frnetorderstream& fs)
372 sys_error (ERR_WARNING, "Tried to write header with ! fs");
376 m_numFileLabels = m_labels.size();
379 fs.writeInt16 (m_headersize);
380 fs.writeInt16 (m_signature);
381 fs.writeInt16 (m_pixelFormat);
382 fs.writeInt16 (m_pixelSize);
383 fs.writeInt16 (m_numFileLabels);
384 fs.writeInt32 (m_nx);
385 fs.writeInt32 (m_ny);
386 fs.writeInt16 (m_axisIncrementKnown);
387 fs.writeFloat64 (m_axisIncrementX);
388 fs.writeFloat64 (m_axisIncrementY);
389 fs.writeInt16 (m_axisExtentKnown);
390 fs.writeFloat64 (m_minX);
391 fs.writeFloat64 (m_maxX);
392 fs.writeFloat64 (m_minY);
393 fs.writeFloat64 (m_maxY);
394 fs.writeFloat64 (m_offsetPV);
395 fs.writeFloat64 (m_scalePV);
397 m_headersize = static_cast<kuint16>(fs.tellp());
399 fs.writeInt16 (m_headersize);
406 Array2dFile::arrayDataWrite (frnetorderstream& fs)
409 sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
416 fs.seekp (m_headersize);
417 int columnSize = m_ny * m_pixelSize;
418 for (unsigned int ix = 0; ix < m_nx; ix++) {
419 unsigned char* ptrColumn = m_arrayData[ix];
420 if (NativeBigEndian()) {
421 for (unsigned int iy = 0; iy < m_ny; iy++) {
422 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
423 fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
424 ptrColumn += m_pixelSize;
427 fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
435 Array2dFile::arrayDataRead (frnetorderstream& fs)
438 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
445 fs.seekg (m_headersize);
446 int columnSize = m_ny * m_pixelSize;
447 for (unsigned int ix = 0; ix < m_nx; ix++) {
448 unsigned char* ptrColumn = m_arrayData[ix];
449 if (NativeBigEndian()) {
450 for (unsigned int iy = 0; iy < m_ny; iy++) {
451 fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
452 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
453 ptrColumn += m_pixelSize;
456 fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
463 Array2dFile::labelsRead (frnetorderstream& fs)
465 off_t pos = m_headersize + m_arraySize;
470 for (int i = 0; i < m_numFileLabels; i++) {
471 kuint16 labelType, year, month, day, hour, minute, second;
474 fs.readInt16 (labelType);
476 fs.readInt16 (month);
479 fs.readInt16 (minute);
480 fs.readInt16 (second);
481 fs.readFloat64 (calcTime);
484 fs.readInt16 (strLength);
485 char* pszLabelStr = new char [strLength+1];
486 fs.read (pszLabelStr, strLength);
487 pszLabelStr[strLength] = 0;
489 Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime);
490 delete pszLabelStr;
\r
492 pLabel->setDateTime (year, month, day, hour, minute, second);
493 m_labels.push_back (pLabel);
\r
500 Array2dFile::labelsWrite (frnetorderstream& fs)
502 off_t pos = m_headersize + m_arraySize;
505 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
506 const Array2dFileLabel& label = **l;
507 kuint16 labelType = label.getLabelType();
508 kfloat64 calcTime = label.getCalcTime();
509 const char* const labelString = label.getLabelString().c_str();
510 int year, month, day, hour, minute, second;
511 kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
513 label.getDateTime (year, month, day, hour, minute, second);
514 yearBuf = year; monthBuf = month; dayBuf = day;
515 hourBuf = hour; minuteBuf = minute; secondBuf = second;
517 fs.writeInt16 (labelType);
518 fs.writeInt16 (yearBuf);
519 fs.writeInt16 (monthBuf);
520 fs.writeInt16 (dayBuf);
521 fs.writeInt16 (hourBuf);
522 fs.writeInt16 (minuteBuf);
523 fs.writeInt16 (secondBuf);
524 fs.writeFloat64 (calcTime);
525 kuint16 strlength = strlen (labelString);
526 fs.writeInt16 (strlength);
527 fs.write (labelString, strlength);
534 Array2dFile::labelAdd (const char* const lstr, double calc_time)
536 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
541 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
543 Array2dFileLabel label (type, lstr, calc_time);
550 Array2dFile::labelAdd (const Array2dFileLabel& label)
552 Array2dFileLabel* pLabel = new Array2dFileLabel(label);
554 m_labels.push_back (pLabel);
558 Array2dFile::labelsCopy (Array2dFile& copyFile, const char* const pszId)
563 for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
564 Array2dFileLabel l (copyFile.labelGet (i));
565 string lstr = l.getLabelString();
567 l.setLabelString (lstr);
573 Array2dFile::arrayDataClear (void)
576 int columnSize = m_ny * m_pixelSize;
577 for (unsigned int ix = 0; ix < m_nx; ix++)
578 memset (m_arrayData[ix], 0, columnSize);
583 Array2dFile::printLabels (ostream& os) const
585 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
586 const Array2dFileLabel& label = **l;
594 const Array2dFileLabel&
595 Array2dFile::labelGet (int i) const