| 1 | #include "pexceptions.h"
|
|---|
| 2 | #include "fitsautoreader.h"
|
|---|
| 3 | #include "dvlist.h"
|
|---|
| 4 |
|
|---|
| 5 | ///////////////////////////////////////////////////////////////////////
|
|---|
| 6 | // ---- gestion de persistance I/O format fits--
|
|---|
| 7 | // objets
|
|---|
| 8 | ////////////////////////////////////////////////////////////////////
|
|---|
| 9 |
|
|---|
| 10 | /*!
|
|---|
| 11 | \class SOPHYA::FITS_AutoReader
|
|---|
| 12 | \ingroup FitsIOServer
|
|---|
| 13 | FITS reader with automatic mapping on SOPHYA objects.
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | FITS_AutoReader::FITS_AutoReader() {InitNull();};
|
|---|
| 17 | FITS_AutoReader::FITS_AutoReader(const char inputfile[])
|
|---|
| 18 | {
|
|---|
| 19 | InitNull();
|
|---|
| 20 | inFits_ = new FitsInFile (inputfile);
|
|---|
| 21 | filename_ = string(inputfile);
|
|---|
| 22 | }
|
|---|
| 23 | FITS_AutoReader::FITS_AutoReader(string const & inputfile)
|
|---|
| 24 | {
|
|---|
| 25 | InitNull();
|
|---|
| 26 | inFits_ = new FitsInFile (inputfile);
|
|---|
| 27 | filename_ = inputfile;
|
|---|
| 28 | }
|
|---|
| 29 | FITS_AutoReader::~FITS_AutoReader()
|
|---|
| 30 | {
|
|---|
| 31 | if (inFits_ != NULL) delete inFits_;
|
|---|
| 32 | if (dobj_ != NULL) delete dobj_;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | int FITS_AutoReader::NbBlocks()
|
|---|
| 36 | {
|
|---|
| 37 | return inFits_->NbBlocks();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | // parametre toutCharger : uniquement pour les BINTABLE et s'il ne
|
|---|
| 41 | // s'agit pas explicitement d'un objet SOPHYA, on charge toute la
|
|---|
| 42 | // table en memoire sous forme d'un xntuple si toutCharger= true.
|
|---|
| 43 | // et si le nombre d'entrees est superieur a 1000.
|
|---|
| 44 | // sinon (c-a-d : il ne s'agit pas d'un objet connu de SOPHYA
|
|---|
| 45 | // c'est une BINTABLE avec plus de 1000 entrees
|
|---|
| 46 | // totuCharger = false)
|
|---|
| 47 | // on ouvre une FitsBTNtuIntf (lecture ulterieure, bufferisee, des valeurs)
|
|---|
| 48 | AnyDataObj* FITS_AutoReader::ReadObject(int hdunum, bool toutCharger) const
|
|---|
| 49 | {
|
|---|
| 50 | if (hdunum<=0)
|
|---|
| 51 | {
|
|---|
| 52 | throw PException(" FITS_AutoReader::ReadObject : hdu number must be positive");
|
|---|
| 53 | }
|
|---|
| 54 | inFits_->ReadHeader(hdunum);
|
|---|
| 55 | if (inFits_->IsFitsEOF()) return NULL;
|
|---|
| 56 | if (inFits_->IsFitsERROR())
|
|---|
| 57 | {
|
|---|
| 58 | throw IOExc("FITS_AutoReader::ReadObject: FITSIO error in reading");
|
|---|
| 59 | }
|
|---|
| 60 | DVList dvl=inFits_->DVListFromFits();
|
|---|
| 61 | string nameObj = dvl.GetS("CONTENT");
|
|---|
| 62 | // cout << " SOPHYA object identified as: " << dvl.GetS("CONTENT") << endl;
|
|---|
| 63 | if (nameObj == string("TArray") )
|
|---|
| 64 | {
|
|---|
| 65 | return newTArray();
|
|---|
| 66 | }
|
|---|
| 67 | else if (nameObj == string("SphereHEALPix") )
|
|---|
| 68 | {
|
|---|
| 69 | return newSphereHEALPix();
|
|---|
| 70 | }
|
|---|
| 71 | else if (nameObj == string("LocalMap") )
|
|---|
| 72 | {
|
|---|
| 73 | return newLocalMap();
|
|---|
| 74 | }
|
|---|
| 75 | else if (nameObj == string("NTuple") )
|
|---|
| 76 | {
|
|---|
| 77 | return newNTuple();
|
|---|
| 78 | }
|
|---|
| 79 | else if (nameObj == string("XNTuple") )
|
|---|
| 80 | {
|
|---|
| 81 | return newXNTuple();
|
|---|
| 82 | }
|
|---|
| 83 | // on n'a trouve aucun nom d'objet connu de SOPHYA, on fait une
|
|---|
| 84 | // recherche qualitative.
|
|---|
| 85 | //
|
|---|
| 86 | // si c'est une image fits, on cree un TArray
|
|---|
| 87 | else if (inFits_->IsFitsImage())
|
|---|
| 88 | {
|
|---|
| 89 | return newTArray();
|
|---|
| 90 | }
|
|---|
| 91 | // si c'est une bintable on cherche le mot cle ORDERING pour identifier
|
|---|
| 92 | // une spherehealpix
|
|---|
| 93 | else if (inFits_->IsFitsTable())
|
|---|
| 94 | {
|
|---|
| 95 | if (dvl.GetS("ORDERING") != string("")) return newSphereHEALPix();
|
|---|
| 96 | // sinon on cherche ntuple ou xntuple
|
|---|
| 97 | else
|
|---|
| 98 | {
|
|---|
| 99 | int index=0;
|
|---|
| 100 | int k;
|
|---|
| 101 | for (k=0; k < inFits_->NbColsFromFits(); k++)
|
|---|
| 102 | {
|
|---|
| 103 | if (inFits_->ColTypeFromFits(k) != FitsFile::FitsDataType_float)
|
|---|
| 104 | {
|
|---|
| 105 | index = 1;
|
|---|
| 106 | break;
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | if (index == 0) return newNTuple();
|
|---|
| 110 | else
|
|---|
| 111 | {
|
|---|
| 112 | if (!toutCharger )
|
|---|
| 113 | {
|
|---|
| 114 | int maxEntries=0;
|
|---|
| 115 | int k;
|
|---|
| 116 | for ( k=0; k < inFits_->NbColsFromFits(); k++)
|
|---|
| 117 | {
|
|---|
| 118 | if (inFits_->NentriesFromFits(k) > maxEntries ) maxEntries = inFits_->NentriesFromFits(k);
|
|---|
| 119 | }
|
|---|
| 120 | if ( maxEntries > 1000) return newFitsBTNtuIntf(hdunum);
|
|---|
| 121 | else return newXNTuple();
|
|---|
| 122 | }
|
|---|
| 123 | else return newXNTuple();
|
|---|
| 124 |
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | else
|
|---|
| 129 | {
|
|---|
| 130 | cout << " WARNING ( FITS_AutoReader::ReadObject) : object not recognized as a SOPHYA object" << endl;
|
|---|
| 131 | return new DVList(dvl);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | AnyDataObj* FITS_AutoReader::newTArray() const
|
|---|
| 137 | {
|
|---|
| 138 | FitsFile::FitsDataType dtype = inFits_->ImageType();
|
|---|
| 139 | switch (dtype)
|
|---|
| 140 | {
|
|---|
| 141 | case FitsFile::FitsDataType_double :
|
|---|
| 142 | {
|
|---|
| 143 | TArray<r_8>* matptr = new TArray<r_8>;
|
|---|
| 144 | FITS_TArray<r_8> fta(matptr);
|
|---|
| 145 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 146 | return matptr;
|
|---|
| 147 | }
|
|---|
| 148 | case FitsFile::FitsDataType_float :
|
|---|
| 149 | {
|
|---|
| 150 | TArray<r_4>* matptr = new TArray<r_4>;
|
|---|
| 151 | FITS_TArray<r_4> fta(matptr);
|
|---|
| 152 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 153 | return matptr;
|
|---|
| 154 | }
|
|---|
| 155 | case FitsFile::FitsDataType_int :
|
|---|
| 156 | {
|
|---|
| 157 | TArray<int_4>* matptr = new TArray<int_4>;
|
|---|
| 158 | FITS_TArray<int_4> fta(matptr);
|
|---|
| 159 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 160 | return matptr;
|
|---|
| 161 | }
|
|---|
| 162 | default :
|
|---|
| 163 | cout << "type = " << (int) dtype << endl;
|
|---|
| 164 | throw IOExc("FITS_AutoReader::ReadObject : unsupported data type for TArray");
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | AnyDataObj* FITS_AutoReader::newSphereHEALPix() const
|
|---|
| 169 | {
|
|---|
| 170 | FitsFile::FitsDataType dtype;
|
|---|
| 171 | if (inFits_->IsFitsTable()) dtype = inFits_->ColTypeFromFits(0);
|
|---|
| 172 | else
|
|---|
| 173 | {
|
|---|
| 174 | cout << " WARNING : Sperehealpix not binary table : unusual " << endl;
|
|---|
| 175 | dtype = inFits_->ImageType();
|
|---|
| 176 | }
|
|---|
| 177 | switch (dtype)
|
|---|
| 178 | {
|
|---|
| 179 | case FitsFile::FitsDataType_double :
|
|---|
| 180 | {
|
|---|
| 181 | SphereHEALPix<r_8>* sphptr = new SphereHEALPix<r_8>;
|
|---|
| 182 | FITS_SphereHEALPix<r_8> fta(sphptr);
|
|---|
| 183 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 184 | return sphptr;
|
|---|
| 185 | }
|
|---|
| 186 | case FitsFile::FitsDataType_float :
|
|---|
| 187 | {
|
|---|
| 188 | SphereHEALPix<r_4>* sphptr = new SphereHEALPix<r_4>;
|
|---|
| 189 | FITS_SphereHEALPix<r_4> fta(sphptr);
|
|---|
| 190 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 191 | return sphptr;
|
|---|
| 192 | }
|
|---|
| 193 | //CMV: Bon c'est quand meme sympa de pouvoir re-lire ce
|
|---|
| 194 | // qu'on a ecrit !! Je fais cette modif mais a priori
|
|---|
| 195 | // ce n'est que du sparadra car ca devrait planter
|
|---|
| 196 | // sur certaines machines (int!=long).
|
|---|
| 197 | case FitsFile::FitsDataType_int :
|
|---|
| 198 | case FitsFile::FitsDataType_long :
|
|---|
| 199 | {
|
|---|
| 200 | SphereHEALPix<int_4>* sphptr = new SphereHEALPix<int_4>;
|
|---|
| 201 | FITS_SphereHEALPix<int_4> fta(sphptr);
|
|---|
| 202 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 203 | return sphptr;
|
|---|
| 204 | }
|
|---|
| 205 | default :
|
|---|
| 206 | cout << "type = " << (int) dtype << endl;
|
|---|
| 207 | throw IOExc("FITS_AutoReader::ReadObject : unsupported data type for SphereHEALpix");
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | AnyDataObj* FITS_AutoReader::newLocalMap() const
|
|---|
| 212 | {
|
|---|
| 213 | FitsFile::FitsDataType dtype;
|
|---|
| 214 | if (inFits_->IsFitsTable()) dtype = inFits_->ColTypeFromFits(0);
|
|---|
| 215 | else
|
|---|
| 216 | {
|
|---|
| 217 | cout << " WARNING : Localmap not binary table : unusual " << endl;
|
|---|
| 218 | dtype = inFits_->ImageType();
|
|---|
| 219 | }
|
|---|
| 220 | switch (dtype)
|
|---|
| 221 | {
|
|---|
| 222 | case FitsFile::FitsDataType_double :
|
|---|
| 223 | {
|
|---|
| 224 | LocalMap<r_8>* locmptr = new LocalMap<r_8>;
|
|---|
| 225 | FITS_LocalMap<r_8> fta(locmptr);
|
|---|
| 226 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 227 | return locmptr;
|
|---|
| 228 | }
|
|---|
| 229 | case FitsFile::FitsDataType_float :
|
|---|
| 230 | {
|
|---|
| 231 | LocalMap<r_4>* locmptr = new LocalMap<r_4>;
|
|---|
| 232 | FITS_LocalMap<r_4> fta(locmptr);
|
|---|
| 233 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 234 | return locmptr;
|
|---|
| 235 | }
|
|---|
| 236 | case FitsFile::FitsDataType_int :
|
|---|
| 237 | {
|
|---|
| 238 | LocalMap<int_4>* locmptr = new LocalMap<int_4>;
|
|---|
| 239 | FITS_LocalMap<int_4> fta(locmptr);
|
|---|
| 240 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 241 | return locmptr;
|
|---|
| 242 | }
|
|---|
| 243 | default :
|
|---|
| 244 | cout << "type = " << (int) dtype << endl;
|
|---|
| 245 | throw IOExc("FITS_AutoReader::ReadObject : unsupported data type for LocalMap");
|
|---|
| 246 | }
|
|---|
| 247 | }
|
|---|
| 248 | NTuple* FITS_AutoReader::newNTuple() const
|
|---|
| 249 | {
|
|---|
| 250 | NTuple* ntptr = new NTuple;
|
|---|
| 251 | FITS_NTuple fta(ntptr);
|
|---|
| 252 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 253 | return ntptr;
|
|---|
| 254 | }
|
|---|
| 255 | XNTuple* FITS_AutoReader::newXNTuple() const
|
|---|
| 256 | {
|
|---|
| 257 | XNTuple* xntptr = new XNTuple;
|
|---|
| 258 | FITS_XNTuple fta(xntptr);
|
|---|
| 259 | fta.Read(*inFits_, inFits_->currentHeaderIndex());
|
|---|
| 260 | return xntptr;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | FitsBTNtuIntf* FITS_AutoReader::newFitsBTNtuIntf(int hdunum) const
|
|---|
| 264 | {
|
|---|
| 265 | FitsBTNtuIntf* btnptr = new FitsBTNtuIntf(filename_,hdunum);
|
|---|
| 266 | return btnptr;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|