r115: *** empty log message ***
[ctsim.git] / include / imagefile.h
index 19f5db07354362fba69b22bf48c313f93981af0c..607f7166cd1cef4bd2f323c273830c0b38bb23e7 100644 (file)
@@ -2,28 +2,35 @@
 #define IDF_H
 
 #include <string>
-#include "kstddef.h"
 #include <sys/types.h>
 #include <unistd.h>
-#include <array2d.h>
+#include <fstream>
+#include <iostream>
+#include "ctsupport.h"
+#include "fnetorderstream.h"
+#include "array2d.h"
 
 using namespace std;
 
+
 class Array2dFileLabel
 {
 private:
     void init (void);
+    Array2dFileLabel (const Array2dFileLabel&);
+    Array2dFileLabel& operator= (const Array2dFileLabel&);
 
 public:
-    kfloat64 calc_time;
-    kuint16 label_type;
-    kuint16 year;
-    kuint16 month;
-    kuint16 day;
-    kuint16 hour;
-    kuint16 minute;
-    kuint16 second;
-    string label_str;
+    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;
@@ -37,22 +44,22 @@ public:
 
     ~Array2dFileLabel();
 
-    string getLabelString (void) const
-       { return label_str; }
+    const string& getLabelString (void) const
+       { return m_strLabel; }
 
     kfloat64 getCalcTime (void) const
-       { return calc_time; }
+       { return m_calcTime; }
 
     kfloat64 getLabelType (void) const
-       { return label_type; }
+       { return m_labelType; }
 
     string& setLabelString (const char* const str)
-       { label_str = str; return (label_str); }
+       { m_strLabel = str; return (m_strLabel); }
 
     string& setLabelString (const string& str)
-       { label_str = str; return (label_str); }
+       { m_strLabel = str; return (m_strLabel); }
 
-    void getDateString (string& str) const;
+    const string& getDateString () const;
 };
 
 
@@ -66,7 +73,7 @@ private:
   kuint16 headersize;
   string  filename;
   int file_id;
-  iostream *io;
+  frnetorderstream *m_pFS;
   bool bHeaderWritten;
   bool bDataWritten;
 
@@ -88,9 +95,12 @@ private:
   kuint32 mNX;
   kuint32 mNY;
 
+  Array2dFile (const Array2dFile&);
+  Array2dFile& operator= (const Array2dFile&);
+
 public:
 
-  Array2d<T> *array;
+  Array2d<T> array;
 
   static const int INT8 = 1;
   static const int UINT8 = 2;
@@ -113,9 +123,9 @@ public:
 
   void labelAdd (const Array2dFileLabel& label);
 
-  void labelAdd (const char* const label_str, double calc_time=0.);
+  void labelAdd (const char* const m_strLabel, double calc_time=0.);
 
-  void labelAdd (int type, const char* const label_str, 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);
 
@@ -139,7 +149,7 @@ public:
   void doPixelOffsetScale (double offset, double scale);
 
   T** getArray (void) const
-      { return (array == NULL ? NULL : array->getArray()); }
+      { return (array.getArray()); }
 
   bool arrayDataWrite (void);
 
@@ -160,7 +170,7 @@ Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
     init();
     mNX = x;
     mNY = y;
-    array = new Array2d<T> (mNX, mNY);
+    array.initSetSize(mNX, mNY);
 }
 
 template<class T>
@@ -170,7 +180,7 @@ Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned in
     init();
     mNX = x;
     mNY = y;
-    array = new Array2d<T> (mNX, mNY);
+    array.initSetSize(mNX, mNY);
 }
 
 template<class T>
@@ -186,7 +196,6 @@ Array2dFile<T>::init (void)
 {
   mPixelSize = sizeof(T);
   signature = ('I' * 256 + 'F');
-  file_id = -1;
   mNX = 0;
   mNY = 0;
   headersize = 0;
@@ -197,8 +206,7 @@ Array2dFile<T>::init (void)
   mMinX = mMaxX = mMinY = mMaxY = 0;
   mOffsetPV = 0;
   mScalePV = 1;
-  array = NULL;
-  io = NULL;
+  m_pFS = NULL;
 
 #if 0
   const type_info& t_id = typeid(T);
@@ -233,7 +241,6 @@ template<class T>
 Array2dFile<T>::~Array2dFile (void)
 {
     fileClose ();
-    delete array;
 }
 
 
@@ -241,10 +248,10 @@ template<class T>
 void
 Array2dFile<T>::fileClose (void)
 {
-  if (file_id >= 0) {
+  if (m_pFS) {
       headerWrite ();
-      close (file_id);
-      file_id = -1;
+      m_pFS->close ();
+      m_pFS = NULL;
   }
 }
 
@@ -252,11 +259,11 @@ 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);
-    }
+  m_pFS = new frnetorderstream (filename.c_str(), ios::out | ios::in | ios::trunc | ios::binary);
+  if (! m_pFS) {
+      sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
+      return (false);
+  }
   headerWrite();
   return (true);
 }
