-class Array2dFileLabel
-{
-private:
- void init (void);
- Array2dFileLabel (const Array2dFileLabel&);
- Array2dFileLabel& operator= (const Array2dFileLabel&);
-
-public:
- kfloat64 m_calcTime;
- kuint16 m_labelType;
- kuint16 m_year;
- kuint16 m_month;
- kuint16 m_day;
- kuint16 m_hour;
- kuint16 m_minute;
- kuint16 m_second;
- string m_strLabel;
- mutable string m_strDate;
-
- static const int L_EMPTY = 0;
- static const int L_HISTORY = 1;
- static const int L_USER = 2;
-
- Array2dFileLabel();
-
- Array2dFileLabel(const char* const str, double ctime = 0.);
-
- Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
-
- ~Array2dFileLabel();
-
- const string& getLabelString (void) const
- { return m_strLabel; }
-
- kfloat64 getCalcTime (void) const
- { return m_calcTime; }
-
- kfloat64 getLabelType (void) const
- { return m_labelType; }
-
- string& setLabelString (const char* const str)
- { m_strLabel = str; return (m_strLabel); }
-
- string& setLabelString (const string& str)
- { m_strLabel = str; return (m_strLabel); }
-
- const string& getDateString () const;
-};
-
-
-template<class T>
-class Array2dFile
-{
-private:
- void init (void);
- kuint16 signature;
- kuint16 num_labels;
- kuint16 headersize;
- string filename;
- int file_id;
- iostream *io;
- bool bHeaderWritten;
- bool bDataWritten;
-
- bool headerWrite (void);
-
- bool headerRead (void);
-
- bool arrayDataRead (void);
-
- bool labelSeek (unsigned int label_num);
-
- kuint16 mPixelSize;
- kuint16 axis_increment_known;
- kfloat64 mIncX, mIncY;
- kuint16 axis_extent_known;
- kfloat64 mMinX, mMaxX, mMinY, mMaxY;
- kfloat64 mOffsetPV, mScalePV;
- kuint16 mPixelType;
- kuint32 mNX;
- kuint32 mNY;
-
- Array2dFile (const Array2dFile&);
- Array2dFile& operator= (const Array2dFile&);
-
-public:
-
- Array2d<T> array;
-
- static const int INT8 = 1;
- static const int UINT8 = 2;
- static const int INT16 = 3;
- static const int UINT16 = 4;
- static const int INT32 = 5;
- static const int UINT32 = 6;
- static const int FLOAT32 = 7;
- static const int FLOAT64 = 8;
-
- Array2dFile (unsigned int nx, unsigned int ny);
- Array2dFile (const char* const filename);
- Array2dFile (const char* const filename, unsigned int nx, unsigned int ny);
- ~Array2dFile ();
-
- unsigned int getNumLabels (void) const
- { return num_labels; }
-
- bool labelRead (Array2dFileLabel& label, unsigned int label_num);
-
- void labelAdd (const Array2dFileLabel& label);
-
- void labelAdd (const char* const m_strLabel, double calc_time=0.);
-
- void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
-
- void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
-
- void fileClose (void);
-
- void setPixelType (int type)
- { mPixelType = type; }
-
- kuint32 nx (void) const
- { return mNX; }
-
- kuint32 ny (void) const
- { return mNY; }
-
- void setAxisIncrement (double mIncX, double mIncY);
-
- void setAxisExtent (double mMinX, double mMaxX, double mMinY, double mMaxY);
-
- void getPixelValueRange (T& pvmin, T& pvmax) const;
-
- void doPixelOffsetScale (double offset, double scale);
-
- T** getArray (void) const
- { return (array.getArray()); }
-
- bool arrayDataWrite (void);
-
- void arrayDataClear (void);
-
- bool fileRead (void);
-
- bool fileCreate (void);
-
- const string& GetFilename (void) const
- { return filename; }
-};
-
-
-template<class T>
-Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
-{
- init();
- mNX = x;
- mNY = y;
- array.initSetSize(mNX, mNY);
-}
-
-template<class T>
-Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
- : filename(str)
-{
- init();
- mNX = x;
- mNY = y;
- array.initSetSize(mNX, mNY);
-}
-
-template<class T>
-Array2dFile<T>::Array2dFile (const char * const str)
- : filename(str)
-{
- init();
-}
-
-template<class T>
-void
-Array2dFile<T>::init (void)
-{
- mPixelSize = sizeof(T);
- signature = ('I' * 256 + 'F');
- file_id = -1;
- mNX = 0;
- mNY = 0;
- headersize = 0;
- num_labels = 0;
- axis_increment_known = false;
- axis_extent_known = false;
- mIncX = mMinY = 0;
- mMinX = mMaxX = mMinY = mMaxY = 0;
- mOffsetPV = 0;
- mScalePV = 1;
- io = NULL;
-
-#if 0
- const type_info& t_id = typeid(T);
- cout << t_id.name() << endl;
- const type_info& comp_id = typeid(T);
-
- if (t_id == comp_id)
- mPixelType = FLOAT64;
- else if (t_id == typeid(kfloat32))
- mPixelType = FLOAT32;
- else if (t_id == typeid(kint32))
- mPixelType = INT32;
- else if (t_id == typeid(kuint32))
- mPixelType = UINT32;
- else if (t_id == typeid(kint16))
- mPixelType = INT16;
- else if (t_id == typeid(kuint16))
- mPixelType = UINT16;
- else if (t_id == typeid(kint8))
- mPixelType = INT8;
- else if (t_id == typeid(kuint8))
- mPixelType = UINT8;
- else
-#endif
- mPixelType = 0;
-
- bHeaderWritten = false;
- bDataWritten = false;
-}
-
-template<class T>
-Array2dFile<T>::~Array2dFile (void)
-{
- fileClose ();
-}
-
-
-template<class T>
-void
-Array2dFile<T>::fileClose (void)
-{
- if (file_id >= 0) {
- headerWrite ();
- close (file_id);
- file_id = -1;
- }
-}
-
-template<class T>
-bool
-Array2dFile<T>::fileCreate (void)
-{
- // io = new iostream(filename, ios::out | ios::in | ios::trunc | io::binary);
- if ((file_id = open (filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
- sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
- return (false);
- }
- headerWrite();
- return (true);
-}
-
-template<class T>
-bool
-Array2dFile<T>::fileRead (void)
-{
- // io = new iostream(filename, ios::out | ios::in | io::binary);
- if ((file_id = open (filename.c_str(), O_RDONLY | O_BINARY)) < 0) {
- sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
- return (false);
- }
-
- headerRead();
-
- array.initSetSize(mNX, mNY);
-
- arrayDataRead();
-
- return (true);
-}
-
-template<class T>
-void
-Array2dFile<T>::setAxisIncrement (double incX, double incY)
-{
- axis_increment_known = true;
- mIncX = incX;
- mIncY = incY;
-}
-
-template<class T>
-void
-Array2dFile<T>::setAxisExtent (double minX, double maxX, double minY, double maxY)
-{
- axis_extent_known = true;
- mMinX = minX;
- mMaxY = maxX;
- mMinX = minX;
- mMaxY = maxY;
-}
-
-template<class T>
-void
-Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
-{
- T** da = array.getArray();
- if (da) {
- pvmax = pvmin = da[0][0];
- for (int ix = 0; ix < mNX; ix++)
- for (int iy = 0; iy < mNY; iy++)
- if (pvmax < da[ix][iy])
- pvmax = da[ix][iy];
- else if (pvmin > da[ix][iy])
- pvmin = da[ix][iy];
- }
-}
-
-template<class T>
-void
-Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
-{
- T** ad = array.getArray();
- if (ad) {
- mOffsetPV = offset;
- mScalePV = scale;
-
- for (unsigned int ix = 0; ix < mNX; ix++)
- for (unsigned int iy = 0; iy < mNY; iy++)
- ad[ix][iy] = (ad[ix][iy] - offset) * scale;
- }
-}
-
-template<class T>
-bool
-Array2dFile<T>::headerRead (void)
-{
- if (file_id < 0) {
- sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
- return (false);
- }
-
- lseek (file_id, 0, SEEK_SET);
- kuint16 file_signature;
- kuint16 file_mPixelSize;
- kuint16 file_mPixelType;
-
- read_nint16 (&headersize, file_id);
- read_nint16 (&file_signature, file_id);
- read_nint16 (&num_labels, file_id);
- read_nint16 (&file_mPixelType, file_id);
- read_nint16 (&file_mPixelSize, file_id);
- read_nint32 (&mNX, file_id);
- read_nint32 (&mNY, file_id);
- read_nint16 (&axis_increment_known, file_id);
- read_nfloat64 (&mIncX, file_id);
- read_nfloat64 (&mIncY, file_id);
- read_nint16 (&axis_extent_known, file_id);
- read_nfloat64 (&mMinX, file_id);
- read_nfloat64 (&mMaxX, file_id);
- read_nfloat64 (&mMinY, file_id);
- read_nfloat64 (&mMaxY, file_id);
- read_nfloat64 (&mOffsetPV, file_id);
- read_nfloat64 (&mScalePV, file_id);
-
- int read_headersize = lseek (file_id, 0, SEEK_CUR);
- if (read_headersize != headersize) {
- sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
- return (false);
- }
- if (file_signature != signature) {
- sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
- return (false);
- }
- if (file_mPixelType != mPixelType) {
- sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_mPixelType, mPixelType);
- return (false);
- }
- if (file_mPixelSize != mPixelSize) {
- sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_mPixelSize, mPixelSize);
- return (false);
- }
-
- return (true);
-}
-
-template<class T>
-bool
-Array2dFile<T>::headerWrite (void)
-{
- if (file_id < 0) {
- sys_error (ERR_WARNING, "Tried to write header with file closed");
- return (false);
- }
-
- lseek (file_id, 0, SEEK_SET);
- write_nint16 (&headersize, file_id);
- write_nint16 (&signature, file_id);
- write_nint16 (&num_labels, file_id);
- write_nint16 (&mPixelType, file_id);
- write_nint16 (&mPixelSize, file_id);
- write_nint32 (&mNX, file_id);
- write_nint32 (&mNY, file_id);
- write_nint16 (&axis_increment_known, file_id);
- write_nfloat64 (&mIncX, file_id);
- write_nfloat64 (&mIncY, file_id);
- write_nint16 (&axis_extent_known, file_id);
- write_nfloat64 (&mMinX, file_id);
- write_nfloat64 (&mMaxX, file_id);
- write_nfloat64 (&mMinY, file_id);
- write_nfloat64 (&mMaxY, file_id);
- write_nfloat64 (&mOffsetPV, file_id);
- write_nfloat64 (&mScalePV, file_id);
-
- headersize = lseek (file_id, 0, SEEK_CUR);
- lseek (file_id, 0, SEEK_SET);
- write_nint16 (&headersize, file_id);
-
- return (true);
-}
-
-template<class T>
-bool
-Array2dFile<T>::arrayDataWrite (void)
-{
- if (file_id < 0) {
- sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
- return (false);
- }
-
- T** da = array.getArray();
- if (! da)
- return (false);
-
- lseek (file_id, headersize, SEEK_SET);
- for (unsigned int ix = 0; ix < mNX; ix++)
- for (unsigned int iy = 0; iy < mNY; iy++) {
- T value = da[ix][iy];
- ConvertReverseNetworkOrder (&value, sizeof(T));
- write (file_id, &value, mPixelSize);
- }
-
- return (true);
-}
-
-template<class T>
-bool
-Array2dFile<T>::arrayDataRead (void)
-{
- if (file_id < 0) {
- sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0");
- return (false);
- }
-
- T** da = array.getArray();
- if (! da)
- return (false);
-
- lseek (file_id, headersize, SEEK_SET);
- T pixelBuffer;
- for (int ix = 0; ix < mNX; ix++)
- for (unsigned int iy = 0; iy < mNY; iy++) {
- read (file_id, &pixelBuffer, mPixelSize);
- ConvertReverseNetworkOrder (&pixelBuffer, sizeof(T));
- da[ix][iy] = pixelBuffer;
- }
-
- return (true);
-}
-
-template<class T>
-bool
-Array2dFile<T>::labelSeek (unsigned int label_num)
-{
- if (label_num > num_labels) {
- sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
- return (false);
- }
-
- if (array.getArray() == NULL) { // Could not have written data if array not allocated
- sys_error (ERR_WARNING, "array == NULL [labelSeek]");
- return (false);
- }
-
- off_t pos = headersize + array.sizeofArray();
- if (lseek (file_id, pos, SEEK_SET) != pos) {
- sys_error (ERR_WARNING, "Can't seek to end of data array");
- return (false);
- }
-
- for (int i = 0; i < label_num; i++)
- {
- pos += 22; // Skip to string length
- if (lseek (file_id, pos, SEEK_SET) != pos) {
- sys_error (ERR_WARNING, "Can't seek to string length");
- return (false);
- }
- kuint16 strlength;
- read_nint16 (&strlength, file_id);
- pos += 2 + strlength; // Skip label string length + data
- if (lseek (file_id, pos, SEEK_SET) != pos) {
- sys_error (ERR_WARNING, "Can't seek past label string");
- return (false);
- }
- }
-
- return (true);
-}
-
-template<class T>
-bool
-Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
-{
- if (label_num >= num_labels) {
- sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
- return (false);
- }
-
- if (! labelSeek (label_num)) {
- sys_error (ERR_WARNING, "Error calling labelSeek");
- return (false);
- }
-
- read_nint16 (&label.m_labelType, file_id);
- read_nint16 (&label.m_year, file_id);
- read_nint16 (&label.m_month, file_id);
- read_nint16 (&label.m_day, file_id);
- read_nint16 (&label.m_hour, file_id);
- read_nint16 (&label.m_minute, file_id);
- read_nint16 (&label.m_second, file_id);
- read_nfloat64 (&label.m_calcTime, file_id);
-
- kuint16 strlength;
- read_nint16 (&strlength, file_id);
- char str [strlength+1];
- read (file_id, str, strlength);
- str[strlength] = 0;
- label.m_strLabel = str;
-
- return (true);
-}
-
-
-template<class T>
-void
-Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
-{
- labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
-}
-
-template<class T>
-void
-Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
-{
- Array2dFileLabel label (type, lstr, calc_time);
-
- labelAdd (label);
-}
-
-template<class T>
-void
-Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
-{
- labelSeek (num_labels);
-
- write_nint16 (&label.m_labelType, file_id);
- write_nint16 (&label.m_year, file_id);
- write_nint16 (&label.m_month, file_id);
- write_nint16 (&label.m_day, file_id);
- write_nint16 (&label.m_hour, file_id);
- write_nint16 (&label.m_minute, file_id);
- write_nint16 (&label.m_second, file_id);
- write_nfloat64 (&label.m_calcTime, file_id);
- kuint16 strlength = label.m_strLabel.length();
- write_nint16 (&strlength, file_id);
- write (file_id, static_cast<const void*>(label.m_strLabel.c_str()), strlength);
-
- num_labels++;
-
- headerWrite();
- fsync(file_id);
-}
-
-template<class T>
-void
-Array2dFile<T>::labelsCopy (Array2dFile& copyFile, const char* const idStr)
-{
- string id = idStr;
- for (int i = 0; i < copyFile.getNumLabels(); i++) {
- Array2dFileLabel l;
- copyFile.labelRead (l, i);
- string lstr = l.getLabelString();
- lstr = idStr + lstr;
- l.setLabelString (lstr);
- labelAdd (l);
- }
-}
-
-template<class T>
-void
-Array2dFile<T>::arrayDataClear (void)
-{
- T** v = array.getArray();
- if (v) {
- for (unsigned int ix = 0; ix < mNX; ix++)
- for (unsigned int iy = 0; iy < mNY; iy++)
- v[ix][iy] = 0;
- }
-}
-
-