Changeset 603 in Sophya for trunk/SophyaExt/FitsIOServer


Ignore:
Timestamp:
Nov 19, 1999, 6:33:34 PM (26 years ago)
Author:
ansari
Message:

ajout save sphereGorski

Location:
trunk/SophyaExt/FitsIOServer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaExt/FitsIOServer/fitsioserver.cc

    r572 r603  
    333333        }
    334334    }
     335  //  cout << " fin relecture debut transfert" << endl;
    335336  for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (float)r_4tab_[j];
     337}
     338void FitsIoServer::load(SphereGorski<double>& sph, char flnm[], int hdunum)
     339{
     340  int npixels=0;
     341  int nside=0;
     342
     343  FITS_tab_typ_ = TDOUBLE;
     344
     345  DVList dvl;
     346  planck_read_bntbl(flnm, hdunum, npixels, dvl);
     347  //dvl.Print();
     348  nside= dvl.GetI("NSIDE");
     349  const char* ordering= dvl.GetS("ORDERING").c_str();
     350  char* ring = "'RING";
     351  if (  strncmp(ordering, ring,3) != 0)
     352
     353    //  if (ordering!="RING    ")
     354    {
     355      cout << " numerotation non RING" << endl;
     356    }
     357  // number of pixels in the sphere
     358  if (sph.NbPixels() != npixels)
     359    {
     360      cout << " found " << npixels << " pixels" << endl;
     361      cout << " expected " << sph.NbPixels() << endl;
     362      if (nside <= 0 )
     363        {
     364          cout<<" FITSIOSERVER: no resolution parameter on fits file "<<endl;
     365          exit(0);
     366        }
     367      if (nside != sph.SizeIndex())
     368        {
     369          sph.Resize(nside);
     370          cout << " resizing the sphere to nside=  " << nside  << endl;
     371        }
     372      else
     373        {
     374          cout << " FITSIOSERVER : same resolution, surprising ! " << endl;
     375          exit(0);
     376        }
     377    }
     378  for (int j = 0; j < sph.NbPixels(); j++) sph(j)= (double)r_8tab_[j];
    336379}
    337380
     
    731774
    732775}
     776void FitsIoServer::save(SphereGorski<float>& sph, char filename[])   
     777{
     778  int npixels = sph.NbPixels();
     779  FITS_tab_typ_ = TFLOAT;
     780  if (r_4tab_ != NULL ) delete[] r_4tab_;
     781  r_4tab_=new r_4[npixels];
     782
     783
     784  for (int j = 0; j < npixels; j++) r_4tab_[j]= (r_4)sph(j);
     785  DVList dvl;
     786  dvl.SetS("PIXTYPE","HEALPIX ");
     787  dvl["ORDERING"]=sph.TypeOfMap();
     788  dvl["NSIDE"] = (int_4)sph.SizeIndex();
     789  dvl["FIRSTPIX"]=0;
     790  dvl["LASTPIX"]=npixels-1;
     791  char* typeOfContent="TEMPERATURE";
     792  char* extname="SIMULATION";
     793  char* comment1="     Sky Map Pixelisation Specific Keywords";
     794  planck_write_bntbl(filename, npixels, typeOfContent, extname, comment1, dvl);
     795  delete[] r_4tab_;
     796
     797}
     798void FitsIoServer::save(SphereGorski<double>& sph, char filename[])   
     799{
     800  int npixels = sph.NbPixels();
     801  FITS_tab_typ_ = TDOUBLE;
     802  if (r_8tab_ != NULL ) delete[] r_8tab_;
     803  r_8tab_=new r_8[npixels];
     804
     805
     806  for (int j = 0; j < npixels; j++) r_8tab_[j]= (r_8)sph(j);
     807  DVList dvl;
     808  dvl.SetS("PIXTYPE","HEALPIX ");
     809  dvl["ORDERING"]=sph.TypeOfMap();
     810  dvl["NSIDE"] = (int_4)sph.SizeIndex();
     811  dvl["FIRSTPIX"]=0;
     812  dvl["LASTPIX"]=npixels-1;
     813  char* typeOfContent="TEMPERATURE";
     814  char* extname="SIMULATION";
     815  char* comment1="     Sky Map Pixelisation Specific Keywords";
     816  planck_write_bntbl(filename, npixels, typeOfContent, extname, comment1, dvl);
     817  delete[] r_8tab_;
     818
     819}
    733820
    734821void FitsIoServer::save(LocalMap<double>& locm, char filename[])   
     
    9831070      if( status ) printerror( status );
    9841071    }
     1072  // close the file
     1073  fits_close_file(fptr, &status);
     1074  if( status )  printerror( status );
     1075}
     1076
     1077void FitsIoServer::planck_write_bntbl(char flnm[], int npixels, char typeOfContent[], char extname[], char comment1[], DVList& dvl)
     1078{
     1079
     1080
     1081  // pointer to the FITS file, defined in fitsio.h
     1082  fitsfile *fptr;
     1083
     1084  // initialize status before calling fitsio routines
     1085  int status = 0;         
     1086
     1087  // delete old file if it already exists
     1088  remove(flnm);
     1089
     1090  // create new FITS file 
     1091  fits_create_file(&fptr, flnm, &status);
     1092  if( status )  printerror( status );
     1093
     1094  // primary header
     1095  int bitpix=LONG_IMG;
     1096  int naxis=0;
     1097  fits_create_img(fptr, bitpix, naxis, NULL, &status);
     1098  // write the current date
     1099  fits_write_date(fptr, &status);
     1100  if( status )  printerror( status );
     1101
     1102
     1103  long nrows=npixels/1024;
     1104  if (nrows==0) nrows=1;
     1105  if (npixels%1024 !=0) nrows++;
     1106  int tfields=1;
     1107  char **ttype, **tform;
     1108  ttype= new char*[tfields];
     1109  tform= new char*[tfields];
     1110  char* format;
     1111  if ( FITS_tab_typ_ == TDOUBLE) format="1024D";
     1112  if ( FITS_tab_typ_ == TFLOAT)  format="1024E";
     1113  if ( FITS_tab_typ_ == TINT)    format="1024I";
     1114  for(int i = 0; i < tfields; i++)
     1115    {
     1116      ttype[i]= new char[FLEN_VALUE];
     1117      strcpy(ttype[i],  typeOfContent);
     1118      tform[i]= new char[FLEN_VALUE];
     1119      strcpy(tform[i], format);
     1120    }
     1121  // create a new empty binary table onto the FITS file
     1122  // physical units if they exist, are defined in the DVList object
     1123  // so the null pointer is given for the tunit parameters.
     1124 fits_create_tbl(fptr,BINARY_TBL,nrows,tfields,ttype,tform,
     1125                 NULL,extname,&status);
     1126  if( status )  printerror( status );
     1127  for( int ii = 0; ii < tfields; ii++)
     1128    {
     1129      delete [] ttype[ii];
     1130      delete [] tform[ii];
     1131    }
     1132  delete [] ttype;
     1133  delete [] tform;
     1134  long nelements= npixels;
     1135  switch (FITS_tab_typ_)
     1136    {
     1137    case TDOUBLE :
     1138      fits_write_col(fptr, TDOUBLE, 1, 1, 1, nelements, r_8tab_, &status);
     1139      if( status )  printerror( status, "probleme dans ecriture du tableau de doubles" );
     1140      break;
     1141
     1142
     1143    case TFLOAT :
     1144      for (int kk=0; kk<10; kk++) cout << r_4tab_[kk] << endl;
     1145      fits_write_col(fptr, TFLOAT, 1, 1, 1, nelements, r_4tab_, &status);
     1146      if( status ) printerror( status );
     1147      break;
     1148    case TINT :
     1149      fits_write_col(fptr, TINT, 1, 1, 1, nelements, i_4tab_, &status);
     1150      if( status ) printerror( status );
     1151      break;
     1152    default :
     1153      cout << " FitsIOServer : type de tableau non traite en ecriture " << endl;
     1154      break;
     1155    }
     1156  // write supplementary keywords
     1157  fits_write_comment(fptr, " ", &status);
     1158  fits_write_comment(fptr, " ", &status);
     1159  if( status ) printerror( status );
     1160  fits_write_comment(fptr,"--------------------------------------------", &status);
     1161  fits_write_comment(fptr, comment1, &status);
     1162  fits_write_comment(fptr,"--------------------------------------------", &status);
     1163  if( status ) printerror( status );
     1164
     1165
     1166  int flen_keyword = 9;
     1167  // FLEN_KEYWORD est la longueur max d'un mot-cle. Il doit y avoir une
     1168  // erreur dans la librairie fits qui donne FLEN_KEYWORD=72
     1169  // contrairement a la notice qui donne FLEN_KEYWORD=9 (ch. 5, p.39)
     1170  DVList::ValList::const_iterator it;
     1171  for(it = dvl.Begin(); it != dvl.End(); it++) 
     1172    {
     1173      int datatype=  key_type_PL2FITS( (*it).second.typ);
     1174      char keyname[]="";
     1175      strncpy(keyname, (*it).first.substr(0,64).c_str(),flen_keyword-1);
     1176      char comment[FLEN_COMMENT]="";
     1177      int ival=0;
     1178      double dval=0.;
     1179      char strval[]="";
     1180      switch (datatype)
     1181        {
     1182        case TINT :
     1183          ival=(*it).second.mtv.iv;
     1184          strcpy(comment," ");
     1185          fits_write_key(fptr, datatype, keyname, &ival, comment, &status);
     1186          break;
     1187        case TDOUBLE :
     1188          dval=(*it).second.mtv.dv;
     1189          strcpy(comment," ");
     1190          fits_write_key(fptr, datatype, keyname, &dval, comment, &status);
     1191          break;
     1192        case TSTRING :
     1193          strcpy(strval, (*it).second.mtv.strv);
     1194          strcpy(comment," ");
     1195          fits_write_key(fptr, datatype, keyname, &strval, comment, &status);
     1196          break;
     1197        default :
     1198          cout << " FitsIOServer : probleme dans type mot cle optionnel" << endl;
     1199          break;
     1200        }
     1201      if( status ) printerror( status );
     1202    }
     1203  //char keyname[]="";
     1204  //char strval[]="";
     1205  //char comment[FLEN_COMMENT]="";
     1206  //strncpy(keyname, "CREATOR",flen_keyword-1);
     1207  //strcpy(strval, "SOPHYA");
     1208  //strcpy(comment," Orsay");
     1209  //fits_write_key(fptr, TSTRING, keyname, &strval, comment, &status);
     1210  //if( status ) printerror( status );
    9851211  // close the file
    9861212  fits_close_file(fptr, &status);
     
    12111437  //cout << " width : " << width << endl;
    12121438  fits_read_key_lng(fptr, "LASTPIX", &lastpix, comment, &status);
    1213   if( status )  printerror( status );
     1439  if( status )  printerror( status," mot cle LASTPIX" );
    12141440
    12151441  long nelements= nrows*repeat;
     
    12401466                    r_8tab_,
    12411467                    &anynull, &status);
    1242       if( status )  printerror( status );
     1468      if( status )  printerror( status, "probleme dans lecture du tableau de doubles" );
    12431469      break;
    12441470    case TFLOAT :
     
    12531479      fits_read_col(fptr, TFLOAT, 1, 1, 1, nelements,  &fnullval,
    12541480                    r_4tab_, &anynull, &status);
    1255       if( status )  printerror( status );
     1481      if( status )  printerror( status,"probleme dans lecture du tableau de floats" );
    12561482      break;
    12571483
     
    12691495      //fits_read_img(fptr, TINT, 1, nelements,  &inullval, i_4tab_,
    12701496      //                    &anynull, &status);
    1271   //if( status )  printerror( status );
     1497      if( status )  printerror( status,"probleme dans lecture du tableau de ints" );
    12721498      break;
    12731499
     
    12971523          {
    12981524            fits_get_keytype(strval,&dtype,&status);
    1299             //       cout<<" keyname= "<< keyname <<" dtype= "<<dtype <<endl;
     1525            //  cout<<" keyname= "<< keyname <<" dtype= "<<dtype <<endl;
    13001526            strip (keyname, 'B',' ');
    13011527            strip(strval, 'B',' ');
     
    13161542                break;
    13171543              default :
    1318                 cout << " FitsIOServer : mot-cle bizerre " << endl;
     1544                cout << " FitsIOServer : mot-cle bizarre " << endl;
    13191545                break;
    13201546              }
     
    14021628}
    14031629
    1404 // projects a SphericalMap<double>, according Mollweide-method, and saves onto
     1630// projects a SphericalMap<float>, according Mollweide-method, and saves onto
    14051631// a FITS-file
    14061632void FitsIoServer::Mollweide_picture_projection(SphericalMap<float>& sph, char filename[])
     
    14401666   
    14411667}
     1668// projects a SphericalMap<double>, according Mollweide-method, and saves onto
     1669// a FITS-file
     1670void FitsIoServer::Mollweide_picture_projection(SphericalMap<double>& sph, char filename[])
     1671{
     1672  // le code de cete methode duplique celui de la precedente, la seule
     1673  //difference etant le type de sphere en entree. Ces methodes de projection
     1674  // sont provisoires, et ne servent que pour les tests. C est pourquoi je
     1675  // ne me suis pas casse la tete, pour l instant
     1676   
     1677  long naxes[2]={600, 300};
     1678  float* map = new float[ 600*300 ];
     1679  int npixels=  naxes[0]*naxes[1];
     1680
     1681   cout << " image FITS en projection MOLLWEIDE" << endl;
     1682  // table will have npixels rows
     1683   for(int j=0; j < npixels; j++) map[j]=0.;
     1684   for(int j=0; j<naxes[1]; j++)
     1685     {
     1686       double yd = (j+0.5)/naxes[1]-0.5;
     1687       double facteur=2.*Pi/sin(acos(yd*2));
     1688       double theta = (0.5-yd)*Pi;
     1689       for(int i=0; i<naxes[0]; i++) 
     1690         {
     1691           double xa = (i+0.5)/naxes[0]-0.5;
     1692           double phi =  xa*facteur+Pi;
     1693           float th=float(theta);
     1694           float ff=float(phi);
     1695           if (phi<2*Pi && phi>= 0)
     1696             {
     1697               map[j*naxes[0]+i] = sph.PixValSph(th, ff);
     1698             }
     1699         }
     1700     }
     1701   
     1702   write_picture(naxes, map,  filename);
     1703   delete [] map;
     1704   
     1705}
    14421706
    14431707
     
    15971861}
    15981862
    1599 void FitsIoServer::printerror(int status) const
     1863void FitsIoServer::printerror(int& status) const
    16001864
    16011865  //*****************************************************/
     
    16051869
    16061870  // print out cfitsio error messages and exit program
    1607   if( status )
    1608     {
    16091871      // print error report 
    16101872      fits_report_error(stderr, status);
    16111873      // terminate the program, returning error status
    1612       exit( status );
    1613     }
    1614   return;
    1615 }
    1616 
    1617 
    1618 
     1874      //      exit( status );
     1875      status=0;
     1876}
     1877void FitsIoServer::printerror(int& status, char* texte) const
     1878
     1879  //*****************************************************/
     1880  //* Print out cfitsio error messages and exit program */
     1881  //*****************************************************/
     1882{
     1883
     1884  // print out cfitsio error messages and exit program
     1885      // print error report 
     1886      fits_report_error(stderr, status);
     1887      cout << " erreur : " << texte << endl;
     1888      status=0;
     1889}
     1890
     1891
     1892
  • trunk/SophyaExt/FitsIOServer/fitsioserver.h

    r564 r603  
    3535 void load(ImageI4& DpcImg,char flnm[]);
    3636 void load(SphereGorski<float>& sph, char flnm[], int hdunum);
     37 void load(SphereGorski<double>& sph, char flnm[], int hdunum);
    3738 void save(TMatrix<double>& mat, char flnm[]);
    3839 void save(NTuple& ntpl,char flnm[]);
    3940 void save(SphericalMap<double>& sph, char flnm[]);
    4041 void save(SphericalMap<float>& sph, char flnm[]);
     42 void save(SphereGorski<float>& sph, char filename[]);   
     43 void save(SphereGorski<double>& sph, char filename[]);   
    4144 void save(LocalMap<double>& locm, char flnm[]);
    4245 void save(const ImageR4& DpcImg,char flnm[]);
     
    4548 void sinus_picture_projection(SphericalMap<float>& sph, char flnm[]);
    4649 void Mollweide_picture_projection(SphericalMap<float>& sph, char flnm[]);
     50 void Mollweide_picture_projection(SphericalMap<double>& sph, char flnm[]);
    4751 void picture(LocalMap<double>& lcm, char flnm[]);
    4852
     
    6064 void planck_read_img(char flnm[],long& naxis,int& n1,int& n2,int& n3,DVList& dvl);
    6165 void planck_read_bntbl(char flnm[], int hdunum, int& npixels, DVList& dvl);
     66 void planck_write_bntbl(char flnm[], int npixels, char typeOfContent[], char extname[], char comment1[], DVList& dvl);
    6267 void write_picture(long* naxes,float* map,char* filename) const;
    6368
    64  void printerror(int status) const;
    65 
     69 void printerror(int& status) const;
     70 void printerror(int& status, char* texte) const;
    6671 bool check_keyword(fitsfile*,int,char keyword[]);
    6772
Note: See TracChangeset for help on using the changeset viewer.