/*****************************************************************************
** FILE IDENTIFICATION
**
-** Name: array2dfile.cpp
+** Name: array2dfile.cpp
** Purpose: 2-dimension array file class
-** Programmer: Kevin Rosenberg
-** Date Started: June 2000
+** Programmer: Kevin Rosenberg
+** Date Started: June 2000
**
** This is part of the CTSim program
-** Copyright (c) 1983-2001 Kevin Rosenberg
-**
-** $Id$
+** Copyright (c) 1983-2009 Kevin Rosenberg
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License (version 2) as
m_second = lt->tm_sec;
}
-Array2dFileLabel::Array2dFileLabel()
+Array2dFileLabel::Array2dFileLabel()
{
init();
}
: m_strLabel (str)
{
init();
-
+
m_labelType = L_USER;
m_calcTime = ctime;
}
: m_strLabel (str)
{
init();
-
+
m_labelType = type;
m_calcTime = ctime;
}
{
}
-void
+void
Array2dFileLabel::setDateTime (int year, int month, int day, int hour, int minute, int second)
{
m_year = year;
m_second = second;
}
-void
+void
Array2dFileLabel::getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const
{
year = m_year;
second = m_second;
}
-const std::string&
+const std::string&
Array2dFileLabel::getDateString (void) const
{
char szDate [128];
m_strLabel = rhs.m_strLabel;
m_year = rhs.m_year; m_month = rhs.m_month; m_day = rhs.m_day;
m_hour = rhs.m_hour; m_minute = rhs.m_minute; m_second = rhs.m_second;
-
+
return (*this);
}
-void
+void
Array2dFile::allocArrays ()
{
- if (m_arrayData)
+ if (m_arrayData)
freeArray (m_arrayData);
if (m_imaginaryArrayData)
freeArray (m_imaginaryArrayData);
rppData[i] = new unsigned char [columnBytes];
}
-void
+void
Array2dFile::freeArrays ()
{
- if (m_arrayData)
+ if (m_arrayData)
freeArray (m_arrayData);
-
- if (m_imaginaryArrayData)
+
+ if (m_imaginaryArrayData)
freeArray (m_imaginaryArrayData);
-
+
}
void
Array2dFile::fileWrite (const char* const filename)
{
m_filename = filename;
-
+
frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::trunc | std::ios::binary);
if (fs.fail()) {
sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", m_filename.c_str());
}
if (! headerWrite(fs))
return false;
-
+
if (! arrayDataWrite (fs))
return false;
-
+
if (! labelsWrite (fs))
return false;
-
+
return true;
}
Array2dFile::fileRead (const char* const filename)
{
m_filename = filename;
-
+
frnetorderstream fs (m_filename.c_str(), std::ios::out | std::ios::in | std::ios::binary);
if (fs.fail()) {
sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", m_filename.c_str());
return false;
}
-
+
if (! headerRead(fs))
return false;
-
+
allocArrays ();
-
+
if (! arrayDataRead(fs))
return false;;
-
+
if (! labelsRead (fs))
return false;
-
+
return true;
}
m_axisIncrementY = incY;
}
-void
+void
Array2dFile::setAxisExtent (double minX, double maxX, double minY, double maxY)
{
m_axisExtentKnown = true;
sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
return false;
}
-
+
fs.seekg (0);
kuint16 file_signature;
-
+
fs.readInt16 (m_headersize);
fs.readInt16 (file_signature);
fs.readInt16 (m_pixelFormat);
fs.readFloat64 (m_maxY);
fs.readFloat64 (m_offsetPV);
fs.readFloat64 (m_scalePV);
-
+
int read_m_headersize = fs.tellg();
if (read_m_headersize != m_headersize) {
sys_error (ERR_WARNING, "Read m_headersize %d != file m_headersize %d", read_m_headersize, m_headersize);
sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, m_signature);
return false;
}
-
+
return ! fs.fail();
}
sys_error (ERR_WARNING, "Tried to write header with ! fs");
return false;
}
-
+
m_numFileLabels = m_labels.size();
-
+
fs.seekp (0);
fs.writeInt16 (m_headersize);
fs.writeInt16 (m_signature);
fs.writeFloat64 (m_maxY);
fs.writeFloat64 (m_offsetPV);
fs.writeFloat64 (m_scalePV);
-
+
m_headersize = static_cast<kuint16>(fs.tellp());
fs.seekp (0);
fs.writeInt16 (m_headersize);
-
+
return ! fs.fail();
}
sys_error (ERR_WARNING, "Tried to arrayDataWrite with !fs");
return false;
}
-
- if (! m_arrayData)
+
+ if (! m_arrayData)
return false;
-
+
fs.seekp (m_headersize);
int columnSize = m_ny * m_pixelSize;
for (unsigned int ix = 0; ix < m_nx; ix++) {
fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
ptrColumn += m_pixelSize;
}
- } else
+ } else
fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
}
if (m_dataType == DATA_TYPE_COMPLEX) {
fs.write (reinterpret_cast<const char*>(ptrColumn), m_pixelSize);
ptrColumn += m_pixelSize;
}
- } else
+ } else
fs.write (reinterpret_cast<const char*>(ptrColumn), columnSize);
}
}
-
+
return true;
}
sys_error (ERR_WARNING, "Tried to arrayDataRead with ! fs");
return false;
}
-
+
if (! m_arrayData)
return false;
-
+
fs.seekg (m_headersize);
int columnSize = m_ny * m_pixelSize;
for (unsigned int ix = 0; ix < m_nx; ix++) {
fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
ptrColumn += m_pixelSize;
- }
+ }
} else
fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
}
fs.read (reinterpret_cast<char*>(ptrColumn), m_pixelSize);
ConvertReverseNetworkOrder (ptrColumn, m_pixelSize);
ptrColumn += m_pixelSize;
- }
+ }
} else
fs.read (reinterpret_cast<char*>(ptrColumn), columnSize);
}
}
-
+
return true;
}
fs.seekg (pos);
if (fs.fail())
return false;
-
+
for (int i = 0; i < m_numFileLabels; i++) {
kuint16 labelType, year, month, day, hour, minute, second;
kfloat64 calcTime;
-
+
fs.readInt16 (labelType);
fs.readInt16 (year);
fs.readInt16 (month);
fs.readInt16 (minute);
fs.readInt16 (second);
fs.readFloat64 (calcTime);
-
+
kuint16 strLength;
fs.readInt16 (strLength);
char* pszLabelStr = new char [strLength+1];
fs.read (pszLabelStr, strLength);
pszLabelStr[strLength] = 0;
-
+
Array2dFileLabel* pLabel = new Array2dFileLabel (labelType, pszLabelStr, calcTime);
delete pszLabelStr;
-
+
pLabel->setDateTime (year, month, day, hour, minute, second);
m_labels.push_back (pLabel);
}
-
+
return true;
}
{
off_t pos = m_headersize + m_arraySize;
fs.seekp (pos);
-
+
for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
const Array2dFileLabel& label = **l;
kuint16 labelType = label.getLabelType();
const char* const labelString = label.getLabelString().c_str();
int year, month, day, hour, minute, second;
kuint16 yearBuf, monthBuf, dayBuf, hourBuf, minuteBuf, secondBuf;
-
+
label.getDateTime (year, month, day, hour, minute, second);
yearBuf = year; monthBuf = month; dayBuf = day;
hourBuf = hour; minuteBuf = minute; secondBuf = second;
-
+
fs.writeInt16 (labelType);
fs.writeInt16 (yearBuf);
fs.writeInt16 (monthBuf);
fs.writeInt16 (strlength);
fs.write (labelString, strlength);
}
-
+
return true;
}
Array2dFile::labelAdd (int type, const char* const lstr, double calc_time)
{
Array2dFileLabel label (type, lstr, calc_time);
-
+
labelAdd (label);
}
Array2dFile::labelAdd (const Array2dFileLabel& label)
{
Array2dFileLabel* pLabel = new Array2dFileLabel(label);
-
+
m_labels.push_back (pLabel);
}
}
}
-void
+void
Array2dFile::arrayDataClear (void)
{
if (m_arrayData) {
{
for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
const Array2dFileLabel& label = **l;
-
+
label.print (os);
os << std::endl;
}
{
for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
const Array2dFileLabel& label = **l;
-
+
label.printBrief (os);
}
}
{
for (constLabelIterator l = m_labels.begin(); l != m_labels.end(); l++) {
const Array2dFileLabel& label = **l;
-
+
label.printBrief (os);
}
}