@@ -265,17 +272,16 @@ 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) {
+  m_pFS = new frnetorderstream (filename.c_str(), ios::out | ios::in | ios::binary | ios::nocreate);
+  if (m_pFS->fail()) {
     sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
     return (false);
   }
 
-  headerRead();
-  if (array != NULL)
-    delete array;
+  if (! headerRead())
+      return false;
 
-  array = new Array2d<T> (mNX, mNY);
+  array.initSetSize(mNX, mNY);
 
   arrayDataRead();
 
@@ -306,8 +312,8 @@ template<class T>
 void 
 Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
 {
-    if (array != NULL) {
-       T** da = array->getArray();
+    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++)
@@ -322,14 +328,14 @@ template<class T>
 void
 Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
 {
-    if (adf != NULL) {
-      mOffsetPV = offset;
-      mScalePV = scale;
-
-      T** ad = adf->getArray();
-      for (unsigned int ix = 0; ix < mNX; ix++) 
-         for (unsigned int iy = 0; iy < mNY; iy++)
-             ad[ix][iy] = (ad[ix][iy] - offset) * 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;
     }
 }
 
@@ -337,35 +343,35 @@ template<class T>
 bool
 Array2dFile<T>::headerRead (void)
 {
-  if (file_id < 0) {
+  if (! m_pFS) {
     sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
     return (false);
   }
 
-  lseek (file_id, 0, SEEK_SET);
+  m_pFS->seekg (0);
   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);
+  m_pFS->readInt16 (headersize);
+  m_pFS->readInt16 (file_signature);
+  m_pFS->readInt16 (num_labels);
+  m_pFS->readInt16 (file_mPixelType);
+  m_pFS->readInt16 (file_mPixelSize);
+  m_pFS->readInt32 (mNX);
+  m_pFS->readInt32 (mNY);
+  m_pFS->readInt16 (axis_increment_known);
+  m_pFS->readFloat64 (mIncX);
+  m_pFS->readFloat64 (mIncY);
+  m_pFS->readInt16 (axis_extent_known);
+  m_pFS->readFloat64 (mMinX);
+  m_pFS->readFloat64 (mMaxX);
+  m_pFS->readFloat64 (mMinY);
+  m_pFS->readFloat64 (mMaxY);
+  m_pFS->readFloat64 (mOffsetPV);
+  m_pFS->readFloat64 (mScalePV);
+
+  int read_headersize = m_pFS->tellg();
   if (read_headersize != headersize) {
     sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
     return (false);
@@ -390,33 +396,32 @@ template<class T>
 bool
 Array2dFile<T>::headerWrite (void)
 {
-  if (file_id < 0) {
+  if (! m_pFS) {
     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);
+  m_pFS->seekp (0);
+  m_pFS->writeInt16 (headersize);
+  m_pFS->writeInt16 (signature);
+  m_pFS->writeInt16 (num_labels);
+  m_pFS->writeInt16 (mPixelType);
+  m_pFS->writeInt16 (mPixelSize);
+  m_pFS->writeInt32 (mNX);
+  m_pFS->writeInt32 (mNY);
+  m_pFS->writeInt16 (axis_increment_known);
+  m_pFS->writeFloat64 (mIncX);
+  m_pFS->writeFloat64 (mIncY);
+  m_pFS->writeInt16 (axis_extent_known);
+  m_pFS->writeFloat64 (mMinX);
+  m_pFS->writeFloat64 (mMaxX);
+  m_pFS->writeFloat64 (mMinY);
+  m_pFS->writeFloat64 (mMaxY);
+  m_pFS->writeFloat64 (mOffsetPV);
+  m_pFS->writeFloat64 (mScalePV);
+
+  headersize = m_pFS->tellp();
+  m_pFS->writeInt16 (headersize);
   
   return (true);
 }
@@ -425,18 +430,26 @@ template<class T>
 bool
 Array2dFile<T>::arrayDataWrite (void)
 {
-  if (file_id < 0) {
-    sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
+  if (! m_pFS) {
+    sys_error (ERR_WARNING, "Tried to arrayDataWrite with !m_pFS");
     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 = array->array_data[ix][iy];
-         ConvertReverseNetworkOrder (&value, sizeof(T));
-         write (file_id, &value, mPixelSize);
-      }
+  T** da = array.getArray();
+  if (! da) 
+      return (false);
+
+  m_pFS->seekp (headersize);
+  for (unsigned int ix = 0; ix < mNX; ix++) {
+      if (NativeBigEndian()) {
+         for (unsigned int iy = 0; iy < mNY; iy++) {
+             T value = da[ix][iy];
+             ConvertReverseNetworkOrder (&value, sizeof(T));
+             m_pFS->write (&value, mPixelSize);
+         }
+      } else 
+         m_pFS->write (da[ix], sizeof(T) * mNY);
+  }
 
   return (true);
 }
@@ -445,19 +458,28 @@ template<class T>
 bool
 Array2dFile<T>::arrayDataRead (void)
 {
-  if (file_id < 0) {
-    sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0");
+  if (! m_pFS) {
+    sys_error (ERR_WARNING, "Tried to arrayDataRead with !m_pFS");
     return (false);
   }
 
-  lseek (file_id, headersize, SEEK_SET);
+  T** da = array.getArray();
+  if (! da) 
+      return (false);
+
+  m_pFS->seekg (headersize);
   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));
-         array->array_data[ix][iy] = pixelBuffer;
-      }
+  for (int ix = 0; ix < mNX; ix++) {
+      if (NativeBigEndian()) {
+         for (unsigned int iy = 0; iy < mNY; iy++) {
+             m_pFS->read (&pixelBuffer, mPixelSize);
+             ConvertReverseNetworkOrder (&pixelBuffer, sizeof(T));
+             da[ix][iy] = pixelBuffer;
+         } 
+      } else
+         m_pFS->read (da[ix], sizeof(T) * mNY);
+  }
+
 
   return (true);
 }
@@ -471,32 +493,27 @@ Array2dFile<T>::labelSeek (unsigned int label_num)
     return (false);
   }
 
-  if (array == NULL) {    // Could not have written data if array not allocated
+  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);
-  }
+  off_t pos = headersize + array.sizeofArray();
+  m_pFS->seekg (pos);
 
   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);
-         }
+         m_pFS->seekg (pos);
+             
          kuint16 strlength;
