12 class Array2dFileLabel
16 Array2dFileLabel (const Array2dFileLabel&);
17 Array2dFileLabel& operator= (const Array2dFileLabel&);
30 static const int L_EMPTY = 0;
31 static const int L_HISTORY = 1;
32 static const int L_USER = 2;
36 Array2dFileLabel(const char* const str, double ctime = 0.);
38 Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
42 string getLabelString (void) const
45 kfloat64 getCalcTime (void) const
48 kfloat64 getLabelType (void) const
49 { return label_type; }
51 string& setLabelString (const char* const str)
52 { label_str = str; return (label_str); }
54 string& setLabelString (const string& str)
55 { label_str = str; return (label_str); }
57 void getDateString (string& str) const;
75 bool headerWrite (void);
77 bool headerRead (void);
79 bool arrayDataRead (void);
81 bool labelSeek (unsigned int label_num);
84 kuint16 axis_increment_known;
85 kfloat64 mIncX, mIncY;
86 kuint16 axis_extent_known;
87 kfloat64 mMinX, mMaxX, mMinY, mMaxY;
88 kfloat64 mOffsetPV, mScalePV;
93 Array2dFile (const Array2dFile&);
94 Array2dFile& operator= (const Array2dFile&);
100 static const int INT8 = 1;
101 static const int UINT8 = 2;
102 static const int INT16 = 3;
103 static const int UINT16 = 4;
104 static const int INT32 = 5;
105 static const int UINT32 = 6;
106 static const int FLOAT32 = 7;
107 static const int FLOAT64 = 8;
109 Array2dFile (unsigned int nx, unsigned int ny);
110 Array2dFile (const char* const filename);
111 Array2dFile (const char* const filename, unsigned int nx, unsigned int ny);
114 unsigned int getNumLabels (void) const
115 { return num_labels; }
117 bool labelRead (Array2dFileLabel& label, unsigned int label_num);
119 void labelAdd (const Array2dFileLabel& label);
121 void labelAdd (const char* const label_str, double calc_time=0.);
123 void labelAdd (int type, const char* const label_str, double calc_time=0.);
125 void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
127 void fileClose (void);
129 void setPixelType (int type)
130 { mPixelType = type; }
132 kuint32 nx (void) const
135 kuint32 ny (void) const
138 void setAxisIncrement (double mIncX, double mIncY);
140 void setAxisExtent (double mMinX, double mMaxX, double mMinY, double mMaxY);
142 void getPixelValueRange (T& pvmin, T& pvmax) const;
144 void doPixelOffsetScale (double offset, double scale);
146 T** getArray (void) const
147 { return (array.getArray()); }
149 bool arrayDataWrite (void);
151 void arrayDataClear (void);
153 bool fileRead (void);
155 bool fileCreate (void);
157 const string& GetFilename (void) const
163 Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
168 array.initSetSize(mNX, mNY);
172 Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
178 array.initSetSize(mNX, mNY);
182 Array2dFile<T>::Array2dFile (const char * const str)
190 Array2dFile<T>::init (void)
192 mPixelSize = sizeof(T);
193 signature = ('I' * 256 + 'F');
199 axis_increment_known = false;
200 axis_extent_known = false;
202 mMinX = mMaxX = mMinY = mMaxY = 0;
208 const type_info& t_id = typeid(T);
209 cout << t_id.name() << endl;
210 const type_info& comp_id = typeid(T);
213 mPixelType = FLOAT64;
214 else if (t_id == typeid(kfloat32))
215 mPixelType = FLOAT32;
216 else if (t_id == typeid(kint32))
218 else if (t_id == typeid(kuint32))
220 else if (t_id == typeid(kint16))
222 else if (t_id == typeid(kuint16))
224 else if (t_id == typeid(kint8))
226 else if (t_id == typeid(kuint8))
232 bHeaderWritten = false;
233 bDataWritten = false;
237 Array2dFile<T>::~Array2dFile (void)
245 Array2dFile<T>::fileClose (void)
256 Array2dFile<T>::fileCreate (void)
258 // io = new iostream(filename, ios::out | ios::in | ios::trunc | io::binary);
259 if ((file_id = open (filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
260 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
269 Array2dFile<T>::fileRead (void)
271 // io = new iostream(filename, ios::out | ios::in | io::binary);
272 if ((file_id = open (filename.c_str(), O_RDONLY | O_BINARY)) < 0) {
273 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
279 array.initSetSize(mNX, mNY);
288 Array2dFile<T>::setAxisIncrement (double incX, double incY)
290 axis_increment_known = true;
297 Array2dFile<T>::setAxisExtent (double minX, double maxX, double minY, double maxY)
299 axis_extent_known = true;
308 Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
310 T** da = array.getArray();
312 pvmax = pvmin = da[0][0];
313 for (int ix = 0; ix < mNX; ix++)
314 for (int iy = 0; iy < mNY; iy++)
315 if (pvmax < da[ix][iy])
317 else if (pvmin > da[ix][iy])
324 Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
326 T** ad = array.getArray();
331 for (unsigned int ix = 0; ix < mNX; ix++)
332 for (unsigned int iy = 0; iy < mNY; iy++)
333 ad[ix][iy] = (ad[ix][iy] - offset) * scale;
339 Array2dFile<T>::headerRead (void)
342 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
346 lseek (file_id, 0, SEEK_SET);
347 kuint16 file_signature;
348 kuint16 file_mPixelSize;
349 kuint16 file_mPixelType;
351 read_nint16 (&headersize, file_id);
352 read_nint16 (&file_signature, file_id);
353 read_nint16 (&num_labels, file_id);
354 read_nint16 (&file_mPixelType, file_id);
355 read_nint16 (&file_mPixelSize, file_id);
356 read_nint32 (&mNX, file_id);
357 read_nint32 (&mNY, file_id);
358 read_nint16 (&axis_increment_known, file_id);
359 read_nfloat64 (&mIncX, file_id);
360 read_nfloat64 (&mIncY, file_id);
361 read_nint16 (&axis_extent_known, file_id);
362 read_nfloat64 (&mMinX, file_id);
363 read_nfloat64 (&mMaxX, file_id);
364 read_nfloat64 (&mMinY, file_id);
365 read_nfloat64 (&mMaxY, file_id);
366 read_nfloat64 (&mOffsetPV, file_id);
367 read_nfloat64 (&mScalePV, file_id);
369 int read_headersize = lseek (file_id, 0, SEEK_CUR);
370 if (read_headersize != headersize) {
371 sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
374 if (file_signature != signature) {
375 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
378 if (file_mPixelType != mPixelType) {
379 sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_mPixelType, mPixelType);
382 if (file_mPixelSize != mPixelSize) {
383 sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_mPixelSize, mPixelSize);
392 Array2dFile<T>::headerWrite (void)
395 sys_error (ERR_WARNING, "Tried to write header with file closed");
399 lseek (file_id, 0, SEEK_SET);
400 write_nint16 (&headersize, file_id);
401 write_nint16 (&signature, file_id);
402 write_nint16 (&num_labels, file_id);
403 write_nint16 (&mPixelType, file_id);
404 write_nint16 (&mPixelSize, file_id);
405 write_nint32 (&mNX, file_id);
406 write_nint32 (&mNY, file_id);
407 write_nint16 (&axis_increment_known, file_id);
408 write_nfloat64 (&mIncX, file_id);
409 write_nfloat64 (&mIncY, file_id);
410 write_nint16 (&axis_extent_known, file_id);
411 write_nfloat64 (&mMinX, file_id);
412 write_nfloat64 (&mMaxX, file_id);
413 write_nfloat64 (&mMinY, file_id);
414 write_nfloat64 (&mMaxY, file_id);
415 write_nfloat64 (&mOffsetPV, file_id);
416 write_nfloat64 (&mScalePV, file_id);
418 headersize = lseek (file_id, 0, SEEK_CUR);
419 lseek (file_id, 0, SEEK_SET);
420 write_nint16 (&headersize, file_id);
427 Array2dFile<T>::arrayDataWrite (void)
430 sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
434 T** da = array.getArray();
438 lseek (file_id, headersize, SEEK_SET);
439 for (unsigned int ix = 0; ix < mNX; ix++)
440 for (unsigned int iy = 0; iy < mNY; iy++) {
441 T value = da[ix][iy];
442 ConvertReverseNetworkOrder (&value, sizeof(T));
443 write (file_id, &value, mPixelSize);
451 Array2dFile<T>::arrayDataRead (void)
454 sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0");
458 T** da = array.getArray();
462 lseek (file_id, headersize, SEEK_SET);
464 for (int ix = 0; ix < mNX; ix++)
465 for (unsigned int iy = 0; iy < mNY; iy++) {
466 read (file_id, &pixelBuffer, mPixelSize);
467 ConvertReverseNetworkOrder (&pixelBuffer, sizeof(T));
468 da[ix][iy] = pixelBuffer;
476 Array2dFile<T>::labelSeek (unsigned int label_num)
478 if (label_num > num_labels) {
479 sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
483 if (array.getArray() == NULL) { // Could not have written data if array not allocated
484 sys_error (ERR_WARNING, "array == NULL [labelSeek]");
488 off_t pos = headersize + array.sizeofArray();
489 if (lseek (file_id, pos, SEEK_SET) != pos) {
490 sys_error (ERR_WARNING, "Can't seek to end of data array");
494 for (int i = 0; i < label_num; i++)
496 pos += 22; // Skip to string length
497 if (lseek (file_id, pos, SEEK_SET) != pos) {
498 sys_error (ERR_WARNING, "Can't seek to string length");
502 read_nint16 (&strlength, file_id);
503 pos += 2 + strlength; // Skip label string length + data
504 if (lseek (file_id, pos, SEEK_SET) != pos) {
505 sys_error (ERR_WARNING, "Can't seek past label string");
515 Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
517 if (label_num >= num_labels) {
518 sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
522 if (! labelSeek (label_num)) {
523 sys_error (ERR_WARNING, "Error calling labelSeek");
527 read_nint16 (&label.label_type, file_id);
528 read_nint16 (&label.year, file_id);
529 read_nint16 (&label.month, file_id);
530 read_nint16 (&label.day, file_id);
531 read_nint16 (&label.hour, file_id);
532 read_nint16 (&label.minute, file_id);
533 read_nint16 (&label.second, file_id);
534 read_nfloat64 (&label.calc_time, file_id);
537 read_nint16 (&strlength, file_id);
538 char *str = new char [strlength+1];
539 read (file_id, str, strlength);
540 label.label_str = str;
548 Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
550 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
555 Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
557 Array2dFileLabel label (type, lstr, calc_time);
564 Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
566 labelSeek (num_labels);
568 write_nint16 (&label.label_type, file_id);
569 write_nint16 (&label.year, file_id);
570 write_nint16 (&label.month, file_id);
571 write_nint16 (&label.day, file_id);
572 write_nint16 (&label.hour, file_id);
573 write_nint16 (&label.minute, file_id);
574 write_nint16 (&label.second, file_id);
575 write_nfloat64 (&label.calc_time, file_id);
576 kuint16 strlength = label.label_str.length();
577 write_nint16 (&strlength, file_id);
578 write (file_id, static_cast<const void*>(label.label_str.c_str()), strlength);
588 Array2dFile<T>::labelsCopy (Array2dFile& copyFile, const char* const idStr)
591 for (int i = 0; i < copyFile.getNumLabels(); i++) {
593 copyFile.labelRead (l, i);
594 string lstr = l.getLabelString();
596 l.setLabelString (lstr);
603 Array2dFile<T>::arrayDataClear (void)
605 T** v = array.getArray();
607 for (unsigned int ix = 0; ix < mNX; ix++)
608 for (unsigned int iy = 0; iy < mNY; iy++)
618 class F32Image : public Array2dFile<kfloat32>
621 F32Image (const char* const fname, unsigned int nx, unsigned int ny)
622 : Array2dFile<kfloat32>::Array2dFile (fname, nx, ny)
624 setPixelType (FLOAT32);
627 F32Image (unsigned int nx, unsigned int ny)
628 : Array2dFile<kfloat32>::Array2dFile (nx, ny)
630 setPixelType (FLOAT32);
633 F32Image (const char* const fname)
634 : Array2dFile<kfloat32>::Array2dFile (fname)
636 setPixelType (FLOAT32);
640 MPI::Datatype getMPIDataType (void) const
641 { return MPI::FLOAT; }
646 class F64Image : public Array2dFile<kfloat64>
650 F64Image (const char* const fname, unsigned int nx, unsigned int ny)
651 : Array2dFile<kfloat64>::Array2dFile (fname, nx, ny)
653 setPixelType (FLOAT64);
656 F64Image (unsigned int nx, unsigned int ny)
657 : Array2dFile<kfloat64>::Array2dFile (nx, ny)
659 setPixelType (FLOAT64);
662 F64Image (const char* const fname)
663 : Array2dFile<kfloat64>::Array2dFile (fname)
665 setPixelType (FLOAT64);
669 MPI::Datatype getMPIDataType (void) const
670 { return MPI::DOUBLE; }
674 #define IMAGEFILE_64_BITS 1
675 #ifdef IMAGEFILE_64_BITS
676 typedef F64Image ImageFile;
677 typedef kfloat64 ImageFileValue;
678 typedef kfloat64* ImageFileColumn;
679 typedef kfloat64** ImageFileArray;
681 typedef F32Image ImageFile;
682 typedef kfloat32 ImageFileValue;
683 typedef kfloat32* ImageFileColumn;
684 typedef kfloat32** ImageFileArray;