10 #include "fnetorderstream.h"
16 class Array2dFileLabel
20 Array2dFileLabel (const Array2dFileLabel&);
21 Array2dFileLabel& operator= (const Array2dFileLabel&);
33 mutable string m_strDate;
35 static const int L_EMPTY = 0;
36 static const int L_HISTORY = 1;
37 static const int L_USER = 2;
41 Array2dFileLabel(const char* const str, double ctime = 0.);
43 Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
47 const string& getLabelString (void) const
48 { return m_strLabel; }
50 kfloat64 getCalcTime (void) const
51 { return m_calcTime; }
53 kfloat64 getLabelType (void) const
54 { return m_labelType; }
56 string& setLabelString (const char* const str)
57 { m_strLabel = str; return (m_strLabel); }
59 string& setLabelString (const string& str)
60 { m_strLabel = str; return (m_strLabel); }
62 const string& getDateString () const;
76 frnetorderstream *m_pFS;
80 bool headerWrite (void);
82 bool headerRead (void);
84 bool arrayDataRead (void);
86 bool labelSeek (unsigned int label_num);
89 kuint16 axis_increment_known;
90 kfloat64 mIncX, mIncY;
91 kuint16 axis_extent_known;
92 kfloat64 mMinX, mMaxX, mMinY, mMaxY;
93 kfloat64 mOffsetPV, mScalePV;
98 Array2dFile (const Array2dFile&);
99 Array2dFile& operator= (const Array2dFile&);
105 static const int INT8 = 1;
106 static const int UINT8 = 2;
107 static const int INT16 = 3;
108 static const int UINT16 = 4;
109 static const int INT32 = 5;
110 static const int UINT32 = 6;
111 static const int FLOAT32 = 7;
112 static const int FLOAT64 = 8;
114 Array2dFile (unsigned int nx, unsigned int ny);
115 Array2dFile (const char* const filename);
116 Array2dFile (const char* const filename, unsigned int nx, unsigned int ny);
119 unsigned int getNumLabels (void) const
120 { return num_labels; }
122 bool labelRead (Array2dFileLabel& label, unsigned int label_num);
124 void labelAdd (const Array2dFileLabel& label);
126 void labelAdd (const char* const m_strLabel, double calc_time=0.);
128 void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
130 void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
132 void fileClose (void);
134 void setPixelType (int type)
135 { mPixelType = type; }
137 kuint32 nx (void) const
140 kuint32 ny (void) const
143 void setAxisIncrement (double mIncX, double mIncY);
145 void setAxisExtent (double mMinX, double mMaxX, double mMinY, double mMaxY);
147 void getPixelValueRange (T& pvmin, T& pvmax) const;
149 void doPixelOffsetScale (double offset, double scale);
151 T** getArray (void) const
152 { return (array.getArray()); }
154 bool arrayDataWrite (void);
156 void arrayDataClear (void);
158 bool fileRead (void);
160 bool fileCreate (void);
162 const string& GetFilename (void) const
168 Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
173 array.initSetSize(mNX, mNY);
177 Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
183 array.initSetSize(mNX, mNY);
187 Array2dFile<T>::Array2dFile (const char * const str)
195 Array2dFile<T>::init (void)
197 mPixelSize = sizeof(T);
198 signature = ('I' * 256 + 'F');
203 axis_increment_known = false;
204 axis_extent_known = false;
206 mMinX = mMaxX = mMinY = mMaxY = 0;
212 const type_info& t_id = typeid(T);
213 cout << t_id.name() << endl;
214 const type_info& comp_id = typeid(T);
217 mPixelType = FLOAT64;
218 else if (t_id == typeid(kfloat32))
219 mPixelType = FLOAT32;
220 else if (t_id == typeid(kint32))
222 else if (t_id == typeid(kuint32))
224 else if (t_id == typeid(kint16))
226 else if (t_id == typeid(kuint16))
228 else if (t_id == typeid(kint8))
230 else if (t_id == typeid(kuint8))
236 bHeaderWritten = false;
237 bDataWritten = false;
241 Array2dFile<T>::~Array2dFile (void)
249 Array2dFile<T>::fileClose (void)
260 Array2dFile<T>::fileCreate (void)
262 m_pFS = new frnetorderstream (filename.c_str(), ios::out | ios::in | ios::trunc | ios::binary);
264 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
273 Array2dFile<T>::fileRead (void)
275 m_pFS = new frnetorderstream (filename.c_str(), ios::out | ios::in | ios::binary | ios::nocreate);
277 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
284 array.initSetSize(mNX, mNY);
293 Array2dFile<T>::setAxisIncrement (double incX, double incY)
295 axis_increment_known = true;
302 Array2dFile<T>::setAxisExtent (double minX, double maxX, double minY, double maxY)
304 axis_extent_known = true;
313 Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
315 T** da = array.getArray();
317 pvmax = pvmin = da[0][0];
318 for (int ix = 0; ix < mNX; ix++)
319 for (int iy = 0; iy < mNY; iy++)
320 if (pvmax < da[ix][iy])
322 else if (pvmin > da[ix][iy])
329 Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
331 T** ad = array.getArray();
336 for (unsigned int ix = 0; ix < mNX; ix++)
337 for (unsigned int iy = 0; iy < mNY; iy++)
338 ad[ix][iy] = (ad[ix][iy] - offset) * scale;
344 Array2dFile<T>::headerRead (void)
347 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
352 kuint16 file_signature;
353 kuint16 file_mPixelSize;
354 kuint16 file_mPixelType;
356 m_pFS->readInt16 (headersize);
357 m_pFS->readInt16 (file_signature);
358 m_pFS->readInt16 (num_labels);
359 m_pFS->readInt16 (file_mPixelType);
360 m_pFS->readInt16 (file_mPixelSize);
361 m_pFS->readInt32 (mNX);
362 m_pFS->readInt32 (mNY);
363 m_pFS->readInt16 (axis_increment_known);
364 m_pFS->readFloat64 (mIncX);
365 m_pFS->readFloat64 (mIncY);
366 m_pFS->readInt16 (axis_extent_known);
367 m_pFS->readFloat64 (mMinX);
368 m_pFS->readFloat64 (mMaxX);
369 m_pFS->readFloat64 (mMinY);
370 m_pFS->readFloat64 (mMaxY);
371 m_pFS->readFloat64 (mOffsetPV);
372 m_pFS->readFloat64 (mScalePV);
374 int read_headersize = m_pFS->tellg();
375 if (read_headersize != headersize) {
376 sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
379 if (file_signature != signature) {
380 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
383 if (file_mPixelType != mPixelType) {
384 sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_mPixelType, mPixelType);
387 if (file_mPixelSize != mPixelSize) {
388 sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_mPixelSize, mPixelSize);
397 Array2dFile<T>::headerWrite (void)
400 sys_error (ERR_WARNING, "Tried to write header with file closed");
405 m_pFS->writeInt16 (headersize);
406 m_pFS->writeInt16 (signature);
407 m_pFS->writeInt16 (num_labels);
408 m_pFS->writeInt16 (mPixelType);
409 m_pFS->writeInt16 (mPixelSize);
410 m_pFS->writeInt32 (mNX);
411 m_pFS->writeInt32 (mNY);
412 m_pFS->writeInt16 (axis_increment_known);
413 m_pFS->writeFloat64 (mIncX);
414 m_pFS->writeFloat64 (mIncY);
415 m_pFS->writeInt16 (axis_extent_known);
416 m_pFS->writeFloat64 (mMinX);
417 m_pFS->writeFloat64 (mMaxX);
418 m_pFS->writeFloat64 (mMinY);
419 m_pFS->writeFloat64 (mMaxY);
420 m_pFS->writeFloat64 (mOffsetPV);
421 m_pFS->writeFloat64 (mScalePV);
423 headersize = m_pFS->tellp();
424 m_pFS->writeInt16 (headersize);
431 Array2dFile<T>::arrayDataWrite (void)
434 sys_error (ERR_WARNING, "Tried to arrayDataWrite with !m_pFS");
438 T** da = array.getArray();
442 m_pFS->seekp (headersize);
443 for (unsigned int ix = 0; ix < mNX; ix++) {
444 if (NativeBigEndian()) {
445 for (unsigned int iy = 0; iy < mNY; iy++) {
446 T value = da[ix][iy];
447 ConvertReverseNetworkOrder (&value, sizeof(T));
448 m_pFS->write (&value, mPixelSize);
451 m_pFS->write (da[ix], sizeof(T) * mNY);
459 Array2dFile<T>::arrayDataRead (void)
462 sys_error (ERR_WARNING, "Tried to arrayDataRead with !m_pFS");
466 T** da = array.getArray();
470 m_pFS->seekg (headersize);
472 for (int ix = 0; ix < mNX; ix++) {
473 if (NativeBigEndian()) {
474 for (unsigned int iy = 0; iy < mNY; iy++) {
475 m_pFS->read (&pixelBuffer, mPixelSize);
476 ConvertReverseNetworkOrder (&pixelBuffer, sizeof(T));
477 da[ix][iy] = pixelBuffer;
480 m_pFS->read (da[ix], sizeof(T) * mNY);
489 Array2dFile<T>::labelSeek (unsigned int label_num)
491 if (label_num > num_labels) {
492 sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
496 if (array.getArray() == NULL) { // Could not have written data if array not allocated
497 sys_error (ERR_WARNING, "array == NULL [labelSeek]");
501 off_t pos = headersize + array.sizeofArray();
504 for (int i = 0; i < label_num; i++)
506 pos += 22; // Skip to string length
510 m_pFS->readInt16 (strlength);
511 pos += 2 + strlength; // Skip label string length + data
523 Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
525 if (label_num >= num_labels) {
526 sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
530 if (! labelSeek (label_num)) {
531 sys_error (ERR_WARNING, "Error calling labelSeek");
535 m_pFS->readInt16 (label.m_labelType);
536 m_pFS->readInt16 (label.m_year);
537 m_pFS->readInt16 (label.m_month);
538 m_pFS->readInt16 (label.m_day);
539 m_pFS->readInt16 (label.m_hour);
540 m_pFS->readInt16 (label.m_minute);
541 m_pFS->readInt16 (label.m_second);
542 m_pFS->readFloat64 (label.m_calcTime);
545 m_pFS->readInt16 (strlength);
546 char str [strlength+1];
547 m_pFS->read (str, strlength);
549 label.m_strLabel = str;
557 Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
559 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
564 Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
566 Array2dFileLabel label (type, lstr, calc_time);
573 Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
575 labelSeek (num_labels);
577 m_pFS->writeInt16 (label.m_labelType);
578 m_pFS->writeInt16 (label.m_year);
579 m_pFS->writeInt16 (label.m_month);
580 m_pFS->writeInt16 (label.m_day);
581 m_pFS->writeInt16 (label.m_hour);
582 m_pFS->writeInt16 (label.m_minute);
583 m_pFS->writeInt16 (label.m_second);
584 m_pFS->writeFloat64 (label.m_calcTime);
585 kuint16 strlength = label.m_strLabel.length();
586 m_pFS->writeInt16 (strlength);
587 m_pFS->write (label.m_strLabel.c_str(), strlength);
596 Array2dFile<T>::labelsCopy (Array2dFile& copyFile, const char* const idStr)
599 for (int i = 0; i < copyFile.getNumLabels(); i++) {
601 copyFile.labelRead (l, i);
602 string lstr = l.getLabelString();
604 l.setLabelString (lstr);
611 Array2dFile<T>::arrayDataClear (void)
613 T** v = array.getArray();
615 for (unsigned int ix = 0; ix < mNX; ix++)
616 for (unsigned int iy = 0; iy < mNY; iy++)
626 class F32Image : public Array2dFile<kfloat32>
629 F32Image (const char* const fname, unsigned int nx, unsigned int ny)
630 : Array2dFile<kfloat32>::Array2dFile (fname, nx, ny)
632 setPixelType (FLOAT32);
635 F32Image (unsigned int nx, unsigned int ny)
636 : Array2dFile<kfloat32>::Array2dFile (nx, ny)
638 setPixelType (FLOAT32);
641 F32Image (const char* const fname)
642 : Array2dFile<kfloat32>::Array2dFile (fname)
644 setPixelType (FLOAT32);
648 MPI::Datatype getMPIDataType (void) const
649 { return MPI::FLOAT; }
654 class F64Image : public Array2dFile<kfloat64>
658 F64Image (const char* const fname, unsigned int nx, unsigned int ny)
659 : Array2dFile<kfloat64>::Array2dFile (fname, nx, ny)
661 setPixelType (FLOAT64);
664 F64Image (unsigned int nx, unsigned int ny)
665 : Array2dFile<kfloat64>::Array2dFile (nx, ny)
667 setPixelType (FLOAT64);
670 F64Image (const char* const fname)
671 : Array2dFile<kfloat64>::Array2dFile (fname)
673 setPixelType (FLOAT64);
677 MPI::Datatype getMPIDataType (void) const
678 { return MPI::DOUBLE; }
682 #undef IMAGEFILE_64_BITS
683 #ifdef IMAGEFILE_64_BITS
684 typedef F64Image ImageFile;
685 typedef kfloat64 ImageFileValue;
686 typedef kfloat64* ImageFileColumn;
687 typedef kfloat64** ImageFileArray;
689 typedef F32Image ImageFile;
690 typedef kfloat32 ImageFileValue;
691 typedef kfloat32* ImageFileColumn;
692 typedef kfloat32** ImageFileArray;