10 class Array2dFileLabel
26 static const int L_EMPTY = 0;
27 static const int L_HISTORY = 1;
28 static const int L_USER = 2;
32 Array2dFileLabel(const char* const str, double ctime = 0.);
34 Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
38 string getLabelString (void) const
41 kfloat64 getCalcTime (void) const
44 kfloat64 getLabelType (void) const
45 { return label_type; }
47 string& setLabelString (const char* const str)
48 { label_str = str; return (label_str); }
50 string& setLabelString (const string& str)
51 { label_str = str; return (label_str); }
53 void getDateString (string& str) const;
71 bool headerWrite (void);
73 bool headerRead (void);
75 bool arrayDataRead (void);
77 bool labelSeek (unsigned int label_num);
80 kuint16 axis_increment_known;
81 kfloat64 mIncX, mIncY;
82 kuint16 axis_extent_known;
83 kfloat64 mMinX, mMaxX, mMinY, mMaxY;
84 kfloat64 mOffsetPV, mScalePV;
93 static const int INT8 = 1;
94 static const int UINT8 = 2;
95 static const int INT16 = 3;
96 static const int UINT16 = 4;
97 static const int INT32 = 5;
98 static const int UINT32 = 6;
99 static const int FLOAT32 = 7;
100 static const int FLOAT64 = 8;
102 Array2dFile (unsigned int nx, unsigned int ny);
103 Array2dFile (const char* const filename);
104 Array2dFile (const char* const filename, unsigned int nx, unsigned int ny);
107 unsigned int getNumLabels (void) const
108 { return num_labels; }
110 bool labelRead (Array2dFileLabel& label, unsigned int label_num);
112 void labelAdd (const Array2dFileLabel& label);
114 void labelAdd (const char* const label_str, double calc_time=0.);
116 void labelAdd (int type, const char* const label_str, double calc_time=0.);
118 void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
120 void fileClose (void);
122 void setPixelType (int type)
123 { mPixelType = type; }
125 kuint32 nx (void) const
128 kuint32 ny (void) const
131 void setAxisIncrement (double mIncX, double mIncY);
133 void setAxisExtent (double mMinX, double mMaxX, double mMinY, double mMaxY);
135 void getPixelValueRange (T& pvmin, T& pvmax) const;
137 void doPixelOffsetScale (double offset, double scale);
139 T** getArray (void) const
140 { return (array == NULL ? NULL : array->getArray()); }
142 bool arrayDataWrite (void);
144 void arrayDataClear (void);
146 bool fileRead (void);
148 bool fileCreate (void);
150 const string& GetFilename (void) const
156 Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
161 array = new Array2d<T> (mNX, mNY);
165 Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
171 array = new Array2d<T> (mNX, mNY);
175 Array2dFile<T>::Array2dFile (const char * const str)
183 Array2dFile<T>::init (void)
185 mPixelSize = sizeof(T);
186 signature = ('I' * 256 + 'F');
192 axis_increment_known = false;
193 axis_extent_known = false;
195 mMinX = mMaxX = mMinY = mMaxY = 0;
202 const type_info& t_id = typeid(T);
203 cout << t_id.name() << endl;
204 const type_info& comp_id = typeid(T);
207 mPixelType = FLOAT64;
208 else if (t_id == typeid(kfloat32))
209 mPixelType = FLOAT32;
210 else if (t_id == typeid(kint32))
212 else if (t_id == typeid(kuint32))
214 else if (t_id == typeid(kint16))
216 else if (t_id == typeid(kuint16))
218 else if (t_id == typeid(kint8))
220 else if (t_id == typeid(kuint8))
226 bHeaderWritten = false;
227 bDataWritten = false;
231 Array2dFile<T>::~Array2dFile (void)
239 Array2dFile<T>::fileClose (void)
242 if (file_id >= 0 && array != NULL) {
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());
276 array = new Array2d<T> (mNX, mNY);
285 Array2dFile<T>::setAxisIncrement (double incX, double incY)
287 axis_increment_known = true;
294 Array2dFile<T>::setAxisExtent (double minX, double maxX, double minY, double maxY)
296 axis_extent_known = true;
305 Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
308 T** da = array->getArray();
309 pvmax = pvmin = da[0][0];
310 for (int ix = 0; ix < mNX; ix++)
311 for (int iy = 0; iy < mNY; iy++)
312 if (pvmax < da[ix][iy])
314 else if (pvmin > da[ix][iy])
321 Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
327 T** ad = adf->getArray();
328 for (unsigned int ix = 0; ix < mNX; ix++)
329 for (unsigned int iy = 0; iy < mNY; iy++)
330 ad[ix][iy] = (ad[ix][iy] - offset) * scale;
336 Array2dFile<T>::headerRead (void)
339 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
343 lseek (file_id, 0, SEEK_SET);
344 kuint16 file_signature;
345 kuint16 file_mPixelSize;
346 kuint16 file_mPixelType;
348 read_nint16 (&headersize, file_id);
349 read_nint16 (&file_signature, file_id);
350 read_nint16 (&num_labels, file_id);
351 read_nint16 (&file_mPixelType, file_id);
352 read_nint16 (&file_mPixelSize, file_id);
353 read_nint32 (&mNX, file_id);
354 read_nint32 (&mNY, file_id);
355 read_nint16 (&axis_increment_known, file_id);
356 read_nfloat64 (&mIncX, file_id);
357 read_nfloat64 (&mIncY, file_id);
358 read_nint16 (&axis_extent_known, file_id);
359 read_nfloat64 (&mMinX, file_id);
360 read_nfloat64 (&mMaxX, file_id);
361 read_nfloat64 (&mMinY, file_id);
362 read_nfloat64 (&mMaxY, file_id);
363 read_nfloat64 (&mOffsetPV, file_id);
364 read_nfloat64 (&mScalePV, file_id);
366 int read_headersize = lseek (file_id, 0, SEEK_CUR);
367 if (read_headersize != headersize) {
368 sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
371 if (file_signature != signature) {
372 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
375 if (file_mPixelType != mPixelType) {
376 sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_mPixelType, mPixelType);
379 if (file_mPixelSize != mPixelSize) {
380 sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_mPixelSize, mPixelSize);
389 Array2dFile<T>::headerWrite (void)
392 sys_error (ERR_WARNING, "Tried to write header with file closed");
396 lseek (file_id, 0, SEEK_SET);
397 write_nint16 (&headersize, file_id);
398 write_nint16 (&signature, file_id);
399 write_nint16 (&num_labels, file_id);
400 write_nint16 (&mPixelType, file_id);
401 write_nint16 (&mPixelSize, file_id);
402 write_nint32 (&mNX, file_id);
403 write_nint32 (&mNY, file_id);
404 write_nint16 (&axis_increment_known, file_id);
405 write_nfloat64 (&mIncX, file_id);
406 write_nfloat64 (&mIncY, file_id);
407 write_nint16 (&axis_extent_known, file_id);
408 write_nfloat64 (&mMinX, file_id);
409 write_nfloat64 (&mMaxX, file_id);
410 write_nfloat64 (&mMinY, file_id);
411 write_nfloat64 (&mMaxY, file_id);
412 write_nfloat64 (&mOffsetPV, file_id);
413 write_nfloat64 (&mScalePV, file_id);
415 headersize = lseek (file_id, 0, SEEK_CUR);
416 lseek (file_id, 0, SEEK_SET);
417 write_nint16 (&headersize, file_id);
424 Array2dFile<T>::arrayDataWrite (void)
427 sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
431 lseek (file_id, headersize, SEEK_SET);
432 for (unsigned int ix = 0; ix < mNX; ix++)
433 for (unsigned int iy = 0; iy < mNY; iy++) {
434 T value = array->array_data[ix][iy];
435 ConvertNetworkOrder (&value, sizeof(T));
436 write (file_id, &value, mPixelSize);
444 Array2dFile<T>::arrayDataRead (void)
447 sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0");
451 lseek (file_id, headersize, SEEK_SET);
453 for (int ix = 0; ix < mNX; ix++)
454 for (unsigned int iy = 0; iy < mNY; iy++) {
455 read (file_id, &pixelBuffer, mPixelSize);
456 ConvertNetworkOrder (&pixelBuffer, sizeof(T));
457 array->array_data[ix][iy] = pixelBuffer;
465 Array2dFile<T>::labelSeek (unsigned int label_num)
467 if (label_num > num_labels) {
468 sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
472 if (array == NULL) { // Could not have written data if array not allocated
473 sys_error (ERR_WARNING, "array == NULL [labelSeek]");
477 off_t pos = headersize + array->sizeofArray();
478 if (lseek (file_id, pos, SEEK_SET) != pos) {
479 sys_error (ERR_WARNING, "Can't seek to end of data array");
483 for (int i = 0; i < label_num; i++)
485 pos += 22; // Skip to string length
486 if (lseek (file_id, pos, SEEK_SET) != pos) {
487 sys_error (ERR_WARNING, "Can't seek to string length");
491 read_nint16 (&strlength, file_id);
492 pos += 2 + strlength; // Skip label string length + data
493 if (lseek (file_id, pos, SEEK_SET) != pos) {
494 sys_error (ERR_WARNING, "Can't seek past label string");
504 Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
506 if (label_num >= num_labels) {
507 sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
511 if (! labelSeek (label_num)) {
512 sys_error (ERR_WARNING, "Error calling labelSeek");
516 read_nint16 (&label.label_type, file_id);
517 read_nint16 (&label.year, file_id);
518 read_nint16 (&label.month, file_id);
519 read_nint16 (&label.day, file_id);
520 read_nint16 (&label.hour, file_id);
521 read_nint16 (&label.minute, file_id);
522 read_nint16 (&label.second, file_id);
523 read_nfloat64 (&label.calc_time, file_id);
526 read_nint16 (&strlength, file_id);
527 char *str = new char [strlength+1];
528 read (file_id, str, strlength);
529 label.label_str = str;
537 Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
539 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
544 Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
546 Array2dFileLabel label (type, lstr, calc_time);
553 Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
555 labelSeek (num_labels);
557 write_nint16 (&label.label_type, file_id);
558 write_nint16 (&label.year, file_id);
559 write_nint16 (&label.month, file_id);
560 write_nint16 (&label.day, file_id);
561 write_nint16 (&label.hour, file_id);
562 write_nint16 (&label.minute, file_id);
563 write_nint16 (&label.second, file_id);
564 write_nfloat64 (&label.calc_time, file_id);
565 kuint16 strlength = label.label_str.length();
566 write_nint16 (&strlength, file_id);
567 write (file_id, static_cast<const void*>(label.label_str.c_str()), strlength);
577 Array2dFile<T>::labelsCopy (Array2dFile& copyFile, const char* const idStr)
580 for (int i = 0; i < copyFile.getNumLabels(); i++) {
582 copyFile.labelRead (l, i);
583 string lstr = l.getLabelString();
585 l.setLabelString (lstr);
592 Array2dFile<T>::arrayDataClear (void)
595 T** v = array->getArray();
596 for (unsigned int ix = 0; ix < mNX; ix++)
597 for (unsigned int iy = 0; iy < mNY; iy++)
610 Array2dFile<kfloat32> adf;
612 F32Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
614 adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
617 F32Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
619 adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
622 F32Image (const char* const fname) : adf (fname)
624 adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
627 kfloat32** getArray(void) const
628 { return adf.getArray(); }
630 kuint32 nx(void) const
633 kuint32 ny(void) const
637 MPI_Datatype getMPIDataType (void) const
638 { return MPI_FLOAT; }
645 Array2dFile<kfloat64> adf;
648 MPI_Datatype getMPIDataType (void) const
649 { return MPI_DOUBLE; }
652 F64Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
654 adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
657 F64Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
659 adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
662 F64Image (const char* const fname) : adf (fname)
664 adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
667 kfloat64** getArray(void) const
668 { return adf.getArray(); }
670 kuint32 nx(void) const
673 kuint32 ny(void) const
677 typedef F64Image ImageFile;
678 typedef kfloat64 ImageFileValue;
679 typedef kfloat64* ImageFileColumn;
680 typedef kfloat64** ImageFileArray;