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 == NULL ? NULL : 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 = new Array2d<T> (mNX, mNY);
167 Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
173 array = new Array2d<T> (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;
204 const type_info& t_id = typeid(T);
205 cout << t_id.name() << endl;
206 const type_info& comp_id = typeid(T);
209 mPixelType = FLOAT64;
210 else if (t_id == typeid(kfloat32))
211 mPixelType = FLOAT32;
212 else if (t_id == typeid(kint32))
214 else if (t_id == typeid(kuint32))
216 else if (t_id == typeid(kint16))
218 else if (t_id == typeid(kuint16))
220 else if (t_id == typeid(kint8))
222 else if (t_id == typeid(kuint8))
228 bHeaderWritten = false;
229 bDataWritten = false;
233 Array2dFile<T>::~Array2dFile (void)
241 Array2dFile<T>::fileClose (void)
244 if (file_id >= 0 && array != NULL) {
253 Array2dFile<T>::fileCreate (void)
255 // io = new iostream(filename, ios::out | ios::in | ios::trunc | io::binary);
256 if ((file_id = open (filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
257 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
266 Array2dFile<T>::fileRead (void)
268 // io = new iostream(filename, ios::out | ios::in | io::binary);
269 if ((file_id = open (filename.c_str(), O_RDONLY | O_BINARY)) < 0) {
270 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
278 array = new Array2d<T> (mNX, mNY);
287 Array2dFile<T>::setAxisIncrement (double incX, double incY)
289 axis_increment_known = true;
296 Array2dFile<T>::setAxisExtent (double minX, double maxX, double minY, double maxY)
298 axis_extent_known = true;
307 Array2dFile<T>::getPixelValueRange (T& pvmin, T& pvmax) const
310 T** da = array->getArray();
311 pvmax = pvmin = da[0][0];
312 for (int ix = 0; ix < mNX; ix++)
313 for (int iy = 0; iy < mNY; iy++)
314 if (pvmax < da[ix][iy])
316 else if (pvmin > da[ix][iy])
323 Array2dFile<T>::doPixelOffsetScale (double offset, double scale)
329 T** ad = adf->getArray();
330 for (unsigned int ix = 0; ix < mNX; ix++)
331 for (unsigned int iy = 0; iy < mNY; iy++)
332 ad[ix][iy] = (ad[ix][iy] - offset) * scale;
338 Array2dFile<T>::headerRead (void)
341 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
345 lseek (file_id, 0, SEEK_SET);
346 kuint16 file_signature;
347 kuint16 file_mPixelSize;
348 kuint16 file_mPixelType;
350 read_nint16 (&headersize, file_id);
351 read_nint16 (&file_signature, file_id);
352 read_nint16 (&num_labels, file_id);
353 read_nint16 (&file_mPixelType, file_id);
354 read_nint16 (&file_mPixelSize, file_id);
355 read_nint32 (&mNX, file_id);
356 read_nint32 (&mNY, file_id);
357 read_nint16 (&axis_increment_known, file_id);
358 read_nfloat64 (&mIncX, file_id);
359 read_nfloat64 (&mIncY, file_id);
360 read_nint16 (&axis_extent_known, file_id);
361 read_nfloat64 (&mMinX, file_id);
362 read_nfloat64 (&mMaxX, file_id);
363 read_nfloat64 (&mMinY, file_id);
364 read_nfloat64 (&mMaxY, file_id);
365 read_nfloat64 (&mOffsetPV, file_id);
366 read_nfloat64 (&mScalePV, file_id);
368 int read_headersize = lseek (file_id, 0, SEEK_CUR);
369 if (read_headersize != headersize) {
370 sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
373 if (file_signature != signature) {
374 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
377 if (file_mPixelType != mPixelType) {
378 sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_mPixelType, mPixelType);
381 if (file_mPixelSize != mPixelSize) {
382 sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_mPixelSize, mPixelSize);
391 Array2dFile<T>::headerWrite (void)
394 sys_error (ERR_WARNING, "Tried to write header with file closed");
398 lseek (file_id, 0, SEEK_SET);
399 write_nint16 (&headersize, file_id);
400 write_nint16 (&signature, file_id);
401 write_nint16 (&num_labels, file_id);
402 write_nint16 (&mPixelType, file_id);
403 write_nint16 (&mPixelSize, file_id);
404 write_nint32 (&mNX, file_id);
405 write_nint32 (&mNY, file_id);
406 write_nint16 (&axis_increment_known, file_id);
407 write_nfloat64 (&mIncX, file_id);
408 write_nfloat64 (&mIncY, file_id);
409 write_nint16 (&axis_extent_known, file_id);
410 write_nfloat64 (&mMinX, file_id);
411 write_nfloat64 (&mMaxX, file_id);
412 write_nfloat64 (&mMinY, file_id);
413 write_nfloat64 (&mMaxY, file_id);
414 write_nfloat64 (&mOffsetPV, file_id);
415 write_nfloat64 (&mScalePV, file_id);
417 headersize = lseek (file_id, 0, SEEK_CUR);
418 lseek (file_id, 0, SEEK_SET);
419 write_nint16 (&headersize, file_id);
426 Array2dFile<T>::arrayDataWrite (void)
429 sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
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 = array->array_data[ix][iy];
437 ConvertNetworkOrder (&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 lseek (file_id, headersize, SEEK_SET);
455 for (int ix = 0; ix < mNX; ix++)
456 for (unsigned int iy = 0; iy < mNY; iy++) {
457 read (file_id, &pixelBuffer, mPixelSize);
458 ConvertNetworkOrder (&pixelBuffer, sizeof(T));
459 array->array_data[ix][iy] = pixelBuffer;
467 Array2dFile<T>::labelSeek (unsigned int label_num)
469 if (label_num > num_labels) {
470 sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
474 if (array == NULL) { // Could not have written data if array not allocated
475 sys_error (ERR_WARNING, "array == NULL [labelSeek]");
479 off_t pos = headersize + array->sizeofArray();
480 if (lseek (file_id, pos, SEEK_SET) != pos) {
481 sys_error (ERR_WARNING, "Can't seek to end of data array");
485 for (int i = 0; i < label_num; i++)
487 pos += 22; // Skip to string length
488 if (lseek (file_id, pos, SEEK_SET) != pos) {
489 sys_error (ERR_WARNING, "Can't seek to string length");
493 read_nint16 (&strlength, file_id);
494 pos += 2 + strlength; // Skip label string length + data
495 if (lseek (file_id, pos, SEEK_SET) != pos) {
496 sys_error (ERR_WARNING, "Can't seek past label string");
506 Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
508 if (label_num >= num_labels) {
509 sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
513 if (! labelSeek (label_num)) {
514 sys_error (ERR_WARNING, "Error calling labelSeek");
518 read_nint16 (&label.label_type, file_id);
519 read_nint16 (&label.year, file_id);
520 read_nint16 (&label.month, file_id);
521 read_nint16 (&label.day, file_id);
522 read_nint16 (&label.hour, file_id);
523 read_nint16 (&label.minute, file_id);
524 read_nint16 (&label.second, file_id);
525 read_nfloat64 (&label.calc_time, file_id);
528 read_nint16 (&strlength, file_id);
529 char *str = new char [strlength+1];
530 read (file_id, str, strlength);
531 label.label_str = str;
539 Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
541 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
546 Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
548 Array2dFileLabel label (type, lstr, calc_time);
555 Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
557 labelSeek (num_labels);
559 write_nint16 (&label.label_type, file_id);
560 write_nint16 (&label.year, file_id);
561 write_nint16 (&label.month, file_id);
562 write_nint16 (&label.day, file_id);
563 write_nint16 (&label.hour, file_id);
564 write_nint16 (&label.minute, file_id);
565 write_nint16 (&label.second, file_id);
566 write_nfloat64 (&label.calc_time, file_id);
567 kuint16 strlength = label.label_str.length();
568 write_nint16 (&strlength, file_id);
569 write (file_id, static_cast<const void*>(label.label_str.c_str()), strlength);
579 Array2dFile<T>::labelsCopy (Array2dFile& copyFile, const char* const idStr)
582 for (int i = 0; i < copyFile.getNumLabels(); i++) {
584 copyFile.labelRead (l, i);
585 string lstr = l.getLabelString();
587 l.setLabelString (lstr);
594 Array2dFile<T>::arrayDataClear (void)
597 T** v = array->getArray();
598 for (unsigned int ix = 0; ix < mNX; ix++)
599 for (unsigned int iy = 0; iy < mNY; iy++)
612 Array2dFile<kfloat32> adf;
614 F32Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
616 adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
619 F32Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
621 adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
624 F32Image (const char* const fname) : adf (fname)
626 adf.setPixelType (Array2dFile<kfloat64>::FLOAT32);
629 kfloat32** getArray(void) const
630 { return adf.getArray(); }
632 kuint32 nx(void) const
635 kuint32 ny(void) const
639 MPI::Datatype getMPIDataType (void) const
640 { return MPI::FLOAT; }
648 Array2dFile<kfloat64> adf;
651 MPI::Datatype getMPIDataType (void) const
652 { return MPI::DOUBLE; }
655 F64Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
657 adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
660 F64Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
662 adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
665 F64Image (const char* const fname) : adf (fname)
667 adf.setPixelType (Array2dFile<kfloat64>::FLOAT64);
670 kfloat64** getArray(void) const
671 { return adf.getArray(); }
673 kuint32 nx(void) const
676 kuint32 ny(void) const
680 typedef F64Image ImageFile;
681 typedef kfloat64 ImageFileValue;
682 typedef kfloat64* ImageFileColumn;
683 typedef kfloat64** ImageFileArray;