Changeset 3660 in Sophya
- Timestamp:
- Oct 19, 2009, 5:36:13 PM (16 years ago)
- Location:
- trunk/SophyaExt/FitsIOServer
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaExt/FitsIOServer/fabtcolread.cc
r3572 r3660 431 431 if(fits_get_colnum(GetFitsPtr(),CASESEN,labelcol,&ColNum,&sta)) { 432 432 FitsOpenFile::printerror(sta); 433 throw NotAvailableOperation("FitsABTColRd::Init: Error getting column n ame\n");433 throw NotAvailableOperation("FitsABTColRd::Init: Error getting column number\n"); 434 434 } 435 435 ColNum--; // Convention [0,ncol[ … … 974 974 /////////////////////////////////////////////////////////////////// 975 975 976 FitsABTColRd1F::FitsABTColRd1F(FitsOpenFile* fof,int ihdu,long blen,long bsens,int lp) 977 { 978 Init(fof,ihdu,blen,bsens,lp); 979 } 980 981 /*! Init routine called by the constructor */ 982 void FitsABTColRd1F::Init(FitsOpenFile* fof,int ihdu,long blen,long bsens,int lp) 983 { 984 // Initialisation des Parametres Generaux 985 ColLabel.resize(0); ColTUnit.resize(0); ColTForm.resize(0); ColTypeCode.resize(0); 986 NBcol = 0; NBline = 0; 987 SetNulVal(); SetDebug(lp); 988 FitsOF = NULL; 989 LineDeb.resize(0); LineFin.resize(0); 990 Buffer = NULL; 991 992 // Caracteristiques du FitsOpenFile 993 FitsOF = fof; 994 if(FitsOF==NULL) 995 throw NullPtrError("FitsABTColRd1F::Init: FitsOpenFile pointer is NULL\n"); 996 997 if(GetFitsPtr()==NULL) 998 throw NullPtrError("FitsABTColRd1F::Init: FitsPtr pointer is NULL\n"); 999 1000 int sta = 0; 1001 if(ihdu<0) ihdu=0; if(ihdu>NHDU()) ihdu=NHDU(); 1002 1003 // Get HDU for bin/ascii table 1004 // ATTENTION: le fichier est ouvert mais non positionne sur un HDU, 1005 // une classe utilisant ce fichier doit le positionner sur un HDU. 1006 // Par contre, si une autre classe utilise ce meme FitsOpenFile, 1007 // elle ne peut le positionner que sur ce meme HDU ! 1008 if(FitsOF->GetPosStatus()==false) { 1009 if(ihdu==0) { // find the first BINARY then the first ASCII 1010 int rc = FitsOF->MoveToFirst(BINARY_TBL); 1011 if(rc!=BINARY_TBL) FitsOF->MoveToFirst(ASCII_TBL); 1012 } else { 1013 int rc = FitsOF->MoveToHDU(ihdu); 1014 if(rc!=ihdu) 1015 throw RangeCheckError("FitsABTColRd1F::Init: Error moving to requested HDU\n"); 1016 } 1017 } else { // Fits file has already been positionned 1018 if(ihdu>0 && ihdu!=HDU()) 1019 throw RangeCheckError("FitsABTColRd1F::Init: file already posit. at another HDU\n"); 1020 } 1021 1022 // Check HDUType and set position status to TRUE 1023 if(HDUType()!=BINARY_TBL && HDUType()!=ASCII_TBL) 1024 throw TypeMismatchExc("FitsABTColRd1F::Init: HDU not ASCII/BINARY table\n"); 1025 if(DbgLevel>1) cout<<"...Init ihdu="<<ihdu<<" HduType="<<HDUType()<<endl; 1026 FitsOF->SetPosStatus(true); 1027 1028 // Get number of columns 1029 if(fits_get_num_cols(GetFitsPtr(),&NBcol,&sta)) { 1030 FitsOpenFile::printerror(sta); 1031 throw NotAvailableOperation("FitsABTColRd1F::Init: Error getting number of columns\n"); 1032 } 1033 if(DbgLevel>1) cout<<"...Init NBcol="<<NBcol<<endl; 1034 if(NBcol<1) 1035 throw RangeCheckError("FitsABTColRd1F::Init: Bad number of colums\n"); 1036 1037 // Get number of rows 1038 if(fits_get_num_rowsll(GetFitsPtr(),&NBline,&sta)) { 1039 FitsOpenFile::printerror(sta); 1040 throw NotAvailableOperation("FitsABTColRd1F::Init: Error getting number of rows\n"); 1041 } 1042 if(DbgLevel>1) cout<<"...Init NBline="<<NBline<<endl; 1043 if(NBline<1) 1044 throw RangeCheckError("FitsABTColRd1F::Init: Bad number of rows\n"); 1045 1046 // --- Boucle sur les colonnes 1047 for(int ColNum=0;ColNum<NBcol;ColNum++) { // ***** ColNum 1048 // Get column type 1049 ColTypeCode.push_back(-999); 1050 if(fits_get_coltypell(GetFitsPtr(),ColNum+1,&ColTypeCode[ColNum],NULL,NULL,&sta)) { 1051 FitsOpenFile::printerror(sta); 1052 throw ParmError("FitsABTColRd1F::Init: Error getting column type\n"); 1053 } 1054 if(DbgLevel>1) cout<<"...Init ColTypeCode="<<ColTypeCode[ColNum]<<endl; 1055 if(ColTypeCode[ColNum]==TSTRING || ColTypeCode[ColNum]==TCOMPLEX 1056 || ColTypeCode[ColNum]==TDBLCOMPLEX || ColTypeCode[ColNum]<0 ) 1057 throw ParmError("FitsABTColRd1F::Init: Selected column is not Numerical\n"); 1058 // Get column name, tunit, tform 1059 char labelcol[128]; 1060 char tunit[64], tform[64], tdisp[64]; 1061 LONGLONG repeat=0; double tscale=1., tzero=0.; 1062 int rc=0; 1063 if(HDUType()==BINARY_TBL) { 1064 fits_get_bcolparmsll(GetFitsPtr(),ColNum+1,labelcol,tunit,tform 1065 ,&repeat,&tscale,&tzero,NULL,tdisp,&sta); 1066 } else { 1067 long repeatlng; 1068 fits_get_acolparms(GetFitsPtr(),ColNum+1,labelcol,&repeatlng,tunit,tform 1069 ,&tscale,&tzero,NULL,tdisp,&sta); 1070 repeat = repeatlng; 1071 } 1072 if(rc) { 1073 FitsOpenFile::printerror(sta); 1074 throw RangeCheckError("FitsABTColRd1F::Init: Error getting the column caracteristics\n"); 1075 } 1076 ColLabel.push_back(labelcol); 1077 ColTUnit.push_back(tunit); 1078 ColTForm.push_back(tform); 1079 // fill the default buffer limits at init 1080 LineDeb.push_back(-1); 1081 LineFin.push_back(-1); 1082 // some debug print if requested 1083 if(DbgLevel) 1084 cout<<"FitsABTColRd1F::Init Num="<<ColNum<<" Label="<<ColLabel[ColNum] 1085 <<" TypeCode="<<ColTypeCode[ColNum]<<" TUnit="<<ColTUnit[ColNum]<<" TForm="<<ColTForm[ColNum]<<endl; 1086 if(DbgLevel>1) 1087 cout<<" (repeat="<<repeat<<",tscale="<<tscale<<",tzero="<<tzero 1088 <<",tdisp="<<tdisp<<")"<<endl; 1089 } // ***** ColNum 1090 1091 // Set the buffer for reading 1092 ChangeBuffer(blen,bsens); 1093 1094 } 1095 1096 /*! Destructor. */ 1097 FitsABTColRd1F::~FitsABTColRd1F() 1098 { 1099 Delete(); 1100 } 1101 1102 /*! Delete called by the destructor */ 1103 void FitsABTColRd1F::Delete(void) 1104 { 1105 if(NBcol>0 && Buffer!=NULL) { 1106 for(int ColNum=0; ColNum<NBcol; ColNum++) 1107 if(Buffer[ColNum]!=NULL) delete [] Buffer[ColNum]; 1108 delete [] Buffer; Buffer=NULL; 1109 } 1110 LineDeb.resize(0); LineFin.resize(0); 1111 //--- Surtout on ne "fits_close_file" pas le fichier FITS !!! 1112 } 1113 1114 ////////////////////////////////////////////////////////////// 1115 /*! Change the buffer caracteristiques (see creator) */ 1116 void FitsABTColRd1F::ChangeBuffer(long blen,long bsens) 1117 { 1118 long oldnbuffer = NBuffer; 1119 1120 // Compute buffer caracteristics 1121 BuffLen = (blen<=0)? 1: blen; 1122 BuffSens = bsens; 1123 NBuffer = BuffLen; 1124 if(bsens==0 && NBuffer%2==0) NBuffer++; 1125 1126 // De-allocate if necessary 1127 if(Buffer!=NULL) { 1128 // On des-alloue si pas assez de place 1129 // ou si l'ancienne place est beaucoup trop grande (>25%) 1130 if(oldnbuffer<NBuffer || (oldnbuffer>NBuffer+long(0.25*NBuffer)) ) { 1131 for(int ColNum=0; ColNum<NBcol; ColNum++) if(Buffer[ColNum]!=NULL) delete [] Buffer[ColNum]; 1132 if(Buffer!=NULL) {delete [] Buffer; Buffer=NULL;} 1133 } 1134 } 1135 1136 // Re-allocate 1137 if(Buffer==NULL) { 1138 Buffer = new double*[NBcol]; 1139 for(int ColNum=0; ColNum<NBcol; ColNum++) Buffer[ColNum] = new double[NBuffer]; 1140 } 1141 1142 // Tell program that nothing is into buffer 1143 for(int ColNum=0; ColNum<NBcol; ColNum++) LineDeb[ColNum] = LineFin[ColNum] = -1; 1144 } 1145 1146 ////////////////////////////////////////////////////////////// 1147 /*! 1148 Read a fitsheader key into double 1149 \param keyname : name of the key 1150 \return value into double 1151 */ 1152 double FitsABTColRd1F::ReadKey(const char *keyname) 1153 { 1154 return FitsOpenFile::ReadKey(GetFitsPtr(),keyname); 1155 } 1156 1157 /*! 1158 Read a fitsheader key into long 1159 \param keyname : name of the key 1160 \return value into long 1161 */ 1162 long FitsABTColRd1F::ReadKeyL(const char *keyname) 1163 { 1164 return FitsOpenFile::ReadKeyL(GetFitsPtr(),keyname); 1165 } 1166 1167 /*! 1168 Read a fitsheader key into long long 1169 \param keyname : name of the key 1170 \return value into long long 1171 */ 1172 LONGLONG FitsABTColRd1F::ReadKeyLL(const char *keyname) 1173 { 1174 return FitsOpenFile::ReadKeyLL(GetFitsPtr(),keyname); 1175 } 1176 1177 /*! 1178 Read a fitsheader key into string 1179 \param keyname : name of the key 1180 \return value into string 1181 */ 1182 string FitsABTColRd1F::ReadKeyS(const char *keyname) 1183 { 1184 return FitsOpenFile::ReadKeyS(GetFitsPtr(),keyname); 1185 } 1186 1187 ///////////////////////////////////////////////// 1188 int FitsABTColRd1F::GetColNum(const char *colname) 1189 // Get column number from column name 1190 { 1191 if(strlen(colname)<1 || NBcol<1) return -1; 1192 string slab(colname); 1193 for(int ColNum=0;ColNum<NBcol;ColNum++) 1194 if(slab == ColLabel[ColNum]) return ColNum; 1195 return -1; 1196 } 1197 1198 ///////////////////////////////////////////////// 1199 /*! 1200 Read row "n" of column "ColNum" and return the value into a double 1201 \warning be carefull for the range: row = [0,NRows[ 1202 \return value in double 1203 \param n : number of the row to be read. 1204 \verbatim 1205 usebuffer == true : use read optimisation with bufferisation 1206 == false : no optimisation with bufferisation 1207 just read one value 1208 \endverbatim 1209 */ 1210 double FitsABTColRd1F::Read(int ColNum,LONGLONG n,bool usebuffer) 1211 // Attention: n=nline [0,NBline[, cfistio veut [1,NBline] 1212 // Attention: colnum [0,NBcol[ , cfistio veut [1,NBcol] 1213 { 1214 int sta=0; 1215 if(ColNum<0 || ColNum>=NBcol) 1216 throw RangeCheckError("FitsABTColRd1F::Read try to read outside column range\n"); 1217 if(n<0 || n>=NBline) 1218 throw RangeCheckError("FitsABTColRd1F::Read try to read outside line range\n"); 1219 1220 // Pas de bufferisation, on lit betement 1221 if(NBuffer==1 || !usebuffer) { 1222 double val; 1223 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,n+1,1,1,NULL,&val,NULL,&sta); 1224 if(sta) { 1225 FitsOpenFile::printerror(sta); 1226 throw NotAvailableOperation("FitsABTColRd1F::Read: Error Reading Fits file\n"); 1227 } 1228 // On ne remplit Buffer[ColNum][0] que si on a choisit 1229 // un mode de lecture non bufferise (n==1) DES LE DEBUT. 1230 // Si on a initialement choisit un mode bufferise (avec n>1), 1231 // Buffer contient les valeurs chargees auparavent. 1232 // Il ne faut pas faire {Buffer[0]=val; LineDeb=LineFin=n;} 1233 // car on perd l'info de ces valeurs. 1234 if(NBuffer==1) {Buffer[ColNum][0]=val; LineDeb[ColNum]=LineFin[ColNum]=n;} 1235 return val; 1236 } 1237 1238 // Gestion avec bufferisation 1239 if(!Buffer) 1240 throw RangeCheckError("FitsABTColRd1F::Read: Buffer not allocated\n"); 1241 if(n<LineDeb[ColNum] || n>LineFin[ColNum]) { 1242 LONGLONG row1,row2,nrow; 1243 if(BuffSens>0) { // Cas remplissage forward 1244 row1 = n+1; 1245 row2 = row1+NBuffer-1; if(row2>NBline) row2 = NBline; 1246 } else if(BuffSens<0) { // Cas remplissage backward 1247 row2 = n+1; 1248 row1 = row2-NBuffer+1; if(row1<1) row1 = 1; 1249 } else { // Cas remplissage centre 1250 row1 = n+1 - NBuffer/2; if(row1<1) row1 = 1; 1251 row2 = n+1 + NBuffer/2; if(row2>NBline) row2 = NBline; 1252 } 1253 nrow = row2 - row1 + 1; 1254 LineDeb[ColNum] = row1-1; LineFin[ColNum] = row2-1; 1255 //cout<<"DBG-FitsRead: row1="<<row1<<" row2="<<row2<<" nrow="<<nrow 1256 // <<" LineDeb,Fin="<<LineDeb[ColNum]<<","<<LineFin[ColNum]<<endl; 1257 fits_read_col(GetFitsPtr(),TDOUBLE,ColNum+1,row1,1,nrow,NULL,Buffer[ColNum],NULL,&sta); 1258 if(sta) { 1259 FitsOpenFile::printerror(sta); 1260 LineDeb[ColNum] = LineFin[ColNum] = -1; 1261 throw NotAvailableOperation("FitsABTColRd1F::Read: Error Reading Fits file\n"); 1262 } 1263 } 1264 1265 long ibuf = n-LineDeb[ColNum]; 1266 return Buffer[ColNum][ibuf]; 1267 } 1268 1269 1270 /*! Print on stream os */ 1271 void FitsABTColRd1F::Print(ostream& os,int lp) const 1272 { 1273 os<<"FitsABTColRd1F:Print ("<<BuffLen<<","<<BuffSens<<","<<NulVal<<")" 1274 <<" ncols="<<NBcol<<" nrows="<<NBline; 1275 os<<"\n... "<<FileName()<<"["<<HDU()<<"/"<<NHDU()<<" type="<<HDUType()<<"]"<<endl; 1276 if(lp>0 && NBcol>0) { 1277 for(int ColNum=0;ColNum<NBcol;ColNum++) { 1278 os<<"..Col="<<ColNum<<" Label="<<ColLabel[ColNum]<<" TypeCode="<<ColTypeCode[ColNum] 1279 <<" TUnit="<<ColTUnit[ColNum]<<" TForm="<<ColTForm[ColNum] 1280 <<endl; 1281 } 1282 } 1283 } 1284 1285 /////////////////////////////////////////////////////////////////// 1286 /////////////////////////////////////////////////////////////////// 1287 /////////////////////////////////////////////////////////////////// 1288 /////////////////////////////////////////////////////////////////// 1289 1290 /*! 1291 Constructor. 1292 Same as before but the column is identified by its column number 1293 \param colnum : number of the column to be read 1294 \warning col = [0,ncol[ 1295 */ 1296 FitsABTColRead1F::FitsABTColRead1F(string fname,int ihdu,long blen,long bsens,int lp) 1297 : FitsABTColRd1F(new FitsOpenFile(fname),ihdu,blen,bsens,lp) 1298 { 1299 } 1300 1301 /*! Constructor. see below */ 1302 FitsABTColRead1F::FitsABTColRead1F(const char * cfname,int ihdu,long blen,long bsens,int lp) 1303 : FitsABTColRd1F(new FitsOpenFile(cfname),ihdu,blen,bsens,lp) 1304 { 1305 } 1306 1307 /*! Destructor. */ 1308 FitsABTColRead1F::~FitsABTColRead1F() 1309 { 1310 Delete(); // ?? inutile ?? 1311 // On detruit le FitsOpenFile, cad qu'on ferme (fits_file_close) le fichier FITS 1312 if(FitsOF!=NULL) delete FitsOF; 1313 } 1314 1315 /////////////////////////////////////////////////////////////////// 1316 /////////////////////////////////////////////////////////////////// 1317 /////////////////////////////////////////////////////////////////// 1318 /////////////////////////////////////////////////////////////////// 1319 976 1320 //! Class for reading a 2D image from a FITS file 977 1321 -
trunk/SophyaExt/FitsIOServer/fabtcolread.h
r3572 r3660 187 187 188 188 /////////////////////////////////////////////////////////////////// 189 //! Class for reading ALL the columns in a FITS ASCII or BINARY table 190 class FitsABTColRd1F : public AnyDataObj { 191 public: 192 FitsABTColRd1F(FitsOpenFile* fof,int ihdu=0 193 ,long blen=100,long bsens=1,int lp=0); 194 virtual ~FitsABTColRd1F(); 195 196 void ChangeBuffer(long blen=100,long bsens=1); 197 198 double ReadKey(const char *keyname); 199 long ReadKeyL(const char *keyname); 200 LONGLONG ReadKeyLL(const char *keyname); 201 string ReadKeyS(const char *keyname); 202 203 double Read(int ColNum,LONGLONG n,bool usebuffer=true); 204 int GetColNum(const char *colname); 205 206 //! Set debug level 207 inline void SetDebug(int lp=0) {DbgLevel = (unsigned short) lp;} 208 //! Set null value to be return when reading null data (0=return the data) 209 inline void SetNulVal(double nulval=0.) {NulVal = nulval;} 210 //! Get the FITS file name 211 inline string FileName(void) const 212 {if(FitsOF) return FitsOF->FileName(); else return (string)"";} 213 //! Get the pointer to FitsOpenFile 214 inline FitsOpenFile* GetFitsOpenFile(void) const {return FitsOF;} 215 //! Get the FITS file pointer (cfistio pointer) 216 inline fitsfile* GetFitsPtr(void) const {return FitsOF->GetFitsPtr();} 217 //! Get the number of HDU in the FITS file 218 inline int NHDU(void) const 219 {if(FitsOF) return FitsOF->NHDU(); else return 0;} 220 //! Get the number of the HDU read 221 inline int HDU(void) const 222 {if(FitsOF) return FitsOF->HDU(); else return 0;} 223 //! Get the HDU type 224 inline int HDUType(void) const 225 {if(FitsOF) return FitsOF->HDUType(); else return 0;} 226 //! Get the number of rows in the FITS HDU to be read 227 inline LONGLONG GetNbLine(void) const {return NBline;} 228 //! Get the number of columns in the FITS HDU to be read 229 inline int GetNbCol(void) const {return NBcol;} 230 //! Get the columns label that is read 231 inline string GetColLabel(int ColNum) const 232 {if(ColNum<0 || ColNum>=NBcol) return string(""); else return ColLabel[ColNum];} 233 //! Get the columns type code that is read 234 inline int GetColTypeCode(int ColNum) const 235 {if(ColNum<0 || ColNum>=NBcol) return -999; else return ColTypeCode[ColNum];} 236 //! Get the columns fits tunit that is read 237 inline string GetColTUnit(int ColNum) const 238 {if(ColNum<0 || ColNum>=NBcol) return string(""); else return ColTUnit[ColNum];} 239 //! Get the columns fits tform that is read 240 inline string GetColTForm(int ColNum) const 241 {if(ColNum<0 || ColNum>=NBcol) return string(""); else return ColTForm[ColNum];} 242 //! Get the read requested buffer length 243 inline long GetBLen(void) const {return BuffLen;} 244 //! Get the read buffer direction 245 inline long GetBSens(void) const {return BuffSens;} 246 //! Print to os 247 virtual void Print(ostream& os,int lp=1) const; 248 //! Print to stdout 249 inline void Print(int lp=1) const {Print(cout,lp);} 250 //! Get the read effective buffer length 251 inline long GetNBuffer(void) const {return NBuffer;} 252 //! Get the read bufferpointer 253 inline double* GetBuffer(int ColNum) 254 {if(ColNum<0 || ColNum>=NBcol) return NULL; else return Buffer[ColNum];} 255 256 protected: 257 void Init(FitsOpenFile* fof,int ihdu,long blen,long bsens,int lp); 258 void Delete(void); 259 260 vector<string> ColLabel,ColTUnit,ColTForm; 261 vector<int> ColTypeCode; 262 int NBcol; 263 LONGLONG NBline; 264 265 double NulVal; 266 unsigned short DbgLevel; 267 long BuffLen, BuffSens; 268 269 FitsOpenFile* FitsOF; 270 vector<LONGLONG> LineDeb, LineFin; 271 double **Buffer; 272 long NBuffer; 273 }; 274 275 276 /////////////////////////////////////////////////////////////////// 277 //! Class for reading ALL the columns in a FITS ASCII or BINARY table with fits file opening 278 class FitsABTColRead1F : public FitsABTColRd1F { 279 public: 280 FitsABTColRead1F(string fname,int ihdu=0 281 ,long blen=100,long bsens=1,int lp=0); 282 FitsABTColRead1F(const char *cfname,int ihdu=0 283 ,long blen=100,long bsens=1,int lp=0); 284 virtual ~FitsABTColRead1F(); 285 }; 286 287 /////////////////////////////////////////////////////////////////// 189 288 //! Class for reading a 2D image from a FITS file 190 289 class FitsImg2DRd : public AnyDataObj { -
trunk/SophyaExt/FitsIOServer/fabtwriter.cc
r3572 r3660 388 388 FitsABTWriter::~FitsABTWriter() 389 389 { 390 // On cree la table avant de fermer le fichier si pas deja fait 391 if(FirstTime) createtbl(); 390 392 } 391 393
Note:
See TracChangeset
for help on using the changeset viewer.