[2820] | 1 | #include "machdefs.h"
|
---|
| 2 | #include "sopnamsp.h"
|
---|
| 3 |
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include <string.h>
|
---|
| 6 | #include <iostream>
|
---|
| 7 | #include <typeinfo>
|
---|
| 8 |
|
---|
| 9 | #include "fitsmanager.h"
|
---|
| 10 | #include "fitshandler.h"
|
---|
| 11 |
|
---|
| 12 | struct hand_list_el {
|
---|
| 13 | FitsHandlerInterface * fhi;
|
---|
[2895] | 14 | int glev;
|
---|
| 15 | string desc;
|
---|
[2820] | 16 | };
|
---|
| 17 |
|
---|
| 18 | typedef list<hand_list_el> HandlerList;
|
---|
| 19 |
|
---|
| 20 | static HandlerList * hlistp = NULL;
|
---|
| 21 |
|
---|
| 22 | static inline void ChkHLP()
|
---|
| 23 | {
|
---|
[2843] | 24 | if (hlistp == NULL) hlistp = new HandlerList;
|
---|
[2820] | 25 | }
|
---|
[2895] | 26 | /*!
|
---|
| 27 | \param fhi : handler object pointer (created by new) which will be kept in the
|
---|
| 28 | handler list
|
---|
| 29 | \param glev : global priority level associated with the handler. Used when different
|
---|
| 30 | handlers return the same value for CheckHandling() or CheckReadability().
|
---|
| 31 | \param desc : classe name description associated with the handler
|
---|
| 32 | */
|
---|
| 33 | int FitsManager::RegisterHandler(FitsHandlerInterface * fhi, int glev, string desc)
|
---|
[2820] | 34 | {
|
---|
[2864] | 35 | ChkHLP();
|
---|
[2820] | 36 | if (fhi == NULL)
|
---|
| 37 | throw NullPtrError("FitsManager::RegisterHandler() fhi=NULL ");
|
---|
| 38 | HandlerList::iterator it;
|
---|
| 39 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 40 | if (typeid(*((*it).fhi)) == typeid(*fhi))
|
---|
| 41 | throw DuplicateIdExc("FitsManager::RegisterHandler() Already registered handler");
|
---|
| 42 | }
|
---|
| 43 | hand_list_el hle;
|
---|
| 44 | hle.fhi = fhi;
|
---|
[2895] | 45 | hle.glev = glev;
|
---|
| 46 | hle.desc = desc;
|
---|
[2820] | 47 | hlistp->push_back(hle);
|
---|
| 48 | return hlistp->size();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | int FitsManager::ListHandlers()
|
---|
| 52 | {
|
---|
[2864] | 53 | ChkHLP();
|
---|
[2820] | 54 | int kk=0;
|
---|
| 55 | cout << "---- FitsManager::ListHandlers() NbHandlers= " << hlistp->size()
|
---|
| 56 | << endl;
|
---|
| 57 | HandlerList::iterator it;
|
---|
| 58 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 59 | kk++;
|
---|
[2895] | 60 | cout << kk << "- " << (*it).desc << " : "
|
---|
| 61 | << typeid(*((*it).fhi)).name() << " glev= " <<(*it).glev << endl;
|
---|
[2820] | 62 | }
|
---|
| 63 | return hlistp->size();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[2901] | 66 | /*!
|
---|
| 67 | Return a NULL pointer if no handler is registered for object \b o .
|
---|
| 68 | The returned pointer should not be used directly and should NOT
|
---|
| 69 | be deleted. For read/write operations, the Clone() method should
|
---|
| 70 | be called on the returned object.
|
---|
| 71 | */
|
---|
[2820] | 72 | FitsHandlerInterface* FitsManager::FindHandler(AnyDataObj & o)
|
---|
| 73 | {
|
---|
[2864] | 74 | ChkHLP();
|
---|
[2820] | 75 | FitsHandlerInterface * fhi = NULL;
|
---|
| 76 | HandlerList::iterator it;
|
---|
[2864] | 77 | int hfg = 0;
|
---|
| 78 | int bhfg = 0;
|
---|
[2895] | 79 | int clev = 0;
|
---|
| 80 | int blev = 0;
|
---|
[2864] | 81 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 82 | hfg = (*it).fhi->CheckHandling(o);
|
---|
[2898] | 83 | if (hfg < 1) continue;
|
---|
[2895] | 84 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
| 85 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
|
---|
[2820] | 86 | }
|
---|
[2864] | 87 | }
|
---|
[2901] | 88 | return fhi ;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /*!
|
---|
| 92 | Throws an exception ( NotFoundExc ) if the write operation can not be performed.
|
---|
| 93 | */
|
---|
| 94 | void FitsManager::Write(FitsInOutFile& os, AnyDataObj & o)
|
---|
| 95 | {
|
---|
| 96 | FitsHandlerInterface * fhi = FindHandler(o);
|
---|
[2820] | 97 | if (fhi == NULL) {
|
---|
[2901] | 98 | string msg = "FitsManager::Write()/FindHandler() Handler not found for ";
|
---|
[2820] | 99 | msg += typeid(o).name();
|
---|
| 100 | throw NotFoundExc(msg);
|
---|
| 101 | }
|
---|
[2901] | 102 | FitsHandlerInterface * fhi2 = fhi->Clone();
|
---|
[2820] | 103 | fhi2->SetDataObj(o);
|
---|
| 104 | fhi2->Write(os);
|
---|
[2901] | 105 | delete fhi2 ;
|
---|
[2820] | 106 | return;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[2901] | 109 | /*!
|
---|
| 110 | Throws an exception ( NotFoundExc ) if the read operation can not be performed.
|
---|
| 111 | */
|
---|
[2864] | 112 | void FitsManager::Read(FitsInOutFile& is, AnyDataObj & o)
|
---|
[2820] | 113 | {
|
---|
[2901] | 114 | FitsHandlerInterface * fhi = FindHandler(o);
|
---|
| 115 | if (fhi == NULL) {
|
---|
| 116 | string msg = "FitsManager::Read()/FindHandler() Handler not found for ";
|
---|
| 117 | msg += typeid(o).name();
|
---|
| 118 | throw NotFoundExc(msg);
|
---|
| 119 | }
|
---|
| 120 | FitsHandlerInterface * fhi2 = fhi->Clone();
|
---|
[2820] | 121 | fhi2->SetDataObj(o);
|
---|
[2864] | 122 | fhi2->Read(is);
|
---|
| 123 | delete fhi2;
|
---|
[2820] | 124 | return;
|
---|
| 125 | }
|
---|
[2864] | 126 |
|
---|
[2901] | 127 | /*!
|
---|
| 128 | Return a NULL pointer if no handler is found capable of reading
|
---|
| 129 | the current HDU.
|
---|
| 130 | The returned pointer should not be used directly and should NOT
|
---|
| 131 | be deleted. For read/write operations, the Clone() method should
|
---|
| 132 | be called on the returned object.
|
---|
| 133 | */
|
---|
[2864] | 134 | FitsHandlerInterface * FitsManager::FindReader(FitsInOutFile& is)
|
---|
| 135 | {
|
---|
| 136 | ChkHLP();
|
---|
| 137 | FitsHandlerInterface * fhi = NULL;
|
---|
| 138 | HandlerList::iterator it;
|
---|
| 139 | int hfg = 0;
|
---|
| 140 | int bhfg = 0;
|
---|
[2895] | 141 | int blev = 0;
|
---|
[2864] | 142 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 143 | hfg = (*it).fhi->CheckReadability(is);
|
---|
[2898] | 144 | if (hfg < 1) continue;
|
---|
[2895] | 145 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
| 146 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev;
|
---|
[2864] | 147 | }
|
---|
| 148 | }
|
---|
[2901] | 149 | return fhi;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /*!
|
---|
| 153 | Throws an exception ( NotFoundExc ) if the read operation can not be performed.
|
---|
| 154 | In case of success, the calling function is responsible for deleting
|
---|
| 155 | the returned FitsHandlerInterface object, when the correspondind data object
|
---|
| 156 | is not needed any more.
|
---|
| 157 | */
|
---|
| 158 | FitsHandlerInterface * FitsManager::Read(FitsInOutFile& is)
|
---|
| 159 | {
|
---|
| 160 | FitsHandlerInterface * fhi = FindReader(is);
|
---|
[2864] | 161 | if (fhi == NULL) {
|
---|
[2901] | 162 | string msg = "FitsManager::Read()/FindReader() Reader/Handler not found ";
|
---|
[2864] | 163 | msg += is.FileName();
|
---|
| 164 | char buff[64];
|
---|
| 165 | sprintf(buff, " HDU= %d Type= %d", (int)(is.CurrentHDU()),
|
---|
| 166 | (int)(is.CurrentHDUType()) );
|
---|
| 167 | msg += buff;
|
---|
| 168 | throw NotFoundExc(msg);
|
---|
| 169 | }
|
---|
| 170 | FitsHandlerInterface * fhi2 = FindReader(is)->Clone();
|
---|
| 171 | fhi2->Read(is);
|
---|
| 172 | return fhi2;
|
---|
| 173 | }
|
---|
[2898] | 174 |
|
---|
| 175 | /*!
|
---|
| 176 | \param filename : FITS file name to be scanned
|
---|
| 177 | \param os : infomation will be sent to formatted stream os
|
---|
| 178 | \param slev : scan level , bit 0 (1/3) print HDU keywords,
|
---|
| 179 | bit 2 (2,3) try to read HDU data using the appropraite handler
|
---|
| 180 | \param Rc : return number of scanned HDU's
|
---|
| 181 | */
|
---|
| 182 | int FitsManager::ScanFile(string filename, ostream& os, int slev)
|
---|
| 183 | {
|
---|
| 184 | FitsInOutFile is(filename, FitsInOutFile::Fits_RO);
|
---|
| 185 | os << "=== FitsManager::ScanFile( " << filename << " ) NbHDUs= "
|
---|
| 186 | << is.NbHDUs() << endl;
|
---|
| 187 | int rc = 0;
|
---|
| 188 | for(int k=0; k<is.NbHDUs(); k++) {
|
---|
| 189 | os << " ------ HDU No " << is.CurrentHDU() << " Type= "
|
---|
| 190 | << is.CurrentHDUTypeStr() << endl;
|
---|
| 191 | int hdutyp = is.CurrentHDUType();
|
---|
| 192 | if (hdutyp == IMAGE_HDU) {
|
---|
| 193 | long naxes[5] = {0,0,0,0,0};
|
---|
| 194 | int naxis=5;
|
---|
| 195 | int imgtyp = is.GetImageHDUInfo(naxis, naxes);
|
---|
| 196 | os << ">> IMAGE_HDU: naxis= " << naxis << " : ";
|
---|
| 197 | for(int i=0; i<naxis; i++) {
|
---|
| 198 | if (i>0) os << " x " ;
|
---|
| 199 | os << naxes[i];
|
---|
| 200 | }
|
---|
| 201 | os << endl;
|
---|
| 202 | }
|
---|
| 203 | else {
|
---|
| 204 | vector<string> colnames;
|
---|
| 205 | vector<int> coltypes;
|
---|
| 206 | vector<long> repcnt;
|
---|
| 207 | vector<long> width;
|
---|
| 208 | int ncols = is.GetColInfo(colnames, coltypes, repcnt, width);
|
---|
| 209 | if (hdutyp == BINARY_TBL) os << ">> BINARY_TBL : NRows= " << is.GetNbRows();
|
---|
| 210 | else os << ">> ASCII_TBL : NRows= " << is.GetNbRows();
|
---|
| 211 | os << " x NCols= " << ncols << endl;
|
---|
| 212 | for(int kk=0; kk<colnames.size(); kk++) {
|
---|
| 213 | os << "Col[" << kk+1 << "] Name= " << colnames[kk]
|
---|
| 214 | << " Type= " << FitsTypes::DataTypeToTypeString(coltypes[kk])
|
---|
| 215 | << " Repeat= " << repcnt[kk]
|
---|
| 216 | << " W= " << width[kk] << endl;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | // Fin the appropriate handler :
|
---|
| 220 | ChkHLP();
|
---|
| 221 | FitsHandlerInterface * fhi = NULL;
|
---|
| 222 | HandlerList::iterator it;
|
---|
| 223 | string hdesc;
|
---|
| 224 | int hfg = 0;
|
---|
| 225 | int bhfg = 0;
|
---|
| 226 | int blev = 0;
|
---|
| 227 | for(it = hlistp->begin(); it != hlistp->end(); it++) {
|
---|
| 228 | hfg = (*it).fhi->CheckReadability(is);
|
---|
| 229 | if (hfg < 1) continue;
|
---|
| 230 | if ( ( hfg > bhfg ) || ( (hfg == bhfg) && ((*it).glev > blev) ) ) {
|
---|
| 231 | fhi = (*it).fhi; bhfg = hfg; blev = (*it).glev; hdesc = (*it).desc;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | if (fhi == NULL)
|
---|
| 235 | os << ">>> Warning : No handler found for this HDU ... " << endl;
|
---|
| 236 | else
|
---|
| 237 | os << ">>> Reader/handler: " << hdesc << " : "
|
---|
| 238 | << typeid(*fhi).name() << " HandLevel= " << blev << ", " << bhfg << endl;
|
---|
| 239 | if (fhi && (slev >= 2)) {
|
---|
| 240 | os << ">>> Trying to read HDU data using the handler ..." << endl;
|
---|
| 241 | FitsHandlerInterface* fhic = fhi->Clone();
|
---|
| 242 | fhic->Read(is);
|
---|
| 243 | os << " FitsHandler.Read() OK " << endl;
|
---|
| 244 | }
|
---|
| 245 | if ( (slev == 1) || (slev == 3) ) {
|
---|
| 246 | os << ">>>> HDU keywords list : " << endl;
|
---|
| 247 | DVList dvl;
|
---|
| 248 | is.GetHeaderRecords(dvl);
|
---|
| 249 | os << dvl;
|
---|
| 250 | }
|
---|
| 251 | os << " --------------------- " << endl;
|
---|
| 252 | is.MoveToNextHDU();
|
---|
| 253 | rc++;
|
---|
| 254 | }
|
---|
| 255 | os << "===================================================" << endl;
|
---|
| 256 | return rc;
|
---|
| 257 | }
|
---|