| [1654] | 1 | /* Lecteur de colonne de table Fits (binaire ou ASCII) avec buffer */
 | 
|---|
| [2615] | 2 | #include "sopnamsp.h"
 | 
|---|
| [1654] | 3 | #include "machdefs.h"
 | 
|---|
 | 4 | #include <stdlib.h>
 | 
|---|
 | 5 | #include <stdio.h>
 | 
|---|
 | 6 | #include "pexceptions.h"
 | 
|---|
 | 7 | #include "fabtcolread.h"
 | 
|---|
| [2449] | 8 | 
 | 
|---|
 | 9 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 10 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 11 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 12 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 13 | 
 | 
|---|
 | 14 | //! Class for opening a FITS file for reading
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | /*!
 | 
|---|
 | 17 |   \class SOPHYA::FitsOpenFile
 | 
|---|
 | 18 |   \ingroup FitsIOServer
 | 
|---|
 | 19 |   Class for opening a FITS file for reading
 | 
|---|
 | 20 | */
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | /*!
 | 
|---|
 | 23 |   Constructor.
 | 
|---|
 | 24 |   \param fname : FITS file name to be opened for reading
 | 
|---|
 | 25 | */
 | 
|---|
 | 26 | FitsOpenFile::FitsOpenFile(string fname)
 | 
|---|
 | 27 | {
 | 
|---|
 | 28 |   Init(fname.c_str());
 | 
|---|
 | 29 | }
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | /*!
 | 
|---|
 | 32 |   Constructor.
 | 
|---|
 | 33 |   \param cfname : FITS file name to be opened for reading
 | 
|---|
 | 34 | */
 | 
|---|
 | 35 | FitsOpenFile::FitsOpenFile(const char *cfname)
 | 
