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