12 class Array2dFileLabel
28 static const int L_EMPTY = 0;
29 static const int L_HISTORY = 1;
30 static const int L_USER = 2;
34 Array2dFileLabel(const char* const str, double ctime = 0.);
36 Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
40 string getLabelString (void) const
43 kfloat64 getCalcTime (void) const
46 kfloat64 getLabelType (void) const
47 { return label_type; }
49 string& setLabelString (const char* const str)
50 { label_str = str; return (label_str); }
52 string& setLabelString (const string& str)
53 { label_str = str; return (label_str); }
55 void getDateString (string& str) const;
73 bool headerWrite (void);
75 bool headerRead (void);
77 bool arrayDataRead (void);
79 bool labelSeek (unsigned int label_num);
82 kuint16 axis_increment_known;
83 kfloat64 mIncX, mIncY;
84 kuint16 axis_extent_known;
85 kfloat64 mMinX, mMaxX, mMinY, mMaxY;
86 kfloat64 mOffsetPV, mScalePV;
95 static const int INT8 = 1;
96 static const int UINT8 = 2;
97 static const int INT16 = 3;
98 static const int UINT16 = 4;
99 static const int INT32 = 5;
100 static const int UINT32 = 6;
101 static const int FLOAT32 = 7;
102 static const int FLOAT64 = 8;
104 Array2dFile (unsigned int nx, unsigned int ny);
105 Array2dFile (const char* const filename);
106 Array2dFile (const char* const filename, unsigned int nx, unsigned int ny);
109 unsigned int getNumLabels (void) const
110 { return num_labels; }
112 bool labelRead (Array2dFileLabel& label, unsigned int label_num);
114 void labelAdd (const Array2dFileLabel& label);
116 void labelAdd (const char* const label_str, double calc_time=0.);
118 void labelAdd (int type, const char* const label_str, double calc_time=0.);
120 void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
122 void fileClose (void);
124 void setPixelType (int type)
125 { mPixelType = type; }
127 kuint32 nx (void) const
130 kuint32 ny (void) const
133 void setAxisIncrement (double mIncX, double mIncY);
135 void setAxisExtent (double mMinX, double mMaxX, double mMinY, double mMaxY);
137 void getPixelValueRange (T& pvmin, T& pvmax) const;
139 void doPixelOffsetScale (double offset, double scale);
141 T** getArray (void) const
142 { return (array.getArray()); }
144 bool arrayDataWrite (void);
146 void arrayDataClear (void);
148 bool fileRead (void);
150 bool fileCreate (void);
152 const string& GetFilename (void) const
158 Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
163 array.initSetSize(mNX, mNY);
167 Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
173 array.initSetSize(mNX, mNY);
177 Array2dFile<T>::Array2dFile (const char * const str)
185 Array2dFile<T>::init (void)
187 mPixelSize = sizeof(T);
188 signature = ('I' * 256 + 'F');
194 axis_increment_known = false;
195 axis_extent_known = false;
197 mMinX = mMaxX = mMinY = mMaxY = 0;
203 const type_info& t_id = typeid(T);
204 cout << t_id.name() << endl;
205 const type_info& comp_id = typeid(T);
208 mPixelType = FLOAT64;
209 else if (t_id == typeid(kfloat32))
210 mPixelType = FLOAT32;
211 else if (t_id == typeid(kint32))
213 else if (t_id == typeid(kuint32))
215 else if (t_id == typeid(kint16))
217 else if (t_id == typeid(kuint16))
219 else if (t_id == typeid(kint8))
221 else if (t_id == typeid(kuint8))
227 bHeaderWritten = false;
228 bDataWritten = false;
232 Array2dFile<T>::~Array2dFile (void)
240 Array2dFile<T>::fileClose (void)
251 Array2dFile<T>::fileCreate (void)
253 // io = new iostream(filename, ios::out | ios::in | ios::trunc | io::binary);
254 if ((file_id = open (filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
255 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
264 Array2dFile<T>::fileRead (void)
266 // io = new iostream(filename, ios::out | ios::in | io::binary);
267 if ((file_id = open (filename.c_str(), O_RDONLY | O_BINARY)) < 0) {
268 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
274 array.initSetSize(mNX, mNY);
283 Array2dFile<T>::setAxisIncrement (double incX, double incY)
285 axis_increment_known = true;
292 Array2dFile<T>::setAxisExtent (double minX, double maxX, double minY, double maxY)
294 axis_extent_known = true;
303 Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
305 T** da = array.getArray();
307 pvmax = pvmin = da[0][0];
308 for (int ix = 0; ix < mNX; ix++)
309 for (int iy = 0; iy < mNY; iy++)
310 if (pvmax < da[ix][iy])
312 else if (pvmin > da[ix][iy])
319 Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
321 T** ad = array.getArray();
326 for (unsigned int ix = 0; ix < mNX; ix++)
327 for (unsigned int iy = 0; iy < mNY; iy++)
328 ad[ix][iy] = (ad[ix][iy] - offset) * scale;
334 Array2dFile<T>::headerRead (void)
337 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
341 lseek (file_id, 0, SEEK_SET);
342 kuint16 file_signature;
343 kuint16 file_mPixelSize;
344 kuint16 file_mPixelType;
346 read_nint16 (&headersize, file_id);
347 read_nint16 (&file_signature, file_id);
348 read_nint16 (&num_labels, file_id);
349 read_nint16 (&file_mPixelType, file_id);
350 read_nint16 (&file_mPixelSize, file_id);
351 read_nint32 (&mNX, file_id);
352 read_nint32 (&mNY, file_id);
353 read_nint16 (&axis_increment_known, file_id);
354 read_nfloat64 (&mIncX, file_id);
355 read_nfloat64 (&mIncY, file_id);
356 read_nint16 (&axis_extent_known, file_id);
357 read_nfloat64 (&mMinX, file_id);
358 read_nfloat64 (&mMaxX, file_id);
359 read_nfloat64 (&mMinY, file_id);
360 read_nfloat64 (&mMaxY, file_id);
361 read_nfloat64 (&mOffsetPV, file_id);
362 read_nfloat64 (&mScalePV, file_id);
364 int read_headersize = lseek (file_id, 0, SEEK_CUR);
365 if (read_headersize != headersize) {
366 sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
369 if (file_signature != signature) {
370 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
373 if (file_mPixelType != mPixelType) {
374 sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_mPixelType, mPixelType);
377 if (file_mPixelSize != mPixelSize) {
378 sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_mPixelSize, mPixelSize);
387 Array2dFile<T>::headerWrite (void)
390 sys_error (ERR_WARNING, "Tried to write header with file closed");
394 lseek (file_id, 0, SEEK_SET);
395 write_nint16 (&headersize, file_id);
396 write_nint16 (&signature, file_id);
397 write_nint16 (&num_labels, file_id);
398 write_nint16 (&mPixelType, file_id);
399 write_nint16 (&mPixelSize, file_id);
400 write_nint32 (&mNX, file_id);
401 write_nint32 (&mNY, file_id);
402 write_nint16 (&axis_increment_known, file_id);
403 write_nfloat64 (&mIncX, file_id);
404 write_nfloat64 (&mIncY, file_id);
405 write_nint16 (&axis_extent_known, file_id);
406 write_nfloat64 (&mMinX, file_id);
407 write_nfloat64 (&mMaxX, file_id);
408 write_nfloat64 (&mMinY, file_id);
409 write_nfloat64 (&mMaxY, file_id);
410 write_nfloat64 (&mOffsetPV, file_id);
411 write_nfloat64 (&mScalePV, file_id);
413 headersize = lseek (file_id, 0, SEEK_CUR);
414 lseek (file_id, 0, SEEK_SET);
415 write_nint16 (&headersize, file_id);
422 Array2dFile<T>::arrayDataWrite (void)
425 sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
429 T** da = array.getArray();
433 lseek (file_id, headersize, SEEK_SET);
434 for (unsigned int ix = 0; ix < mNX; ix++)
435 for (unsigned int iy = 0; iy < mNY; iy++) {
436 T value = da[ix][iy];
437 ConvertReverseNetworkOrder (&value, sizeof(T));
438 write (file_id, &value, mPixelSize);
446 Array2dFile<T>::arrayDataRead (void)
449 sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0");
453 T** da = array.getArray();
457 lseek (file_id, headersize, SEEK_SET);
459 for (int ix = 0; ix < mNX; ix++)
460 for (unsigned int iy = 0; iy < mNY; iy++) {
461 read (file_id, &pixelBuffer, mPixelSize);
462 ConvertReverseNetworkOrder (&pixelBuffer, sizeof(T));
463 da[ix][iy] = pixelBuffer;
471 Array2dFile<T>::labelSeek (unsigned int label_num)
473 if (label_num > num_labels) {
474 sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
478 if (array.getArray() == NULL) { // Could not have written data if array not allocated
479 sys_error (ERR_WARNING, "array == NULL [labelSeek]");
483 off_t pos = headersize + array.sizeofArray();
484 if (lseek (file_id, pos, SEEK_SET) != pos) {
485 sys_error (ERR_WARNING, "Can't seek to end of data array");
489 for (int i = 0; i < label_num; i++)
491 pos += 22; // Skip to string length
492 if (lseek (file_id, pos, SEEK_SET) != pos) {
493 sys_error (ERR_WARNING, "Can't seek to string length");
497 read_nint16 (&strlength, file_id);
498 pos += 2 + strlength; // Skip label string length + data
499 if (lseek (file_id, pos, SEEK_SET) != pos) {
500 sys_error (ERR_WARNING, "Can't seek past label string");
510 Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
512 if (label_num >= num_labels) {
513 sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
517 if (! labelSeek (label_num)) {
518 sys_error (ERR_WARNING, "Error calling labelSeek");
522 read_nint16 (&label.label_type, file_id);
523 read_nint16 (&label.year, file_id);
524 read_nint16 (&label.month, file_id);
525 read_nint16 (&label.day, file_id);
526 read_nint16 (&label.hour, file_id);
527 read_nint16 (&label.minute, file_id);
528 read_nint16 (&label.second, file_id);
529 read_nfloat64 (&label.calc_time, file_id);
532 read_nint16 (&strlength, file_id);
533 char *str = new char [strlength+1];
534 read (file_id, str, strlength);
535 label.label_str = str;
543 Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
545 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
550 Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
552 Array2dFileLabel label (type, lstr, calc_time);
559 Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
561 labelSeek (num_labels);
563 write_nint16 (&label.label_type, file_id);
564 write_nint16 (&label.year, file_id);
565 write_nint16 (&label.month, file_id);
566 write_nint16 (&label.day, file_id);
567 write_nint16 (&label.hour, file_id);
568 write_nint16 (&label.minute, file_id);
569 write_nint16 (&label.second, file_id);
570 write_nfloat64 (&label.calc_time, file_id);
571 kuint16 strlength = label.label_str.length();
572 write_nint16 (&strlength, file_id);
573 write (file_id, static_cast<const void*>(label.label_str.c_str()), strlength);
583 Array2dFile<T>::labelsCopy (Array2dFile& copyFile, const char* const idStr)
586 for (int i = 0; i < copyFile.getNumLabels(); i++) {
588 copyFile.labelRead (l, i);
589 string lstr = l.getLabelString();
591 l.setLabelString (lstr);
598 Array2dFile<T>::arrayDataClear (void)
600 T** v = array.getArray();
602 for (unsigned int ix = 0; ix < mNX; ix++)
603 for (unsigned int iy = 0; iy < mNY; iy++)
613 class F32Image : public Array2dFile<kfloat32>
616 F32Image (const char* const fname, unsigned int nx, unsigned int ny)
617 : Array2dFile<kfloat32>::Array2dFile (fname, nx, ny)
619 setPixelType (FLOAT32);
622 F32Image (unsigned int nx, unsigned int ny)
623 : Array2dFile<kfloat32>::Array2dFile (nx, ny)
625 setPixelType (FLOAT32);
628 F32Image (const char* const fname)
629 : Array2dFile<kfloat32>::Array2dFile (fname)
631 setPixelType (FLOAT32);
635 MPI::Datatype getMPIDataType (void) const
636 { return MPI::FLOAT; }
641 class F64Image : public Array2dFile<kfloat64>
645 F64Image (const char* const fname, unsigned int nx, unsigned int ny)
646 : Array2dFile<kfloat64>::Array2dFile (fname, nx, ny)
648 setPixelType (FLOAT64);
651 F64Image (unsigned int nx, unsigned int ny)
652 : Array2dFile<kfloat64>::Array2dFile (nx, ny)
654 setPixelType (FLOAT64);
657 F64Image (const char* const fname)
658 : Array2dFile<kfloat64>::Array2dFile (fname)
660 setPixelType (FLOAT64);
664 MPI::Datatype getMPIDataType (void) const
665 { return MPI::DOUBLE; }
669 #define IMAGEFILE_64_BITS 1
670 #ifdef IMAGEFILE_64_BITS
671 typedef F64Image ImageFile;
672 typedef kfloat64 ImageFileValue;
673 typedef kfloat64* ImageFileColumn;
674 typedef kfloat64** ImageFileArray;
676 typedef F32Image ImageFile;
677 typedef kfloat32 ImageFileValue;
678 typedef kfloat32* ImageFileColumn;
679 typedef kfloat32** ImageFileArray;