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