FitsNTuple.cxx
Go to the documentation of this file.
1 
12 #include "FitsNTuple.h"
13 
14 #include "FitsFile.h"
15 
16 #include "axes/Range.h"
17 #include "datasrcs/DataColumn.h"
18 #include "pattern/string_convert.h"
19 
20 #include <algorithm>
21 #include <stdexcept>
22 
23 #include <cassert>
24 
25 using std::for_each;
26 using std::mem_fun;
27 using std::runtime_error;
28 using std::string;
29 using std::vector;
30 
31 using namespace hippodraw;
32 
34 FitsNTuple ( FitsFile * file )
35  : DataSource ( false ),
36  m_file ( file )
37 {
38  m_hdu_num = m_file -> getHDUNumber ();
39  initColumns ();
40 }
41 
44  : DataSource ( false ),
45  m_file ( 0 )
46 {
47 }
48 
50 {
51 }
52 
53 void
55 copy ( const DataSource & )
56 {
57  assert ( false );
58 }
59 
60 const FitsFile *
62 getFile () const
63 {
64  return m_file;
65 }
66 
67 void
70 {
71  m_dups.clear();
72 
73  vector < string > labels;
74  vector < double > values;
75  m_file -> fillColumnNames ( labels );
76  unsigned int size = labels.size ();
77  for ( unsigned int i = 0; i < size; i++ ) {
78  const string & label = labels [i];
79  int index = indexOf ( label );
80  if ( index < 0 ) {
81  addColumn ( label, values );
82  }
83  else {
84  m_dups.push_back ( label );
85  }
86  }
87 }
88 
89 void
91 notifyObservers ( ) const
92 {
94 }
95 
96 unsigned int
98 rows() const
99 {
100  return m_file -> getNumberOfRows ();
101 }
102 std::size_t
104 size ( unsigned int column ) const
105 {
106  const DataColumn * dc = m_columns [ column ];
107 
108  return dc -> size ();
109 }
110 
111 int
113 fillDataCache ( unsigned int column )
114 {
115  DataColumn * data_column = m_columns [ column ];
116 
117  vector < intptr_t > shape;
118  m_file -> fillShape ( shape, column );
119  data_column -> setShape ( shape );
120 
121  std::size_t size = data_column -> size ();
122  vector < double > & data = data_column -> getData ();
123  data.resize ( size );
124  int status = m_file -> fillDoubleVectorFromColumn ( data, column );
125 
126  return status;
127 }
128 
129 void
132 {
133  for_each ( m_columns.begin(), m_columns.end (),
134  std::mem_fun ( &DataColumn::clear ) );
135 }
136 
137 void
139 reserve ( unsigned int )
140 {
141  assert ( false );
142 }
143 
144 bool
146 empty () const
147 {
148  return rows () == 0;
149 }
150 
151 double
153 operator [] ( std::vector < unsigned int > & ) const
154 {
155  assert ( false );
156  return 0.0;
157 }
158 
159 double
161 valueAt ( unsigned int row, unsigned int column ) const
162 {
163  const vector < double > & data = m_columns[ column ] -> getData();
164  if ( data.empty () ) {
165  FitsNTuple * tuple = const_cast < FitsNTuple * > ( this );
166  tuple -> fillDataCache ( column );
167  }
168 
169  return data [ row ];
170 }
171 
172 const vector < double > &
174 getColumn ( unsigned int index ) const
175 {
176  isValidColumn ( index ); // might throw
177 
178  DataColumn * dc = m_columns [ index ];
179  const vector < double > & data = dc -> getData ();
180  if ( data.empty () ) {
181  FitsNTuple * tuple = const_cast < FitsNTuple * > ( this );
182  tuple -> fillDataCache ( index );
183  }
184 
185  return data;
186 }
187 
188 const std::vector < double > &
190 getRow ( unsigned int row ) const
191 {
192  unsigned int size = m_columns.size();
193  m_array.resize ( size );
194  for ( unsigned int column = 0; column < size; column++ ) {
195  m_array [ column ] = valueAt ( row, column );
196  }
197 
198  return m_array;
199 }
200 
201 bool
203 fillRange ( unsigned int column, Range & range ) const
204 {
205  bool isValid = true;;
206  const vector < double > & data = m_columns [ column ] -> getData ();
207 
208  if ( data.empty () ) {
209  FitsNTuple * rtuple = const_cast < FitsNTuple * > ( this );
210  int status = rtuple -> fillDataCache ( column );
211  if ( status != 0 ) isValid = false;
212  }
213 
214  range.setRange ( data.begin(), data.end() );
215 
216  return isValid;
217 }
218 
219 void
221 checkLabel ( const std::string & label )
222 {
223  if ( isValidLabel ( label ) == true ) { // alrady exists
224  string what ( "FitsNTuple: The column label `" );
225  what += label;
226  what += "' already exists in this DataSource.";
227  throw runtime_error ( what );
228  }
229 }
230 
231 int
233 addColumn ( const std::string & label,
234  const std::vector < double > & column )
235 {
236  unsigned int size = column.size ();
237  const vector < intptr_t > shape ( 1, size );
238 
239  return addColumn ( label, column, shape );
240 }
241 
244 int
246 addColumn ( const std::string & label,
247  const std::vector < double > & column,
248  const std::vector < intptr_t > & shape )
249 {
250  checkLabel ( label );
251 
252  // Check if column has right size.
253  if ( m_columns.empty () == false &&
254  column.empty() == false ) {
255  unsigned int old_size = rows ();
256  unsigned int new_size = shape[0];
257 
258  if ( old_size != 0 && old_size != new_size ) {
259  string what ( "FitsNTuple Attempt to add a column whose size"
260  " is not equal to other columns." );
261  throw runtime_error ( what );
262  }
263  }
264 
265  DataColumn * dc = new DataColumn();
266  m_columns.push_back ( dc );
267  if ( column.empty () == false ) {
268  vector < double > & data = m_columns.back() -> getData(); // last one
269  data = column; // copy
270  }
271  addLabel ( label );
272  dc -> setShape ( shape );
273 
274  return m_columns.size () - 1;
275 }
276 
277 void
279 replaceColumn ( unsigned int index, const std::vector < double > & data )
280 {
281  int size = data.size();
282  const vector < intptr_t > shape ( 1, size );
283 
284  replaceColumn ( index, data, shape );
285 }
286 
287 void
289 replaceColumn ( unsigned int index,
290  const std::vector < double > & data,
291  const std::vector < intptr_t > & shape )
292 {
293  unsigned int size = columns ();
294  if ( index >= size ) {
295  string what ( "FitsNTuple: Attempt to replace column " );
296  what += String::convert ( index );
297  what += " with only ";
298  what += String ::convert ( size );
299  what += " columns in data source.";
300  throw runtime_error ( what );
301  }
302  size = rows ();
303  unsigned int new_size = data.size ();
304  if ( size != 0 && size != new_size ) {
305  const string what
306  ( "FitsNTuple: Attempt to replace column with one whose "
307  "size is not equal to other columns." );
308  throw runtime_error ( what );
309  }
310  DataColumn * dc = m_columns [ index ];
311  vector < double > & pvec = dc -> getData ();
312  pvec.resize ( new_size );
313  std::copy ( data.begin(), data.end (), pvec.begin () );
314  dc -> setShape ( shape );
315 
316  notifyObservers ();
317 }
318 
319 void
321 replaceColumn ( const std::string & label,
322  const std::vector < double > & data,
323  const std::vector < intptr_t > & shape )
324 {
325  unsigned int index = indexOf ( label );
326  replaceColumn ( index, data, shape );
327 }
328 
329 void
331 setShape ( std::vector < unsigned int > & shape )
332 {
333  assert ( false ); // not tested
334  m_shape = shape;
335 }
336 
337 const vector < unsigned int > &
339 getShape () const
340 {
341  assert ( false ); // not tested
342  return m_shape;
343 }
344 
345 
348 void
350 fillShape ( std::vector < intptr_t > & shape, unsigned int column ) const
351 {
352  DataColumn * dc = m_columns [ column ];
353  shape = dc -> getShape ();
354 }
355 
356 double *
358 doubleArrayAt ( unsigned int row, unsigned int column ) const
359 {
361 
362  return data -> doubleArrayAt ( row );
363 }
virtual double operator[](std::vector< unsigned int > &indices) const
Raises assertion as this method is not yet implemented.
Definition: FitsNTuple.cxx:153
unsigned int i
int addColumn(const std::string &label, const std::vector< double > &column)
Adds a column to the end of the FitsNTuple.
Definition: FitsNTuple.cxx:233
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: Observable.cxx:93
FitsFile * m_file
The FitsFile used for this DataSource.
Definition: FitsNTuple.h:42
virtual ~FitsNTuple()
The destructor.
Definition: FitsNTuple.cxx:49
int m_hdu_num
The HDU number used for this object.
Definition: FitsNTuple.h:47
std::vector< std::string > m_dups
A list of labels that duplicated others in a DataSource file.
Definition: DataSource.h:100
virtual double * doubleArrayAt(unsigned int row, unsigned int column) const
Returns data array as double for row and column.
Definition: FitsNTuple.cxx:358
std::size_t size(unsigned int c) const
Returns the the column.
Definition: FitsNTuple.cxx:104
hippodraw::FitsNTuple class interface.
int fillDataCache(unsigned int column)
Fills the data cache for column column.
Definition: FitsNTuple.cxx:113
virtual bool fillRange(unsigned int column, Range &) const
Fills the Range object from data indexed by column.
Definition: FitsNTuple.cxx:203
void initColumns()
Initializes the columns of the DataSource.
Definition: FitsNTuple.cxx:69
void * data(numeric::array arr)
Definition: num_util.cpp:389
void replaceColumn(unsigned int index, const std::vector< double > &data)
Replaces the data in column index.
Definition: FitsNTuple.cxx:279
virtual void addLabel(const std::string &label)
Adds a new label for a column.
Definition: DataSource.cxx:154
column
The column indices for 2 dimension data point tuple.
virtual void setShape(std::vector< unsigned int > &shape)
Sets the shape of the data elements.
Definition: FitsNTuple.cxx:331
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317
bool isValidLabel(const std::string &label) const
Returns true if label is a valid label for a column in the DataSource.
Definition: DataSource.cxx:257
virtual void fillShape(std::vector< intptr_t > &shape, unsigned int index) const
Returns the shape of the column index.
Definition: FitsNTuple.cxx:350
const std::vector< unsigned int > & getShape() const
Returns the shape of the data elements.
Definition: FitsNTuple.cxx:339
The namespace for conversion to string.
virtual void clear()
Clears the data cache; doesn&#39;t not modify the contained fits file.
Definition: FitsNTuple.cxx:131
hippodraw::DataColumn class interface.
std::vector< unsigned int > m_shape
The shape of the data.
Definition: DataSource.h:96
virtual void reserve(unsigned int count)
Raises assertion as the contained FITS file should not be changed.
Definition: FitsNTuple.cxx:139
virtual const std::vector< double > & getRow(unsigned int) const
Returns a const reference to slice along the axis known as a row.
Definition: FitsNTuple.cxx:190
virtual const std::vector< double > & getColumn(unsigned int c) const
Returns the data of the column.
Definition: FitsNTuple.cxx:174
virtual unsigned int rows() const
Returns the number of rows of FITS ASCII or binary table.
Definition: FitsNTuple.cxx:98
Wrapper class to CFITSIO.
Definition: FitsFile.h:39
virtual void copy(const DataSource &)
Raises exception because with this release, copying the fits table is not supported.
Definition: FitsNTuple.cxx:55
void checkLabel(const std::string &label)
If label already exists for a column, throw DataSourceException, otherwise do nothing.
Definition: FitsNTuple.cxx:221
bool isValidColumn(unsigned int index) const
Returns true if index is valid, otherwise throws a DataSourceException.
Definition: DataSource.cxx:212
virtual double valueAt(unsigned int row, unsigned int column) const
Returns the value in the table in position given by the row and column indexes.
Definition: FitsNTuple.cxx:161
string convert(int i)
Converts an integer to a string.
void setRange(double low, double high, double pos)
Changes the current Range.
Definition: Range.cxx:126
hippodraw::Range class interface
virtual int indexOf(const std::string &label) const
Returns true if the specified column labeled label has been filled.
Definition: DataSource.cxx:193
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
A DataSource class implemented with a vector &lt; DataColumn * &gt; objects.
Definition: FitsNTuple.h:31
FitsNTuple()
The default constructor.
Definition: FitsNTuple.cxx:43
A class to hold data and its attributes.
Definition: DataColumn.h:31
hippodraw::FitsFile interface
Expresses a range of values.
Definition: Range.h:33
return index
Definition: PickTable.cxx:182
virtual bool empty() const
Returns true, if FitsNTuple is empty, i.e.
Definition: FitsNTuple.cxx:146
void clear()
Clears the data array.
Definition: DataColumn.cxx:80
const FitsFile * getFile() const
Returns the FITS file used to create this object.
Definition: FitsNTuple.cxx:62
virtual void notifyObservers() const
Notifies observers.
Definition: FitsNTuple.cxx:91
std::vector< DataColumn * > m_columns
The DataColumn objects that contains the data and its attributes.
Definition: FitsNTuple.h:38
std::vector< double > m_array
A temporary array that can be returned by const reference.
Definition: DataSource.h:88
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen