[1505] | 1 | /* Interface Fits BINARY/ASCII Table cmv 21/05/2001 */
|
---|
| 2 | #include "machdefs.h"
|
---|
| 3 | #include <stdlib.h>
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include "pexceptions.h"
|
---|
| 6 | #include "fbtntintf.h"
|
---|
| 7 |
|
---|
| 8 | //////////////////////////////////////////////////////////////
|
---|
[1507] | 9 | FitsBTNtuIntf::FitsBTNtuIntf(string fname,int ihdu
|
---|
| 10 | ,int_4 blen,int_4 bsens,uint_2 lp)
|
---|
[1505] | 11 | {
|
---|
| 12 | DbgLevel = lp;
|
---|
| 13 | SetNulVal();
|
---|
[1507] | 14 | SetBuffer(blen,bsens);
|
---|
[1505] | 15 | FitsFN = fname;
|
---|
| 16 | IHdu = ihdu;
|
---|
| 17 | Init();
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[1507] | 20 | FitsBTNtuIntf::FitsBTNtuIntf(char * cfname,int ihdu
|
---|
| 21 | ,int_4 blen,int_4 bsens,uint_2 lp)
|
---|
[1505] | 22 | {
|
---|
| 23 | DbgLevel = lp;
|
---|
| 24 | SetNulVal();
|
---|
[1507] | 25 | SetBuffer(blen,bsens);
|
---|
[1505] | 26 | FitsFN = cfname;
|
---|
| 27 | IHdu = ihdu;
|
---|
| 28 | Init();
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | FitsBTNtuIntf::FitsBTNtuIntf(FitsBTNtuIntf& fbtnt)
|
---|
| 32 | {
|
---|
| 33 | DbgLevel = fbtnt.DbgLevel;
|
---|
[1507] | 34 | SetNulVal(fbtnt.NulVal);
|
---|
| 35 | SetBuffer(fbtnt.BuffLen,fbtnt.BuffSens);
|
---|
[1505] | 36 | FitsFN = fbtnt.FitsFN;
|
---|
| 37 | IHdu = fbtnt.IHdu;
|
---|
| 38 | Init();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | FitsBTNtuIntf::FitsBTNtuIntf()
|
---|
| 42 | {
|
---|
| 43 | DbgLevel = 0;
|
---|
| 44 | SetNulVal();
|
---|
[1507] | 45 | SetBuffer();
|
---|
[1505] | 46 | FitsFN = "";
|
---|
| 47 | IHdu = -1;
|
---|
| 48 | Init();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | FitsBTNtuIntf::~FitsBTNtuIntf()
|
---|
| 52 | {
|
---|
| 53 | Delete();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[1507] | 56 |
|
---|
[1505] | 57 | //////////////////////////////////////////////////////////////
|
---|
[1507] | 58 | void FitsBTNtuIntf::ChangeBuffer(int_4 blen,int_4 bsens)
|
---|
| 59 | {
|
---|
| 60 | SetBuffer(blen,bsens);
|
---|
| 61 | DeAllocBuff();
|
---|
| 62 | AllocBuff();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void FitsBTNtuIntf::AllocBuff(void)
|
---|
| 66 | {
|
---|
| 67 | if(Buffer==NULL || NBcol<=0) return;
|
---|
| 68 | int_4 n = BuffLen; if(BuffSens==0) n++;
|
---|
| 69 | for(int icol=0;icol<NBcol;icol++)
|
---|
| 70 | if(ColReadable[icol]) Buffer[icol] = new double[n];
|
---|
| 71 | else Buffer[icol] = NULL;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void FitsBTNtuIntf::DeAllocBuff(void)
|
---|
| 75 | {
|
---|
| 76 | if(Buffer==NULL || NBcol<=0) return;
|
---|
| 77 | for(int icol=0;icol<NBcol;icol++)
|
---|
| 78 | if(Buffer[icol]!=NULL)
|
---|
| 79 | {delete [] Buffer[icol]; Buffer[icol] = NULL;}
|
---|
| 80 | LineDeb = LineFin = -1;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void FitsBTNtuIntf::Delete()
|
---|
| 84 | {
|
---|
| 85 | DeAllocBuff();
|
---|
| 86 | delete [] Buffer; Buffer=NULL;
|
---|
| 87 | if(mRet!=NULL) delete [] mRet;
|
---|
| 88 | if(ColReadable!=NULL) delete [] ColReadable;
|
---|
| 89 |
|
---|
| 90 | int sta = 0;
|
---|
| 91 | if(fits_close_file(FitsPtr,&sta)) printerror(sta);
|
---|
| 92 | FitsPtr = NULL;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[1505] | 95 | void FitsBTNtuIntf::Init()
|
---|
| 96 | {
|
---|
| 97 | NBcol = 0; NBline = 0;
|
---|
| 98 | LineDeb = LineFin = -1;
|
---|
| 99 | ColName.resize(0);
|
---|
| 100 | ColReadable = NULL;
|
---|
| 101 | Buffer = NULL;
|
---|
| 102 | mRet = NULL;
|
---|
| 103 | NFitsRead = 0;
|
---|
| 104 |
|
---|
| 105 | if(FitsFN.size() <= 0 ) {
|
---|
| 106 | FitsPtr = NULL;
|
---|
| 107 | IHdu = -1;
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | //////////////////////////
|
---|
| 112 | // Ouverture du fichier //
|
---|
| 113 | //////////////////////////
|
---|
| 114 |
|
---|
| 115 | int sta=0,nhdu,hdutype;
|
---|
| 116 | const char * cfitsfn = FitsFN.c_str();
|
---|
| 117 |
|
---|
| 118 | // Open fits
|
---|
| 119 | if(fits_open_file(&FitsPtr,cfitsfn,READONLY,&sta)) printerror(sta);
|
---|
| 120 |
|
---|
| 121 | // Get number of hdu
|
---|
| 122 | if(fits_get_num_hdus(FitsPtr,&nhdu,&sta)) printerror(sta);
|
---|
| 123 | if(DbgLevel) cout<<"FitsBTNtuIntf::Init nhdu="<<nhdu<<endl;
|
---|
| 124 | if(nhdu<=0) {Delete(); return;}
|
---|
| 125 |
|
---|
| 126 | // Get HDU for bin table
|
---|
| 127 | // si IHdu <=0 on cherche la 1ere bin table
|
---|
| 128 | // sinon on se positionne sur IHdu
|
---|
| 129 | if(IHdu<=0 || IHdu>nhdu)
|
---|
| 130 | for(int ihdu=1;ihdu<=nhdu;ihdu++) {
|
---|
| 131 | if(fits_movabs_hdu(FitsPtr,ihdu,&hdutype,&sta)) printerror(sta);
|
---|
| 132 | if(DbgLevel) cout<<"...Init ihdu="
|
---|
| 133 | <<ihdu<<" hdutype="<<hdutype<<endl;
|
---|
| 134 | if(hdutype==BINARY_TBL || hdutype==ASCII_TBL) {IHdu = ihdu; break;}
|
---|
| 135 | }
|
---|
| 136 | if(IHdu<=0 || IHdu>nhdu) {
|
---|
| 137 | cout<<"NO BINARY or ASCII hdu found"<<endl;
|
---|
| 138 | Delete();
|
---|
| 139 | return;
|
---|
| 140 | }
|
---|
| 141 | if(fits_movabs_hdu(FitsPtr,IHdu,&hdutype,&sta)) printerror(sta);
|
---|
| 142 | if(hdutype!=BINARY_TBL && hdutype!=ASCII_TBL) {Delete(); return;}
|
---|
| 143 |
|
---|
| 144 | // Get number of columns
|
---|
| 145 | if(fits_get_num_cols(FitsPtr,&NBcol,&sta)) printerror(sta);
|
---|
| 146 | if(DbgLevel) cout<<"...Init NBcol="<<NBcol<<endl;
|
---|
| 147 | if(NBcol<1) {Delete(); return;}
|
---|
| 148 | mRet = new double[NBcol];
|
---|
| 149 | ColReadable = new bool[NBcol];
|
---|
| 150 | Buffer = new double*[NBcol];
|
---|
[1511] | 151 | int icol;
|
---|
| 152 | for(icol=0;icol<NBcol;icol++)
|
---|
[1505] | 153 | {Buffer[icol]=NULL; ColReadable[icol]=false;}
|
---|
| 154 |
|
---|
| 155 | // Get number of rows
|
---|
| 156 | if(fits_get_num_rows(FitsPtr,&NBline,&sta)) printerror(sta);
|
---|
| 157 | if(DbgLevel) cout<<"...Init NBline="<<NBline<<endl;
|
---|
| 158 | if(NBline<1) {Delete(); return;}
|
---|
| 159 |
|
---|
| 160 | // Get column infos
|
---|
[1511] | 161 | for(icol=0;icol<NBcol;icol++) {
|
---|
[1505] | 162 | int colnum,typecode;
|
---|
| 163 | char templt[16], colname[256];
|
---|
| 164 | sprintf(templt,"%d",icol+1);
|
---|
| 165 | if(fits_get_colname(FitsPtr,CASESEN,templt,colname,&colnum,&sta))
|
---|
| 166 | printerror(sta);
|
---|
| 167 | ColName.push_back(colname);
|
---|
| 168 | if(fits_get_coltype(FitsPtr,icol+1,&typecode,NULL,NULL,&sta))
|
---|
| 169 | printerror(sta);
|
---|
| 170 | if(typecode!=TSTRING && typecode!=TCOMPLEX && typecode!=TDBLCOMPLEX)
|
---|
| 171 | ColReadable[icol] = true;
|
---|
| 172 | if(DbgLevel)
|
---|
| 173 | cout<<"...Init col="<<icol+1
|
---|
| 174 | <<" name="<<ColName[icol]
|
---|
| 175 | <<" typecode="<<typecode<<" (readable="
|
---|
| 176 | <<ColReadable[icol]<<")"<<endl;
|
---|
| 177 | }
|
---|
[1507] | 178 | // Prepare buffer
|
---|
| 179 | AllocBuff();
|
---|
[1505] | 180 |
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /////////////////////////////////////////////////
|
---|
| 184 | uint_4 FitsBTNtuIntf::NbLines() const
|
---|
| 185 | {
|
---|
| 186 | return NBline;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | uint_4 FitsBTNtuIntf::NbColumns() const
|
---|
| 190 | {
|
---|
| 191 | return NBcol;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | r_8 * FitsBTNtuIntf::GetLineD(int n) const
|
---|
| 195 | // Attention: n [0,NBline[, cfistio veut [1,NBline]
|
---|
| 196 | {
|
---|
| 197 | int sta=0,anynul;
|
---|
| 198 | if(n<0 || n>=NBline) return NULL;
|
---|
| 199 |
|
---|
| 200 | // Pas de bufferisation, on lit betement
|
---|
| 201 | if(BuffLen==1) {
|
---|
| 202 | NFitsRead++;
|
---|
| 203 | for(int icol=0;icol<NBcol;icol++) {
|
---|
| 204 | mRet[icol] = 0.;
|
---|
| 205 | if(!ColReadable[icol]) continue;
|
---|
| 206 | fits_read_col_dbl(FitsPtr,icol+1,n+1,1,1,NulVal,&mRet[icol],&anynul,&sta);
|
---|
| 207 | if(sta) printerror(sta);
|
---|
| 208 | }
|
---|
| 209 | return mRet;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | // Gestion avec bufferisation
|
---|
[1507] | 213 | if(!Buffer) {
|
---|
| 214 | cout<<"FitsBTNtuIntf::GetLineD Buffer not allocated"<<endl;
|
---|
| 215 | return NULL;
|
---|
| 216 | }
|
---|
[1505] | 217 | if(n<LineDeb || n>LineFin) {
|
---|
| 218 | NFitsRead++;
|
---|
[1507] | 219 | long row1,row2,nrow;
|
---|
| 220 | if(BuffSens>0) { // Cas remplissage forward
|
---|
| 221 | row1 = n+1;
|
---|
| 222 | row2 = row1+BuffLen-1; if(row2>NBline) row2 = NBline;
|
---|
[1646] | 223 | } else if(BuffSens<0) { // Cas remplissage backward
|
---|
[1507] | 224 | row2 = n+1;
|
---|
| 225 | row1 = row2-BuffLen+1; if(row1<1) row1 = 1;
|
---|
| 226 | } else { // Cas remplissage centre
|
---|
| 227 | row1 = n+1 - BuffLen/2; if(row1<1) row1 = 1;
|
---|
| 228 | row2 = n+1 + BuffLen/2; if(row2>NBline) row2 = NBline;
|
---|
| 229 | }
|
---|
| 230 | nrow = row2 - row1 + 1;
|
---|
| 231 | LineDeb = row1-1; LineFin = row2-1;
|
---|
| 232 | //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow
|
---|
| 233 | // <<" LineDeb,Fin="<<LineDeb<<","<<LineFin<<endl;
|
---|
[1505] | 234 | for(int icol=0;icol<NBcol;icol++) {
|
---|
| 235 | if(!ColReadable[icol]) continue;
|
---|
| 236 | fits_read_col_dbl(FitsPtr,icol+1,row1,1,nrow,NulVal,Buffer[icol],&anynul,&sta);
|
---|
| 237 | if(sta) printerror(sta);
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[1507] | 241 | int_4 ibuf = n-LineDeb;
|
---|
[1505] | 242 | for(int icol=0;icol<NBcol;icol++)
|
---|
[1507] | 243 | if(ColReadable[icol]) mRet[icol] = (Buffer[icol])[ibuf];
|
---|
[1505] | 244 | else mRet[icol] = 0.;
|
---|
| 245 |
|
---|
| 246 | return mRet;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | r_8 FitsBTNtuIntf::GetCell(int n, int k) const
|
---|
| 250 | {
|
---|
| 251 | if(n<0 || n>=NBline || k<0 || k>=NBcol) return 0.;
|
---|
| 252 | r_8 * r = GetLineD(n);
|
---|
| 253 | return r[k];
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /*!
|
---|
| 257 | Retourne une chaine de caracteres avec la declaration des noms de
|
---|
| 258 | variables. si "nomx!=NULL" , des instructions d'affectation
|
---|
| 259 | a partir d'un tableau "nomx[i]" sont ajoutees (pour interface NTuple).
|
---|
| 260 | */
|
---|
| 261 | string FitsBTNtuIntf::VarList_C(const char* nomx) const
|
---|
| 262 | {
|
---|
| 263 | string rets;
|
---|
| 264 | rets = "\ndouble";
|
---|
[1511] | 265 | int icol;
|
---|
| 266 | for(icol=0;icol<NBcol;icol++) {
|
---|
[1505] | 267 | rets += " " + ColName[icol];
|
---|
| 268 | if(icol!=NBcol-1) rets += ","; else rets += ";\n";
|
---|
| 269 | }
|
---|
| 270 | if(!nomx) return rets;
|
---|
| 271 | char buff[256];
|
---|
[1511] | 272 | for(icol=0;icol<NBcol;icol++) {
|
---|
[1505] | 273 | sprintf(buff," = %s[%d];\n",nomx,icol);
|
---|
| 274 | rets += ColName[icol] + buff;
|
---|
| 275 | }
|
---|
| 276 | return rets;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /////////////////////////////////////////////////
|
---|
| 280 | void FitsBTNtuIntf::printerror(int sta) const
|
---|
| 281 | {
|
---|
| 282 | int stat = sta;
|
---|
| 283 | fits_report_error(stdout,stat);
|
---|
| 284 | return;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | void FitsBTNtuIntf::Print(ostream& os,int lp) const
|
---|
| 288 | {
|
---|
| 289 | os<<"FitsBTNtuIntf:Print ("<<BuffLen<<","<<NulVal<<")"
|
---|
| 290 | <<" ncols="<<NBcol<<" nrows="<<NBline;
|
---|
| 291 | if(lp>0) os<<" fitsrd="<<NFitsRead<<"*ncols";
|
---|
| 292 | os<<"\n"<<FitsFN<<"["<<IHdu<<"]"<<endl;
|
---|
| 293 | if(NBcol<=0 || lp<1) return;
|
---|
| 294 | for(int icol=0;icol<NBcol;icol++) {
|
---|
| 295 | os<<" "<<ColName[icol]<<"("<<ColReadable[icol]<<")";
|
---|
| 296 | if(icol%4==3 || icol==NBcol-1) os<<endl;
|
---|
| 297 | }
|
---|
| 298 | }
|
---|