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.8 2000/07/29 19:50:08 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 ///////////////////////////////////////////////////////////////////////////
36 // CLASS IMPLEMENTATION
38 // Name: Array2dFileLabel
39 // Purpose: Labels for Array2dFiles
40 ///////////////////////////////////////////////////////////////////////////
43 Array2dFileLabel::init (void)
46 m_labelType = L_EMPTY;
48 tm* lt = localtime(&t);
53 m_minute = lt->tm_min;
54 m_second = lt->tm_sec;
57 Array2dFileLabel::Array2dFileLabel()
62 Array2dFileLabel::Array2dFileLabel(const char* const str, double ctime = 0.)
71 Array2dFileLabel::Array2dFileLabel(const int type, const char* const str, double ctime = 0.)
80 Array2dFileLabel::~Array2dFileLabel()
85 Array2dFileLabel::setDateTime (int year, int month, int day, int hour, int minute, int second)
96 Array2dFileLabel::getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const
107 Array2dFileLabel::getDateString (void) const
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();
116 Array2dFileLabel::Array2dFileLabel (const Array2dFileLabel& rhs)
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;
126 Array2dFileLabel::operator= (const Array2dFileLabel& rhs)
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;
138 Array2dFileLabel::print (ostream& os) const
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;
149 os << "Unknown (" << m_labelType << "): " << m_strLabel << endl;
150 os << " Timestamp = %s" << getDateString() << endl;
155 ///////////////////////////////////////////////////////////////////////////
156 // CLASS IMPLEMENTATION
159 // Purpose: Array2dFiles
160 ///////////////////////////////////////////////////////////////////////////
163 Array2dFile::Array2dFile (int x, int y, int pixelSize, int pixelFormat)
166 setArraySize (x, y, pixelSize, pixelFormat);
169 Array2dFile::~Array2dFile (void)
172 for (labelIterator l = m_labels.begin(); l != m_labels.end(); l++)
176 Array2dFile::Array2dFile (void)
182 Array2dFile::init (void)
185 m_pixelFormat = PIXEL_INVALID;
190 m_axisIncrementKnown = false;
191 m_axisIncrementX = m_axisIncrementY = 0;
192 m_axisExtentKnown = false;
193 m_minX = m_maxX = m_minY = m_maxY = 0;
200 Array2dFile::setArraySize (int x, int y, int pixelSize, int pixelFormat)
202 m_pixelSize = pixelSize;
203 m_pixelFormat = pixelFormat;
208 Array2dFile::setArraySize (int x, int y)
216 Array2dFile::allocArray (void)
221 m_arraySize = m_nx * m_ny * m_pixelSize;
222 m_arrayData = new unsigned char* [m_nx];
224 int columnBytes = m_ny * m_pixelSize;
225 for (unsigned int i = 0; i < m_nx; i++)
226 m_arrayData[i] = new unsigned char [columnBytes];
230 Array2dFile::freeArray (void)
233 for (unsigned int i = 0; i < m_nx; i++)
234 delete m_arrayData[i];
241 Array2dFile::fileWrite (const string& filename)
243 return fileWrite (filename.c_str());
247 Array2dFile::fileWrite (const char* const filename)
249 m_filename = filename;
251 frnetorderstream fs (m_filename.c_str(), ios::out | ios::in | ios::trunc | ios::binary);
253 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
256 if (! headerWrite(fs))
259 if (! arrayDataWrite (fs))
262 if (! labelsWrite (fs))
269 Array2dFile::fileRead (const string& filename)
271 return fileRead (filename.c_str());
275 Array2dFile::fileRead (const char* const filename)
277 m_filename = filename;
279 frnetorderstream fs (m_filename.c_str(), ios::out | ios::in | ios::binary | ios::nocreate);
281 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
285 if (! headerRead(fs))
290 if (! arrayDataRead(fs))
293 if (! labelsRead (fs))
300 Array2dFile::setAxisIncrement (double incX, double incY)
302 m_axisIncrementKnown = true;
303 m_axisIncrementX = incX;
304 m_axisIncrementY = incY;
308 Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
310 m_axisExtentKnown = true;
318 Array2dFile::headerRead (frnetorderstream& fs)
321 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
326 kuint16 file_signature;
328 fs.readInt16 (m_headersize);
329 fs.readInt16 (file_signature);
330 fs.readInt16 (m_pixelFormat);
331 fs.readInt16 (m_pixelSize);
332 fs.readInt16 (m_numFileLabels);
335 fs.readInt16 (m_axisIncrementKnown);
336 fs.readFloat64 (m_axisIncrementX);
337 fs.readFloat64 (m_axisIncrementY);
338 fs.readInt16 (m_axisExtentKnown);
339 fs.readFloat64 (m_minX);
340 fs.readFloat64 (m_maxX);
341 fs.readFloat64 (m_minY);
342 fs.readFloat64 (m_maxY);
343 fs.readFloat64 (m_offsetPV);
344 fs.readFloat64 (m_scalePV);
346 int read_m_headersize = fs.tellg();
347 if (read_m_headersize != m_headersize) {
348 sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
351 if (file_signature != m_signature) {
352 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
361 Array2dFile::headerWrite (frnetorderstream& fs)
364 sys_error (ERR_WARNING, "Tried to write header with ! fs");
368 m_numFileLabels = m_labels.size();
371 fs.writeInt16 (m_headersize);
372 fs.writeInt16 (m_signature);
373 fs.writeInt16 (m_pixelFormat);
374 fs.writeInt16 (m_pixelSize);
375 fs.writeInt16 (m_numFileLabels);
376 fs.writeInt32 (m_nx);
377 fs.writeInt32 (m_ny);
378 fs.writeInt16 (m_axisIncrementKnown);
379 fs.writeFloat64 (m_axisIncrementX);
380 fs.writeFloat64 (m_axisIncrementY);
381 fs.writeInt16 (m_axisExtentKnown);
382 fs.writeFloat64 (m_minX);
383 fs.writeFloat64 (m_maxX);
384 fs.writeFloat64 (m_minY);
385 fs.writeFloat64 (m_maxY);
386 fs.writeFloat64 (m_offsetPV);
387 fs.writeFloat64 (m_scalePV);
389 m_headersize = fs.tellp();
391 fs.writeInt16 (m_headersize);
398 Array2dFile::arrayDataWrite (frnetorderstream& fs)
401 sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
408 fs.seekp (m_headersize);
409 int columnSize = m_ny * m_pixelSize;
410 for (unsigned int ix = 0; ix < m_nx; ix++) {
411 unsigned char* ptrColumn = m_arrayData[ix];
412 if (NativeBigEndian()) {
413 for (unsigned int iy = 0; iy < m_ny; iy++) {
414 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
415 fs.write (ptrColumn, m_pixelSize);
416 ptrColumn += m_pixelSize;
419 fs.write (ptrColumn, columnSize);
427 Array2dFile::arrayDataRead (frnetorderstream& fs)
430 sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
437 fs.seekg (m_headersize);
438 int columnSize = m_ny * m_pixelSize;
439 for (unsigned int ix = 0; ix < m_nx; ix++) {
440 unsigned char* ptrColumn = m_arrayData[ix];
441 if (NativeBigEndian()) {
442 for (unsigned int iy = 0; iy < m_ny; iy++) {
443 fs.read (ptrColumn, m_pixelSize);
444 ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
445 ptrColumn += m_pixelSize;
448 fs.read (ptrColumn, columnSize);
455 Array2dFile::labelsRead (frnetorderstream& fs)
457 off_t pos = m_headersize + m_arraySize;
462 for (int i = 0; i < m_numFileLabels; i++) {
463 kuint16 labelType, year, month, day, hour, minute, second;
466 fs.readInt16 (labelType);
468 fs.readInt16 (month);
471 fs.readInt16 (minute);
472 fs.readInt16 (second);
473 fs.readFloat64 (calcTime);
476 fs.readInt16 (strLength);
477 char labelStr [strLength+1];
478 fs.read (labelStr, strLength);
479 labelStr[strLength] = 0;
481 Array2dFileLabel* pLabel = new Array2dFileLabel(labelType, labelStr, calcTime);
482 pLabel->setDateTime (year, month, day, hour, minute, second);
483 m_labels.push_back (pLabel);
490 Array2dFile::labelsWrite (frnetorderstream& fs)
492 off_t pos = m_headersize + m_arraySize;
495 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
496 const Array2dFileLabel& label = **l;
497 kuint16 labelType = label.getLabelType();
498 kfloat64 calcTime = label.getCalcTime();
499 const char* const labelString = label.getLabelString().c_str();
500 int year, month, day, hour, minute, second;
501 kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
503 label.getDateTime (year, month, day, hour, minute, second);
504 yearBuf = year; monthBuf = month; dayBuf = day;
505 hourBuf = hour; minuteBuf = minute; secondBuf = second;
507 fs.writeInt16 (labelType);
508 fs.writeInt16 (yearBuf);
509 fs.writeInt16 (monthBuf);
510 fs.writeInt16 (dayBuf);
511 fs.writeInt16 (hourBuf);
512 fs.writeInt16 (minuteBuf);
513 fs.writeInt16 (secondBuf);
514 fs.writeFloat64 (calcTime);
515 kuint16 strlength = strlen (labelString);
516 fs.writeInt16 (strlength);
517 fs.write (labelString, strlength);
524 Array2dFile::labelAdd (const char* const lstr, double calc_time=0.)
526 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
531 Array2dFile::labelAdd (int type, const char* const lstr, double calc_time=0.)
533 Array2dFileLabel label (type, lstr, calc_time);
540 Array2dFile::labelAdd (const Array2dFileLabel& label)
542 Array2dFileLabel* pLabel = new Array2dFileLabel(label);
544 m_labels.push_back (pLabel);
548 Array2dFile::labelsCopy (Array2dFile& copyFile, const char* const idStr)
551 for (unsigned int i = 0; i < copyFile.getNumLabels(); i++) {
552 Array2dFileLabel l (copyFile.labelGet (i));
553 string lstr = l.getLabelString();
555 l.setLabelString (lstr);
561 Array2dFile::arrayDataClear (void)
564 int columnSize = m_ny * m_pixelSize;
565 for (unsigned int ix = 0; ix < m_nx; ix++)
566 memset (m_arrayData[ix], 0, columnSize);
571 Array2dFile::printLabels (ostream& os) const
573 for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
574 const Array2dFileLabel& label = **l;
582 const Array2dFileLabel&
583 Array2dFile::labelGet (int i) const