-         read_nint16 (&strlength, file_id);
+         m_pFS->readInt16 (strlength);
          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);
-         }
+         m_pFS->seekg (pos);
       }
+  
+  if (! m_pFS)
+      return false;
 
   return (true);
 }
@@ -515,20 +532,21 @@ Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
     return (false);
   }
 
-  read_nint16 (&label.label_type, file_id);
-  read_nint16 (&label.year, file_id);
-  read_nint16 (&label.month, file_id);
-  read_nint16 (&label.day, file_id);
-  read_nint16 (&label.hour, file_id);
-  read_nint16 (&label.minute, file_id);
-  read_nint16 (&label.second, file_id);
-  read_nfloat64 (&label.calc_time, file_id);
+  m_pFS->readInt16 (label.m_labelType);
+  m_pFS->readInt16 (label.m_year);
+  m_pFS->readInt16 (label.m_month);
+  m_pFS->readInt16 (label.m_day);
+  m_pFS->readInt16 (label.m_hour);
+  m_pFS->readInt16 (label.m_minute);
+  m_pFS->readInt16 (label.m_second);
+  m_pFS->readFloat64 (label.m_calcTime);
 
   kuint16 strlength;
-  read_nint16 (&strlength, file_id);
-  char *str = new char [strlength+1];
-  read (file_id, str, strlength);
-  label.label_str = str;
+  m_pFS->readInt16 (strlength);
+  char str [strlength+1];
+  m_pFS->read (str, strlength);
+  str[strlength] = 0;
+  label.m_strLabel = str;
 
   return (true);
 }
