00001
00012 #include "FitsFile.h"
00013 #include "FitsNTuple.h"
00014
00015 #include "axes/Range.h"
00016 #include "datasrcs/DataSourceException.h"
00017
00018 #include <cassert>
00019
00020 using std::string;
00021 using std::vector;
00022
00023 FitsNTuple::
00024 FitsNTuple ( FitsFile * file )
00025 : DataSource ( false ),
00026 m_file ( file )
00027 {
00028 m_hdu_num = m_file -> getHDUNumber ();
00029 initColumns ();
00030 }
00031
00032 FitsNTuple::
00033 FitsNTuple ( )
00034 : DataSource ( false ),
00035 m_file ( 0 )
00036 {
00037 }
00038
00039 FitsNTuple::~FitsNTuple()
00040 {
00041 }
00042
00043 void
00044 FitsNTuple::
00045 copy ( const DataSource & )
00046 {
00047 assert ( false );
00048 }
00049
00050 const FitsFile *
00051 FitsNTuple::
00052 getFile () const
00053 {
00054 return m_file;
00055 }
00056
00057 void
00058 FitsNTuple::
00059 initColumns ( )
00060 {
00061 vector < string > labels;
00062 vector < double > values;
00063 m_file -> fillColumnNames ( labels );
00064 unsigned int size = labels.size ();
00065
00066 for ( unsigned int i = 0; i < size; i++ ) {
00067 const string & label = labels [i];
00068 addColumn ( label, values );
00069 }
00070 }
00071
00072 void
00073 FitsNTuple::
00074 notifyObservers ( ) const
00075 {
00076 Observable::notifyObservers ();
00077 }
00078
00079 unsigned int
00080 FitsNTuple::
00081 rows() const
00082 {
00083 return m_file -> getNumberOfRows ();
00084 }
00085
00086 int
00087 FitsNTuple::
00088 fillDataCache ( unsigned int column )
00089 {
00090 unsigned int size = rows ();
00091 vector < double > * cache = new vector < double > ( size );
00092 int status = m_file -> fillDoubleVectorFromColumn ( *cache, column );
00093
00094 m_data [ column ] = cache;
00095
00096 return status;
00097 }
00098
00099 void
00100 FitsNTuple::
00101 clear ()
00102 {
00103 for ( unsigned int i = 0; i < m_data.size (); i++ ) {
00104 if ( m_data[i] != 0 ) {
00105 delete m_data[i];
00106 }
00107 m_data[i] = 0;
00108 }
00109
00110 }
00111
00112 void
00113 FitsNTuple::
00114 reserve ( unsigned int count )
00115 {
00116 assert ( false );
00117 }
00118
00119 bool
00120 FitsNTuple::
00121 empty () const
00122 {
00123 return rows () == 0;
00124 }
00125
00126 void
00127 FitsNTuple::
00128 addRow ( const std::vector < double > & row )
00129 {
00130 assert ( false );
00131 }
00132
00133 double
00134 FitsNTuple::
00135 operator [] ( std::vector < unsigned int > & indices ) const
00136 {
00137 assert ( false );
00138 return 0.0;
00139 }
00140
00141 double
00142 FitsNTuple::
00143 valueAt ( unsigned int row, unsigned int column ) const
00144 {
00145 if ( m_data [ column ] == 0 ) {
00146 FitsNTuple * rtuple = const_cast < FitsNTuple * > ( this );
00147 rtuple -> fillDataCache ( column );
00148 }
00149 const vector < double > & cache = *m_data [ column ];
00150
00151 return cache[row];
00152 }
00153
00154 const std::vector < double > &
00155 FitsNTuple::
00156 getRow ( unsigned int row ) const
00157 {
00158 unsigned int size = m_data.size();
00159 m_array.resize ( size );
00160 for ( unsigned int column = 0; column < size; column++ ) {
00161 m_array [ column ] = valueAt ( row, column );
00162 }
00163
00164 return m_array;
00165 }
00166
00167 bool
00168 FitsNTuple::
00169 fillRange ( unsigned int column, Range & range ) const
00170 {
00171 bool isValid = false;
00172 if ( m_data [ column ] == 0 ) {
00173 FitsNTuple * rtuple = const_cast < FitsNTuple * > ( this );
00174 int status = rtuple -> fillDataCache ( column );
00175 if ( status == 0 ) isValid = true;
00176 }
00177 vector < double > & v = *m_data[column];
00178
00179 range.setRange ( v.begin(), v.end() );
00180
00181 return isValid;
00182 }
00183
00184 int
00185 FitsNTuple::
00186 addColumn ( const std::string & label,
00187 const std::vector < double > & column )
00188 {
00189
00190 int index = indexOf ( label );
00191 if ( index >= 0 ) {
00192 string what ( "FitsNTuple: Attempt to add a column whose label"
00193 " is same as other column." );
00194 throw DataSourceException ( what );
00195 }
00196
00197 unsigned int new_size = column.size ();
00198
00199
00200 if ( m_data.empty () == false &&
00201 column.empty() == false ) {
00202 unsigned int old_size = rows ();
00203
00204 if ( old_size != 0 && old_size != new_size ) {
00205 string what ( "FitsNTuple Attempt to add a column whose size"
00206 " is not equal to other columns." );
00207 throw DataSourceException ( what );
00208 }
00209 }
00210 if ( column.empty () ) {
00211 m_data.push_back ( 0 );
00212 }
00213 else {
00214 vector < double > * vec = new vector < double > ( column );
00215 m_data.push_back ( vec );
00216 }
00217 addLabel ( label );
00218
00219 return m_data.size() - 1;
00220 }
00221
00222 void
00223 FitsNTuple::
00224 replaceColumn ( unsigned int index, const std::vector < double > & data )
00225 {
00226 unsigned int size = columns ();
00227 if ( index >= size ) {
00228 const string what
00229 ( "FitsNTuple: Attempt to replace non-existant column." );
00230 throw DataSourceException ( what );
00231 }
00232 size = rows ();
00233 unsigned int new_size = data.size ();
00234 if ( size != 0 && size != new_size ) {
00235 const string what
00236 ( "FitsNTuple: Attempt to replace column with one whose "
00237 "size is not equal to other columns." );
00238 throw DataSourceException ( what );
00239 }
00240
00241 m_data [ index ] -> resize ( new_size );
00242 std::copy ( data.begin(), data.end (), m_data [ index ] -> begin () );
00243
00244 notifyObservers ();
00245 }
00246
00247 void
00248 FitsNTuple::
00249 replaceColumn ( const std::string & label,
00250 const std::vector < double > & data )
00251 {
00252 unsigned int index = indexOf ( label );
00253 replaceColumn ( index, data );
00254 }
00255
00256 void
00257 FitsNTuple::
00258 setShape ( std::vector < unsigned int > & shape )
00259 {
00260 assert ( false );
00261 unsigned int size = 1;
00262 for ( unsigned int i = 0; i < shape.size(); i++ ) {
00263 size *= shape[i];
00264 }
00265
00266 m_shape = shape;
00267 }
00268
00269 const vector < unsigned int > &
00270 FitsNTuple::
00271 getShape () const
00272 {
00273 assert ( false );
00274 return m_shape;
00275 }
00276
00277 unsigned int
00278 FitsNTuple::getRank () const
00279 {
00280 assert ( false );
00281
00282 return m_shape.size();
00283 }