CCfits  2.4
ColumnData.h
1 // Astrophysics Science Division,
2 // NASA/ Goddard Space Flight Center
3 // HEASARC
4 // http://heasarc.gsfc.nasa.gov
5 // e-mail: ccfits@legacy.gsfc.nasa.gov
6 //
7 // Original author: Ben Dorman
8 
9 #ifndef COLUMNDATA_H
10 #define COLUMNDATA_H 1
11 #include "CCfits.h"
12 
13 // vector
14 #include <vector>
15 // Column
16 #include "Column.h"
17 #ifdef _MSC_VER
18 #include "MSconfig.h"
19 #endif
20 
21 #include <complex>
22 #include <memory>
23 #include <iterator>
24 #include "FITSUtil.h"
25 using std::complex;
26 #include "FITS.h"
27 
28 
29 namespace CCfits {
30 
31 
32 
33  template <typename T>
34  class ColumnData : public Column //## Inherits: <unnamed>%385E51565EE8
35  {
36 
37  public:
38  ColumnData(const ColumnData< T > &right);
39  ColumnData (Table* p = 0);
40  ColumnData (int columnIndex, const string &columnName, ValueType type, const String &format, const String &unit, Table* p, int rpt = 1, long w = 1, const String &comment = "");
41  ~ColumnData();
42 
43  virtual ColumnData<T>* clone () const;
44  virtual void readData (long firstRow, long nelements, long firstElem = 1);
45  void setDataLimits (T* limits);
46  const T minLegalValue () const;
47  void minLegalValue (T value);
48  const T maxLegalValue () const;
49  void maxLegalValue (T value);
50  const T minDataValue () const;
51  void minDataValue (T value);
52  const T maxDataValue () const;
53  void maxDataValue (T value);
54  const std::vector<T>& data () const;
55  void setData (const std::vector<T>& value);
56  T data (int i);
57  void data (int i, T value);
58 
59  // Additional Public Declarations
60  friend class Column;
61  protected:
62  // Additional Protected Declarations
63 
64  private:
65  ColumnData< T > & operator=(const ColumnData< T > &right);
66 
67  void readColumnData (long firstRow, long nelements, T* nullValue = 0);
68  virtual bool compare (const Column &right) const;
69  virtual std::ostream& put (std::ostream& s) const;
70  void writeData (T* indata, long nRows = 1, long firstRow = 1, T* nullValue = 0);
71  void writeData (const std::vector<T>& indata, long firstRow = 1, T* nullValue = 0);
72  // Insert one or more blank rows into a FITS column.
73  virtual void insertRows (long first, long number = 1);
74  virtual void deleteRows (long first, long number = 1);
75 
76  // Additional Private Declarations
77 
78  private: //## implementation
79  // Data Members for Class Attributes
80  T m_minLegalValue;
81  T m_maxLegalValue;
82  T m_minDataValue;
83  T m_maxDataValue;
84 
85  // Data Members for Associations
86  std::vector<T> m_data;
87 
88  // Additional Implementation Declarations
89 
90  };
91 
92  // Parameterized Class CCfits::ColumnData
93 
94  template <typename T>
95  inline void ColumnData<T>::readData (long firstRow, long nelements, long firstElem)
96  {
97  readColumnData(firstRow,nelements,static_cast<T*>(0));
98  }
99 
100  template <typename T>
101  inline const T ColumnData<T>::minLegalValue () const
102  {
103  return m_minLegalValue;
104  }
105 
106  template <typename T>
107  inline void ColumnData<T>::minLegalValue (T value)
108  {
109  m_minLegalValue = value;
110  }
111 
112  template <typename T>
113  inline const T ColumnData<T>::maxLegalValue () const
114  {
115  return m_maxLegalValue;
116  }
117 
118  template <typename T>
119  inline void ColumnData<T>::maxLegalValue (T value)
120  {
121  m_maxLegalValue = value;
122  }
123 
124  template <typename T>
125  inline const T ColumnData<T>::minDataValue () const
126  {
127  return m_minDataValue;
128  }
129 
130  template <typename T>
131  inline void ColumnData<T>::minDataValue (T value)
132  {
133  m_minDataValue = value;
134  }
135 
136  template <typename T>
137  inline const T ColumnData<T>::maxDataValue () const
138  {
139  return m_maxDataValue;
140  }
141 
142  template <typename T>
143  inline void ColumnData<T>::maxDataValue (T value)
144  {
145  m_maxDataValue = value;
146  }
147 
148  template <typename T>
149  inline const std::vector<T>& ColumnData<T>::data () const
150  {
151  return m_data;
152  }
153 
154  template <typename T>
155  inline void ColumnData<T>::setData (const std::vector<T>& value)
156  {
157  m_data = value;
158  }
159 
160  template <typename T>
161  inline T ColumnData<T>::data (int i)
162  {
163  // return data stored in the ith row, which is in the i-1 th location in the array.
164  return m_data[i - 1];
165  }
166 
167  template <typename T>
168  inline void ColumnData<T>::data (int i, T value)
169  {
170  // assign data to i-1 th location in the array, representing the ith row.
171  m_data[i - 1] = value;
172  }
173 
174  // Parameterized Class CCfits::ColumnData
175 
176  template <typename T>
177  ColumnData<T>::ColumnData(const ColumnData<T> &right)
178  :Column(right),
179  m_minLegalValue(right.m_minLegalValue),
180  m_maxLegalValue(right.m_maxLegalValue),
181  m_minDataValue(right.m_minDataValue),
182  m_maxDataValue(right.m_maxDataValue),
183  m_data(right.m_data)
184  {
185  }
186 
187  template <typename T>
188  ColumnData<T>::ColumnData (Table* p)
189  : Column(p),
190  m_minLegalValue(),
191  m_maxLegalValue(),
192  m_minDataValue(),
193  m_maxDataValue(),
194  m_data()
195  {
196  }
197 
198  template <typename T>
199  ColumnData<T>::ColumnData (int columnIndex, const string &columnName, ValueType type, const String &format, const String &unit, Table* p, int rpt, long w, const String &comment)
200  : Column(columnIndex,columnName,type,format,unit,p,rpt,w,comment),
201  m_minLegalValue(),
202  m_maxLegalValue(),
203  m_minDataValue(),
204  m_maxDataValue(),
205  m_data()
206  {
207  }
208 
209 
210  template <typename T>
211  ColumnData<T>::~ColumnData()
212  {
213  }
214 
215 
216  template <typename T>
217  void ColumnData<T>::readColumnData (long firstRow, long nelements, T* nullValue)
218  {
219  if ( rows() < nelements )
220  {
221  std::cerr << "CCfits: More data requested than contained in table. ";
222  std::cerr << "Extracting complete column.\n";
223  nelements = rows();
224  }
225 
226  int status(0);
227  int anynul(0);
228 
229  FITSUtil::auto_array_ptr<T> array(new T[nelements]);
230 
231  makeHDUCurrent();
232 
233  if ( fits_read_col(fitsPointer(),type(), index(), firstRow, 1,
234  nelements, nullValue, array.get(), &anynul, &status) ) throw FitsError(status);
235 
236 
237  if (m_data.size() != static_cast<size_t>( rows() ) ) m_data.resize(rows());
238 
239  std::copy(&array[0],&array[nelements],m_data.begin()+firstRow-1);
240  if (nelements == rows()) isRead(true);
241  }
242 
243  template <typename T>
244  bool ColumnData<T>::compare (const Column &right) const
245  {
246  if ( !Column::compare(right) ) return false;
247  const ColumnData<T>& that = static_cast<const ColumnData<T>&>(right);
248  unsigned int n = m_data.size();
249  if ( that.m_data.size() != n ) return false;
250  for (unsigned int i = 0; i < n ; i++)
251  {
252  if (m_data[i] != that.m_data[i]) return false;
253  }
254  return true;
255  }
256 
257  template <typename T>
258  ColumnData<T>* ColumnData<T>::clone () const
259  {
260  return new ColumnData<T>(*this);
261  }
262 
263  template <typename T>
264  std::ostream& ColumnData<T>::put (std::ostream& s) const
265  {
266  Column::put(s);
267  if (FITS::verboseMode() && type() != Tstring)
268  {
269  s << " Column Legal limits: ( " << m_minLegalValue << "," << m_maxLegalValue << " )\n"
270  << " Column Data limits: ( " << m_minDataValue << "," << m_maxDataValue << " )\n";
271  }
272  if (!m_data.empty())
273  {
274  std::ostream_iterator<T> output(s,"\n");
275  // output each row on a separate line.
276  // user can supply manipulators to stream for formatting.
277  std::copy(m_data.begin(),m_data.end(),output);
278  }
279 
280  return s;
281  }
282 
283  template <typename T>
284  void ColumnData<T>::writeData (T* indata, long nRows, long firstRow, T* nullValue)
285  {
286 
287  // set columnData's data member to equal what's written to file.
288  // indata has size nRows: elements firstRow to firstRow + nRows - 1 will be written.
289  // if this exceeds the current rowlength of the HDU, update the return value for
290  // rows() in the parent after the fitsio call.
291  int status(0);
292  long elementsToWrite(nRows + firstRow -1);
293  // get a copy for restorative action.
294  std::vector<T> __tmp(m_data);
295 
296 
297  if (elementsToWrite != static_cast<long>(m_data.size()))
298  {
299 
300  m_data.resize(elementsToWrite,T());
301  }
302 
303  std::copy(&indata[0],&indata[nRows],m_data.begin()+firstRow-1);
304 
305  // if successful, write to disk.
306 
307  try
308  {
309  if (nullValue)
310  {
311  if (fits_write_colnull(fitsPointer(), type(), index(), firstRow, 1, nRows,
312  indata, nullValue, &status) != 0) throw FitsError(status);
313  }
314  else
315  {
316  if (fits_write_col(fitsPointer(), type(), index(), firstRow, 1, nRows,
317  indata, &status) != 0) throw FitsError(status);
318  }
319 
320  // tell the Table that the number of rows has changed
321  parent()->updateRows();
322  }
323  catch (FitsError) // the only thing that can throw here.
324  {
325  // reset to original content and rethrow the exception.
326  m_data = __tmp;
327  if (status == NO_NULL) throw NoNullValue(name());
328  else throw;
329  }
330  }
331 
332  template <typename T>
333  void ColumnData<T>::writeData (const std::vector<T>& indata, long firstRow, T* nullValue)
334  {
335  FITSUtil::CVarray<T> convert;
336  FITSUtil::auto_array_ptr<T> pcolData (convert(indata));
337  T* columnData = pcolData.get();
338  writeData(columnData,indata.size(),firstRow,nullValue);
339  }
340 
341  template <typename T>
342  void ColumnData<T>::insertRows (long first, long number)
343  {
344  FITSUtil::FitsNullValue<T> blank;
345  typename std::vector<T>::iterator in;
346  if (first !=0)
347  {
348  in = m_data.begin()+first;
349  }
350  else
351  {
352  in = m_data.begin();
353  }
354 
355  // non-throwing operations.
356  m_data.insert(in,number,blank());
357  }
358 
359  template <typename T>
360  void ColumnData<T>::deleteRows (long first, long number)
361  {
362  m_data.erase(m_data.begin()+first-1,m_data.begin()+first-1+number);
363  }
364 
365  template <typename T>
366  void ColumnData<T>::setDataLimits (T* limits)
367  {
368  m_minLegalValue = limits[0];
369  m_maxLegalValue = limits[1];
370  m_minDataValue = std::max(limits[2],limits[0]);
371  m_maxDataValue = std::min(limits[3],limits[1]);
372  }
373 
374  // Additional Declarations
375 
376  // all functions that operate on strings or complex data that call cfitsio
377  // need to be specialized.
378 
379 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
380 template <>
381 inline void ColumnData<complex<float> >::setDataLimits (complex<float>* limits)
382  {
383  m_minLegalValue = limits[0];
384  m_maxLegalValue = limits[1];
385  m_minDataValue = limits[2];
386  m_maxDataValue = limits[3];
387  }
388 #else
389 template <>
390  void ColumnData<complex<float> >::setDataLimits (complex<float>* limits);
391 #endif
392 
393 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
394 template <>
395 inline void ColumnData<complex<double> >::setDataLimits (complex<double>* limits)
396  {
397  m_minLegalValue = limits[0];
398  m_maxLegalValue = limits[1];
399  m_minDataValue = limits[2];
400  m_maxDataValue = limits[3];
401  }
402 #else
403  template <>
404  void ColumnData<complex<double> >::setDataLimits (complex<double>* limits);
405 #endif
406 
407 
408 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
409  template <>
410  inline void ColumnData<string>::readColumnData (long firstRow,
411  long nelements,
412  string* nullValue)
413  {
414  int status = 0;
415 
416  int anynul = 0;
417  char** array = new char*[nelements];
418 
419  int j(0);
420  for ( ; j < nelements; ++j)
421  {
422  array[j] = new char[width() + 1];
423  }
424 
425  char* nulval = 0;
426  if (nullValue)
427  {
428  nulval = const_cast<char*>(nullValue->c_str());
429  }
430  else
431  {
432  nulval = new char;
433  *nulval = '\0';
434  }
435 
436 
437  try
438  {
439  makeHDUCurrent();
440  if (fits_read_col_str(fitsPointer(),index(), firstRow,1,nelements,
441  nulval,array, &anynul,&status) ) throw FitsError(status);
442  }
443  catch (FitsError)
444  {
445  // ugly. but better than leaking resources.
446  for (int jj = 0; jj < nelements; ++jj)
447  {
448  delete [] array[jj];
449  }
450 
451  delete [] array;
452  delete nulval;
453  throw;
454  }
455 
456 
457  if (m_data.size() != rows()) setData(std::vector<string>(rows(),string(nulval)));
458 
459  // the 'first -1 ' converts to zero based indexing.
460 
461  for ( j = 0; j < nelements; j++)
462  {
463  m_data[j - 1 + firstRow] = string(array[j]);
464  }
465 
466  for ( j = 0; j < nelements; j++)
467  {
468  delete [] array[j];
469  }
470 
471  delete [] array;
472  delete nulval;
473  if (nelements == rows()) isRead(true);
474 
475  }
476 #else
477  template <>
478 void ColumnData<string>::readColumnData (long firstRow, long nelements, string* nullValue);
479 #endif
480 
481 
482 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
483  template <>
484  inline void ColumnData<complex<float> >::readColumnData (long firstRow,
485  long nelements,
486  complex<float>* nullValue)
487  {
488  // specialization for ColumnData<string>
489  int status(0);
490  int anynul(0);
491  FITSUtil::auto_array_ptr<float> pArray(new float[nelements*2]);
492  float* array = pArray.get();
493  float nulval(0);
494  makeHDUCurrent();
495 
496 
497  if (fits_read_col_cmp(fitsPointer(),index(), firstRow,1,nelements,
498  nulval,array, &anynul,&status) ) throw FitsError(status);
499 
500 
501  if (m_data.size() != rows()) m_data.resize(rows());
502 
503  // the 'j -1 ' converts to zero based indexing.
504 
505  for (int j = 0; j < nelements; ++j)
506  {
507 
508  m_data[j - 1 + firstRow] = std::complex<float>(array[2*j],array[2*j+1]);
509  }
510  if (nelements == rows()) isRead(true);
511 
512  }
513 #else
514 template <>
515 void ColumnData<complex<float> >::readColumnData (long firstRow, long nelements,complex<float>* nullValue );
516 #endif
517 
518 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
519  template <>
520  inline void ColumnData<complex<double> >::readColumnData (long firstRow,
521  long nelements,
522  complex<double>* nullValue)
523  {
524  // specialization for ColumnData<complex<double> >
525  int status(0);
526  int anynul(0);
527  FITSUtil::auto_array_ptr<double> pArray(new double[nelements*2]);
528  double* array = pArray.get();
529  double nulval(0);
530  makeHDUCurrent();
531 
532 
533  if (fits_read_col_dblcmp(fitsPointer(), index(), firstRow,1,nelements,
534  nulval,array, &anynul,&status) ) throw FitsError(status);
535 
536 
537 
538 
539  if (m_data.size() != rows()) setData(std::vector<complex<double> >(rows(),nulval));
540 
541  // the 'j -1 ' converts to zero based indexing.
542 
543  for (int j = 0; j < nelements; j++)
544  {
545 
546  m_data[j - 1 + firstRow] = std::complex<double>(array[2*j],array[2*j+1]);
547  }
548  if (nelements == rows()) isRead(true);
549 
550  }
551 #else
552 template <>
553 void ColumnData<complex<double> >::readColumnData (long firstRow, long nelements,complex<double>* nullValue);
554 #endif
555 
556 #if SPEC_TEMPLATE_DECL_DEFECT
557  template <>
558  inline void ColumnData<string>::writeData (const std::vector<string>& indata,
559  long firstRow, string* nullValue)
560  {
561  int status=0;
562  char** columnData=FITSUtil::CharArray(indata);
563 
564  if ( fits_write_colnull(fitsPointer(), TSTRING, index(), firstRow, 1, indata.size(),
565  columnData, 0, &status) != 0 )
566  throw FitsError(status);
567  unsigned long elementsToWrite (indata.size() + firstRow - 1);
568  std::vector<string> __tmp(m_data);
569  if (m_data.size() < elementsToWrite)
570  {
571  m_data.resize(elementsToWrite,"");
572  std::copy(__tmp.begin(),__tmp.end(),m_data.begin());
573  }
574  std::copy(indata.begin(),indata.end(),m_data.begin()+firstRow-1);
575 
576 
577  for (size_t i = 0; i < indata.size(); ++i)
578  {
579  delete [] columnData[i];
580  }
581  delete [] columnData;
582  }
583 #else
584 template <>
585 void ColumnData<string>::writeData (const std::vector<string>& inData, long firstRow, string* nullValue);
586 #endif
587 
588 #ifdef SPEC_TEMPLATE_DECL_DEFECT
589  template <>
590  inline void ColumnData<complex<float> >::writeData (const std::vector<complex<float> >& inData,
591  long firstRow,
592  complex<float>* nullValue)
593  {
594  int status(0);
595  int nRows (inData.size());
596  FITSUtil::auto_array_ptr<float> pData(new float[nRows*2]);
597  float* Data = pData.get();
598  std::vector<complex<float> > __tmp(m_data);
599  for (int j = 0; j < nRows; ++j)
600  {
601  Data[ 2*j] = inData[j].real();
602  Data[ 2*j + 1] = inData[j].imag();
603  }
604 
605  try
606  {
607 
608  if (fits_write_col_cmp(fitsPointer(), index(), firstRow, 1,
609  nRows,Data, &status) != 0) throw FitsError(status);
610  long elementsToWrite(nRows + firstRow -1);
611  if (elementsToWrite > static_cast<long>(m_data.size()))
612  {
613 
614  m_data.resize(elementsToWrite);
615  }
616 
617  std::copy(inData.begin(),inData.end(),m_data.begin()+firstRow-1);
618 
619  // tell the Table that the number of rows has changed
620  parent()->updateRows();
621  }
622  catch (FitsError) // the only thing that can throw here.
623  {
624  // reset to original content and rethrow the exception.
625  m_data.resize(__tmp.size());
626  m_data = __tmp;
627  }
628 
629  }
630 
631 #else
632 template <>
633 void ColumnData<complex<float> >::writeData (const std::vector<complex<float> >& inData, long firstRow,
634  complex<float>* nullValue);
635 #endif
636 
637 #ifdef SPEC_TEMPLATE_DECL_DEFECT
638  template <>
639  inline void ColumnData<complex<double> >::writeData (const std::vector<complex<double> >& inData,
640  long firstRow,
641  complex<double>* nullValue)
642  {
643  int status(0);
644  int nRows (inData.size());
645  FITSUtil::auto_array_ptr<double> pData(new double[nRows*2]);
646  double* Data = pData.get();
647  std::vector<complex<double> > __tmp(m_data);
648  for (int j = 0; j < nRows; ++j)
649  {
650  pData[ 2*j] = inData[j].real();
651  pData[ 2*j + 1] = inData[j].imag();
652  }
653 
654  try
655  {
656 
657  if (fits_write_col_dblcmp(fitsPointer(), index(), firstRow, 1,
658  nRows,Data, &status) != 0) throw FitsError(status);
659  long elementsToWrite(nRows + firstRow -1);
660  if (elementsToWrite > static_cast<long>(m_data.size()))
661  {
662 
663  m_data.resize(elementsToWrite);
664  }
665 
666  std::copy(inData.begin(),inData.end(),m_data.begin()+firstRow-1);
667 
668  // tell the Table that the number of rows has changed
669  parent()->updateRows();
670  }
671  catch (FitsError) // the only thing that can throw here.
672  {
673  // reset to original content and rethrow the exception.
674  m_data.resize(__tmp.size());
675  m_data = __tmp;
676  }
677 
678  }
679 
680 #else
681 template <>
682 void ColumnData<complex<double> >::writeData (const std::vector<complex<double> >& inData, long firstRow,
683  complex<double>* nullValue);
684 
685 #endif
686 } // namespace CCfits
687 
688 
689 #endif
virtual std::ostream & put(std::ostream &s) const
internal implementation of << operator.
Definition: Column.cxx:302
static bool verboseMode()
return verbose setting for library
Definition: FITS.h:891
Namespace enclosing all CCfits classes and globals definitions.
Definition: AsciiTable.cxx:26
ValueType
CCfits value types and their CFITSIO equivalents (in caps)
Definition: CCfits.h:79
Column(const Column &right)
copy constructor, used in copying Columns to standard library containers.
Definition: Column.cxx:171