00001
00012 #include "FitsController.h"
00013
00014 #include "FitsFile.h"
00015 #include "FitsNTuple.h"
00016
00017 #include "axes/Range.h"
00018 #include "datasrcs/DataSourceException.h"
00019 #include "datasrcs/DataSourceController.h"
00020
00021 #include "plotters/PlotterBase.h"
00022
00023 #include <fstream>
00024
00025 #ifdef SSTREAM_DEFECT
00026 #include <strstream>
00027 using std::ostrstream;
00028 #else
00029 #include <sstream>
00030 using std::ostringstream;
00031 #endif
00032
00033 #include <cassert>
00034
00035 using std::ifstream;
00036 using std::string;
00037 using std::vector;
00038
00041 typedef std::map < std::string, FitsFile * >::iterator FileMapIterator;
00042
00043 FitsController * FitsController::s_instance = 0;
00044
00045 FitsController *
00046 FitsController::
00047 instance ()
00048 {
00049 if ( s_instance == 0 ) {
00050 s_instance = new FitsController ();
00051 }
00052 return s_instance;
00053 }
00054
00055 FitsController::
00056 ~FitsController()
00057 {
00058 }
00059
00060 const std::string &
00061 FitsController::
00062 version () const
00063 {
00064 #ifdef SSTREAM_DEFECT
00065 ostrstream strm_text;
00066 #else
00067 std::ostringstream strm_text;
00068 #endif
00069 float version = 0.0;
00070 fits_get_version ( & version );
00071
00072 strm_text << version << std::ends;
00073 m_version = strm_text.str();
00074
00075 return m_version;
00076 }
00077
00078 FitsFile *
00079 FitsController::
00080 openFile ( const std::string & name )
00081 {
00082 FitsFile * file = 0;
00083 FileMapIterator first = m_file_map.find ( name );
00084
00085 if ( first == m_file_map.end() ) {
00086 ifstream test ( name.c_str (), std::ios::in );
00087 if ( test.is_open () == false ) {
00088 string what ( "FitsController: File `" );
00089 what += name;
00090 what += "' not found";
00091 throw DataSourceException ( what );
00092 }
00093 file = new FitsFile ( name );
00094 int status = file -> status();
00095
00096 if ( status != 0 ) {
00097 string what ( "cfitsio: Error opening file `" );
00098 what += name;
00099 what += "'";
00100 delete file;
00101 throw DataSourceException ( what );
00102 }
00103 m_file_map [ name ] = file;
00104 }
00105 else {
00106 file = first -> second;
00107 }
00108
00109 return file;
00110 }
00111
00112 void
00113 FitsController::
00114 closeFile ( const std::string & name )
00115 {
00116
00117
00118
00119
00120
00121
00122 }
00123
00124 const vector < string > &
00125 FitsController::
00126 getNTupleNames ( const std::string & file_name )
00127 {
00128 m_ntuple_names.clear ();
00129 FitsFile * file = openFile ( file_name );
00130 if ( file != 0 ) {
00131 file -> fillHDUNames ( m_ntuple_names );
00132 }
00133
00134 return m_ntuple_names;
00135 }
00136
00137
00138 DataSource *
00139 FitsController::
00140 createNTuple ( const std::string & filename,
00141 const std::string & name )
00142 {
00143 int index = -1;
00144
00145 const vector < string > & names
00146 = getNTupleNames ( filename );
00147 unsigned int size = names.size ();
00148
00149 for ( unsigned int i = 0; i < size; i++ ) {
00150 if ( name == names[i] ) {
00151 index = i;
00152 break;
00153 }
00154 }
00155
00156 if ( index == -1 ) {
00157 string what ( "FitsController: File\n" );
00158 what += filename + "\n";
00159 what += "does not have extensison with name ";
00160 what += name;
00161 throw DataSourceException ( what );
00162 }
00163
00164 return createNTuple ( filename, name, index );
00165 }
00166
00167 DataSource *
00168 FitsController::
00169 createNTuple ( const std::string & filename,
00170 const std::string & name,
00171 int index )
00172 {
00173 DataSource * ntuple = 0;
00174
00175 string ds_name = filename;
00176 ds_name += ": ";
00177 ds_name += name;
00178
00179 DataSourceController * controller = DataSourceController::instance();
00180 ntuple = controller -> getDataSource ( ds_name );
00181
00182 if ( ntuple == 0 ) {
00183 FitsFile * file = openFile ( filename );
00184 int retval = file -> moveToHDU ( index );
00185
00186 if ( retval == 0 ) {
00187 try {
00188 ntuple = new FitsNTuple ( file );
00189 }
00190 catch ( const DataSourceException & e ) {
00191 throw e;
00192 }
00193
00194 ntuple -> setTitle ( name );
00195 ntuple -> setName ( ds_name );
00196 controller -> registerNTuple ( ntuple );
00197 }
00198 }
00199
00200 return ntuple;
00201 }
00202
00203 using namespace hippodraw;
00204
00205 void
00206 FitsController::
00207 checkForImage ( PlotterBase * plotter, const DataSource & source,
00208 const std::vector < std::string > & binding )
00209 {
00210 const FitsFile * file = 0;
00211 try {
00212 const FitsNTuple & ntuple
00213 = dynamic_cast < const FitsNTuple & > ( source );
00214 file = ntuple.getFile ();
00215 }
00216 catch ( ... ) {
00217
00218 }
00219 if ( file == 0 ) return;
00220
00221 FitsFileBase::HduType type = file -> getHduType ();
00222 if ( type != FitsFileBase::Image ) return;
00223
00224 vector < long > sizes;
00225 file -> fillAxisSizes ( sizes );
00226 if ( sizes.size () != 2 ) {
00227 assert ( sizes[2] == 1 );
00228 }
00229 plotter -> setNumberOfBins ( Axes::X, sizes[0] );
00230 plotter -> setNumberOfBins ( Axes::Y, sizes[1] );
00231
00232 vector < double > deltas;
00233 file -> fillImageDeltas ( deltas );
00234 plotter -> setBinWidth ( Axes::X, deltas[0] );
00235 plotter -> setBinWidth ( Axes::Y, deltas[1] );
00236
00237 vector < double > ref_values;
00238 file -> fillRefPixelValues ( ref_values );
00239 vector < int > ref_indices;
00240 file -> fillRefPixelIndices ( ref_indices );
00241
00242 double x_orig = - deltas[0] * ( ref_indices[0] -1 ) + ref_values[0];
00243 double y_orig = - deltas[1] * ( ref_indices[1] -1 ) + ref_values[1];
00244 plotter ->setOffset ( Axes::X, x_orig );
00245 plotter ->setOffset ( Axes::Y, y_orig );
00246
00247 Range range;
00248 if ( deltas[0] < 0. ) {
00249 range.setLow ( x_orig + ( sizes[0] + 1 ) * deltas[0] );
00250 range.setHigh ( x_orig );
00251 }
00252 else {
00253 range.setLow ( x_orig );
00254 range.setLength ( ( sizes[0] + 1 ) * deltas[0] ) ;
00255 }
00256 plotter -> setRange ( Axes::X, range, false, true );
00257 range.setLow ( y_orig );
00258 range.setLength ( ( sizes[1] + 1 ) * deltas[1] );
00259 plotter -> setRange ( Axes::Y, range, false, true );
00260
00261 plotter -> matrixTranspose ( true );
00262
00263 bool yes = file -> isHammerAitoff ();
00264 if ( yes ) {
00265 plotter ->setAspectRatio ( 2.0 );
00266 }
00267 }