| 1 | #include "pexceptions.h" | 
|---|
| 2 | #include "fitsspherehealpix.h" | 
|---|
| 3 | #include "tarray.h" | 
|---|
| 4 | #include "fitstarray.h" | 
|---|
| 5 |  | 
|---|
| 6 |  | 
|---|
| 7 |  | 
|---|
| 8 | /////////////////////////////////////////////////////////////////////// | 
|---|
| 9 | // ----objets delegues  pour la gestion de persistance I/O format fits-- | 
|---|
| 10 | // SphereHealpix | 
|---|
| 11 | //////////////////////////////////////////////////////////////////// | 
|---|
| 12 |  | 
|---|
| 13 | /*! | 
|---|
| 14 | \class SOPHYA::FITS_SphereHEALPix | 
|---|
| 15 | \ingroup FitsIOServer | 
|---|
| 16 | FITS format I/O handler for SOPHYA::SphereHEALPix objects. | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | template <class T> | 
|---|
| 21 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix() | 
|---|
| 22 | { | 
|---|
| 23 | dobj_= new SphereHEALPix<T>; | 
|---|
| 24 | ownobj_= true; | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | template <class T> | 
|---|
| 28 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix(char inputfile[],int hdunum) | 
|---|
| 29 | { | 
|---|
| 30 | dobj_= new SphereHEALPix<T>; | 
|---|
| 31 | ownobj_= true; | 
|---|
| 32 | Read(inputfile,hdunum); | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | template <class T> | 
|---|
| 37 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix(const SphereHEALPix<T>& obj) | 
|---|
| 38 | { | 
|---|
| 39 | dobj_= new SphereHEALPix<T>(obj); | 
|---|
| 40 | ownobj_= true; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | template <class T> | 
|---|
| 44 | FITS_SphereHEALPix<T>::FITS_SphereHEALPix(SphereHEALPix<T>* obj) | 
|---|
| 45 | { | 
|---|
| 46 | dobj_= obj; | 
|---|
| 47 | ownobj_= false; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | template <class T> | 
|---|
| 51 | FITS_SphereHEALPix<T>::~FITS_SphereHEALPix() | 
|---|
| 52 | { | 
|---|
| 53 | if (ownobj_ && dobj_) delete dobj_; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | template <class T> | 
|---|
| 57 | AnyDataObj* FITS_SphereHEALPix<T>::DataObj() | 
|---|
| 58 | { | 
|---|
| 59 | return(dobj_); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | template <class T> | 
|---|
| 63 | void FITS_SphereHEALPix<T>::SetDataObj(AnyDataObj & o) | 
|---|
| 64 | { | 
|---|
| 65 | SphereHEALPix<T> * po = dynamic_cast< SphereHEALPix<T> * >(&o); | 
|---|
| 66 | if (po == NULL) return; | 
|---|
| 67 | if (ownobj_ && dobj_) delete dobj_; | 
|---|
| 68 | dobj_ = po; | 
|---|
| 69 | ownobj_ = false; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 |  | 
|---|
| 74 | template <class T> | 
|---|
| 75 | void FITS_SphereHEALPix<T>::WriteToFits(FitsOutFile& os) | 
|---|
| 76 | { | 
|---|
| 77 | if(dobj_ == NULL) | 
|---|
| 78 | { | 
|---|
| 79 | cout << " WriteToFits:: dobj_= null " << endl; | 
|---|
| 80 | return; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | DVList dvl( dobj_->Info() ); | 
|---|
| 84 |  | 
|---|
| 85 | SphereCoordSys* cs= dobj_->GetCoordSys(); | 
|---|
| 86 | string description= cs->description(); | 
|---|
| 87 | dvl["COORDSYS"]= description; | 
|---|
| 88 |  | 
|---|
| 89 | dvl["PIXTYPE"] = "HEALPIX"; | 
|---|
| 90 | dvl.SetComment("PIXTYPE", "HEALPIX Pixelization"); | 
|---|
| 91 | dvl["ORDERING"]= dobj_->TypeOfMap(); | 
|---|
| 92 | dvl.SetComment("ORDERING", "Pixel ordering scheme, either RING or NESTED"); | 
|---|
| 93 |  | 
|---|
| 94 | int nSide= dobj_->SizeIndex(); | 
|---|
| 95 | dvl["NSIDE"]= (int_4) nSide; | 
|---|
| 96 | dvl.SetComment("NSIDE","Resolution parameter for HEALPIX" ); | 
|---|
| 97 |  | 
|---|
| 98 | int nPix= dobj_->NbPixels(); | 
|---|
| 99 | dvl["FIRSTPIX"]= (int_4) 0; | 
|---|
| 100 | dvl.SetComment("FIRSTPIX", "First pixel # (0 based)"); | 
|---|
| 101 | dvl["LASTPIX"]= (int_4) nPix-1; | 
|---|
| 102 | dvl.SetComment("LASTPIX", "Last pixel # (0 based)"); | 
|---|
| 103 | dvl["Content"]= "SphereHEALPix"; | 
|---|
| 104 | dvl.SetComment("Content", "name of SOPHYA object"); | 
|---|
| 105 |  | 
|---|
| 106 | // On ecrit les dataBlocks | 
|---|
| 107 | vector<string> Noms; | 
|---|
| 108 | Noms.push_back(dvl.GetS("Content")); | 
|---|
| 109 | string extname("SIMULATION"); | 
|---|
| 110 |  | 
|---|
| 111 | string Type; | 
|---|
| 112 | if (typeid(T) == typeid(r_8) ) Type+='D'; | 
|---|
| 113 | else | 
|---|
| 114 | if (typeid(T) == typeid(r_4) )  Type+='E'; | 
|---|
| 115 | else | 
|---|
| 116 | { | 
|---|
| 117 | cout <<  " type de la sphere= " <<  typeid(T).name() << endl; | 
|---|
| 118 | throw IOExc("FITS_SphereHEALPix:: unknown type"); | 
|---|
| 119 | } | 
|---|
| 120 | vector<int> dummy; | 
|---|
| 121 | os.makeHeaderBntblOnFits(Type, Noms, nPix, 1, &dvl, extname, dummy); | 
|---|
| 122 | os.PutColToFits(0,  nPix, dobj_->pixels_.Data()); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | template <class T> | 
|---|
| 126 | void FITS_SphereHEALPix<T>::ReadFromFits(FitsInFile& is) | 
|---|
| 127 | { | 
|---|
| 128 | if(dobj_ == NULL) | 
|---|
| 129 | { | 
|---|
| 130 | dobj_= new SphereHEALPix<T>; | 
|---|
| 131 | ownobj_= true; | 
|---|
| 132 | } | 
|---|
| 133 | int nbcols, nbentries; | 
|---|
| 134 | // | 
|---|
| 135 |  | 
|---|
| 136 | nbcols = is.NbColsFromFits(); | 
|---|
| 137 | if (nbcols != 1) | 
|---|
| 138 | { | 
|---|
| 139 | throw IOExc("le fichier fits n'est pas une sphere Healpix"); | 
|---|
| 140 | } | 
|---|
| 141 | DVList dvl=is.DVListFromFits(); | 
|---|
| 142 | nbentries = is.NentriesFromFits(0); | 
|---|
| 143 | int lastpix=dvl.GetI("LASTPIX"); | 
|---|
| 144 | if (lastpix>0) | 
|---|
| 145 | { | 
|---|
| 146 | if (nbentries!=lastpix+1) | 
|---|
| 147 | { | 
|---|
| 148 | nbentries=lastpix+1; | 
|---|
| 149 | } | 
|---|
| 150 | } | 
|---|
| 151 | // Let's Read the SphereCoordSys object | 
|---|
| 152 | int id= SphereCoordSys_NEUTRAL; | 
|---|
| 153 | string description= "NEUTRAL SphereCoordSystem"; | 
|---|
| 154 | string coordsys= dvl.GetS("COORDSYS"); | 
|---|
| 155 | // $CHECK$ - Reza 2/05/2000 - compare(size_t, size_t,...) passe pas sur g++ | 
|---|
| 156 | //  if(coordsys.compare(0,7,"ROTATION",0,7) == 0)  $CHECK$ | 
|---|
| 157 | if(coordsys.substr(0,8) == "ROTATION") | 
|---|
| 158 | { | 
|---|
| 159 | id= SphereCoordSys_ROTATION; | 
|---|
| 160 | description= "ROTATION SphereCoordSystem"; | 
|---|
| 161 | } | 
|---|
| 162 | //  else if(coordsys.compare(0,4,"OTHER",0,4) == 0) $CHECK$ Reza 2/05/2000 | 
|---|
| 163 | else if(coordsys.substr(0,5)  == "OTHER" ) | 
|---|
| 164 | { | 
|---|
| 165 | id= SphereCoordSys_OTHER; | 
|---|
| 166 | description= "OTHER SphereCoordSystem"; | 
|---|
| 167 | } | 
|---|
| 168 | dobj_->SetCoordSys(new SphereCoordSys(id,description)); | 
|---|
| 169 |  | 
|---|
| 170 | string ordering= dvl.GetS("ORDERING"); | 
|---|
| 171 | //  if(ordering.compare(0,3,"RING",0,3) != 0)  $CHECK$ Reza 2/05/2000 | 
|---|
| 172 | if(ordering.substr(0,4) != "RING" ) | 
|---|
| 173 | { | 
|---|
| 174 | cerr << "FITS_SphereHEALPix/Error Not supported ORDERING= " | 
|---|
| 175 | << ordering << " substr(0,4)=[" << ordering.substr(0,4) << "]" << endl; | 
|---|
| 176 | throw IOExc(" FITS_SphereHEALPix:: numerotation non RING"); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | int nside= dvl.GetI("NSIDE"); | 
|---|
| 180 | int nPix = 0; | 
|---|
| 181 | if(nside <= 0) | 
|---|
| 182 | { | 
|---|
| 183 | //      throw IOExc("FITS_SphereHEALPix:: No resol parameter nside"); | 
|---|
| 184 | if (lastpix>0) | 
|---|
| 185 | { | 
|---|
| 186 | nside = sqrt((double)(nbentries/12)); | 
|---|
| 187 | } | 
|---|
| 188 | else | 
|---|
| 189 | { | 
|---|
| 190 | //      dvl.Print(); | 
|---|
| 191 | // int naxis2 = dvl.GetI("NAXIS2"); | 
|---|
| 192 | // if (naxis2 > 0 ) | 
|---|
| 193 | //  { | 
|---|
| 194 | nside = sqrt((double)(nbentries/12)); | 
|---|
| 195 | // } | 
|---|
| 196 | // else | 
|---|
| 197 | //  throw IOExc("FITS_SphereHEALPix:: No NSIDE nor LASTPIX nor NAXIS2 on file"); | 
|---|
| 198 | } | 
|---|
| 199 | } | 
|---|
| 200 | nPix = 12*nside*nside; | 
|---|
| 201 | if (nPix != nbentries) | 
|---|
| 202 | { | 
|---|
| 203 | cout << "WARNING: le nombre de pixels relu " << nbentries << " est incoherent avec la pixelisation Healpix qui donne nPix= " << nPix << endl; | 
|---|
| 204 | } | 
|---|
| 205 | double Omega= 4.0*Pi/nPix; | 
|---|
| 206 | dobj_->setParameters(nside,nPix,Omega); | 
|---|
| 207 | // On lit les DataBlocks; | 
|---|
| 208 | // | 
|---|
| 209 | dobj_->pixels_.ReSize(nPix); | 
|---|
| 210 | is.GetSingleColumn(dobj_->pixels_.Data(),nPix); | 
|---|
| 211 | // | 
|---|
| 212 |  | 
|---|
| 213 | // on effectue le decoupage en tranches | 
|---|
| 214 | dobj_->SetThetaSlices(); | 
|---|
| 215 | // | 
|---|
| 216 | dobj_->Info()  = is.DVListFromFits(); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | template <class T> | 
|---|
| 220 | void FITS_SphereHEALPix<T>::Mollweide_picture_projection(char filename[]) | 
|---|
| 221 | { | 
|---|
| 222 | int ni = 300; | 
|---|
| 223 | int nj = 600; | 
|---|
| 224 | TMatrix<float> map(ni, nj); | 
|---|
| 225 |  | 
|---|
| 226 | int i,j; | 
|---|
| 227 | for(i=0; i < ni; i++) | 
|---|
| 228 | { | 
|---|
| 229 | double yd = (i+0.5)/ni-0.5; | 
|---|
| 230 | double facteur=2.*Pi/sin(acos(yd*2)); | 
|---|
| 231 | double theta = (0.5-yd)*Pi; | 
|---|
| 232 | for(j=0; j < nj; j++) | 
|---|
| 233 | { | 
|---|
| 234 | double xa = (j+0.5)/nj-0.5; | 
|---|
| 235 | double phi =  xa*facteur+Pi; | 
|---|
| 236 | float th=float(theta); | 
|---|
| 237 | float ff=float(phi); | 
|---|
| 238 | if (phi<2*Pi && phi>= 0) | 
|---|
| 239 | { | 
|---|
| 240 | map(i,j) = (float)dobj_->PixValSph(th, ff); | 
|---|
| 241 | } | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 | FitsOutFile  fout(filename); | 
|---|
| 245 | fout.firstImageOnPrimaryHeader(true); | 
|---|
| 246 | FITS_TArray<r_4> fta(map); | 
|---|
| 247 | fta.Write(fout); | 
|---|
| 248 |  | 
|---|
| 249 | } | 
|---|
| 250 | template <class T> | 
|---|
| 251 | void FITS_SphereHEALPix<T>::sinus_picture_projection(char filename[]) | 
|---|
| 252 | { | 
|---|
| 253 | int ni = 300; | 
|---|
| 254 | int nj = 600; | 
|---|
| 255 | TMatrix<float> map(ni, nj); | 
|---|
| 256 |  | 
|---|
| 257 | int i,j; | 
|---|
| 258 | for(i=0; i < ni; i++) | 
|---|
| 259 | { | 
|---|
| 260 | double yd = (i+0.5)/ni-0.5; | 
|---|
| 261 | double theta = (0.5-yd)*Pi; | 
|---|
| 262 | double facteur=1./sin(theta); | 
|---|
| 263 | for(j=0; j < nj; j++) | 
|---|
| 264 | { | 
|---|
| 265 | double xa = (j+0.5)/nj-0.5; | 
|---|
| 266 | double phi = 2.*Pi*xa*facteur+Pi; | 
|---|
| 267 | float th=float(theta); | 
|---|
| 268 | float ff=float(phi); | 
|---|
| 269 | if (phi<2*Pi && phi>= 0) | 
|---|
| 270 | { | 
|---|
| 271 | map(i,j) = (float)dobj_->PixValSph(th, ff); | 
|---|
| 272 | } | 
|---|
| 273 | } | 
|---|
| 274 | } | 
|---|
| 275 | FitsOutFile  fout(filename); | 
|---|
| 276 | fout.firstImageOnPrimaryHeader(true); | 
|---|
| 277 | FITS_TArray<r_4> fta(map); | 
|---|
| 278 | fta.Write(fout); | 
|---|
| 279 |  | 
|---|
| 280 |  | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 |  | 
|---|
| 284 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
| 285 | #pragma define_template FITS_SphereHEALPix<r_8> | 
|---|
| 286 | #pragma define_template FITS_SphereHEALPix<r_4> | 
|---|
| 287 | #pragma define_template FITS_SphereHEALPix<int_4> | 
|---|
| 288 | #endif | 
|---|
| 289 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES) | 
|---|
| 290 | template class FITS_SphereHEALPix<r_8>; | 
|---|
| 291 | template class FITS_SphereHEALPix<r_4>; | 
|---|
| 292 | template class FITS_SphereHEALPix<int_4>; | 
|---|
| 293 | #endif | 
|---|