@@ -556,22 +574,21 @@ Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
 {
   labelSeek (num_labels);
   
-  write_nint16 (&label.label_type, file_id);
-  write_nint16 (&label.year, file_id);
-  write_nint16 (&label.month, file_id);
-  write_nint16 (&label.day, file_id);
-  write_nint16 (&label.hour, file_id);
-  write_nint16 (&label.minute, file_id);
-  write_nint16 (&label.second, file_id);
-  write_nfloat64 (&label.calc_time, file_id);
-  kuint16 strlength = label.label_str.length();
-  write_nint16 (&strlength, file_id);
-  write (file_id, static_cast<const void*>(label.label_str.c_str()), strlength);
+  m_pFS->writeInt16 (label.m_labelType);
+  m_pFS->writeInt16 (label.m_year);
+  m_pFS->writeInt16 (label.m_month);
+  m_pFS->writeInt16 (label.m_day);
+  m_pFS->writeInt16 (label.m_hour);
+  m_pFS->writeInt16 (label.m_minute);
+  m_pFS->writeInt16 (label.m_second);
+  m_pFS->writeFloat64 (label.m_calcTime);
+  kuint16 strlength = label.m_strLabel.length();
+  m_pFS->writeInt16 (strlength);
+  m_pFS->write (label.m_strLabel.c_str(), strlength);
 
   num_labels++;
 
   headerWrite();
-  fsync(file_id);
 }
 
 template<class T>
@@ -593,8 +610,8 @@ template<class T>
 void 
 Array2dFile<T>::arrayDataClear (void)
 {
-    if (array != NULL) {
-       T** v = array->getArray();
+    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;
@@ -602,39 +619,31 @@ Array2dFile<T>::arrayDataClear (void)
 }
 
 
-#ifdef MPI_CT
-#include <mpi.h>
+#ifdef HAVE_MPI
+#include <mpi++.h>
 #endif
 
-class F32Image
+class F32Image : public Array2dFile<kfloat32>
 {
 public:
-  Array2dFile<kfloat32> adf;
-
-  F32Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
+  F32Image (const char* const fname, unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat32>::Array2dFile (fname, nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
+      setPixelType (FLOAT32);
   }
 
-  F32Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
+  F32Image (unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat32>::Array2dFile (nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
+      setPixelType (FLOAT32);
   }
 
-  F32Image (const char* const fname) : adf (fname)
+  F32Image (const char* const fname)
+      : Array2dFile<kfloat32>::Array2dFile (fname)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
+      setPixelType (FLOAT32);
   }
 
-  kfloat32** getArray(void) const
-      { return adf.getArray(); }
-
-  kuint32 nx(void) const
-      { return adf.nx(); }
-
-  kuint32 ny(void) const
-      { return adf.ny(); }
-
 #ifdef HAVE_MPI
   MPI::Datatype getMPIDataType (void) const
       { return MPI::FLOAT; }
@@ -642,45 +651,46 @@ public:
 };
 
 
-class F64Image 
+class F64Image : public Array2dFile<kfloat64>
 {
-public:
-  Array2dFile<kfloat64> adf;
-
-#ifdef HAVE_MPI
-  MPI::Datatype getMPIDataType (void) const
-      { return MPI::DOUBLE; }
-#endif
+ public:
 
-  F64Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
+  F64Image (const char* const fname, unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat64>::Array2dFile (fname, nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
+      setPixelType (FLOAT64);
   }
 
-  F64Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
+  F64Image (unsigned int nx, unsigned int ny)
+      : Array2dFile<kfloat64>::Array2dFile (nx, ny)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
+      setPixelType (FLOAT64);
   }
 
-  F64Image (const char* const fname) : adf (fname)
+  F64Image (const char* const fname)
+      : Array2dFile<kfloat64>::Array2dFile (fname)
   {
-      adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
+      setPixelType (FLOAT64);
   }
 
-  kfloat64** getArray(void) const
-      { return adf.getArray(); }
-
-  kuint32 nx(void) const
-      { return adf.nx(); }
-
-  kuint32 ny(void) const
-      { return adf.ny(); }
+#ifdef HAVE_MPI
+  MPI::Datatype getMPIDataType (void) const
+      { return MPI::DOUBLE; }
+#endif
 };
 
+#undef IMAGEFILE_64_BITS
+#ifdef IMAGEFILE_64_BITS
 typedef F64Image   ImageFile;
 typedef kfloat64   ImageFileValue;
 typedef kfloat64*  ImageFileColumn;
 typedef kfloat64** ImageFileArray;
+#else
+typedef F32Image   ImageFile;
+typedef kfloat32   ImageFileValue;
+typedef kfloat32*  ImageFileColumn;
+typedef kfloat32** ImageFileArray;
+#endif
 
 #endif