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