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 virtual unsigned int getNumLabels (void) const
102 { return num_labels; }
104 bool labelRead (Array2dFileLabel& label, unsigned int label_num);
106 bool labelAdd (const char* const label_str, double calc_time=0.);
108 bool labelAdd (int type, const char* const label_str, double calc_time=0.);
110 void fileClose (void);
112 void set_axis_increment (double xinc, double yinc);
114 void set_axis_extent (double xmin, double xmax, double ymin, double ymax);
116 void get_pixel_extent (T& pvmin, T& pvmax);
118 void set_pixel_offset_extent (double offset, double scale);
120 T** getArray (void) const
121 { return (array == NULL ? NULL : array->getArray()); }
123 bool arrayDataWrite (void);
125 bool fileRead (void);
127 bool fileCreate (void);
129 const string& GetFilename (void) const
136 Array2dFile<T>::set_axis_increment (double xinc, double yinc)
138 axis_increment_known = true;
145 Array2dFile<T>::set_axis_extent (double xmin, double xmax, double ymin, double ymax)
147 axis_extent_known = true;
156 Array2dFile<T>::get_pixel_extent (T& pvmin, T& pvmax)
159 T** da = array.GetArray();
160 pvmax = pvmin = da[0][0];
161 for (int ix = 0; ix < nx; ix++)
162 for (int iy = 0; iy < ny; iy++)
163 if (pvmax < da[ix][iy])
165 else if (pvmin > da[ix][iy])
172 Array2dFile<T>::set_pixel_offset_extent (double offset, double scale)
180 Array2dFile<T>::init (void)
182 pixel_size = sizeof(T);
183 signature = ('I' * 256 + 'F');
189 axis_increment_known = false;
190 axis_extent_known = false;
192 xmin = xmax = ymin = ymax = 0;
199 const type_info& t_id = typeid(T);
200 cout << t_id.name() << endl;
201 const type_info& comp_id = typeid(T);
204 pixel_type = FLOAT64;
205 else if (t_id == typeid(kfloat32))
206 pixel_type = FLOAT32;
207 else if (t_id == typeid(kint32))
209 else if (t_id == typeid(kuint32))
211 else if (t_id == typeid(kint16))
213 else if (t_id == typeid(kuint16))
215 else if (t_id == typeid(kint8))
217 else if (t_id == typeid(kuint8))
223 bHeaderWritten = false;
224 bDataWritten = false;
228 Array2dFile<T>::Array2dFile (unsigned int x, unsigned int y)
233 array = new Array2d<T> (nx, ny);
237 Array2dFile<T>::Array2dFile (const char * const str, unsigned int x, unsigned int y)
243 array = new Array2d<T> (nx, ny);
247 Array2dFile<T>::Array2dFile (const char * const str)
255 Array2dFile<T>::fileCreate (void)
257 // io = new iostream(filename, ios::out | ios::in | ios::trunc | io::binary);
258 if ((file_id = open (filename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
259 sys_error (ERR_WARNING, "Error opening file %s for writing [fileCreate]", filename.c_str());
268 Array2dFile<T>::fileRead (void)
270 // io = new iostream(filename, ios::out | ios::in | io::binary);
271 if ((file_id = open (filename.c_str(), O_RDONLY | O_BINARY)) < 0) {
272 sys_error (ERR_WARNING, "Unable to open file %s [fileRead]", filename.c_str());
280 array = new Array2d<T> (nx, ny);
289 Array2dFile<T>::headerRead (void)
292 sys_error (ERR_WARNING, "Tried to read header with file closed [headerRead]");
296 lseek (file_id, 0, SEEK_SET);
297 kuint16 file_signature;
298 kuint16 file_pixel_size;
299 kuint16 file_pixel_type;
301 read_nint16 (&headersize, file_id);
302 read_nint16 (&file_signature, file_id);
303 read_nint16 (&num_labels, file_id);
304 read_nint16 (&file_pixel_type, file_id);
305 read_nint16 (&file_pixel_size, file_id);
306 read_nint32 (&nx, file_id);
307 read_nint32 (&ny, file_id);
308 read_nint16 (&axis_increment_known, file_id);
309 read_nfloat64 (&xinc, file_id);
310 read_nfloat64 (&yinc, file_id);
311 read_nint16 (&axis_extent_known, file_id);
312 read_nfloat64 (&xmin, file_id);
313 read_nfloat64 (&xmax, file_id);
314 read_nfloat64 (&ymin, file_id);
315 read_nfloat64 (&ymax, file_id);
316 read_nfloat64 (&pv_offset, file_id);
317 read_nfloat64 (&pv_scale, file_id);
319 int read_headersize = lseek (file_id, 0, SEEK_CUR);
320 if (read_headersize != headersize) {
321 sys_error (ERR_WARNING, "Read headersize %d != file headersize %d", read_headersize, headersize);
324 if (file_signature != signature) {
325 sys_error (ERR_WARNING, "File signature %d != true signature %d", file_signature, signature);
328 if (file_pixel_type != pixel_type) {
329 sys_error (ERR_WARNING, "File pixel type %d != class pixel type %d", file_pixel_type, pixel_type);
332 if (file_pixel_size != pixel_size) {
333 sys_error (ERR_WARNING, "File pixel size %d != class pixel size %d", file_pixel_size, pixel_size);
342 Array2dFile<T>::headerWrite (void)
345 sys_error (ERR_WARNING, "Tried to write header with file closed");
349 lseek (file_id, 0, SEEK_SET);
350 write_nint16 (&headersize, file_id);
351 write_nint16 (&signature, file_id);
352 write_nint16 (&num_labels, file_id);
353 write_nint16 (&pixel_type, file_id);
354 write_nint16 (&pixel_size, file_id);
355 write_nint32 (&nx, file_id);
356 write_nint32 (&ny, file_id);
357 write_nint16 (&axis_increment_known, file_id);
358 write_nfloat64 (&xinc, file_id);
359 write_nfloat64 (&yinc, file_id);
360 write_nint16 (&axis_extent_known, file_id);
361 write_nfloat64 (&xmin, file_id);
362 write_nfloat64 (&xmax, file_id);
363 write_nfloat64 (&ymin, file_id);
364 write_nfloat64 (&ymax, file_id);
365 write_nfloat64 (&pv_offset, file_id);
366 write_nfloat64 (&pv_scale, file_id);
368 headersize = lseek (file_id, 0, SEEK_CUR);
369 lseek (file_id, 0, SEEK_SET);
370 write_nint16 (&headersize, file_id);
377 Array2dFile<T>::arrayDataWrite (void)
380 sys_error (ERR_WARNING, "Tried to arrayDataWrite with file_id < 0");
384 lseek (file_id, headersize, SEEK_SET);
385 for (int ix = 0; ix < nx; ix++)
387 write (file_id, array->array_data[ix], ny * pixel_size);
395 Array2dFile<T>::fileClose (void)
398 if (file_id >= 0 && array != NULL) {
406 Array2dFile<T>::~Array2dFile (void)
414 Array2dFile<T>::arrayDataRead (void)
417 sys_error (ERR_WARNING, "Tried to arrayDataRead with file_id < 0");
421 lseek (file_id, headersize, SEEK_SET);
422 for (int ix = 0; ix < nx; ix++)
424 read (file_id, array->array_data[ix], ny * pixel_size);
432 Array2dFile<T>::labelSeek (unsigned int label_num)
434 if (label_num > num_labels) {
435 sys_error (ERR_WARNING, "label_num %d > num_labels %d [labelSeek]");
439 if (array == NULL) { // Could not have written data if array not allocated
440 sys_error (ERR_WARNING, "array == NULL [labelSeek]");
444 off_t pos = headersize + array->sizeofArray();
445 if (lseek (file_id, pos, SEEK_SET) != pos) {
446 sys_error (ERR_WARNING, "Can't seek to end of data array");
450 for (int i = 0; i < label_num; i++)
452 pos += 22; // Skip to string length
453 if (lseek (file_id, pos, SEEK_SET) != pos) {
454 sys_error (ERR_WARNING, "Can't seek to string length");
458 read_nint16 (&strlength, file_id);
459 pos += 2 + strlength; // Skip label string length + data
460 if (lseek (file_id, pos, SEEK_SET) != pos) {
461 sys_error (ERR_WARNING, "Can't seek past label string");
471 Array2dFile<T>::labelRead (Array2dFileLabel& label, unsigned int label_num)
473 if (label_num >= num_labels) {
474 sys_error (ERR_WARNING, "Trying to read past number of labels [labelRead]");
478 if (! labelSeek (label_num)) {
479 sys_error (ERR_WARNING, "Error calling labelSeek");
483 read_nint16 (&label.label_type, file_id);
484 read_nint16 (&label.year, file_id);
485 read_nint16 (&label.month, file_id);
486 read_nint16 (&label.day, file_id);
487 read_nint16 (&label.hour, file_id);
488 read_nint16 (&label.minute, file_id);
489 read_nint16 (&label.second, file_id);
490 read_nfloat64 (&label.calc_time, file_id);
493 read_nint16 (&strlength, file_id);
494 char *str = new char [strlength+1];
495 read (file_id, str, strlength);
496 label.label_str = str;
504 Array2dFile<T>::labelAdd (const char* const lstr, double calc_time=0.)
506 labelAdd (Array2dFileLabel::L_HISTORY, lstr, calc_time);
511 Array2dFile<T>::labelAdd (int type, const char* const lstr, double calc_time=0.)
513 Array2dFileLabel label (type, lstr, calc_time);
515 labelSeek (num_labels);
517 write_nint16 (&label.label_type, file_id);
518 write_nint16 (&label.year, file_id);
519 write_nint16 (&label.month, file_id);
520 write_nint16 (&label.day, file_id);
521 write_nint16 (&label.hour, file_id);
522 write_nint16 (&label.minute, file_id);
523 write_nint16 (&label.second, file_id);
524 write_nfloat64 (&label.calc_time, file_id);
525 kuint16 strlength = label.label_str.length();
526 write_nint16 (&strlength, file_id);
527 write (file_id, static_cast<const void*>(label.label_str.c_str()), strlength);
542 Array2dFile<kfloat32> idf;
544 F32Image (const char* const fname, unsigned int nx, unsigned int ny) : idf (fname, nx, ny)
546 idf.pixel_type = Array2dFile<kfloat32>::FLOAT32;
549 F32Image (unsigned int nx, unsigned int ny) : idf (nx, ny)
551 idf.pixel_type = Array2dFile<kfloat32>::FLOAT32;
554 F32Image (const char* const fname) : idf (fname)
556 idf.pixel_type = Array2dFile<kfloat32>::FLOAT32;
563 Array2dFile<kfloat64> idf;
565 F64Image (const char* const fname, unsigned int nx, unsigned int ny) : idf (fname, nx, ny)
567 idf.pixel_type = Array2dFile<kfloat64>::FLOAT64;
570 F64Image (unsigned int nx, unsigned int ny) : idf (nx, ny)
572 idf.pixel_type = Array2dFile<kfloat64>::FLOAT64;
575 F64Image (const char* const fname) : idf (fname)
577 idf.pixel_type = Array2dFile<kfloat64>::FLOAT64;
581 typedef F64Image ImageFile;