|---|
 | 36 | {
 | 
|---|
 | 37 |   Init(cfname);
 | 
|---|
 | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | /*!
 | 
|---|
 | 41 |   Constructor by default.
 | 
|---|
 | 42 | */
 | 
|---|
 | 43 | FitsOpenFile::FitsOpenFile()
 | 
|---|
 | 44 | {
 | 
|---|
 | 45 |   FitsFN = "";
 | 
|---|
| [2456] | 46 |   NHdu = IHdu = HduType = 0;
 | 
|---|
 | 47 |   HasBeenPos = false;
 | 
|---|
| [2449] | 48 |   FitsPtr = NULL;
 | 
|---|
 | 49 | }
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | /*!
 | 
|---|
 | 52 |   Constructor by copy.
 | 
|---|
 | 53 | */
 | 
|---|
 | 54 | FitsOpenFile::FitsOpenFile(FitsOpenFile& fof)
 | 
|---|
 | 55 | {
 | 
|---|
| [2456] | 56 |   Init(fof.FileName().c_str());
 | 
|---|
| [2449] | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | /*!
 | 
|---|
 | 60 |   Destructor.
 | 
|---|
 | 61 | */
 | 
|---|
 | 62 | FitsOpenFile::~FitsOpenFile()
 | 
|---|
 | 63 | {
 | 
|---|
 | 64 |   Delete();
 | 
|---|
 | 65 |   FitsFN = "";
 | 
|---|
| [2456] | 66 |   NHdu = IHdu = HduType = 0;
 | 
|---|
 | 67 |   HasBeenPos = false;
 | 
|---|
| [2449] | 68 | }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | /*!
 | 
|---|
 | 71 |   Delete routine called by the destructor.
 | 
|---|
 | 72 | */
 | 
|---|
 | 73 | void FitsOpenFile::Delete(void)
 | 
|---|
 | 74 | {
 | 
|---|
 | 75 |   if(FitsPtr != NULL) {
 | 
|---|
 | 76 |     int sta = 0;
 | 
|---|
 | 77 |     if(fits_close_file(FitsPtr,&sta)) printerror(sta);
 | 
|---|
 | 78 |     FitsPtr = NULL;
 | 
|---|
 | 79 |   }
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | /*! Init routine called by the constructor */
 | 
|---|
 | 83 | void FitsOpenFile::Init(const char* fname)
 | 
|---|
 | 84 | {
 | 
|---|
| [2456] | 85 |   // Parametres Generaux
 | 
|---|
 | 86 |   FitsFN = fname;
 | 
|---|
 | 87 |   NHdu = IHdu = HduType = 0;
 | 
|---|
 | 88 |   HasBeenPos = false;
 | 
|---|
 | 89 |   FitsPtr = NULL;
 | 
|---|
| [2449] | 90 | 
 | 
|---|
| [2456] | 91 |   // Ouverture du fichier
 | 
|---|
 | 92 |   if(FitsFN.size() <= 0 )
 | 
|---|
 | 93 |      throw ParmError("FitsOpenFile::Init: Fits file name error\n");
 | 
|---|
| [2449] | 94 | 
 | 
|---|
| [2456] | 95 |   int sta = 0;
 | 
|---|
 | 96 |   if(fits_open_file(&FitsPtr,FitsFN.c_str(),READONLY,&sta)) {
 | 
|---|
 | 97 |     printerror(sta);
 | 
|---|
 | 98 |     FitsPtr = NULL;
 | 
|---|
 | 99 |     throw NullPtrError("FitsOpenFile::Init: Error opening Fits file\n");
 | 
|---|
 | 100 |   }
 | 
|---|
| [2449] | 101 | 
 | 
|---|
| [2456] | 102 |   // Get number of hdu
 | 
|---|
 | 103 |   if(fits_get_num_hdus(FitsPtr,&NHdu,&sta)) {
 | 
|---|
 | 104 |     printerror(sta);
 | 
|---|
 | 105 |     NHdu = 0;
 | 
|---|
 | 106 |     Delete();
 | 
|---|
 | 107 |     throw NotAvailableOperation("FitsOpenFile::Init: Error getting NHdu\n");
 | 
|---|
 | 108 |   }
 | 
|---|
 | 109 |   if(NHdu<=0) {
 | 
|---|
 | 110 |     Delete();
 | 
|---|
 | 111 |     throw SzMismatchError("FitsOpenFile::Init: Bad NHdu\n");
 | 
|---|
 | 112 |   }
 | 
|---|
| [2449] | 113 | 
 | 
|---|
| [2456] | 114 |   MoveToHDU(1);
 | 
|---|
 | 115 | }
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | /*! Move to an HDU
 | 
|---|
 | 118 | \param ihdu: hdu number to move
 | 
|---|
 | 119 | \warning ihdu = [1,nhdu]
 | 
|---|
 | 120 | \return 0 if positionning failed, ihdu if success
 | 
|---|
 | 121 | */
 | 
|---|
 | 122 | int FitsOpenFile::MoveToHDU(int ihdu)
 | 
|---|
 | 123 | {
 | 
|---|
 | 124 |  if(FitsPtr==NULL)
 | 
|---|
 | 125 |    throw NullPtrError("FitsOpenFile::MoveToHDU: no fits file open FitsPtr==NULL\n");
 | 
|---|
 | 126 |  int ihdusave = IHdu;
 | 
|---|
 | 127 |  if(ihdu<=0) ihdu=1; if(ihdu>NHdu) ihdu=NHdu;
 | 
|---|
 | 128 |  int sta=0;
 | 
|---|
 | 129 |  if(fits_movabs_hdu(FitsPtr,ihdu,&HduType,&sta)) {
 | 
|---|
 | 130 |    printerror(sta);
 | 
|---|
 | 131 |    // On se repositionne ou on etait
 | 
|---|
 | 132 |    fits_movabs_hdu(FitsPtr,ihdusave,&HduType,&sta);
 | 
|---|
 | 133 |    IHdu = ihdusave;
 | 
|---|
 | 134 |  } else IHdu = ihdu;
 | 
|---|
 | 135 |  return IHdu;
 | 
|---|
 | 136 | }
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 | /*! Move to the first HDU of a certain type
 | 
|---|
 | 139 | \param hdutype: type of the hdu
 | 
|---|
 | 140 | \param hdudeb: start at that hdu
 | 
|---|
 | 141 | \return the type of HDU the file is positionned
 | 
|---|
 | 142 | */
 | 
|---|
 | 143 | int FitsOpenFile::MoveToFirst(int hdutype,int ihdudeb)
 | 
|---|
 | 144 | {
 | 
|---|
 | 145 |  if(ihdudeb<=0) ihdudeb=1; if(ihdudeb>NHdu) ihdudeb=NHdu;
 | 
|---|
 | 146 |  int ihdusave = IHdu;
 | 
|---|
 | 147 |  for(int ihdu=ihdudeb;ihdu<=NHdu;ihdu++) {
 | 
|---|
 | 148 |    MoveToHDU(ihdu);
 | 
|---|
 | 149 |    if(HduType==hdutype) break;
 | 
|---|
| [2449] | 150 |  }
 | 
|---|
| [2456] | 151 |  // Si echec, on se repositionne ou on etait
 | 
|---|
 | 152 |  if(HduType!=hdutype) MoveToHDU(ihdusave);
 | 
|---|
 | 153 |  return HduType;
 | 
|---|
 | 154 | }
 | 
|---|
| [2449] | 155 | 
 | 
|---|
| [2456] | 156 | /*! Move to the last HDU of a certain type
 | 
|---|
 | 157 | \param hdutype: type of the hdu
 | 
|---|
 | 158 | \param hdudeb: stop at that hdu
 | 
|---|
 | 159 | \return the type of HDU the file is positionned
 | 
|---|
 | 160 | */
 | 
|---|
 | 161 | int FitsOpenFile::MoveToLast(int hdutype,int ihdudeb)
 | 
|---|
 | 162 | {
 | 
|---|
 | 163 |  if(ihdudeb<=0) ihdudeb=1; if(ihdudeb>NHdu) ihdudeb=NHdu;
 | 
|---|
 | 164 |  int ihdusave = IHdu;
 | 
|---|
 | 165 |  for(int ihdu=NHdu;ihdu>=ihdudeb;ihdu--) {
 | 
|---|
 | 166 |    MoveToHDU(ihdu);
 | 
|---|
 | 167 |    if(HduType==hdutype) break;
 | 
|---|
 | 168 |  }
 | 
|---|
 | 169 |  // Si echec, on se repositionne ou on etait
 | 
|---|
 | 170 |  if(HduType!=hdutype) MoveToHDU(ihdusave);
 | 
|---|
 | 171 |  return HduType;
 | 
|---|
 | 172 | }
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 | /*! Print */
 | 
|---|
 | 175 | void FitsOpenFile::Print(void)
 | 
|---|
 | 176 | {
 | 
|---|
 | 177 |  cout<<"FitsOpenFile::Print: "<<FitsFN
 | 
|---|
 | 178 |      <<" hdu="<<IHdu<<"/"<<NHdu<<" type="<<HduType
 | 
|---|
 | 179 |      <<" hasbeenpos="<<HasBeenPos<<endl;
 | 
|---|
 | 180 | }
 | 
|---|
 | 181 | 
 | 
|---|
| [2453] | 182 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 183 | //// Methodes statiques
 | 
|---|
 | 184 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 185 | /*!
 | 
|---|
 | 186 |   Read a fitsheader key into double
 | 
|---|
 | 187 |   \param fitsptr : cfitio pointer to Fits file
 | 
|---|
 | 188 |   \param keyname : name of the key
 | 
|---|
 | 189 |   \return value into double
 | 
|---|
 | 190 | */
 | 
|---|
 | 191 | double FitsOpenFile::ReadKey(fitsfile *fitsptr,char *keyname)
 | 
|---|
 | 192 | {
 | 
|---|
| [2456] | 193 |  if(keyname==NULL || fitsptr==NULL) return 0.;
 | 
|---|
| [2453] | 194 |  int sta=0; double val=0.;
 | 
|---|
 | 195 |  if(fits_read_key(fitsptr,TDOUBLE,keyname,&val,NULL,&sta))
 | 
|---|
 | 196 |    printerror(sta);
 | 
|---|
 | 197 |  return val;
 | 
|---|
 | 198 | }
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 | /*!
 | 
|---|
 | 201 |   Read a fitsheader key into long
 | 
|---|
 | 202 |   \param fitsptr : cfitio pointer to Fits file
 | 
|---|
 | 203 |   \param keyname : name of the key
 | 
|---|
 | 204 |   \return value into long
 | 
|---|
 | 205 | */
 | 
|---|
 | 206 | long FitsOpenFile::ReadKeyL(fitsfile *fitsptr,char *keyname)
 | 
|---|
 | 207 | {
 | 
|---|
| [2456] | 208 |  if(keyname==NULL || fitsptr==NULL) return 0;
 | 
|---|
| [2453] | 209 |  int sta=0; long val=0;
 | 
|---|
 | 210 |  if(fits_read_key(fitsptr,TLONG,keyname,&val,NULL,&sta))
 | 
|---|
 | 211 |    printerror(sta);
 | 
|---|
 | 212 |  return val;
 | 
|---|
 | 213 | }
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 | /*!
 | 
|---|
 | 216 |   Read a fitsheader key into string
 | 
|---|
 | 217 |   \param fitsptr : cfitio pointer to Fits file
 | 
|---|
 | 218 |   \param keyname : name of the key
 | 
|---|
 | 219 |   \return value into string
 | 
|---|
 | 220 | */
 | 
|---|
 | 221 | string FitsOpenFile::ReadKeyS(fitsfile *fitsptr,char *keyname)
 | 
|---|
 | 222 | {
 | 
|---|
| [2456] | 223 |  if(keyname==NULL || fitsptr==NULL) return (string)"";
 | 
|---|
| [2453] | 224 |  int sta=0; char val[FLEN_VALUE];
 | 
|---|
 | 225 |  if(fits_read_key(fitsptr,TSTRING,keyname,val,NULL,&sta))
 | 
|---|
 | 226 |    printerror(sta);
 | 
|---|
 | 227 |  string sval = val;
 | 
|---|
 | 228 |  return sval;
 | 
|---|
 | 229 | }
 | 
|---|
 | 230 | 
 | 
|---|
 | 231 | /*!
 | 
|---|
 | 232 |   CFitsIO error printing routine
 | 
|---|
 | 233 |   \param sta : cfitio error return code
 | 
|---|
 | 234 | */
 | 
|---|
 | 235 |  void FitsOpenFile::printerror(int sta)
 | 
|---|
| [2449] | 236 |  {
 | 
|---|
 | 237 |   int stat = sta;
 | 
|---|
 | 238 |   fits_report_error(stdout,stat);
 | 
|---|
 | 239 |   fflush(stdout);
 | 
|---|
 | 240 |   return;
 | 
|---|
 | 241 |  }
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 244 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 245 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 246 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 247 | 
 | 
|---|
 | 248 | ///////////////////////////////////////////////////////////////////
 | 
|---|
| [1654] | 249 | //! Class for reading a column in a FITS ASCII or BINARY table
 | 
|---|
 | 250 | 
 | 
|---|
 | 251 | /*!
 | 
|---|
| [2449] | 252 |   \class SOPHYA::FitsABTColRd
 | 
|---|
| [1654] | 253 |   \ingroup FitsIOServer
 | 
|---|
| [2789] | 254 |   Class for reading a column in a FITS ASCII or BINARY table.
 | 
|---|
 | 255 |   You can read many columns of the same FITS table by instanciating
 | 
|---|
 | 256 |   many FitsABTColRd on the same FitsOpenFile. So, the FITS file is
 | 
|---|
 | 257 |   opened only once. Of course the various FitsABTColRd must read
 | 
|---|
 | 258 |   the same FITS file HDU.
 | 
|---|
| [1654] | 259 |   \verbatim
 | 
|---|
| [1659] | 260 |   -- Exemple:
 | 
|---|
| [2449] | 261 |   // Open the fits file with FitsOpenFile
 | 
|---|
 | 262 |   FitsOpenFile fof = new FitsOpenFile("myfits.fits");
 | 
|---|
 | 263 |   // Select the column to be read
 | 
|---|
 | 264 |   FitsABTColRd fbt(fof,"BoloMuv_28",0,1000,1,3);
 | 
|---|
| [2456] | 265 |   FitsABTColRd fbt2(fof,"BoloMuv_29",0,1000,1,3);
 | 
|---|
| [1654] | 266 |   fbt.SetDebug(3);
 | 
|---|
 | 267 |   fbt.Print(3);
 | 
|---|
| [1659] | 268 |   // Read element by element
 | 
|---|
| [1654] | 269 |   for(long i=0;i<fbt.GetNbLine();i++) {
 | 
|---|
 | 270 |     double x = fbt.Read(i);
 | 
|---|
| [2456] | 271 |     double y = fbt2.Read(i);
 | 
|---|
 | 272 |     if(i%lpmod==0) cout<<i<<": "<<x<<", "<<y<<endl;
 | 
|---|
| [1654] | 273 |   }
 | 
|---|
| [1659] | 274 |   // Read into a vector
 | 
|---|
 | 275 |   TVector<double> data;
 | 
|---|
 | 276 |   long n = fbt.Read(32,50,data);
 | 
|---|
 | 277 |     cout<<"Number of values read: "<<n<<endl;
 | 
|---|
 | 278 |   data.ReSize(100);
 | 
|---|
 | 279 |   n = fbt.Read(10,-1,data);
 | 
|---|
 | 280 |     cout<<"Number of values read: "<<n<<endl;
 | 
|---|
| [2456] | 281 |   TVector<double> data2;
 | 
|---|
 | 282 |   fbt2.Read(32,50,data);
 | 
|---|
| [2449] | 283 |   // Close the fits file
 | 
|---|
 | 284 |   delete fof;
 | 
|---|
| [1654] | 285 |   \endverbatim
 | 
|---|
 | 286 | */
 | 
|---|
 | 287 | 
 | 
|---|
 | 288 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 289 | /*!
 | 
|---|
 | 290 |   Constructor.
 | 
|---|
| [2449] | 291 |   \param fof : Pointer to the Class for opening the FITS file
 | 
|---|
| [1659] | 292 |   \param collabel : label of the column to be read
 | 
|---|
 | 293 |   \param ihdu : number of the HDU where the column is.
 | 
|---|
 | 294 |   \param blen : read buffer length
 | 
|---|
 | 295 |   \param bsens : buffer reading direction
 | 
|---|
 | 296 |   \param lp : debug level
 | 
|---|
| [1654] | 297 |   \verbatim
 | 
|---|
| [2456] | 298 |   - if ihdu<=0 first BINARY or ASCII table is taken
 | 
|---|
 | 299 |   - if ihdu>nhdu ihdu is set to nhdu
 | 
|---|
| [1659] | 300 |   - bsens>0    read forward
 | 
|---|
 | 301 |     bsens<0    read backward
 | 
|---|
 | 302 |     bsens==0   read centered
 | 
|---|
| [1654] | 303 |   \endverbatim
 | 
|---|
| [1659] | 304 |   \warning ihdu = [1,nhdu]
 | 
|---|
| [1654] | 305 | */
 | 
|---|
| [2449] | 306 | FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,string collabel
 | 
|---|
 | 307 |                           ,int ihdu,long blen,long bsens,int lp)
 | 
|---|
| [1654] | 308 | {
 | 
|---|
| [2449] | 309 |   Init(fof,collabel.c_str(),-1,ihdu,blen,bsens,lp);
 | 
|---|
| [1654] | 310 | }
 | 
|---|
 | 311 | 
 | 
|---|
 | 312 | /*!
 | 
|---|
 | 313 |   Constructor.
 | 
|---|
| [1659] | 314 |   Same as before but the column is identified by its column number
 | 
|---|
 | 315 |   \param colnum : number of the column to be read
 | 
|---|
 | 316 |   \warning col = [0,ncol[
 | 
|---|
| [1654] | 317 | */
 | 
|---|
| [2449] | 318 | FitsABTColRd::FitsABTColRd(FitsOpenFile* fof,int colnum
 | 
|---|
 | 319 |                           ,int ihdu,long blen,long bsens,int lp)
 | 
|---|
| [1654] | 320 | {
 | 
|---|
| [2449] | 321 |   Init(fof,"",colnum,ihdu,blen,bsens,lp);
 | 
|---|
| [1654] | 322 | }
 | 
|---|
 | 323 | 
 | 
|---|
| [2449] | 324 | /*! Constructor by copy */
 | 
|---|
 | 325 | FitsABTColRd::FitsABTColRd(FitsABTColRd& fbt)
 | 
|---|
| [1654] | 326 | {
 | 
|---|
| [2449] | 327 |   Init(fbt.GetFitsOpenFile(),fbt.GetColLabel().c_str()
 | 
|---|
| [2456] | 328 |       ,fbt.GetColNum(),fbt.HDU()
 | 
|---|
| [2449] | 329 |       ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
 | 
|---|
| [1654] | 330 | }
 | 
|---|
 | 331 | 
 | 
|---|
| [2449] | 332 | /*! Constructor by default */
 | 
|---|
 | 333 | FitsABTColRd::FitsABTColRd()
 | 
|---|
| [1654] | 334 | {
 | 
|---|
| [2449] | 335 |  ColLabel = ""; ColTUnit = ""; ColTForm = "";
 | 
|---|
 | 336 |  ColNum = -1; ColTypeCode = 0;
 | 
|---|
 | 337 |  NBcol = 0; NBline = 0;
 | 
|---|
 | 338 |  SetNulVal(); SetDebug(0);
 | 
|---|
 | 339 |  NFitsRead = 0;
 | 
|---|
 | 340 |  FitsOF = NULL; FitsPtr = NULL;
 | 
|---|
 | 341 |  LineDeb = LineFin = -1;
 | 
|---|
 | 342 |  Buffer = NULL;
 | 
|---|
| [1654] | 343 | }
 | 
|---|
 | 344 | 
 | 
|---|
 | 345 | /*! Init routine called by the constructor */
 | 
|---|
| [2449] | 346 | void FitsABTColRd::Init(FitsOpenFile* fof,const char* collabel,int colnum
 | 
|---|
| [1654] | 347 |                         ,int ihdu,long blen,long bsens,int lp)
 | 
|---|
 | 348 | {
 | 
|---|
| [2449] | 349 |  // Initialisation des Parametres Generaux
 | 
|---|
| [2456] | 350 |  ColLabel=collabel; ColTUnit=""; ColTForm=""; ColNum=colnum; ColTypeCode=0;
 | 
|---|
 | 351 |  NBcol = 0; NBline = 0;
 | 
|---|
 | 352 |  SetNulVal(); SetDebug(lp);
 | 
|---|
| [1654] | 353 |  NFitsRead = 0;
 | 
|---|
| [2456] | 354 |  FitsOF = NULL; FitsPtr = NULL;
 | 
|---|
| [1654] | 355 |  LineDeb = LineFin = -1;
 | 
|---|
 | 356 |  Buffer = NULL;
 | 
|---|
 | 357 | 
 | 
|---|
| [2449] | 358 |  // Caracteristiques du FitsOpenFile
 | 
|---|
 | 359 |  FitsOF = fof;
 | 
|---|
| [2456] | 360 |  if(FitsOF==NULL)
 | 
|---|
| [2449] | 361 |    throw NullPtrError("FitsABTColRd::Init: FitsOpenFile pointer is NULL\n");
 | 
|---|
| [2456] | 362 | 
 | 
|---|
| [2449] | 363 |  FitsPtr = FitsOF->GetFitsPtr();
 | 
|---|
| [2456] | 364 |  if(FitsPtr==NULL)
 | 
|---|
| [2449] | 365 |    throw NullPtrError("FitsABTColRd::Init: FitsPtr pointer is NULL\n");
 | 
|---|
| [1654] | 366 | 
 | 
|---|
| [2449] | 367 |  int sta = 0;
 | 
|---|
| [2456] | 368 |  if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU();
 | 
|---|
| [2449] | 369 | 
 | 
|---|
| [1654] | 370 |  // Get HDU for bin/ascii table
 | 
|---|
| [2456] | 371 |  // ATTENTION: le fichier est ouvert mais non positionne sur un HDU,
 | 
|---|
 | 372 |  // une classe utilisant ce fichier doit le positionner sur un HDU.
 | 
|---|
 | 373 |  // Par contre, si une autre classe utilise ce meme FitsOpenFile,
 | 
|---|
 | 374 |  // elle ne peut le positionner que sur ce meme HDU !
 | 
|---|
 | 375 |  if(FitsOF->GetPosStatus()==false) {
 | 
|---|
 | 376 |    if(ihdu==0) { // find the first BINARY then the first ASCII
 | 
|---|
 | 377 |      int rc = FitsOF->MoveToFirst(BINARY_TBL);
 | 
|---|
 | 378 |      if(rc!=BINARY_TBL) FitsOF->MoveToFirst(ASCII_TBL);
 | 
|---|
 | 379 |    } else {
 | 
|---|
 | 380 |      int rc = FitsOF->MoveToHDU(ihdu);
 | 
|---|
 | 381 |      if(rc!=ihdu)
 | 
|---|
 | 382 |        throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n");
 | 
|---|
| [1654] | 383 |    }
 | 
|---|
| [2456] | 384 |  } else { // Fits file has already been positionned
 | 
|---|
 | 385 |    if(ihdu>0 && ihdu!=HDU())
 | 
|---|
 | 386 |      throw RangeCheckError("FitsABTColRd::Init: file already posit. at another HDU\n");
 | 
|---|
| [1654] | 387 |  }
 | 
|---|
| [2456] | 388 | 
 | 
|---|
 | 389 |  // Check HDUType and set position status to TRUE
 | 
|---|
 | 390 |  if(HDUType()!=BINARY_TBL && HDUType()!=ASCII_TBL)
 | 
|---|
| [2449] | 391 |    throw TypeMismatchExc("FitsABTColRd::Init: HDU not ASCII/BINARY table\n");
 | 
|---|
| [2456] | 392 |  if(DbgLevel>1) cout<<"...Init ihdu="<<ihdu<<" HduType="<<HDUType()<<endl;
 | 
|---|
 | 393 |  FitsOF->SetPosStatus(true);
 | 
|---|
| [1654] | 394 | 
 | 
|---|
 | 395 |  // Get number of columns
 | 
|---|
 | 396 |  if(fits_get_num_cols(FitsPtr,&NBcol,&sta)) {
 | 
|---|
| [2456] | 397 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 398 |    throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of columns\n");
 | 
|---|
| [1654] | 399 |  }
 | 
|---|
 | 400 |  if(DbgLevel>1) cout<<"...Init  NBcol="<<NBcol<<endl;
 | 
|---|
| [2456] | 401 |  if(NBcol<1)
 | 
|---|
| [2449] | 402 |    throw RangeCheckError("FitsABTColRd::Init: Bad number of colums\n");
 | 
|---|
| [1654] | 403 | 
 | 
|---|
 | 404 |  // Get number of rows
 | 
|---|
 | 405 |  if(fits_get_num_rows(FitsPtr,&NBline,&sta)) {
 | 
|---|
| [2456] | 406 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 407 |    throw NotAvailableOperation("FitsABTColRd::Init: Error getting number of rows\n");
 | 
|---|
| [1654] | 408 |  }
 | 
|---|
 | 409 |  if(DbgLevel>1) cout<<"...Init  NBline="<<NBline<<endl;
 | 
|---|
| [2456] | 410 |  if(NBline<1)
 | 
|---|
| [2449] | 411 |    throw RangeCheckError("FitsABTColRd::Init: Bad number of rows\n");
 | 
|---|
| [1654] | 412 | 
 | 
|---|
 | 413 |  // Get column number
 | 
|---|
| [1660] | 414 |  char labelcol[128];
 | 
|---|
| [1654] | 415 |  if(ColLabel.size() > 0) {
 | 
|---|
 | 416 |    strcpy(labelcol,ColLabel.c_str());
 | 
|---|
 | 417 |    if(fits_get_colnum(FitsPtr,CASESEN,labelcol,&ColNum,&sta)) {
 | 
|---|
| [2456] | 418 |      FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 419 |      throw NotAvailableOperation("FitsABTColRd::Init: Error getting column name\n");
 | 
|---|
| [1654] | 420 |    }
 | 
|---|
 | 421 |    ColNum--;  // Convention [0,ncol[
 | 
|---|
 | 422 |  }
 | 
|---|
 | 423 |  if(DbgLevel>1) cout<<"...Init  ColNum="<<ColNum<<endl;
 | 
|---|
| [2456] | 424 |  if(ColNum<0 || ColNum>=NBcol)
 | 
|---|
| [2449] | 425 |    throw RangeCheckError("FitsABTColRd::Init: Bad column number\n");
 | 
|---|
| [1654] | 426 | 
 | 
|---|
 | 427 |  // Get column type
 | 
|---|
 | 428 |  if(fits_get_coltype(FitsPtr,ColNum+1,&ColTypeCode,NULL,NULL,&sta)) {
 | 
|---|
| [2456] | 429 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 430 |    throw ParmError("FitsABTColRd::Init: Error getting column type\n");
 | 
|---|
| [1654] | 431 |  }
 | 
|---|
 | 432 |  if(DbgLevel>1) cout<<"...Init ColTypeCode="<<ColTypeCode<<endl;
 | 
|---|
| [1660] | 433 |  if(ColTypeCode==TSTRING || ColTypeCode==TCOMPLEX ||  ColTypeCode==TDBLCOMPLEX
 | 
|---|
| [2456] | 434 |                          || ColTypeCode<0 )
 | 
|---|
| [2449] | 435 |    throw ParmError("FitsABTColRd::Init: Selected column is not Numerical\n");
 | 
|---|
| [1654] | 436 | 
 | 
|---|
 | 437 |  // Get column name back, tunit, tform
 | 
|---|
| [2174] | 438 |  char tunit[64], tform[64], tdisp[64];
 | 
|---|
| [2173] | 439 |  long repeat=0; double tscale=1., tzero=0.;
 | 
|---|
| [1654] | 440 |  int rc=0;
 | 
|---|
| [2456] | 441 |  if(HDUType()==BINARY_TBL) {
 | 
|---|
| [2173] | 442 |    fits_get_bcolparms(FitsPtr,ColNum+1,labelcol,tunit,tform
 | 
|---|
| [2174] | 443 |                      ,&repeat,&tscale,&tzero,NULL,tdisp,&sta);
 | 
|---|
| [1654] | 444 |  } else {
 | 
|---|
| [2173] | 445 |    fits_get_acolparms(FitsPtr,ColNum+1,labelcol,&repeat,tunit,tform
 | 
|---|
| [2174] | 446 |                      ,&tscale,&tzero,NULL,tdisp,&sta);
 | 
|---|
| [1654] | 447 |  }
 | 
|---|
 | 448 |  if(rc) {
 | 
|---|
| [2456] | 449 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 450 |    throw RangeCheckError("FitsABTColRd::Init: Error getting the column caracteristics\n");
 | 
|---|
| [1654] | 451 |  }
 | 
|---|
 | 452 |  ColLabel = labelcol;
 | 
|---|
 | 453 |  ColTUnit = tunit;
 | 
|---|
 | 454 |  ColTForm = tform;
 | 
|---|
 | 455 | 
 | 
|---|
| [2456] | 456 |  // Set the buffer for reading
 | 
|---|
 | 457 |  ChangeBuffer(blen,bsens);
 | 
|---|
 | 458 | 
 | 
|---|
| [1654] | 459 |  if(DbgLevel)
 | 
|---|
| [2449] | 460 |    cout<<"FitsABTColRd::Init Num="<<ColNum<<" Label="<<ColLabel
 | 
|---|
| [2173] | 461 |        <<" TypeCode="<<ColTypeCode<<" TUnit="<<ColTUnit<<" TForm="<<ColTForm<<endl;
 | 
|---|
 | 462 |  if(DbgLevel>1)
 | 
|---|
| [2174] | 463 |    cout<<"      (repeat="<<repeat<<",tscale="<<tscale<<",tzero="<<tzero
 | 
|---|
 | 464 |        <<",tdisp="<<tdisp<<")"<<endl;
 | 
|---|
| [1654] | 465 | 
 | 
|---|
 | 466 | }
 | 
|---|
 | 467 | 
 | 
|---|
 | 468 | /*! Destructor. */
 | 
|---|
| [2449] | 469 | FitsABTColRd::~FitsABTColRd()
 | 
|---|
| [1654] | 470 | {
 | 
|---|
 | 471 |  Delete();
 | 
|---|
 | 472 | }
 | 
|---|
 | 473 | 
 | 
|---|
| [2449] | 474 | /*! Delete called by the destructor */
 | 
|---|
 | 475 | void FitsABTColRd::Delete(void)
 | 
|---|
| [1814] | 476 | {
 | 
|---|
| [2449] | 477 |  if(Buffer!=NULL) {delete [] Buffer; Buffer=NULL;}
 | 
|---|
 | 478 |  LineDeb = LineFin = -1;
 | 
|---|
 | 479 |  //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
 | 
|---|
| [1814] | 480 | }
 | 
|---|
| [1654] | 481 | 
 | 
|---|
 | 482 | //////////////////////////////////////////////////////////////
 | 
|---|
| [1659] | 483 | /*! Change the buffer caracteristiques (see creator) */
 | 
|---|
| [2449] | 484 | void FitsABTColRd::ChangeBuffer(long blen,long bsens)
 | 
|---|
| [1654] | 485 | {
 | 
|---|
| [1657] | 486 |  long oldnbuffer = NBuffer;
 | 
|---|
 | 487 | 
 | 
|---|
 | 488 |  // Compute buffer caracteristics
 | 
|---|
| [1654] | 489 |  BuffLen = (blen<=0)? 1: blen;
 | 
|---|
 | 490 |  BuffSens = bsens;
 | 
|---|
| [1657] | 491 |  NBuffer = BuffLen;
 | 
|---|
 | 492 |  if(bsens==0 && NBuffer%2==0) NBuffer++;
 | 
|---|
| [1654] | 493 | 
 | 
|---|
| [1657] | 494 |  // De-allocate if necessary
 | 
|---|
| [1659] | 495 |  if(Buffer!=NULL) {
 | 
|---|
 | 496 |    // On des-alloue si pas assez de place
 | 
|---|
 | 497 |    // ou si l'ancienne place est beaucoup trop grande (>25%)
 | 
|---|
 | 498 |    if(oldnbuffer<NBuffer || (oldnbuffer>NBuffer+long(0.25*NBuffer)) )
 | 
|---|
 | 499 |      {delete [] Buffer; Buffer=NULL;}
 | 
|---|
 | 500 |  }
 | 
|---|
| [1654] | 501 | 
 | 
|---|
| [1657] | 502 |  // Re-allocate
 | 
|---|
 | 503 |  if(Buffer==NULL) Buffer = new double[NBuffer];
 | 
|---|
 | 504 | 
 | 
|---|
| [1654] | 505 |  // Tell program that nothing is into buffer
 | 
|---|
 | 506 |  LineDeb = LineFin = -1;
 | 
|---|
 | 507 | }
 | 
|---|
 | 508 | 
 | 
|---|
| [2449] | 509 | //////////////////////////////////////////////////////////////
 | 
|---|
| [2451] | 510 | /*!
 | 
|---|
 | 511 |   Read a fitsheader key into double
 | 
|---|
 | 512 |   \param keyname : name of the key
 | 
|---|
 | 513 |   \return value into double
 | 
|---|
 | 514 | */
 | 
|---|
| [2449] | 515 | double FitsABTColRd::ReadKey(char *keyname)
 | 
|---|
| [1654] | 516 | {
 | 
|---|
| [2453] | 517 |  return FitsOpenFile::ReadKey(FitsPtr,keyname);
 | 
|---|
| [1654] | 518 | }
 | 
|---|
 | 519 | 
 | 
|---|
| [2451] | 520 | /*!
 | 
|---|
 | 521 |   Read a fitsheader key into long
 | 
|---|
 | 522 |   \param keyname : name of the key
 | 
|---|
 | 523 |   \return value into long
 | 
|---|
 | 524 | */
 | 
|---|
 | 525 | long FitsABTColRd::ReadKeyL(char *keyname)
 | 
|---|
 | 526 | {
 | 
|---|
| [2453] | 527 |  return FitsOpenFile::ReadKeyL(FitsPtr,keyname);
 | 
|---|
| [2451] | 528 | }
 | 
|---|
 | 529 | 
 | 
|---|
 | 530 | /*!
 | 
|---|
 | 531 |   Read a fitsheader key into string
 | 
|---|
 | 532 |   \param keyname : name of the key
 | 
|---|
 | 533 |   \return value into string
 | 
|---|
 | 534 | */
 | 
|---|
 | 535 | string FitsABTColRd::ReadKeyS(char *keyname)
 | 
|---|
 | 536 | {
 | 
|---|
| [2453] | 537 |  return FitsOpenFile::ReadKeyS(FitsPtr,keyname);
 | 
|---|
| [2451] | 538 | }
 | 
|---|
 | 539 | 
 | 
|---|
| [1654] | 540 | /////////////////////////////////////////////////
 | 
|---|
 | 541 | /*!
 | 
|---|
| [1659] | 542 |   Read row "n" and return the value into a double
 | 
|---|
 | 543 |   \warning be carefull for the range: row = [0,NRows[
 | 
|---|
 | 544 |   \return value in double
 | 
|---|
 | 545 |   \param n : number of the row to be read.
 | 
|---|
| [1654] | 546 |   \verbatim
 | 
|---|
| [1659] | 547 |   usebuffer == true  : use read optimisation with bufferisation
 | 
|---|
 | 548 |             == false : no optimisation with bufferisation
 | 
|---|
 | 549 |                        just read one value
 | 
|---|
| [1654] | 550 |   \endverbatim
 | 
|---|
 | 551 | */
 | 
|---|
| [2449] | 552 | double FitsABTColRd::Read(long n,bool usebuffer)
 | 
|---|
| [1654] | 553 | // Attention: n=nline [0,NBline[, cfistio veut [1,NBline]
 | 
|---|
 | 554 | // Attention: colnum  [0,NBcol[ , cfistio veut [1,NBcol]
 | 
|---|
 | 555 | {
 | 
|---|
| [1659] | 556 |  int sta=0;
 | 
|---|
| [1654] | 557 |  if(n<0 || n>=NBline)
 | 
|---|
| [2449] | 558 |    throw RangeCheckError("FitsABTColRd::Read try to read outside line range\n");
 | 
|---|
| [1654] | 559 | 
 | 
|---|
 | 560 |  // Pas de bufferisation, on lit betement
 | 
|---|
| [1659] | 561 |  if(NBuffer==1 || !usebuffer) {
 | 
|---|
| [1654] | 562 |    NFitsRead++;
 | 
|---|
| [1659] | 563 |    double val;
 | 
|---|
 | 564 |    fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta);
 | 
|---|
| [1654] | 565 |    if(sta) {
 | 
|---|
| [2453] | 566 |      FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 567 |      throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
 | 
|---|
| [1654] | 568 |    }
 | 
|---|
| [1659] | 569 |    // On ne remplit Buffer[0] que si on a choisit
 | 
|---|
 | 570 |    // un mode de lecture non bufferise (n==1) DES LE DEBUT.
 | 
|---|
 | 571 |    // Si on a initialement choisit un mode bufferise (avec n>1),
 | 
|---|
 | 572 |    // Buffer contient les valeurs chargees auparavent.
 | 
|---|
 | 573 |    // Il ne faut pas faire {Buffer[0]=val; LineDeb=LineFin=n;}
 | 
|---|
 | 574 |    // car on perd l'info de ces valeurs.
 | 
|---|
 | 575 |    if(NBuffer==1) {Buffer[0]=val; LineDeb=LineFin=n;}
 | 
|---|
 | 576 |    return val;
 | 
|---|
| [1654] | 577 |  }
 | 
|---|
 | 578 | 
 | 
|---|
 | 579 |  // Gestion avec bufferisation
 | 
|---|
| [1659] | 580 |  if(!Buffer)
 | 
|---|
| [2449] | 581 |    throw RangeCheckError("FitsABTColRd::Read: Buffer not allocated\n");
 | 
|---|
| [1654] | 582 |  if(n<LineDeb || n>LineFin) {
 | 
|---|
 | 583 |    NFitsRead++;
 | 
|---|
 | 584 |    long row1,row2,nrow;
 | 
|---|
 | 585 |    if(BuffSens>0) { // Cas remplissage forward
 | 
|---|
 | 586 |      row1 = n+1;
 | 
|---|
| [1657] | 587 |      row2 = row1+NBuffer-1; if(row2>NBline) row2 = NBline;
 | 
|---|
| [1654] | 588 |    } else if(BuffSens<0) { // Cas remplissage backward
 | 
|---|
 | 589 |      row2 = n+1;
 | 
|---|
| [1657] | 590 |      row1 = row2-NBuffer+1; if(row1<1) row1 = 1;
 | 
|---|
| [1654] | 591 |    } else { // Cas remplissage centre
 | 
|---|
| [1657] | 592 |      row1 = n+1 - NBuffer/2; if(row1<1) row1 = 1;
 | 
|---|
 | 593 |      row2 = n+1 + NBuffer/2; if(row2>NBline) row2 = NBline;
 | 
|---|
| [1654] | 594 |    }
 | 
|---|
 | 595 |    nrow = row2 - row1 + 1;
 | 
|---|
 | 596 |    LineDeb = row1-1; LineFin = row2-1;
 | 
|---|
 | 597 |    //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
 | 
|---|
 | 598 |    //    <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
 | 
|---|
| [1659] | 599 |    fits_read_col(FitsPtr,TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer,NULL,&sta);
 | 
|---|
| [1654] | 600 |    if(sta) {
 | 
|---|
| [2453] | 601 |      FitsOpenFile::printerror(sta);
 | 
|---|
| [1654] | 602 |      LineDeb = LineFin = -1;
 | 
|---|
| [2449] | 603 |      throw NotAvailableOperation("FitsABTColRd::Read: Error Reading Fits file\n");
 | 
|---|
| [1654] | 604 |    }
 | 
|---|
 | 605 |  }
 | 
|---|
 | 606 | 
 | 
|---|
 | 607 |  long ibuf = n-LineDeb;
 | 
|---|
 | 608 |  return Buffer[ibuf];
 | 
|---|
 | 609 | }
 | 
|---|
 | 610 | 
 | 
|---|
 | 611 | /*!
 | 
|---|
| [1659] | 612 |   Read rows from "n1" to "n2" and return the values into TVector of double
 | 
|---|
 | 613 |   \return NREAD the number of values read (n2-n1+1).
 | 
|---|
 | 614 |   \warning row = [0,NRows[, the routine read [n1,n2]
 | 
|---|
| [1654] | 615 |   \verbatim
 | 
|---|
| [1659] | 616 |   - if n2<0 then read [n1,n2] where "n2=min(n1+vector_size-1,nrows-1)"
 | 
|---|
 | 617 |   - Last row read is ALWAYS: "n2 = n1 + NREAD -1"
 | 
|---|
 | 618 |   - The TVector is never resized if not necessary
 | 
|---|
 | 619 |   -------------------------------------------------------------------------
 | 
|---|
 | 620 |   - ex: suppose the column table contains 10 elements: nrows=10, rows=[0,9]
 | 
|---|
 | 621 | 
 | 
|---|
 | 622 |     TVector<double> V(5);
 | 
|---|
 | 623 |       bt.Read(3,5,V)  -> read rows=3,4,5     -> V.Size()==5 -> return 3
 | 
|---|
 | 624 |       bt.Read(3,-1,V) -> read rows=3,4,5,6,7 -> V.Size()==5 -> return 5
 | 
|---|
 | 625 |       bt.Read(7,-1,V) -> read rows=7,8,9     -> V.Size()==5 -> return 3
 | 
|---|
 | 626 |       bt.Read(2,-1,V) -> read rows=2,3,4,5,6 -> V.Size()==5 -> return 5
 | 
|---|
 | 627 |       bt.Read(-1,5,V) -> throw exception
 | 
|---|
 | 628 | 
 | 
|---|
 | 629 |     TVector<double> V(5);
 | 
|---|
 | 630 |       bt.Read(3,99,V) -> read rows=3,4,5,6,7,8,9 -> V.Size()==7 -> return 7
 | 
|---|
 | 631 | 
 | 
|---|
 | 632 |     TVector<double> V(5);
 | 
|---|
 | 633 |       bt.Read(2,8,V)  -> read rows=2,3,4,5,6,7,8 -> V.Size()==7 -> return 7
 | 
|---|
 | 634 | 
 | 
|---|
 | 635 |     TVector<double> V;
 | 
|---|
 | 636 |       bt.Read(3,5,V)  -> read rows=3,4,5 -> V.Size()==3 -> return 3
 | 
|---|
 | 637 | 
 | 
|---|
 | 638 |     TVector<double> V;
 | 
|---|
 | 639 |       bt.Read(3,-1,V) -> throw exception
 | 
|---|
 | 640 |   -------------------------------------------------------------------------
 | 
|---|
| [1654] | 641 |   \endverbatim
 | 
|---|
 | 642 | */
 | 
|---|
| [2449] | 643 | long FitsABTColRd::Read(long n1,long n2,TVector<double>& data)
 | 
|---|
| [1654] | 644 | {
 | 
|---|
| [1659] | 645 |  if(n1<0 || n1>=NBline)
 | 
|---|
| [2449] | 646 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
 | 
|---|
| [1659] | 647 |  if(data.Size()<=0 && n2<n1)
 | 
|---|
| [2449] | 648 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
 | 
|---|
| [1659] | 649 |  if(n2<0) n2 = n1 + data.Size()-1;
 | 
|---|
 | 650 |  if(n2>=NBline) n2 = NBline-1;
 | 
|---|
| [1654] | 651 | 
 | 
|---|
| [1659] | 652 |  sa_size_t nread = n2-n1+1;
 | 
|---|
 | 653 |  if(data.Size()<nread) data.SetSize(nread);
 | 
|---|
 | 654 | 
 | 
|---|
 | 655 |  //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
 | 
|---|
 | 656 |  int sta=0;
 | 
|---|
 | 657 |  fits_read_col(FitsPtr,TDOUBLE,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
 | 
|---|
 | 658 |  if(sta) {
 | 
|---|
| [2453] | 659 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 660 |    throw NotAvailableOperation("FitsABTColRd::Read_TVector<double>: Error Reading Fits file\n");
 | 
|---|
| [1659] | 661 |  }
 | 
|---|
 | 662 | 
 | 
|---|
 | 663 |  return nread;
 | 
|---|
| [1654] | 664 | }
 | 
|---|
 | 665 | 
 | 
|---|
| [1659] | 666 | /*! idem before but for TVector of float */
 | 
|---|
| [2449] | 667 | long FitsABTColRd::Read(long n1,long n2,TVector<float>& data)
 | 
|---|
| [1659] | 668 | {
 | 
|---|
 | 669 |  if(n1<0 || n1>=NBline)
 | 
|---|
| [2449] | 670 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
 | 
|---|
| [1659] | 671 |  if(data.Size()<=0 && n2<n1)
 | 
|---|
| [2449] | 672 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
 | 
|---|
| [1659] | 673 |  if(n2<0) n2 = n1 + data.Size()-1;
 | 
|---|
 | 674 |  if(n2>=NBline) n2 = NBline-1;
 | 
|---|
 | 675 | 
 | 
|---|
 | 676 |  sa_size_t nread = n2-n1+1;
 | 
|---|
 | 677 |  if(data.Size()<nread) data.SetSize(nread);
 | 
|---|
 | 678 | 
 | 
|---|
 | 679 |  //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
 | 
|---|
 | 680 |  int sta=0;
 | 
|---|
 | 681 |  fits_read_col(FitsPtr,TFLOAT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
 | 
|---|
 | 682 |  if(sta) {
 | 
|---|
| [2453] | 683 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 684 |    throw NotAvailableOperation("FitsABTColRd::Read_TVector<float>: Error Reading Fits file\n");
 | 
|---|
| [1659] | 685 |  }
 | 
|---|
 | 686 | 
 | 
|---|
 | 687 |  return nread;
 | 
|---|
 | 688 | }
 | 
|---|
 | 689 | 
 | 
|---|
| [2170] | 690 | /*! idem before but for TVector of unsigned short */
 | 
|---|
| [2449] | 691 | long FitsABTColRd::Read(long n1,long n2,TVector<uint_2>& data)
 | 
|---|
| [2170] | 692 | {
 | 
|---|
 | 693 |  if(n1<0 || n1>=NBline)
 | 
|---|
| [2449] | 694 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
 | 
|---|
| [2170] | 695 |  if(data.Size()<=0 && n2<n1)
 | 
|---|
| [2449] | 696 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
 | 
|---|
| [2170] | 697 |  if(n2<0) n2 = n1 + data.Size()-1;
 | 
|---|
 | 698 |  if(n2>=NBline) n2 = NBline-1;
 | 
|---|
 | 699 | 
 | 
|---|
 | 700 |  sa_size_t nread = n2-n1+1;
 | 
|---|
 | 701 |  if(data.Size()<nread) data.SetSize(nread);
 | 
|---|
 | 702 | 
 | 
|---|
 | 703 |  int sta=0;
 | 
|---|
 | 704 |  fits_read_col(FitsPtr,TUSHORT,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
 | 
|---|
 | 705 |  if(sta) {
 | 
|---|
| [2453] | 706 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 707 |    throw NotAvailableOperation("FitsABTColRd::Read_TVector<uint_2>: Error Reading Fits file\n");
 | 
|---|
| [2170] | 708 |  }
 | 
|---|
 | 709 | 
 | 
|---|
 | 710 |  return nread;
 | 
|---|
 | 711 | }
 | 
|---|
 | 712 | 
 | 
|---|
| [1659] | 713 | /*! idem before but for TVector of int_4 */
 | 
|---|
| [2449] | 714 | long FitsABTColRd::Read(long n1,long n2,TVector<int_4>& data)
 | 
|---|
| [1659] | 715 | {
 | 
|---|
 | 716 |  if(n1<0 || n1>=NBline)
 | 
|---|
| [2449] | 717 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
 | 
|---|
| [1659] | 718 |  if(data.Size()<=0 && n2<n1)
 | 
|---|
| [2449] | 719 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
 | 
|---|
| [1659] | 720 |  if(n2<0) n2 = n1 + data.Size()-1;
 | 
|---|
 | 721 |  if(n2>=NBline) n2 = NBline-1;
 | 
|---|
 | 722 | 
 | 
|---|
 | 723 |  sa_size_t nread = n2-n1+1;
 | 
|---|
 | 724 |  if(data.Size()<nread) data.SetSize(nread);
 | 
|---|
 | 725 | 
 | 
|---|
 | 726 |  //for(long i=n1;i<=n2;i++) data(i-n1) = Read(i);
 | 
|---|
 | 727 |  int sta=0;
 | 
|---|
 | 728 |  int T = (sizeof(long)==4) ? TLONG: TINT;
 | 
|---|
 | 729 |  fits_read_col(FitsPtr,T,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
 | 
|---|
 | 730 |  if(sta) {
 | 
|---|
| [2453] | 731 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 732 |    throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_4>: Error Reading Fits file\n");
 | 
|---|
| [1659] | 733 |  }
 | 
|---|
 | 734 | 
 | 
|---|
 | 735 |  return nread;
 | 
|---|
 | 736 | }
 | 
|---|
 | 737 | 
 | 
|---|
| [2169] | 738 | /*! idem before but for TVector of int_8 */
 | 
|---|
| [2449] | 739 | long FitsABTColRd::Read(long n1,long n2,TVector<int_8>& data)
 | 
|---|
| [2169] | 740 | {
 | 
|---|
 | 741 | #ifdef TLONGLONG
 | 
|---|
 | 742 |  if(n1<0 || n1>=NBline)
 | 
|---|
| [2449] | 743 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 1srt line \n");
 | 
|---|
| [2169] | 744 |  if(data.Size()<=0 && n2<n1)
 | 
|---|
| [2449] | 745 |    throw RangeCheckError("FitsABTColRd::Read TVector bad requested 2sd line \n");
 | 
|---|
| [2169] | 746 |  if(n2<0) n2 = n1 + data.Size()-1;
 | 
|---|
 | 747 |  if(n2>=NBline) n2 = NBline-1;
 | 
|---|
 | 748 | 
 | 
|---|
 | 749 |  sa_size_t nread = n2-n1+1;
 | 
|---|
 | 750 |  if(data.Size()<nread) data.SetSize(nread);
 | 
|---|
 | 751 | 
 | 
|---|
 | 752 |  int sta=0;
 | 
|---|
 | 753 |  fits_read_col(FitsPtr,TLONGLONG,ColNum+1,n1+1,1,nread,NULL,data.Data(),NULL,&sta);
 | 
|---|
 | 754 |  if(sta) {
 | 
|---|
| [2453] | 755 |    FitsOpenFile::printerror(sta);
 | 
|---|
| [2449] | 756 |    throw NotAvailableOperation("FitsABTColRd::Read_TVector<int_8>: Error Reading Fits file\n");
 | 
|---|
| [2169] | 757 |  }
 | 
|---|
 | 758 | 
 | 
|---|
 | 759 |  return nread;
 | 
|---|
 | 760 | #else
 | 
|---|
| [2449] | 761 |   throw PException("FitsABTColRd::Read(..,TVector<int_8>&) Not in that cfitsio version");
 | 
|---|
| [2169] | 762 | #endif
 | 
|---|
 | 763 | }
 | 
|---|
 | 764 | 
 | 
|---|
| [1654] | 765 | /////////////////////////////////////////////////
 | 
|---|
| [1659] | 766 | /*!
 | 
|---|
 | 767 |   Return the number of the first row where "val1"<=val<="val2" starting at row "rowstart"
 | 
|---|
 | 768 |   \verbatim
 | 
|---|
 | 769 |   - The search is performed from "rowstart" to the end
 | 
|---|
 | 770 |       in ascending order (from "rowstart" to nrows).
 | 
|---|
 | 771 |   - Warning: "rowstart<0" means "rowstart==0" (search all the table column)
 | 
|---|
 | 772 |              That is the default
 | 
|---|
 | 773 |   \endverbatim
 | 
|---|
 | 774 |   \return <0 means not found
 | 
|---|
 | 775 | */
 | 
|---|
| [2449] | 776 | long FitsABTColRd::FirstRow(double val1,double val2,long rowstart)
 | 
|---|
| [1659] | 777 | {
 | 
|---|
 | 778 |  long row = -1;
 | 
|---|
 | 779 |  if(NBline==0) return row;
 | 
|---|
 | 780 |  // Change buffer for efficiency
 | 
|---|
 | 781 |  long bsens=BuffSens; bool bchange=false;
 | 
|---|
 | 782 |  if(bsens<=0) {ChangeBuffer(BuffLen,1); bchange=true;}
 | 
|---|
 | 783 |  if(rowstart<0) rowstart = 0;
 | 
|---|
 | 784 |  if(rowstart>=NBline) rowstart = NBline-1;
 | 
|---|
 | 785 |  for(long i=rowstart;i<NBline;i++) {
 | 
|---|
 | 786 |    double val = Read(i);
 | 
|---|
 | 787 |    if(val<val1 || val>val2) continue;
 | 
|---|
 | 788 |    row = i;
 | 
|---|
 | 789 |    break;
 | 
|---|
 | 790 |  }
 | 
|---|
 | 791 |  if(bchange) ChangeBuffer(BuffLen,bsens);
 | 
|---|
 | 792 |  return row;
 | 
|---|
 | 793 | }
 | 
|---|
 | 794 | 
 | 
|---|
 | 795 | /*!
 | 
|---|
 | 796 |   Return the number of the first row where val1<=val<=val2 starting at row rowstart
 | 
|---|
 | 797 |   \return <0 means not found
 | 
|---|
 | 798 |   \verbatim
 | 
|---|
 | 799 |   - The search is performed from "rowstart" to the beginning
 | 
|---|
 | 800 |       in descending order (from "rowstart" to 0).
 | 
|---|
 | 801 |   - Warning: "rowstart<0" means "rowstart==nrows-1" (search all the table column)
 | 
|---|
 | 802 |              That is the default
 | 
|---|
 | 803 |   \endverbatim
 | 
|---|
 | 804 | */
 | 
|---|
| [2449] | 805 | long FitsABTColRd::LastRow(double val1,double val2,long rowstart)
 | 
|---|
| [1659] | 806 | {
 | 
|---|
 | 807 |  long row = -1;
 | 
|---|
 | 808 |  if(NBline==0) return row;
 | 
|---|
 | 809 |  // Change buffer for efficiency
 | 
|---|
 | 810 |  long bsens=BuffSens; bool bchange=false;
 | 
|---|
 | 811 |  if(bsens>=0) {ChangeBuffer(BuffLen,-1); bchange=true;}
 | 
|---|
 | 812 |  if(rowstart<0 || rowstart>=NBline) rowstart = NBline-1;
 | 
|---|
 | 813 |  for(long i=rowstart;i>=0;i--) {
 | 
|---|
 | 814 |    double val = Read(i);
 | 
|---|
 | 815 |    if(val<val1 || val>val2) continue;
 | 
|---|
 | 816 |    row = i;
 | 
|---|
 | 817 |    break;
 | 
|---|
 | 818 |  }
 | 
|---|
 | 819 |  if(bchange) ChangeBuffer(BuffLen,bsens);
 | 
|---|
 | 820 |  return row;
 | 
|---|
 | 821 | }
 | 
|---|
 | 822 | 
 | 
|---|
| [1654] | 823 | /*! Print on stream os */
 | 
|---|
| [2449] | 824 | void FitsABTColRd::Print(ostream& os,int lp) const
 | 
|---|
| [1654] | 825 | {
 | 
|---|
| [2449] | 826 |  os<<"FitsABTColRd:Print ("<<BuffLen<<","<<BuffSens<<","<<NulVal<<")"
 | 
|---|
| [1654] | 827 |    <<" ncols="<<NBcol<<" nrows="<<NBline;
 | 
|---|
 | 828 |  if(lp>0) os<<" NRead="<<NFitsRead;
 | 
|---|
| [2456] | 829 |  os<<"\n... "<<FileName()<<"["<<HDU()<<"/"<<NHDU()<<" type="<<HDUType()<<"]"
 | 
|---|
 | 830 |    <<"\n... Label["<<ColNum<<"]="<<ColLabel<<" TypeCode="<<ColTypeCode
 | 
|---|
| [1654] | 831 |    <<" TUnit="<<ColTUnit<<" TForm="<<ColTForm
 | 
|---|
 | 832 |    <<endl;
 | 
|---|
 | 833 | }
 | 
|---|
| [2449] | 834 | 
 | 
|---|
 | 835 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 836 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 837 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 838 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 839 | 
 | 
|---|
 | 840 | //! Class for reading a column in a FITS ASCII or BINARY table with fits file opening
 | 
|---|
 | 841 | 
 | 
|---|
 | 842 | /*!
 | 
|---|
 | 843 |   \class SOPHYA::FitsABTColRead
 | 
|---|
 | 844 |   \ingroup FitsIOServer
 | 
|---|
| [2789] | 845 |   Class for reading a column in a FITS ASCII or BINARY table with fits file opening.
 | 
|---|
 | 846 |   The FITS file is opened each time you instanciate a FitsABTColRead.
 | 
|---|
 | 847 |   So reading "n" columns of the same FITS table by instanciating "n"
 | 
|---|
 | 848 |   FitsABTColRead, will open "n" times te FITS file.
 | 
|---|
 | 849 |   Use FitsABTColRd if you want to open the FITS file only once.
 | 
|---|
| [2449] | 850 |   \verbatim
 | 
|---|
 | 851 |   -- Exemple:
 | 
|---|
 | 852 |   FitsABTColRead fbt("myfits.fits","BoloMuv_28",0,1000,1,3);
 | 
|---|
 | 853 |   fbt.SetDebug(3);
 | 
|---|
 | 854 |   fbt.Print(3);
 | 
|---|
 | 855 |   // Read element by element
 | 
|---|
 | 856 |   for(long i=0;i<fbt.GetNbLine();i++) {
 | 
|---|
 | 857 |     double x = fbt.Read(i);
 | 
|---|
 | 858 |     if(i%lpmod==0) cout<<i<<": "<<x<<endl;
 | 
|---|
 | 859 |   }
 | 
|---|
 | 860 |   // Read into a vector
 | 
|---|
 | 861 |   TVector<double> data;
 | 
|---|
 | 862 |   long n = fbt.Read(32,50,data);
 | 
|---|
 | 863 |     cout<<"Number of values read: "<<n<<endl;
 | 
|---|
 | 864 |   data.ReSize(100);
 | 
|---|
 | 865 |   n = fbt.Read(10,-1,data);
 | 
|---|
 | 866 |     cout<<"Number of values read: "<<n<<endl;
 | 
|---|
 | 867 |   \endverbatim
 | 
|---|
 | 868 | */
 | 
|---|
 | 869 | 
 | 
|---|
 | 870 | 
 | 
|---|
 | 871 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 872 | /*!
 | 
|---|
 | 873 |   Constructor.
 | 
|---|
 | 874 |   \param fname : FITS file name to be read
 | 
|---|
 | 875 |   \param collabel : label of the column to be read
 | 
|---|
 | 876 |   \param ihdu : number of the HDU where the column is.
 | 
|---|
 | 877 |   \param blen : read buffer length
 | 
|---|
 | 878 |   \param bsens : buffer reading direction
 | 
|---|
 | 879 |   \param lp : debug level
 | 
|---|
 | 880 |   \verbatim
 | 
|---|
| [2456] | 881 |   - if ihdu<=0 first BINARY or ASCII table is taken
 | 
|---|
 | 882 |   - if ihdu>nhdu ihdu is set to nhdu
 | 
|---|
| [2449] | 883 |   - bsens>0    read forward
 | 
|---|
 | 884 |     bsens<0    read backward
 | 
|---|
 | 885 |     bsens==0   read centered
 | 
|---|
 | 886 |   \endverbatim
 | 
|---|
 | 887 |   \warning ihdu = [1,nhdu]
 | 
|---|
 | 888 | */
 | 
|---|
 | 889 | FitsABTColRead::FitsABTColRead(string fname,string collabel
 | 
|---|
 | 890 |                               ,int ihdu,long blen,long bsens,int lp)
 | 
|---|
 | 891 | : FitsABTColRd(new FitsOpenFile(fname),collabel,ihdu,blen,bsens,lp)
 | 
|---|
 | 892 | {
 | 
|---|
 | 893 | }
 | 
|---|
 | 894 | 
 | 
|---|
 | 895 | /*!
 | 
|---|
 | 896 |   Constructor.
 | 
|---|
 | 897 |   Same as before but the column is identified by its column number
 | 
|---|
 | 898 |   \param colnum : number of the column to be read
 | 
|---|
 | 899 |   \warning col = [0,ncol[
 | 
|---|
 | 900 | */
 | 
|---|
 | 901 | FitsABTColRead::FitsABTColRead(string fname,int colnum
 | 
|---|
 | 902 |                               ,int ihdu,long blen,long bsens,int lp)
 | 
|---|
 | 903 | : FitsABTColRd(new FitsOpenFile(fname),colnum,ihdu,blen,bsens,lp)
 | 
|---|
 | 904 | {
 | 
|---|
 | 905 | }
 | 
|---|
 | 906 | 
 | 
|---|
 | 907 | /*! Constructor. see below */
 | 
|---|
 | 908 | FitsABTColRead::FitsABTColRead(const char * cfname,const char* collabel
 | 
|---|
 | 909 |                               ,int ihdu,long blen,long bsens,int lp)
 | 
|---|
 | 910 | : FitsABTColRd(new FitsOpenFile(cfname),collabel,ihdu,blen,bsens,lp)
 | 
|---|
 | 911 | {
 | 
|---|
 | 912 | }
 | 
|---|
 | 913 | 
 | 
|---|
 | 914 | /*! Constructor. see below */
 | 
|---|
 | 915 | FitsABTColRead::FitsABTColRead(const char * cfname,int colnum
 | 
|---|
 | 916 |                               ,int ihdu,long blen,long bsens,int lp)
 | 
|---|
 | 917 | : FitsABTColRd(new FitsOpenFile(cfname),colnum,ihdu,blen,bsens,lp)
 | 
|---|
 | 918 | {
 | 
|---|
 | 919 | }
 | 
|---|
 | 920 | /*! Constructor by default */
 | 
|---|
 | 921 | FitsABTColRead::FitsABTColRead()
 | 
|---|
| [2791] | 922 | : FitsABTColRd()
 | 
|---|
| [2449] | 923 | {
 | 
|---|
 | 924 | }
 | 
|---|
 | 925 | 
 | 
|---|
 | 926 | /*! Constructor by copy */
 | 
|---|
 | 927 | FitsABTColRead::FitsABTColRead(FitsABTColRead& fbt)
 | 
|---|
 | 928 | {
 | 
|---|
 | 929 |  // --- ATTENTION ---
 | 
|---|
 | 930 |  // FitsABTColRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
 | 
|---|
 | 931 |  FitsOpenFile* fof = new FitsOpenFile(*fbt.GetFitsOpenFile());
 | 
|---|
 | 932 |  Init(fof,fbt.GetColLabel().c_str()
 | 
|---|
| [2456] | 933 |      ,fbt.GetColNum(),fbt.HDU()
 | 
|---|
| [2449] | 934 |      ,fbt.GetBLen(),fbt.GetBSens(),fbt.DbgLevel);
 | 
|---|
 | 935 | }
 | 
|---|
 | 936 | 
 | 
|---|
 | 937 | /*! Destructor. */
 | 
|---|
 | 938 | FitsABTColRead::~FitsABTColRead()
 | 
|---|
 | 939 | {
 | 
|---|
| [2789] | 940 |  Delete(); // ?? inutile ??
 | 
|---|
 | 941 |  // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
 | 
|---|
| [2449] | 942 |  if(FitsOF!=NULL) delete FitsOF;
 | 
|---|
 | 943 | }
 | 
|---|
| [2453] | 944 | 
 | 
|---|
 | 945 | ///////////////////////////////////////////////////////////////////
 | 
|---|
| [2791] | 946 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 947 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 948 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 949 | 
 | 
|---|
| [2453] | 950 | //! Class for reading a 2D image from a FITS file
 | 
|---|
 | 951 | 
 | 
|---|
 | 952 | /*!
 | 
|---|
 | 953 |   \class SOPHYA::FitsImg2DRd
 | 
|---|
 | 954 |   \ingroup FitsIOServer
 | 
|---|
 | 955 |   Class for reading a 2D image from a FITS file
 | 
|---|
 | 956 | */
 | 
|---|
 | 957 | 
 | 
|---|
 | 958 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 959 | /*!
 | 
|---|
 | 960 |   Constructor.
 | 
|---|
 | 961 |   \param fof : Pointer to the Class for opening the FITS file
 | 
|---|
| [2456] | 962 |   \param ihdu : number of the HDU where the image is.
 | 
|---|
| [2453] | 963 |   \param lp : debug level
 | 
|---|
 | 964 |   \verbatim
 | 
|---|
| [2456] | 965 |   - if ihdu<=0 first IMAGE hdu is taken
 | 
|---|
 | 966 |   - if ihdu>nhdu ihdu is set to nhdu
 | 
|---|
| [2453] | 967 |   \endverbatim
 | 
|---|
 | 968 |   \warning ihdu = [1,nhdu]
 | 
|---|
 | 969 | */
 | 
|---|
 | 970 | FitsImg2DRd::FitsImg2DRd(FitsOpenFile* fof,int ihdu,int lp)
 | 
|---|
 | 971 | {
 | 
|---|
 | 972 |  Init(fof,ihdu,lp);
 | 
|---|
 | 973 | }
 | 
|---|
 | 974 | 
 | 
|---|
 | 975 | /*! Constructor by copy */
 | 
|---|
 | 976 | FitsImg2DRd::FitsImg2DRd(FitsImg2DRd& fbt)
 | 
|---|
 | 977 | {
 | 
|---|
| [2456] | 978 |  Init(fbt.GetFitsOpenFile(),fbt.HDU(),fbt.DbgLevel);
 | 
|---|
| [2453] | 979 | }
 | 
|---|
 | 980 | 
 | 
|---|
 | 981 | /*! Constructor by default */
 | 
|---|
 | 982 | FitsImg2DRd::FitsImg2DRd()
 | 
|---|
 | 983 | {
 | 
|---|
 | 984 |  Naxis[0] = Naxis[1] = 0;
 | 
|---|
 | 985 |  SetNulVal(); SetDebug(0);
 | 
|---|
 | 986 |  FitsOF = NULL; FitsPtr = NULL;
 | 
|---|
 | 987 | }
 | 
|---|
 | 988 | 
 | 
|---|
 | 989 | /*! Init routine called by the constructor */
 | 
|---|
 | 990 | void FitsImg2DRd::Init(FitsOpenFile* fof,int ihdu,int lp)
 | 
|---|
 | 991 | {
 | 
|---|
 | 992 |  // Initialisation des Parametres Generaux
 | 
|---|
 | 993 |  Naxis[0] = Naxis[1] = 0;
 | 
|---|
 | 994 |  SetNulVal(); SetDebug(lp);
 | 
|---|
 | 995 |  FitsOF = NULL; FitsPtr = NULL;
 | 
|---|
 | 996 | 
 | 
|---|
 | 997 |  // Caracteristiques du FitsOpenFile
 | 
|---|
 | 998 |  FitsOF = fof;
 | 
|---|
 | 999 |  if(FitsOF==NULL)
 | 
|---|
 | 1000 |    throw NullPtrError("FitsImg2DRd::Init: FitsOpenFile pointer is NULL\n");
 | 
|---|
| [2456] | 1001 | 
 | 
|---|
| [2453] | 1002 |  FitsPtr = FitsOF->GetFitsPtr();
 | 
|---|
 | 1003 |  if(FitsPtr==NULL)
 | 
|---|
 | 1004 |    throw NullPtrError("FitsImg2DRd::Init: FitsPtr pointer is NULL\n");
 | 
|---|
 | 1005 | 
 | 
|---|
 | 1006 |  int sta = 0;
 | 
|---|
| [2456] | 1007 |  if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU(); 
 | 
|---|
| [2453] | 1008 | 
 | 
|---|
 | 1009 |  // Get HDU 2D image
 | 
|---|
| [2456] | 1010 |  // ATTENTION: ... cf blabla equivalent dans FitsABTColRd::Init()
 | 
|---|
 | 1011 |  if(FitsOF->GetPosStatus()==false) {
 | 
|---|
 | 1012 |    if(ihdu==0) { // find the first IMAGE_HDU
 | 
|---|
 | 1013 |      FitsOF->MoveToFirst(IMAGE_HDU);
 | 
|---|
 | 1014 |    } else {
 | 
|---|
 | 1015 |      int rc = FitsOF->MoveToHDU(ihdu);
 | 
|---|
 | 1016 |      if(rc!=ihdu)
 | 
|---|
 | 1017 |        throw RangeCheckError("FitsABTColRd::Init: Error moving to requested HDU\n");
 | 
|---|
| [2453] | 1018 |    }
 | 
|---|
| [2456] | 1019 |  } else { // Fits file has already been positionned
 | 
|---|
 | 1020 |    if(ihdu>0 && ihdu!=HDU())
 | 
|---|
 | 1021 |      throw RangeCheckError("FitsABTColRd::Init: file already posit. at another HDU\n");
 | 
|---|
| [2453] | 1022 |  }
 | 
|---|
| [2456] | 1023 | 
 | 
|---|
 | 1024 |  // Check HDUType and set position status to TRUE
 | 
|---|
 | 1025 |  if(HDUType()!=IMAGE_HDU)
 | 
|---|
| [2453] | 1026 |    throw TypeMismatchExc("FitsImg2DRd::Init: HDU not IMAGE_HDU\n");
 | 
|---|
| [2456] | 1027 |  FitsOF->SetPosStatus(true);
 | 
|---|
| [2453] | 1028 | 
 | 
|---|
 | 1029 |  // Get NAXIS 1 et 2
 | 
|---|
 | 1030 |  int nfound=0;
 | 
|---|
 | 1031 |  if(fits_read_keys_lng(FitsPtr,"NAXIS",1,2,Naxis,&nfound,&sta)) {
 | 
|---|
 | 1032 |    FitsOpenFile::printerror(sta);
 | 
|---|
 | 1033 |    throw RangeCheckError("FitsImg2DRd::Init: Error reading NAXIS cards\n");
 | 
|---|
 | 1034 |  }
 | 
|---|
 | 1035 |  if(DbgLevel>1)
 | 
|---|
| [2456] | 1036 |    cout<<"...Init(hdu="<<HDU()<<")  NAXIS1="<<Naxis[0]<<" NAXIS2="
 | 
|---|
 | 1037 |        <<Naxis[1]<<" (nfound="<<nfound<<")"<<endl;
 | 
|---|
| [2453] | 1038 |  if(nfound!=2 || Naxis[0]<=0 || Naxis[1]<=0)
 | 
|---|
 | 1039 |    throw NotAvailableOperation("FitsImg2DRd::Init: bad Naxis[0-1] value\n");
 | 
|---|
 | 1040 | 
 | 
|---|
 | 1041 | }
 | 
|---|
 | 1042 | 
 | 
|---|
 | 1043 | /*! Destructor. */
 | 
|---|
 | 1044 | FitsImg2DRd::~FitsImg2DRd()
 | 
|---|
 | 1045 | {
 | 
|---|
 | 1046 |  //--- Surtout on ne "fits_close_file" pas le fichier FITS !!!
 | 
|---|
 | 1047 |  Naxis[0] = Naxis[1] = 0;
 | 
|---|
 | 1048 | }
 | 
|---|
 | 1049 | 
 | 
|---|
 | 1050 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 1051 | /*!
 | 
|---|
 | 1052 |   Read a fitsheader key into double
 | 
|---|
 | 1053 |   \param keyname : name of the key
 | 
|---|
 | 1054 |   \return value into double
 | 
|---|
 | 1055 | */
 | 
|---|
 | 1056 | double FitsImg2DRd::ReadKey(char *keyname)
 | 
|---|
 | 1057 | {
 | 
|---|
 | 1058 |  return FitsOpenFile::ReadKey(FitsPtr,keyname);
 | 
|---|
 | 1059 | }
 | 
|---|
 | 1060 | 
 | 
|---|
 | 1061 | /*!
 | 
|---|
 | 1062 |   Read a fitsheader key into long
 | 
|---|
 | 1063 |   \param keyname : name of the key
 | 
|---|
 | 1064 |   \return value into long
 | 
|---|
 | 1065 | */
 | 
|---|
 | 1066 | long FitsImg2DRd::ReadKeyL(char *keyname)
 | 
|---|
 | 1067 | {
 | 
|---|
 | 1068 |  return FitsOpenFile::ReadKeyL(FitsPtr,keyname);
 | 
|---|
 | 1069 | }
 | 
|---|
 | 1070 | 
 | 
|---|
 | 1071 | /*!
 | 
|---|
 | 1072 |   Read a fitsheader key into string
 | 
|---|
 | 1073 |   \param keyname : name of the key
 | 
|---|
 | 1074 |   \return value into string
 | 
|---|
 | 1075 | */
 | 
|---|
 | 1076 | string FitsImg2DRd::ReadKeyS(char *keyname)
 | 
|---|
 | 1077 | {
 | 
|---|
 | 1078 |  return FitsOpenFile::ReadKeyS(FitsPtr,keyname);
 | 
|---|
 | 1079 | }
 | 
|---|
 | 1080 | 
 | 
|---|
 | 1081 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 1082 | /* REMARQUE:
 | 
|---|
 | 1083 |  * Si une image FITS a NAXIS1=100 et NAXIS2=50
 | 
|---|
 | 1084 |  * alors un tableau 2D juste assez grand pour contenir l'image
 | 
|---|
 | 1085 |  * doit etre declare array[50][100] (et non pas array[100][50])
 | 
|---|
 | 1086 |  *                   array[NAXIS2][NAXIS1]
 | 
|---|
 | 1087 |  */
 | 
|---|
 | 1088 | /*!
 | 
|---|
 | 1089 | Read image into a TMatrix<uint_2>
 | 
|---|
 | 1090 | \warning TMatrix data(Naxis2,Naxis1)
 | 
|---|
 | 1091 | */
 | 
|---|
 | 1092 | long FitsImg2DRd::Read(TMatrix<uint_2>& data)
 | 
|---|
 | 1093 | {
 | 
|---|
 | 1094 |  int sta=0;
 | 
|---|
 | 1095 |  uint_2* arr = new uint_2[Naxis[0]];
 | 
|---|
 | 1096 |  data.ReSize(Naxis[1],Naxis[0]);
 | 
|---|
 | 1097 | 
 | 
|---|
 | 1098 |  for(int j=0;j<Naxis[1];j++) {
 | 
|---|
 | 1099 |    long deb = j*Naxis[0]+1, nel = Naxis[0];
 | 
|---|
 | 1100 |    fits_read_img(FitsPtr,TUSHORT,deb,nel,&NulVal,arr,NULL,&sta);
 | 
|---|
 | 1101 |    if(sta) {
 | 
|---|
 | 1102 |      FitsOpenFile::printerror(sta); delete [] arr;
 | 
|---|
 | 1103 |      throw
 | 
|---|
 | 1104 |        NotAvailableOperation("FitsImg2DRd::Read(TMatrix<uint_2>): Error Reading Fits file\n");
 | 
|---|
 | 1105 |    }
 | 
|---|
 | 1106 |    for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
 | 
|---|
 | 1107 |  }
 | 
|---|
 | 1108 | 
 | 
|---|
 | 1109 |  delete [] arr;
 | 
|---|
 | 1110 |  return Naxis[0]*Naxis[1];
 | 
|---|
 | 1111 |  }
 | 
|---|
 | 1112 | 
 | 
|---|
 | 1113 | /*! Read image into a TMatrix<int_4> */
 | 
|---|
 | 1114 | long FitsImg2DRd::Read(TMatrix<int_4>& data)
 | 
|---|
 | 1115 | {
 | 
|---|
 | 1116 |  int sta=0;
 | 
|---|
 | 1117 |  int_4* arr = new int_4[Naxis[0]];
 | 
|---|
 | 1118 |  data.ReSize(Naxis[1],Naxis[0]);
 | 
|---|
 | 1119 |  int T = (sizeof(long)==4) ? TLONG: TINT;
 | 
|---|
 | 1120 | 
 | 
|---|
 | 1121 |  for(int j=0;j<Naxis[1];j++) {
 | 
|---|
 | 1122 |    long deb = j*Naxis[0]+1, nel = Naxis[0];
 | 
|---|
 | 1123 |    fits_read_img(FitsPtr,T,deb,nel,&NulVal,arr,NULL,&sta);
 | 
|---|
 | 1124 |    if(sta) {
 | 
|---|
 | 1125 |      FitsOpenFile::printerror(sta); delete [] arr;
 | 
|---|
 | 1126 |      throw
 | 
|---|
 | 1127 |        NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_4>): Error Reading Fits file\n");
 | 
|---|
 | 1128 |    }
 | 
|---|
 | 1129 |    for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
 | 
|---|
 | 1130 |  }
 | 
|---|
 | 1131 | 
 | 
|---|
 | 1132 |  delete [] arr;
 | 
|---|
 | 1133 |  return Naxis[0]*Naxis[1];
 | 
|---|
 | 1134 |  }
 | 
|---|
 | 1135 | 
 | 
|---|
 | 1136 | /*! Read image into a TMatrix<int_8> */
 | 
|---|
 | 1137 | long FitsImg2DRd::Read(TMatrix<int_8>& data)
 | 
|---|
 | 1138 | {
 | 
|---|
 | 1139 |  int sta=0;
 | 
|---|
 | 1140 |  int_8* arr = new int_8[Naxis[0]];
 | 
|---|
 | 1141 |  data.ReSize(Naxis[1],Naxis[0]);
 | 
|---|
 | 1142 | 
 | 
|---|
 | 1143 |  for(int j=0;j<Naxis[1];j++) {
 | 
|---|
 | 1144 |    long deb = j*Naxis[0]+1, nel = Naxis[0];
 | 
|---|
 | 1145 |    fits_read_img(FitsPtr,TLONGLONG,deb,nel,&NulVal,arr,NULL,&sta);
 | 
|---|
 | 1146 |    if(sta) {
 | 
|---|
 | 1147 |      FitsOpenFile::printerror(sta); delete [] arr;
 | 
|---|
 | 1148 |      throw
 | 
|---|
 | 1149 |        NotAvailableOperation("FitsImg2DRd::Read(TMatrix<int_8>): Error Reading Fits file\n");
 | 
|---|
 | 1150 |    }
 | 
|---|
 | 1151 |    for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
 | 
|---|
 | 1152 |  }
 | 
|---|
 | 1153 | 
 | 
|---|
 | 1154 |  delete [] arr;
 | 
|---|
 | 1155 |  return Naxis[0]*Naxis[1];
 | 
|---|
 | 1156 |  }
 | 
|---|
 | 1157 | 
 | 
|---|
 | 1158 | /*! Read image into a TMatrix<float> */
 | 
|---|
 | 1159 | long FitsImg2DRd::Read(TMatrix<float>& data)
 | 
|---|
 | 1160 | {
 | 
|---|
 | 1161 |  int sta=0;
 | 
|---|
 | 1162 |  float* arr = new float[Naxis[0]];
 | 
|---|
 | 1163 |  data.ReSize(Naxis[1],Naxis[0]);
 | 
|---|
 | 1164 | 
 | 
|---|
 | 1165 |  for(int j=0;j<Naxis[1];j++) {
 | 
|---|
 | 1166 |    long deb = j*Naxis[0]+1, nel = Naxis[0];
 | 
|---|
 | 1167 |    fits_read_img(FitsPtr,TFLOAT,deb,nel,&NulVal,arr,NULL,&sta);
 | 
|---|
 | 1168 |    if(sta) {
 | 
|---|
 | 1169 |      FitsOpenFile::printerror(sta); delete [] arr;
 | 
|---|
 | 1170 |      throw
 | 
|---|
 | 1171 |        NotAvailableOperation("FitsImg2DRd::Read(TMatrix<float>): Error Reading Fits file\n");
 | 
|---|
 | 1172 |    }
 | 
|---|
 | 1173 |    for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
 | 
|---|
 | 1174 |  }
 | 
|---|
 | 1175 | 
 | 
|---|
 | 1176 |  delete [] arr;
 | 
|---|
 | 1177 |  return Naxis[0]*Naxis[1];
 | 
|---|
 | 1178 |  }
 | 
|---|
 | 1179 | 
 | 
|---|
 | 1180 | /*! Read image into a TMatrix<double> */
 | 
|---|
 | 1181 | long FitsImg2DRd::Read(TMatrix<double>& data)
 | 
|---|
 | 1182 | {
 | 
|---|
 | 1183 |  int sta=0;
 | 
|---|
 | 1184 |  double* arr = new double[Naxis[0]];
 | 
|---|
 | 1185 |  data.ReSize(Naxis[1],Naxis[0]);
 | 
|---|
 | 1186 | 
 | 
|---|
 | 1187 |  for(int j=0;j<Naxis[1];j++) {
 | 
|---|
 | 1188 |    long deb = j*Naxis[0]+1, nel = Naxis[0];
 | 
|---|
 | 1189 |    fits_read_img(FitsPtr,TDOUBLE,deb,nel,&NulVal,arr,NULL,&sta);
 | 
|---|
 | 1190 |    if(sta) {
 | 
|---|
 | 1191 |      FitsOpenFile::printerror(sta); delete [] arr;
 | 
|---|
 | 1192 |      throw
 | 
|---|
 | 1193 |        NotAvailableOperation("FitsImg2DRd::Read(TMatrix<double>): Error Reading Fits file\n");
 | 
|---|
 | 1194 |    }
 | 
|---|
 | 1195 |    for(int i=0;i<Naxis[0];i++) data(j,i) = arr[i];
 | 
|---|
 | 1196 |  }
 | 
|---|
 | 1197 | 
 | 
|---|
 | 1198 |  delete [] arr;
 | 
|---|
 | 1199 |  return Naxis[0]*Naxis[1];
 | 
|---|
 | 1200 |  }
 | 
|---|
| [2791] | 1201 | 
 | 
|---|
 | 1202 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 1203 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 1204 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 1205 | ///////////////////////////////////////////////////////////////////
 | 
|---|
 | 1206 | 
 | 
|---|
 | 1207 | //! Class for reading a 2D image from a FITS file
 | 
|---|
 | 1208 | 
 | 
|---|
 | 1209 | /*!
 | 
|---|
 | 1210 |   \class SOPHYA::FitsImg2DRead
 | 
|---|
 | 1211 |   \ingroup FitsIOServer
 | 
|---|
 | 1212 |   Class for reading a 2D image from a FITS file
 | 
|---|
 | 1213 | */
 | 
|---|
 | 1214 | 
 | 
|---|
 | 1215 | //////////////////////////////////////////////////////////////
 | 
|---|
 | 1216 | /*!
 | 
|---|
 | 1217 |   Constructor.
 | 
|---|
 | 1218 |   \param fname : name of the FITS file
 | 
|---|
 | 1219 |   \param ihdu : number of the HDU where the image is.
 | 
|---|
 | 1220 |   \param lp : debug level
 | 
|---|
 | 1221 |   \verbatim
 | 
|---|
 | 1222 |   - if ihdu<=0 first IMAGE hdu is taken
 | 
|---|
 | 1223 |   - if ihdu>nhdu ihdu is set to nhdu
 | 
|---|
 | 1224 |   \endverbatim
 | 
|---|
 | 1225 |   \warning ihdu = [1,nhdu]
 | 
|---|
 | 1226 | */
 | 
|---|
 | 1227 | FitsImg2DRead::FitsImg2DRead(string fname,int ihdu,int lp)
 | 
|---|
 | 1228 | : FitsImg2DRd(new FitsOpenFile(fname),ihdu,lp)
 | 
|---|
 | 1229 | {
 | 
|---|
 | 1230 | }
 | 
|---|
 | 1231 | 
 | 
|---|
 | 1232 | /*! Constructor. see below */
 | 
|---|
 | 1233 | FitsImg2DRead::FitsImg2DRead(const char * cfname,int ihdu,int lp)
 | 
|---|
 | 1234 | : FitsImg2DRd(new FitsOpenFile(cfname),ihdu,lp)
 | 
|---|
 | 1235 | {
 | 
|---|
 | 1236 | }
 | 
|---|
 | 1237 | 
 | 
|---|
 | 1238 | /*! Constructor by default */
 | 
|---|
 | 1239 | FitsImg2DRead::FitsImg2DRead()
 | 
|---|
 | 1240 | : FitsImg2DRd()
 | 
|---|
 | 1241 | {
 | 
|---|
 | 1242 | }
 | 
|---|
 | 1243 | 
 | 
|---|
 | 1244 | /*! Constructor by copy */
 | 
|---|
 | 1245 | FitsImg2DRead::FitsImg2DRead(FitsImg2DRead& fimg)
 | 
|---|
 | 1246 | {
 | 
|---|
 | 1247 |  // --- ATTENTION ---
 | 
|---|
 | 1248 |  // FitsImg2DRead ferme le fichier FITS: il faut dupliquer le FitsOpenFile
 | 
|---|
 | 1249 |  FitsOpenFile* fof = new FitsOpenFile(*fimg.GetFitsOpenFile());
 | 
|---|
 | 1250 |  Init(fof,fimg.HDU(),fimg.DbgLevel);
 | 
|---|
 | 1251 | }
 | 
|---|
 | 1252 | 
 | 
|---|
 | 1253 | /*! Destructor. */
 | 
|---|
 | 1254 | FitsImg2DRead::~FitsImg2DRead()
 | 
|---|
 | 1255 | {
 | 
|---|
 | 1256 |  // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS
 | 
|---|
 | 1257 |  if(FitsOF!=NULL) delete FitsOF;
 | 
|---|
 | 1258 | }
 | 
|---|