- Timestamp:
- Dec 22, 1999, 5:57:10 PM (26 years ago)
- Location:
- trunk/SophyaExt/FitsIOServer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaExt/FitsIOServer/fitsioserver.cc
r687 r689 12 12 #include "machdefs.h" 13 13 #include <iostream.h> 14 15 #ifdef __USE_STD_IOSTREAM 16 #include <sstream> 17 #endif 18 14 19 #include <list> 15 20 #include <string> … … 19 24 #include "strutil.h" 20 25 26 AnyDataObj* FitsIoServer::loadobj(char flnm[],int hdunum) 27 { 28 // pointer to the FITS file, defined in fitsio.h 29 fitsfile *fptr; 30 31 // initialize status before calling fitsio routines 32 int status= 0; 33 fits_open_file(&fptr,flnm,READONLY,&status); 34 if( status ) printerror( status ); 35 36 // move to the specified HDU number 37 int hdutype; 38 fits_movabs_hdu(fptr,hdunum,&hdutype,&status); 39 if( status ) printerror( status ); 40 41 if(hdutype == BINARY_TBL) 42 { 43 cout << " Reading a FITS binary table in HDU : " << hdunum << endl; 44 45 // get number of keywords 46 int nkeys = 0; 47 int keypos= 0; 48 fits_get_hdrpos(fptr,&nkeys,&keypos,&status); 49 if( status ) printerror( status ); 50 51 // get number of fields (columns) in the table 52 int tfields= 0; 53 fits_get_num_cols(fptr,&tfields,&status); 54 if( status ) printerror( status ); 55 56 // only a table with ONE column is allowed 57 if(tfields != 1) 58 throw IOExc("FitsIOServer::dataobj ERROR more than one column !"); 59 60 // get the datatype of the values 61 int DTYPE; 62 long repeat,width; 63 fits_get_coltype(fptr,1,&DTYPE,&repeat,&width,&status); 64 if( status ) printerror( status ); 65 66 // check if the keyword ORDERING exists 67 bool ordering= check_keyword(fptr,nkeys,"ORDERING"); 68 69 // check if the keyword NSIDE exists 70 int nside= 2; 71 if(check_keyword(fptr,nkeys,"NSIDE")) 72 { 73 fits_read_key(fptr,TINT,"NSIDE",&nside,NULL,&status); 74 if( status ) printerror( status ); 75 } 76 77 // close the file 78 fits_close_file(fptr,&status); 79 if( status ) printerror( status ); 80 81 if(ordering) 82 { 83 if(DTYPE == TDOUBLE) 84 { 85 SphereGorski<double> *sph= new SphereGorski<double>(nside); 86 load(*sph,flnm,hdunum); 87 return sph; 88 } 89 90 else if(DTYPE == TFLOAT) 91 { 92 SphereGorski<float> *sph= new SphereGorski<float>(nside); 93 load(*sph,flnm,hdunum); 94 return sph; 95 } 96 97 else 98 { 99 cout << " FitsIOServer::dataobj:: DTYPE= " << DTYPE << endl; 100 throw IOExc("datatype code not yet programmed"); 101 } 102 } 103 else 104 { 105 NTuple *ntpl= new NTuple; 106 load(*ntpl,flnm,hdunum); 107 return ntpl; 108 } 109 } 110 else if(hdutype == IMAGE_HDU) 111 { 112 cout << " Reading a FITS image in HDU : " << hdunum << endl; 113 114 // bits per pixels 115 int bitpix; 116 fits_read_key(fptr,TINT,"BITPIX",&bitpix,NULL,&status); 117 if( status ) printerror( status ); 118 119 // number of dimensions in the FITS array 120 int naxis= 0; 121 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status); 122 if( status ) printerror( status ); 123 124 // read the NAXIS1 and NAXIS2 keyword to get image size 125 long naxes[3]= {0,0,0}; 126 int nfound; 127 fits_read_keys_lng(fptr,"NAXIS",1,naxis,naxes,&nfound,&status); 128 if( status ) printerror( status ); 129 130 int nrows= (int)naxes[0]; 131 int ncols= 0; 132 133 if(naxis == 1) ncols= 1; 134 if(naxis == 2) ncols= (int)naxes[1]; 135 if(naxis == 3 && naxes[2] < 2) 136 { 137 naxis= 2; 138 ncols= (int)naxes[1]; 139 } 140 141 // close the file 142 fits_close_file(fptr,&status); 143 if( status ) printerror( status ); 144 145 if(bitpix == DOUBLE_IMG) 146 { 147 TMatrix<double> *mat=new TMatrix<double>(nrows,ncols); 148 load(*mat,flnm); 149 if(naxis == 1) 150 { 151 TVector<double> *vect=new TVector<double>(*mat); 152 delete mat; 153 return vect; 154 } 155 else if(naxis == 2) 156 return mat; 157 } 158 else if(bitpix == FLOAT_IMG) 159 { 160 TMatrix<float> *mat=new TMatrix<float>(nrows,ncols); 161 load(*mat,flnm); 162 if(naxis == 1) 163 { 164 TVector<float> *vect=new TVector<float>(*mat); 165 delete mat; 166 return vect; 167 } 168 else if(naxis == 2) 169 return mat; 170 } 171 else if(bitpix == LONG_IMG) 172 { 173 TMatrix<int> *mat=new TMatrix<int>(nrows,ncols); 174 load(*mat,flnm); 175 if(naxis == 1) 176 { 177 TVector<int> *vect=new TVector<int>(*mat); 178 delete mat; 179 return vect; 180 } 181 else if(naxis == 2) 182 return mat; 183 } 184 } 185 else 186 throw IOExc("Error:: this HDU is not a FITS image or binary table"); 187 188 return NULL; 189 } 190 21 191 void FitsIoServer::load(TMatrix<double>& mat,char flnm[]) 22 192 { 23 int nbrows= 0;24 int nbcols= 0;25 FITS_tab_typ_ 26 longnaxis;27 int n1, n2,n3;193 int nbrows= 0; 194 int nbcols= 0; 195 FITS_tab_typ_= TDOUBLE; 196 int naxis; 197 int n1,n2,n3; 28 198 DVList dvl; 29 planck_read_img(flnm, naxis, n1, n2, n3, dvl); 30 31 nbrows=n1; 32 nbcols=n2; 33 if (naxis == 1) nbcols=1; 34 if (naxis > 2 && n3>1) 35 { 36 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl; 199 planck_read_img(flnm,naxis,n1,n2,n3,dvl); 200 201 nbrows= n1; 202 nbcols= n2; 203 if(naxis == 1) nbcols= 1; 204 if(naxis > 2 && n3 > 1) 205 { 206 cout << " naxis = " << naxis << endl; 207 throw IOExc("FitsIOServer::load() this Fits file is not a matrix"); 37 208 } 38 209 39 210 // number of components 40 if (mat.NRows() != nbrows || mat.NCols() != nbcols ) 41 { 42 // cout << " found " << nbrows << " rows "; 43 // cout << " expected " << mat.NRows() << endl; 44 // cout << " found " << nbcols << " columns " ; 45 // cout << " expected " << mat.NCols() << endl; 211 if(mat.NRows() != nbrows || mat.NCols() != nbcols ) 212 { 46 213 mat.ReSize(nbrows,nbcols); 47 cout << " resize the vector to nbrows= " << nbrows << " nbcols= " << nbcols << endl; 48 } 49 int ij=0; 50 for (int j=0; j< nbcols; j++) 51 for (int i = 0; i < nbrows; i++) mat(i,j) = (double)r_8tab_[ij++]; 214 cout<<" FitsIOServer::load resize the matrix"; 215 cout<<" nbrows= "<<nbrows<<" nbcols= "<<nbcols<<endl; 216 } 217 218 int ij= 0; 219 for(int j = 0; j < nbcols; j++) 220 for (int i = 0; i < nbrows; i++) mat(i,j)= (double)r_8tab_[ij++]; 52 221 } 53 222 54 223 void FitsIoServer::load(TMatrix<float>& mat,char flnm[]) 55 224 { 56 int nbrows= 0;57 int nbcols= 0;58 FITS_tab_typ_ 59 longnaxis;60 int n1, n2,n3;225 int nbrows= 0; 226 int nbcols= 0; 227 FITS_tab_typ_= TFLOAT; 228 int naxis; 229 int n1,n2,n3; 61 230 DVList dvl; 62 planck_read_img(flnm, naxis, n1, n2, n3, dvl); 63 64 nbrows=n1; 65 nbcols=n2; 66 if (naxis == 1) nbcols=1; 67 if (naxis > 2 && n3>1) 68 { 69 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl; 231 planck_read_img(flnm,naxis,n1,n2,n3,dvl); 232 233 nbrows= n1; 234 nbcols= n2; 235 if(naxis == 1) nbcols= 1; 236 if(naxis > 2 && n3 > 1) 237 { 238 cout << " naxis = " << naxis << endl; 239 throw IOExc("FitsIOServer::load() this Fits file is not a matrix"); 70 240 } 71 241 72 242 // number of components 73 if (mat.NRows() != nbrows || mat.NCols() != nbcols ) 74 { 75 // cout << " found " << nbrows << " rows "; 76 // cout << " expected " << mat.NRows() << endl; 77 // cout << " found " << nbcols << " columns " ; 78 // cout << " expected " << mat.NCols() << endl; 243 if(mat.NRows() != nbrows || mat.NCols() != nbcols ) 244 { 79 245 mat.ReSize(nbrows,nbcols); 80 cout << " resize the vector to nbrows= " << nbrows << " nbcols= " << nbcols << endl; 81 } 82 int ij=0; 83 for (int j=0; j< nbcols; j++) 84 for (int i = 0; i < nbrows; i++) mat(i,j) = (float)r_4tab_[ij++]; 246 cout<<" FitsIOServer::load resize the matrix"; 247 cout<<" nbrows= "<<nbrows<<" nbcols= "<<nbcols<<endl; 248 } 249 250 int ij= 0; 251 for(int j = 0; j < nbcols; j++) 252 for(int i = 0; i < nbrows; i++) mat(i,j)= (float)r_4tab_[ij++]; 85 253 } 86 254 87 255 void FitsIoServer::load(TMatrix<int_4>& mat,char flnm[]) 88 256 { 89 int nbrows= 0;90 int nbcols= 0;91 FITS_tab_typ_ 92 longnaxis;93 int n1, n2,n3;257 int nbrows= 0; 258 int nbcols= 0; 259 FITS_tab_typ_= TINT; 260 int naxis; 261 int n1,n2,n3; 94 262 DVList dvl; 95 planck_read_img(flnm, naxis, n1, n2, n3, dvl); 96 97 nbrows=n1; 98 nbcols=n2; 99 if (naxis == 1) nbcols=1; 100 if (naxis > 2 && n3>1) 101 { 102 cout<<" FitsIOServer : le fichier fits n'est pas une matrice, naxis= " <<naxis<< endl; 263 planck_read_img(flnm,naxis,n1,n2,n3,dvl); 264 265 nbrows= n1; 266 nbcols= n2; 267 if(naxis == 1) nbcols= 1; 268 if(naxis > 2 && n3 > 1) 269 { 270 cout << " naxis = " << naxis << endl; 271 throw IOExc("FitsIOServer::load() this Fits file is not a matrix"); 103 272 } 104 273 105 274 // number of components 106 if (mat.NRows() != nbrows || mat.NCols() != nbcols ) 107 { 108 // cout << " found " << nbrows << " rows "; 109 // cout << " expected " << mat.NRows() << endl; 110 // cout << " found " << nbcols << " columns " ; 111 // cout << " expected " << mat.NCols() << endl; 275 if(mat.NRows() != nbrows || mat.NCols() != nbcols ) 276 { 112 277 mat.ReSize(nbrows,nbcols); 113 cout << " resize the vector to nbrows= " << nbrows << " nbcols= " << nbcols << endl; 114 } 115 int ij=0; 116 for (int j=0; j< nbcols; j++) 117 for (int i = 0; i < nbrows; i++) mat(i,j) = (int_4)i_4tab_[ij++]; 118 } 119 120 void FitsIoServer::load(NTuple& ntpl,char flnm[],int hdunum) 121 122 //********************************************************/ 123 //* move to the HDU which has the specified number hdunum*/ 124 //* in the FITS file, perform read operations and write */ 125 //* the elements in an NTuple. */ 126 //********************************************************/ 127 { 128 129 // pointer to the FITS file, defined in fitsio.h 278 cout<<" FitsIOServer::load resize the matrix"; 279 cout<<" nbrows= "<<nbrows<<" nbcols= "<<nbcols<<endl; 280 } 281 282 int ij= 0; 283 for(int j = 0; j < nbcols; j++) 284 for(int i = 0; i < nbrows; i++) mat(i,j)= (int_4)i_4tab_[ij++]; 285 } 286 287 void FitsIoServer::load(NTuple& ntpl,char flnm[],int hdunum) 288 289 // ***************************************************** 290 // move to the HDU which has the specified number hdunum 291 // in the FITS file, read data values form an ASCII or 292 // binary table and write the elements in an NTuple. 293 // Only TFLOAT or TDOUBLE datatype are allowed 294 // ***************************************************** 295 296 { 297 // pointer to the FITS file, defined in fitsio.h 130 298 fitsfile *fptr; 131 int status 132 if( fits_open_file(&fptr,flnm,READONLY,&status) )133 299 int status= 0; 300 fits_open_file(&fptr,flnm,READONLY,&status); 301 if( status ) printerror( status ); 134 302 135 303 // move to the HDU 136 int hdutype; 137 if( fits_movabs_hdu(fptr,hdunum,&hdutype,&status) ) 138 printerror( status ); 304 int hdutype= 0; 305 fits_movabs_hdu(fptr,hdunum,&hdutype,&status); 306 if( status ) printerror( status ); 307 308 if(hdutype == ASCII_TBL) 309 printf("\nReading ASCII table in HDU %d:\n",hdunum); 310 else if(hdutype == BINARY_TBL) 311 printf("\nReading binary table in HDU %d:\n",hdunum); 312 else 313 { 314 printf("Error:: this HDU is not an ASCII or binary table\n"); 315 throw IOExc("FitsIoServer::load(NTuple& ," + (string)flnm + ") Error"); 316 } 317 318 // get the number of columns 319 int ncols= 0; 320 fits_get_num_cols(fptr,&ncols,&status); 321 if( status ) printerror( status ); 322 323 // get the number of rows 324 long naxis2= 0; 325 fits_get_num_rows(fptr,&naxis2,&status); 326 if( status ) printerror( status ); 327 int nrows= (int)naxis2; 328 329 // get the datatype of the values and the min repeat count 330 list<int> tfields; 331 long repeat; 332 int ntest= 0; 333 for(int ii = 0; ii < ncols; ii++) 334 { 335 int DTYPE; 336 long rept,width; 337 fits_get_coltype(fptr,ii+1,&DTYPE,&rept,&width,&status); 338 if(DTYPE == TFLOAT || DTYPE == TDOUBLE) 339 { 340 tfields.push_back(ii+1); 341 if(ntest == 0) repeat= rept; 342 if(rept < repeat) repeat= rept; 343 ntest++; 344 } 345 } 346 347 if(tfields.empty()) 348 { 349 cout << " No data values of type TFLOAT or TDOUBLE" << endl; 350 throw IOExc("FitsIoServer::load(NTuple&) Exit"); 351 } 352 else 353 cout << " nbre tfields= " << tfields.size() << " rep= " << repeat << endl; 354 355 // get input elements to create the NTuple 356 char **clname; 357 clname= new char*[tfields.size()]; 358 for(int ii = 0; ii < tfields.size(); ii++) 359 { 360 ostringstream oss; oss<<"Col_"<<ii+1; 361 string ss= oss.str(); 362 clname[ii]= new char[FLEN_VALUE]; 363 strcpy(clname[ii],ss.data()); 364 } 365 366 // create a NTuple 367 NTuple nt0(tfields.size(),clname); 368 for(int ii = 0; ii < tfields.size(); ii++) 369 delete [] clname[ii]; 370 delete [] clname; 371 372 // value to represent undefined array elements 373 int anull= 0; 374 float fnull = FLOATNULLVALUE; 375 float value[1]; 376 377 // read elements from columns and fill NTuple 378 for (int k = 0; k < nrows; k++) 379 { 380 for(int i = 0; i < repeat; i++) 381 { 382 int j= 0; 383 float *xnt= new float[tfields.size()]; 384 list<int>::iterator jtr; 385 for(jtr= tfields.begin(); jtr != tfields.end(); jtr++) 386 { 387 fits_read_col(fptr,TFLOAT,*jtr,k+1,i+1,1,&fnull,value, 388 &anull,&status); 389 if( status ) printerror(status,"fits_read_col"); 390 xnt[j++]= value[0]; 391 } 392 nt0.Fill(xnt); 393 delete[] xnt; 394 } 395 } 139 396 140 397 // get number of keywords 141 398 int nkeys,keypos; 142 if( fits_get_hdrpos(fptr,&nkeys,&keypos,&status) ) 143 printerror( status ); 144 145 146 if (hdutype == BINARY_TBL) 147 printf("\nReading binary table in HDU %d:\n",hdunum); 148 else 149 { 150 printf("Error:: this HDU is not a binary table\n"); 151 throw IOExc("FitsIoServer::load(NTuple& ," + (string)flnm + ") Error Not a bin table !"); 152 // exit ( status ); 153 } 154 155 // number of columns 156 int tfields; 157 if( fits_get_num_cols(fptr,&tfields,&status) ) 158 printerror( status ); 159 160 // to get table size 161 long naxis[2]; 162 int nfound; 163 if( fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxis, &nfound, &status) ) 164 printerror( status ); 165 int nrows= naxis[1]; 166 167 //Information about each column 168 char **ttype, **tform; 169 ttype= new char*[tfields]; 170 tform= new char*[tfields]; 171 int ii; 172 for(ii = 0; ii < tfields; ii++) 173 { 174 ttype[ii]= new char[FLEN_VALUE]; 175 tform[ii]= new char[FLEN_VALUE]; 176 } 177 178 // number of TTYPEn,TFORMn and TUNITn keywords found 179 int num= 0; 180 181 // read the column names from the TTYPEn keywords 182 fits_read_keys_str(fptr,"TTYPE",1,tfields,ttype,&nfound,&status); 183 num += nfound; 184 // read the column names from the TFORMn keywords 185 fits_read_keys_str(fptr,"TFORM",1,tfields,tform,&nfound,&status); 186 num += nfound; 187 188 //for(int ii = 0; ii < tfields; ii++) 189 // printf("\nColumn name & Format %8s %8s", ttype[ii],tform[ii]); 399 fits_get_hdrpos(fptr,&nkeys,&keypos,&status); 400 if( status ) printerror( status ); 401 402 // put other reserved keywords in a DVList object 403 char keyname[LEN_KEYWORD]= ""; 404 char strval[FLEN_VALUE]= ""; 405 char dtype; 406 char card[FLEN_CARD]; 407 char *comkey = "COMMENT"; 408 409 // shift with the number of mandatory keywords 410 int num= 8; 411 412 for(int j = num+1; j <= nkeys; j++) 413 { 414 fits_read_keyn(fptr,j,card,strval,NULL,&status); 415 if(status) printerror(status); 416 417 strncpy(keyname,card,LEN_KEYWORD-1); 418 419 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0 420 && strlen(strval) != 0) 421 { 422 fits_get_keytype(strval,&dtype,&status); 423 if(status) printerror(status); 424 425 strip(keyname, 'B',' '); 426 strip(strval, 'B',' '); 427 strip(strval, 'B','\''); 428 switch( dtype ) 429 { 430 case 'C': 431 nt0.Info()[keyname]= strval; 432 break; 433 case 'I': 434 int ival; 435 fits_read_key(fptr,TINT,keyname,&ival,NULL,&status); 436 nt0.Info()[keyname]= ival; 437 break; 438 case 'L': 439 int ilog; 440 if(strncmp(strval,"T",1) == 0) ilog= 1; 441 else ilog= 0; 442 nt0.Info()[keyname]= ilog; 443 break; 444 case 'F': 445 double dval; 446 fits_read_key(fptr,TDOUBLE,keyname,&dval,NULL,&status); 447 nt0.Info()[keyname]= dval; 448 break; 449 } 450 } 451 } 190 452 191 // select the columns with float data values 192 int typecode; 193 long repeat,width; 194 list<int> column; 195 for(ii = 0; ii < tfields; ii++) 196 { 197 fits_binary_tform(tform[ii],&typecode, &repeat, &width, &status); 198 //printf("\n%4s %3d %2ld %2ld", tform[ii], typecode, repeat, width); 199 if(typecode == TFLOAT) 200 column.push_back(ii+1); 201 } 202 203 // get input elements to create the NTuple 204 char **clname; 205 clname= new char*[column.size()]; 206 for(ii = 0; ii < column.size(); ii++) 207 clname[ii]= new char[FLEN_VALUE]; 208 209 list<int>::iterator itr; 210 int index= 0; 211 for(itr= column.begin(); itr != column.end(); itr++) 212 strcpy(clname[index++],ttype[*itr-1]); 213 214 for(ii = 0; ii < tfields; ii++) 215 { 216 delete [] ttype[ii]; 217 delete [] tform[ii]; 218 } 219 delete [] ttype; 220 delete [] tform; 221 222 223 // check if the specified keyword BLK exists 224 int blk= 512; 225 if(check_keyword(fptr,nkeys,"BLK")) 226 { 227 if( fits_read_key(fptr,TINT,"BLK",&blk,NULL,&status) ) 228 printerror( status ); 229 } 230 // create a NTuple 231 NTuple nt0(column.size(),clname,blk); 232 233 for(ii = 0; ii < column.size(); ii++) 234 delete [] clname[ii]; 235 delete [] clname; 236 237 float value[1]; 238 long felem = 1; 239 char strnull[10]; 240 strcpy(strnull, " "); 241 int anynull= 0; 242 243 // value to represent undefined array elements 244 float floatnull = FLOATNULLVALUE; 245 246 // read elements from columns and fill NTuple 247 for (int k = 0; k < nrows; k++) 248 { 249 int j= 0; 250 float *xnt= new float[column.size()]; 251 list<int>::iterator itr; 252 for(itr= column.begin(); itr != column.end(); itr++) 253 { 254 fits_read_col(fptr,TFLOAT,*itr,k+1,felem,1,&floatnull,value, 255 &anynull, &status); 256 xnt[j++]= value[0]; 257 } 258 nt0.Fill(xnt); 259 delete[] xnt; 260 } 261 262 // the TUNITn keywords are optional, if they exist they are put 263 // in the DVLIst object 264 char keyname[LEN_KEYWORD]= ""; 265 char strval[FLEN_VALUE]; 266 for(ii = 0; ii < tfields; ii++) 267 { 268 fits_make_keyn("TUNIT",ii+1,keyname,&status); 269 if(check_keyword(fptr,nkeys,keyname)) 270 { 271 num++; 272 if( fits_read_key_str(fptr,keyname,strval,NULL,&status) ) 273 printerror(status); 274 strip (keyname, 'B',' '); 275 strip(strval, 'B',' '); 276 nt0.Info()[keyname]= strval; 277 } 278 } 279 280 // add the number of mandatory keywords of a binary table 281 num += 8; 282 283 // put names and values of other reserved keywords in a DVList object 284 char comment[FLEN_COMMENT]; 285 char dtype; 286 for(int j = num+1; j <= nkeys; j++) 287 { 288 fits_read_keyn(fptr,j,keyname,strval,comment,&status); 289 fits_get_keytype(strval,&dtype,&status); 290 strip (keyname, 'B',' '); 291 strip(strval, 'B',' '); 292 strip(strval, 'B','\''); 293 294 switch( dtype ) 295 { 296 case 'C': 297 nt0.Info()[keyname]= strval; 298 break; 299 case 'I': 300 int ival; 301 ctoi(strval,&ival); 302 nt0.Info()[keyname]= (int_4)ival; 303 break; 304 case 'F': 305 double dval; 306 ctof(strval,&dval); 307 nt0.Info()[keyname]= dval; 308 break; 309 } 310 } 311 312 // copy in the input NTuple 313 ntpl= nt0; 314 315 if( fits_close_file(fptr, &status) ) 316 printerror(status); 317 318 printf("\n"); 319 return; 320 } 321 453 // copy in the input NTuple 454 ntpl= nt0; 455 456 // close the file 457 fits_close_file(fptr,&status); 458 if(status) printerror(status); 459 } 322 460 323 461 void FitsIoServer::load(SphericalMap<double>& sph, char flnm[]) … … 325 463 int npixels=0; 326 464 int nside=0; 327 longnaxis;465 int naxis; 328 466 int n1, n2, n3; 329 467 … … 365 503 366 504 367 void FitsIoServer::load(SphereGorski<float>& sph, char flnm[], int hdunum) 368 { 369 int npixels=0; 370 int nside=0; 371 372 FITS_tab_typ_ = TFLOAT; 373 505 void FitsIoServer::load(SphereGorski<float>& sph,char flnm[],int hdunum) 506 { 507 int npixels= 0; 508 int nside = 0; 509 510 FITS_tab_typ_= TFLOAT; 374 511 DVList dvl; 375 planck_read_bntbl(flnm, hdunum, npixels, dvl); 376 //dvl.Print();512 513 planck_read_bntbl(flnm,hdunum,npixels,dvl); 377 514 nside= dvl.GetI("NSIDE"); 515 378 516 const char* ordering= dvl.GetS("ORDERING").c_str(); 517 379 518 char* ring = "RING"; 380 if ( strncmp(ordering, ring,4) != 0) 381 382 // if (ordering!="RING ") 383 { 384 cout << " numerotation non RING" << endl; 385 } 519 if(strncmp(ordering,ring,4) != 0) 520 cout << " numerotation non RING" << endl; 521 522 386 523 // number of pixels in the sphere 387 if (sph.NbPixels() != npixels) 388 { 389 //DBG cout << " found " << npixels << " pixels" << endl; 390 //DBG cout << " expected " << sph.NbPixels() << endl; 524 if(sph.NbPixels() != npixels) 525 { 391 526 if (nside <= 0 ) 392 527 { 393 528 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl; 394 throw IOExc("FitsIoServer::load(SphericalMap<float>& ," + (string)flnm + ", ) Error No resolution parameter !"); 395 // exit(0); 529 throw IOExc("FitsIoServer::load(SphereGorski<float>& ," + (string)flnm + ", ) Error No resolution parameter !"); 396 530 } 397 531 if (nside != sph.SizeIndex()) 398 532 { 399 533 sph.Resize(nside); 400 cout << "FitsIoServer::load(SphereGorski<float> ...) ReSizing to NSide= " << nside << endl; 401 } 402 // else 403 // { 404 // cout << " FITSIOSERVER : same resolution, surprising ! " << endl; 405 // exit(0); $CHECK$ - Il ne faut pas sortir, me semble-t-il , Reza 20/11/99 406 // } 407 } 408 for (int j = 0; j < sph.NbPixels(); j++) 409 { 410 sph(j)= (float)r_4tab_[j]; 411 } 412 } 413 void FitsIoServer::load(SphereGorski<double>& sph, char flnm[], int hdunum) 534 cout << " FitsIoServer::load(SphereGorski<float> ...)"; 535 cout << " ReSizing to NSide= " << nside << endl; 536 } 537 } 538 539 for(int j = 0; j < sph.NbPixels(); j++) sph(j)= (float)r_4tab_[j]; 540 } 541 542 void FitsIoServer::load(SphereGorski<double>& sph,char flnm[],int hdunum) 543 { 544 int npixels= 0; 545 int nside = 0; 546 547 FITS_tab_typ_ = TDOUBLE; 548 DVList dvl; 549 planck_read_bntbl(flnm,hdunum,npixels,dvl); 550 551 nside= dvl.GetI("NSIDE"); 552 553 const char* ordering= dvl.GetS("ORDERING").c_str(); 554 555 char* ring = "RING"; 556 if(strncmp(ordering,ring,4) != 0) 557 cout << " numerotation non RING" << endl; 558 559 // number of pixels in the sphere 560 if(sph.NbPixels() != npixels) 561 { 562 if(nside <= 0) 563 throw IOExc("FitsIoServer::load(SphereGorski<double>& ," + (string)flnm + ", ) No resol parameter !"); 564 565 if(nside != sph.SizeIndex()) 566 { 567 sph.Resize(nside); 568 cout << " FitsIoServer::load(SphereGorski<double> ...)"; 569 cout << " ReSizing to NSide= " << nside << endl; 570 } 571 } 572 573 for(int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j]; 574 } 575 576 void FitsIoServer::load(SphericalMap<float>& sph, char flnm[]) 414 577 { 415 578 int npixels=0; 416 579 int nside=0; 417 418 FITS_tab_typ_ = TDOUBLE; 419 580 int naxis; 581 int n1, n2, n3; 420 582 DVList dvl; 421 planck_read_bntbl(flnm, hdunum, npixels, dvl); 422 //dvl.Print(); 583 584 FITS_tab_typ_ = TFLOAT; 585 586 planck_read_img(flnm, naxis, n1, n2, n3, dvl); 587 if (naxis != 1) 588 { 589 cout << " le fichier fits n'est pas une sphere, naxis= " << naxis << endl; 590 } 591 npixels=n1; 423 592 nside= dvl.GetI("NSIDE"); 424 const char* ordering= dvl.GetS("ORDERING").c_str(); 425 char* ring = "RING"; 426 if ( strncmp(ordering, ring,4) != 0) 427 428 // if (ordering!="RING ") 429 { 430 cout << " numerotation non RING" << endl; 431 } 593 432 594 // number of pixels in the sphere 433 595 if (sph.NbPixels() != npixels) … … 438 600 { 439 601 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl; 440 throw IOExc("FitsIoServer::load(Spher eGorski<double>& ," + (string)flnm + ", ) No resol parameter!");602 throw IOExc("FitsIoServer::load(SphericalMap<float>& ," + (string)flnm + ") No resolution param !"); 441 603 // exit(0); 442 604 } … … 449 611 { 450 612 cout << " FITSIOSERVER : same resolution, surprising ! " << endl; 451 // exit(0); $CHECK$ - ne pas sortir , Reza 20/11/99452 }453 }454 for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j];455 }456 457 void FitsIoServer::load(SphericalMap<float>& sph, char flnm[])458 {459 int npixels=0;460 int nside=0;461 long naxis;462 int n1, n2, n3;463 DVList dvl;464 465 FITS_tab_typ_ = TFLOAT;466 467 planck_read_img(flnm, naxis, n1, n2, n3, dvl);468 if (naxis != 1)469 {470 cout << " le fichier fits n'est pas une sphere, naxis= " << naxis << endl;471 }472 npixels=n1;473 nside= dvl.GetI("NSIDE");474 475 // number of pixels in the sphere476 if (sph.NbPixels() != npixels)477 {478 cout << " found " << npixels << " pixels" << endl;479 cout << " expected " << sph.NbPixels() << endl;480 if (nside <= 0 )481 {482 cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;483 throw IOExc("FitsIoServer::load(SphericalMap<float>& ," + (string)flnm + ") No resolution param !");484 // exit(0);485 }486 if (nside != sph.SizeIndex())487 {488 sph.Resize(nside);489 cout << " resizing the sphere to nside= " << nside << endl;490 }491 else492 {493 cout << " FITSIOSERVER : same resolution, surprising ! " << endl;494 613 // exit(0); $CHECK$ - Ne pas sortir , Reza 20/11/99 495 614 } … … 503 622 int nbcols=0; 504 623 FITS_tab_typ_ = TDOUBLE; 505 longnaxis;624 int naxis; 506 625 int n1, n2, n3; 507 626 DVList dvl; … … 545 664 { 546 665 FITS_tab_typ_ = TFLOAT; 547 longnaxis;666 int naxis; 548 667 int siz_x; 549 668 int siz_y; … … 585 704 { 586 705 FITS_tab_typ_ = TINT; 587 longnaxis;706 int naxis; 588 707 int siz_x; 589 708 int siz_y; … … 1235 1354 1236 1355 // primary header 1237 int bitpix=LONG_IMG;1238 int naxis=0;1239 fits_create_img(fptr, bitpix, naxis, NULL, &status);1356 // int bitpix=LONG_IMG; 1357 // int naxis=0; 1358 // fits_create_img(fptr, bitpix, naxis, NULL, &status); 1240 1359 // write the current date 1241 fits_write_date(fptr, &status);1360 // fits_write_date(fptr, &status); 1242 1361 //DBG cerr << " DBG - Apres write_date status = " << status << endl; 1243 if( status ) printerror( status );1362 // if( status ) printerror( status ); 1244 1363 1245 1364 … … 1367 1486 } 1368 1487 1369 void FitsIoServer::planck_read_img(char flnm[], long& naxis,int& n1, int& n2, int& n3, DVList& dvl) 1370 { 1371 int status=0; 1372 long bitpix; 1373 long naxes[3]={0,0,0}; 1374 char* comment=NULL; 1488 void FitsIoServer::planck_read_img(char flnm[],int &naxis,int &n1,int &n2,int &n3,DVList &dvl) 1489 { 1490 int status= 0; 1375 1491 1376 1492 // pointer to the FITS file, defined in fitsio.h 1377 1493 fitsfile *fptr; 1494 1378 1495 // initialize status before calling fitsio routines 1379 fits_open_file(&fptr, flnm, READONLY, &status); 1380 if( status ) printerror( status ); 1381 1382 1383 fits_read_key_lng(fptr, "BITPIX", &bitpix, comment, &status); 1384 if( status ) printerror( status ); 1385 fits_read_key_lng(fptr, "NAXIS", &naxis, comment, &status); 1386 if( status ) printerror( status ); 1496 fits_open_file(&fptr,flnm,READONLY,&status); 1497 if( status ) printerror( status ); 1498 1499 // bits per pixels 1500 int bitpix; 1501 fits_read_key(fptr,TINT,"BITPIX",&bitpix,NULL,&status); 1502 if( status ) printerror( status ); 1503 1504 // number of dimensions in the FITS array 1505 fits_read_key(fptr,TINT,"NAXIS",&naxis,NULL,&status); 1506 if( status ) printerror( status ); 1507 1508 // read the NAXIS1,NAXIS2 and NAXIS3 keyword to get image size 1509 long naxes[3]= {0,0,0}; 1387 1510 int nfound; 1388 int nkeys=(int)naxis; 1389 fits_read_keys_lng(fptr, "NAXIS", 1, nkeys, naxes, &nfound, &status); 1390 if( status ) printerror( status ); 1391 1392 n1 = naxes[0] ; 1393 n2 = naxes[1] ; 1394 n3 = naxes[2] ; 1395 1511 fits_read_keys_lng(fptr,"NAXIS",1,naxis,naxes,&nfound,&status); 1512 if( status ) printerror( status ); 1513 1514 n1 = (int)naxes[0]; 1515 n2 = (int)naxes[1]; 1516 n3 = (int)naxes[2]; 1396 1517 1397 1518 long nelements= naxes[0]; 1398 if (naxis >=2) nelements*=naxes[1]; 1399 if (naxis == 3) nelements*=naxes[2]; 1400 int anynull=0; 1401 r_8 dnullval=DOUBLENULLVALUE; 1402 r_4 fnullval=FLOATNULLVALUE; 1403 int_4 inullval=0; 1519 if(naxis >= 2) nelements*= naxes[1]; 1520 if(naxis == 3) nelements*= naxes[2]; 1521 1522 int anull= 0; 1523 r_8 dnull= DOUBLENULLVALUE; 1524 r_4 fnull= FLOATNULLVALUE; 1525 int_4 inull= 0; 1526 1404 1527 // on laisse a fits le soin de convertir le type du tableau lu vers 1405 1528 // le type suppose par l'utilisateur de fitsioserver 1406 // 1407 switch ( FITS_tab_typ_) 1529 switch (FITS_tab_typ_) 1408 1530 { 1409 1531 case TDOUBLE : 1410 if (bitpix != DOUBLE_IMG) 1411 { 1412 cout << " FitsIOServer : the data type on fits file " << flnm << " is not double, " 1413 << " conversion to double will be achieved by cfitsio lib " << endl; 1414 } 1415 if (r_8tab_ != NULL) { delete [] r_8tab_; r_8tab_ = NULL; } 1532 if(bitpix != DOUBLE_IMG) 1533 { 1534 cout << " FitsIOServer:: the data type on fits file " << flnm; 1535 cout << " is not double, " << "conversion to double will"; 1536 cout << " be achieved by cfitsio lib" << endl; 1537 } 1538 if(r_8tab_ != NULL) { delete [] r_8tab_; r_8tab_= NULL; } 1416 1539 r_8tab_=new r_8[nelements]; 1417 fits_read_img(fptr, TDOUBLE, 1, nelements, &dnullval, r_8tab_, 1418 &anynull, &status); 1540 fits_read_img(fptr,TDOUBLE,1,nelements,&dnull,r_8tab_,&anull,&status); 1419 1541 if( status ) printerror( status ); 1420 1542 break; 1543 1421 1544 case TFLOAT : 1422 if (bitpix != FLOAT_IMG) 1423 { 1424 cout << " FitsIOServer : the data type on fits file " << flnm << " is not float, " 1425 << " conversion to float will be achieved by cfitsio lib " << endl; 1426 } 1427 if (r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_ = NULL; } 1545 if(bitpix != FLOAT_IMG) 1546 { 1547 cout << " FitsIOServer:: the data type on fits file " << flnm; 1548 cout << " is not float, " << "conversion to float will"; 1549 cout << " be achieved by cfitsio lib" << endl; 1550 } 1551 if(r_4tab_ != NULL) { delete [] r_4tab_; r_4tab_= NULL; } 1428 1552 r_4tab_=new r_4[nelements]; 1429 fits_read_img(fptr, TFLOAT, 1, nelements, &fnullval, r_4tab_,1430 &anynull, &status);1553 fits_read_img(fptr,TFLOAT,1,nelements,&fnull,r_4tab_,&anull,&status); 1554 if( status ) printerror( status ); 1431 1555 //SHV: remove useless print... 1432 // for (int kk=0; kk<nelements; kk++) cout << r_4tab_[kk] << endl; 1433 cout << " anynull= " << anynull << endl; 1556 // for (int kk=0; kk<nelements; kk++) cout << r_4tab_[kk] << endl; 1557 break; 1558 1559 case TINT : 1560 if(bitpix != LONG_IMG) 1561 { 1562 cout << " FitsIOServer:: the data type on fits file " << flnm; 1563 cout << " is not long, " << "conversion to long will"; 1564 cout << " be achieved by cfitsio lib" << endl; 1565 } 1566 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_= NULL; } 1567 i_4tab_=new int_4[nelements]; 1568 fits_read_img(fptr,TINT,1,nelements,&inull,i_4tab_,&anull, &status); 1434 1569 if( status ) printerror( status ); 1435 1570 break; 1436 1571 1437 1438 case TINT : 1439 if (bitpix != LONG_IMG) 1440 { 1441 cout << " FitsIOServer : the data type on fits file " << flnm << " is not long, " 1442 << " conversion to long will be achieved by cfitsio lib " << endl; 1443 } 1444 if (i_4tab_ != NULL) { delete [] i_4tab_; i_4tab_ = NULL; } 1445 i_4tab_=new int_4[nelements]; 1446 fits_read_img(fptr, TINT, 1, nelements, &inullval, i_4tab_, 1447 &anynull, &status); 1448 if( status ) printerror( status ); 1572 default : 1573 cout << " FitsIOServer:: datatype code " << FITS_tab_typ_; 1574 cout << " FitsIOServer::planck_read_img not yet programmed" << endl; 1449 1575 break; 1450 1451 1452 default : 1453 cout << " FitsIOServer::read_img : type non traite: " << FITS_tab_typ_ << endl; 1454 break; 1455 } 1576 } 1577 1456 1578 status = 0; 1579 1580 int nkeys; 1581 int keypos; 1582 fits_get_hdrpos(fptr,&nkeys,&keypos,&status); 1583 if( status ) printerror( status ); 1584 1457 1585 char card[FLEN_CARD]; 1458 1586 char keyname[LEN_KEYWORD]= ""; 1459 1587 char strval[FLEN_VALUE]; 1460 char comment1[FLEN_COMMENT];1461 1588 char dtype; 1462 char * comkey = "COMMENT"; 1463 int keypos; 1464 fits_get_hdrpos(fptr,&nkeys,&keypos,&status); 1465 if( status ) printerror( status ); 1466 // cout << " planck_read_img : nombre de mots-cles : " << nkeys << endl; 1467 for(int j = 1; j <= nkeys; j++) 1468 { 1469 fits_read_keyn(fptr,j,card,strval,comment1,&status); 1470 strncpy(keyname,card,LEN_KEYWORD-1); 1471 if ( strncmp(keyname,comkey ,LEN_KEYWORD-1) != 0) 1472 { 1473 fits_get_keytype(strval,&dtype,&status); 1474 // cout<<" keyname= "<< keyname <<" dtype= "<<dtype <<endl; 1475 strip (keyname, 'B',' '); 1476 strip(strval, 'B',' '); 1477 strip(strval, 'B','\''); 1478 switch( dtype ) 1479 { 1480 case 'C': 1481 dvl[keyname]= strval; 1482 break; 1483 case 'I': 1484 int ival; 1485 ctoi(strval,&ival); 1486 dvl[keyname]= (int_4)ival; 1487 break; 1488 case 'L': 1489 int ilog; 1490 if (strncmp(strval,"T" ,1) ==0) ilog=1; 1491 else ilog=0; 1492 dvl[keyname]= (int_4)ilog; 1493 break; 1494 case 'F': 1495 double dval; 1496 ctof(strval,&dval); 1497 dvl[keyname]= dval; 1498 break; 1499 default : 1500 cout << " FitsIOServer : type de mot-cle bizarre: " << keyname <<" dtype= "<<dtype << endl; 1501 break; 1502 } 1503 } 1504 } 1589 char *comkey = "COMMENT"; 1590 1591 for(int j = 1; j <= nkeys; j++) 1592 { 1593 fits_read_keyn(fptr,j,card,strval,NULL,&status); 1594 strncpy(keyname,card,LEN_KEYWORD-1); 1595 1596 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0) 1597 { 1598 fits_get_keytype(strval,&dtype,&status); 1599 strip(keyname, 'B',' '); 1600 strip(strval, 'B',' '); 1601 strip(strval, 'B','\''); 1602 switch( dtype ) 1603 { 1604 case 'C': 1605 dvl[keyname]= strval; 1606 break; 1607 case 'I': 1608 int ival; 1609 ctoi(strval,&ival); 1610 dvl[keyname]= (int_4)ival; 1611 break; 1612 case 'L': 1613 int ilog; 1614 if (strncmp(strval,"T" ,1) ==0) ilog=1; 1615 else ilog=0; 1616 dvl[keyname]= (int_4)ilog; 1617 break; 1618 case 'F': 1619 double dval; 1620 ctof(strval,&dval); 1621 dvl[keyname]= dval; 1622 break; 1623 default : 1624 cout << " FitsIOServer:: type de mot-cle bizarre"; 1625 cout << " keyname= " << keyname <<" dtype= "<< dtype << endl; 1626 break; 1627 } 1628 } 1629 } 1505 1630 1506 1631 // close the file 1507 status= 0;1632 status= 0; 1508 1633 fits_close_file(fptr, &status); 1509 1634 if( status ) printerror( status ); 1510 1635 } 1511 1512 1636 1513 1637 void FitsIoServer::planck_read_bntbl(char flnm[], int hdunum, int& npixels, DVList& dvl) … … 1657 1781 //char bidon[LEN_KEYWORD]; 1658 1782 char * comkey = "COMMENT"; 1783 char blank[LEN_KEYWORD]= ""; 1659 1784 1660 1785 for(int j = 1; j <= nkeys; j++) 1661 1786 { 1662 // 1663 // 1664 // cout << " bidon= " << keyname << endl;1787 //fits_read_record(fptr, j, card, &status); 1788 //strncpy(keyname,card,LEN_KEYWORD-1); 1789 //cout << " cle= " << keyname << endl; 1665 1790 // if ( strncmp(keyname,comkey ,LEN_KEYWORD-1) != 0) 1666 fits_read_keyn(fptr,j,card,strval,comment1,&status); 1791 1792 fits_read_keyn(fptr,j,card,strval,NULL,&status); 1667 1793 strncpy(keyname,card,LEN_KEYWORD-1); 1668 1794 1669 if ( strncmp(keyname,comkey ,LEN_KEYWORD-1) != 0)1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1795 if(strncmp(keyname,comkey,LEN_KEYWORD-1) != 0 && strlen(keyname) != 0) 1796 { 1797 fits_get_keytype(strval,&dtype,&status); 1798 // cout<<" keyname= "<< keyname <<" dtype= "<<dtype <<endl; 1799 strip (keyname, 'B',' '); 1800 strip(strval, 'B',' '); 1801 strip(strval, 'B','\''); 1802 switch( dtype ) 1803 { 1804 case 'C': 1805 dvl[keyname]= strval; 1806 break; 1807 case 'I': 1808 int ival; 1809 ctoi(strval,&ival); 1810 dvl[keyname]= (int_4)ival; 1811 break; 1812 case 'L': 1813 int ilog; 1814 if (strncmp(strval,"T" ,1) ==0) ilog=1; 1815 else ilog=0; 1816 dvl[keyname]= (int_4)ilog; 1817 break; 1818 case 'F': 1819 double dval; 1820 ctof(strval,&dval); 1821 dvl[keyname]= dval; 1822 break; 1823 default : 1824 cout << " FitsIOServer : type de mot-cle bizarre: " << keyname <<" dtype= "<<dtype << endl; 1825 break; 1826 } 1827 } 1702 1828 } 1703 1829 1704 1830 // close the file 1705 1831 status=0; -
trunk/SophyaExt/FitsIOServer/fitsioserver.h
r663 r689 11 11 #include "cimage.h" 12 12 #include "dvlist.h" 13 13 #include "anydataobj.h" 14 14 #include <fitsio.h> /* entre <> pour que mkmflib fonctionne correctement */ 15 15 … … 21 21 public : 22 22 23 FitsIoServer() : FITS_tab_typ_(0),i_4tab_(NULL),r_4tab_(NULL),r_8tab_(NULL) {;} 23 FitsIoServer() : FITS_tab_typ_(0),i_4tab_(NULL),r_4tab_(NULL),r_8tab_(NULL) 24 {;} 24 25 ~FitsIoServer() 25 26 { … … 56 57 void Mollweide_picture_projection(SphericalMap<double>& sph, char flnm[]); 57 58 void picture(LocalMap<double>& lcm, char flnm[]); 58 59 60 61 59 void readheader(char flnm[]); 62 60 61 virtual AnyDataObj* loadobj(char flnm[],int hdunum = 1); 62 63 63 private : 64 65 64 66 65 // creer, ecrire une imageformat FITS, a partir des tableaux de donnees 67 66 // dtab_, ftab_ prealablement remplis 68 void planck_write_img(char flnm[],int naxis,int n1,int n2,int n3,DVList &dvl);67 void planck_write_img(char flnm[],int naxis,int n1,int n2,int n3,DVList &dvl); 69 68 70 void planck_read_img(char flnm[], long& naxis,int& n1,int& n2,int& n3,DVList&dvl);69 void planck_read_img(char flnm[],int &naxis,int &n1,int &n2,int &n3,DVList &dvl); 71 70 void planck_read_bntbl(char flnm[], int hdunum, int& npixels, DVList& dvl); 72 71 void planck_write_bntbl(char flnm[], int npixels, char typeOfContent[], char extname[], char comment1[], DVList& dvl);
Note:
See TracChangeset
for help on using the changeset viewer.