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 const string& getLabelString (void) const
41 kfloat64 getCalcTime (void) const
44 kfloat64 getLabelType (void) const
45 { return label_type; }
47 void getDateString (string& str) const;
65 bool headerWrite (void);
67 bool headerRead (void);
69 bool arrayDataRead (void);
71 bool labelSeek (unsigned int label_num);
79 kuint16 axis_increment_known;
81 kuint16 axis_extent_known;
82 kfloat64 xmin, xmax, ymin, ymax;
83 kfloat64 pv_offset, pv_scale;
87 static const int INT8 = 1;
88 static const int UINT8 = 2;
89 static const int INT16 = 3;
90 static const int UINT16 = 4;
91 static const int INT32 = 5;
92 static const int UINT32 = 6;
93 static const int FLOAT32 = 7;
94 static const int FLOAT64 = 8;
96 Array2dFile (unsigned int nx, unsigned int ny);
97 Array2dFile (const char* const filename);
98 Array2dFile (const char* const filename, unsigned int nx, unsigned int ny);
101 unsigned int getNumLabels (void) const
102 { return num_labels; }
104 bool labelRead (Array2dFileLabel& label, unsigned int label_num);
106 void labelAdd (const Array2dFileLabel& label);
108 void labelAdd (const char* const label_str, double calc_time=0.);
110 void labelAdd (int type, const char* const label_str, double calc_time=0.);
112 void fileClose (void);
114 void set_axis_increment (double xinc, double yinc);
116 void set_axis_extent (double xmin, double xmax, double ymin, double ymax);
118 void get_pixel_extent (T& pvmin, T& pvmax);
120 void set_pixel_offset_extent (double offset, double scale);
122 T** getArray (void) const
123 { return (array == NULL ? NULL : array->getArray()); }
125 bool arrayDataWrite (void);
127 bool fileRead (void);
129 bool fileCreate (void);
131 const string& GetFilename (void) const
138 Array2dFile<T>::set_axis_increment (double xinc, double yinc)
140 axis_increment_known = true;
147 Array2dFile<T>::set_axis_extent (double xmin, double xmax, double ymin, double ymax)
149 axis_extent_known = true;
158 Array2dFile<T>::get_pixel_extent (T& pvmin, T& pvmax)
161 T** da = array.GetArray();
162 pvmax = pvmin = da[0][0];
163 for (int ix = 0; ix < nx; ix++)
164 for (int iy = 0; iy < ny; iy++)
165 if (pvmax < da[ix][iy])
167 else if (pvmin > da[ix][iy])
174 Array2dFile<T>::set_pixel_offset_extent (double offset, double scale)
182 Array2dFile<T>::init (void)
184 pixel_size = sizeof(T);
185 signature = ('I' * 256 + 'F');
191 axis_increment_known = false;
192 axis_extent_known = false;
194 xmin = xmax = ymin = ymax = 0;
201 const type_info& t_id = typeid(T);
202 cout << t_id.name() << endl;
203 const type_info& comp_id = typeid(T);
206 pixel_type = FLOAT64;
207 else if (t_id == typeid(kfloat32))
208 pixel_type = FLOAT32;
209 else if (t_id == typeid(kint32))
211 else if (t_id == typeid(kuint32))
213 else if (t_id == typeid(kint16))
215 else if (t_id == typeid(kuint16))
217 else if (t_id == typeid(kint8))
219 else if (t_id == typeid(kuint8))
225 bHeaderWritten = false;
226 bDataWritten = false;
230 Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
235 array = new Array2d<T> (nx, ny);
239 Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
245 array = new Array2d<T> (nx, ny);
249 Array2dFile<T>::Array2dFile (const char * const str)
257 Array2dFile<T>::fileCreate (void)
259 // io = new iostream(filename, ios::out | ios::in | ios::trunc | io::binary);
260 if ((file_id = open (filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
261 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
270 Array2dFile<T>::fileRead (void)
272 // io = new iostream(filename, ios::out | ios::in | io::binary);
273 if ((file_id = open (filename.c_str(), O_RDONLY | O_BINARY)) < 0) {
274 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
282 array = new Array2d<T> (nx, ny);
291 Array2dFile<T>::headerRead (void)
294 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
298 lseek (file_id, 0, SEEK_SET);
299 kuint16 file_signature;
300 kuint16 file_pixel_size;
301 kuint16 file_pixel_type;
303 read_nint16 (&headersize, file_id);
304 read_nint16 (&file_signature, file_id);
305 read_nint16 (&num_labels, file_id);
306 read_nint16 (&file_pixel_type, file_id);
307 read_nint16 (&file_pixel_size, file_id);
308 read_nint32 (&nx, file_id);
309 read_nint32 (&ny, file_id);
310 read_nint16 (&axis_increment_known, file_id);
311 read_nfloat64 (&xinc, file_id);
312 read_nfloat64 (&yinc, file_id);
313 read_nint16 (&axis_extent_known, file_id);
314 read_nfloat64 (&xmin, file_id);
315 read_nfloat64 (&xmax, file_id);
316 read_nfloat64 (&ymin, file_id);
317 read_nfloat64 (&ymax, file_id);
318 read_nfloat64 (&pv_offset, file_id);
319 read_nfloat64 (&pv_scale, file_id);
321 int read_headersize = lseek (file_id, 0, SEEK_CUR);
322 if (read_headersize != headersize) {
323 sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
326 if (file_signature != signature) {
327 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
330 if (file_pixel_type != pixel_type) {
331 sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_pixel_type, pixel_type);
334 if (file_pixel_size != pixel_size) {
335 sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_pixel_size, pixel_size);
344 Array2dFile<T>::headerWrite (void)
347 sys_error (ERR_WARNING, "Tried to write header with file closed");
351 lseek (file_id, 0, SEEK_SET);
352 write_nint16 (&headersize, file_id);
353 write_nint16 (&signature, file_id);
354 write_nint16 (&num_labels, file_id);
355 write_nint16 (&pixel_type, file_id);
356 write_nint16 (&pixel_size, file_id);
357 write_nint32 (&nx, file_id);
358 write_nint32 (&ny, file_id);
359 write_nint16 (&axis_increment_known, file_id);
360 write_nfloat64 (&xinc, file_id);
361 write_nfloat64 (&yinc, file_id);
362 write_nint16 (&axis_extent_known, file_id);
363 write_nfloat64 (&xmin, file_id);
364 write_nfloat64 (&xmax, file_id);
365 write_nfloat64 (&ymin, file_id);
366 write_nfloat64 (&ymax, file_id);
367 write_nfloat64 (&pv_offset, file_id);
368 write_nfloat64 (&pv_scale, file_id);
370 headersize = lseek (file_id, 0, SEEK_CUR);
371 lseek (file_id, 0, SEEK_SET);
372 write_nint16 (&headersize, file_id);
379 Array2dFile<T>::arrayDataWrite (void)
382 sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
386 lseek (file_id, headersize, SEEK_SET);
387 for (unsigned int ix = 0; ix < nx; ix++)
389 write (file_id, array->array_data[ix], ny * pixel_size);
397 Array2dFile<T>::fileClose (void)
400 if (file_id >= 0 && array != NULL) {
408 Array2dFile<T>::~Array2dFile (void)
416 Array2dFile<T>::arrayDataRead (void)
419 sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0");
423 lseek (file_id, headersize, SEEK_SET);
424 for (int ix = 0; ix < nx; ix++)
426 read (file_id, array->array_data[ix], ny * pixel_size);
434 Array2dFile<T>::labelSeek (unsigned int label_num)
436 if (label_num > num_labels) {
437 sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
441 if (array == NULL) { // Could not have written data if array not allocated
442 sys_error (ERR_WARNING, "array == NULL [labelSeek]");
446 off_t pos = headersize + array->sizeofArray();
447 if (lseek (file_id, pos, SEEK_SET) != pos) {
448 sys_error (ERR_WARNING, "Can't seek to end of data array");
452 for (int i = 0; i < label_num; i++)
454 pos += 22; // Skip to string length
455 if (lseek (file_id, pos, SEEK_SET) != pos) {
456 sys_error (ERR_WARNING, "Can't seek to string length");
460 read_nint16 (&strlength, file_id);
461 pos += 2 + strlength; // Skip label string length + data
462 if (lseek (file_id, pos, SEEK_SET) != pos) {
463 sys_error (ERR_WARNING, "Can't seek past label string");
473 Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
475 if (label_num >= num_labels) {
476 sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
480 if (! labelSeek (label_num)) {
481 sys_error (ERR_WARNING, "Error calling labelSeek");
485 read_nint16 (&label.label_type, file_id);
486 read_nint16 (&label.year, file_id);
487 read_nint16 (&label.month, file_id);
488 read_nint16 (&label.day, file_id);
489 read_nint16 (&label.hour, file_id);
490 read_nint16 (&label.minute, file_id);
491 read_nint16 (&label.second, file_id);
492 read_nfloat64 (&label.calc_time, file_id);
495 read_nint16 (&strlength, file_id);
496 char *str = new char [strlength+1];
497 read (file_id, str, strlength);
498 label.label_str = str;
506 Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
508 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
513 Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
515 Array2dFileLabel label (type, lstr, calc_time);
522 Array2dFile<T>::labelAdd (const Array2dFileLabel& label)
524 labelSeek (num_labels);
526 write_nint16 (&label.label_type, file_id);
527 write_nint16 (&label.year, file_id);
528 write_nint16 (&label.month, file_id);
529 write_nint16 (&label.day, file_id);
530 write_nint16 (&label.hour, file_id);
531 write_nint16 (&label.minute, file_id);
532 write_nint16 (&label.second, file_id);
533 write_nfloat64 (&label.calc_time, file_id);
534 kuint16 strlength = label.label_str.length();
535 write_nint16 (&strlength, file_id);
536 write (file_id, static_cast<const void*>(label.label_str.c_str()), strlength);
554 Array2dFile<kfloat32> adf;
556 F32Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
558 adf.pixel_type = Array2dFile<kfloat32>::FLOAT32;
561 F32Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
563 adf.pixel_type = Array2dFile<kfloat32>::FLOAT32;
566 F32Image (const char* const fname) : adf (fname)
568 adf.pixel_type = Array2dFile<kfloat32>::FLOAT32;
572 MPI_Datatype getMPIDataType (void) const
573 { return MPI_FLOAT; }
580 Array2dFile<kfloat64> adf;
583 MPI_Datatype getMPIDataType (void) const
584 { return MPI_DOUBLE; }
587 F64Image (const char* const fname, unsigned int nx, unsigned int ny) : adf (fname, nx, ny)
589 adf.pixel_type = Array2dFile<kfloat64>::FLOAT64;
592 F64Image (unsigned int nx, unsigned int ny) : adf (nx, ny)
594 adf.pixel_type = Array2dFile<kfloat64>::FLOAT64;
597 F64Image (const char* const fname) : adf (fname)
599 adf.pixel_type = Array2dFile<kfloat64>::FLOAT64;
603 typedef F64Image ImageFile;
604 typedef kfloat64 ImageFileValue;
605 typedef kfloat64* ImageFileColumn;
606 typedef kfloat64** ImageFileArray;